forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpCodeGenerator.cs
More file actions
227 lines (197 loc) · 6.11 KB
/
Copy pathCSharpCodeGenerator.cs
File metadata and controls
227 lines (197 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using ReClassNET.Extensions;
using ReClassNET.Logger;
using ReClassNET.Nodes;
using ReClassNET.Project;
namespace ReClassNET.CodeGenerator
{
public class CSharpCodeGenerator : ICodeGenerator
{
private static readonly Dictionary<Type, string> nodeTypeToTypeDefinationMap = new Dictionary<Type, string>
{
[typeof(DoubleNode)] = "double",
[typeof(FloatNode)] = "float",
[typeof(BoolNode)] = "bool",
[typeof(Int8Node)] = "sbyte",
[typeof(Int16Node)] = "short",
[typeof(Int32Node)] = "int",
[typeof(Int64Node)] = "long",
[typeof(UInt8Node)] = "byte",
[typeof(UInt16Node)] = "ushort",
[typeof(UInt32Node)] = "uint",
[typeof(UInt64Node)] = "ulong",
[typeof(FunctionPtrNode)] = "IntPtr",
[typeof(Utf8TextPtrNode)] = "IntPtr",
[typeof(Utf16TextPtrNode)] = "IntPtr",
[typeof(Utf32TextPtrNode)] = "IntPtr",
[typeof(PointerNode)] = "IntPtr",
[typeof(VirtualMethodTableNode)] = "IntPtr",
[typeof(Vector2Node)] = "Vector2",
[typeof(Vector3Node)] = "Vector3",
[typeof(Vector4Node)] = "Vector4"
};
public Language Language => Language.CSharp;
public string GenerateCode(IReadOnlyList<ClassNode> classes, IReadOnlyList<EnumDescription> enums, ILogger logger)
{
using (var sw = new StringWriter())
{
using (var iw = new IndentedTextWriter(sw, "\t"))
{
iw.WriteLine($"// Created with {Constants.ApplicationName} {Constants.ApplicationVersion} by {Constants.Author}");
iw.WriteLine();
iw.WriteLine("// Warning: The C# code generator doesn't support all node types!");
iw.WriteLine();
iw.WriteLine("using System.Runtime.InteropServices;");
iw.WriteLine("// optional namespace, only for vectors");
iw.WriteLine("using System.Numerics;");
iw.WriteLine();
using (var en = enums.GetEnumerator())
{
if (en.MoveNext())
{
WriteEnum(iw, en.Current);
while (en.MoveNext())
{
iw.WriteLine();
WriteEnum(iw, en.Current);
}
iw.WriteLine();
}
}
var classesToWrite = classes
.Where(c => c.Nodes.None(n => n is FunctionNode)) // Skip class which contains FunctionNodes because these are not data classes.
.Distinct();
using (var en = classesToWrite.GetEnumerator())
{
if (en.MoveNext())
{
WriteClass(iw, en.Current, logger);
while (en.MoveNext())
{
iw.WriteLine();
WriteClass(iw, en.Current, logger);
}
}
}
}
return sw.ToString();
}
}
/// <summary>
/// Outputs the C# code for the given enum to the <see cref="TextWriter"/> instance.
/// </summary>
/// <param name="writer">The writer to output to.</param>
/// <param name="enum">The enum to output.</param>
private static void WriteEnum(IndentedTextWriter writer, EnumDescription @enum)
{
Contract.Requires(writer != null);
Contract.Requires(@enum != null);
writer.Write($"enum {@enum.Name} : ");
switch (@enum.Size)
{
case EnumDescription.UnderlyingTypeSize.OneByte:
writer.WriteLine(nodeTypeToTypeDefinationMap[typeof(Int8Node)]);
break;
case EnumDescription.UnderlyingTypeSize.TwoBytes:
writer.WriteLine(nodeTypeToTypeDefinationMap[typeof(Int16Node)]);
break;
case EnumDescription.UnderlyingTypeSize.FourBytes:
writer.WriteLine(nodeTypeToTypeDefinationMap[typeof(Int32Node)]);
break;
case EnumDescription.UnderlyingTypeSize.EightBytes:
writer.WriteLine(nodeTypeToTypeDefinationMap[typeof(Int64Node)]);
break;
}
writer.WriteLine("{");
writer.Indent++;
for (var j = 0; j < @enum.Values.Count; ++j)
{
var kv = @enum.Values[j];
writer.Write(kv.Key);
writer.Write(" = ");
writer.Write(kv.Value);
if (j < @enum.Values.Count - 1)
{
writer.Write(",");
}
writer.WriteLine();
}
writer.Indent--;
writer.WriteLine("};");
}
/// <summary>
/// Outputs the C# code for the given class to the <see cref="TextWriter"/> instance.
/// </summary>
/// <param name="writer">The writer to output to.</param>
/// <param name="class">The class to output.</param>
/// <param name="logger">The logger.</param>
private static void WriteClass(IndentedTextWriter writer, ClassNode @class, ILogger logger)
{
Contract.Requires(writer != null);
Contract.Requires(@class != null);
Contract.Requires(logger != null);
writer.WriteLine("[StructLayout(LayoutKind.Explicit)]");
writer.Write("public struct ");
writer.Write(@class.Name);
if (!string.IsNullOrEmpty(@class.Comment))
{
writer.Write(" // ");
writer.Write(@class.Comment);
}
writer.WriteLine();
writer.WriteLine("{");
writer.Indent++;
var nodes = @class.Nodes
.WhereNot(n => n is FunctionNode || n is BaseHexNode);
foreach (var node in nodes)
{
var type = GetTypeDefinition(node);
if (type != null)
{
writer.Write($"[FieldOffset(0x{node.Offset:X})] public readonly {type} {node.Name};");
if (!string.IsNullOrEmpty(node.Comment))
{
writer.Write(" ");
writer.Write(node.Comment);
}
writer.WriteLine();
}
else
{
logger.Log(LogLevel.Warning, $"Skipping node with unhandled type: {node.GetType()}");
}
}
writer.Indent--;
writer.WriteLine("}");
}
/// <summary>
/// Gets the type definition for the given node. If the node is not a simple node <c>null</c> is returned.
/// </summary>
/// <param name="node">The target node.</param>
/// <returns>The type definition for the node or null if no simple type is available.</returns>
private static string GetTypeDefinition(BaseNode node)
{
Contract.Requires(node != null);
if (node is BitFieldNode bitFieldNode)
{
var underlayingNode = bitFieldNode.GetUnderlayingNode();
underlayingNode.CopyFromNode(node);
node = underlayingNode;
}
if (nodeTypeToTypeDefinationMap.TryGetValue(node.GetType(), out var type))
{
return type;
}
if (node is EnumNode enumNode)
{
return enumNode.Enum.Name;
}
return null;
}
}
}