Only save the cheatlist file (stella.cht) when something has changed.

During development, sometimes Stella would crash, and for some reason
take the cheatfile contents with it.  Now, at least, this should
occur very rarely, since cheats won't be changed most of the time.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@1062 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2006-03-22 15:12:02 +00:00
parent baedc94f5e
commit 4eb94a3a2b
3 changed files with 42 additions and 14 deletions

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: CheatManager.cxx,v 1.9 2006-03-11 21:22:47 stephena Exp $
// $Id: CheatManager.cxx,v 1.10 2006-03-22 15:12:02 stephena Exp $
//============================================================================
#include <sstream>
@ -28,7 +28,9 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheatManager::CheatManager(OSystem* osystem)
: myOSystem(osystem)
: myOSystem(osystem),
myCurrentCheat(""),
myListIsDirty(false)
{
}
@ -260,11 +262,15 @@ void CheatManager::loadCheatDatabase()
}
in.close();
myListIsDirty = false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatManager::saveCheatDatabase()
{
if(!myListIsDirty)
return;
string cheatfile = myOSystem->baseDir() + BSPF_PATH_SEPARATOR + "stella.cht";
ofstream out(cheatfile.c_str(), ios::out);
if(!out)
@ -283,6 +289,7 @@ void CheatManager::saveCheatDatabase()
void CheatManager::loadCheats(const string& md5sum)
{
clear();
myCurrentCheat = "";
// Set up any cheatcodes that was on the command line
// (and remove the key from the settings, so they won't get set again)
@ -294,6 +301,10 @@ void CheatManager::loadCheats(const string& md5sum)
if(iter == myCheatMap.end() && cheats == "")
return;
// Remember the cheats for this ROM
myCurrentCheat = iter->second;
// Parse the cheat list, constructing cheats and adding them to the manager
parse(iter->second + cheats);
}
@ -310,15 +321,24 @@ void CheatManager::saveCheats(const string& md5sum)
cheats << ",";
}
bool changed = cheats.str() != myCurrentCheat;
// Only update the list if absolutely necessary
if(changed)
{
CheatCodeMap::iterator iter = myCheatMap.find(md5sum);
// Erase old entry
// Erase old entry and add a new one only if it's changed
if(iter != myCheatMap.end())
myCheatMap.erase(iter);
// Add new entry only if there are any cheats defined
if(cheats.str() != "")
myCheatMap.insert(make_pair(md5sum, cheats.str()));
}
// Update the dirty flag
myListIsDirty = myListIsDirty || changed;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: CheatManager.hxx,v 1.6 2005-12-18 18:37:01 stephena Exp $
// $Id: CheatManager.hxx,v 1.7 2006-03-22 15:12:02 stephena Exp $
//============================================================================
#ifndef CHEAT_MANAGER_HXX
@ -36,7 +36,7 @@ typedef map<string,string> CheatCodeMap;
the list of all cheats currently in use.
@author Stephen Anthony
@version $Id: CheatManager.hxx,v 1.6 2005-12-18 18:37:01 stephena Exp $
@version $Id: CheatManager.hxx,v 1.7 2006-03-22 15:12:02 stephena Exp $
*/
class CheatManager
{
@ -158,6 +158,13 @@ class CheatManager
CheatCodeMap myCheatMap;
string myCheatFile;
// This is set each time a new cheat/ROM is loaded, for later
// comparison to see if the cheatcode list has actually been modified
string myCurrentCheat;
// Indicates that the list has been modified, and should be saved to disk
bool myListIsDirty;
};
#endif

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: OSystem.cxx,v 1.64 2006-03-17 19:44:18 stephena Exp $
// $Id: OSystem.cxx,v 1.65 2006-03-22 15:12:02 stephena Exp $
//============================================================================
#include <cassert>
@ -266,6 +266,9 @@ void OSystem::createSound()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool OSystem::createConsole(const string& romfile)
{
// Delete any lingering console object
delete myConsole; myConsole = NULL;
bool retval = false, showmessage = false;
// If a blank ROM has been given, we reload the current one (assuming one exists)
@ -287,8 +290,6 @@ bool OSystem::createConsole(const string& romfile)
string md5;
if(openROM(myRomFile, md5, &image, &size))
{
delete myConsole; myConsole = NULL;
// Create an instance of the 2600 game console
// The Console c'tor takes care of updating the eventhandler state
myConsole = new Console(image, size, md5, this);