aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/Util_ROM.cpp
diff options
context:
space:
mode:
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);
+}
+
}