-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathLibCpp2IlContextBuilder.cs
More file actions
159 lines (120 loc) · 5.63 KB
/
LibCpp2IlContextBuilder.cs
File metadata and controls
159 lines (120 loc) · 5.63 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
using System;
using System.IO;
using System.Linq;
using AssetRipper.Primitives;
using LibCpp2IL.Logging;
using LibCpp2IL.Metadata;
namespace LibCpp2IL;
public sealed class LibCpp2IlContextBuilder
{
private readonly LibCpp2IlContext _context = new(
new LibCpp2IlMain.LibCpp2IlSettings // Snapshot settings at creation time.
{
AllowManualMetadataAndCodeRegInput = LibCpp2IlMain.Settings.AllowManualMetadataAndCodeRegInput,
DisableMethodPointerMapping = LibCpp2IlMain.Settings.DisableMethodPointerMapping,
DisableGlobalResolving = LibCpp2IlMain.Settings.DisableGlobalResolving,
});
private bool _metadataLoaded;
private bool _binaryLoaded;
public void LoadMetadata(byte[] metadataBytes, UnityVersion unityVersion)
{
var start = DateTime.Now;
LibLogger.InfoNewline("Initializing Metadata...");
Il2CppMetadata metadata;
try
{
metadata = Il2CppMetadata.ReadFrom(metadataBytes, unityVersion);
}
catch (Exception e)
{
if (LibCpp2IlMain.Settings.MetadataFixupFunc is not { } fixupFunc)
throw;
try
{
LibLogger.WarnNewline("Metadata read failed, but a fixup function is registered. Calling fixup function and then will attempt to read again...");
var fixedBytes = fixupFunc(metadataBytes, unityVersion);
if(fixedBytes == null)
throw new Exception("Metadata fixup function returned null, cannot proceed with metadata loading. Original exception follows.", e);
metadata = Il2CppMetadata.ReadFrom(fixedBytes, unityVersion);
LibLogger.InfoNewline("Metadata read succeeded after fixup.");
}
catch (Exception ex2)
{
throw new Exception("Failed to read metadata, even after attempted fixup.", ex2);
}
}
_context.Metadata = metadata;
_context.Il2CppTypeHasNumMods5Bits = metadata.MetadataVersion >= 27.2f;
LibLogger.InfoNewline($"Initialized Metadata in {(DateTime.Now - start).TotalMilliseconds:F0}ms");
// Legacy/static API compatibility: some in-binary structures still resolve via LibCpp2IlMain.Binary/TheMetadata
// during binary initialization, so we must set metadata defaults before initializing the binary.
LibCpp2IlMain.TheMetadata = metadata;
LibCpp2IlMain.DefaultContext = _context;
LibCpp2IlMain.Il2CppTypeHasNumMods5Bits = _context.Il2CppTypeHasNumMods5Bits;
_metadataLoaded = true;
}
public void LoadBinary(byte[] binaryBytes)
{
if (!_metadataLoaded)
throw new InvalidOperationException("Metadata must be loaded before the binary can be loaded.");
var bin = _context.Binary = LibCpp2IlBinaryRegistry.CreateAndInit(binaryBytes, _context.Metadata);
// Complete legacy/static initialization now that the binary exists.
LibCpp2IlMain.Binary = bin;
_binaryLoaded = true;
}
public void LoadBinary(Il2CppBinary binary)
{
if (!_metadataLoaded)
throw new InvalidOperationException("Metadata must be loaded before the binary can be loaded.");
binary.Init(_context.Metadata);
_context.Binary = binary;
// Complete legacy/static initialization now that the binary exists.
LibCpp2IlMain.Binary = binary;
_binaryLoaded = true;
}
public LibCpp2IlContext Build()
{
if (!_metadataLoaded)
throw new InvalidOperationException("Metadata must be loaded before context can be built.");
if (!_binaryLoaded)
throw new InvalidOperationException("Binary must be loaded before context can be built.");
DateTime start;
if (!_context.Settings.DisableGlobalResolving && _context.MetadataVersion < 27)
{
start = DateTime.Now;
LibLogger.Info("Mapping Globals...");
LibCpp2IlGlobalMapper.MapGlobalIdentifiers(_context.Metadata, _context.Binary);
LibLogger.InfoNewline($"OK ({(DateTime.Now - start).TotalMilliseconds:F0}ms)");
}
if (!_context.Settings.DisableMethodPointerMapping)
{
start = DateTime.Now;
LibLogger.Info("Mapping pointers to Il2CppMethodDefinitions...");
foreach (var (method, ptr) in _context.Metadata.methodDefs.Select(method => (method, ptr: method.MethodPointer)))
{
if (!_context.MethodsByPtr.TryGetValue(ptr, out var list))
_context.MethodsByPtr[ptr] = list = [];
list.Add(method);
}
LibLogger.InfoNewline($"Processed {_context.Metadata.methodDefs.Length} OK ({(DateTime.Now - start).TotalMilliseconds:F0}ms)");
}
_context.ReflectionCache.Init(_context);
return _context;
}
public static LibCpp2IlContext Build(byte[] binaryBytes, byte[] metadataBytes, UnityVersion unityVersion)
{
LibCpp2IlContextBuilder builder = new();
builder.LoadMetadata(metadataBytes, unityVersion);
builder.LoadBinary(binaryBytes);
return builder.Build();
}
public static LibCpp2IlContext BuildFromFiles(string pePath, string metadataPath, UnityVersion unityVersion)
{
var metadataBytes = File.ReadAllBytes(metadataPath);
var peBytes = File.ReadAllBytes(pePath);
LibCpp2IlContextBuilder builder = new();
builder.LoadMetadata(metadataBytes, unityVersion);
builder.LoadBinary(peBytes);
return builder.Build();
}
}