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