Skip to content

Commit

Permalink
Added save filename and path to config file (#130)
Browse files Browse the repository at this point in the history
* Added save filename and path to config file

Closes #110

* Moved FileIO to libultraship

* Moved OOT Specific Code to OTRGlobals and made libultraship for general file IO. Combined config options.

* Moved filesystem include into GlobalCtx2.h
  • Loading branch information
linkian209 authored Jun 20, 2022
1 parent 36b9b95 commit 39d8b77
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 29 deletions.
2 changes: 2 additions & 0 deletions libultraship/libultraship/ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace Ship {
(*this)["ARCHIVE"]["Main Archive"] = "oot.otr";
(*this)["ARCHIVE"]["Patches Directory"] = "";

(*this)["SAVE"]["Save Filename"] = "oot_save.sav";

(*this)["CONTROLLERS"]["CONTROLLER 1"] = "Auto";
(*this)["CONTROLLERS"]["CONTROLLER 2"] = "Unplugged";
(*this)["CONTROLLERS"]["CONTROLLER 3"] = "Unplugged";
Expand Down
21 changes: 21 additions & 0 deletions libultraship/libultraship/GlobalCtx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,25 @@ namespace Ship {
std::cout << "Log initialization failed: " << ex.what() << std::endl;
}
}

void GlobalCtx2::WriteSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size) {
std::ofstream saveFile = std::ofstream(savePath, std::fstream::in | std::fstream::out | std::fstream::binary);
saveFile.seekp(addr);
saveFile.write((char*)dramAddr, size);
saveFile.close();
}

void GlobalCtx2::ReadSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size) {
std::ifstream saveFile = std::ifstream(savePath, std::fstream::in | std::fstream::out | std::fstream::binary);

// If the file doesn't exist, initialize DRAM
if (saveFile.good()) {
saveFile.seekg(addr);
saveFile.read((char*)dramAddr, size);
} else {
memset(dramAddr, 0, size);
}

saveFile.close();
}
}
4 changes: 4 additions & 0 deletions libultraship/libultraship/GlobalCtx2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#ifdef __cplusplus
#include <filesystem>
#include <memory>
#include "spdlog/spdlog.h"
#include "ConfigFile.h"
Expand All @@ -23,6 +24,9 @@ namespace Ship {
std::shared_ptr<spdlog::logger> GetLogger() { return Logger; }
std::shared_ptr<ConfigFile> GetConfig() { return Config; }

void WriteSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size);
void ReadSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size);

GlobalCtx2(const std::string& Name);
~GlobalCtx2();

Expand Down
68 changes: 68 additions & 0 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "OTRGlobals.h"
#include "OTRAudio.h"
#include <iostream>
#include <filesystem>
#include <locale>
#include <codecvt>
#include "GlobalCtx2.h"
Expand Down Expand Up @@ -46,6 +47,7 @@ OTRGlobals::OTRGlobals() {
context = Ship::GlobalCtx2::CreateInstance("Ship of Harkinian");
gSaveStateMgr = std::make_shared<SaveStateMgr>();
context->GetWindow()->Init();
CheckSaveFile(SRAM_SIZE);
}

OTRGlobals::~OTRGlobals() {
Expand Down Expand Up @@ -840,6 +842,7 @@ extern "C" AnimationHeaderCommon* ResourceMgr_LoadAnimByName(const 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 @@ -1084,6 +1087,71 @@ extern "C" s32* ResourceMgr_LoadCSByName(const char* path)
return (s32*)res->commands.data();
}

std::filesystem::path GetSaveFile(Ship::ConfigFile& Conf) {
std::string fileName = Conf.get("SAVE").get("Save Filename");

if (fileName.empty()) {
Conf["SAVE"]["Save Filename"] = "oot_save.sav";
Conf.Save();
}
std::filesystem::path saveFile = std::filesystem::absolute(fileName);

if (!std::filesystem::exists(saveFile.parent_path())) {
std::filesystem::create_directories(saveFile.parent_path());
}

return saveFile;
}

std::filesystem::path GetSaveFile() {
std::shared_ptr<Ship::ConfigFile> pConf = OTRGlobals::Instance->context->GetConfig();
Ship::ConfigFile& Conf = *pConf.get();

return GetSaveFile(Conf);
}

void OTRGlobals::CheckSaveFile(size_t sramSize) {
std::shared_ptr<Ship::ConfigFile> pConf = context->GetConfig();
Ship::ConfigFile& Conf = *pConf.get();

std::filesystem::path savePath = GetSaveFile(Conf);
std::fstream saveFile(savePath, std::fstream::in | std::fstream::out | std::fstream::binary);
if (saveFile.fail()) {
saveFile.open(savePath, std::fstream::in | std::fstream::out | std::fstream::binary | std::fstream::app);
for (int i = 0; i < sramSize; ++i) {
saveFile.write("\0", 1);
}
}
saveFile.close();
}

extern "C" void Ctx_ReadSaveFile(uintptr_t addr, void* dramAddr, size_t size) {
OTRGlobals::Instance->context->ReadSaveFile(GetSaveFile(), addr, dramAddr, size);
}

extern "C" void Ctx_WriteSaveFile(uintptr_t addr, void* dramAddr, size_t size) {
OTRGlobals::Instance->context->WriteSaveFile(GetSaveFile(), addr, dramAddr, size);
}

/* 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
8 changes: 7 additions & 1 deletion soh/soh/OTRGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OTRGlobals
~OTRGlobals();

private:

void CheckSaveFile(size_t sramSize);
};
#endif

Expand All @@ -47,11 +47,17 @@ Gfx* ResourceMgr_LoadGfxByCRC(uint64_t crc);
Gfx* ResourceMgr_LoadGfxByName(const char* path);
Gfx* ResourceMgr_PatchGfxByName(const char* path, int size);
Vtx* ResourceMgr_LoadVtxByCRC(uint64_t crc);

Vtx* ResourceMgr_LoadVtxByName(const char* path);
SoundFont* ResourceMgr_LoadAudioSoundFont(const char* path);
SequenceData ResourceMgr_LoadSeqByName(const char* path);
SoundFontSample* ResourceMgr_LoadAudioSample(const char* path);
CollisionHeader* ResourceMgr_LoadColByName(const char* path);
void Ctx_ReadSaveFile(uintptr_t addr, void* dramAddr, size_t size);
void Ctx_WriteSaveFile(uintptr_t addr, void* dramAddr, size_t size);
char* Config_getValue(char* category, char* key);
bool Config_setValue(char* category, char* key, char* value);

uint64_t GetPerfCounter();
struct SkeletonHeader* ResourceMgr_LoadSkeletonByName(const char* path);
int ResourceMgr_OTRSigCheck(char* imgData);
Expand Down
31 changes: 3 additions & 28 deletions soh/src/code/z_ss_sram.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,13 @@ void SsSram_Dma(void* dramAddr, size_t size, s32 direction) {

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);
//Check to see if the file exists
FILE* saveFile;
saveFile = fopen("oot_save.sav", "rb");

if (saveFile == NULL) {

saveFile = fopen("oot_save.sav", "wb");
fseek(saveFile, 0, SEEK_SET);
assert(saveFile != NULL); // OTRTODO LOG
uint8_t zero = 0;

for (uint32_t i = 0; i < SRAM_SIZE; i++) {
fwrite(&zero, 1, 1, saveFile);
}
fclose(saveFile);
} else {
fclose(saveFile);
}

switch (direction) {
case OS_WRITE: {
saveFile = fopen("oot_save.sav", "r+b");
rewind(saveFile);
fseek(saveFile, addr, SEEK_SET);
fwrite(dramAddr, size, 1, saveFile);
fclose(saveFile);
Ctx_WriteSaveFile(addr, dramAddr, size);
} break;
case OS_READ: {
saveFile = fopen("oot_save.sav", "rb+");
rewind(saveFile);
fseek(saveFile, addr, SEEK_SET);
fread(dramAddr, size, 1, saveFile);
fclose(saveFile);
Ctx_ReadSaveFile(addr, dramAddr, size);
} break;
}
//SsSram_Init(addr, DEVICE_TYPE_SRAM, PI_DOMAIN2, 5, 0xD, 2, 0xC, 0);
Expand Down

0 comments on commit 39d8b77

Please sign in to comment.