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+ }
0 commit comments