Skip to content

Commit

Permalink
Stabilized and optimized library
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Mar 8, 2018
1 parent 5426f4a commit 34031be
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
18 changes: 9 additions & 9 deletions MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ internal void FlushToClient(int clientId)
using (BinaryWriter writer = new BinaryWriter(stream))
{
//Write all indexes
writer.Write(dirtyFields.Length);
writer.Write((byte)dirtyFields.Length);
writer.Write(networkId); //NetId
writer.Write(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < dirtyFields.Length; i++)
{
writer.Write(networkId);
writer.Write(networkedObject.GetOrderIndex(this));
writer.Write(i); //Index
writer.Write(i); //FieldIndex
switch (syncedFieldTypes[i])
{
case FieldType.Bool:
Expand Down Expand Up @@ -332,8 +332,8 @@ internal void FlushToClient(int clientId)
break;
}
}
NetworkingManager.singleton.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED", stream.ToArray());
}
NetworkingManager.singleton.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED", stream.ToArray());
}
}

Expand All @@ -353,14 +353,14 @@ internal void SyncVarUpdate()
{
//Write all indexes
writer.Write(dirtyCount);
writer.Write(networkId); //NetId
writer.Write(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < dirtyFields.Length; i++)
{
//Writes all the indexes of the dirty syncvars.
if (dirtyFields[i] == true)
{
writer.Write(networkId);
writer.Write(networkedObject.GetOrderIndex(this));
writer.Write(i); //Index
writer.Write(i); //FieldIndex
switch (syncedFieldTypes[i])
{
case FieldType.Bool:
Expand Down Expand Up @@ -425,8 +425,8 @@ internal void SyncVarUpdate()
dirtyFields[i] = false;
}
}
NetworkingManager.singleton.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED", stream.ToArray(), ownerClientId);
}
NetworkingManager.singleton.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED", stream.ToArray());
}
lastSyncTime = Time.time;
}
Expand Down
3 changes: 1 addition & 2 deletions MLAPI/MonoBehaviours/Core/NetworkedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ internal void InvokeBehaviourNetworkSpawn()
if(!childNetworkedBehaviours[i].networkedStartInvoked)
{
childNetworkedBehaviours[i].NetworkStart();
if (NetworkingManager.singleton.isServer)
childNetworkedBehaviours[i].SyncVarInit();
childNetworkedBehaviours[i].SyncVarInit();
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions MLAPI/MonoBehaviours/Core/NetworkingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ private void HandleIncomingData(int clientId, byte[] data, int channelId)
//Custom message, invoke all message handlers
if(targeted)
{
if(!MessageManager.targetedMessages.ContainsKey(messageType))
{
Debug.LogWarning("MLAPI: No handlers for the given messagetype");
return;
}
else if(!MessageManager.targetedMessages[messageType].ContainsKey(targetNetworkId))
{
Debug.LogWarning("MLAPI: No handlers for the given networkId");
return;
}
List<int> handlerIds = MessageManager.targetedMessages[messageType][targetNetworkId];
for (int i = 0; i < handlerIds.Count; i++)
{
Expand Down Expand Up @@ -688,20 +698,20 @@ private void HandleIncomingData(int clientId, byte[] data, int channelId)
}
}
break;
case 9:
case 9: //Syncvar
if (isClient)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
byte dirtyCount = messageReader.ReadByte();
if(dirtyCount > 0)
uint netId = messageReader.ReadUInt32();
ushort orderIndex = messageReader.ReadUInt16();
if (dirtyCount > 0)
{
for (int i = 0; i < dirtyCount; i++)
{
uint netId = messageReader.ReadUInt32(); //NetId the syncvar is from
ushort orderIndex = messageReader.ReadUInt16();
byte fieldIndex = messageReader.ReadByte();
if(!SpawnManager.spawnedObjects.ContainsKey(netId))
{
Expand Down
2 changes: 1 addition & 1 deletion MLAPI/MonoBehaviours/Prototyping/NetworkedTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void Update()
writer.Write(transform.rotation.eulerAngles.z);
}
if (isServer)
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromClient", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
else
SendToServerTarget("MLAPI_OnRecieveTransformFromClient", "MLAPI_POSITION_UPDATE", writeStream.GetBuffer());
}
Expand Down

0 comments on commit 34031be

Please sign in to comment.