Skip to content

Commit de3ab4d

Browse files
committed
fix cpp2il related issues
1 parent 0cf9028 commit de3ab4d

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

TypeTreeGeneratorAPI/TypeTreeGenerator/AssetsTools/AssetsToolsGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ public override void LoadDll(Stream dllStream)
9090
monoCecilGenerator.loadedAssemblies.Add(assembly.MainModule.Name, assembly);
9191
}
9292

93+
public override void LoadIl2Cpp(byte[] assemblyData, byte[] metadataData)
94+
{
95+
base.LoadIl2Cpp(assemblyData, metadataData);
96+
cpp2IlGenerator.SetInitialized(true);
97+
}
98+
9399
public override List<(string, string)> GetMonoBehaviourDefinitions()
94100
{
95101
if (monoLoaded)

TypeTreeGeneratorAPI/TypeTreeGenerator/AssetsTools/MonoBehaviourTemplateGeneratorPatch.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ public class Cpp2IlTempGeneratorPatch : Cpp2IlTempGenerator, IMonoBehaviourTempl
1717
{
1818
public Cpp2IlTempGeneratorPatch() : base("", "")
1919
{
20-
// As we initialized LibCpp2Il, we can set the _initialized field to true
21-
// otherwise Cpp2IlTempGenerator will try to initialize again from the dummy paths
22-
typeof(Cpp2IlTempGenerator)
23-
.GetField("_initialized", BindingFlags.Instance | BindingFlags.NonPublic)
24-
?.SetValue(this, true);
20+
}
21+
public void SetInitialized(bool initialized)
22+
{
23+
// This is a workaround to set the _initialized field to true
24+
// as we already initialized LibCpp2Il in the constructor
25+
var field = typeof(Cpp2IlTempGenerator).GetField("_initialized", BindingFlags.Instance | BindingFlags.NonPublic);
26+
if (field != null)
27+
{
28+
field.SetValue(this, initialized);
29+
}
30+
else
31+
{
32+
throw new InvalidOperationException("Could not find the _initialized field in Cpp2IlTempGenerator.");
33+
}
2534
}
2635
}
2736

TypeTreeGeneratorAPI/TypeTreeGenerator/Il2CppHandler.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,13 @@ public class Il2CppHandler
1414

1515
static Il2CppHandler()
1616
{
17-
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_32);
18-
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_64);
19-
InstructionSetRegistry.RegisterInstructionSet<WasmInstructionSet>(DefaultInstructionSets.WASM);
20-
InstructionSetRegistry.RegisterInstructionSet<ArmV7InstructionSet>(DefaultInstructionSets.ARM_V7);
21-
bool useNewArm64 = false;
22-
if (useNewArm64)
23-
{
24-
InstructionSetRegistry.RegisterInstructionSet<NewArmV8InstructionSet>(DefaultInstructionSets.ARM_V8);
25-
}
26-
else
27-
{
28-
InstructionSetRegistry.RegisterInstructionSet<Arm64InstructionSet>(DefaultInstructionSets.ARM_V8);
29-
}
30-
31-
LibCpp2IlBinaryRegistry.RegisterBuiltInBinarySupport();
17+
Cpp2IlApi.Init();
18+
Cpp2IlApi.ConfigureLib(false);
3219
}
3320

3421
public static void Initialize(byte[] assemblyData, byte[] metadataData, string unityVersionString)
3522
{
3623
unityVersion = UnityVersion.Parse(unityVersionString);
37-
Cpp2IlApi.Init();
38-
Cpp2IlApi.ConfigureLib(false);
3924
Cpp2IlApi.InitializeLibCpp2Il(assemblyData, metadataData, unityVersion, false);
4025
}
4126

@@ -60,6 +45,7 @@ public static void Initialize(byte[] assemblyData, byte[] metadataData, string u
6045
var assemblyResolver = new AssemblyResolver();
6146
foreach (var assembly in outputFormat.BuildAssemblies(Cpp2IlApi.CurrentAppContext!))
6247
{
48+
assemblyResolver.AddToCache(assembly.ManifestModule.Name, assembly);
6349
foreach (var module in assembly.Modules)
6450
{
6551
module.MetadataResolver = new AsmResolver.DotNet.DefaultMetadataResolver(assemblyResolver);
@@ -72,6 +58,11 @@ public class AssemblyResolver : AsmResolver.DotNet.IAssemblyResolver
7258
{
7359
public Dictionary<string, AsmResolver.DotNet.AssemblyDefinition> assemblyDefinitions = new();
7460

61+
public void AddToCache(string name, AsmResolver.DotNet.AssemblyDefinition definition)
62+
{
63+
assemblyDefinitions[name] = definition;
64+
}
65+
7566
public void AddToCache(AsmResolver.DotNet.AssemblyDescriptor descriptor, AsmResolver.DotNet.AssemblyDefinition definition)
7667
{
7768
assemblyDefinitions[descriptor.Name] = definition;

0 commit comments

Comments
 (0)