1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2010-2014 - Hans-Kristian Arntzen
3  *  Copyright (C) 2011-2017 - Daniel De Matteis
4  *
5  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
6  *  of the GNU General Public License as published by the Free Software Found-
7  *  ation, either version 3 of the License, or (at your option) any later version.
8  *
9  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  *  PURPOSE.  See the GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License along with RetroArch.
14  *  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef __CHEAT_MANAGER_H
18 #define __CHEAT_MANAGER_H
19 
20 #include <boolean.h>
21 #include <retro_common_api.h>
22 
23 #include "../setting_list.h"
24 
25 RETRO_BEGIN_DECLS
26 
27 enum cheat_handler_type
28 {
29    CHEAT_HANDLER_TYPE_EMU = 0,
30    CHEAT_HANDLER_TYPE_RETRO,
31    CHEAT_HANDLER_TYPE_END
32 };
33 
34 enum cheat_type
35 {
36    CHEAT_TYPE_DISABLED = 0,
37    CHEAT_TYPE_SET_TO_VALUE,
38    CHEAT_TYPE_INCREASE_VALUE,
39    CHEAT_TYPE_DECREASE_VALUE,
40    CHEAT_TYPE_RUN_NEXT_IF_EQ,
41    CHEAT_TYPE_RUN_NEXT_IF_NEQ,
42    CHEAT_TYPE_RUN_NEXT_IF_LT,
43    CHEAT_TYPE_RUN_NEXT_IF_GT
44 };
45 
46 enum cheat_search_type
47 {
48    CHEAT_SEARCH_TYPE_EXACT = 0,
49    CHEAT_SEARCH_TYPE_LT,
50    CHEAT_SEARCH_TYPE_LTE,
51    CHEAT_SEARCH_TYPE_GT,
52    CHEAT_SEARCH_TYPE_GTE,
53    CHEAT_SEARCH_TYPE_EQ,
54    CHEAT_SEARCH_TYPE_NEQ,
55    CHEAT_SEARCH_TYPE_EQPLUS,
56    CHEAT_SEARCH_TYPE_EQMINUS
57 };
58 
59 enum cheat_match_action_type
60 {
61    CHEAT_MATCH_ACTION_TYPE_VIEW = 0,
62    CHEAT_MATCH_ACTION_TYPE_DELETE,
63    CHEAT_MATCH_ACTION_TYPE_COPY,
64    CHEAT_MATCH_ACTION_TYPE_BROWSE
65 };
66 
67 enum cheat_rumble_type
68 {
69    RUMBLE_TYPE_DISABLED = 0,
70    RUMBLE_TYPE_CHANGES,
71    RUMBLE_TYPE_DOES_NOT_CHANGE,
72    RUMBLE_TYPE_INCREASE,
73    RUMBLE_TYPE_DECREASE,
74    RUMBLE_TYPE_EQ_VALUE,
75    RUMBLE_TYPE_NEQ_VALUE,
76    RUMBLE_TYPE_LT_VALUE,
77    RUMBLE_TYPE_GT_VALUE,
78    RUMBLE_TYPE_INCREASE_BY_VALUE,
79    RUMBLE_TYPE_DECREASE_BY_VALUE,
80    RUMBLE_TYPE_END_LIST
81 };
82 
83 /* Some codes are ridiculously large - over 10000 bytes */
84 #define CHEAT_CODE_SCRATCH_SIZE 16*1024
85 #define CHEAT_DESC_SCRATCH_SIZE 255
86 
87 struct item_cheat
88 {
89    /* Clock value for when rumbling should stop */
90    retro_time_t rumble_primary_end_time;
91    retro_time_t rumble_secondary_end_time;
92 
93    char *desc;
94    char *code;
95 
96    unsigned int idx;
97    unsigned int handler;
98    /* Number of bits = 2^memory_search_size
99     * 0=1, 1=2, 2=4, 3=8, 4=16, 5=32
100     */
101    unsigned int memory_search_size;
102    unsigned int cheat_type;
103    unsigned int value;
104    unsigned int address;
105    /*
106     * address_mask used when memory_search_size <8 bits
107     * if memory_search_size=0, then the number of bits is 1 and this value can be one of the following:
108     * 0 : 00000001
109     * 1 : 00000010
110     * 2 : 00000100
111     * 3 : 00001000
112     * 4 : 00010000
113     * 5 : 00100000
114     * 6 : 01000000
115     * 7 : 10000000
116     * if memory_search_size=1, then the number of bits is 2 and this value can be one of the following:
117     * 0 : 00000011
118     * 1 : 00001100
119     * 2 : 00110000
120     * 3 : 11000000
121     * if memory_search_size=2, then the number of bits is 4 and this value can be one of the following:
122     * 0 : 00001111
123     * 1 : 11110000
124     */
125    unsigned int address_mask;
126    unsigned int rumble_type;
127    unsigned int rumble_value;
128    unsigned int rumble_prev_value;
129    unsigned int rumble_initialized;
130    /* 0-15 for specific port, anything else means "all ports" */
131    unsigned int rumble_port;
132    unsigned int rumble_primary_strength; /* 0-65535 */
133    unsigned int rumble_primary_duration; /* in milliseconds */
134    unsigned int rumble_secondary_strength; /* 0-65535 */
135    unsigned int rumble_secondary_duration; /* in milliseconds */
136 
137    /*
138     * The repeat_ variables allow for a single cheat code to affect multiple memory addresses.
139     * repeat_count - the number of times the cheat code should be applied
140     * repeat_add_to_value - every iteration of repeat_count will have this amount added to item_cheat.value
141     * repeat_add_to_address - every iteration of repeat_count will have this amount added to item_cheat.address
142     *
143     * Note that repeat_add_to_address represents the number of "memory_search_size" blocks to add to
144     * item_cheat.address.  If memory_seach_size is 16-bits and repeat_add_to_address is 2, then item_cheat.address
145     * will be increased by 4 bytes 2*(16-bits) for every iteration.
146     *
147     * This is a cheating structure used for codes like unlocking all levels, giving yourself 1 of every item,etc.
148     */
149    unsigned int repeat_count;
150    unsigned int repeat_add_to_value;
151    unsigned int repeat_add_to_address;
152 
153    bool state;
154    /* Whether to apply the cheat based on big-endian console memory or not */
155    bool big_endian;
156 };
157 
158 struct cheat_manager
159 {
160    struct item_cheat working_cheat; /* retro_time_t alignment */
161    struct item_cheat *cheats;
162    uint8_t *curr_memory_buf;
163    uint8_t *prev_memory_buf;
164    uint8_t *matches;
165    uint8_t **memory_buf_list;
166    unsigned *memory_size_list;
167    unsigned int delete_state;
168    unsigned int loading_cheat_size;
169    unsigned int loading_cheat_offset;
170    unsigned ptr;
171    unsigned size;
172    unsigned buf_size;
173    unsigned total_memory_size;
174    unsigned num_memory_buffers;
175    unsigned match_idx;
176    unsigned match_action;
177    unsigned search_bit_size;
178    unsigned dummy;
179    unsigned search_exact_value;
180    unsigned search_eqplus_value;
181    unsigned search_eqminus_value;
182    unsigned num_matches;
183    unsigned browse_address;
184    char working_desc[CHEAT_DESC_SCRATCH_SIZE];
185    char working_code[CHEAT_CODE_SCRATCH_SIZE];
186    bool  big_endian;
187    bool  memory_initialized;
188    bool  memory_search_initialized;
189 };
190 
191 typedef struct cheat_manager cheat_manager_t;
192 
193 extern cheat_manager_t cheat_manager_state;
194 
195 unsigned cheat_manager_get_size(void);
196 
197 bool cheat_manager_load(const char *path, bool append);
198 
199 /**
200  * cheat_manager_save:
201  * @path                      : Path to cheats file (absolute path).
202  *
203  * Saves cheats to file on disk.
204  *
205  * Returns: true (1) if successful, otherwise false (0).
206  **/
207 bool cheat_manager_save(const char *path,
208       const char *cheat_database, bool overwrite);
209 
210 bool cheat_manager_realloc(unsigned new_size, unsigned default_handler);
211 
212 void cheat_manager_set_code(unsigned index, const char *str);
213 
214 void cheat_manager_index_next(void);
215 
216 void cheat_manager_index_prev(void);
217 
218 void cheat_manager_toggle(void);
219 
220 void cheat_manager_apply_cheats(void);
221 
222 void cheat_manager_update(cheat_manager_t *handle, unsigned handle_idx);
223 
224 void cheat_manager_toggle_index(bool apply_cheats_after_toggle,
225       unsigned i);
226 
227 unsigned cheat_manager_get_buf_size(void);
228 
229 const char *cheat_manager_get_desc(unsigned i);
230 
231 const char *cheat_manager_get_code(unsigned i);
232 
233 bool cheat_manager_get_code_state(unsigned i);
234 
235 void cheat_manager_state_free(void);
236 
237 bool cheat_manager_alloc_if_empty(void);
238 
239 bool cheat_manager_copy_idx_to_working(unsigned idx);
240 
241 bool cheat_manager_copy_working_to_idx(unsigned idx);
242 
243 void cheat_manager_load_game_specific_cheats(const char *path_cheat_database);
244 
245 void cheat_manager_save_game_specific_cheats(const char *path_cheat_database);
246 
247 int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool wraparound);
248 
249 int cheat_manager_search_exact(rarch_setting_t *setting, size_t idx, bool wraparound);
250 
251 int cheat_manager_search_lt(rarch_setting_t *setting, size_t idx, bool wraparound);
252 
253 int cheat_manager_search_gt(rarch_setting_t *setting, size_t idx, bool wraparound);
254 
255 int cheat_manager_search_lte(rarch_setting_t *setting, size_t idx, bool wraparound);
256 
257 int cheat_manager_search_gte(rarch_setting_t *setting, size_t idx, bool wraparound);
258 
259 int cheat_manager_search_eq(rarch_setting_t *setting, size_t idx, bool wraparound);
260 
261 int cheat_manager_search_neq(rarch_setting_t *setting, size_t idx, bool wraparound);
262 
263 int cheat_manager_search_eqplus(rarch_setting_t *setting, size_t idx, bool wraparound);
264 
265 int cheat_manager_search_eqminus(rarch_setting_t *setting, size_t idx, bool wraparound);
266 
267 unsigned cheat_manager_get_state_search_size(unsigned search_size);
268 
269 int cheat_manager_add_matches(const char *path,
270       const char *label, unsigned type, size_t idx, size_t entry_idx);
271 
272 void cheat_manager_apply_retro_cheats(void);
273 
274 void cheat_manager_match_action(
275       enum cheat_match_action_type match_action,
276       unsigned int target_match_idx,
277       unsigned int *address, unsigned int *address_mask,
278       unsigned int *prev_value, unsigned int *curr_value);
279 
280 int cheat_manager_copy_match(rarch_setting_t *setting, size_t idx, bool wraparound);
281 
282 int cheat_manager_delete_match(rarch_setting_t *setting, size_t idx, bool wraparound);
283 
284 RETRO_END_DECLS
285 
286 #endif
287