diff --git a/libultraship/libultraship/ConfigFile.cpp b/libultraship/libultraship/ConfigFile.cpp index 1b49521395e..13c5ca5bdca 100644 --- a/libultraship/libultraship/ConfigFile.cpp +++ b/libultraship/libultraship/ConfigFile.cpp @@ -58,6 +58,9 @@ namespace Ship { (*this)["ARCHIVE"]["Main Archive"] = "oot.otr"; (*this)["ARCHIVE"]["Patches Directory"] = ""; + (*this)["SAVE"]["Save Filename"] = "oot_save.sav"; + (*this)["SAVE"]["Save File Directory"] = ""; + (*this)["CONTROLLERS"]["CONTROLLER 1"] = "Auto"; (*this)["CONTROLLERS"]["CONTROLLER 2"] = "Unplugged"; (*this)["CONTROLLERS"]["CONTROLLER 3"] = "Unplugged"; diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 0732a80e187..55e4d004741 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -328,6 +328,7 @@ extern "C" int ResourceMgr_OTRSigCheck(char* imgData) { uintptr_t i = (uintptr_t)(imgData); + if (i == 0xD9000000 || i == 0xE7000000 || (i & 0xF0000000) == 0xF0000000) return 0; @@ -358,6 +359,7 @@ extern "C" AnimationHeaderCommon* ResourceMgr_LoadAnimByName(char* path) { for (int i = 0; i < res->rotationValues.size(); i++) animNormal->frameData[i] = res->rotationValues[i]; + animNormal->jointIndices = (JointIndex*)malloc(res->rotationIndices.size() * sizeof(Vec3s)); for (int i = 0; i < res->rotationIndices.size(); i++) { @@ -602,6 +604,27 @@ extern "C" s32* ResourceMgr_LoadCSByName(char* path) return (s32*)res->commands.data(); } +/* Remember to free after use of value */ +extern "C" char* Config_getValue(char* category, char* key) +{ + std::shared_ptr pConf = OTRGlobals::Instance->context->GetConfig(); + Ship::ConfigFile& Conf = *pConf.get(); + + std::string data = Conf.get(std::string(category)).get(std::string(key)); + char* retval = (char*)malloc(data.length()+1); + strcpy(retval, data.c_str()); + + return retval; +} + +extern "C" bool Config_setValue(char* category, char* key, char* value) +{ + std::shared_ptr pConf = OTRGlobals::Instance->context->GetConfig(); + Ship::ConfigFile& Conf = *pConf.get(); + Conf[std::string(category)][std::string(key)] = std::string(value); + return Conf.Save(); +} + std::wstring StringToU16(const std::string& s) { std::vector result; size_t i = 0; diff --git a/soh/soh/OTRGlobals.h b/soh/soh/OTRGlobals.h index f7e52583492..680f81c7f1b 100644 --- a/soh/soh/OTRGlobals.h +++ b/soh/soh/OTRGlobals.h @@ -39,6 +39,8 @@ Gfx* ResourceMgr_LoadGfxByName(char* path); Vtx* ResourceMgr_LoadVtxByCRC(uint64_t crc); Vtx* ResourceMgr_LoadVtxByName(char* path); CollisionHeader* ResourceMgr_LoadColByName(char* path); +char* Config_getValue(char* category, char* key); +bool Config_setValue(char* category, char* key, char* value); uint64_t GetPerfCounter(); struct SkeletonHeader* ResourceMgr_LoadSkeletonByName(char* path); int ResourceMgr_OTRSigCheck(char* imgData); diff --git a/soh/src/code/z_ss_sram.c b/soh/src/code/z_ss_sram.c index 6b4ea50ca2d..6865f9ec68d 100644 --- a/soh/src/code/z_ss_sram.c +++ b/soh/src/code/z_ss_sram.c @@ -2,7 +2,15 @@ #include "global.h" #include +#include #include +#include + +#if defined(WIN32) +#define DIR_SEPARATOR '\\' +#else +#define DIR_SEPARATOR '/' +#endif #if 0 typedef struct { @@ -56,15 +64,71 @@ void SsSram_Dma(void* dramAddr, size_t size, s32 direction) { } #endif +//https://stackoverflow.com/a/15019112 +void combineDirectoryAndFileName(char* destination, char* directory, char* fileName) +{ + if(directory && *directory) { + int len = strlen(directory); + strcpy(destination, directory); + + if (destination[len - 1] == DIR_SEPARATOR) + { + if (fileName && *fileName) + { + strcpy(destination + len, (*fileName == DIR_SEPARATOR) ? (fileName + 1) : fileName); + } + } + else + { + if(fileName && *fileName) + { + if (*fileName == DIR_SEPARATOR) + { + strcpy(destination + len, fileName); + } + else + { + destination[len] = DIR_SEPARATOR; + strcpy(destination + len + 1, fileName); + } + } + } + } + else if(fileName && *fileName) + { + strcpy(destination, fileName); + } + else + { + destination[0] = '\0'; + } +} + void SsSram_ReadWrite(uintptr_t addr, void* dramAddr, size_t size, s32 direction) { osSyncPrintf("ssSRAMReadWrite:%08x %08x %08x %d\n", addr, (uintptr_t)dramAddr, size, direction); + + char* directory = Config_getValue("SAVE", "Save File Directory"); + char* fileName = Config_getValue("SAVE", "Save Filename"); + + // This only happens if a user deletes the default filename and doesn't replace it + if(fileName[0] == '\0') + { + Config_setValue("SAVE", "Save Filename", "oot_save.sav"); + free(fileName); + fileName = malloc(sizeof("oot_save.sav")+1); + strcpy(fileName, "oot_save.sav"); + } + + char* file = malloc(strlen(directory) + strlen(fileName) + 2); + combineDirectoryAndFileName(file, directory, fileName); + //Check to see if the file exists FILE* saveFile; - saveFile = fopen("oot_save.sav", "rb"); + saveFile = fopen(file, "rb"); if (saveFile == NULL) { - saveFile = fopen("oot_save.sav", "wb"); + saveFile = fopen(file, "wb"); fseek(saveFile, 0, SEEK_SET); assert(saveFile != NULL); // OTRTODO LOG uint8_t zero = 0; @@ -78,14 +142,14 @@ void SsSram_ReadWrite(uintptr_t addr, void* dramAddr, size_t size, s32 direction } switch (direction) { case OS_WRITE: { - saveFile = fopen("oot_save.sav", "r+b"); + saveFile = fopen(file, "r+b"); rewind(saveFile); fseek(saveFile, addr, SEEK_SET); fwrite(dramAddr, size, 1, saveFile); fclose(saveFile); } break; case OS_READ: { - saveFile = fopen("oot_save.sav", "rb+"); + saveFile = fopen(file, "rb+"); rewind(saveFile); fseek(saveFile, addr, SEEK_SET); fread(dramAddr, size, 1, saveFile); @@ -94,4 +158,8 @@ void SsSram_ReadWrite(uintptr_t addr, void* dramAddr, size_t size, s32 direction } //SsSram_Init(addr, DEVICE_TYPE_SRAM, PI_DOMAIN2, 5, 0xD, 2, 0xC, 0); //SsSram_Dma(dramAddr, size, direction); + + free(file); + free(directory); + free(fileName); }