Skip to content

Commit 398fe53

Browse files
committed
1.2.0
1 parent a28e5a9 commit 398fe53

File tree

6 files changed

+181
-176
lines changed

6 files changed

+181
-176
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ A patcher, contains some patches for minecraft.
1010
```
1111
MCPatcher.exe [path]
1212
```
13-
- You can provide no arguments and it will ask you for the path to binary.
13+
- You can provide no arguments, and it will ask you for the path to binary.
1414
```
1515
MCPatcher.exe
1616
```
1717

1818
### Platform: Windows
1919

2020
> Binary: *Minecraft.Windows.exe*
21-
> Tested: 1.19.22, 1.19.30, 1.19.40.22 (Preview)
22-
21+
> As of 23/05/18, it is effective in all recent versions (release-1.19, preview-1.20), and theoretically supports all versions.
2322
- No Trial
2423
- No Chat Report (wip)

main.cpp

+40-79
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,43 @@
11
#include <shobjidl.h>
22
#include <fstream>
3+
#include <sstream>
4+
#include <spdlog/spdlog.h>
35

46
#include "patcher.h"
57
#include "utils.h"
6-
#include "logger.h"
78

8-
constexpr unsigned int FAIL_CANNOT_OPEN_FILE = 0x1001;
9-
constexpr unsigned int FAIL_CANNOT_READ_FILE = 0x1002;
10-
constexpr unsigned int FAIL_CANNOT_FIND_BYTE = 0x1003;
11-
constexpr unsigned int FAIL_CURRENT_PLATFORM_NO_PATCH = 0x1004;
12-
constexpr unsigned int FAIL_BACKUP = 0x1005;
9+
constexpr unsigned int FAIL_CANNOT_OPEN_FILE = (0x1001);
10+
constexpr unsigned int FAIL_CANNOT_READ_FILE = (0x1002);
11+
constexpr unsigned int FAIL_CANNOT_FIND_BYTE = (0x1003);
12+
constexpr unsigned int FAIL_CURRENT_PLATFORM_NO_PATCH = (0x1004);
13+
constexpr unsigned int FAIL_BACKUP = (0x1005);
1314

14-
#define VERSION "1.1.0"
15+
#define VERSION "1.2.0"
1516

1617
int main(int argc, char *argv[]) {
1718

1819
// Welcome message.
1920

20-
Info("MCPatcher v{} OpenSource: github.com/Redbeanw44602/MCPatcher [MIT]",VERSION);
21+
spdlog::set_pattern("[%H:%M:%S.%e] [%^%l%$] %v");
22+
spdlog::info("MCPatcher v{}, Repository: github.com/Redbeanw44602/MCPatcher [MIT]", VERSION);
2123

22-
// Add known patches;
24+
// Add known patch(es);
2325

2426
MCPatcher patcher;
2527

26-
// PatchName *from* [PV(preview)|FM(formal)|BT(beta)][VERSION][-][FUNCTION ADDRESS]
27-
28-
patcher.registerPatch(
29-
Platform::Win10,
30-
"PV1193025-1403A4F20",
31-
{
32-
{
33-
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC },
34-
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC }
35-
},
36-
{
37-
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x7C, 0x24 },
38-
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x7C, 0x24 }
39-
}
40-
}
41-
);
42-
43-
patcher.registerPatch(
44-
Platform::Win10,
45-
"PV1198020-14124C910",
46-
{
47-
{
48-
{ 0x0D, 0xB8, 0x00, 0xEB, 0x04, 0x0F, 0xB6, 0x42, 0x10, 0x84, 0xC0, 0x74, 0x26, 0xB0, /*O*/0x01, 0x48 },
49-
{ 0x0D, 0xB8, 0x00, 0xEB, 0x04, 0x0F, 0xB6, 0x42, 0x10, 0x84, 0xC0, 0x74, 0x26, 0xB0, /*N*/0x00, 0x48 }
50-
}
51-
}
52-
);
28+
auto generalPatch = MCPatcher::compile(
29+
"48 8B 42 08 48 8B 88 80 01 00 00 48 85 C9 74 07 E8 ?? ?? ?? 00 "
30+
"EB 04 0F B6 42 10 84 C0 74 ?? B0 01(00) 48 8B 4C 24 ?? 48 33 CC");
31+
32+
patcher.registerPatch(Platform::Win10, "General_Patch_V2", generalPatch);
5333

5434
// Ask for binary file;
5535

5636
string strpath;
57-
if (argc == 1)
58-
{
37+
if (argc == 1) {
5938
HRESULT result = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
60-
if (SUCCEEDED(result))
61-
{
62-
Info("Please open an executable for minecraft. (Minecraft.Windows.exe)");
39+
if (SUCCEEDED(result)) {
40+
spdlog::info("Please open an executable for minecraft. (Minecraft.Windows.exe)");
6341
IFileOpenDialog *openFile;
6442
CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_ALL,
6543
IID_IFileOpenDialog, reinterpret_cast<void**>(&openFile));
@@ -71,53 +49,40 @@ int main(int argc, char *argv[]) {
7149
openFile->Show(nullptr);
7250
IShellItem *pItem;
7351
result = openFile->GetResult(&pItem);
74-
if (SUCCEEDED(result))
75-
{
52+
if (SUCCEEDED(result)) {
7653
PWSTR pPath;
7754
pItem->GetDisplayName(SIGDN_FILESYSPATH, &pPath);
7855
std::wstringstream path;
7956
path << pPath;
8057
strpath = wchar2string(path.str().c_str());
81-
Info("Selected {}.",strpath);
58+
spdlog::info("Selected {}.",strpath);
8259
pItem->Release();
83-
}
84-
else
85-
{
86-
Error("Open file failed!");
60+
} else {
61+
spdlog::error("Open file failed!");
8762
}
8863
openFile->Release();
8964
CoUninitialize();
9065
}
9166
if (strpath.empty())
9267
return FAIL_CANNOT_OPEN_FILE;
93-
}
94-
else if (argc == 2)
95-
{
68+
} else if (argc == 2) {
9669
strpath = argv[1];
97-
}
98-
else
99-
{
100-
Error("Wrong parameter!");
70+
} else {
71+
spdlog::error("Wrong parameter!");
10172
}
10273

10374
// Open file;
10475

105-
if (!patcher.target(strpath))
106-
{
107-
Error("Can't read executable file!");
76+
if (!patcher.target(strpath)) {
77+
spdlog::error("Can't read executable file!");
10878
return FAIL_CANNOT_READ_FILE;
109-
}
110-
else
111-
{
79+
} else {
11280
std::ofstream ofs(strpath + ".bak", std::ios::binary);
11381
ofs << patcher.getImage().rdbuf();
114-
if (ofs.good())
115-
{
116-
Info("Backup created to: {}.bak",strpath);
117-
}
118-
else
119-
{
120-
Error("Fail to create backup!");
82+
if (ofs.good()) {
83+
spdlog::info("Backup created to: {}.bak",strpath);
84+
} else {
85+
spdlog::error("Fail to create backup!");
12186
return FAIL_BACKUP;
12287
}
12388
ofs.close();
@@ -127,22 +92,18 @@ int main(int argc, char *argv[]) {
12792

12893
auto platform = Platform::Win10;
12994
auto patches = patcher.getPatches(platform);
130-
if (patches.empty())
131-
{
132-
Error("There are no patches available for the current platform.");
95+
if (patches.empty()) {
96+
spdlog::error("There are no patches available for the current platform.");
13397
return FAIL_CURRENT_PLATFORM_NO_PATCH;
13498
}
13599

136100
// Do patch;
137101

138-
Info("Looking for bytes...");
139-
if (patcher.apply(platform))
140-
{
141-
Info("Patch successfully.");
142-
}
143-
else
144-
{
145-
Error("Failed, if it is the latest version, please send issue.");
102+
spdlog::info("Looking for bytes...");
103+
if (patcher.apply(platform)) {
104+
spdlog::info("Patch successfully.");
105+
} else {
106+
spdlog::error("Failed, if it is the latest version, please send issue.");
146107
return FAIL_CANNOT_FIND_BYTE;
147108
}
148109

patcher.cpp

+83-47
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,71 @@
33
//
44

55
#include "patcher.h"
6+
#include <spdlog/spdlog.h>
67

7-
void MCPatcher::registerPatch(Platform platform, const string& name, const vector<SinglePatch>& patch) {
8-
for (auto& i : patch)
9-
{
10-
if (i.mBefore.size() != i.mAfter.size())
11-
{
12-
Error("The wrong patch is being registered!");
13-
return;
14-
}
8+
void MCPatcher::registerPatch(Platform platform, const string& name, const PatchEntity& patch) {
9+
if (patch.valid() && !name.empty()) {
10+
mPatches[platform][name] = patch;
1511
}
16-
mPatches[platform][name] = patch;
1712
}
1813

1914
bool MCPatcher::apply(Platform platform) {
20-
vector<std::pair<long long, BinarySequence>> needModify;
21-
auto isOk = true;
22-
for (auto& it : mPatches[platform])
23-
{
24-
Info("Trying \"{}\" patch.", it.first);
25-
Info("Need to find {} binary position...", it.second.size());
26-
needModify.clear();
27-
auto count = 0;
28-
for (auto& bin : it.second)
29-
{
30-
count++;
31-
auto pos = findBytes(mImage, bin.mBefore);
32-
if (pos)
33-
{
34-
Info("Point {} founded, {}.", count, pos);
35-
needModify.emplace_back(std::pair{pos, bin.mAfter});
36-
isOk = true;
37-
}
38-
else
39-
{
40-
Warn("Point {} not found, try the next set.", count);
41-
isOk = false;
42-
break;
43-
}
44-
}
45-
if (isOk)
15+
vector<std::pair<Address, BYTE>> needModify;
16+
for (auto& it : mPatches[platform]) {
17+
spdlog::info("Trying \"{}\" patch.", it.first);
18+
needModify = handleBytes(it.second.mBytes);
19+
if (needModify.empty()) {
20+
spdlog::warn("Byte sequence not found.");
21+
} else {
22+
spdlog::info("Successfully found the byte sequence.");
4623
break;
47-
}
48-
if (!isOk || needModify.empty())
49-
return false;
50-
for (auto& patch : needModify)
51-
{
52-
mImage.seekg(patch.first);
53-
for (auto& bts : patch.second)
54-
{
55-
mImage.write((char *)&bts, sizeof(bts));
5624
}
5725
}
26+
if (needModify.empty()) return false;
27+
for (auto& patch : needModify) {
28+
mImage.seekg(patch.first - 1);
29+
mImage.write((char *)&patch.second, sizeof(patch.second));
30+
}
5831
return mImage.good();
5932
}
6033

34+
vector<std::pair<MCPatcher::Address, BYTE>> MCPatcher::handleBytes(const vector<ByteEntity> &bytes) {
35+
if (bytes.empty()) return {};
36+
vector<std::pair<MCPatcher::Address, BYTE>> rtn;
37+
mImage.seekg(0);
38+
int matchedSize = 0;
39+
BYTE chr;
40+
while(mImage.read((char *)&chr, sizeof(chr))) {
41+
auto& byte = bytes.at(matchedSize);
42+
if (byte.mType == DataType::ALL) {
43+
matchedSize++;
44+
} else if (byte.mType == DataType::NORMAL) {
45+
if (byte.mData == chr) {
46+
matchedSize++;
47+
} else {
48+
matchedSize = 0;
49+
rtn.clear();
50+
continue;
51+
}
52+
}
53+
if (byte.mReplacer.mEnabled) {
54+
rtn.emplace_back(std::pair {mImage.tellg(), byte.mReplacer.mData});
55+
}
56+
if (matchedSize == bytes.size()) {
57+
//for (auto& i : rtn) {
58+
// mImage.seekg(i.first);
59+
// for (int k = 0; k < 20; k++) {
60+
// mImage.read((char *)&chr, sizeof(chr));
61+
// spdlog::info("byte -> {}", char2hex(chr));
62+
// }
63+
// spdlog::info("address -> {}", i.first);
64+
//}
65+
return rtn;
66+
}
67+
}
68+
return {};
69+
}
70+
6171
bool MCPatcher::target(const string& path) {
6272
mImage.open(path, std::ios::binary | std::ios::in | std::ios::out);
6373
return mImage.is_open();
@@ -67,12 +77,38 @@ fstream& MCPatcher::getImage() {
6777
return mImage;
6878
}
6979

70-
unordered_map<string, vector<MCPatcher::SinglePatch>>& MCPatcher::getPatches(Platform platform) {
80+
unordered_map<string, MCPatcher::PatchEntity>& MCPatcher::getPatches(Platform platform) {
7181
return mPatches[platform];
7282
}
7383

7484
MCPatcher::~MCPatcher() {
75-
7685
mImage.close();
77-
7886
}
87+
88+
// e.g. "74 ?? B0 01(00) 48"
89+
// Checkless! Boom if input not correctly!
90+
MCPatcher::PatchEntity MCPatcher::compile(string patchExp) {
91+
PatchEntity rtn;
92+
patchExp += " ";
93+
int nextPosition = 0;
94+
for (int i = 0; i < patchExp.size(); i++) {
95+
if (i != nextPosition) continue;
96+
auto pos = patchExp.find(" ", i, 1);
97+
if (pos == patchExp.npos) throw CompileFailed();
98+
auto substr = patchExp.substr(i, pos - i);
99+
ByteEntity entity;
100+
if (substr.substr(0, 2) == "??") {
101+
entity.mType = DataType::ALL;
102+
} else {
103+
entity.mType = DataType::NORMAL;
104+
entity.mData = hex2char(substr.substr(0, 2));
105+
}
106+
if (substr.find("(") != substr.npos) {
107+
entity.mReplacer.mEnabled = true;
108+
entity.mReplacer.mData = hex2char(substr.substr(3, 2));
109+
}
110+
nextPosition = pos + 1;
111+
rtn.add(entity);
112+
}
113+
return rtn;
114+
}

0 commit comments

Comments
 (0)