-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use ILPostProcessor for weaver (#525)
The weaver now uses ILPostProcessor API from unity. This is the same thing MLAPI will use soon. This is an internal feature from Unity that is still subject to change, see: https://forum.unity.com/threads/how-does-unity-do-codegen-and-why-cant-i-do-it-myself.853867/#post-5646937 but it solves a lot of issues: * The weaver can now weave the runtime too!, so mirror core can use [Syncvar] and all that jazz. * The weaver is multithreaded, each assembly is weaved in a different thread. In theory, this should be faster when you have multiple CPU cores, but I have not benchmarked it. * No more cases where weaver doesn't run * Some weaver errors now take you to the code when double clicking * No need to rebuild all assemblies to bootstrap weaver Note editor scripts are no longer weaved. Apparently, ILPostProcessor is not applied consistently to editor scripts. For this reason, some of the tests must now be done in runtime tests. BREAKING CHANGE: MirrorNG assembly no longer contains the components. Reference Mirror.Components instead. BREAKING CHANGE: Editor scripts are no longer weaved
- Loading branch information
Showing
382 changed files
with
827 additions
and
565 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
|
2 changes: 1 addition & 1 deletion
2
...or/Weaver/CompilationFinishedHook.cs.meta → Assets/Mirror/AssemblyInfo.cs.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 3 additions & 2 deletions
5
...s/Experimental/Mirror.Experimental.asmdef → ...irror/Components/Mirror.Components.asmdef
This file contains 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
2 changes: 1 addition & 1 deletion
2
...rror.Tests.Performance.Editor.asmdef.meta → .../Components/Mirror.Components.asmdef.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,28 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Mono.Cecil; | ||
using UnityEngine; | ||
using Mono.Cecil.Cil; | ||
using Unity.CompilationPipeline.Common.Diagnostics; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
public class Logger : IWeaverLogger | ||
{ | ||
public void Error(string msg) | ||
public List<DiagnosticMessage> Diagnostics = new List<DiagnosticMessage>(); | ||
|
||
public void Error(string message) | ||
{ | ||
Debug.LogError(msg); | ||
AddMessage(message, null, DiagnosticType.Error); | ||
} | ||
|
||
public void Error(string message, MemberReference mr) | ||
{ | ||
Error($"{message} (at {mr})"); | ||
} | ||
|
||
public void Error(string message, MethodDefinition md) | ||
{ | ||
AddMessage($"{message} (at {md})", md.DebugInformation.SequencePoints.FirstOrDefault(), DiagnosticType.Error); | ||
} | ||
|
||
public void Warning(string message, MemberReference mr) | ||
{ | ||
Warning($"{message} (at {mr})"); | ||
} | ||
|
||
public void Warning(string msg) | ||
public void Warning(string message) | ||
{ | ||
AddMessage($"{message}", null, DiagnosticType.Warning); | ||
} | ||
|
||
private void AddMessage(string message, SequencePoint sequencePoint, DiagnosticType diagnosticType) | ||
{ | ||
Debug.LogWarning(msg); | ||
Diagnostics.Add(new DiagnosticMessage | ||
{ | ||
DiagnosticType = diagnosticType, | ||
File = sequencePoint?.Document.Url.Replace($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}", ""), | ||
Line = sequencePoint?.StartLine ?? 0, | ||
Column = sequencePoint?.StartColumn ?? 0, | ||
MessageData = message | ||
}) ; | ||
} | ||
} | ||
} |
This file contains 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,45 @@ | ||
using Unity.CompilationPipeline.Common.ILPostProcessing; | ||
using System.Linq; | ||
using System.IO; | ||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
public class MirrorIlPostProcessor : ILPostProcessor | ||
{ | ||
public const string RuntimeAssemblyName = "Mirror"; | ||
|
||
public override ILPostProcessor GetInstance() => this; | ||
|
||
public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly) | ||
{ | ||
if (!WillProcess(compiledAssembly)) | ||
return null; | ||
|
||
var logger = new Logger(); | ||
var weaver = new Weaver(logger); | ||
|
||
AssemblyDefinition assemblyDefinition = weaver.Weave(compiledAssembly); | ||
|
||
// write | ||
var pe = new MemoryStream(); | ||
var pdb = new MemoryStream(); | ||
|
||
var writerParameters = new WriterParameters | ||
{ | ||
SymbolWriterProvider = new PortablePdbWriterProvider(), | ||
SymbolStream = pdb, | ||
WriteSymbols = true | ||
}; | ||
|
||
assemblyDefinition?.Write(pe, writerParameters); | ||
|
||
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), logger.Diagnostics); | ||
} | ||
|
||
public override bool WillProcess(ICompiledAssembly compiledAssembly) => | ||
compiledAssembly.Name == RuntimeAssemblyName || | ||
compiledAssembly.References.Any(filePath => Path.GetFileNameWithoutExtension(filePath) == RuntimeAssemblyName); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...Editor/EnterPlayModeSettingsCheck.cs.meta → ...itor/Weaver/MirrorIlPostProcessor.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.