fceugx/source/cheatmgr.cpp

210 lines
3.9 KiB
C++
Raw Normal View History

2009-07-22 03:58:45 +02:00
/****************************************************************************
2009-07-22 04:05:49 +02:00
* FCE Ultra
2009-07-22 03:58:45 +02:00
* Nintendo Wii/Gamecube Port
*
* Tantric 2008-2009
*
* cheatmgr.cpp
*
* Cheat handling
***************************************************************************/
#include <malloc.h>
2010-08-29 23:15:42 +02:00
#include <gctypes.h>
2009-07-22 03:58:45 +02:00
#include "fceugx.h"
#include "fceusupport.h"
#include "fileop.h"
#include "filebrowser.h"
#include "menu.h"
2010-03-22 00:49:24 +01:00
#include "fceultra/cheat.h"
2009-07-22 03:58:45 +02:00
int numcheats = 0;
/****************************************************************************
* LoadCheats
*
* Loads cheat file from save buffer
* Custom version of FCEU_LoadGameCheats()
***************************************************************************/
static int LoadCheats (int length)
{
unsigned int addr;
unsigned int val;
unsigned int status;
unsigned int type;
unsigned int compare;
int x;
char *namebuf;
int tc=0;
char * linebreak = strtok((char *)savebuffer, "\n");
while(linebreak != NULL)
{
2009-07-30 06:37:29 +02:00
char *tbuf=linebreak;
2009-07-22 03:58:45 +02:00
int doc=0;
addr=val=compare=status=type=0;
if(tbuf[0]=='S')
{
tbuf++;
type=1;
}
else type=0;
if(tbuf[0]=='C')
{
tbuf++;
doc=1;
}
if(tbuf[0]==':')
{
tbuf++;
status=0;
}
else status=1;
if(doc)
{
char *neo=&tbuf[4+2+2+1+1+1];
if(sscanf(tbuf,"%04x%*[:]%02x%*[:]%02x",&addr,&val,&compare)!=3)
continue;
namebuf=(char *)malloc(strlen(neo)+1);
strcpy(namebuf,neo);
}
else
{
char *neo=&tbuf[4+2+1+1];
if(sscanf(tbuf,"%04x%*[:]%02x",&addr,&val)!=2)
continue;
namebuf=(char *)malloc(strlen(neo)+1);
strcpy(namebuf,neo);
}
for(x=0;x<(int)strlen(namebuf);x++)
{
if(namebuf[x]==10 || namebuf[x]==13)
{
namebuf[x]=0;
break;
}
else if(namebuf[x]<0x20) namebuf[x]=' ';
}
AddCheatEntry(namebuf,addr,val,doc?compare:-1,status,type);
FCEUI_ToggleCheat(tc); // turn cheat off
tc++;
// find next line break
linebreak = strtok(NULL, "\n");
}
RebuildSubCheats();
return tc;
}
/****************************************************************************
* SetupCheats
*
* Erases any prexisting cheats, loads cheats from a cheat file
* Called when a ROM is first loaded
***************************************************************************/
void
SetupCheats()
{
FCEU_PowerCheats();
numcheats = 0;
if(GameInfo->type == GIT_NSF)
return;
char filepath[1024];
2009-10-04 01:08:40 +02:00
size_t offset = 0;
2009-07-22 03:58:45 +02:00
2009-10-02 00:21:25 +02:00
int device = GCSettings.SaveMethod;
2009-07-22 03:58:45 +02:00
2009-10-02 00:21:25 +02:00
if(device == DEVICE_AUTO)
device = autoSaveMethod(SILENT);
2009-07-22 03:58:45 +02:00
2009-10-02 00:21:25 +02:00
if(device == DEVICE_AUTO)
2009-07-22 03:58:45 +02:00
return;
2009-10-02 00:21:25 +02:00
if(!MakeFilePath(filepath, FILE_CHEAT))
2009-07-22 03:58:45 +02:00
return;
AllocSaveBuffer();
2009-10-02 00:21:25 +02:00
offset = LoadFile(filepath, SILENT);
2009-07-22 03:58:45 +02:00
// load cheat file if present
if(offset > 0)
numcheats = LoadCheats(offset);
FreeSaveBuffer ();
}
2009-10-07 08:59:10 +02:00
bool FindGameGenie()
{
2009-10-07 08:59:10 +02:00
if (GENIEROM)
return true;
if(GCSettings.LoadMethod == DEVICE_AUTO)
GCSettings.LoadMethod = autoLoadMethod();
if(GCSettings.LoadMethod == DEVICE_AUTO)
return false;
char * tmpbuffer = (char *) memalign(32, 512 * 1024);
if(!tmpbuffer)
2009-10-07 08:59:10 +02:00
return false;
size_t romSize = 0;
char filepath[1024];
sprintf (filepath, "%s%s/gg.rom", pathPrefix[GCSettings.LoadMethod], APPFOLDER);
romSize = LoadFile(tmpbuffer, filepath, 0, SILENT);
if(romSize == 0 && strlen(appPath) > 0)
{
sprintf (filepath, "%s/gg.rom", appPath);
2009-10-02 00:21:25 +02:00
romSize = LoadFile(tmpbuffer, filepath, 0, SILENT);
}
if (romSize > 0)
{
GENIEROM=(uint8 *)malloc(4096+1024);
if(tmpbuffer[0]==0x4E) /* iNES ROM image */
{
memcpy(GENIEROM,tmpbuffer+16,4096);
memcpy(GENIEROM+4096,tmpbuffer+16400,256);
}
else
{
memcpy(GENIEROM,tmpbuffer,4352);
}
/* Workaround for the FCE Ultra CHR page size only being 1KB */
for(int x=0; x<4; x++)
memcpy(GENIEROM+4096+(x<<8),GENIEROM+4096,256);
}
free(tmpbuffer);
2009-10-07 08:59:10 +02:00
if(romSize > 0)
return true;
return false;
}
void OpenGameGenie()
{
if(!GCSettings.gamegenie)
geniestage=0;
else if (FindGameGenie())
geniestage=1;
}