From 0c120494b4298d5e03c3db3cc5afbecf23b02743 Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Tue, 18 Oct 2022 15:26:14 +0200 Subject: [PATCH] Improve Mono.Cecil type search if typed reference doesn't exist --- .../Tools/Flax.Build/Utilities/MonoCecil.cs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Source/Tools/Flax.Build/Utilities/MonoCecil.cs b/Source/Tools/Flax.Build/Utilities/MonoCecil.cs index 305b4e34f..4ff78178a 100644 --- a/Source/Tools/Flax.Build/Utilities/MonoCecil.cs +++ b/Source/Tools/Flax.Build/Utilities/MonoCecil.cs @@ -97,10 +97,44 @@ namespace Flax.Build return instance; } - public static void GetTypeReference(this ModuleDefinition module, string fullName, out TypeReference type) + public static void GetType(this ModuleDefinition module, string fullName, out TypeReference type) { if (!module.TryGetTypeReference(fullName, out type)) + { + // Do manual search + foreach (var a in module.AssemblyReferences) + { + var assembly = module.AssemblyResolver.Resolve(a); + if (assembly == null || assembly.MainModule == null) + continue; + foreach (var t in assembly.MainModule.Types) + { + if (t.FindTypeWithin(fullName, out type)) + { + module.ImportReference(type); + return; + } + } + } + throw new Exception($"Failed to find type {fullName} from module {module.FileName}."); + } + } + + private static bool FindTypeWithin(this TypeDefinition t, string fullName, out TypeReference type) + { + if (t.FullName == fullName) + { + type = t; + return true; + } + foreach (var n in t.NestedTypes) + { + if (n.FindTypeWithin(fullName, out type)) + return true; + } + type = null; + return false; } } }