From e22f34ec7f7ad3310e8054faf6ab2d5a53dd3e83 Mon Sep 17 00:00:00 2001 From: dborth Date: Sat, 18 Jul 2009 06:19:04 +0000 Subject: [PATCH] code cleanup --- source/fceultra/state.cpp | 54 +++--- source/fceultra/utils/memorystream.h | 2 +- source/ngc/fceuload.cpp | 33 +--- source/ngc/fceustate.cpp | 258 ++------------------------- source/ngc/fceusupport.h | 22 +-- 5 files changed, 51 insertions(+), 318 deletions(-) diff --git a/source/fceultra/state.cpp b/source/fceultra/state.cpp index 65cbee1..fc6c989 100644 --- a/source/fceultra/state.cpp +++ b/source/fceultra/state.cpp @@ -50,8 +50,8 @@ using namespace std; -void (*SPreSave)(void); -void (*SPostSave)(void); +static void (*SPreSave)(void); +static void (*SPostSave)(void); static int SaveStateStatus[10]; static int StateShow; @@ -68,7 +68,7 @@ bool undoLS = false; //This will be true if a backupstate was made and it was bool redoLS = false; //This will be true if a backupstate was loaded, meaning redoLoadState can be run #define SFMDATA_SIZE (64) -SFORMAT SFMDATA[SFMDATA_SIZE]; +static SFORMAT SFMDATA[SFMDATA_SIZE]; static int SFEXINDEX; #define RLSB FCEUSTATE_RLSB //0x80000000 @@ -443,7 +443,7 @@ void FCEUSS_Save(const char *fname) fn = strdup(FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0).c_str()); //backup existing savestate first - if (CheckFileExists(fn)) + if (CheckFileExists(fn)) { CreateBackupSaveState(fn); //Make a backup of previous savestate before overwriting it strcpy(lastSavestateMade,fn); //Remember what the last savestate filename was (for undoing later) @@ -451,7 +451,7 @@ void FCEUSS_Save(const char *fname) } else undoSS = false; //so backup made so lastSavestateMade does have a backup file, so no undo - + st = FCEUD_UTF8_fstream(fn,"wb"); free(fn); } @@ -492,7 +492,7 @@ int FCEUSS_LoadFP_old(std::istream* is, ENUM_SSLOADPARAMS params) //{ // fn=FCEU_MakeFName(FCEUMKF_NPTEMP,0,0); // FILE *fp; - // + // // if((fp=fopen(fn,"wb"))) // { // if(FCEUSS_SaveFP(fp)) @@ -547,7 +547,7 @@ int FCEUSS_LoadFP_old(std::istream* is, ENUM_SSLOADPARAMS params) if(x) { FCEUPPU_LoadState(stateversion); - FCEUSND_LoadState(stateversion); + FCEUSND_LoadState(stateversion); x=FCEUMOV_PostLoad(); } if(fn) @@ -556,7 +556,7 @@ int FCEUSS_LoadFP_old(std::istream* is, ENUM_SSLOADPARAMS params) //{ // * Oops! Load the temporary savestate */ // FILE *fp; - // + // // if((fp=fopen(fn,"rb"))) // { // FCEUSS_LoadFP(fp,SSLOADPARAM_NOBACKUP); @@ -592,7 +592,7 @@ bool FCEUSS_LoadFP(std::istream* is, ENUM_SSLOADPARAMS params) if(!ret && backup) FCEUSS_LoadFP(&msBackupSavestate,SSLOADPARAM_NOBACKUP); return ret; } - + int totalsize = FCEU_de32lsb(header + 4); int stateversion = FCEU_de32lsb(header + 8); int comprlen = FCEU_de32lsb(header + 12); @@ -744,8 +744,8 @@ void ResetExState(void (*PreSave)(void), void (*PostSave)(void)) free(SFMDATA[x].desc); } // adelikat, 3/14/09: had to add this to clear out the size parameter. NROM(mapper 0) games were having savestate crashes if loaded after a non NROM game because the size variable was carrying over and causing savestates to save too much data - SFMDATA[0].s = 0; - + SFMDATA[0].s = 0; + SPreSave = PreSave; SPostSave = PostSave; SFEXINDEX=0; @@ -806,7 +806,7 @@ void FCEUI_SaveState(const char *fname) if(!FCEU_IsValidUI(FCEUI_SAVESTATE)) return; StateShow=0; - + FCEUSS_Save(fname); } @@ -825,10 +825,10 @@ void FCEUI_LoadState(const char *fname) from this ;)). */ BackupLoadState(); //Backup the current state before loading a new one - + if (!movie_readonly && autoMovieBackup && freshMovie) //If auto-backup is on, movie has not been altered this session and the movie is in read+write mode { - FCEUI_MakeBackupMovie(false); //Backup the movie before the contents get altered, but do not display messages + FCEUI_MakeBackupMovie(false); //Backup the movie before the contents get altered, but do not display messages } if(FCEUSS_Load(fname)) { @@ -882,8 +882,8 @@ string GenerateBackupSaveStateFn(const char *fname) string filename; filename = fname; //Convert fname to a string object int x = filename.find_last_of("."); //Find file extension - filename.insert(x,"-bak"); //add "-bak" before the dot. - + filename.insert(x,"-bak"); //add "-bak" before the dot. + return filename; } @@ -902,15 +902,15 @@ void SwapSaveState() //-------------------------------------------------------------------------------------------- //Both files must exist //-------------------------------------------------------------------------------------------- - - if (!lastSavestateMade) + + if (!lastSavestateMade) { FCEUI_DispMessage("Can't Undo"); FCEUI_printf("Undo savestate was attempted but unsuccessful because there was not a recently used savestate.\n"); return; //If there is no last savestate, can't undo } string backup = GenerateBackupSaveStateFn(lastSavestateMade); //Get filename of backup state - if (!CheckFileExists(backup.c_str())) + if (!CheckFileExists(backup.c_str())) { FCEUI_DispMessage("Can't Undo"); FCEUI_printf("Undo savestate was attempted but unsuccessful because there was not a backup of the last used savestate.\n"); @@ -922,11 +922,11 @@ void SwapSaveState() //-------------------------------------------------------------------------------------------- string temp = backup; //Put backup filename in temp temp.append("x"); //Add x - + rename(backup.c_str(),temp.c_str()); //rename backup file to temp file rename(lastSavestateMade,backup.c_str()); //rename current as backup rename(temp.c_str(),lastSavestateMade); //rename backup as current - + undoSS = true; //Just in case, if this was run, then there is definately a last savestate and backup if (redoSS) //This was a redo function, so if run again it will be an undo again redoSS = false; @@ -935,8 +935,8 @@ void SwapSaveState() FCEUI_DispMessage("%s restored",backup.c_str()); FCEUI_printf("%s restored\n",backup.c_str()); -} - +} + //------------------------------------------------------------------------------------------------------------------------------------------------------ //************************************************************************* //Loadstate backup functions @@ -949,7 +949,7 @@ string GetBackupFileName() //particularly from unintentional loadstating string filename; int x; - + filename = strdup(FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0).c_str()); //Generate normal savestate filename x = filename.find_last_of("."); //Find last dot filename = filename.substr(0,x); //Chop off file extension @@ -963,11 +963,11 @@ bool CheckBackupSaveStateExist() //This function simply checks to see if the backup loadstate exists, the backup loadstate is a special savestate //That is made before loading any state, so that the user never loses his data string filename = GetBackupFileName(); //Get backup savestate filename - + //Check if this filename exists fstream test; test.open(filename.c_str(),fstream::in); - + if (test.fail()) { test.close(); @@ -1011,4 +1011,4 @@ void RedoLoadState() } redoLS = false; //Flag that RedoLoadState can not be run again undoLS = true; //Flag that LoadBackup can be run again -} \ No newline at end of file +} diff --git a/source/fceultra/utils/memorystream.h b/source/fceultra/utils/memorystream.h index 7c6a343..8c13dcd 100644 --- a/source/fceultra/utils/memorystream.h +++ b/source/fceultra/utils/memorystream.h @@ -185,7 +185,7 @@ private: throw new std::runtime_error("memory_streambuf is not expandable"); size_t newcapacity; - if(upto == -1) + if(upto == 0) newcapacity = capacity + capacity/2 + 2; else newcapacity = std::max(upto,capacity); diff --git a/source/ngc/fceuload.cpp b/source/ngc/fceuload.cpp index 16b82de..062a7e0 100644 --- a/source/ngc/fceuload.cpp +++ b/source/ngc/fceuload.cpp @@ -29,38 +29,15 @@ bool romLoaded = false; #define SAMPLERATE 48000 -static FCEUFILE *fceufp = NULL; -static memorystream* fceumem = NULL; - -static void MakeFCEUFile(char * membuffer, int length) -{ - if(fceufp != NULL) - { - delete fceufp; - fceufp = NULL; - } - - fceumem = new memorystream(membuffer, length); // we never need to delete this...? - - fceufp = new FCEUFILE(); - fceufp->size = length; - fceufp->stream = fceumem; - fceufp->filename = romFilename; -} - int GCMemROM(int method, int size) { ResetGameLoaded(); - //AutosaveStatus[0] = AutosaveStatus[1] = 0; - //AutosaveStatus[2] = AutosaveStatus[3] = 0; - CloseGame(); GameInfo = new FCEUGI(); memset(GameInfo, 0, sizeof(FCEUGI)); GameInfo->filename = strdup(romFilename); - //if(fceufp->archiveFilename != "") GameInfo->archiveFilename = strdup(fceufp->archiveFilename.c_str()); GameInfo->archiveCount = 0; /*** Set some default values ***/ @@ -78,7 +55,12 @@ int GCMemROM(int method, int size) FCEUI_SetSoundVolume(100); // 0-100 FCEUI_SetLowPass(0); - MakeFCEUFile((char *) nesrom, size); + memorystream * fceumem = new memorystream((char *) nesrom, size); + + FCEUFILE * fceufp = new FCEUFILE(); + fceufp->size = size; + fceufp->stream = fceumem; + fceufp->filename = romFilename; nesGameType = 0; @@ -121,6 +103,9 @@ int GCMemROM(int method, int size) nesGameType = 4; } + //delete fceufp; + //delete fceumem; + if (nesGameType > 0) { FCEU_ResetVidSys(); diff --git a/source/ngc/fceustate.cpp b/source/ngc/fceustate.cpp index 2b2fabd..76a61ba 100644 --- a/source/ngc/fceustate.cpp +++ b/source/ngc/fceustate.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "pngu/pngu.h" #include "fceugx.h" @@ -27,247 +28,6 @@ #include "fileop.h" #include "gcvideo.h" -#define RLSB 0x80000000 - -/**************************************************************************** - * Memory based file functions - ****************************************************************************/ -static int sboffset; // Used as a basic fileptr - -/*** Open a file ***/ -static void memopen() -{ - sboffset = 0; -} - -/*** Write to the file ***/ -static void memfwrite(void *buffer, int len) -{ - if ((sboffset + len) > SAVEBUFFERSIZE) - ErrorPrompt("Buffer Exceeded"); - - if (len > 0) - { - memcpy(&savebuffer[sboffset], buffer, len); - sboffset += len; - } -} - -/*** Read from a file ***/ -static void memfread(void *buffer, int len) -{ - if ((sboffset + len) > SAVEBUFFERSIZE) - ErrorPrompt("Buffer exceeded"); - - if (len > 0) - { - memcpy(buffer, &savebuffer[sboffset], len); - sboffset += len; - } -} - -/**************************************************************************** - * GCReadChunk - * - * Read the array of SFORMAT structures to memory - ****************************************************************************/ -static int GCReadChunk(int chunkid, SFORMAT *sf) -{ - uint32 csize; - static char chunk[6]; - int chunklength; - int thischunk; - char info[128]; - - memfread(&chunk, 4); - memfread(&thischunk, 4); - memfread(&chunklength, 4); - - if (memcmp(&chunk, "CHNK", 4) == 0) - { - if (chunkid == thischunk) - { - /*** Now decode the array of chunks to this one ***/ - while (sf->v) - { - memfread(&chunk, 4); - if (memcmp(&chunk, "CHKE", 4) == 0) - return 1; - - if (memcmp(&chunk, sf->desc, 4) == 0) - { - memfread(&csize, 4); - if (csize == (sf->s & (~RLSB))) - { - memfread(sf->v, csize); - sprintf(info, "%s %d", chunk, csize); - } - else - { - ErrorPrompt("Bad chunk link"); - return 0; - } - } - else - { - sprintf(info, "No Sync %s %s", chunk, sf->desc); - ErrorPrompt(info); - return 0; - } - sf++; - } - } - else - return 0; - } - else - return 0; - - return 1; -} - -/**************************************************************************** - * GCFCEUSS_Load - * - * Reads the SFORMAT arrays - ****************************************************************************/ -static int GCFCEUSS_Load(int method) -{ - memopen(); // reset file pointer - - sboffset += 16; // skip FCEU header - - // Now read the chunks back - if (GCReadChunk(1, SFCPU)) - { - if (GCReadChunk(2, SFCPUC)) - { - X.mooPI = X.P; // Quick and dirty hack. - if (GCReadChunk(3, FCEUPPU_STATEINFO)) - { - if (GCReadChunk(4, FCEUCTRL_STATEINFO)) - { - if (GCReadChunk(5, FCEUSND_STATEINFO)) - { - if (GCReadChunk(0x10, SFMDATA)) - { - if (GameStateRestore) - GameStateRestore(FCEU_VERSION_NUMERIC); - - FCEUPPU_LoadState(FCEU_VERSION_NUMERIC); - FCEUSND_LoadState(FCEU_VERSION_NUMERIC); - return 1; - } - } - } - } - } - } - - return 0; -} - -/**************************************************************************** - * GCSaveChunk - * - * Write the array of SFORMAT structures to the file - ****************************************************************************/ -static int GCSaveChunk(int chunkid, SFORMAT *sf) -{ - int chnkstart; - int csize = 0; - int chsize = 0; - char chunk[] = "CHNK"; - - /*** Add chunk marker ***/ - memfwrite(&chunk, 4); - memfwrite(&chunkid, 4); - chnkstart = sboffset; /*** Save ptr ***/ - sboffset += 4; /*** Space for length ***/ - csize += 12; - - /*** Now run through this structure ***/ - while (sf->v) - { - /*** Check that there is a decription ***/ - if (sf->desc == NULL) - break; - - /*** Write out the description ***/ - memfwrite(sf->desc, 4); - - /*** Write the length of this chunk ***/ - chsize = (sf->s & (~RLSB)); - memfwrite(&chsize, 4); - - if (chsize > 0) - { - /*** Write the actual data ***/ - memfwrite(sf->v, chsize); - } - - csize += 8; - csize += chsize; - - sf++; - } - - /*** Update CHNK length ***/ - memcpy(&savebuffer[chnkstart], &csize, 4); - - return csize; -} - -/**************************************************************************** - * GCFCEUSS_Save - * - * This is a modified version of FCEUSS_Save - * It uses memory for it's I/O and has an added CHNK block. - * The file is terminated with CHNK length of 0. - ****************************************************************************/ -extern void (*SPreSave)(void); -extern void (*SPostSave)(void); - -static int GCFCEUSS_Save(int method) -{ - int totalsize = 0; - unsigned char header[16] = "FCS\xff"; - char chunk[] = "CHKE"; - int zero = 0; - int version = 0x981211; - memcpy(&header[8], &version, 4); // Add version ID - - memopen(); // Reset Memory File - - // Do internal Saving - FCEUPPU_SaveState(); - FCEUSND_SaveState(); - - // Write header - memfwrite(&header, 16); - totalsize += 16; - totalsize += GCSaveChunk(1, SFCPU); - totalsize += GCSaveChunk(2, SFCPUC); - totalsize += GCSaveChunk(3, FCEUPPU_STATEINFO); - totalsize += GCSaveChunk(4, FCEUCTRL_STATEINFO); - totalsize += GCSaveChunk(5, FCEUSND_STATEINFO); - - if(nesGameType == 4) // FDS - SPreSave(); - - totalsize += GCSaveChunk(0x10, SFMDATA); - - if(nesGameType == 4) // FDS - SPostSave(); - - // Add terminating CHNK - memfwrite(&chunk,4); - memfwrite(&zero,4); - totalsize += 8; - - return totalsize; -} - bool SaveState (char * filepath, int method, bool silent) { bool retval = false; @@ -306,9 +66,12 @@ bool SaveState (char * filepath, int method, bool silent) FreeSaveBuffer (); } - AllocSaveBuffer (); - - datasize = GCFCEUSS_Save(method); + std::vector tmpbuffer(SAVEBUFFERSIZE); + memorystream save(&tmpbuffer); + FCEUSS_SaveMS(&save, Z_NO_COMPRESSION); + save.sync(); + save.trim(); + datasize = save.size(); if (datasize) { @@ -322,9 +85,11 @@ bool SaveState (char * filepath, int method, bool silent) SetMCSaveComments(comments); } + AllocSaveBuffer (); + memcpy(savebuffer, save.buf(), datasize); offset = SaveFile(filepath, datasize, method, silent); + FreeSaveBuffer (); } - FreeSaveBuffer (); if (offset > 0) { @@ -369,7 +134,8 @@ bool LoadState (char * filepath, int method, bool silent) if (offset > 0) { - GCFCEUSS_Load(method); + memorystream save((char *)savebuffer, offset); + FCEUSS_LoadFP(&save, SSLOADPARAM_NOBACKUP); retval = true; } else diff --git a/source/ngc/fceusupport.h b/source/ngc/fceusupport.h index c9187d1..1c7b570 100644 --- a/source/ngc/fceusupport.h +++ b/source/ngc/fceusupport.h @@ -1,8 +1,6 @@ #ifndef _FCEUSUPPORT_H_ #define _FCEUSUPPORT_H_ -extern unsigned char * nesrom; - #include #include #include @@ -23,6 +21,8 @@ extern unsigned char * nesrom; #include "file.h" #include "cheat.h" +extern unsigned char * nesrom; + extern int FDSLoad(const char *name, FCEUFILE *fp); extern int iNESLoad(const char *name, FCEUFILE *fp, int o); extern int UNIFLoad(const char *name, FCEUFILE *fp); @@ -40,24 +40,6 @@ extern CartInfo UNIFCart; extern INPUTC *FCEU_InitZapper(int w); extern void FCEU_ResetPalette(void); -/*** External functions ***/ -extern void FCEUPPU_SaveState(void); -extern void FCEUSND_SaveState(void); -extern void FCEUPPU_LoadState(int version); -extern void FCEUSND_LoadState(int version); -extern void FlipByteOrder(uint8 *src, uint32 count); -extern void (*GameStateRestore)(int version); - -/*** External save structures ***/ -extern SFORMAT SFCPU[]; -extern SFORMAT SFCPUC[]; -extern SFORMAT FCEUPPU_STATEINFO[]; -extern SFORMAT FCEUCTRL_STATEINFO[]; -extern SFORMAT FCEUSND_STATEINFO[]; -extern SFORMAT SFMDATA[64]; -extern u32 iNESGameCRC32; - -extern int eoptions; #define EO_NO8LIM 1 #define EO_SUBASE 2 #define EO_CLIPSIDES 8