cheat_manager.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #ifndef __CHEAT_MANAGER_H
  17. #define __CHEAT_MANAGER_H
  18. #include <boolean.h>
  19. #include <retro_common_api.h>
  20. #include "../setting_list.h"
  21. RETRO_BEGIN_DECLS
  22. enum cheat_handler_type
  23. {
  24. CHEAT_HANDLER_TYPE_EMU = 0,
  25. CHEAT_HANDLER_TYPE_RETRO,
  26. CHEAT_HANDLER_TYPE_END
  27. };
  28. enum cheat_type
  29. {
  30. CHEAT_TYPE_DISABLED = 0,
  31. CHEAT_TYPE_SET_TO_VALUE,
  32. CHEAT_TYPE_INCREASE_VALUE,
  33. CHEAT_TYPE_DECREASE_VALUE,
  34. CHEAT_TYPE_RUN_NEXT_IF_EQ,
  35. CHEAT_TYPE_RUN_NEXT_IF_NEQ,
  36. CHEAT_TYPE_RUN_NEXT_IF_LT,
  37. CHEAT_TYPE_RUN_NEXT_IF_GT
  38. };
  39. enum cheat_search_type
  40. {
  41. CHEAT_SEARCH_TYPE_EXACT = 0,
  42. CHEAT_SEARCH_TYPE_LT,
  43. CHEAT_SEARCH_TYPE_LTE,
  44. CHEAT_SEARCH_TYPE_GT,
  45. CHEAT_SEARCH_TYPE_GTE,
  46. CHEAT_SEARCH_TYPE_EQ,
  47. CHEAT_SEARCH_TYPE_NEQ,
  48. CHEAT_SEARCH_TYPE_EQPLUS,
  49. CHEAT_SEARCH_TYPE_EQMINUS
  50. };
  51. enum cheat_match_action_type
  52. {
  53. CHEAT_MATCH_ACTION_TYPE_VIEW = 0,
  54. CHEAT_MATCH_ACTION_TYPE_DELETE,
  55. CHEAT_MATCH_ACTION_TYPE_COPY,
  56. CHEAT_MATCH_ACTION_TYPE_BROWSE
  57. };
  58. enum cheat_rumble_type
  59. {
  60. RUMBLE_TYPE_DISABLED = 0,
  61. RUMBLE_TYPE_CHANGES,
  62. RUMBLE_TYPE_DOES_NOT_CHANGE,
  63. RUMBLE_TYPE_INCREASE,
  64. RUMBLE_TYPE_DECREASE,
  65. RUMBLE_TYPE_EQ_VALUE,
  66. RUMBLE_TYPE_NEQ_VALUE,
  67. RUMBLE_TYPE_LT_VALUE,
  68. RUMBLE_TYPE_GT_VALUE,
  69. RUMBLE_TYPE_INCREASE_BY_VALUE,
  70. RUMBLE_TYPE_DECREASE_BY_VALUE,
  71. RUMBLE_TYPE_END_LIST
  72. };
  73. /* Some codes are ridiculously large - over 10000 bytes */
  74. #define CHEAT_CODE_SCRATCH_SIZE 16*1024
  75. #define CHEAT_DESC_SCRATCH_SIZE 255
  76. struct item_cheat
  77. {
  78. /* Clock value for when rumbling should stop */
  79. retro_time_t rumble_primary_end_time;
  80. retro_time_t rumble_secondary_end_time;
  81. char *desc;
  82. char *code;
  83. unsigned int idx;
  84. unsigned int handler;
  85. /* Number of bits = 2^memory_search_size
  86. * 0=1, 1=2, 2=4, 3=8, 4=16, 5=32
  87. */
  88. unsigned int memory_search_size;
  89. unsigned int cheat_type;
  90. unsigned int value;
  91. unsigned int address;
  92. /*
  93. * address_mask used when memory_search_size <8 bits
  94. * if memory_search_size=0, then the number of bits is 1 and this value can be one of the following:
  95. * 0 : 00000001
  96. * 1 : 00000010
  97. * 2 : 00000100
  98. * 3 : 00001000
  99. * 4 : 00010000
  100. * 5 : 00100000
  101. * 6 : 01000000
  102. * 7 : 10000000
  103. * if memory_search_size=1, then the number of bits is 2 and this value can be one of the following:
  104. * 0 : 00000011
  105. * 1 : 00001100
  106. * 2 : 00110000
  107. * 3 : 11000000
  108. * if memory_search_size=2, then the number of bits is 4 and this value can be one of the following:
  109. * 0 : 00001111
  110. * 1 : 11110000
  111. */
  112. unsigned int address_mask;
  113. unsigned int rumble_type;
  114. unsigned int rumble_value;
  115. unsigned int rumble_prev_value;
  116. unsigned int rumble_initialized;
  117. /* 0-15 for specific port, anything else means "all ports" */
  118. unsigned int rumble_port;
  119. unsigned int rumble_primary_strength; /* 0-65535 */
  120. unsigned int rumble_primary_duration; /* in milliseconds */
  121. unsigned int rumble_secondary_strength; /* 0-65535 */
  122. unsigned int rumble_secondary_duration; /* in milliseconds */
  123. /*
  124. * The repeat_ variables allow for a single cheat code to affect multiple memory addresses.
  125. * repeat_count - the number of times the cheat code should be applied
  126. * repeat_add_to_value - every iteration of repeat_count will have this amount added to item_cheat.value
  127. * repeat_add_to_address - every iteration of repeat_count will have this amount added to item_cheat.address
  128. *
  129. * Note that repeat_add_to_address represents the number of "memory_search_size" blocks to add to
  130. * item_cheat.address. If memory_seach_size is 16-bits and repeat_add_to_address is 2, then item_cheat.address
  131. * will be increased by 4 bytes 2*(16-bits) for every iteration.
  132. *
  133. * This is a cheating structure used for codes like unlocking all levels, giving yourself 1 of every item,etc.
  134. */
  135. unsigned int repeat_count;
  136. unsigned int repeat_add_to_value;
  137. unsigned int repeat_add_to_address;
  138. bool state;
  139. /* Whether to apply the cheat based on big-endian console memory or not */
  140. bool big_endian;
  141. };
  142. struct cheat_manager
  143. {
  144. struct item_cheat working_cheat; /* retro_time_t alignment */
  145. struct item_cheat *cheats;
  146. uint8_t *curr_memory_buf;
  147. uint8_t *prev_memory_buf;
  148. uint8_t *matches;
  149. uint8_t **memory_buf_list;
  150. unsigned *memory_size_list;
  151. unsigned int delete_state;
  152. unsigned int loading_cheat_size;
  153. unsigned int loading_cheat_offset;
  154. unsigned ptr;
  155. unsigned size;
  156. unsigned buf_size;
  157. unsigned total_memory_size;
  158. unsigned num_memory_buffers;
  159. unsigned match_idx;
  160. unsigned match_action;
  161. unsigned search_bit_size;
  162. unsigned dummy;
  163. unsigned search_exact_value;
  164. unsigned search_eqplus_value;
  165. unsigned search_eqminus_value;
  166. unsigned num_matches;
  167. unsigned browse_address;
  168. char working_desc[CHEAT_DESC_SCRATCH_SIZE];
  169. char working_code[CHEAT_CODE_SCRATCH_SIZE];
  170. bool big_endian;
  171. bool memory_initialized;
  172. bool memory_search_initialized;
  173. };
  174. typedef struct cheat_manager cheat_manager_t;
  175. extern cheat_manager_t cheat_manager_state;
  176. unsigned cheat_manager_get_size(void);
  177. bool cheat_manager_load(const char *path, bool append);
  178. /**
  179. * cheat_manager_save:
  180. * @path : Path to cheats file (absolute path).
  181. *
  182. * Saves cheats to file on disk.
  183. *
  184. * Returns: true (1) if successful, otherwise false (0).
  185. **/
  186. bool cheat_manager_save(const char *path,
  187. const char *cheat_database, bool overwrite);
  188. bool cheat_manager_realloc(unsigned new_size, unsigned default_handler);
  189. void cheat_manager_set_code(unsigned index, const char *str);
  190. void cheat_manager_index_next(void);
  191. void cheat_manager_index_prev(void);
  192. void cheat_manager_toggle(void);
  193. void cheat_manager_apply_cheats(void);
  194. void cheat_manager_update(cheat_manager_t *handle, unsigned handle_idx);
  195. void cheat_manager_toggle_index(bool apply_cheats_after_toggle,
  196. unsigned i);
  197. unsigned cheat_manager_get_buf_size(void);
  198. const char *cheat_manager_get_desc(unsigned i);
  199. const char *cheat_manager_get_code(unsigned i);
  200. bool cheat_manager_get_code_state(unsigned i);
  201. void cheat_manager_state_free(void);
  202. bool cheat_manager_alloc_if_empty(void);
  203. bool cheat_manager_copy_idx_to_working(unsigned idx);
  204. bool cheat_manager_copy_working_to_idx(unsigned idx);
  205. void cheat_manager_load_game_specific_cheats(const char *path_cheat_database);
  206. void cheat_manager_save_game_specific_cheats(const char *path_cheat_database);
  207. int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool wraparound);
  208. int cheat_manager_search_exact(rarch_setting_t *setting, size_t idx, bool wraparound);
  209. int cheat_manager_search_lt(rarch_setting_t *setting, size_t idx, bool wraparound);
  210. int cheat_manager_search_gt(rarch_setting_t *setting, size_t idx, bool wraparound);
  211. int cheat_manager_search_lte(rarch_setting_t *setting, size_t idx, bool wraparound);
  212. int cheat_manager_search_gte(rarch_setting_t *setting, size_t idx, bool wraparound);
  213. int cheat_manager_search_eq(rarch_setting_t *setting, size_t idx, bool wraparound);
  214. int cheat_manager_search_neq(rarch_setting_t *setting, size_t idx, bool wraparound);
  215. int cheat_manager_search_eqplus(rarch_setting_t *setting, size_t idx, bool wraparound);
  216. int cheat_manager_search_eqminus(rarch_setting_t *setting, size_t idx, bool wraparound);
  217. unsigned cheat_manager_get_state_search_size(unsigned search_size);
  218. int cheat_manager_add_matches(const char *path,
  219. const char *label, unsigned type, size_t idx, size_t entry_idx);
  220. void cheat_manager_apply_retro_cheats(void);
  221. void cheat_manager_match_action(
  222. enum cheat_match_action_type match_action,
  223. unsigned int target_match_idx,
  224. unsigned int *address, unsigned int *address_mask,
  225. unsigned int *prev_value, unsigned int *curr_value);
  226. int cheat_manager_copy_match(rarch_setting_t *setting, size_t idx, bool wraparound);
  227. int cheat_manager_delete_match(rarch_setting_t *setting, size_t idx, bool wraparound);
  228. RETRO_END_DECLS
  229. #endif