diff --git a/source/gct.cpp b/source/gct.cpp new file mode 100644 index 00000000..d3a952c0 --- /dev/null +++ b/source/gct.cpp @@ -0,0 +1,248 @@ +#include "gct.h" + +GCTCheats::GCTCheats(void) +{ + iCntCheats = 0; +} + +GCTCheats::~GCTCheats(void) +{ +} + +int GCTCheats::getCnt() +{ + return iCntCheats; +} + +string GCTCheats::getGameName(void) +{ + return sGameTitle; +} + +string GCTCheats::getGameID(void) +{ + return sGameID; +} + +string GCTCheats::getCheat(int nr) +{ + if (nr <= (iCntCheats-1)) + { + return sCheats[nr]; + } + else + { + return OUTOFRANGE;//"Error: CheatNr out of range"; + } +} + +string GCTCheats::getCheatName(int nr) +{ + if (nr <= (iCntCheats-1)) + { + return sCheatName[nr]; + } + else + { + return "Error: CheatNr out of range"; + } +} + +int GCTCheats::createGCT(int nr,const char * filename) +{ + ofstream filestr; + filestr.open(filename); + + if (filestr.fail()) + return 0; + + //Reversed Header and Footer + char header[] = { 0x00, 0xd0, 0xc0, 0xde, 0x00, 0xd0, 0xc0, 0xde}; + char footer[] = { 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + string buf = getCheat(nr); + filestr.write(header,sizeof(header)); + + int x = 0; + long int li; + int len = buf.size(); + + while (x < len) + { + string temp = buf.substr(x,2); + li = strtol(temp.c_str(),NULL,16); + temp = li; + filestr.write(temp.c_str(),1); + x +=2; + } + filestr.write(footer,sizeof(footer)); + + filestr.close(); + return 1; +} + +int GCTCheats::createGCT(const char * chtbuffer,const char * filename) +{ + ofstream filestr; + filestr.open(filename); + + if (filestr.fail()) + return 0; + + //Reversed Header and Footer + char header[] = { 0x00, 0xd0, 0xc0, 0xde, 0x00, 0xd0, 0xc0, 0xde}; + char footer[] = { 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + string buf = chtbuffer; + filestr.write(header,sizeof(header)); + + int x = 0; + long int li; + int len = buf.size(); + + while (x < len) + { + string temp = buf.substr(x,2); + li = strtol(temp.c_str(),NULL,16); + temp = li; + filestr.write(temp.c_str(),1); + x +=2; + } + + filestr.write(footer,sizeof(footer)); + + filestr.close(); + +return 1; +} + +int GCTCheats::createGCT(int nr[],int cnt,const char * filename) +{ + + ofstream filestr; + filestr.open(filename); + + if (filestr.fail()) + return 0; + + //Reversed Header and Footer + char header[] = { 0x00, 0xd0, 0xc0, 0xde, 0x00, 0xd0, 0xc0, 0xde}; + char footer[] = { 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + filestr.write(header,sizeof(header)); + + int c = 0; + while (c != cnt) + { + int actnr = nr[c]; + string buf = getCheat(actnr); + long int li; + int len = buf.size(); + int x = 0; + + while (x < len) + { + string temp = buf.substr(x,2); + li = strtol(temp.c_str(),NULL,16); + temp = li; + filestr.write(temp.c_str(),1); + x +=2; + } + c++; + } + +filestr.write(footer,sizeof(footer)); +filestr.close(); +return 1; +} + +int GCTCheats::openTxtfile(const char * filename) +{ + ifstream filestr; + int i = 0; + string str; + filestr.open(filename); + + if (filestr.fail()) + return 0; + + filestr.seekg(0,ios_base::end); + int size = filestr.tellg(); + if (size <= 0) return -1; + filestr.seekg(0,ios_base::beg); + + getline(filestr,sGameID); + getline(filestr,sGameTitle); + filestr.ignore(); + + while(!filestr.eof()) + { + getline(filestr,sCheatName[i]); + string cheatdata; + bool emptyline = false; + + do + { + getline(filestr,str,'\n'); + cheatdata.append(str); + + if (str == "") + { + emptyline = true; + break; + } + + size_t found=cheatdata.find(' '); + cheatdata.replace(found,1,""); + + if (filestr.eof()) break; + } while(!emptyline); + + sCheats[i] = cheatdata; + i++; +} +iCntCheats = i; +filestr.close(); +return 1; +} + +struct GCTCheats::chtentries GCTCheats::getCheatList(void) +{ + + struct GCTCheats::chtentries cheatlist; + int i = 0; + cheatlist.sGameID = sGameID; + cheatlist.sGameTitle = sGameTitle; + + while (i < iCntCheats) + { + cheatlist.sCheatName[i] = sCheatName[i]; + cheatlist.sCheats[i] = sCheats[i]; + i++; + } + return cheatlist; +} + +struct GCTCheats::chtentries GCTCheats::getCheatList(const char * filename) +{ + openTxtfile(filename); + struct GCTCheats::chtentries cheatlist; + int i = 0; + cheatlist.sGameID = sGameID; + cheatlist.sGameTitle = sGameTitle; + cheatlist.iCntCheats = iCntCheats; + + while (i < iCntCheats) + { + cheatlist.sCheatName[i] = sCheatName[i]; + cheatlist.sCheats[i] = sCheats[i]; + i++; + } + return cheatlist; +} + +int GCTCheats::download_txtcheat(int id) +{ +//ToDo +return 1; +} diff --git a/source/gct.h b/source/gct.h new file mode 100644 index 00000000..265363c4 --- /dev/null +++ b/source/gct.h @@ -0,0 +1,106 @@ +/* + * gct.h + * Class to handle Ocarina TXT Cheatfiles + * WIP: Actually itīs needed to call fatInitDefault() or the file will not be found + * and no Comments supported for now + */ + +#ifndef _GCT_H +#define _GCT_H + +#include +#include +#include + +#define OUTOFRANGE "Error:Range" +#define MAXCHEATS 40 +#define GCT_PATH "sd:/codes/" +#define GECKOSITE "http://www.usbgecko.com/codes/codes/" + +using namespace std; + +struct chtentrie +{ + string sGameID; + string sGameTitle; + string sCheatName[MAXCHEATS]; + string sCheats[MAXCHEATS]; + int iCntCheats; +}; + +//!Handles Ocarina TXT Cheatfiles +class GCTCheats +{ +private: + chtentrie ccc; + string sGameID; + string sGameTitle; + string sCheatName[MAXCHEATS]; + string sCheats[MAXCHEATS]; + int iCntCheats; + +public: + +struct chtentries +{ + string sGameID; + string sGameTitle; + string sCheatName[MAXCHEATS]; + string sCheats[MAXCHEATS]; + int iCntCheats; +}; + + //!Constructor + GCTCheats(void); + //!Destructor + ~GCTCheats(void); + //!Open txt file with cheats + //!\param filename name of TXT file + //!\return error code + int openTxtfile(const char * filename); + //!Creates GCT file for one cheat + //!\param nr selected Cheat Numbers + //!\param filename name of GCT file + //!\return error code + int createGCT(int nr,const char * filename); + //!Creates GCT file from a buffer + //!\param chtbuffer buffer that holds the cheat data + //!\param filename name of GCT file + //!\return error code + int createGCT(const char * chtbuffer,const char * filename); + //!Creates GCT file + //!\param nr[] array of selected Cheat Numbers + //!\param cnt size of array + //!\param filename name of GCT file + //!\return error code + int createGCT(int nr[],int cnt,const char * filename); + //!Gets Count cheats + //!\return Count cheats + int getCnt(); + //!Gets Game Name + //!\return Game Name + string getGameName(void); + //!Gets GameID + //!\return GameID + string getGameID(void); + //!Gets cheat data + //!\return cheat data + string getCheat(int nr); + //!Gets Cheat Name + //!\return Cheat Name + string getCheatName(int nr); + //!Gets Cheat List + //!\return struct chtentrie + //struct chtentrie getCheatList2(void); + //!Gets Cheat List + //!\return struct chtentries + struct chtentries getCheatList(void); + //!Gets Cheat List from file + //!\param filename name of TXT file + //!\return struct chtentries + struct chtentries getCheatList(const char * filename); + + int download_txtcheat(int id); +}; + +#endif /* _GCT_H */