-
Notifications
You must be signed in to change notification settings - Fork 461
refactor: ILPP workarounds for 2019.4 and 2020.2+ bugs #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bda3a04
refactor: ilpp refactor to work with older versions of unity (2019.4)
wackoisgod 6f62936
fix: Removing the need to load the asmdef and fixing a compile revert…
wackoisgod fad7410
fix: We need to make this public as to follow the ILPP rules
wackoisgod 87d09b2
ci: add unity 2019.4 version target
0xFA11 f010ec2
refactor: replace if-guards with UNITY_X_Y_OR_NEWER macros, minor fix…
0xFA11 d74fca8
refactor: add `__` prefix to framework internal types & fields and ma…
0xFA11 4cee658
ci: disable built-in Unity ILPP on 2021.1+ (trunk) temporarily
0xFA11 e3282ec
refactor: minor naming fixes, removing 2021.1+ guards (disable ILPP o…
0xFA11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #if !UNITY_2020_2_OR_NEWER | ||
| using System.IO; | ||
| using Unity.CompilationPipeline.Common.ILPostProcessing; | ||
|
|
||
| namespace MLAPI.Editor.CodeGen | ||
| { | ||
| internal class ILPostProcessCompiledAssembly : ICompiledAssembly | ||
| { | ||
| private readonly string m_AssemblyFilename; | ||
| private readonly string m_OutputPath; | ||
| private InMemoryAssembly m_InMemoryAssembly; | ||
|
|
||
| public ILPostProcessCompiledAssembly(string asmName, string[] refs, string[] defines, string outputPath) | ||
| { | ||
| m_AssemblyFilename = asmName; | ||
| Name = Path.GetFileNameWithoutExtension(m_AssemblyFilename); | ||
| References = refs; | ||
| Defines = defines; | ||
| m_OutputPath = outputPath; | ||
| } | ||
|
|
||
| public string Name { get; } | ||
| public string[] References { get; } | ||
| public string[] Defines { get; } | ||
|
|
||
| public InMemoryAssembly InMemoryAssembly | ||
| { | ||
| get | ||
| { | ||
| if (m_InMemoryAssembly == null) | ||
| { | ||
| m_InMemoryAssembly = new InMemoryAssembly( | ||
| File.ReadAllBytes(Path.Combine(m_OutputPath, m_AssemblyFilename)), | ||
| File.ReadAllBytes(Path.Combine(m_OutputPath, $"{Path.GetFileNameWithoutExtension(m_AssemblyFilename)}.pdb"))); | ||
| } | ||
|
|
||
| return m_InMemoryAssembly; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
11 changes: 11 additions & 0 deletions
11
com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessCompiledAssembly.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #if !UNITY_2020_2_OR_NEWER | ||
| using Unity.CompilationPipeline.Common.ILPostProcessing; | ||
|
|
||
| namespace MLAPI.Editor.CodeGen | ||
| { | ||
| public abstract class ILPostProcessor | ||
| { | ||
| public abstract bool WillProcess(ICompiledAssembly compiledAssembly); | ||
| public abstract ILPostProcessResult Process(ICompiledAssembly compiledAssembly); | ||
| public abstract ILPostProcessor GetInstance(); | ||
| } | ||
| } | ||
| #endif |
11 changes: 11 additions & 0 deletions
11
com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
217 changes: 217 additions & 0 deletions
217
com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| #if !UNITY_2020_2_OR_NEWER | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Unity.CompilationPipeline.Common.Diagnostics; | ||
| using Unity.CompilationPipeline.Common.ILPostProcessing; | ||
| using UnityEditor; | ||
| using UnityEditor.Compilation; | ||
| using UnityEngine; | ||
|
|
||
| using Assembly = System.Reflection.Assembly; | ||
|
|
||
| namespace MLAPI.Editor.CodeGen | ||
| { | ||
| internal static class ILPostProcessProgram | ||
| { | ||
| private static ILPostProcessor[] s_ILPostProcessors { get; set; } | ||
|
|
||
| [InitializeOnLoadMethod] | ||
| private static void OnInitializeOnLoad() | ||
| { | ||
| CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished; | ||
| s_ILPostProcessors = FindAllPostProcessors(); | ||
| } | ||
|
|
||
| private static ILPostProcessor[] FindAllPostProcessors() | ||
| { | ||
| var typesDerivedFrom = TypeCache.GetTypesDerivedFrom<ILPostProcessor>(); | ||
| var localILPostProcessors = new List<ILPostProcessor>(typesDerivedFrom.Count); | ||
|
|
||
| foreach (var typeCollection in typesDerivedFrom) | ||
| { | ||
| try | ||
| { | ||
| localILPostProcessors.Add((ILPostProcessor)Activator.CreateInstance(typeCollection)); | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| Debug.LogError($"Could not create ILPostProcessor ({typeCollection.FullName}):{Environment.NewLine}{exception.StackTrace}"); | ||
| } | ||
| } | ||
|
|
||
| // Default sort by type full name | ||
| localILPostProcessors.Sort((left, right) => string.Compare(left.GetType().FullName, right.GetType().FullName, StringComparison.Ordinal)); | ||
|
|
||
| return localILPostProcessors.ToArray(); | ||
| } | ||
|
|
||
| private static void OnCompilationFinished(string targetAssembly, CompilerMessage[] messages) | ||
| { | ||
| if (messages.Length > 0) | ||
| { | ||
| if (messages.Any(msg => msg.type == CompilerMessageType.Error)) | ||
| { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Should not run on the editor only assemblies | ||
| if (targetAssembly.Contains("-Editor") || targetAssembly.Contains(".Editor")) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Should not run on Unity Engine modules but we can run on the MLAPI Runtime DLL | ||
| if ((targetAssembly.Contains("com.unity") || Path.GetFileName(targetAssembly).StartsWith("Unity")) && !targetAssembly.Contains("Unity.Multiplayer.")) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Debug.Log($"Running MLAPI ILPP on {targetAssembly}"); | ||
|
|
||
| var outputDirectory = $"{Application.dataPath}/../{Path.GetDirectoryName(targetAssembly)}"; | ||
| var unityEngine = string.Empty; | ||
| var mlapiRuntimeAssemblyPath = string.Empty; | ||
| var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | ||
| var usesMLAPI = false; | ||
| var foundThisAssembly = false; | ||
|
|
||
| var depenencyPaths = new List<string>(); | ||
| foreach (var assembly in assemblies) | ||
| { | ||
| // Find the assembly currently being compiled from domain assembly list and check if it's using unet | ||
| if (assembly.GetName().Name == Path.GetFileNameWithoutExtension(targetAssembly)) | ||
| { | ||
| foundThisAssembly = true; | ||
| foreach (var dependency in assembly.GetReferencedAssemblies()) | ||
| { | ||
| // Since this assembly is already loaded in the domain this is a no-op and returns the | ||
| // already loaded assembly | ||
| depenencyPaths.Add(Assembly.Load(dependency).Location); | ||
| if (dependency.Name.Contains(CodeGenHelpers.RuntimeAssemblyName)) | ||
| { | ||
| usesMLAPI = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if (assembly.Location.Contains("UnityEngine.CoreModule")) | ||
| { | ||
| unityEngine = assembly.Location; | ||
| } | ||
|
|
||
| if (assembly.Location.Contains(CodeGenHelpers.RuntimeAssemblyName)) | ||
| { | ||
| mlapiRuntimeAssemblyPath = assembly.Location; | ||
| } | ||
| } | ||
| catch (NotSupportedException) | ||
| { | ||
| // in memory assembly, can't get location | ||
| } | ||
| } | ||
|
|
||
| if (!foundThisAssembly) | ||
| { | ||
| // Target assembly not found in current domain, trying to load it to check references | ||
| // will lead to trouble in the build pipeline, so lets assume it should go to weaver. | ||
| // Add all assemblies in current domain to dependency list since there could be a | ||
| // dependency lurking there (there might be generated assemblies so ignore file not found exceptions). | ||
| // (can happen in runtime test framework on editor platform and when doing full library reimport) | ||
| foreach (var assembly in assemblies) | ||
| { | ||
| try | ||
| { | ||
| if (!(assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder)) | ||
| { | ||
| depenencyPaths.Add(Assembly.Load(assembly.GetName().Name).Location); | ||
| } | ||
| } | ||
| catch (FileNotFoundException) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| usesMLAPI = true; | ||
| } | ||
|
|
||
| // We check if we are the MLAPI! | ||
| if (!usesMLAPI) | ||
| { | ||
| // we shall also check and see if it we are ourself | ||
| usesMLAPI = targetAssembly.Contains(CodeGenHelpers.RuntimeAssemblyName); | ||
| } | ||
|
|
||
| if (!usesMLAPI) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(unityEngine)) | ||
| { | ||
| Debug.LogError("Failed to find UnityEngine assembly"); | ||
| return; | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(mlapiRuntimeAssemblyPath)) | ||
| { | ||
| Debug.LogError("Failed to find mlapi runtime assembly"); | ||
| return; | ||
| } | ||
|
|
||
| var assemblyPathName = Path.GetFileName(targetAssembly); | ||
|
|
||
| var targetCompiledAssembly = new ILPostProcessCompiledAssembly(assemblyPathName, depenencyPaths.ToArray(), null, outputDirectory); | ||
|
|
||
| void WriteAssembly(InMemoryAssembly inMemoryAssembly, string outputPath, string assName) | ||
| { | ||
| if (inMemoryAssembly == null) | ||
| { | ||
| throw new ArgumentException("InMemoryAssembly has never been accessed or modified"); | ||
| } | ||
|
|
||
| var asmPath = Path.Combine(outputPath, assName); | ||
| var pdbFileName = $"{Path.GetFileNameWithoutExtension(assName)}.pdb"; | ||
| var pdbPath = Path.Combine(outputPath, pdbFileName); | ||
|
|
||
| File.WriteAllBytes(asmPath, inMemoryAssembly.PeData); | ||
| File.WriteAllBytes(pdbPath, inMemoryAssembly.PdbData); | ||
| } | ||
|
|
||
| foreach (var i in s_ILPostProcessors) | ||
| { | ||
| var result = i.Process(targetCompiledAssembly); | ||
| if (result == null) | ||
| continue; | ||
|
|
||
| if (result.Diagnostics.Count > 0) | ||
| { | ||
| Debug.LogError($"ILPostProcessor - {i.GetType().Name} failed to run on {targetCompiledAssembly.Name}"); | ||
|
|
||
| foreach (var message in result.Diagnostics) | ||
| { | ||
| switch (message.DiagnosticType) | ||
| { | ||
| case DiagnosticType.Error: | ||
| Debug.LogError($"ILPostProcessor Error - {message.MessageData} {message.File} {message.Line} {message.Column}"); | ||
| break; | ||
| case DiagnosticType.Warning: | ||
| Debug.LogWarning($"ILPostProcessor Warning - {message.MessageData} {message.File} {message.Line} {message.Column}"); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| // we now need to write out the result? | ||
| WriteAssembly(result.InMemoryAssembly, outputDirectory, assemblyPathName); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif |
11 changes: 11 additions & 0 deletions
11
com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.