only apply special weaver stuff to qsb messages

This commit is contained in:
JohnCorby 2022-01-14 00:28:49 -08:00
parent 40b1227868
commit 0f0a3916e3
4 changed files with 24 additions and 10 deletions

View File

@ -7,5 +7,6 @@
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
<PackageReference Include="OuterWildsGameLibs" Version="1.1.12.125" IncludeAssets="compile" />
<ProjectReference Include="..\Mirror\Mirror.csproj" />
<ProjectReference Include="..\QSB\QSB.csproj" />
</ItemGroup>
</Project>

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using QSB.Messaging;
namespace Mirror.Weaver
{
@ -208,6 +209,9 @@ namespace Mirror.Weaver
return null;
}
public static bool IsQSBMessageType(this TypeDefinition typeDefinition) =>
typeDefinition.IsDerivedFrom<QSBMessage>() || typeDefinition.IsDerivedFrom<QSBMessageRaw>();
// Finds public fields in type and base type
public static IEnumerable<FieldDefinition> FindAllPublicFields(this TypeReference variable)
{
@ -217,15 +221,24 @@ namespace Mirror.Weaver
// Finds public fields in type and base type
public static IEnumerable<FieldDefinition> FindAllPublicFields(this TypeDefinition typeDefinition)
{
var isQSBMessageType = typeDefinition.IsQSBMessageType();
while (typeDefinition != null)
{
foreach (FieldDefinition field in typeDefinition.Fields)
{
if (field.IsStatic)
continue;
if (isQSBMessageType)
{
if (field.IsStatic)
continue;
if (field.HasCustomAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
continue;
if (field.HasCustomAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
continue;
}
else
{
if (field.IsStatic || field.IsPrivate)
continue;
}
if (field.IsNotSerialized)
continue;

View File

@ -334,7 +334,7 @@ namespace Mirror.Weaver
// try to use Deserialize if this is a message
bool ReadFromDeserialize(TypeDefinition klass, ILProcessor worker)
{
if (!klass.ImplementsInterface<NetworkMessage>())
if (!klass.IsQSBMessageType())
return false;
var toSearch = klass;
@ -360,7 +360,7 @@ namespace Mirror.Weaver
continue;
// todo does this even work?
Log.Error($"! read using {method}", klass);
// Log.Error($"! read using {method}", klass);
// mismatched ldloca/ldloc for struct/class combinations is invalid IL, which causes crash at runtime
var opcode = klass.IsValueType ? OpCodes.Ldloca : OpCodes.Ldloc;
worker.Emit(opcode, 0); // the klass
@ -388,7 +388,7 @@ namespace Mirror.Weaver
if (readFunc != null)
{
worker.Emit(OpCodes.Ldarg_0);
Log.Error($"! read field {field}", variable);
// Log.Error($"! read field {field}", variable);
worker.Emit(OpCodes.Call, readFunc);
}
else

View File

@ -251,7 +251,7 @@ namespace Mirror.Weaver
// try to use Serialize if this is a message
bool WriteFromSerialize(TypeDefinition klass, ILProcessor worker)
{
if (!klass.ImplementsInterface<NetworkMessage>())
if (!klass.IsQSBMessageType())
return false;
var toSearch = klass;
@ -276,7 +276,7 @@ namespace Mirror.Weaver
continue;
// todo does this even work?
Log.Error($"! write using {method}", klass);
// Log.Error($"! write using {method}", klass);
worker.Emit(OpCodes.Ldarg_1); // the klass
worker.Emit(OpCodes.Ldarg_0); // the writer
worker.Emit(OpCodes.Callvirt, method);
@ -307,7 +307,7 @@ namespace Mirror.Weaver
worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Ldarg_1);
worker.Emit(OpCodes.Ldfld, fieldRef);
Log.Error($"! write field {field}", variable);
// Log.Error($"! write field {field}", variable);
worker.Emit(OpCodes.Call, writeFunc);
}