Skip to content

Commit e7b213c

Browse files
authored
Merge pull request #124 from jaglitegrann/feat/support-gameobject-networkedobject-networkedbehaviour-types-in-writter-reader
Support more types in BitWritter/BitReader
2 parents f0b3312 + ceff72e commit e7b213c

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 45 additions & 0 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
@@ -129,6 +131,49 @@ public object ReadObjectPacked(Type type)
129131
return ReadCharPacked();
130132
if (type.IsEnum)
131133
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+
}
132177
if (typeof(IBitWritable).IsAssignableFrom(type))
133178
{
134179
object instance = Activator.CreateInstance(type);

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
}

0 commit comments

Comments
 (0)