Extenject
Extenject copied to clipboard
Reflection baking doesn't work for assembly definitions
Describe the bug When a DLL of an assembly definition is added to assemblies included in Reflection Baking, the DLL gets broken and is not executed.
To Reproduce Steps to reproduce the behavior:
- Import Zenject into an empty project.
- Go to Assets/Plugins/Zenject/OptionalExtras/SampleGame1 (Beginner)/Scripts
- Create an assembly definition called Beginner and add Zenject to assembly definition references.
- Create the ZenjectReflectionBakingSettings scriptable object.
- Untick "All Generated Assemblies", tick "Is Enabled In Editor".
- Add Zenject.dll and Beginner.dll to included assemblies.
- Open the Asteroids scene.
- Enter Play Mode.
- Get the following errors:
Error when modifying type 'Asteroid'
FileNotFoundException: Could not load file or assembly 'Zenject-usage, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
Unloading broken assembly Library/ScriptAssemblies/Beginner.dll, this assembly can cause crashes in the runtime
Detailed stack traces:
Error when modifying type 'Asteroid'
UnityEngine.Debug:LogError (object)
ModestTree.Log:ErrorException (string,System.Exception)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor:TryEditType (Zenject.ReflectionBaking.Mono.Cecil.TypeDefinition,System.Type) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:158)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor:Run () (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:84)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor:WeaveAssembly (Zenject.ReflectionBaking.Mono.Cecil.ModuleDefinition,System.Reflection.Assembly,System.Collections.Generic.List`1<string>) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:56)
Zenject.ReflectionBaking.ReflectionBakingBuildObserver:TryWeaveAssembly (string) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Unity/ReflectionBakingBuildObserver.cs:93)
Zenject.ReflectionBaking.ReflectionBakingBuildObserver:OnAssemblyCompiled (string,UnityEditor.Compilation.CompilerMessage[]) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Unity/ReflectionBakingBuildObserver.cs:37)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:TickCompilationPipeline (UnityEditor.Scripting.ScriptCompilation.EditorScriptCompilationOptions,UnityEditor.BuildTargetGroup,UnityEditor.BuildTarget,string[])
FileNotFoundException: Could not load file or assembly 'Zenject-usage, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
System.MonoCustomAttrs.GetCustomAttributesBase (System.Reflection.ICustomAttributeProvider obj, System.Type attributeType, System.Boolean inheritedOnly) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.MonoCustomAttrs.GetCustomAttributes (System.Reflection.ICustomAttributeProvider obj, System.Type attributeType, System.Boolean inherit) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Reflection.MonoMethod.GetCustomAttributes (System.Type attributeType, System.Boolean inherit) (at <695d1cc93cca45069c528c15c9fdd749>:0)
Zenject.Internal.ReflectionTypeAnalyzer+<>c__DisplayClass12_1.<GetMethodInfos>b__1 (System.Type a) (at <e711547f4dd74929ab297f402fe81f63>:0)
System.Linq.Enumerable.Any[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
Zenject.Internal.ReflectionTypeAnalyzer+<>c.<GetMethodInfos>b__12_0 (System.Reflection.MethodInfo x) (at <e711547f4dd74929ab297f402fe81f63>:0)
System.Linq.Enumerable+WhereArrayIterator`1[TSource].ToList () (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) (at <351e49e2a5bf4fd6beabb458ce2255f3>:0)
Zenject.Internal.ReflectionTypeAnalyzer.GetMethodInfos (System.Type type) (at <e711547f4dd74929ab297f402fe81f63>:0)
Zenject.Internal.ReflectionTypeAnalyzer.GetReflectionInfo (System.Type type) (at <e711547f4dd74929ab297f402fe81f63>:0)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor.TryEditType (Zenject.ReflectionBaking.Mono.Cecil.TypeDefinition typeDef, System.Type actualType) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:144)
UnityEngine.Debug:LogException(Exception)
ModestTree.Log:ErrorException(String, Exception)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor:TryEditType(TypeDefinition, Type) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:158)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor:Run() (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:84)
Zenject.ReflectionBaking.ReflectionBakingModuleEditor:WeaveAssembly(ModuleDefinition, Assembly, List`1) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Common/ReflectionBakingModuleEditor.cs:56)
Zenject.ReflectionBaking.ReflectionBakingBuildObserver:TryWeaveAssembly(String) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Unity/ReflectionBakingBuildObserver.cs:93)
Zenject.ReflectionBaking.ReflectionBakingBuildObserver:OnAssemblyCompiled(String, CompilerMessage[]) (at Assets/Plugins/Zenject/OptionalExtras/ReflectionBaking/Unity/ReflectionBakingBuildObserver.cs:37)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:TickCompilationPipeline(EditorScriptCompilationOptions, BuildTargetGroup, BuildTarget, String[])
Expected behavior Reflection is baked for Beginner.dll, the are no broken DLLs.
Screenshots
Extenject and Unity info (please complete the following information):
- Zenject version: 9.2.0 (also tried to build a package with the recent changes from @mnarimani (#5 #6 #9) and use it.)
- Unity version: 2020.3.8f1, 2020.3.10f1
- Project's scripting backend: Tried both Mono and IL2CPP, NET Standard 2.0 and NET4.x
The reflection baking works fine for Zenject itself, and for Assembly-CSharp.dll, but not for in-project assembly definitions.
It seems to me that this feature should be removed, because reflection is baked through il2cpp. I fixed this problem in my project and the a bench showed that the project even slowed down a little.