diff --git a/VContainer/Assets/VContainer/Editor/CodeGen/InjectionILGenerator.cs b/VContainer/Assets/VContainer/Editor/CodeGen/InjectionILGenerator.cs index cb7d68f4..3c282fea 100644 --- a/VContainer/Assets/VContainer/Editor/CodeGen/InjectionILGenerator.cs +++ b/VContainer/Assets/VContainer/Editor/CodeGen/InjectionILGenerator.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Cecil.Rocks; using Unity.CompilationPipeline.Common.Diagnostics; +using Unity.CompilationPipeline.Common.ILPostProcessing; using MethodAttributes = Mono.Cecil.MethodAttributes; using TypeAttributes = Mono.Cecil.TypeAttributes; using VContainer.Internal; @@ -16,9 +18,11 @@ namespace VContainer.Editor.CodeGen sealed class InjectionILGenerator { readonly ModuleDefinition module; - readonly Assembly assembly; + readonly ICompiledAssembly compiledAssembly; readonly IList targetNamespaces; + Assembly currentAssembly; + TypeReference ObjectResolverTypeRef => objectResolverTypeRef ?? (objectResolverTypeRef = module.ImportReference(typeof(IObjectResolver))); @@ -58,11 +62,11 @@ sealed class InjectionILGenerator public InjectionILGenerator( ModuleDefinition module, - Assembly assembly, - IList targetNamespaces) + ICompiledAssembly compiledAssembly, + IList targetNamespaces = null) { this.module = module; - this.assembly = assembly; + this.compiledAssembly = compiledAssembly; this.targetNamespaces = targetNamespaces; } @@ -76,9 +80,11 @@ public bool TryGenerate(out List diagnosticMessages) foreach (var typeDef in module.Types) { + if (typeDef.FullName == "") continue; + try { - if (TryGenerate(typeDef, diagnosticMessages)) + if (TryGenerateType(typeDef, diagnosticMessages)) { count += 1; } @@ -114,9 +120,35 @@ public bool TryGenerate(out List diagnosticMessages) return false; } - bool TryGenerate(TypeDefinition typeDef, List diagnosticMessages) + bool NeedsInjectType(Type type) + => !type.IsEnum && + !type.IsValueType && + !type.IsInterface && + !(type.IsAbstract && type.IsSealed) && + !typeof(Delegate).IsAssignableFrom(type) && + !typeof(Attribute).IsAssignableFrom(type) && + !type.IsGenericType && + (targetNamespaces == null || + targetNamespaces.Count <= 0 || + targetNamespaces.Contains(type.Namespace)); + + Type GetTypeFromDef(TypeDefinition typeDef) + { + try + { + return Type.GetType($"{typeDef.FullName}, {module.Assembly.FullName}"); + } + catch (FileLoadException) + { + if (currentAssembly == null) + currentAssembly = Assembly.Load(compiledAssembly.InMemoryAssembly.PeData); + return currentAssembly.GetType(typeDef.FullName); + } + } + + bool TryGenerateType(TypeDefinition typeDef, List diagnosticMessages) { - var type = assembly.GetType(typeDef.FullName); + var type = GetTypeFromDef(typeDef); if (type == null) { diagnosticMessages.Add(new DiagnosticMessage @@ -140,7 +172,7 @@ bool TryGenerate(TypeDefinition typeDef, List diagnosticMessa diagnosticMessages.Add(new DiagnosticMessage { DiagnosticType = DiagnosticType.Warning, - MessageData = $"Failed to analyze {type.FullName} : {ex.Message}" + MessageData = $"Failed to analyze {type.FullName} : {ex.GetType()} {ex.Message}" }); return false; } @@ -149,18 +181,6 @@ bool TryGenerate(TypeDefinition typeDef, List diagnosticMessa return true; } - bool NeedsInjectType(Type type) - => !type.IsEnum && - !type.IsValueType && - !type.IsInterface && - !(type.IsAbstract && type.IsSealed) && - !typeof(Delegate).IsAssignableFrom(type) && - !typeof(Attribute).IsAssignableFrom(type) && - !type.IsGenericType && - (targetNamespaces == null || - targetNamespaces.Count <= 0 || - targetNamespaces.Contains(type.Namespace)); - void GenerateInnerInjectorType(TypeDefinition typeDef, InjectTypeInfo injectTypeInfo) { var injectorTypeDef = new TypeDefinition( diff --git a/VContainer/Assets/VContainer/Editor/CodeGen/VContainerILPostProcessor.cs b/VContainer/Assets/VContainer/Editor/CodeGen/VContainerILPostProcessor.cs index ef3a93ce..2477d59b 100644 --- a/VContainer/Assets/VContainer/Editor/CodeGen/VContainerILPostProcessor.cs +++ b/VContainer/Assets/VContainer/Editor/CodeGen/VContainerILPostProcessor.cs @@ -26,10 +26,8 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) if (!WillProcess(compiledAssembly)) return null; - var assembly = Assembly.Load(compiledAssembly.InMemoryAssembly.PeData); - var assemblyDefinition = Utils.LoadAssemblyDefinition(compiledAssembly); - var generator = new InjectionILGenerator(assemblyDefinition.MainModule, assembly, null); + var generator = new InjectionILGenerator(assemblyDefinition.MainModule, compiledAssembly, null); if (generator.TryGenerate(out var diagnosticMessages)) {