2009-07-31 14:45:14 +02:00
|
|
|
/*
|
|
|
|
* gct.h
|
|
|
|
* Class to handle Ocarina TXT Cheatfiles
|
|
|
|
* nIxx
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2009-10-14 19:20:24 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-07-31 14:45:14 +02:00
|
|
|
#include "gct.h"
|
|
|
|
|
|
|
|
#define ERRORRANGE "Error: CheatNr out of range"
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
GCTCheats::GCTCheats( void )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
iCntCheats = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
GCTCheats::~GCTCheats( void )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string sGameID = "";
|
2009-07-31 14:45:14 +02:00
|
|
|
string sGameTitle = "";
|
|
|
|
/*string sCheatName[MAXCHEATS];
|
|
|
|
string sCheats[MAXCHEATS];
|
|
|
|
string sCheatComment[MAXCHEATS];*/
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GCTCheats::getCnt()
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return iCntCheats;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string GCTCheats::getGameName( void )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return sGameTitle;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string GCTCheats::getGameID( void )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return sGameID;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string GCTCheats::getCheat( int nr )
|
|
|
|
{
|
|
|
|
if ( nr <= ( iCntCheats - 1 ) )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return sCheats[nr];
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return ERRORRANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string GCTCheats::getCheatName( int nr )
|
|
|
|
{
|
|
|
|
if ( nr <= ( iCntCheats - 1 ) )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return sCheatName[nr];
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return ERRORRANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string GCTCheats::getCheatComment( int nr )
|
|
|
|
{
|
|
|
|
if ( nr <= ( iCntCheats - 1 ) )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return sCheatComment[nr];
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
return ERRORRANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GCTCheats::createGCT( int nr, const char * filename )
|
|
|
|
{
|
2009-09-18 10:28:31 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( nr == 0 )
|
|
|
|
return 0;
|
2009-09-18 10:28:31 +02:00
|
|
|
|
2009-07-31 14:45:14 +02:00
|
|
|
ofstream filestr;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.open( filename );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( filestr.fail() )
|
2009-07-31 14:45:14 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
//Header and Footer
|
|
|
|
char header[] = { 0x00, 0xd0, 0xc0, 0xde, 0x00, 0xd0, 0xc0, 0xde};
|
|
|
|
char footer[] = { 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
string buf = getCheat( nr );
|
|
|
|
filestr.write( header, sizeof( header ) );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
long int li;
|
|
|
|
int len = buf.size();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( x < len )
|
|
|
|
{
|
|
|
|
string temp = buf.substr( x, 2 );
|
|
|
|
li = strtol( temp.c_str(), NULL, 16 );
|
2009-07-31 14:45:14 +02:00
|
|
|
temp = li;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( temp.c_str(), 1 );
|
|
|
|
x += 2;
|
2009-07-31 14:45:14 +02:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( footer, sizeof( footer ) );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
|
|
|
filestr.close();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GCTCheats::createGCT( const char * chtbuffer, const char * filename )
|
|
|
|
{
|
2009-09-18 10:28:31 +02:00
|
|
|
|
2009-07-31 14:45:14 +02:00
|
|
|
ofstream filestr;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.open( filename );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( filestr.fail() )
|
2009-07-31 14:45:14 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
//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;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( header, sizeof( header ) );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
long int li;
|
|
|
|
int len = buf.size();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( x < len )
|
|
|
|
{
|
|
|
|
string temp = buf.substr( x, 2 );
|
|
|
|
li = strtol( temp.c_str(), NULL, 16 );
|
2009-07-31 14:45:14 +02:00
|
|
|
temp = li;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( temp.c_str(), 1 );
|
|
|
|
x += 2;
|
2009-07-31 14:45:14 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( footer, sizeof( footer ) );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
|
|
|
filestr.close();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GCTCheats::createGCT( int nr[], int cnt, const char * filename )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( cnt == 0 )
|
|
|
|
return 0;
|
2009-09-18 10:28:31 +02:00
|
|
|
|
2009-07-31 14:45:14 +02:00
|
|
|
ofstream filestr;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.open( filename );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( filestr.fail() )
|
2009-07-31 14:45:14 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
//Header and Footer
|
|
|
|
char header[] = { 0x00, 0xd0, 0xc0, 0xde, 0x00, 0xd0, 0xc0, 0xde};
|
|
|
|
char footer[] = { 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( header, sizeof( header ) );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
|
|
|
int c = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( c != cnt )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
int actnr = nr[c];
|
2010-09-19 01:16:05 +02:00
|
|
|
string buf = getCheat( actnr );
|
2009-07-31 14:45:14 +02:00
|
|
|
long int li;
|
|
|
|
int len = buf.size();
|
|
|
|
int x = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( x < len )
|
|
|
|
{
|
|
|
|
string temp = buf.substr( x, 2 );
|
|
|
|
li = strtol( temp.c_str(), NULL, 16 );
|
2009-07-31 14:45:14 +02:00
|
|
|
temp = li;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( temp.c_str(), 1 );
|
|
|
|
x += 2;
|
2009-07-31 14:45:14 +02:00
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.write( footer, sizeof( footer ) );
|
2009-07-31 14:45:14 +02:00
|
|
|
filestr.close();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int GCTCheats::openTxtfile( const char * filename )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
ifstream filestr;
|
|
|
|
int i = 0;
|
|
|
|
string str;
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.open( filename );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( filestr.fail() )
|
2009-07-31 14:45:14 +02:00
|
|
|
return 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
filestr.seekg( 0, ios_base::end );
|
2009-07-31 14:45:14 +02:00
|
|
|
int size = filestr.tellg();
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( size <= 0 ) return -1;
|
|
|
|
filestr.seekg( 0, ios_base::beg );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
getline( filestr, sGameID );
|
|
|
|
if ( sGameID[sGameID.length() - 1] == '\r' )
|
|
|
|
sGameID.erase( sGameID.length() - 1 );
|
2010-09-15 21:54:09 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
getline( filestr, sGameTitle );
|
|
|
|
if ( sGameTitle[sGameTitle.length() - 1] == '\r' )
|
|
|
|
sGameTitle.erase( sGameTitle.length() - 1 );
|
2010-09-15 21:54:09 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
getline( filestr, sCheatName[i] ); // skip first line if file uses CRLF
|
|
|
|
if ( !sGameTitle[sGameTitle.length() - 1] == '\r' )
|
|
|
|
filestr.seekg( 0, ios_base::beg );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( !filestr.eof() )
|
|
|
|
{
|
|
|
|
getline( filestr, sCheatName[i] ); // '\n' delimiter by default
|
|
|
|
if ( sCheatName[i][sCheatName[i].length() - 1] == '\r' )
|
|
|
|
sCheatName[i].erase( sCheatName[i].length() - 1 );
|
2009-08-07 18:10:54 +02:00
|
|
|
|
2009-07-31 14:45:14 +02:00
|
|
|
string cheatdata;
|
|
|
|
bool emptyline = false;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
getline( filestr, str );
|
|
|
|
if ( str[str.length() - 1] == '\r' )
|
|
|
|
str.erase( str.length() - 1 );
|
2010-09-15 21:54:09 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( str == "" || str[0] == '\r' || str[0] == '\n' )
|
|
|
|
{
|
2009-07-31 14:45:14 +02:00
|
|
|
emptyline = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( IsCode( str ) )
|
|
|
|
{
|
|
|
|
// remove any garbage (comment) after code
|
|
|
|
while ( str.size() > 17 )
|
|
|
|
{
|
|
|
|
str.erase( str.length() - 1 );
|
|
|
|
}
|
|
|
|
cheatdata.append( str );
|
|
|
|
size_t found = cheatdata.find( ' ' );
|
|
|
|
cheatdata.replace( found, 1, "" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 06:05:50 +02:00
|
|
|
//printf("%i",str.size());
|
2009-07-31 14:45:14 +02:00
|
|
|
sCheatComment[i] = str;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( filestr.eof() ) break;
|
2010-09-15 21:54:09 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
while ( !emptyline );
|
2009-07-31 14:45:14 +02:00
|
|
|
|
|
|
|
sCheats[i] = cheatdata;
|
2010-09-19 01:16:05 +02:00
|
|
|
i++;
|
|
|
|
if ( i == MAXCHEATS ) break;
|
2009-07-31 14:45:14 +02:00
|
|
|
}
|
|
|
|
iCntCheats = i;
|
|
|
|
filestr.close();
|
|
|
|
return 1;
|
|
|
|
}
|
2009-09-16 06:05:50 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool GCTCheats::IsCode( const std::string& str )
|
|
|
|
{
|
|
|
|
if ( str[8] == ' ' && str.size() >= 17 )
|
|
|
|
{
|
|
|
|
// accept strings longer than 17 in case there is a comment on the same line as the code
|
|
|
|
char part1[9];
|
|
|
|
char part2[9];
|
|
|
|
snprintf( part1, sizeof( part1 ), "%c%c%c%c%c%c%c%c", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7] );
|
|
|
|
snprintf( part2, sizeof( part2 ), "%c%c%c%c%c%c%c%c", str[9], str[10], str[11], str[12], str[13], str[14], str[15], str[16] );
|
|
|
|
if ( ( strtok( part1, "0123456789ABCDEFabcdef" ) == NULL ) && ( strtok( part2, "0123456789ABCDEFabcdef" ) == NULL ) )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2009-09-16 06:05:50 +02:00
|
|
|
}
|