This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
forked from bricktea/MCPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.cpp
114 lines (105 loc) · 3.43 KB
/
patcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Created by RedbeanW on 9/16/2022.
//
#include "patcher.h"
#include <spdlog/spdlog.h>
void MCPatcher::registerPatch(Platform platform, const string& name, const PatchEntity& patch) {
if (patch.valid() && !name.empty()) {
mPatches[platform][name] = patch;
}
}
bool MCPatcher::apply(Platform platform) {
vector<std::pair<Address, BYTE>> needModify;
for (auto& it : mPatches[platform]) {
spdlog::info("Trying \"{}\" patch.", it.first);
needModify = handleBytes(it.second.mBytes);
if (needModify.empty()) {
spdlog::warn("Byte sequence not found.");
} else {
spdlog::info("Successfully found the byte sequence.");
break;
}
}
if (needModify.empty()) return false;
for (auto& patch : needModify) {
mImage.seekg(patch.first - 1);
mImage.write((char *)&patch.second, sizeof(patch.second));
}
return mImage.good();
}
vector<std::pair<MCPatcher::Address, BYTE>> MCPatcher::handleBytes(const vector<ByteEntity> &bytes) {
if (bytes.empty()) return {};
vector<std::pair<MCPatcher::Address, BYTE>> rtn;
mImage.seekg(0);
int matchedSize = 0;
BYTE chr;
while(mImage.read((char *)&chr, sizeof(chr))) {
auto& byte = bytes.at(matchedSize);
if (byte.mType == DataType::ALL) {
matchedSize++;
} else if (byte.mType == DataType::NORMAL) {
if (byte.mData == chr) {
matchedSize++;
} else {
matchedSize = 0;
rtn.clear();
continue;
}
}
if (byte.mReplacer.mEnabled) {
rtn.emplace_back(std::pair {mImage.tellg(), byte.mReplacer.mData});
}
if (matchedSize == bytes.size()) {
//for (auto& i : rtn) {
// mImage.seekg(i.first);
// for (int k = 0; k < 20; k++) {
// mImage.read((char *)&chr, sizeof(chr));
// spdlog::info("byte -> {}", char2hex(chr));
// }
// spdlog::info("address -> {}", i.first);
//}
return rtn;
}
}
return {};
}
bool MCPatcher::target(const string& path) {
mImage.open(path, std::ios::binary | std::ios::in | std::ios::out);
return mImage.is_open();
}
fstream& MCPatcher::getImage() {
return mImage;
}
unordered_map<string, MCPatcher::PatchEntity>& MCPatcher::getPatches(Platform platform) {
return mPatches[platform];
}
MCPatcher::~MCPatcher() {
mImage.close();
}
// e.g. "74 ?? B0 01(00) 48"
// Checkless! Boom if input not correctly!
MCPatcher::PatchEntity MCPatcher::compile(string patchExp) {
PatchEntity rtn;
patchExp += " ";
int nextPosition = 0;
for (int i = 0; i < patchExp.size(); i++) {
if (i != nextPosition) continue;
auto pos = patchExp.find(" ", i, 1);
if (pos == patchExp.npos) throw CompileFailed();
auto substr = patchExp.substr(i, pos - i);
ByteEntity entity;
if (substr.substr(0, 2) == "??") {
entity.mType = DataType::ALL;
} else {
entity.mType = DataType::NORMAL;
entity.mData = hex2char(substr.substr(0, 2));
}
if (substr.find("(") != substr.npos) {
entity.mReplacer.mEnabled = true;
entity.mReplacer.mData = hex2char(substr.substr(3, 2));
}
nextPosition = pos + 1;
rtn.add(entity);
}
return rtn;
}