Skip to content

Commit d352dce

Browse files
authored
feat: Serializable Types (RFC#2) (#459)
* initial BitSerializer and Tests implementation * implement tests * fix BitWriter.WriteColor32 bug, implement more tests * implement more array serialization and more serialization tests * ILPP to utilize BitSerializer (arrays still WIP) * implement enum array serialization ILPP * implement INetworkSerializable array serialization ILPP * implement passing RPC params over __ntable * implement whole ServerRpcParams & ClientRpcParams to passthrough RPC ILPP * fix small field naming typo * minor pooling optimization
1 parent 1b3a020 commit d352dce

24 files changed

Lines changed: 4111 additions & 700 deletions

com.unity.multiplayer.mlapi/Editor/CodeGen/CodeGenHelpers.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ internal static class CodeGenHelpers
2828
public static readonly string ServerRpcParams_FullName = typeof(ServerRpcParams).FullName;
2929
public static readonly string ClientRpcParams_FullName = typeof(ClientRpcParams).FullName;
3030
public static readonly string INetworkSerializable_FullName = typeof(INetworkSerializable).FullName;
31-
public static readonly string INetworkSerializable_NetworkRead_Name = nameof(INetworkSerializable.NetworkRead);
32-
public static readonly string INetworkSerializable_NetworkWrite_Name = nameof(INetworkSerializable.NetworkWrite);
31+
public static readonly string INetworkSerializable_NetworkSerialize_Name = nameof(INetworkSerializable.NetworkSerialize);
3332
public static readonly string UnityColor_FullName = typeof(Color).FullName;
33+
public static readonly string UnityColor32_FullName = typeof(Color32).FullName;
3434
public static readonly string UnityVector2_FullName = typeof(Vector2).FullName;
3535
public static readonly string UnityVector3_FullName = typeof(Vector3).FullName;
3636
public static readonly string UnityVector4_FullName = typeof(Vector4).FullName;
@@ -78,6 +78,8 @@ public static bool IsSubclassOf(this TypeDefinition typeDefinition, string Class
7878

7979
public static bool HasInterface(this TypeReference typeReference, string InterfaceTypeFullName)
8080
{
81+
if (typeReference.IsArray) return false;
82+
8183
try
8284
{
8385
var typeDef = typeReference.Resolve();
@@ -91,11 +93,11 @@ public static bool HasInterface(this TypeReference typeReference, string Interfa
9193
return false;
9294
}
9395

94-
public static bool IsSupportedType(this TypeReference typeReference)
96+
public static bool IsSerializable(this TypeReference typeReference)
9597
{
9698
var typeSystem = typeReference.Module.TypeSystem;
9799

98-
// common primitives
100+
// C# primitives
99101
if (typeReference == typeSystem.Boolean) return true;
100102
if (typeReference == typeSystem.Char) return true;
101103
if (typeReference == typeSystem.SByte) return true;
@@ -112,31 +114,30 @@ public static bool IsSupportedType(this TypeReference typeReference)
112114

113115
// Unity primitives
114116
if (typeReference.FullName == UnityColor_FullName) return true;
117+
if (typeReference.FullName == UnityColor32_FullName) return true;
115118
if (typeReference.FullName == UnityVector2_FullName) return true;
116119
if (typeReference.FullName == UnityVector3_FullName) return true;
117120
if (typeReference.FullName == UnityVector4_FullName) return true;
118121
if (typeReference.FullName == UnityQuaternion_FullName) return true;
119122
if (typeReference.FullName == UnityRay_FullName) return true;
120123
if (typeReference.FullName == UnityRay2D_FullName) return true;
121124

122-
// INetworkSerializable
123-
if (typeReference.HasInterface(INetworkSerializable_FullName)) return true;
124-
125125
// Enum
126126
if (typeReference.GetEnumAsInt() != null) return true;
127127

128-
// todo: [RFC] Serializable Types
129-
// StaticArray[]
130-
// IEnumerable<T>
131-
// IEnumerable<KeyValuePair<K, V>>
132-
// IEnumerable<Tuple<T1, T2, T3...T7>>
133-
// IEnumerable<Tuple<T1, T2...TRest>>
128+
// INetworkSerializable
129+
if (typeReference.HasInterface(INetworkSerializable_FullName)) return true;
130+
131+
// Static array
132+
if (typeReference.IsArray) return typeReference.GetElementType().IsSerializable();
134133

135134
return false;
136135
}
137136

138137
public static TypeReference GetEnumAsInt(this TypeReference typeReference)
139138
{
139+
if (typeReference.IsArray) return null;
140+
140141
try
141142
{
142143
var typeDef = typeReference.Resolve();

com.unity.multiplayer.mlapi/Editor/CodeGen/ILPostProcessorProgram.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ void WriteAssembly(InMemoryAssembly inMemoryAssembly, string outputPath, string
197197
switch (message.DiagnosticType)
198198
{
199199
case DiagnosticType.Error:
200-
Debug.LogError($"ILPostProcessor Error - {message.MessageData} {message.File} {message.Line} {message.Column}");
200+
Debug.LogError($"ILPostProcessor Error - {message.MessageData} {message.File}:{message.Line}");
201201
break;
202202
case DiagnosticType.Warning:
203-
Debug.LogWarning($"ILPostProcessor Warning - {message.MessageData} {message.File} {message.Line} {message.Column}");
203+
Debug.LogWarning($"ILPostProcessor Warning - {message.MessageData} {message.File}:{message.Line}");
204204
break;
205205
}
206206
}

0 commit comments

Comments
 (0)