From 2a5c3a351eeb1772306567969fb8dc5b31eaf34e Mon Sep 17 00:00:00 2001 From: fgsfds Date: Mon, 2 Sep 2024 10:48:54 +0200 Subject: [PATCH] port: refactor gbcRomCheck a bit --- port/include/romdata.h | 2 +- port/src/main.c | 3 ++- port/src/romdata.c | 53 +++++++++++++++++++++++------------------- src/game/pak.c | 4 ++-- src/include/bss.h | 2 +- 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/port/include/romdata.h b/port/include/romdata.h index d72c411ed..07e83ee4e 100644 --- a/port/include/romdata.h +++ b/port/include/romdata.h @@ -22,6 +22,6 @@ u8 *romdataSegGetData(const char *segName); u8 *romdataSegGetDataEnd(const char *segName); u32 romdataSegGetSize(const char *segName); -void gbcRomCheck(void); +s32 romdataCheckGbcRom(void); #endif diff --git a/port/src/main.c b/port/src/main.c index fb225e6f6..5e404426d 100644 --- a/port/src/main.c +++ b/port/src/main.c @@ -107,7 +107,8 @@ int main(int argc, const char **argv) inputInit(); audioInit(); romdataInit(); - gbcRomCheck(); + + g_ValidGbcRomFound = romdataCheckGbcRom(); gameInit(); diff --git a/port/src/romdata.c b/port/src/romdata.c index 20fc55557..ade6a0f77 100644 --- a/port/src/romdata.c +++ b/port/src/romdata.c @@ -11,9 +11,6 @@ #include "system.h" #include "preprocess.h" #include "platform.h" -#ifndef PLATFORM_N64 -#include "bss.h" -#endif /** * asset files and ROM segments can be replaced by optional external files, @@ -392,43 +389,51 @@ s32 romdataInit(void) return 0; } -void gbcRomCheck(void) +static inline bool romdataCheckGbcRomContents(const u8 *gbcRomFile, const u32 gbcRomSize) { - u32 gbcRomSize; - u8* gbcRomFile = fsFileLoad(GBC_ROM_NAME, &gbcRomSize); - - if (!gbcRomFile) { - return; - } - if (gbcRomSize != GBC_ROM_SIZE) { - free(gbcRomFile); - return; + return false; } // ROM title - if (memcmp(gbcRomFile + 0x134, "PerfDark VPDE", 15) != 0) { - free(gbcRomFile); - return; + return false; } // Licensee code - if (memcmp(gbcRomFile + 0x144, "4Y", 2) != 0) { - free(gbcRomFile); - return; + return false; } // Header and global checksums - if (gbcRomFile[0x14D] != 0xA1 || gbcRomFile[0x14E] != 0xAD || gbcRomFile[0x14F] != 0x0F) { - free(gbcRomFile); - return; + return false; } - g_validGbcRomFound = true; - free(gbcRomFile); + return true; +} + +s32 romdataCheckGbcRom(void) +{ + if (fsFileSize(GBC_ROM_NAME) < 0) { + // bail early if it doesn't exist to avoid generating error messages + return false; + } + + u32 gbcRomSize = 0; + u8 *gbcRomFile = fsFileLoad(GBC_ROM_NAME, &gbcRomSize); + if (!gbcRomFile) { + return false; + } + + const bool ret = romdataCheckGbcRomContents(gbcRomFile, gbcRomSize); + sysMemFree(gbcRomFile); + + if (ret) { + sysLogPrintf(LOG_NOTE, "romdataCheckGbcRom: valid GBC rom found"); + } + + return ret; } s32 romdataFileGetSize(s32 fileNum) diff --git a/src/game/pak.c b/src/game/pak.c index 09c2fef7d..5747f74ee 100644 --- a/src/game/pak.c +++ b/src/game/pak.c @@ -330,7 +330,7 @@ bool var80075d14 = true; #endif #ifndef PLATFORM_N64 -bool g_validGbcRomFound = false; +bool g_ValidGbcRomFound = false; #endif u32 pakGetBlockSize(s8 device) @@ -5997,7 +5997,7 @@ bool gbpakIsAnyPerfectDark(void) return false; #else - return g_validGbcRomFound; + return g_ValidGbcRomFound; #endif } diff --git a/src/include/bss.h b/src/include/bss.h index a322a9a50..a5046aa05 100644 --- a/src/include/bss.h +++ b/src/include/bss.h @@ -294,7 +294,7 @@ extern struct chrdata *g_MpBotChrPtrs[MAX_BOTS]; extern s32 g_JpnMaxCacheItems; extern s32 var8009d370jf; #ifndef PLATFORM_N64 -extern bool g_validGbcRomFound; +extern bool g_ValidGbcRomFound; #endif #endif