This commit is contained in:
SimoneN64
2024-05-15 23:11:23 +02:00
parent 45d44aec9d
commit 4046b4d65d
3 changed files with 18 additions and 18 deletions

View File

@@ -45,18 +45,16 @@ void Mem::LoadSRAM(SaveType save_type, fs::path path) {
saveData.unmap(); saveData.unmap();
} }
FILE *f = fopen(sramPath.c_str(), "rb"); auto sramVec = Util::ReadFileBinary(sramPath);
if (!f) { if(sramVec.empty()) {
Util::panic("Could not open {}", sramPath); Util::WriteFileBinary(std::array<u8, SRAM_SIZE>{}, sramPath);
sramVec = Util::ReadFileBinary(sramPath);
} }
fseek(f, 0, SEEK_END); if (sramVec.size() != SRAM_SIZE) {
size_t actualSize = ftell(f);
fseek(f, 0, SEEK_SET);
if (actualSize != SRAM_SIZE) {
Util::panic("Corrupt SRAM!"); Util::panic("Corrupt SRAM!");
} }
fclose(f);
saveData = mio::make_mmap_sink( saveData = mio::make_mmap_sink(
sramPath, 0, mio::map_entire_file, error); sramPath, 0, mio::map_entire_file, error);
if (error) { Util::panic("Could not mmap {}", sramPath); } if (error) { Util::panic("Could not mmap {}", sramPath); }

View File

@@ -20,18 +20,17 @@ void Flash::Load(SaveType saveType, const std::string& path) {
saveData.unmap(); saveData.unmap();
} }
FILE *f = fopen(flashPath.c_str(), "rb"); auto flashVec = Util::ReadFileBinary(flashPath);
if (!f) { if(flashVec.empty()) {
Util::panic("Could not open {}", flashPath); std::vector<u8> dummy{};
dummy.resize(FLASH_SIZE);
Util::WriteFileBinary(dummy, flashPath);
flashVec = Util::ReadFileBinary(flashPath);
} }
fseek(f, 0, SEEK_END); if (flashVec.size() != FLASH_SIZE) {
size_t actualSize = ftell(f); Util::panic("Corrupt SRAM!");
fseek(f, 0, SEEK_SET);
if (actualSize != FLASH_SIZE) {
Util::panic("Corrupt flash!");
} }
fclose(f);
saveData = mio::make_mmap_sink( saveData = mio::make_mmap_sink(
flashPath, 0, mio::map_entire_file, error); flashPath, 0, mio::map_entire_file, error);

View File

@@ -39,8 +39,10 @@ void PIF::MaybeLoadMempak() {
} }
auto mempakVec = Util::ReadFileBinary(mempakPath); auto mempakVec = Util::ReadFileBinary(mempakPath);
if(mempak.empty()) if(mempak.empty()) {
Util::WriteFileBinary(std::array<u8, MEMPAK_SIZE>{}, mempakPath); Util::WriteFileBinary(std::array<u8, MEMPAK_SIZE>{}, mempakPath);
mempakVec = Util::ReadFileBinary(mempakPath);
}
if (mempakVec.size() != MEMPAK_SIZE) { if (mempakVec.size() != MEMPAK_SIZE) {
Util::panic("Corrupt mempak!"); Util::panic("Corrupt mempak!");
@@ -87,6 +89,7 @@ void PIF::LoadEeprom(SaveType saveType, const std::string& path) {
std::vector<u8> dummy{}; std::vector<u8> dummy{};
dummy.resize(GetSaveSize(saveType)); dummy.resize(GetSaveSize(saveType));
Util::WriteFileBinary(dummy, eepromPath); Util::WriteFileBinary(dummy, eepromPath);
eepromVec = Util::ReadFileBinary(eepromPath);
} }
if (eepromVec.size() != eepromSize) { if (eepromVec.size() != eepromSize) {