Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Redbeanw44602 committed May 18, 2023
1 parent a28e5a9 commit 398fe53
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 176 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ A patcher, contains some patches for minecraft.
```
MCPatcher.exe [path]
```
- You can provide no arguments and it will ask you for the path to binary.
- You can provide no arguments, and it will ask you for the path to binary.
```
MCPatcher.exe
```

### Platform: Windows

> Binary: *Minecraft.Windows.exe*
> Tested: 1.19.22, 1.19.30, 1.19.40.22 (Preview)
> As of 23/05/18, it is effective in all recent versions (release-1.19, preview-1.20), and theoretically supports all versions.
- No Trial
- No Chat Report (wip)
119 changes: 40 additions & 79 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,43 @@
#include <shobjidl.h>
#include <fstream>
#include <sstream>
#include <spdlog/spdlog.h>

#include "patcher.h"
#include "utils.h"
#include "logger.h"

constexpr unsigned int FAIL_CANNOT_OPEN_FILE = 0x1001;
constexpr unsigned int FAIL_CANNOT_READ_FILE = 0x1002;
constexpr unsigned int FAIL_CANNOT_FIND_BYTE = 0x1003;
constexpr unsigned int FAIL_CURRENT_PLATFORM_NO_PATCH = 0x1004;
constexpr unsigned int FAIL_BACKUP = 0x1005;
constexpr unsigned int FAIL_CANNOT_OPEN_FILE = (0x1001);
constexpr unsigned int FAIL_CANNOT_READ_FILE = (0x1002);
constexpr unsigned int FAIL_CANNOT_FIND_BYTE = (0x1003);
constexpr unsigned int FAIL_CURRENT_PLATFORM_NO_PATCH = (0x1004);
constexpr unsigned int FAIL_BACKUP = (0x1005);

#define VERSION "1.1.0"
#define VERSION "1.2.0"

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

// Welcome message.

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

// Add known patches;
// Add known patch(es);

MCPatcher patcher;

// PatchName *from* [PV(preview)|FM(formal)|BT(beta)][VERSION][-][FUNCTION ADDRESS]

patcher.registerPatch(
Platform::Win10,
"PV1193025-1403A4F20",
{
{
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC },
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC }
},
{
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x7C, 0x24 },
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x7C, 0x24 }
}
}
);

patcher.registerPatch(
Platform::Win10,
"PV1198020-14124C910",
{
{
{ 0x0D, 0xB8, 0x00, 0xEB, 0x04, 0x0F, 0xB6, 0x42, 0x10, 0x84, 0xC0, 0x74, 0x26, 0xB0, /*O*/0x01, 0x48 },
{ 0x0D, 0xB8, 0x00, 0xEB, 0x04, 0x0F, 0xB6, 0x42, 0x10, 0x84, 0xC0, 0x74, 0x26, 0xB0, /*N*/0x00, 0x48 }
}
}
);
auto generalPatch = MCPatcher::compile(
"48 8B 42 08 48 8B 88 80 01 00 00 48 85 C9 74 07 E8 ?? ?? ?? 00 "
"EB 04 0F B6 42 10 84 C0 74 ?? B0 01(00) 48 8B 4C 24 ?? 48 33 CC");

patcher.registerPatch(Platform::Win10, "General_Patch_V2", generalPatch);

// Ask for binary file;

string strpath;
if (argc == 1)
{
if (argc == 1) {
HRESULT result = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(result))
{
Info("Please open an executable for minecraft. (Minecraft.Windows.exe)");
if (SUCCEEDED(result)) {
spdlog::info("Please open an executable for minecraft. (Minecraft.Windows.exe)");
IFileOpenDialog *openFile;
CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&openFile));
Expand All @@ -71,53 +49,40 @@ int main(int argc, char *argv[]) {
openFile->Show(nullptr);
IShellItem *pItem;
result = openFile->GetResult(&pItem);
if (SUCCEEDED(result))
{
if (SUCCEEDED(result)) {
PWSTR pPath;
pItem->GetDisplayName(SIGDN_FILESYSPATH, &pPath);
std::wstringstream path;
path << pPath;
strpath = wchar2string(path.str().c_str());
Info("Selected {}.",strpath);
spdlog::info("Selected {}.",strpath);
pItem->Release();
}
else
{
Error("Open file failed!");
} else {
spdlog::error("Open file failed!");
}
openFile->Release();
CoUninitialize();
}
if (strpath.empty())
return FAIL_CANNOT_OPEN_FILE;
}
else if (argc == 2)
{
} else if (argc == 2) {
strpath = argv[1];
}
else
{
Error("Wrong parameter!");
} else {
spdlog::error("Wrong parameter!");
}

// Open file;

if (!patcher.target(strpath))
{
Error("Can't read executable file!");
if (!patcher.target(strpath)) {
spdlog::error("Can't read executable file!");
return FAIL_CANNOT_READ_FILE;
}
else
{
} else {
std::ofstream ofs(strpath + ".bak", std::ios::binary);
ofs << patcher.getImage().rdbuf();
if (ofs.good())
{
Info("Backup created to: {}.bak",strpath);
}
else
{
Error("Fail to create backup!");
if (ofs.good()) {
spdlog::info("Backup created to: {}.bak",strpath);
} else {
spdlog::error("Fail to create backup!");
return FAIL_BACKUP;
}
ofs.close();
Expand All @@ -127,22 +92,18 @@ int main(int argc, char *argv[]) {

auto platform = Platform::Win10;
auto patches = patcher.getPatches(platform);
if (patches.empty())
{
Error("There are no patches available for the current platform.");
if (patches.empty()) {
spdlog::error("There are no patches available for the current platform.");
return FAIL_CURRENT_PLATFORM_NO_PATCH;
}

// Do patch;

Info("Looking for bytes...");
if (patcher.apply(platform))
{
Info("Patch successfully.");
}
else
{
Error("Failed, if it is the latest version, please send issue.");
spdlog::info("Looking for bytes...");
if (patcher.apply(platform)) {
spdlog::info("Patch successfully.");
} else {
spdlog::error("Failed, if it is the latest version, please send issue.");
return FAIL_CANNOT_FIND_BYTE;
}

Expand Down
130 changes: 83 additions & 47 deletions patcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,71 @@
//

#include "patcher.h"
#include <spdlog/spdlog.h>

void MCPatcher::registerPatch(Platform platform, const string& name, const vector<SinglePatch>& patch) {
for (auto& i : patch)
{
if (i.mBefore.size() != i.mAfter.size())
{
Error("The wrong patch is being registered!");
return;
}
void MCPatcher::registerPatch(Platform platform, const string& name, const PatchEntity& patch) {
if (patch.valid() && !name.empty()) {
mPatches[platform][name] = patch;
}
mPatches[platform][name] = patch;
}

bool MCPatcher::apply(Platform platform) {
vector<std::pair<long long, BinarySequence>> needModify;
auto isOk = true;
for (auto& it : mPatches[platform])
{
Info("Trying \"{}\" patch.", it.first);
Info("Need to find {} binary position...", it.second.size());
needModify.clear();
auto count = 0;
for (auto& bin : it.second)
{
count++;
auto pos = findBytes(mImage, bin.mBefore);
if (pos)
{
Info("Point {} founded, {}.", count, pos);
needModify.emplace_back(std::pair{pos, bin.mAfter});
isOk = true;
}
else
{
Warn("Point {} not found, try the next set.", count);
isOk = false;
break;
}
}
if (isOk)
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 (!isOk || needModify.empty())
return false;
for (auto& patch : needModify)
{
mImage.seekg(patch.first);
for (auto& bts : patch.second)
{
mImage.write((char *)&bts, sizeof(bts));
}
}
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();
Expand All @@ -67,12 +77,38 @@ fstream& MCPatcher::getImage() {
return mImage;
}

unordered_map<string, vector<MCPatcher::SinglePatch>>& MCPatcher::getPatches(Platform platform) {
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;
}
Loading

0 comments on commit 398fe53

Please sign in to comment.