From ca223610073954f33af56e3340788e5a2b2f9f63 Mon Sep 17 00:00:00 2001 From: stephena Date: Sun, 27 Nov 2005 15:48:06 +0000 Subject: [PATCH] More work on cheatcode support. The '-cheat' commandline argument now works correctly, and inserts the cheats for whatever ROM happens to be loaded. The CheatCodeDialog now communicates with CheatManager, meaning that cheats can be seen in the dialog, and enabled/disabled with their respective checkboxes. Still TODO is add support for adding/removing/ deleting cheats, and for one-shot cheats. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@896 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/cheat/CheatCodeDialog.cxx | 28 +++++---- stella/src/cheat/CheatManager.cxx | 92 ++++++++++++++++++---------- stella/src/cheat/CheatManager.hxx | 29 +++++---- stella/src/cheat/RamCheat.cxx | 13 ++-- stella/src/common/mainSDL.cxx | 13 +--- stella/src/emucore/Console.cxx | 6 +- stella/src/emucore/OSystem.cxx | 19 +++--- 7 files changed, 120 insertions(+), 80 deletions(-) diff --git a/stella/src/cheat/CheatCodeDialog.cxx b/stella/src/cheat/CheatCodeDialog.cxx index 7b806f1bb..23c2eedf6 100644 --- a/stella/src/cheat/CheatCodeDialog.cxx +++ b/stella/src/cheat/CheatCodeDialog.cxx @@ -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: CheatCodeDialog.cxx,v 1.3 2005-11-26 21:23:35 stephena Exp $ +// $Id: CheatCodeDialog.cxx,v 1.4 2005-11-27 15:48:04 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -28,6 +28,7 @@ #include "CheatCodeDialog.hxx" #include "GuiUtils.hxx" #include "CheckListWidget.hxx" +#include "CheatManager.hxx" #include "bspf.hxx" @@ -95,17 +96,17 @@ CheatCodeDialog::~CheatCodeDialog() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatCodeDialog::loadConfig() { - // FIXME - connect to CheatManager + // Load items from CheatManager + // Note that the items are always in the same order/number as given in + // the CheatManager, so the arrays will be one-to-one StringList l; BoolArray b; - ostringstream buf; - for(int i = 0; i < 10; ++i) + const CheatList& list = instance()->cheat().myCheatList; + for(unsigned int i = 0; i < list.size(); ++i) { - buf << "Line " << i+1; - l.push_back(buf.str()); - b.push_back(bool(i % 2)); - buf.str(""); + l.push_back(list[i]->name()); + b.push_back(bool(list[i]->enabled())); } myCheatList->setList(l, b); } @@ -113,9 +114,14 @@ void CheatCodeDialog::loadConfig() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatCodeDialog::saveConfig() { - // FIXME - connect to CheatManager - for(int i = 0; i < 10; ++i) - cerr << "Cheat " << i << ": " << myCheatList->getState(i) << endl; + // Inspect checkboxes for enable/disable codes + for(unsigned int i = 0; i < myCheatList->getList().size(); ++i) + { + if(myCheatList->getState(i)) + instance()->cheat().myCheatList[i]->enable(); + else + instance()->cheat().myCheatList[i]->disable(); + } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/stella/src/cheat/CheatManager.cxx b/stella/src/cheat/CheatManager.cxx index 404ba904d..38106c838 100644 --- a/stella/src/cheat/CheatManager.cxx +++ b/stella/src/cheat/CheatManager.cxx @@ -13,9 +13,11 @@ // 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.2 2005-11-27 00:17:16 stephena Exp $ +// $Id: CheatManager.cxx,v 1.3 2005-11-27 15:48:05 stephena Exp $ //============================================================================ +#include + #include "OSystem.hxx" #include "Cheat.hxx" #include "CheetahCheat.hxx" @@ -33,7 +35,7 @@ CheatManager::CheatManager(OSystem* osystem) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CheatManager::~CheatManager() { - saveAllCheats(); + saveCheatDatabase(); myCheatMap.clear(); clear(); } @@ -48,44 +50,37 @@ const Cheat* CheatManager::add(const string& name, const string& code, Cheat* cheat = (Cheat*) NULL; - // Check if already present - bool duplicate = false; + // Delete duplicate entries for(unsigned int i = 0; i < myCheatList.size(); i++) { if(myCheatList[i]->code() == code) { - cheat = myCheatList[i]; - duplicate = true; -cerr << "Duplicate found: " << cheat->name() << ":" << cheat->code() << endl; + myCheatList.remove_at(i); break; } } - // Only create new cheat when necessary - if(!duplicate) + // Create new cheat based on string length + switch(code.size()) { - switch(code.size()) - { - case 4: - cheat = new RamCheat(myOSystem, name, code); - break; + case 4: + cheat = new RamCheat(myOSystem, name, code); + break; - case 6: - cheat = new CheetahCheat(myOSystem, name, code); - break; + case 6: + cheat = new CheetahCheat(myOSystem, name, code); + break; - case 8: - cheat = new BankRomCheat(myOSystem, name, code); - break; - } - - // Add the cheat to the main cheat list - if(cheat) - myCheatList.push_back(cheat); + case 8: + cheat = new BankRomCheat(myOSystem, name, code); + break; } + // Add the cheat to the main cheat list if(cheat) { + myCheatList.push_back(cheat); + // And enable/disable it (the cheat knows how to enable or disable itself) if(enable) cheat->enable(); @@ -104,7 +99,7 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable) // Make sure there are no duplicates bool found = false; - unsigned int i = 0; + unsigned int i; for(i = 0; i < myPerFrameList.size(); i++) { if(myPerFrameList[i]->code() == cheat->code()) @@ -115,11 +110,15 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable) } if(enable) + { if(!found) myPerFrameList.push_back(cheat); + } else + { if(found) myPerFrameList.remove_at(i); + } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -174,9 +173,6 @@ void CheatManager::parse(const string& cheats) lastPos = cheats.find_first_not_of(",", pos); pos = cheats.find_first_of(",", lastPos); } - - -// add("TEST", cheats, true); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -196,7 +192,7 @@ void CheatManager::enable(const string& code, bool enable) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void CheatManager::loadAllCheats() +void CheatManager::loadCheatDatabase() { string cheatfile = myOSystem->baseDir() + BSPF_PATH_SEPARATOR + "stella.cht"; ifstream in(cheatfile.c_str(), ios::in); @@ -233,7 +229,7 @@ void CheatManager::loadAllCheats() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void CheatManager::saveAllCheats() +void CheatManager::saveCheatDatabase() { string cheatfile = myOSystem->baseDir() + BSPF_PATH_SEPARATOR + "stella.cht"; ofstream out(cheatfile.c_str(), ios::out); @@ -254,11 +250,41 @@ void CheatManager::loadCheats(const string& md5sum) { clear(); + // Set up any cheatcodes that was on the command line + // (and remove the key from the settings, so they won't get set again) + string cheats = myOSystem->settings().getString("cheat"); + if(cheats != "") + myOSystem->settings().setString("cheat", "", false); + CheatCodeMap::iterator iter = myCheatMap.find(md5sum); - if(iter == myCheatMap.end()) + if(iter == myCheatMap.end() && cheats == "") return; - parse(iter->second); + parse(iter->second + cheats); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void CheatManager::saveCheats(const string& md5sum) +{ + ostringstream cheats; + for(unsigned int i = 0; i < myCheatList.size(); i++) + { + cheats << myCheatList[i]->name() << ":" + << myCheatList[i]->code() << ":" + << myCheatList[i]->enabled(); + if(i+1 < myCheatList.size()) + cheats << ","; + } + + CheatCodeMap::iterator iter = myCheatMap.find(md5sum); + + // Erase old entry + 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())); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/stella/src/cheat/CheatManager.hxx b/stella/src/cheat/CheatManager.hxx index ac5c2019d..92aebffe8 100644 --- a/stella/src/cheat/CheatManager.hxx +++ b/stella/src/cheat/CheatManager.hxx @@ -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.2 2005-11-27 00:17:16 stephena Exp $ +// $Id: CheatManager.hxx,v 1.3 2005-11-27 15:48:05 stephena Exp $ //============================================================================ #ifndef CHEAT_MANAGER_HXX @@ -36,10 +36,12 @@ typedef map CheatCodeMap; the list of all cheats currently in use. @author Stephen Anthony - @version $Id: CheatManager.hxx,v 1.2 2005-11-27 00:17:16 stephena Exp $ + @version $Id: CheatManager.hxx,v 1.3 2005-11-27 15:48:05 stephena Exp $ */ class CheatManager { + friend class CheatCodeDialog; + public: CheatManager(OSystem* osystem); virtual ~CheatManager(); @@ -65,13 +67,6 @@ class CheatManager */ void addPerFrame(Cheat* cheat, bool enable); - /** - Parses a list of cheats and adds/enables each one. - - @param cheats Comma-separated list of cheats (without any names) - */ - void parse(const string& cheats); - /** Enable/disabled the cheat specified by the given code. @@ -88,19 +83,31 @@ class CheatManager /** Load all cheats (for all ROMs) from disk to internal database. */ - void loadAllCheats(); + void loadCheatDatabase(); /** Save all cheats (for all ROMs) in internal database to disk. */ - void saveAllCheats(); + void saveCheatDatabase(); /** Load cheats for ROM with given MD5sum to cheatlist(s). */ void loadCheats(const string& md5sum); + /** + Saves cheats for ROM with given MD5sum to cheat map. + */ + void saveCheats(const string& md5sum); + private: + /** + Parses a list of cheats and adds/enables each one. + + @param cheats Comma-separated list of cheats (without any names) + */ + void parse(const string& cheats); + /** Clear all per-ROM cheats lists. */ diff --git a/stella/src/cheat/RamCheat.cxx b/stella/src/cheat/RamCheat.cxx index e343f2e25..d21c24c0a 100644 --- a/stella/src/cheat/RamCheat.cxx +++ b/stella/src/cheat/RamCheat.cxx @@ -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: RamCheat.cxx,v 1.1 2005-11-11 21:44:18 stephena Exp $ +// $Id: RamCheat.cxx,v 1.2 2005-11-27 15:48:05 stephena Exp $ //============================================================================ #include "System.hxx" @@ -39,18 +39,21 @@ bool RamCheat::enable() { if(!myEnabled) { - myOSystem->cheat().addPerFrame(this, true); myEnabled = true; + myOSystem->cheat().addPerFrame(this, myEnabled); } - return myEnabled; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool RamCheat::disable() { - cerr << "perFrame remove " << myName << ":" << myCode << endl; - return false; + if(myEnabled) + { + myEnabled = false; + myOSystem->cheat().addPerFrame(this, myEnabled); + } + return myEnabled; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/stella/src/common/mainSDL.cxx b/stella/src/common/mainSDL.cxx index 4e4fc1800..8f0858a02 100644 --- a/stella/src/common/mainSDL.cxx +++ b/stella/src/common/mainSDL.cxx @@ -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: mainSDL.cxx,v 1.55 2005-11-27 00:17:16 stephena Exp $ +// $Id: mainSDL.cxx,v 1.56 2005-11-27 15:48:05 stephena Exp $ //============================================================================ #include @@ -210,16 +210,7 @@ int main(int argc, char* argv[]) { #ifdef CHEATCODE_SUPPORT // Create internal cheat database for all ROMs - theOSystem->cheat().loadAllCheats(); - - // Set up any cheeetah code that was on the command line - // (and remove the key from the settings, so they won't get set again) - string cheats = theOSystem->settings().getString("cheat"); - if(cheats != "") - { - theOSystem->cheat().parse(cheats); - theOSystem->settings().setString("cheat", "", false); - } + theOSystem->cheat().loadCheatDatabase(); #endif theOSystem->createConsole(romfile); diff --git a/stella/src/emucore/Console.cxx b/stella/src/emucore/Console.cxx index e61d43a63..3cab2b627 100644 --- a/stella/src/emucore/Console.cxx +++ b/stella/src/emucore/Console.cxx @@ -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: Console.cxx,v 1.75 2005-11-27 00:17:16 stephena Exp $ +// $Id: Console.cxx,v 1.76 2005-11-27 15:48:06 stephena Exp $ //============================================================================ #include @@ -232,6 +232,10 @@ Console::Console(const Console& console) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Console::~Console() { +#ifdef CHEATCODE_SUPPORT + myOSystem->cheat().saveCheats(myProperties.get("Cartridge.MD5")); +#endif + delete mySystem; delete mySwitches; delete myControllers[0]; diff --git a/stella/src/emucore/OSystem.cxx b/stella/src/emucore/OSystem.cxx index d479541e5..f3d964b3b 100644 --- a/stella/src/emucore/OSystem.cxx +++ b/stella/src/emucore/OSystem.cxx @@ -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.47 2005-11-26 21:23:35 stephena Exp $ +// $Id: OSystem.cxx,v 1.48 2005-11-27 15:48:06 stephena Exp $ //============================================================================ #include @@ -129,13 +129,6 @@ OSystem::~OSystem() delete myFont; delete myConsoleFont; -#ifdef DEVELOPER_SUPPORT - delete myDebugger; -#endif -#ifdef CHEATCODE_SUPPORT - delete myCheatManager; -#endif - // Remove any game console that is currently attached delete myConsole; @@ -143,6 +136,16 @@ OSystem::~OSystem() // since it created them delete myFrameBuffer; delete mySound; + + // These must be deleted after all the others + // This is a bit hacky, since it depends on ordering + // of d'tor calls +#ifdef DEVELOPER_SUPPORT + delete myDebugger; +#endif +#ifdef CHEATCODE_SUPPORT + delete myCheatManager; +#endif } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -