Skip to content

Commit 7158a17

Browse files
committed
feat: improve serializing, try to make it faster with source generate and refection caching
1 parent 1dc3cee commit 7158a17

25 files changed

Lines changed: 1386 additions & 374 deletions

Scripts/Extensions/NetDataReaderExtension.cs

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using LiteNetLibManager.Serialization;
2+
using System;
23
using System.Collections.Generic;
34
using UnityEngine;
45

@@ -13,73 +14,17 @@ public static TType GetValue<TType>(this NetDataReader reader)
1314

1415
public static object GetValue(this NetDataReader reader, Type type)
1516
{
16-
#region Generic Values
1717
if (type.IsEnum)
1818
type = type.GetEnumUnderlyingType();
1919

20-
if (type == typeof(bool))
21-
return reader.GetBool();
22-
23-
if (type == typeof(byte))
24-
return reader.GetByte();
25-
26-
if (type == typeof(char))
27-
return reader.GetChar();
28-
29-
if (type == typeof(double))
30-
return reader.GetDouble();
31-
32-
if (type == typeof(float))
33-
return reader.GetFloat();
34-
35-
if (type == typeof(int))
36-
return reader.GetPackedInt();
37-
38-
if (type == typeof(long))
39-
return reader.GetPackedLong();
40-
41-
if (type == typeof(sbyte))
42-
return reader.GetSByte();
43-
44-
if (type == typeof(short))
45-
return reader.GetPackedShort();
46-
47-
if (type == typeof(string))
48-
return reader.GetString();
49-
50-
if (type == typeof(uint))
51-
return reader.GetPackedUInt();
52-
53-
if (type == typeof(ulong))
54-
return reader.GetPackedULong();
55-
56-
if (type == typeof(ushort))
57-
return reader.GetPackedUShort();
58-
#endregion
59-
60-
#region Unity Values
61-
if (type == typeof(Color))
62-
return reader.GetColor();
63-
64-
if (type == typeof(Quaternion))
65-
return reader.GetQuaternion();
66-
67-
if (type == typeof(Vector2))
68-
return reader.GetVector2();
69-
70-
if (type == typeof(Vector2Int))
71-
return reader.GetVector2Int();
72-
73-
if (type == typeof(Vector3))
74-
return reader.GetVector3();
75-
76-
if (type == typeof(Vector3Int))
77-
return reader.GetVector3Int();
78-
79-
if (type == typeof(Vector4))
80-
return reader.GetVector4();
81-
#endregion
20+
if (ReaderRegistry.TryGetReader(type, out Func<NetDataReader, object> readerFunc))
21+
{
22+
return readerFunc(reader);
23+
}
8224

25+
#if UNITY_EDITOR || DEVELOPMENT_BUILD
26+
Debug.LogWarning($"No reader registered for type: {type.FullName}");
27+
#endif
8328
if (typeof(INetSerializable).IsAssignableFrom(type))
8429
{
8530
object instance = Activator.CreateInstance(type);

Scripts/Extensions/NetDataWriterExtension.cs

Lines changed: 7 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using LiteNetLibManager.Serialization;
2+
using System;
23
using System.Collections.Generic;
34
using UnityEngine;
45

@@ -13,133 +14,18 @@ public static void PutValue<TType>(this NetDataWriter writer, TType value)
1314

1415
public static void PutValue(this NetDataWriter writer, Type type, object value)
1516
{
16-
#region Generic Values
1717
if (type.IsEnum)
1818
type = type.GetEnumUnderlyingType();
1919

20-
if (type == typeof(bool))
20+
if (WriterRegistry.TryGetWriter(type, out Action<NetDataWriter, object> writeFunc))
2121
{
22-
writer.Put((bool)value);
22+
writeFunc(writer, value);
2323
return;
2424
}
2525

26-
if (type == typeof(byte))
27-
{
28-
writer.Put((byte)value);
29-
return;
30-
}
31-
32-
if (type == typeof(char))
33-
{
34-
writer.Put((char)value);
35-
return;
36-
}
37-
38-
if (type == typeof(double))
39-
{
40-
writer.Put((double)value);
41-
return;
42-
}
43-
44-
if (type == typeof(float))
45-
{
46-
writer.Put((float)value);
47-
return;
48-
}
49-
50-
if (type == typeof(int))
51-
{
52-
writer.PutPackedInt((int)value);
53-
return;
54-
}
55-
56-
if (type == typeof(long))
57-
{
58-
writer.PutPackedLong((long)value);
59-
return;
60-
}
61-
62-
if (type == typeof(sbyte))
63-
{
64-
writer.Put((sbyte)value);
65-
return;
66-
}
67-
68-
if (type == typeof(short))
69-
{
70-
writer.PutPackedShort((short)value);
71-
return;
72-
}
73-
74-
if (type == typeof(string))
75-
{
76-
writer.Put((string)value);
77-
return;
78-
}
79-
80-
if (type == typeof(uint))
81-
{
82-
writer.PutPackedUInt((uint)value);
83-
return;
84-
}
85-
86-
if (type == typeof(ulong))
87-
{
88-
writer.PutPackedULong((ulong)value);
89-
return;
90-
}
91-
92-
if (type == typeof(ushort))
93-
{
94-
writer.PutPackedUShort((ushort)value);
95-
return;
96-
}
97-
#endregion
98-
99-
#region Unity Values
100-
if (type == typeof(Color))
101-
{
102-
writer.PutColor((Color)value);
103-
return;
104-
}
105-
106-
if (type == typeof(Quaternion))
107-
{
108-
writer.PutQuaternion((Quaternion)value);
109-
return;
110-
}
111-
112-
if (type == typeof(Vector2))
113-
{
114-
writer.PutVector2((Vector2)value);
115-
return;
116-
}
117-
118-
if (type == typeof(Vector2Int))
119-
{
120-
writer.PutVector2Int((Vector2Int)value);
121-
return;
122-
}
123-
124-
if (type == typeof(Vector3))
125-
{
126-
writer.PutVector3((Vector3)value);
127-
return;
128-
}
129-
130-
if (type == typeof(Vector3Int))
131-
{
132-
writer.PutVector3Int((Vector3Int)value);
133-
return;
134-
}
135-
136-
if (type == typeof(Vector4))
137-
{
138-
writer.PutVector4((Vector4)value);
139-
return;
140-
}
141-
#endregion
142-
26+
#if UNITY_EDITOR || DEVELOPMENT_BUILD
27+
Debug.LogWarning($"No writer registered for type: {type.FullName}");
28+
#endif
14329
if (typeof(INetSerializable).IsAssignableFrom(type))
14430
{
14531
(value as INetSerializable).Serialize(writer);

0 commit comments

Comments
 (0)