Prevent mod cache from MD5'ing every file on every boot

This commit is contained in:
MysterD 2022-05-07 17:03:35 -07:00
parent 9f410ad161
commit dec1fb9665
3 changed files with 22 additions and 4 deletions

View File

@ -103,7 +103,7 @@ void mod_activate(struct Mod* mod) {
// activate dynos models // activate dynos models
for (int i = 0; i < mod->fileCount; i++) { for (int i = 0; i < mod->fileCount; i++) {
struct ModFile* file = &mod->files[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")) { if (str_ends_with(file->relativePath, ".bin")) {
mod_activate_bin(file); mod_activate_bin(file);
} }
@ -567,7 +567,7 @@ bool mod_load(struct Mods* mods, char* basePath, char* modName) {
if (isDirectory) { if (isDirectory) {
for (int i = 0; i < mod->fileCount; i++) { for (int i = 0; i < mod->fileCount; i++) {
struct ModFile* file = &mod->files[i]; struct ModFile* file = &mod->files[i];
mod_cache_add(mod, file); mod_cache_add(mod, file, true);
LOG_INFO(" - %s", file->relativePath); LOG_INFO(" - %s", file->relativePath);
} }
} }

View File

@ -102,6 +102,19 @@ struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash) {
return NULL; 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) { struct ModCacheEntry* mod_cache_get_from_path(const char* path) {
if (path == NULL || strlen(path) == 0) { return NULL; } if (path == NULL || strlen(path) == 0) { return NULL; }
struct ModCacheEntry* node = sModCacheHead; 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?"); 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 // sanity check
if (mod == NULL || file == NULL) { if (mod == NULL || file == NULL) {
LOG_ERROR("Could not add to cache, mod or file is 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); normalize_path(modFilePath);
file->cachedPath = strdup(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 // hash and cache
mod_cache_md5(file->cachedPath, file->dataHash); mod_cache_md5(file->cachedPath, file->dataHash);
mod_cache_add_internal(file->dataHash, 0, strdup(file->cachedPath)); mod_cache_add_internal(file->dataHash, 0, strdup(file->cachedPath));

View File

@ -13,7 +13,7 @@ struct ModCacheEntry {
void mod_cache_shutdown(void); void mod_cache_shutdown(void);
struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash); struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash);
struct ModCacheEntry* mod_cache_get_from_path(const char* path); 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_load(void);
void mod_cache_save(void); void mod_cache_save(void);