quantum-space-buddies/MirrorWeaver/QSBReaderWriterProcessor.cs
2022-02-13 19:45:29 -08:00

73 lines
2.4 KiB
C#

using Mirror;
using Mirror.Weaver;
using Mono.Cecil;
using System;
using System.Diagnostics;
namespace MirrorWeaver
{
public static class QSBReaderWriterProcessor
{
/// <summary>
/// finds usages of the generic read/write methods and generates read/write functions for them.
/// <para/>
/// traverses from non generic classes up thru base types
/// in order to replace generic parameters with their corresponding generic arguments.
/// </summary>
public static void Process(ModuleDefinition module, Writers writers, Readers readers, ref bool weavingFailed)
{
var sw = Stopwatch.StartNew();
var NetworkWriter_Write = typeof(NetworkWriter).GetMethod(nameof(NetworkWriter.Write));
var NetworkReader_Read = typeof(NetworkReader).GetMethod(nameof(NetworkReader.Read));
var count = 0;
foreach (var type in module.GetTypes())
{
if (type.HasGenericParameters) continue;
TypeReference currentType = type;
while (currentType != null)
{
var currentTypeDef = currentType.Resolve();
foreach (var method in currentTypeDef.Methods)
{
if (!method.HasBody) continue;
foreach (var instruction in method.Body.Instructions)
{
if (instruction.Operand is not GenericInstanceMethod calledMethod) continue;
if (calledMethod.DeclaringType.FullName == NetworkWriter_Write.DeclaringType.FullName &&
calledMethod.Name == NetworkWriter_Write.Name)
{
var argType = calledMethod.GenericArguments[0];
if (argType is GenericParameter genericParameter && genericParameter.Owner == currentTypeDef)
argType = ((GenericInstanceType)currentType).GenericArguments[genericParameter.Position];
writers.GetWriteFunc(argType, ref weavingFailed);
count++;
}
else if (calledMethod.DeclaringType.FullName == NetworkReader_Read.DeclaringType.FullName &&
calledMethod.Name == NetworkReader_Read.Name)
{
var argType = calledMethod.GenericArguments[0];
if (argType is GenericParameter genericParameter && genericParameter.Owner == currentTypeDef)
argType = ((GenericInstanceType)currentType).GenericArguments[genericParameter.Position];
readers.GetReadFunc(argType, ref weavingFailed);
count++;
}
}
}
currentType = currentTypeDef.BaseType;
}
}
Console.WriteLine($"got/generated {count} read/write funcs in {sw.ElapsedMilliseconds} ms");
}
}
}