From 1a88151eeee0eb3f2ee7a38ad38f72634a1a5e1b Mon Sep 17 00:00:00 2001 From: lifehackerhansol Date: Sat, 5 Oct 2024 20:15:30 -0700 Subject: [PATCH] cheatwnd: add utilities for exporting cheat data to a file - Use a u32 vector instead of std::string - Use that vector to allow exporting to file Co-authored-by: Pk11 --- arm9/source/cheatwnd.cpp | 27 ++++++++++++++++++++++----- arm9/source/cheatwnd.h | 7 ++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/arm9/source/cheatwnd.cpp b/arm9/source/cheatwnd.cpp index 0f3ada5..7a21ca8 100644 --- a/arm9/source/cheatwnd.cpp +++ b/arm9/source/cheatwnd.cpp @@ -395,11 +395,8 @@ bool cCheatWnd::parseInternal(FILE* aDat, u32 gamecode, u32 crc32) { flagItem | ((*ccode & 0xff000000) ? selectValue : 0), dataPos + (((char*)ccode + 3) - buffer))); if ((*ccode & 0xff000000) && (flagItem & cParsedItem::EOne)) selectValue = 0; - for (size_t jj = 0; jj < cheatDataLen; ++jj) { - _data.back()._cheat += formatString("%08X", *(cheatData + jj)); - _data.back()._cheat += ((jj + 1) % 2) ? " " : "\n"; - } - if (cheatDataLen % 2) _data.back()._cheat += "\n"; + _data.back()._cheat.resize(cheatDataLen); + memcpy(_data.back()._cheat.data(), cheatData, cheatDataLen * 4); } cc++; ccode = (u32*)((u32)ccode + (((*ccode & 0x00ffffff) + 1) * 4)); @@ -442,3 +439,23 @@ void cCheatWnd::deselectFolder(size_t anIndex) { ++itr; } } + +std::vector cCheatWnd::getCheats() { + std::vector cheats; + for (uint i = 0; i < _data.size(); i++) { + if (_data[i]._flags & cParsedItem::ESelected) { + cheats.insert(cheats.end(), _data[i]._cheat.begin(), _data[i]._cheat.end()); + } + } + return cheats; +} + +void cCheatWnd::writeCheatsToFile(const char* path) { + FILE* file = fopen(path, "wb"); + if (file) { + std::vector cheats(getCheats()); + fwrite(cheats.data(), 4, cheats.size(), file); + fwrite("\0\0\0\xCF", 4, 1, file); + fclose(file); + } +} diff --git a/arm9/source/cheatwnd.h b/arm9/source/cheatwnd.h index 65e7040..e685a1b 100644 --- a/arm9/source/cheatwnd.h +++ b/arm9/source/cheatwnd.h @@ -7,6 +7,9 @@ #pragma once +#include +#include + #include "button.h" #include "form.h" #include "formdesc.h" @@ -20,6 +23,8 @@ class cCheatWnd : public akui::cForm { bool parse(const std::string& aFileName); static bool searchCheatData(FILE* aDat, u32 gamecode, u32 crc32, long& aPos, size_t& aSize); static bool romData(const std::string& aFileName, u32& aGameCode, u32& aCrc32); + std::vector getCheats(); + void writeCheatsToFile(const char* path); protected: void draw(); @@ -58,7 +63,7 @@ class cCheatWnd : public akui::cForm { public: std::string _title; std::string _comment; - std::string _cheat; + std::vector _cheat; u32 _flags; u32 _offset; cParsedItem(const std::string& title, const std::string& comment, u32 flags, u32 offset = 0)