Skip to content

Commit 2c24284

Browse files
committed
add patch and unpatch function, make patch definition inline
1 parent 9265bf1 commit 2c24284

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

patch1337tocpp/Program.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace patch1337tocpp {
1111
class Program {
1212
static void Main(string[] args) {
1313
CommandLineOpts opts = null;
14-
14+
1515
Parser.Default.ParseArguments<CommandLineOpts>(args)
1616
.WithParsed<CommandLineOpts>(o => opts = o);
1717

@@ -21,7 +21,7 @@ static void Main(string[] args) {
2121
StringBuilder finalOutput = new StringBuilder();
2222

2323
Patch? currentPatch = null;
24-
24+
2525
foreach (var line in lines) {
2626
if (line.StartsWith('>')) {
2727
var p = new Patch() {ModuleName = line.Substring(1, line.Length - 1)};
@@ -32,7 +32,7 @@ static void Main(string[] args) {
3232
else {
3333
if (!currentPatch.HasValue)
3434
throw new InvalidDataException("Patch does not start with a valid module identifier!");
35-
35+
3636
currentPatch.Value.Patches.Add(new PatchAddress() {
3737
Address = int.Parse(line.Substring(0, 8), NumberStyles.HexNumber),
3838
OldByte = byte.Parse(line.Substring(9, 2), NumberStyles.HexNumber),
@@ -41,9 +41,11 @@ static void Main(string[] args) {
4141
}
4242
}
4343

44+
4445
using (StreamWriter w = new StreamWriter("output.cpp")) {
45-
46+
4647
w.WriteLine("#include <vector>");
48+
w.WriteLine("#include <windows.h>");
4749
w.Write("struct PatchAddress { ");
4850
w.Write("int Address; ");
4951
w.Write("unsigned char OldByte; ");
@@ -53,20 +55,34 @@ static void Main(string[] args) {
5355
w.Write("char* ModuleName; ");
5456
w.Write("std::vector<PatchAddress> Patches;");
5557
w.WriteLine("};");
58+
w.WriteLine("void PatchUChar(unsigned char* dst, unsigned char* src, int size) {");
59+
w.WriteLine("DWORD oldprotect;");
60+
w.WriteLine("VirtualProtect(dst, size, PAGE_EXECUTE_READWRITE, &oldprotect);");
61+
w.WriteLine("memcpy(dst, src, size);");
62+
w.WriteLine("VirtualProtect(dst, size, oldprotect, &oldprotect); };");
5663

5764
foreach (var patch in patches) {
5865
var addressableName = Regex.Replace(patch.ModuleName, "([^A-Za-z0-9])+", "_");
59-
w.WriteLine($"Patch {addressableName};");
60-
w.WriteLine($"{addressableName}.ModuleName = \"{patch.ModuleName}\";");
61-
w.WriteLine($"{addressableName}.Patches = {{");
6266

63-
foreach (var addr in patch.Patches) {
67+
w.WriteLine($"Patch {addressableName} = Patch {{ \"{patch.ModuleName}\", {{");
68+
69+
foreach (var addr in patch.Patches) {
6470
w.WriteLine($"PatchAddress{{ {addr.Address}, {addr.OldByte}, {addr.NewByte} }},");
6571
}
6672

73+
w.WriteLine("} };");
74+
75+
w.WriteLine($"void patch_{addressableName}() {{");
76+
w.WriteLine($"for (PatchAddress addr : {addressableName}.Patches) {{");
77+
w.WriteLine("PatchUChar((unsigned char*)addr.Address, (unsigned char*)addr.NewByte, 1); }");
78+
w.WriteLine("};");
79+
80+
w.WriteLine($"void unpatch_{addressableName}() {{");
81+
w.WriteLine($"for (PatchAddress addr : {addressableName}.Patches) {{");
82+
w.WriteLine("PatchUChar((unsigned char*)addr.Address,(unsigned char*)addr.OldByte, 1); }");
6783
w.WriteLine("};");
6884
}
69-
85+
7086
w.Flush();
7187
}
7288
}

0 commit comments

Comments
 (0)