-
-
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: custom reader/writer via extension methods (#1047)
* Remove hardcoded writers from weaver * Remove hardcoded readers from weaver * Remove unused stuff * cleanups * Cleanup some comments * remove unused import * Update Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs * Improve comments a bit * small cleanups * Remove redundant private keyword * Remove unnecesary blank space * Remove unused field * Measure how long it takes to find the readers and writers * Find all extension methods for readers and writers * Get rid of debugging log * Don't comment obvious stuff * Don't swallow exception * only scan static classes for extension methods * Add unit test for custom readers and writers * Update Assets/Mirror/Editor/Weaver/Weaver.cs * Update Assets/Mirror/Runtime/NetworkWriterExt.cs * Update ReaderWriterProcessor.cs * Renamed to avoid abreviations * Move the extensions inside the same .cs * Update NetworkReader.cs * Update NetworkWriter.cs
- Loading branch information
Showing
10 changed files
with
622 additions
and
505 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
89 changes: 89 additions & 0 deletions
89
Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs
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,89 @@ | ||
using System; | ||
using Mono.CecilX; | ||
using UnityEditor.Compilation; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
public static class ReaderWriterProcessor | ||
{ | ||
// find all readers and writers and register them | ||
public static void ProcessReadersAndWriters(AssemblyDefinition CurrentAssembly) | ||
{ | ||
Readers.Init(); | ||
Writers.Init(); | ||
|
||
foreach (Assembly unityAsm in CompilationPipeline.GetAssemblies()) | ||
{ | ||
if (unityAsm.name != CurrentAssembly.Name.Name) | ||
{ | ||
using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver()) | ||
using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(unityAsm.outputPath, new ReaderParameters { ReadWrite = false, ReadSymbols = true, AssemblyResolver = asmResolver })) | ||
{ | ||
ProcessAssemblyClasses(CurrentAssembly, assembly); | ||
} | ||
} | ||
} | ||
|
||
ProcessAssemblyClasses(CurrentAssembly, CurrentAssembly); | ||
} | ||
|
||
static void ProcessAssemblyClasses(AssemblyDefinition CurrentAssembly, AssemblyDefinition assembly) | ||
{ | ||
foreach (TypeDefinition klass in assembly.MainModule.Types) | ||
{ | ||
// extension methods only live in static classes | ||
// static classes are represented as sealed and abstract | ||
if (klass.IsAbstract && klass.IsSealed) | ||
{ | ||
LoadWriters(CurrentAssembly, klass); | ||
LoadReaders(CurrentAssembly, klass); | ||
} | ||
} | ||
} | ||
|
||
static void LoadWriters(AssemblyDefinition currentAssembly, TypeDefinition klass) | ||
{ | ||
// register all the writers in this class. Skip the ones with wrong signature | ||
foreach (MethodDefinition method in klass.Methods) | ||
{ | ||
if (method.Parameters.Count != 2) | ||
continue; | ||
|
||
if (method.Parameters[0].ParameterType.FullName != "Mirror.NetworkWriter") | ||
continue; | ||
|
||
if (method.ReturnType.FullName != "System.Void") | ||
continue; | ||
|
||
if (method.GetCustomAttribute("System.Runtime.CompilerServices.ExtensionAttribute") == null) | ||
continue; | ||
|
||
TypeReference dataType = method.Parameters[1].ParameterType; | ||
Writers.Register(dataType, currentAssembly.MainModule.ImportReference(method)); | ||
} | ||
} | ||
|
||
static void LoadReaders(AssemblyDefinition currentAssembly, TypeDefinition klass) | ||
{ | ||
// register all the reader in this class. Skip the ones with wrong signature | ||
foreach (MethodDefinition method in klass.Methods) | ||
{ | ||
if (method.Parameters.Count != 1) | ||
continue; | ||
|
||
if (method.Parameters[0].ParameterType.FullName != "Mirror.NetworkReader") | ||
continue; | ||
|
||
if (method.ReturnType.FullName == "System.Void") | ||
continue; | ||
|
||
if (method.GetCustomAttribute("System.Runtime.CompilerServices.ExtensionAttribute") == null) | ||
continue; | ||
|
||
Readers.Register(method.ReturnType, currentAssembly.MainModule.ImportReference(method)); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Oops, something went wrong.