#include #include #include #include #include #include #include "save.h" #include "ui.h" #include "map.h" gameDataSav GAMEDATASAV; const u32 KOROK_COUNTER_HASH = 0x8a94e07a; void openSave() { GAMEDATASAV.saveFile = NULL; FILE *fp = fopen("save:/5/game_data.sav", "rb"); //TODO: Dont hardcode save slot if (fp != NULL) { /* Go to the end of the file. */ if (fseek(fp, 0L, SEEK_END) == 0) { /* Get the size of the file. */ long bufsize = ftell(fp); if (bufsize == -1) { /* Error */ } /* Allocate our buffer to that size. */ GAMEDATASAV.saveFile = malloc(sizeof(char) * (bufsize)); /* Go back to the start of the file. */ if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ } /* Read the entire file into memory. */ GAMEDATASAV.length = fread(GAMEDATASAV.saveFile, sizeof(char), bufsize, fp); if (ferror( fp ) != 0) { printMessage("Error reading file."); } else { printMessage("Read %.2f kbytes successfully.", GAMEDATASAV.length/1024.0); } } fclose(fp); } else { printMessage("Error opening file."); } } void processSave() { countMapPins(); printMessage("number of korok seeds: %d.", readU32FromHash(KOROK_COUNTER_HASH)); loadPlayerLocation(); } u32 getSaveSize() { return GAMEDATASAV.length; } void writeSave() { FILE *fp = fopen("save:/5/game_data.sav", "wb"); //TODO: dont hardcode save slot if (fp != NULL) { size_t bytes_writen = fwrite(GAMEDATASAV.saveFile, sizeof(char), GAMEDATASAV.length, fp); if (ferror( fp ) != 0) { printMessage("Error writing file."); } else { printMessage("Wrote %f.2 kbytes successfully.", bytes_writen/1024.0); } fclose(fp); } else { printMessage("Error opening file for writing."); } } u32 _searchHash(u32 hash) { for(u32 i=0x0c; i> 8; GAMEDATASAV.saveFile[addr+6] = (value & 0x00ff0000) >> 16; GAMEDATASAV.saveFile[addr+7] = (value & 0xff000000) >> 24; //printMessage("wrote u32 0x%x to 0x%x, read check: 0x%x", value, addr, readU32FromAddr(addr)); } } void closeAndCommitSave() { writeSave(); if (R_FAILED(fsdevCommitDevice("save"))) { printMessage("Committing failed."); } free(GAMEDATASAV.saveFile); }