From ec50f462bc821c17b3c016a49a0680fa057a867c Mon Sep 17 00:00:00 2001 From: vis2k Date: Sat, 28 Aug 2021 20:09:52 +0800 Subject: [PATCH] Weaver tests: compilationpipeline dependencies moved into Unity.Mirror.Weaver.CodeGen assembly. so Tests assembly is actually weaved. --- .../ILPostProcessorFromFile.cs | 51 +++++++++++++++++++ .../ILPostProcessorFromFile.cs.meta | 3 ++ .../Tests/Editor/Weaver/WeaverAssembler.cs | 47 ++++------------- 3 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs create mode 100644 Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs.meta diff --git a/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs b/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs new file mode 100644 index 000000000..1e165d9f2 --- /dev/null +++ b/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs @@ -0,0 +1,51 @@ +// helper function to use ILPostProcessor for an assembly from file. +// we keep this in Weaver folder because we can access CompilationPipleine here. +// in tests folder we can't, unless we rename to "Unity.*.CodeGen", +// but then tests wouldn't be weaved anymore. +using System; +using System.IO; +using Unity.CompilationPipeline.Common.Diagnostics; +using Unity.CompilationPipeline.Common.ILPostProcessing; + +namespace Mirror.Weaver +{ + public static class ILPostProcessorFromFile + { + // read, weave, write file via ILPostProcessor + public static void ILPostProcessFile(string assemblyPath, string[] references, Action OnWarning, Action OnError) + { + // we COULD Weave() with a test logger manually. + // but for test result consistency on all platforms, + // let's invoke the ILPostProcessor here too. + CompiledAssemblyFromFile assembly = new CompiledAssemblyFromFile(assemblyPath); + assembly.References = references; + + // create ILPP and check WillProcess like Unity would. + ILPostProcessorHook ilpp = new ILPostProcessorHook(); + if (ilpp.WillProcess(assembly)) + { + //Debug.Log("Will Process: " + assembly.Name); + + // process it like Unity would + ILPostProcessResult result = ilpp.Process(assembly); + + // handle the error messages like Unity would + foreach (DiagnosticMessage message in result.Diagnostics) + { + if (message.DiagnosticType == DiagnosticType.Warning) + { + OnWarning(message.MessageData); + } + else if (message.DiagnosticType == DiagnosticType.Error) + { + OnError(message.MessageData); + } + } + + // save the weaved assembly to file. + // some tests open it and check for certain IL code. + File.WriteAllBytes(assemblyPath, result.InMemoryAssembly.PeData); + } + } + } +} diff --git a/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs.meta b/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs.meta new file mode 100644 index 000000000..e06dfa7b0 --- /dev/null +++ b/Assets/Mirror/Editor/Weaver/EntryPointILPostProcessor/ILPostProcessorFromFile.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2a4b115486b74d27a9540f3c39ae2d46 +timeCreated: 1630152191 \ No newline at end of file diff --git a/Assets/Mirror/Tests/Editor/Weaver/WeaverAssembler.cs b/Assets/Mirror/Tests/Editor/Weaver/WeaverAssembler.cs index c99c1fcf7..f28160150 100644 --- a/Assets/Mirror/Tests/Editor/Weaver/WeaverAssembler.cs +++ b/Assets/Mirror/Tests/Editor/Weaver/WeaverAssembler.cs @@ -3,8 +3,6 @@ using System.IO; using System.Linq; using System.Threading; -using Unity.CompilationPipeline.Common.Diagnostics; -using Unity.CompilationPipeline.Common.ILPostProcessing; using UnityEditor.Compilation; using UnityEngine; @@ -126,10 +124,8 @@ public static void Build(Action OnWarning, Action OnError) // TODO invoke ILPostProcessor manually // TODO save to file manually, so tests using the DLLs use the waved ones. - // we COULD Weave() with a test logger manually. - // but for test result consistency on all platforms, - // let's invoke the ILPostProcessor here too. - CompiledAssemblyFromFile assembly = new CompiledAssemblyFromFile(assemblyPath); + + // // References needs to be set to something. // otherwise we get NullReferenceException in WillProcess while // checking References. @@ -139,38 +135,15 @@ public static void Build(Action OnWarning, Action OnError) references.AddRange(assemblyBuilder.defaultReferences); if (assemblyBuilder.additionalReferences != null) references.AddRange(assemblyBuilder.additionalReferences); - assembly.References = references.ToArray(); - // create ILPP and check WillProcess like Unity would. - ILPostProcessorHook ilpp = new ILPostProcessorHook(); - if (ilpp.WillProcess(assembly)) - { - Debug.Log("Will Process: " + assembly.Name); - - // process it like Unity would - ILPostProcessResult result = ilpp.Process(assembly); - - // handle the error messages like Unity would - foreach (DiagnosticMessage message in result.Diagnostics) - { - if (message.DiagnosticType == DiagnosticType.Warning) - { - OnWarning(message.MessageData); - } - else if (message.DiagnosticType == DiagnosticType.Error) - { - OnError(message.MessageData); - } - } - - // save the weaved assembly to file. - // some tests open it and check for certain IL code. - File.WriteAllBytes(assemblyPath, result.InMemoryAssembly.PeData); - } - else - { - Debug.LogWarning("WONT PROCESS: " + assembly.Name); - } + // we COULD Weave() with a test logger manually. + // but for test result consistency on all platforms, + // let's invoke the ILPostProcessor here too. + // NOTE: CompilationPipeline can only be imported if this + // assembly's name is 'Unity.*.CodeGen'. + // BUT: then this assembly itself isn't weaved. + // we need it to be weaved for tests too though. + ILPostProcessorFromFile.ILPostProcessFile(assemblyPath, references.ToArray(), OnWarning, OnError); #endif };