1//============================================================================
2//
3// SSSS tt lll lll
4// SS SS tt ll ll
5// SS tttttt eeee ll ll aaaa
6// SSSS tt ee ee ll ll aa
7// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8// SS SS tt ee ll ll aa aa
9// SSSS ttt eeeee llll llll aaaaa
10//
11// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
12// and the Stella Team
13//
14// See the file "License.txt" for information on usage and redistribution of
15// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16//============================================================================
17
18#ifndef CHEAT_MANAGER_HXX
19#define CHEAT_MANAGER_HXX
20
21#include <map>
22
23class Cheat;
24class OSystem;
25
26#include "bspf.hxx"
27
28using CheatList = vector<shared_ptr<Cheat>>;
29
30/**
31 This class provides an interface for performing all cheat operations
32 in Stella. It is accessible from the OSystem interface, and contains
33 the list of all cheats currently in use.
34
35 @author Stephen Anthony
36*/
37class CheatManager
38{
39 public:
40 explicit CheatManager(OSystem& osystem);
41
42 /**
43 Adds the specified cheat to an internal list.
44
45 @param name Name of the cheat (not absolutely required)
46 @param code The actual cheatcode (in hex)
47 @param enable Whether to enable this cheat right away
48 @param idx Index at which to insert the cheat
49
50 @return Whether the cheat was created and enabled.
51 */
52 bool add(const string& name, const string& code,
53 bool enable = true, int idx = -1);
54
55 /**
56 Remove the cheat at 'idx' from the cheat list(s).
57
58 @param idx Location in myCheatList of the cheat to remove
59 */
60 void remove(int idx);
61
62 /**
63 Adds the specified cheat to the internal per-frame list.
64 This method doesn't create a new cheat; it just adds/removes
65 an already created cheat to the per-frame list.
66
67 @param name Name of the cheat
68 @param code The actual cheatcode
69 @param enable Add or remove the cheat to the per-frame list
70 */
71 void addPerFrame(const string& name, const string& code, bool enable);
72
73 /**
74 Creates and enables a one-shot cheat. One-shot cheats are the
75 same as normal cheats, except they are only enabled once, and
76 they're not saved at all.
77
78 @param name Name of the cheat (not absolutely required)
79 @param code The actual cheatcode (in hex)
80 */
81 void addOneShot(const string& name, const string& code);
82
83 /**
84 Enable/disabled the cheat specified by the given code.
85
86 @param code The actual cheatcode to search for
87 @param enable Enable/disable the cheat
88 */
89 void enable(const string& code, bool enable);
90
91 /**
92 Returns the game cheatlist.
93 */
94 const CheatList& list() { return myCheatList; }
95
96 /**
97 Returns the per-frame cheatlist (needed to evaluate cheats each frame)
98 */
99 const CheatList& perFrame() { return myPerFrameList; }
100
101 /**
102 Load all cheats (for all ROMs) from disk to internal database.
103 */
104 void loadCheatDatabase();
105
106 /**
107 Save all cheats (for all ROMs) in internal database to disk.
108 */
109 void saveCheatDatabase();
110
111 /**
112 Load cheats for ROM with given MD5sum to cheatlist(s).
113 */
114 void loadCheats(const string& md5sum);
115
116 /**
117 Saves cheats for ROM with given MD5sum to cheat map.
118 */
119 void saveCheats(const string& md5sum);
120
121 /**
122 Checks if a code is valid.
123 */
124 bool isValidCode(const string& code) const;
125
126 private:
127 /**
128 Create a cheat defined by the given code.
129
130 @param name Name of the cheat (not absolutely required)
131 @param code The actual cheatcode (in hex)
132
133 @return The cheat (if was created), else nullptr.
134 */
135 shared_ptr<Cheat> createCheat(const string& name, const string& code) const;
136
137 /**
138 Parses a list of cheats and adds/enables each one.
139
140 @param cheats Comma-separated list of cheats (without any names)
141 */
142 void parse(const string& cheats);
143
144 private:
145 OSystem& myOSystem;
146
147 CheatList myCheatList;
148 CheatList myPerFrameList;
149
150 std::map<string,string> myCheatMap;
151 string myCheatFile;
152
153 // This is set each time a new cheat/ROM is loaded, for later
154 // comparison to see if the cheatcode list has actually been modified
155 string myCurrentCheat;
156
157 // Indicates that the list has been modified, and should be saved to disk
158 bool myListIsDirty;
159
160 private:
161 // Following constructors and assignment operators not supported
162 CheatManager() = delete;
163 CheatManager(const CheatManager&) = delete;
164 CheatManager(CheatManager&&) = delete;
165 CheatManager& operator=(const CheatManager&) = delete;
166 CheatManager& operator=(CheatManager&&) = delete;
167};
168
169#endif
170