fix(weaver): Warning for multiple write/read functions for the same type (#2453)

* fix(weaver): Warning for multiple write/read functions for the same type

Adding warning message when trying to register a write or read function for a type that is already in dictionary. Previously weaver would be silent about this making it hard to know when an extension method overrides another one.

* test for warnings
This commit is contained in:
James Frowen 2020-11-22 02:42:53 +00:00 committed by GitHub
parent fd9a7b0561
commit 301e791d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 1 deletions

View File

@ -17,6 +17,12 @@ public static void Init()
internal static void Register(TypeReference dataType, MethodReference methodReference)
{
string typeName = dataType.FullName;
if (readFuncs.ContainsKey(typeName))
{
Weaver.Warning($"Registering a Read method for {typeName} when one already exists", methodReference);
}
readFuncs[dataType.FullName] = methodReference;
}

View File

@ -17,7 +17,13 @@ public static void Init()
public static void Register(TypeReference dataType, MethodReference methodReference)
{
writeFuncs[dataType.FullName] = methodReference;
string typeName = dataType.FullName;
if (writeFuncs.ContainsKey(typeName))
{
Weaver.Warning($"Registering a Write method for {typeName} when one already exists", methodReference);
}
writeFuncs[typeName] = methodReference;
}
static void RegisterWriteFunc(TypeReference typeReference, MethodDefinition newWriterFunc)

View File

@ -133,6 +133,7 @@
<Compile Include="WeaverCommandTests~\ErrorForOptionalNetworkConnectionThatIsNotSenderConnection.cs" />
<Compile Include="WeaverGeneralTests~\RecursionCount.cs" />
<Compile Include="WeaverGeneralTests~\TestingScriptableObjectArraySerialization.cs" />
<Compile Include="WeaverGeneratedReaderWriterTests~\GivesWarningWhenRegisteringExistingExtensionMethod.cs" />
<Compile Include="WeaverMessageTests~\AbstractMessageMethods.cs" />
<Compile Include="WeaverMessageTests~\MessageMemberGeneric.cs" />
<Compile Include="WeaverMessageTests~\MessageMemberInterface.cs" />

View File

@ -203,5 +203,16 @@ public void GivesErrorForInvalidListType()
//HasError("Cannot generate reader for List because element MonoBehaviour does not have a reader. Use a supported type or provide a custom reader",
// "System.Collections.Generic.List`1<UnityEngine.MonoBehaviour>");
}
[Test]
public void GivesWarningWhenRegisteringExistingExtensionMethod()
{
const string typeName = "GeneratedReaderWriter.GivesWarningWhenRegisteringExistingExtensionMethod.MyType";
HasNoErrors();
HasWarning($"Registering a Write method for {typeName} when one already exists",
"System.Void GeneratedReaderWriter.GivesWarningWhenRegisteringExistingExtensionMethod.ReadWriteExtension::WriteMyType2(Mirror.NetworkWriter,GeneratedReaderWriter.GivesWarningWhenRegisteringExistingExtensionMethod.MyType)");
HasWarning($"Registering a Read method for {typeName} when one already exists",
"GeneratedReaderWriter.GivesWarningWhenRegisteringExistingExtensionMethod.MyType GeneratedReaderWriter.GivesWarningWhenRegisteringExistingExtensionMethod.ReadWriteExtension::ReadMyType2(Mirror.NetworkReader)");
}
}
}

View File

@ -0,0 +1,32 @@
using Mirror;
namespace GeneratedReaderWriter.GivesWarningWhenRegisteringExistingExtensionMethod
{
public struct MyType
{
public int number;
}
public static class ReadWriteExtension
{
public static void WriteMyType(this NetworkWriter writer, MyType data)
{
writer.WriteInt32(data.number);
}
public static void WriteMyType2(this NetworkWriter writer, MyType data)
{
writer.WriteInt32(data.number);
}
public static MyType ReadMyType(this NetworkReader reader)
{
return new MyType { number = reader.ReadInt32() };
}
public static MyType ReadMyType2(this NetworkReader reader)
{
return new MyType { number = reader.ReadInt32() };
}
}
}