Skip to content

Commit

Permalink
Added customizeable save name and path to ini
Browse files Browse the repository at this point in the history
Also added functions that allow soh to interact with the current
context's config with the Config_getValue and Config_setValue function.

Closes HarbourMasters#110
  • Loading branch information
linkian209 committed Apr 1, 2022
1 parent e5976c4 commit 6705ca4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
23 changes: 23 additions & 0 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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<Ship::ConfigFile> 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<Ship::ConfigFile> 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<unsigned long> result;
size_t i = 0;
Expand Down
2 changes: 2 additions & 0 deletions soh/soh/OTRGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 72 additions & 4 deletions soh/src/code/z_ss_sram.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
#include "global.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#if defined(WIN32)
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif

#if 0
typedef struct {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}

0 comments on commit 6705ca4

Please sign in to comment.