From d08507edbb36073a0180f0c243682395ff8aa19b Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 16 Apr 2022 21:21:31 -0700 Subject: [PATCH] Yet another mod cache fix --- src/pc/lua/utils/smlua_audio_utils.c | 2 +- src/pc/mods/mod_cache.c | 16 ++++----------- src/pc/mods/mods.c | 8 +++++--- src/pc/mods/mods_utils.c | 29 ++++++++++++++++++++++++++++ src/pc/mods/mods_utils.h | 1 + 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/pc/lua/utils/smlua_audio_utils.c b/src/pc/lua/utils/smlua_audio_utils.c index a91c2e2af..9e832c7e9 100644 --- a/src/pc/lua/utils/smlua_audio_utils.c +++ b/src/pc/lua/utils/smlua_audio_utils.c @@ -107,7 +107,7 @@ void smlua_audio_utils_replace_sequence(u8 sequenceId, u8 bankId, u8 defaultVolu for (s32 i = 0; i < gLuaActiveMod->fileCount; i++) { struct ModFile* file = &gLuaActiveMod->files[i]; - if (!strcmp(file->relativePath, m64path)) { + if (str_ends_with(file->cachedPath, m64path)) { struct AudioOverride* override = &sAudioOverrides[sequenceId]; smlua_audio_utils_reset(override); LOG_INFO("Loading audio: %s", file->cachedPath); diff --git a/src/pc/mods/mod_cache.c b/src/pc/mods/mod_cache.c index a49f4dccd..0459066b7 100644 --- a/src/pc/mods/mod_cache.c +++ b/src/pc/mods/mod_cache.c @@ -8,7 +8,7 @@ #include "pc/utils/md5.h" #define MOD_CACHE_FILENAME "mod.cache" -#define MOD_CACHE_VERSION 4 +#define MOD_CACHE_VERSION 5 #define MD5_BUFFER_SIZE 1024 struct ModCacheEntry* sModCacheHead = NULL; @@ -138,17 +138,9 @@ void mod_cache_add(struct Mod* mod, struct ModFile* file) { return; } - if (modFilePath[0] == '.' && (modFilePath[1] == '/' || modFilePath[1] == '\\')) { - char modAbsFilePath[SYS_MAX_PATH] = { 0 }; - getcwd(modAbsFilePath, SYS_MAX_PATH-1); - strncat(modAbsFilePath, "/", SYS_MAX_PATH-1); - strncat(modAbsFilePath, modFilePath, SYS_MAX_PATH-1); - normalize_path(modAbsFilePath); - file->cachedPath = strdup(modAbsFilePath); - } else { - normalize_path(modFilePath); - file->cachedPath = strdup(modFilePath); - } + // set path + normalize_path(modFilePath); + file->cachedPath = strdup(modFilePath); // hash and cache mod_cache_md5(file->cachedPath, file->dataHash); diff --git a/src/pc/mods/mods.c b/src/pc/mods/mods.c index d16568ef1..8db1ef2d7 100644 --- a/src/pc/mods/mods.c +++ b/src/pc/mods/mods.c @@ -156,10 +156,12 @@ void mods_init(void) { // load mods if (hasUserPath) { mods_load(&gLocalMods, userModPath); } + + const char* exePath = path_to_executable(); char defaultModsPath[SYS_MAX_PATH] = { 0 }; - if (snprintf(defaultModsPath, SYS_MAX_PATH - 1, "%s", "./" MOD_DIRECTORY) >= 0) { - mods_load(&gLocalMods, defaultModsPath); - } + path_get_folder((char*)exePath, defaultModsPath); + strncat(defaultModsPath, MOD_DIRECTORY, SYS_MAX_PATH-1); + mods_load(&gLocalMods, defaultModsPath); // calculate total size gLocalMods.size = 0; diff --git a/src/pc/mods/mods_utils.c b/src/pc/mods/mods_utils.c index 500b87675..a1933b2d7 100644 --- a/src/pc/mods/mods_utils.c +++ b/src/pc/mods/mods_utils.c @@ -4,6 +4,13 @@ #include "mods_utils.h" #include "pc/debuglog.h" +#if defined(_WIN32) || defined(_WIN64) +#include +#include +#else +#include +#endif + void mods_size_enforce(struct Mods* mods) { for (int i = 0; i < mods->entryCount; i++) { struct Mod* mod = mods->entries[i]; @@ -156,6 +163,28 @@ char* extract_lua_field(char* fieldName, char* buffer) { ////////////////////////////////////////////////////////////////////////////////////////// +const char* path_to_executable(void) { + static char exePath[SYS_MAX_PATH] = { 0 }; + if (exePath[0] != '\0') { return exePath; } + +#if defined(_WIN32) || defined(_WIN64) + HMODULE hModule = GetModuleHandle(NULL); + if (hModule == NULL) { + LOG_ERROR("unable to retrieve absolute exe path!"); + return NULL; + } + GetModuleFileName(hModule, exePath, SYS_MAX_PATH-1); +#else + snprintf(exePath, MAX_LAUNCH_CMD - 1, "/proc/%d/exe", getpid()); + rc = readlink(exePath, cmd, MAX_LAUNCH_CMD - 1); + if (rc <= 0) { + LOG_ERROR("unable to retrieve absolute exe path!"); + return NULL; + } +#endif + return exePath; +} + bool path_is_portable_filename(char* string) { char* s = string; while (*s != '\0') { diff --git a/src/pc/mods/mods_utils.h b/src/pc/mods/mods_utils.h index 82549f985..529e1dad0 100644 --- a/src/pc/mods/mods_utils.h +++ b/src/pc/mods/mods_utils.h @@ -15,6 +15,7 @@ bool str_ends_with(char* string, char* suffix); char* extract_lua_field(char* fieldName, char* buffer); +const char* path_to_executable(void); bool path_is_portable_filename(char* string); bool path_exists(char* path); bool is_directory(char* path);