Weaver: Readers as non static class

This commit is contained in:
vis2k 2021-08-17 16:35:25 +08:00
parent 5fcf5eb62f
commit b7c186f2b9
4 changed files with 28 additions and 32 deletions

View File

@ -561,7 +561,7 @@ void DeserializeNetworkIdentityField(FieldDefinition syncVar, ILProcessor worker
// reader. for 'reader.Read()' below
worker.Emit(OpCodes.Ldarg_1);
// Read()
worker.Emit(OpCodes.Call, Readers.GetReadFunc(Weaver.weaverTypes.Import<uint>()));
worker.Emit(OpCodes.Call, ReaderWriterProcessor.readers.GetReadFunc(Weaver.weaverTypes.Import<uint>()));
// netId
worker.Emit(OpCodes.Stfld, netIdField);
@ -662,7 +662,7 @@ void DeserializeNetworkBehaviourField(FieldDefinition syncVar, ILProcessor worke
// reader. for 'reader.Read()' below
worker.Emit(OpCodes.Ldarg_1);
// Read()
worker.Emit(OpCodes.Call, Readers.GetReadFunc(Weaver.weaverTypes.Import<NetworkBehaviour.NetworkBehaviourSyncVar>()));
worker.Emit(OpCodes.Call, ReaderWriterProcessor.readers.GetReadFunc(Weaver.weaverTypes.Import<NetworkBehaviour.NetworkBehaviourSyncVar>()));
// netId
worker.Emit(OpCodes.Stfld, netIdField);
@ -726,7 +726,7 @@ void DeserializeNormalField(FieldDefinition syncVar, ILProcessor serWorker, Meth
}
*/
MethodReference readFunc = Readers.GetReadFunc(syncVar.FieldType);
MethodReference readFunc = ReaderWriterProcessor.readers.GetReadFunc(syncVar.FieldType);
if (readFunc == null)
{
Weaver.Error($"{syncVar.Name} has unsupported type. Use a supported Mirror type instead", syncVar);
@ -846,7 +846,7 @@ void GenerateDeSerialization()
// get dirty bits
serWorker.Append(serWorker.Create(OpCodes.Ldarg_1));
serWorker.Append(serWorker.Create(OpCodes.Call, Readers.GetReadFunc(Weaver.weaverTypes.Import<ulong>())));
serWorker.Append(serWorker.Create(OpCodes.Call, ReaderWriterProcessor.readers.GetReadFunc(Weaver.weaverTypes.Import<ulong>())));
serWorker.Append(serWorker.Create(OpCodes.Stloc_0));
// conditionally read each syncvar
@ -904,7 +904,7 @@ public static bool ReadArguments(MethodDefinition method, ILProcessor worker, Re
}
MethodReference readFunc = Readers.GetReadFunc(param.ParameterType);
MethodReference readFunc = ReaderWriterProcessor.readers.GetReadFunc(param.ParameterType);
if (readFunc == null)
{

View File

@ -12,11 +12,10 @@ namespace Mirror.Weaver
public static class ReaderWriterProcessor
{
public static Writers writers = new Writers();
public static Readers readers = new Readers();
public static bool Process(AssemblyDefinition CurrentAssembly)
{
// initialize readers & writers
Readers.Init();
foreach (Assembly unityAsm in CompilationPipeline.GetAssemblies())
{
if (unityAsm.name == "Mirror")
@ -60,7 +59,7 @@ static bool LoadMessageReadWriter(ModuleDefinition module, TypeDefinition klass)
bool modified = false;
if (!klass.IsAbstract && !klass.IsInterface && klass.ImplementsInterface<NetworkMessage>())
{
Readers.GetReadFunc(module.ImportReference(klass));
readers.GetReadFunc(module.ImportReference(klass));
writers.GetWriteFunc(module.ImportReference(klass));
modified = true;
}
@ -121,7 +120,7 @@ static bool LoadDeclaredReaders(AssemblyDefinition currentAssembly, TypeDefiniti
if (method.HasGenericParameters)
continue;
Readers.Register(method.ReturnType, currentAssembly.MainModule.ImportReference(method));
readers.Register(method.ReturnType, currentAssembly.MainModule.ImportReference(method));
modified = true;
}
return modified;
@ -171,7 +170,7 @@ public static void InitializeReaderAndWriters(AssemblyDefinition currentAssembly
//worker.Emit(OpCodes.Call, Weaver.weaverTypes.logWarningReference);
writers.InitializeWriters(worker);
Readers.InitializeReaders(worker);
readers.InitializeReaders(worker);
worker.Emit(OpCodes.Ret);

View File

@ -41,7 +41,7 @@ static void GenerateReadersAndWriters(TypeReference tr)
{
if (!argument.IsGenericParameter)
{
Readers.GetReadFunc(argument);
ReaderWriterProcessor.readers.GetReadFunc(argument);
ReaderWriterProcessor.writers.GetWriteFunc(argument);
}
}

View File

@ -6,16 +6,13 @@
namespace Mirror.Weaver
{
public static class Readers
// not static, because ILPostProcessor is multithreaded
public class Readers
{
static Dictionary<TypeReference, MethodReference> readFuncs;
Dictionary<TypeReference, MethodReference> readFuncs =
new Dictionary<TypeReference, MethodReference>(new TypeReferenceComparer());
public static void Init()
{
readFuncs = new Dictionary<TypeReference, MethodReference>(new TypeReferenceComparer());
}
internal static void Register(TypeReference dataType, MethodReference methodReference)
internal void Register(TypeReference dataType, MethodReference methodReference)
{
if (readFuncs.ContainsKey(dataType))
{
@ -30,7 +27,7 @@ internal static void Register(TypeReference dataType, MethodReference methodRefe
readFuncs[imported] = methodReference;
}
static void RegisterReadFunc(TypeReference typeReference, MethodDefinition newReaderFunc)
void RegisterReadFunc(TypeReference typeReference, MethodDefinition newReaderFunc)
{
Register(typeReference, newReaderFunc);
@ -38,7 +35,7 @@ static void RegisterReadFunc(TypeReference typeReference, MethodDefinition newRe
}
// Finds existing reader for type, if non exists trys to create one
public static MethodReference GetReadFunc(TypeReference variable)
public MethodReference GetReadFunc(TypeReference variable)
{
if (readFuncs.TryGetValue(variable, out MethodReference foundFunc))
{
@ -51,7 +48,7 @@ public static MethodReference GetReadFunc(TypeReference variable)
}
}
static MethodReference GenerateReader(TypeReference variableReference)
MethodReference GenerateReader(TypeReference variableReference)
{
// Arrays are special, if we resolve them, we get the element type,
// so the following ifs might choke on it for scriptable objects
@ -148,7 +145,7 @@ static MethodReference GenerateReader(TypeReference variableReference)
return GenerateClassOrStructReadFunction(variableReference);
}
static MethodReference GetNetworkBehaviourReader(TypeReference variableReference)
MethodReference GetNetworkBehaviourReader(TypeReference variableReference)
{
// uses generic ReadNetworkBehaviour rather than having weaver create one for each NB
MethodReference generic = Weaver.weaverTypes.readNetworkBehaviourGeneric;
@ -162,7 +159,7 @@ static MethodReference GetNetworkBehaviourReader(TypeReference variableReference
return readFunc;
}
static MethodDefinition GenerateEnumReadFunc(TypeReference variable)
MethodDefinition GenerateEnumReadFunc(TypeReference variable)
{
MethodDefinition readerFunc = GenerateReaderFunction(variable);
@ -178,7 +175,7 @@ static MethodDefinition GenerateEnumReadFunc(TypeReference variable)
return readerFunc;
}
static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable)
MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable)
{
GenericInstanceType genericInstance = (GenericInstanceType)variable;
TypeReference elementType = genericInstance.GenericArguments[0];
@ -198,7 +195,7 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable)
return readerFunc;
}
static MethodDefinition GenerateReaderFunction(TypeReference variable)
MethodDefinition GenerateReaderFunction(TypeReference variable)
{
string functionName = "_Read_" + variable.FullName;
@ -216,7 +213,7 @@ static MethodDefinition GenerateReaderFunction(TypeReference variable)
return readerFunc;
}
static MethodDefinition GenerateReadCollection(TypeReference variable, TypeReference elementType, string readerFunction)
MethodDefinition GenerateReadCollection(TypeReference variable, TypeReference elementType, string readerFunction)
{
MethodDefinition readerFunc = GenerateReaderFunction(variable);
// generate readers for the element
@ -241,7 +238,7 @@ static MethodDefinition GenerateReadCollection(TypeReference variable, TypeRefer
return readerFunc;
}
static MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable)
MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable)
{
MethodDefinition readerFunc = GenerateReaderFunction(variable);
@ -263,7 +260,7 @@ static MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable
return readerFunc;
}
static void GenerateNullCheck(ILProcessor worker)
void GenerateNullCheck(ILProcessor worker)
{
// if (!reader.ReadBoolean()) {
// return null;
@ -280,7 +277,7 @@ static void GenerateNullCheck(ILProcessor worker)
}
// Initialize the local variable with a new instance
static void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition td)
void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition td)
{
if (variable.IsValueType)
{
@ -313,7 +310,7 @@ static void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition
}
}
static void ReadAllFields(TypeReference variable, ILProcessor worker)
void ReadAllFields(TypeReference variable, ILProcessor worker)
{
foreach (FieldDefinition field in variable.FindAllPublicFields())
{
@ -338,7 +335,7 @@ static void ReadAllFields(TypeReference variable, ILProcessor worker)
}
// Save a delegate for each one of the readers into Reader{T}.read
internal static void InitializeReaders(ILProcessor worker)
internal void InitializeReaders(ILProcessor worker)
{
ModuleDefinition module = Weaver.CurrentAssembly.MainModule;