Skip to content

Commit b3cfd89

Browse files
committed
initial commit
0 parents  commit b3cfd89

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
patch1337tocpp/bin
2+
patch1337tocpp/obj
3+
/.idea

patch1337tocpp.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "patch1337tocpp", "patch1337tocpp\patch1337tocpp.csproj", "{3EB7100B-1197-4E17-B057-7F9135AE1392}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{3EB7100B-1197-4E17-B057-7F9135AE1392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{3EB7100B-1197-4E17-B057-7F9135AE1392}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{3EB7100B-1197-4E17-B057-7F9135AE1392}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{3EB7100B-1197-4E17-B057-7F9135AE1392}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

patch1337tocpp/Program.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
5+
using System.Text;
6+
using CommandLine;
7+
8+
namespace patch1337tocpp {
9+
class Program {
10+
static void Main(string[] args) {
11+
CommandLineOpts opts = null;
12+
13+
Parser.Default.ParseArguments<CommandLineOpts>(args)
14+
.WithParsed<CommandLineOpts>(o => opts = o);
15+
16+
var lines = File.ReadAllLines(opts.patchFile);
17+
18+
List<Patch> patches = new();
19+
StringBuilder finalOutput = new StringBuilder();
20+
21+
Patch? currentPatch = null;
22+
23+
foreach (var line in lines) {
24+
if (line.StartsWith('>')) {
25+
var p = new Patch() {ModuleName = line.Substring(1, line.Length - 1)};
26+
p.Patches = new();
27+
currentPatch = p;
28+
patches.Add(p);
29+
}
30+
else {
31+
if (!currentPatch.HasValue)
32+
throw new InvalidDataException("Patch does not start with a valid module identifier!");
33+
34+
currentPatch.Value.Patches.Add(new PatchAddress() {
35+
Address = int.Parse(line.Substring(0, 8), NumberStyles.HexNumber),
36+
OldByte = byte.Parse(line.Substring(9, 2), NumberStyles.HexNumber),
37+
NewByte = byte.Parse(line.Substring(13, 2), NumberStyles.HexNumber)
38+
});
39+
}
40+
}
41+
42+
using (StreamWriter w = new StreamWriter("output.cpp")) {
43+
44+
w.WriteLine("#include <vector>");
45+
w.Write("struct PatchAddress { ");
46+
w.Write("int Address; ");
47+
w.Write("unsigned char OldByte; ");
48+
w.Write("unsigned char NewByte; ");
49+
w.WriteLine("};");
50+
w.Write("struct Patch { ");
51+
w.Write("char* ModuleName; ");
52+
w.Write("std::vector<PatchAddress> Patches ");
53+
w.WriteLine("};");
54+
55+
foreach (var patch in patches) {
56+
var addressableName = patch.ModuleName.Replace(" ", "_");
57+
w.WriteLine($"Patch {addressableName};");
58+
w.WriteLine($"{addressableName}.ModuleName = \"{patch.ModuleName}\";");
59+
w.WriteLine($"{addressableName}.Patches = {{");
60+
61+
foreach (var addr in patch.Patches) {
62+
w.WriteLine($"PatchAddress{{ {addr.Address}, {addr.OldByte}, {addr.NewByte} }},");
63+
}
64+
65+
w.WriteLine("};");
66+
}
67+
68+
w.Flush();
69+
}
70+
}
71+
72+
struct Patch {
73+
public string ModuleName;
74+
public List<PatchAddress> Patches;
75+
}
76+
77+
struct PatchAddress {
78+
public int Address;
79+
public byte OldByte;
80+
public byte NewByte;
81+
}
82+
}
83+
84+
public class CommandLineOpts
85+
{
86+
[Option('f', "patch_file", Required = true, HelpText = "The patch file.")]
87+
public string patchFile { get; set; }
88+
}
89+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="CommandLineParser" Version="2.9.0-preview1" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)