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
This commit is contained in:
stephena 2005-11-27 15:48:06 +00:00
parent ac914c699a
commit ca22361007
7 changed files with 120 additions and 80 deletions

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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 // Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project // Copyright (C) 2002-2004 The ScummVM project
@ -28,6 +28,7 @@
#include "CheatCodeDialog.hxx" #include "CheatCodeDialog.hxx"
#include "GuiUtils.hxx" #include "GuiUtils.hxx"
#include "CheckListWidget.hxx" #include "CheckListWidget.hxx"
#include "CheatManager.hxx"
#include "bspf.hxx" #include "bspf.hxx"
@ -95,17 +96,17 @@ CheatCodeDialog::~CheatCodeDialog()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatCodeDialog::loadConfig() 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; StringList l;
BoolArray b; BoolArray b;
ostringstream buf; const CheatList& list = instance()->cheat().myCheatList;
for(int i = 0; i < 10; ++i) for(unsigned int i = 0; i < list.size(); ++i)
{ {
buf << "Line " << i+1; l.push_back(list[i]->name());
l.push_back(buf.str()); b.push_back(bool(list[i]->enabled()));
b.push_back(bool(i % 2));
buf.str("");
} }
myCheatList->setList(l, b); myCheatList->setList(l, b);
} }
@ -113,9 +114,14 @@ void CheatCodeDialog::loadConfig()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CheatCodeDialog::saveConfig() void CheatCodeDialog::saveConfig()
{ {
// FIXME - connect to CheatManager // Inspect checkboxes for enable/disable codes
for(int i = 0; i < 10; ++i) for(unsigned int i = 0; i < myCheatList->getList().size(); ++i)
cerr << "Cheat " << i << ": " << myCheatList->getState(i) << endl; {
if(myCheatList->getState(i))
instance()->cheat().myCheatList[i]->enable();
else
instance()->cheat().myCheatList[i]->disable();
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -13,9 +13,11 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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 <sstream>
#include "OSystem.hxx" #include "OSystem.hxx"
#include "Cheat.hxx" #include "Cheat.hxx"
#include "CheetahCheat.hxx" #include "CheetahCheat.hxx"
@ -33,7 +35,7 @@ CheatManager::CheatManager(OSystem* osystem)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheatManager::~CheatManager() CheatManager::~CheatManager()
{ {
saveAllCheats(); saveCheatDatabase();
myCheatMap.clear(); myCheatMap.clear();
clear(); clear();
} }
@ -48,44 +50,37 @@ const Cheat* CheatManager::add(const string& name, const string& code,
Cheat* cheat = (Cheat*) NULL; Cheat* cheat = (Cheat*) NULL;
// Check if already present // Delete duplicate entries
bool duplicate = false;
for(unsigned int i = 0; i < myCheatList.size(); i++) for(unsigned int i = 0; i < myCheatList.size(); i++)
{ {
if(myCheatList[i]->code() == code) if(myCheatList[i]->code() == code)
{ {
cheat = myCheatList[i]; myCheatList.remove_at(i);
duplicate = true;
cerr << "Duplicate found: " << cheat->name() << ":" << cheat->code() << endl;
break; break;
} }
} }
// Only create new cheat when necessary // Create new cheat based on string length
if(!duplicate) switch(code.size())
{ {
switch(code.size()) case 4:
{ cheat = new RamCheat(myOSystem, name, code);
case 4: break;
cheat = new RamCheat(myOSystem, name, code);
break;
case 6: case 6:
cheat = new CheetahCheat(myOSystem, name, code); cheat = new CheetahCheat(myOSystem, name, code);
break; break;
case 8: case 8:
cheat = new BankRomCheat(myOSystem, name, code); cheat = new BankRomCheat(myOSystem, name, code);
break; break;
}
// Add the cheat to the main cheat list
if(cheat)
myCheatList.push_back(cheat);
} }
// Add the cheat to the main cheat list
if(cheat) if(cheat)
{ {
myCheatList.push_back(cheat);
// And enable/disable it (the cheat knows how to enable or disable itself) // And enable/disable it (the cheat knows how to enable or disable itself)
if(enable) if(enable)
cheat->enable(); cheat->enable();
@ -104,7 +99,7 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable)
// Make sure there are no duplicates // Make sure there are no duplicates
bool found = false; bool found = false;
unsigned int i = 0; unsigned int i;
for(i = 0; i < myPerFrameList.size(); i++) for(i = 0; i < myPerFrameList.size(); i++)
{ {
if(myPerFrameList[i]->code() == cheat->code()) if(myPerFrameList[i]->code() == cheat->code())
@ -115,11 +110,15 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable)
} }
if(enable) if(enable)
{
if(!found) if(!found)
myPerFrameList.push_back(cheat); myPerFrameList.push_back(cheat);
}
else else
{
if(found) if(found)
myPerFrameList.remove_at(i); myPerFrameList.remove_at(i);
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -174,9 +173,6 @@ void CheatManager::parse(const string& cheats)
lastPos = cheats.find_first_not_of(",", pos); lastPos = cheats.find_first_not_of(",", pos);
pos = cheats.find_first_of(",", lastPos); 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"; string cheatfile = myOSystem->baseDir() + BSPF_PATH_SEPARATOR + "stella.cht";
ifstream in(cheatfile.c_str(), ios::in); 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"; string cheatfile = myOSystem->baseDir() + BSPF_PATH_SEPARATOR + "stella.cht";
ofstream out(cheatfile.c_str(), ios::out); ofstream out(cheatfile.c_str(), ios::out);
@ -254,11 +250,41 @@ void CheatManager::loadCheats(const string& md5sum)
{ {
clear(); 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); CheatCodeMap::iterator iter = myCheatMap.find(md5sum);
if(iter == myCheatMap.end()) if(iter == myCheatMap.end() && cheats == "")
return; 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()));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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 #ifndef CHEAT_MANAGER_HXX
@ -36,10 +36,12 @@ typedef map<string,string> CheatCodeMap;
the list of all cheats currently in use. the list of all cheats currently in use.
@author Stephen Anthony @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 class CheatManager
{ {
friend class CheatCodeDialog;
public: public:
CheatManager(OSystem* osystem); CheatManager(OSystem* osystem);
virtual ~CheatManager(); virtual ~CheatManager();
@ -65,13 +67,6 @@ class CheatManager
*/ */
void addPerFrame(Cheat* cheat, bool enable); 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. 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. 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. 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). Load cheats for ROM with given MD5sum to cheatlist(s).
*/ */
void loadCheats(const string& md5sum); void loadCheats(const string& md5sum);
/**
Saves cheats for ROM with given MD5sum to cheat map.
*/
void saveCheats(const string& md5sum);
private: 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. Clear all per-ROM cheats lists.
*/ */

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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" #include "System.hxx"
@ -39,18 +39,21 @@ bool RamCheat::enable()
{ {
if(!myEnabled) if(!myEnabled)
{ {
myOSystem->cheat().addPerFrame(this, true);
myEnabled = true; myEnabled = true;
myOSystem->cheat().addPerFrame(this, myEnabled);
} }
return myEnabled; return myEnabled;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool RamCheat::disable() bool RamCheat::disable()
{ {
cerr << "perFrame remove " << myName << ":" << myCode << endl; if(myEnabled)
return false; {
myEnabled = false;
myOSystem->cheat().addPerFrame(this, myEnabled);
}
return myEnabled;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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 <fstream> #include <fstream>
@ -210,16 +210,7 @@ int main(int argc, char* argv[])
{ {
#ifdef CHEATCODE_SUPPORT #ifdef CHEATCODE_SUPPORT
// Create internal cheat database for all ROMs // Create internal cheat database for all ROMs
theOSystem->cheat().loadAllCheats(); theOSystem->cheat().loadCheatDatabase();
// 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);
}
#endif #endif
theOSystem->createConsole(romfile); theOSystem->createConsole(romfile);

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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 <assert.h> #include <assert.h>
@ -232,6 +232,10 @@ Console::Console(const Console& console)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Console::~Console() Console::~Console()
{ {
#ifdef CHEATCODE_SUPPORT
myOSystem->cheat().saveCheats(myProperties.get("Cartridge.MD5"));
#endif
delete mySystem; delete mySystem;
delete mySwitches; delete mySwitches;
delete myControllers[0]; delete myControllers[0];

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // 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 <cassert> #include <cassert>
@ -129,13 +129,6 @@ OSystem::~OSystem()
delete myFont; delete myFont;
delete myConsoleFont; delete myConsoleFont;
#ifdef DEVELOPER_SUPPORT
delete myDebugger;
#endif
#ifdef CHEATCODE_SUPPORT
delete myCheatManager;
#endif
// Remove any game console that is currently attached // Remove any game console that is currently attached
delete myConsole; delete myConsole;
@ -143,6 +136,16 @@ OSystem::~OSystem()
// since it created them // since it created them
delete myFrameBuffer; delete myFrameBuffer;
delete mySound; 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
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -