Skip to content

Commit

Permalink
fix info in InvalidTypeExceptions. Fix for list serializing for fastc…
Browse files Browse the repository at this point in the history
…allstatic (fix #390)
  • Loading branch information
RevenantX committed Aug 8, 2020
1 parent bbdbb5a commit f01cca3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
7 changes: 6 additions & 1 deletion LiteNetLib.Tests/NetSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public void Init()
TestObj = new SampleNetSerializable {Value = 5},
TestArray = new [] { new SampleNetSerializable { Value = 6 }, new SampleNetSerializable { Value = 15 } },
SampleClassArray = new[] { new SampleClass { Value = 6 }, new SampleClass { Value = 15 } },
SampleClassList = new List<SampleClass> { new SampleClass { Value = 1 }, new SampleClass { Value = 5 }}
SampleClassList = new List<SampleClass> { new SampleClass { Value = 1 }, new SampleClass { Value = 5 }},
VectorList = new List<SomeVector2> { new SomeVector2(-1,-2), new SomeVector2(700, 800) }
};

_packetProcessor = new NetPacketProcessor();
Expand Down Expand Up @@ -123,6 +124,7 @@ private class SamplePacket
public SampleNetSerializable[] TestArray { get; set; }
public SampleClass[] SampleClassArray { get; set; }
public List<SampleClass> SampleClassList { get; set; }
public List<SomeVector2> VectorList { get; set; }
}

private static bool AreSame(string s1, string s2)
Expand Down Expand Up @@ -164,10 +166,12 @@ public void CustomPackageTest()
Assert.AreEqual(_samplePacket.SomeByteArray, readPackage.SomeByteArray);
Assert.AreEqual(_samplePacket.SampleClassArray, readPackage.SampleClassArray);
CollectionAssert.AreEqual(_samplePacket.SampleClassList, readPackage.SampleClassList);
CollectionAssert.AreEqual(_samplePacket.VectorList, readPackage.VectorList);

//remove test
_samplePacket.SampleClassList.RemoveAt(0);
_samplePacket.SampleClassArray = new []{new SampleClass {Value = 1}};
_samplePacket.VectorList.RemoveAt(0);

writer.Reset();
_packetProcessor.Write(writer, _samplePacket);
Expand All @@ -181,6 +185,7 @@ public void CustomPackageTest()
_samplePacket.SampleClassList.Add(new SampleClass { Value = 152 });
_samplePacket.SampleClassList.Add(new SampleClass { Value = 154 });
_samplePacket.SampleClassArray = new[] { new SampleClass { Value = 1 }, new SampleClass { Value = 2 }, new SampleClass { Value = 3 } };
_samplePacket.VectorList.Add(new SomeVector2(500,600));

writer.Reset();
_packetProcessor.Write(writer, _samplePacket);
Expand Down
44 changes: 40 additions & 4 deletions LiteNetLib/Utils/NetSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ private abstract class FastCall<T>
public virtual void Init(MethodInfo getMethod, MethodInfo setMethod, CallType type) { Type = type; }
public abstract void Read(T inf, NetDataReader r);
public abstract void Write(T inf, NetDataWriter w);
public virtual void ReadArray(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: " + typeof(T) + "[]"); }
public virtual void WriteArray(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: " + typeof(T) + "[]"); }
public virtual void ReadList(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<" + typeof(T) + ">"); }
public virtual void WriteList(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<" + typeof(T) + ">"); }
public abstract void ReadArray(T inf, NetDataReader r);
public abstract void WriteArray(T inf, NetDataWriter w);
public abstract void ReadList(T inf, NetDataReader r);
public abstract void WriteList(T inf, NetDataWriter w);
}

private abstract class FastCallSpecific<TClass, TProperty> : FastCall<TClass>
Expand All @@ -45,6 +45,11 @@ private abstract class FastCallSpecific<TClass, TProperty> : FastCall<TClass>
protected Func<TClass, List<TProperty>> GetterList;
protected Action<TClass, List<TProperty>> SetterList;

public override void ReadArray(TClass inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
public override void WriteArray(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
public override void ReadList(TClass inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }
public override void WriteList(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }

protected TProperty[] ReadArrayHelper(TClass inf, NetDataReader r)
{
ushort count = r.GetUShort();
Expand Down Expand Up @@ -155,6 +160,33 @@ public FastCallStatic(Action<NetDataWriter, TProperty> write, Func<NetDataReader
public override void Read(TClass inf, NetDataReader r) { Setter(inf, _reader(r)); }
public override void Write(TClass inf, NetDataWriter w) { _writer(w, Getter(inf)); }

public override void ReadList(TClass inf, NetDataReader r)
{
int len;
var list = ReadListHelper(inf, r, out len);
int listCount = list.Count;
if (len > listCount)
{
for (int i = 0; i < listCount; i++)
list[i] = _reader(r);
for (int i = listCount; i < len; i++)
list.Add(_reader(r));
return;
}
if (len < listCount)
list.RemoveRange(len, listCount - len);
for (int i = 0; i < len; i++)
list[i] = _reader(r);
}

public override void WriteList(TClass inf, NetDataWriter w)
{
int len;
var list = WriteListHelper(inf, w, out len);
for (int i = 0; i < len; i++)
_writer(w, list[i]);
}

public override void ReadArray(TClass inf, NetDataReader r)
{
var arr = ReadArrayHelper(inf, r);
Expand Down Expand Up @@ -427,6 +459,10 @@ public EnumByteSerializer(PropertyInfo property, Type propertyType)
}
public override void Read(T inf, NetDataReader r) { Property.SetValue(inf, Enum.ToObject(PropertyType, r.GetByte()), null); }
public override void Write(T inf, NetDataWriter w) { w.Put((byte)Property.GetValue(inf, null)); }
public override void ReadArray(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
public override void WriteArray(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
public override void ReadList(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
public override void WriteList(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
}

private class EnumIntSerializer<T> : EnumByteSerializer<T>
Expand Down
Binary file modified LiteNetLibSampleUnity/Assets/LiteNetLib.dll
Binary file not shown.

0 comments on commit f01cca3

Please sign in to comment.