From 2dc280d2927633f6bf3fe5b90b6727773ffe4a45 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Mon, 28 Sep 2020 12:12:17 -0500 Subject: [PATCH] Refactor reader function generation --- Assets/Mirror/Editor/Weaver/Readers.cs | 79 +++++++------------------- 1 file changed, 22 insertions(+), 57 deletions(-) diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index 0a43d14fc..62d8b8b4f 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -138,21 +138,11 @@ static MethodDefinition GenerateArrayReadFunc(TypeReference variable, int recurs return null; } - string functionName = "_Read_" + variable.FullName; - - // create new reader for this type - MethodDefinition readerFunc = new MethodDefinition(functionName, - MethodAttributes.Public | - MethodAttributes.Static | - MethodAttributes.HideBySig, - variable); - - readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import())); + MethodDefinition readerFunc = GenerateReaderFunction(variable); readerFunc.Body.Variables.Add(new VariableDefinition(WeaverTypes.Import())); readerFunc.Body.Variables.Add(new VariableDefinition(variable)); readerFunc.Body.Variables.Add(new VariableDefinition(WeaverTypes.Import())); - readerFunc.Body.InitLocals = true; ILProcessor worker = readerFunc.Body.GetILProcessor(); @@ -214,19 +204,7 @@ static MethodDefinition GenerateArrayReadFunc(TypeReference variable, int recurs static MethodDefinition GenerateEnumReadFunc(TypeReference variable) { - string functionName = "_Read_" + variable.FullName; - - // create new reader for this type - var readerFunc = new MethodDefinition(functionName, - MethodAttributes.Public | - MethodAttributes.Static | - MethodAttributes.HideBySig, - Weaver.CurrentAssembly.MainModule.ImportReference(variable)); - - // create local for return value - readerFunc.Body.InitLocals = true; - - readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import())); + MethodDefinition readerFunc = GenerateReaderFunction(variable); ILProcessor worker = readerFunc.Body.GetILProcessor(); @@ -252,16 +230,7 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable, int return null; } - string functionName = "_Read_" + variable.FullName; - - // create new reader for this type - MethodDefinition readerFunc = new MethodDefinition(functionName, - MethodAttributes.Public | - MethodAttributes.Static | - MethodAttributes.HideBySig, - variable); - - readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import())); + MethodDefinition readerFunc = GenerateReaderFunction(variable); // int lengh readerFunc.Body.Variables.Add(new VariableDefinition(WeaverTypes.Import())); @@ -269,7 +238,7 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable, int readerFunc.Body.Variables.Add(new VariableDefinition(elementType.MakeArrayType())); // int i; readerFunc.Body.Variables.Add(new VariableDefinition(WeaverTypes.Import())); - readerFunc.Body.InitLocals = true; + ILProcessor worker = readerFunc.Body.GetILProcessor(); @@ -323,6 +292,22 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable, int return readerFunc; } + private static MethodDefinition GenerateReaderFunction(TypeReference variable) + { + string functionName = "_Read_" + variable.FullName; + + // create new reader for this type + var readerFunc = new MethodDefinition(functionName, + MethodAttributes.Public | + MethodAttributes.Static | + MethodAttributes.HideBySig, + Weaver.CurrentAssembly.MainModule.ImportReference(variable)); + + readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import())); + readerFunc.Body.InitLocals = true; + return readerFunc; + } + static MethodDefinition GenerateListReadFunc(TypeReference variable, int recursionCount) { GenericInstanceType genericInstance = (GenericInstanceType)variable; @@ -335,21 +320,11 @@ static MethodDefinition GenerateListReadFunc(TypeReference variable, int recursi return null; } - string functionName = "_Read_" + variable.FullName + "_"; - - // create new reader for this type - MethodDefinition readerFunc = new MethodDefinition(functionName, - MethodAttributes.Public | - MethodAttributes.Static | - MethodAttributes.HideBySig, - variable); - - readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import())); + MethodDefinition readerFunc = GenerateReaderFunction(variable); readerFunc.Body.Variables.Add(new VariableDefinition(WeaverTypes.Import())); readerFunc.Body.Variables.Add(new VariableDefinition(variable)); readerFunc.Body.Variables.Add(new VariableDefinition(WeaverTypes.Import())); - readerFunc.Body.InitLocals = true; ILProcessor worker = readerFunc.Body.GetILProcessor(); @@ -428,20 +403,10 @@ static MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable return null; } - string functionName = "_Read_" + variable.FullName; - - // create new reader for this type - MethodDefinition readerFunc = new MethodDefinition(functionName, - MethodAttributes.Public | - MethodAttributes.Static | - MethodAttributes.HideBySig, - variable); + MethodDefinition readerFunc = GenerateReaderFunction(variable); // create local for return value readerFunc.Body.Variables.Add(new VariableDefinition(variable)); - readerFunc.Body.InitLocals = true; - - readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import())); ILProcessor worker = readerFunc.Body.GetILProcessor();