Skip to content

Commit 2f83820

Browse files
committed
Merge branch 'master' into certificate-hail
2 parents a46a1e7 + f4dd766 commit 2f83820

File tree

4 files changed

+125
-15
lines changed

4 files changed

+125
-15
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,29 @@ private ulong HashMethodName(string name)
413413

414414
return 0;
415415
}
416-
416+
417+
private MethodInfo[] GetNetworkedBehaviorChildClassesMethods(Type type, List<MethodInfo> list = null)
418+
{
419+
if (list == null)
420+
{
421+
list = new List<MethodInfo>();
422+
list.AddRange(type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance));
423+
}
424+
else
425+
{
426+
list.AddRange(type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance));
427+
}
428+
429+
if (type.BaseType != null && type.BaseType != typeof(NetworkedBehaviour))
430+
{
431+
return GetNetworkedBehaviorChildClassesMethods(type.BaseType, list);
432+
}
433+
else
434+
{
435+
return list.ToArray();
436+
}
437+
}
438+
417439
private void CacheAttributes()
418440
{
419441
Type type = GetType();
@@ -425,7 +447,7 @@ private void CacheAttributes()
425447
if (Methods.ContainsKey(type)) methods = Methods[type];
426448
else
427449
{
428-
methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
450+
methods = GetNetworkedBehaviorChildClassesMethods(type);
429451
Methods.Add(type, methods);
430452
}
431453

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System;
77
using System.IO;
88
using System.Text;
9+
using MLAPI.Components;
910
using MLAPI.Internal;
11+
using MLAPI.Logging;
1012
using UnityEngine;
1113

1214
namespace MLAPI.Serialization
@@ -108,7 +110,7 @@ public object ReadObjectPacked(Type type)
108110
if (type == typeof(double))
109111
return ReadDoublePacked();
110112
if (type == typeof(string))
111-
return ReadStringPacked();
113+
return ReadStringPacked().ToString();
112114
if (type == typeof(bool))
113115
return ReadBool();
114116
if (type == typeof(Vector2))
@@ -127,14 +129,57 @@ public object ReadObjectPacked(Type type)
127129
return ReadRotation(3);
128130
if (type == typeof(char))
129131
return ReadCharPacked();
130-
if (type == typeof(IBitWritable))
132+
if (type.IsEnum)
133+
return ReadInt32Packed();
134+
if (type == typeof(GameObject))
135+
{
136+
uint networkId = ReadUInt32Packed();
137+
if (SpawnManager.SpawnedObjects.ContainsKey(networkId))
138+
{
139+
return SpawnManager.SpawnedObjects[networkId].gameObject;
140+
}
141+
else
142+
{
143+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal)
144+
LogHelper.LogWarning("BitReader canot find the GameObject sent in the SpawnedObjects list, it may have been destroyed. NetworkId: " + networkId.ToString());
145+
return null;
146+
}
147+
}
148+
if (type == typeof(NetworkedObject))
149+
{
150+
uint networkId = ReadUInt32Packed();
151+
if (SpawnManager.SpawnedObjects.ContainsKey(networkId))
152+
{
153+
return SpawnManager.SpawnedObjects[networkId];
154+
}
155+
else
156+
{
157+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal)
158+
LogHelper.LogWarning("BitReader canot find the NetworkedObject sent in the SpawnedObjects list, it may have been destroyed. NetworkId: " + networkId.ToString());
159+
return null;
160+
}
161+
}
162+
if (type == typeof(NetworkedBehaviour))
163+
{
164+
uint networkId = ReadUInt32Packed();
165+
ushort behaviourId = ReadUInt16Packed();
166+
if (SpawnManager.SpawnedObjects.ContainsKey(networkId))
167+
{
168+
return SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId);
169+
}
170+
else
171+
{
172+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal)
173+
LogHelper.LogWarning("BitReader canot find the NetworkedBehaviour sent in the SpawnedObjects list, it may have been destroyed. NetworkId: " + networkId.ToString());
174+
return null;
175+
}
176+
}
177+
if (typeof(IBitWritable).IsAssignableFrom(type))
131178
{
132179
object instance = Activator.CreateInstance(type);
133180
((IBitWritable)instance).Read(this.source);
134181
return instance;
135182
}
136-
if (type.IsEnum)
137-
return ReadInt32Packed();
138183

139184
throw new ArgumentException("BitReader cannot read type " + type.Name);
140185
}

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,53 @@ public void WriteObjectPacked(object value)
145145
WriteCharPacked((char)value);
146146
return;
147147
}
148-
else if(value is IBitWritable)
149-
{
150-
((IBitWritable)value).Write(this.sink);
151-
return;
152-
}
153148
else if (value.GetType().IsEnum)
154149
{
155150
WriteInt32Packed((int)value);
156151
return;
157152
}
153+
else if (value is GameObject)
154+
{
155+
if(value == null)
156+
{
157+
throw new ArgumentException("BitWriter cannot write GameObject types with a null value");
158+
}
159+
NetworkedObject networkedObject = ((GameObject)value).GetComponent<NetworkedObject>();
160+
if(networkedObject == null)
161+
{
162+
throw new ArgumentException("BitWriter cannot write GameObject types that does not has a NetworkedObject component attached. GameObject: " + ((GameObject)value).name);
163+
}
164+
else
165+
{
166+
WriteUInt32Packed(networkedObject.NetworkId);
167+
}
168+
return;
169+
}
170+
else if (value is NetworkedObject)
171+
{
172+
if (value == null)
173+
{
174+
throw new ArgumentException("BitWriter cannot write NetworkedObject types with a null value");
175+
}
176+
WriteUInt32Packed(((NetworkedObject)value).NetworkId);
177+
return;
178+
}
179+
else if (value is NetworkedBehaviour)
180+
{
181+
if(value == null)
182+
{
183+
throw new ArgumentException("BitWriter cannot write NetworkedBehaviour types with a null value");
184+
}
185+
WriteUInt32Packed(((NetworkedBehaviour)value).networkId);
186+
WriteUInt16Packed(((NetworkedBehaviour)value).GetBehaviourId());
187+
return;
188+
}
189+
else if(value is IBitWritable)
190+
{
191+
((IBitWritable)value).Write(this.sink);
192+
return;
193+
}
194+
158195

159196
throw new ArgumentException("BitWriter cannot write type " + value.GetType().Name);
160197
}

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,11 @@ internal static void HandleServerRPC(uint clientId, Stream stream, int channelId
487487
ushort behaviourId = reader.ReadUInt16Packed();
488488
ulong hash = reader.ReadUInt64Packed();
489489

490-
NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId);
491-
behaviour.OnRemoteServerRPC(hash, clientId, stream);
490+
if (SpawnManager.SpawnedObjects.ContainsKey(networkId))
491+
{
492+
NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId);
493+
behaviour.OnRemoteServerRPC(hash, clientId, stream);
494+
}
492495
}
493496
}
494497

@@ -500,8 +503,11 @@ internal static void HandleClientRPC(uint clientId, Stream stream, int channelId
500503
ushort behaviourId = reader.ReadUInt16Packed();
501504
ulong hash = reader.ReadUInt64Packed();
502505

503-
NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId);
504-
behaviour.OnRemoteClientRPC(hash, clientId, stream);
506+
if (SpawnManager.SpawnedObjects.ContainsKey(networkId))
507+
{
508+
NetworkedBehaviour behaviour = SpawnManager.SpawnedObjects[networkId].GetBehaviourAtOrderIndex(behaviourId);
509+
behaviour.OnRemoteClientRPC(hash, clientId, stream);
510+
}
505511
}
506512
}
507513

0 commit comments

Comments
 (0)