From dec1fb96651090c0edbe2b5e2c2714b6ffed3070 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 7 May 2022 17:03:35 -0700 Subject: [PATCH] Prevent mod cache from MD5'ing every file on every boot --- src/pc/mods/mod.c | 4 ++-- src/pc/mods/mod_cache.c | 20 +++++++++++++++++++- src/pc/mods/mod_cache.h | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/pc/mods/mod.c b/src/pc/mods/mod.c index 9667387a..3a9c00c6 100644 --- a/src/pc/mods/mod.c +++ b/src/pc/mods/mod.c @@ -103,7 +103,7 @@ void mod_activate(struct Mod* mod) { // activate dynos models for (int i = 0; i < mod->fileCount; i++) { struct ModFile* file = &mod->files[i]; - mod_cache_add(mod, file); + mod_cache_add(mod, file, false); if (str_ends_with(file->relativePath, ".bin")) { mod_activate_bin(file); } @@ -567,7 +567,7 @@ bool mod_load(struct Mods* mods, char* basePath, char* modName) { if (isDirectory) { for (int i = 0; i < mod->fileCount; i++) { struct ModFile* file = &mod->files[i]; - mod_cache_add(mod, file); + mod_cache_add(mod, file, true); LOG_INFO(" - %s", file->relativePath); } } diff --git a/src/pc/mods/mod_cache.c b/src/pc/mods/mod_cache.c index f1000ae7..c586fe07 100644 --- a/src/pc/mods/mod_cache.c +++ b/src/pc/mods/mod_cache.c @@ -102,6 +102,19 @@ struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash) { return NULL; } +static bool mod_cache_has_path(const char* path) { + if (path == NULL || strlen(path) == 0) { return NULL; } + struct ModCacheEntry* node = sModCacheHead; + while (node != NULL) { + struct ModCacheEntry* next = node->next; + if (!strcmp(node->path, path)) { + return true; + } + node = next; + } + return false; +} + struct ModCacheEntry* mod_cache_get_from_path(const char* path) { if (path == NULL || strlen(path) == 0) { return NULL; } struct ModCacheEntry* node = sModCacheHead; @@ -183,7 +196,7 @@ void mod_cache_add_internal(u8* dataHash, u64 lastLoaded, const char* path) { LOG_ERROR("Did not add node for some reason?"); } -void mod_cache_add(struct Mod* mod, struct ModFile* file) { +void mod_cache_add(struct Mod* mod, struct ModFile* file, bool useFilePath) { // sanity check if (mod == NULL || file == NULL) { LOG_ERROR("Could not add to cache, mod or file is null"); @@ -206,6 +219,11 @@ void mod_cache_add(struct Mod* mod, struct ModFile* file) { normalize_path(modFilePath); file->cachedPath = strdup(modFilePath); + // if we already have the filepath, don't MD5 it again + if (useFilePath && mod_cache_has_path(file->cachedPath)) { + return; + } + // hash and cache mod_cache_md5(file->cachedPath, file->dataHash); mod_cache_add_internal(file->dataHash, 0, strdup(file->cachedPath)); diff --git a/src/pc/mods/mod_cache.h b/src/pc/mods/mod_cache.h index 78d42c86..a9d20bcd 100644 --- a/src/pc/mods/mod_cache.h +++ b/src/pc/mods/mod_cache.h @@ -13,7 +13,7 @@ struct ModCacheEntry { void mod_cache_shutdown(void); struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash); struct ModCacheEntry* mod_cache_get_from_path(const char* path); -void mod_cache_add(struct Mod* mod, struct ModFile* modFile); +void mod_cache_add(struct Mod* mod, struct ModFile* modFile, bool useFilePath); void mod_cache_load(void); void mod_cache_save(void);