Skip to content

Commit f01cca3

Browse files
committed
fix info in InvalidTypeExceptions. Fix for list serializing for fastcallstatic (fix RevenantX#390)
1 parent bbdbb5a commit f01cca3

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

LiteNetLib.Tests/NetSerializerTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public void Init()
2424
TestObj = new SampleNetSerializable {Value = 5},
2525
TestArray = new [] { new SampleNetSerializable { Value = 6 }, new SampleNetSerializable { Value = 15 } },
2626
SampleClassArray = new[] { new SampleClass { Value = 6 }, new SampleClass { Value = 15 } },
27-
SampleClassList = new List<SampleClass> { new SampleClass { Value = 1 }, new SampleClass { Value = 5 }}
27+
SampleClassList = new List<SampleClass> { new SampleClass { Value = 1 }, new SampleClass { Value = 5 }},
28+
VectorList = new List<SomeVector2> { new SomeVector2(-1,-2), new SomeVector2(700, 800) }
2829
};
2930

3031
_packetProcessor = new NetPacketProcessor();
@@ -123,6 +124,7 @@ private class SamplePacket
123124
public SampleNetSerializable[] TestArray { get; set; }
124125
public SampleClass[] SampleClassArray { get; set; }
125126
public List<SampleClass> SampleClassList { get; set; }
127+
public List<SomeVector2> VectorList { get; set; }
126128
}
127129

128130
private static bool AreSame(string s1, string s2)
@@ -164,10 +166,12 @@ public void CustomPackageTest()
164166
Assert.AreEqual(_samplePacket.SomeByteArray, readPackage.SomeByteArray);
165167
Assert.AreEqual(_samplePacket.SampleClassArray, readPackage.SampleClassArray);
166168
CollectionAssert.AreEqual(_samplePacket.SampleClassList, readPackage.SampleClassList);
169+
CollectionAssert.AreEqual(_samplePacket.VectorList, readPackage.VectorList);
167170

168171
//remove test
169172
_samplePacket.SampleClassList.RemoveAt(0);
170173
_samplePacket.SampleClassArray = new []{new SampleClass {Value = 1}};
174+
_samplePacket.VectorList.RemoveAt(0);
171175

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

185190
writer.Reset();
186191
_packetProcessor.Write(writer, _samplePacket);

LiteNetLib/Utils/NetSerializer.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ private abstract class FastCall<T>
3030
public virtual void Init(MethodInfo getMethod, MethodInfo setMethod, CallType type) { Type = type; }
3131
public abstract void Read(T inf, NetDataReader r);
3232
public abstract void Write(T inf, NetDataWriter w);
33-
public virtual void ReadArray(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: " + typeof(T) + "[]"); }
34-
public virtual void WriteArray(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: " + typeof(T) + "[]"); }
35-
public virtual void ReadList(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<" + typeof(T) + ">"); }
36-
public virtual void WriteList(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<" + typeof(T) + ">"); }
33+
public abstract void ReadArray(T inf, NetDataReader r);
34+
public abstract void WriteArray(T inf, NetDataWriter w);
35+
public abstract void ReadList(T inf, NetDataReader r);
36+
public abstract void WriteList(T inf, NetDataWriter w);
3737
}
3838

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

48+
public override void ReadArray(TClass inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
49+
public override void WriteArray(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: " + typeof(TProperty) + "[]"); }
50+
public override void ReadList(TClass inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }
51+
public override void WriteList(TClass inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<" + typeof(TProperty) + ">"); }
52+
4853
protected TProperty[] ReadArrayHelper(TClass inf, NetDataReader r)
4954
{
5055
ushort count = r.GetUShort();
@@ -155,6 +160,33 @@ public FastCallStatic(Action<NetDataWriter, TProperty> write, Func<NetDataReader
155160
public override void Read(TClass inf, NetDataReader r) { Setter(inf, _reader(r)); }
156161
public override void Write(TClass inf, NetDataWriter w) { _writer(w, Getter(inf)); }
157162

163+
public override void ReadList(TClass inf, NetDataReader r)
164+
{
165+
int len;
166+
var list = ReadListHelper(inf, r, out len);
167+
int listCount = list.Count;
168+
if (len > listCount)
169+
{
170+
for (int i = 0; i < listCount; i++)
171+
list[i] = _reader(r);
172+
for (int i = listCount; i < len; i++)
173+
list.Add(_reader(r));
174+
return;
175+
}
176+
if (len < listCount)
177+
list.RemoveRange(len, listCount - len);
178+
for (int i = 0; i < len; i++)
179+
list[i] = _reader(r);
180+
}
181+
182+
public override void WriteList(TClass inf, NetDataWriter w)
183+
{
184+
int len;
185+
var list = WriteListHelper(inf, w, out len);
186+
for (int i = 0; i < len; i++)
187+
_writer(w, list[i]);
188+
}
189+
158190
public override void ReadArray(TClass inf, NetDataReader r)
159191
{
160192
var arr = ReadArrayHelper(inf, r);
@@ -427,6 +459,10 @@ public EnumByteSerializer(PropertyInfo property, Type propertyType)
427459
}
428460
public override void Read(T inf, NetDataReader r) { Property.SetValue(inf, Enum.ToObject(PropertyType, r.GetByte()), null); }
429461
public override void Write(T inf, NetDataWriter w) { w.Put((byte)Property.GetValue(inf, null)); }
462+
public override void ReadArray(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
463+
public override void WriteArray(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: Enum[]"); }
464+
public override void ReadList(T inf, NetDataReader r) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
465+
public override void WriteList(T inf, NetDataWriter w) { throw new InvalidTypeException("Unsupported type: List<Enum>"); }
430466
}
431467

432468
private class EnumIntSerializer<T> : EnumByteSerializer<T>
512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)