Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/CwCheat.h
5721 views
1
// Rough and ready CwCheats implementation, disabled by default.
2
3
#pragma once
4
5
#include <string>
6
#include <string_view>
7
#include <vector>
8
#include <iostream>
9
#include <sstream>
10
11
#include "Common/File/Path.h"
12
#include "Core/MemMap.h"
13
14
class PointerWrap;
15
16
void __CheatInit();
17
void __CheatShutdown();
18
void __CheatDoState(PointerWrap &p);
19
20
// Return whether cheats are enabled and in effect.
21
bool CheatsInEffect();
22
23
struct CheatLine {
24
uint32_t part1;
25
uint32_t part2;
26
};
27
28
struct CheatCode {
29
std::string name;
30
std::vector<CheatLine> lines;
31
};
32
33
struct CheatFileInfo {
34
int lineNum;
35
std::string name;
36
bool enabled;
37
};
38
39
struct CheatOperation;
40
41
class CWCheatEngine {
42
public:
43
CWCheatEngine(std::string_view gameID);
44
std::vector<CheatFileInfo> FileInfo() const;
45
void ParseCheats();
46
void CreateCheatFile();
47
const Path &CheatFilename() const {
48
return filename_;
49
}
50
void Run();
51
bool HasCheats();
52
static u32 GetAddress(u32 value) {
53
// TODO: This comment is weird:
54
// Returns static address used by ppsspp. Some games may not like this, and causes cheats to not work without offset
55
u32 address = (value + 0x08800000) & 0x3FFFFFFF;
56
return address;
57
}
58
59
private:
60
void InvalidateICache(u32 addr, int size) const;
61
62
CheatOperation InterpretNextCwCheat(const CheatCode &cheat, size_t &i);
63
64
void ExecuteOp(const CheatOperation &op, const CheatCode &cheat, size_t &i);
65
inline void ApplyMemoryOperator(const CheatOperation &op, uint32_t(*oper)(uint32_t, uint32_t));
66
inline bool TestIf(const CheatOperation &op, bool(*oper)(int a, int b)) const;
67
inline bool TestIfAddr(const CheatOperation &op, bool(*oper)(int a, int b)) const;
68
69
std::vector<CheatCode> cheats_;
70
std::string gameID_;
71
Path filename_;
72
};
73
74
class CheatFileParser {
75
public:
76
CheatFileParser(const Path &filename, std::string_view gameID = "");
77
~CheatFileParser();
78
79
bool Parse();
80
81
const std::vector<std::string> &GetErrors() const { return errors_; }
82
const std::vector<CheatCode> &GetCheats() const { return cheats_; }
83
const std::vector<CheatFileInfo> &GetFileInfo() const { return cheatInfo_; }
84
85
protected:
86
void Flush();
87
void FlushCheatInfo();
88
void AddError(const std::string &msg, int lineNumber);
89
void ParseLine(const std::string &line, int lineNumber);
90
void ParseDataLine(const std::string &line, int lineNumber);
91
bool ValidateGameID(std::string_view gameID);
92
93
FILE *fp_ = nullptr;
94
std::string validGameID_;
95
96
int games_ = 0;
97
std::vector<std::string> errors_;
98
std::vector<CheatFileInfo> cheatInfo_;
99
std::vector<CheatCode> cheats_;
100
std::vector<CheatLine> pendingLines_;
101
CheatFileInfo lastCheatInfo_;
102
bool gameEnabled_ = true;
103
bool gameRiskyEnabled_ = false;
104
bool cheatEnabled_ = false;
105
};
106
107