aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/Util_ROM.cpp
diff options
context:
space:
mode:
authorWaluigiWare64 <68647953+WaluigiWare64@users.noreply.github.com>2020-10-23 00:39:29 +0100
committerGitHub <noreply@github.com>2020-10-23 00:39:29 +0100
commit8d70d0926c6307368398a894cbebfbdc0f538194 (patch)
tree72774a626ff8a30850c36eba33fdac38962739cb /src/frontend/Util_ROM.cpp
parenta8851a51f19577f153a3fa5d1021be5794f0921a (diff)
parent65be1840f02a7499fa08178abcefddfefec6d9b0 (diff)
Merge branch 'master' into feature/zip-support
Diffstat (limited to 'src/frontend/Util_ROM.cpp')
-rw-r--r--src/frontend/Util_ROM.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/frontend/Util_ROM.cpp b/src/frontend/Util_ROM.cpp
index 8116a93..f61c3e3 100644
--- a/src/frontend/Util_ROM.cpp
+++ b/src/frontend/Util_ROM.cpp
@@ -27,6 +27,8 @@
#include "NDS.h"
#include "GBACart.h"
+#include "AREngine.h"
+
namespace Frontend
{
@@ -37,6 +39,9 @@ char PrevSRAMPath[ROMSlot_MAX][1024]; // for savestate 'undo load'
bool SavestateLoaded;
+ARCodeFile* CheatFile;
+bool CheatsOn;
+
void Init_ROM()
{
@@ -48,6 +53,18 @@ void Init_ROM()
memset(SRAMPath[ROMSlot_GBA], 0, 1024);
memset(PrevSRAMPath[ROMSlot_NDS], 0, 1024);
memset(PrevSRAMPath[ROMSlot_GBA], 0, 1024);
+
+ CheatFile = nullptr;
+ CheatsOn = false;
+}
+
+void DeInit_ROM()
+{
+ if (CheatFile)
+ {
+ delete CheatFile;
+ CheatFile = nullptr;
+ }
}
// TODO: currently, when failing to load a ROM for whatever reason, we attempt
@@ -198,6 +215,32 @@ int VerifyDSiNAND()
return Load_OK;
}
+void LoadCheats()
+{
+ if (CheatFile)
+ {
+ delete CheatFile;
+ CheatFile = nullptr;
+ }
+
+ char filename[1024];
+ if (ROMPath[ROMSlot_NDS][0] != '\0')
+ {
+ strncpy(filename, ROMPath[ROMSlot_NDS], 1023);
+ filename[1023] = '\0';
+ strncpy(filename + strlen(ROMPath[ROMSlot_NDS]) - 3, "mch", 3);
+ }
+ else
+ {
+ strncpy(filename, "firmware.mch", 1023);
+ }
+
+ // TODO: check for error (malformed cheat file, ...)
+ CheatFile = new ARCodeFile(filename);
+
+ AREngine::SetCodeFile(CheatsOn ? CheatFile : nullptr);
+}
+
int LoadBIOS()
{
int res;
@@ -235,6 +278,8 @@ int LoadBIOS()
SavestateLoaded = false;
+ LoadCheats();
+
return Load_OK;
}
@@ -295,6 +340,8 @@ int LoadROM(const char* file, int slot)
{
SavestateLoaded = false;
+ LoadCheats();
+
// Reload the inserted GBA cartridge (if any)
// TODO: report failure there??
if (ROMPath[ROMSlot_GBA][0] != '\0') NDS::LoadGBAROM(ROMPath[ROMSlot_GBA], SRAMPath[ROMSlot_GBA]);
@@ -387,6 +434,8 @@ int Reset()
return Load_ROMLoadError;
}
+ LoadCheats();
+
return Load_OK;
}
@@ -539,4 +588,26 @@ void UndoStateLoad()
}
}
+int ImportSRAM(const char* filename)
+{
+ FILE* file = fopen(filename, "rb");
+ fseek(file, 0, SEEK_END);
+ u32 size = ftell(file);
+ u8* importData = new u8[size];
+ rewind(file);
+ fread(importData, size, 1, file);
+ fclose(file);
+
+ int diff = NDS::ImportSRAM(importData, size);
+ delete[] importData;
+ return diff;
+}
+
+void EnableCheats(bool enable)
+{
+ CheatsOn = enable;
+ if (CheatFile)
+ AREngine::SetCodeFile(CheatsOn ? CheatFile : nullptr);
+}
+
}