Error with message overloads (#2168)

* weaver error with message overloads

* finding method with 1 arg
This commit is contained in:
James Frowen 2020-08-22 20:07:33 +01:00 committed by GitHub
parent d5d434cf13
commit bd7c93674e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 2 deletions

View File

@ -203,6 +203,14 @@ public static MethodDefinition GetMethod(this TypeDefinition td, string methodNa
return td.Methods.FirstOrDefault(method => method.Name == methodName);
}
public static MethodDefinition GetMethodWith1Arg(this TypeDefinition tr, string methodName, TypeReference argType)
{
return tr.GetMethods(methodName).Where(m =>
m.Parameters.Count == 1
&& m.Parameters[0].ParameterType.FullName == argType.FullName
).FirstOrDefault();
}
public static List<MethodDefinition> GetMethods(this TypeDefinition td, string methodName)
{
// Linq allocations don't matter in weaver

View File

@ -32,7 +32,7 @@ public static void Process(TypeDefinition td)
static void GenerateSerialization(TypeDefinition td)
{
Weaver.DLog(td, " GenerateSerialization");
MethodDefinition existingMethod = td.GetMethod("Serialize");
MethodDefinition existingMethod = td.GetMethodWith1Arg("Serialize", WeaverTypes.NetworkWriterType);
// do nothing if method exists and is abstract or not empty
if (existingMethod != null && (existingMethod.IsAbstract || !existingMethod.Body.IsEmptyDefault()))
{
@ -129,7 +129,7 @@ static void CallBase(TypeDefinition td, ILProcessor worker, string name)
static void GenerateDeSerialization(TypeDefinition td)
{
Weaver.DLog(td, " GenerateDeserialization");
MethodDefinition existingMethod = td.GetMethod("Deserialize");
MethodDefinition existingMethod = td.GetMethodWith1Arg("Deserialize", WeaverTypes.NetworkReaderType);
// do nothing if method exists and is abstract or not empty
if (existingMethod != null && (existingMethod.IsAbstract || !existingMethod.Body.IsEmptyDefault()))

View File

@ -0,0 +1,66 @@
using NUnit.Framework;
namespace Mirror.Tests.MessageTests
{
class NoArgMethodMessage : IMessageBase
{
public int someValue;
// Weaver should ignore these methods because they have no args
public void Serialize() { /* method with no arg */ }
public void Deserialize() { /* method with no arg */ }
// Mirror will fill out these empty methods
public void Serialize(NetworkWriter writer) { }
public void Deserialize(NetworkReader reader) { }
}
class TwoArgMethodMessage : IMessageBase
{
public int someValue;
// Weaver should ignore these methods because they have two args
public void Serialize(NetworkWriter writer, int AnotherValue) { /* method with 2 args */ }
public void Deserialize(NetworkReader reader, int AnotherValue) { /* method with 2 args */ }
// Mirror will fill out these empty methods
public void Serialize(NetworkWriter writer) { }
public void Deserialize(NetworkReader reader) { }
}
public class OverloadMethodTest
{
[Test]
public void MethodsWithNoArgs()
{
const int value = 10;
NoArgMethodMessage intMessage = new NoArgMethodMessage
{
someValue = value
};
byte[] data = MessagePacker.Pack(intMessage);
NoArgMethodMessage unpacked = MessagePacker.Unpack<NoArgMethodMessage>(data);
Assert.That(unpacked.someValue, Is.EqualTo(value));
}
[Test]
public void MethodsWithTwoArgs()
{
const int value = 10;
TwoArgMethodMessage intMessage = new TwoArgMethodMessage
{
someValue = value
};
byte[] data = MessagePacker.Pack(intMessage);
TwoArgMethodMessage unpacked = MessagePacker.Unpack<TwoArgMethodMessage>(data);
Assert.That(unpacked.someValue, Is.EqualTo(value));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bbe7affc888ce1041a8d6752b0f3f94b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: