Cheat System: Minor code cleanup and robustness improvements.

This commit is contained in:
rogerman 2023-06-30 15:13:35 -07:00
parent f48aee928c
commit 7fb5a5871b
2 changed files with 34 additions and 21 deletions

View File

@ -54,10 +54,15 @@ const char* CHEATS::getFilePath() const
} }
void CHEATS::setFilePath(const char *thePath) void CHEATS::setFilePath(const char *thePath)
{
memset(this->_filename, '\0', sizeof(this->_filename));
if (thePath != NULL)
{ {
strncpy((char *)this->_filename, thePath, sizeof(this->_filename)); strncpy((char *)this->_filename, thePath, sizeof(this->_filename));
this->_filename[ sizeof(this->_filename) - 1 ] = '\0'; this->_filename[ sizeof(this->_filename) - 1 ] = '\0';
} }
}
size_t CHEATS::addItem(const CHEATS_LIST &srcCheat) size_t CHEATS::addItem(const CHEATS_LIST &srcCheat)
{ {
@ -165,7 +170,7 @@ static void CheatWrite(int size, int proc, u32 addr, u32 val)
} }
void CHEATS::ARparser(CHEATS_LIST& theList) void CHEATS::ARparser(const CHEATS_LIST &theList)
{ {
//primary organizational source (seems to be referenced by cheaters the most) - http://doc.kodewerx.org/hacking_nds.html //primary organizational source (seems to be referenced by cheaters the most) - http://doc.kodewerx.org/hacking_nds.html
//secondary clarification and details (for programmers) - http://problemkaputt.de/gbatek.htm#dscartcheatactionreplayds //secondary clarification and details (for programmers) - http://problemkaputt.de/gbatek.htm#dscartcheatactionreplayds
@ -868,7 +873,7 @@ CHEATS_LIST* CHEATS::getListPtr()
bool CHEATS::copyItemFromIndex(const size_t pos, CHEATS_LIST &outCheatItem) bool CHEATS::copyItemFromIndex(const size_t pos, CHEATS_LIST &outCheatItem)
{ {
CHEATS_LIST *itemPtr = this->getItemPtrAtIndex(pos); const CHEATS_LIST *itemPtr = this->getItemPtrAtIndex(pos);
if (itemPtr == NULL) if (itemPtr == NULL)
{ {
return false; return false;
@ -879,22 +884,23 @@ bool CHEATS::copyItemFromIndex(const size_t pos, CHEATS_LIST &outCheatItem)
return true; return true;
} }
CHEATS_LIST* CHEATS::getItemPtrAtIndex(const size_t pos) CHEATS_LIST* CHEATS::getItemPtrAtIndex(const size_t pos) const
{ {
if (pos >= this->getListSize()) if (pos >= this->getListSize())
{ {
return NULL; return NULL;
} }
return &this->_list[pos]; const CHEATS_LIST *itemPtr = &this->_list[pos];
return (CHEATS_LIST *)itemPtr;
} }
size_t CHEATS::getListSize() size_t CHEATS::getListSize() const
{ {
return this->_list.size(); return this->_list.size();
} }
size_t CHEATS::getActiveCount() size_t CHEATS::getActiveCount() const
{ {
size_t activeCheatCount = 0; size_t activeCheatCount = 0;
const size_t cheatListCount = this->getListSize(); const size_t cheatListCount = this->getListSize();
@ -1009,6 +1015,12 @@ bool CHEATS::load()
{ {
bool didLoadAllItems = false; bool didLoadAllItems = false;
int valueReadResult = 0; int valueReadResult = 0;
if (strlen((const char *)this->_filename) == 0)
{
return didLoadAllItems;
}
EMUFILE_FILE flist((char *)this->_filename, "r"); EMUFILE_FILE flist((char *)this->_filename, "r");
if (flist.fail()) if (flist.fail())
{ {
@ -1128,7 +1140,7 @@ bool CHEATS::load()
return didLoadAllItems; return didLoadAllItems;
} }
void CHEATS::process(int targetType) void CHEATS::process(int targetType) const
{ {
if (CommonSettings.cheatsDisable) return; if (CommonSettings.cheatsDisable) return;
@ -1177,7 +1189,7 @@ void CHEATS::process(int targetType)
} //end case 0 internal cheat system } //end case 0 internal cheat system
case 1: // Action Replay case 1: // Action Replay
ARparser(this->_list[i]); CHEATS::ARparser(this->_list[i]);
break; break;
case 2: // Codebreaker case 2: // Codebreaker
break; break;
@ -1197,14 +1209,14 @@ void CHEATS::process(int targetType)
#endif #endif
} }
void CHEATS::getXXcodeString(CHEATS_LIST theList, char *res_buf) void CHEATS::StringFromXXCode(const CHEATS_LIST &srcCheatItem, char *outCStringBuffer)
{ {
char buf[50] = { 0 }; char buf[50] = { 0 };
for (u32 i = 0; i < theList.num; i++) for (u32 i = 0; i < srcCheatItem.num; i++)
{ {
snprintf(buf, 19, "%08X %08X\n", theList.code[i][0], theList.code[i][1]); snprintf(buf, 19, "%08X %08X\n", srcCheatItem.code[i][0], srcCheatItem.code[i][1]);
strcat(res_buf, buf); strcat(outCStringBuffer, buf);
} }
} }

View File

@ -60,8 +60,6 @@ private:
u8 _filename[MAX_PATH]; u8 _filename[MAX_PATH];
size_t _currentGet; size_t _currentGet;
void clear();
void ARparser(CHEATS_LIST& cheat);
char *clearCode(char *s); char *clearCode(char *s);
public: public:
@ -72,6 +70,7 @@ public:
} }
~CHEATS() {} ~CHEATS() {}
void clear();
void init(const char *thePath); void init(const char *thePath);
const char* getFilePath() const; const char* getFilePath() const;
void setFilePath(const char *thePath); void setFilePath(const char *thePath);
@ -105,15 +104,17 @@ public:
bool getList(CHEATS_LIST *cheat); bool getList(CHEATS_LIST *cheat);
CHEATS_LIST* getListPtr(); CHEATS_LIST* getListPtr();
bool copyItemFromIndex(const size_t pos, CHEATS_LIST &outCheatItem); bool copyItemFromIndex(const size_t pos, CHEATS_LIST &outCheatItem);
CHEATS_LIST* getItemPtrAtIndex(const size_t pos); CHEATS_LIST* getItemPtrAtIndex(const size_t pos) const;
size_t getListSize(); size_t getListSize() const;
size_t getActiveCount(); size_t getActiveCount() const;
void setDescription(const char *description, const size_t pos); void setDescription(const char *description, const size_t pos);
bool save(); bool save();
bool load(); bool load();
void process(int targetType); void process(int targetType) const;
void getXXcodeString(CHEATS_LIST theList, char *res_buf);
static void ARparser(const CHEATS_LIST &cheat);
static void StringFromXXCode(const CHEATS_LIST &srcCheatItem, char *outCStringBuffer);
static bool XXCodeFromString(const std::string codeString, CHEATS_LIST &outCheatItem); static bool XXCodeFromString(const std::string codeString, CHEATS_LIST &outCheatItem);
static bool XXCodeFromString(const char *codeString, CHEATS_LIST &outCheatItem); static bool XXCodeFromString(const char *codeString, CHEATS_LIST &outCheatItem);
}; };