|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using AssetRipper.Primitives; |
| 5 | +using LibCpp2IL.Logging; |
| 6 | +using LibCpp2IL.Metadata; |
| 7 | + |
| 8 | +namespace LibCpp2IL; |
| 9 | + |
| 10 | +public sealed class LibCpp2IlContextBuilder |
| 11 | +{ |
| 12 | + private readonly LibCpp2IlContext _context = new( |
| 13 | + new LibCpp2IlMain.LibCpp2IlSettings // Snapshot settings at creation time. |
| 14 | + { |
| 15 | + AllowManualMetadataAndCodeRegInput = LibCpp2IlMain.Settings.AllowManualMetadataAndCodeRegInput, |
| 16 | + DisableMethodPointerMapping = LibCpp2IlMain.Settings.DisableMethodPointerMapping, |
| 17 | + DisableGlobalResolving = LibCpp2IlMain.Settings.DisableGlobalResolving, |
| 18 | + }); |
| 19 | + |
| 20 | + private bool _metadataLoaded; |
| 21 | + private bool _binaryLoaded; |
| 22 | + |
| 23 | + public void LoadMetadata(byte[] metadataBytes, UnityVersion unityVersion) |
| 24 | + { |
| 25 | + var start = DateTime.Now; |
| 26 | + LibLogger.InfoNewline("Initializing Metadata..."); |
| 27 | + |
| 28 | + var metadata = _context.Metadata = Il2CppMetadata.ReadFrom(metadataBytes, unityVersion); |
| 29 | + |
| 30 | + _context.Il2CppTypeHasNumMods5Bits = metadata.MetadataVersion >= 27.2f; |
| 31 | + |
| 32 | + LibLogger.InfoNewline($"Initialized Metadata in {(DateTime.Now - start).TotalMilliseconds:F0}ms"); |
| 33 | + |
| 34 | + // Legacy/static API compatibility: some in-binary structures still resolve via LibCpp2IlMain.Binary/TheMetadata |
| 35 | + // during binary initialization, so we must set metadata defaults before initializing the binary. |
| 36 | + LibCpp2IlMain.TheMetadata = metadata; |
| 37 | + LibCpp2IlMain.DefaultContext = _context; |
| 38 | + LibCpp2IlMain.Il2CppTypeHasNumMods5Bits = _context.Il2CppTypeHasNumMods5Bits; |
| 39 | + |
| 40 | + _metadataLoaded = true; |
| 41 | + } |
| 42 | + |
| 43 | + public void LoadBinary(byte[] binaryBytes) |
| 44 | + { |
| 45 | + if (!_metadataLoaded) |
| 46 | + throw new InvalidOperationException("Metadata must be loaded before the binary can be loaded."); |
| 47 | + |
| 48 | + var bin = _context.Binary = LibCpp2IlBinaryRegistry.CreateAndInit(binaryBytes, _context.Metadata); |
| 49 | + |
| 50 | + // Complete legacy/static initialization now that the binary exists. |
| 51 | + LibCpp2IlMain.Binary = bin; |
| 52 | + |
| 53 | + _binaryLoaded = true; |
| 54 | + } |
| 55 | + |
| 56 | + public void LoadBinary(Il2CppBinary binary) |
| 57 | + { |
| 58 | + if (!_metadataLoaded) |
| 59 | + throw new InvalidOperationException("Metadata must be loaded before the binary can be loaded."); |
| 60 | + |
| 61 | + binary.Init(_context.Metadata); |
| 62 | + |
| 63 | + _context.Binary = binary; |
| 64 | + |
| 65 | + // Complete legacy/static initialization now that the binary exists. |
| 66 | + LibCpp2IlMain.Binary = binary; |
| 67 | + |
| 68 | + _binaryLoaded = true; |
| 69 | + } |
| 70 | + |
| 71 | + public LibCpp2IlContext Build() |
| 72 | + { |
| 73 | + if (!_metadataLoaded) |
| 74 | + throw new InvalidOperationException("Metadata must be loaded before context can be built."); |
| 75 | + |
| 76 | + if (!_binaryLoaded) |
| 77 | + throw new InvalidOperationException("Binary must be loaded before context can be built."); |
| 78 | + |
| 79 | + DateTime start; |
| 80 | + if (!_context.Settings.DisableGlobalResolving && _context.MetadataVersion < 27) |
| 81 | + { |
| 82 | + start = DateTime.Now; |
| 83 | + LibLogger.Info("Mapping Globals..."); |
| 84 | + LibCpp2IlGlobalMapper.MapGlobalIdentifiers(_context.Metadata, _context.Binary); |
| 85 | + LibLogger.InfoNewline($"OK ({(DateTime.Now - start).TotalMilliseconds:F0}ms)"); |
| 86 | + } |
| 87 | + |
| 88 | + if (!_context.Settings.DisableMethodPointerMapping) |
| 89 | + { |
| 90 | + start = DateTime.Now; |
| 91 | + LibLogger.Info("Mapping pointers to Il2CppMethodDefinitions..."); |
| 92 | + foreach (var (method, ptr) in _context.Metadata.methodDefs.Select(method => (method, ptr: method.MethodPointer))) |
| 93 | + { |
| 94 | + if (!_context.MethodsByPtr.TryGetValue(ptr, out var list)) |
| 95 | + _context.MethodsByPtr[ptr] = list = []; |
| 96 | + |
| 97 | + list.Add(method); |
| 98 | + } |
| 99 | + |
| 100 | + LibLogger.InfoNewline($"Processed {_context.Metadata.methodDefs.Length} OK ({(DateTime.Now - start).TotalMilliseconds:F0}ms)"); |
| 101 | + } |
| 102 | + |
| 103 | + _context.ReflectionCache.Init(_context); |
| 104 | + |
| 105 | + return _context; |
| 106 | + } |
| 107 | + |
| 108 | + public static LibCpp2IlContext Build(byte[] binaryBytes, byte[] metadataBytes, UnityVersion unityVersion) |
| 109 | + { |
| 110 | + LibCpp2IlContextBuilder builder = new(); |
| 111 | + |
| 112 | + builder.LoadMetadata(metadataBytes, unityVersion); |
| 113 | + builder.LoadBinary(binaryBytes); |
| 114 | + |
| 115 | + return builder.Build(); |
| 116 | + } |
| 117 | + |
| 118 | + public static LibCpp2IlContext BuildFromFiles(string pePath, string metadataPath, UnityVersion unityVersion) |
| 119 | + { |
| 120 | + var metadataBytes = File.ReadAllBytes(metadataPath); |
| 121 | + var peBytes = File.ReadAllBytes(pePath); |
| 122 | + |
| 123 | + LibCpp2IlContextBuilder builder = new(); |
| 124 | + |
| 125 | + builder.LoadMetadata(metadataBytes, unityVersion); |
| 126 | + builder.LoadBinary(peBytes); |
| 127 | + |
| 128 | + return builder.Build(); |
| 129 | + } |
| 130 | +} |
0 commit comments