Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/core/cheats.h
4211 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]> and contributors.
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "common/bitfield.h"
7
8
#include "types.h"
9
10
#include <functional>
11
#include <memory>
12
#include <optional>
13
#include <string>
14
#include <string_view>
15
#include <utility>
16
#include <vector>
17
18
class Error;
19
20
namespace Cheats {
21
enum class CodeType : u8
22
{
23
Gameshark,
24
Count
25
};
26
27
enum class CodeActivation : u8
28
{
29
Manual,
30
EndFrame,
31
Count,
32
};
33
34
enum class FileFormat : u8
35
{
36
Unknown,
37
DuckStation,
38
PCSX,
39
Libretro,
40
EPSXe,
41
Count
42
};
43
44
using CodeOption = std::pair<std::string, u32>;
45
using CodeOptionList = std::vector<CodeOption>;
46
47
/// Contains all the information required to present a cheat code to the user.
48
struct CodeInfo
49
{
50
std::string name;
51
std::string author;
52
std::string description;
53
std::string body;
54
CodeOptionList options;
55
u16 option_range_start = 0;
56
u16 option_range_end = 0;
57
u32 file_offset_start = 0;
58
u32 file_offset_body_start = 0;
59
u32 file_offset_end = 0;
60
CodeType type = CodeType::Gameshark;
61
CodeActivation activation = CodeActivation::EndFrame;
62
bool from_database = false;
63
bool disallow_for_achievements = false;
64
65
std::string_view GetNamePart() const;
66
std::string_view GetNameParentPart() const;
67
68
bool HasOptionChoices() const { return (!options.empty()); }
69
bool HasOptionRange() const { return (option_range_end > option_range_start); }
70
std::string_view MapOptionValueToName(u32 value) const;
71
std::string_view MapOptionValueToName(const std::string_view value) const;
72
u32 MapOptionNameToValue(const std::string_view opt_name) const;
73
};
74
75
using CodeInfoList = std::vector<CodeInfo>;
76
77
/// Returns the internal identifier for a code type.
78
extern const char* GetTypeName(CodeType type);
79
80
/// Returns the human-readable name for a code type.
81
extern const char* GetTypeDisplayName(CodeType type);
82
83
/// Parses an internal identifier, returning the code type.
84
extern std::optional<CodeType> ParseTypeName(const std::string_view str);
85
86
/// Returns the internal identifier for a code activation.
87
extern const char* GetActivationName(CodeActivation activation);
88
89
/// Returns the human-readable name for a code activation.
90
extern const char* GetActivationDisplayName(CodeActivation activation);
91
92
/// Parses an internal identifier, returning the activation type.
93
extern std::optional<CodeActivation> ParseActivationName(const std::string_view str);
94
95
/// Returns a list of all available cheats/patches for a given game.
96
extern CodeInfoList GetCodeInfoList(const std::string_view serial, std::optional<GameHash> hash, bool cheats,
97
bool load_from_database, bool sort_by_name);
98
99
/// Returns a list of all unique prefixes/groups for a cheat list.
100
extern std::vector<std::string_view> GetCodeListUniquePrefixes(const CodeInfoList& list, bool include_empty);
101
102
/// Searches for a given code by name.
103
extern const CodeInfo* FindCodeInInfoList(const CodeInfoList& list, const std::string_view name);
104
105
/// Searches for a given code by name.
106
extern CodeInfo* FindCodeInInfoList(CodeInfoList& list, const std::string_view name);
107
108
/// Formats the given cheat code in the format that it would be saved to a file.
109
extern std::string FormatCodeForFile(const CodeInfo& code);
110
111
/// Imports all codes from the provided string.
112
extern bool ImportCodesFromString(CodeInfoList* dst, const std::string_view file_contents, FileFormat file_format,
113
bool stop_on_error, Error* error);
114
115
/// Exports codes to the given file, in DuckStation format.
116
extern bool ExportCodesToFile(std::string path, const CodeInfoList& codes, Error* error);
117
118
/// Adds, updates, or removes the specified code from the file, rewriting it. If code is null, it will be removed.
119
extern bool UpdateCodeInFile(const char* path, const std::string_view name, const CodeInfo* code, Error* error);
120
121
/// Updates or adds multiple codes to the file, rewriting it.
122
extern bool SaveCodesToFile(const char* path, const CodeInfoList& codes, Error* error);
123
124
/// Removes any .cht files for the specified game.
125
extern void RemoveAllCodes(const std::string_view serial, const std::string_view title, std::optional<GameHash> hash);
126
127
/// Returns the path to a new cheat/patch cht for the specified serial and hash.
128
extern std::string GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats);
129
130
/// Reloads cheats and game patches. The parameters control the degree to which data is reloaded.
131
extern void ReloadCheats(bool reload_files, bool reload_enabled_list, bool verbose, bool verbose_if_changed,
132
bool show_disabled_codes);
133
134
/// Releases all cheat-related state.
135
extern void UnloadAll();
136
137
/// Returns true if any patches have setting overrides specified.
138
extern bool HasAnySettingOverrides();
139
140
/// Applies setting changes based on patches.
141
extern void ApplySettingOverrides();
142
143
/// Applies all currently-registered frame end cheat codes.
144
extern void ApplyFrameEndCodes();
145
146
/// Returns true if cheats are enabled in the current game's configuration.
147
extern bool AreCheatsEnabled();
148
149
/// Enumerates the names of all manually-activated codes.
150
extern bool EnumerateManualCodes(std::function<bool(const std::string& name)> callback);
151
152
/// Invokes/applies the specified manually-activated code.
153
extern bool ApplyManualCode(const std::string_view name);
154
155
/// Returns the number of active patches.
156
extern u32 GetActivePatchCount();
157
158
/// Returns the number of active cheats.
159
extern u32 GetActiveCheatCount();
160
161
// Config sections/keys to use to enable patches.
162
extern const char* PATCHES_CONFIG_SECTION;
163
extern const char* CHEATS_CONFIG_SECTION;
164
extern const char* PATCH_ENABLE_CONFIG_KEY;
165
166
} // namespace Cheats
167
168