mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
Error with message overloads (#2168)
* weaver error with message overloads * finding method with 1 arg
This commit is contained in:
parent
d5d434cf13
commit
bd7c93674e
@ -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
|
||||
|
@ -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()))
|
||||
|
66
Assets/Mirror/Tests/Editor/OverloadMethodTest.cs
Normal file
66
Assets/Mirror/Tests/Editor/OverloadMethodTest.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Mirror/Tests/Editor/OverloadMethodTest.cs.meta
Normal file
11
Assets/Mirror/Tests/Editor/OverloadMethodTest.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bbe7affc888ce1041a8d6752b0f3f94b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user