-
-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathLibCpp2IlContextBuilder.cs
More file actions
141 lines (109 loc) · 4.85 KB
/
Copy pathLibCpp2IlContextBuilder.cs
File metadata and controls
141 lines (109 loc) · 4.85 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
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,
MetadataFixupFunc = LibCpp2IlMain.Settings.MetadataFixupFunc
});
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) when (_context.Settings.MetadataFixupFunc is { } fixupFunc)
{
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);
}
}
LibLogger.InfoNewline($"Initialized Metadata in {(DateTime.Now - start).TotalMilliseconds:F0}ms");
LoadMetadata(metadata);
}
public void LoadMetadata(Il2CppMetadata metadata)
{
_context.Metadata = metadata;
metadata.SetOwningContext(_context);
_metadataLoaded = true;
}
public void LoadBinary(byte[] binaryBytes)
{
if (!_metadataLoaded)
throw new InvalidOperationException("Metadata must be loaded before the binary can be loaded.");
LibCpp2IlBinaryRegistry.CreateAndInit(binaryBytes, _context);
_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);
_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.");
_context.ReflectionCache.Init(_context);
if (!_context.Settings.DisableGlobalResolving && _context.Metadata.MetadataVersion < 27)
{
var start = DateTime.Now;
LibLogger.Info("Mapping Globals...");
_context.MapGlobalIdentifiers();
LibLogger.InfoNewline($"OK ({(DateTime.Now - start).TotalMilliseconds:F0}ms)");
}
if (!_context.Settings.DisableMethodPointerMapping)
{
var 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)");
}
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);
return Build(peBytes, metadataBytes, unityVersion);
}
}