Skip to content

Commit c2b5e6e

Browse files
committed
Fixed SyncedVar issues
1 parent ad92afe commit c2b5e6e

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

MLAPI/Data/FieldType.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
using MLAPI.NetworkingManagerComponents.Binary;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Reflection;
53
using UnityEngine;
64

75
namespace MLAPI.Data
86
{
97
internal static class FieldTypeHelper
108
{
9+
internal static bool ObjectEqual(object o1, object o2)
10+
{
11+
if (o1.GetType() != o2.GetType())
12+
return false;
13+
if (o1.GetType().IsArray != o2.GetType().IsArray)
14+
return false;
15+
if (o1.GetType().IsArray && o1.GetType().GetElementType() != o2.GetType().GetElementType())
16+
return false;
17+
18+
if (o1.GetType().IsArray)
19+
{
20+
Array ar1 = (Array)o1;
21+
Array ar2 = (Array)o2;
22+
if (ar1.Length != ar2.Length)
23+
return false;
24+
25+
int i = 0;
26+
foreach (object item in ar1)
27+
{
28+
if (item != ar2.GetValue(i))
29+
return false;
30+
i++;
31+
}
32+
return true;
33+
}
34+
return o1.Equals(o2);
35+
}
36+
1137
internal static void WriteFieldType(BitWriter writer, object value)
1238
{
1339
Type type = value.GetType();
1440
if (type.IsArray)
1541
{
16-
ushort arrayLength = (ushort)((Array)value).Length;
42+
Array array = (Array)value;
43+
ushort arrayLength = (ushort)array.Length;
1744
writer.WriteUShort(arrayLength);
18-
IEnumerable<object> array = (IEnumerable<object>)value;
1945
foreach (object element in array) WriteFieldType(writer, element);
2046
}
2147
else

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ internal void FlushToClient(uint clientId)
524524
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
525525

526526
bool[] mask = GetDirtyMask(false, clientId);
527-
for (int i = 0; i < mask.Length; i++) writer.WriteBool(mask[i]);
528527

529528
for (int i = 0; i < syncedVarFields.Count; i++)
530529
{
530+
writer.WriteBool(mask[i]);
531531
if (syncedVarFields[i].Target && clientId != ownerClientId)
532532
continue;
533533
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo.GetValue(this));
@@ -584,10 +584,10 @@ internal void SyncVarUpdate()
584584
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
585585

586586
bool[] mask = GetDirtyMask(false);
587-
for (int i = 0; i < mask.Length; i++) writer.WriteBool(mask[i]);
588587

589588
for (int i = 0; i < syncedVarFields.Count; i++)
590589
{
590+
writer.WriteBool(mask[i]);
591591
//Writes all the indexes of the dirty syncvars.
592592
if (syncedVarFields[i].Dirty == true)
593593
{
@@ -616,10 +616,10 @@ internal void SyncVarUpdate()
616616
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
617617

618618
bool[] mask = GetDirtyMask(false);
619-
for (int i = 0; i < mask.Length; i++) writer.WriteBool(mask[i]);
620619

621620
for (int i = 0; i < syncedVarFields.Count; i++)
622621
{
622+
writer.WriteBool(mask[i]);
623623
//Writes all the indexes of the dirty syncvars.
624624
if (syncedVarFields[i].Dirty == true)
625625
{
@@ -649,10 +649,10 @@ internal void SyncVarUpdate()
649649
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
650650

651651
bool[] mask = GetDirtyMask(true);
652-
for (int i = 0; i < mask.Length; i++) writer.WriteBool(mask[i]);
653652

654653
for (int i = 0; i < syncedVarFields.Count; i++)
655654
{
655+
writer.WriteBool(mask[i]);
656656
//Writes all the indexes of the dirty syncvars.
657657
if (syncedVarFields[i].Dirty == true && !syncedVarFields[i].Target)
658658
{
@@ -682,7 +682,7 @@ private bool SetDirtyness()
682682
if (NetworkingManager.singleton.NetworkTime - syncedVarFields[i].Attribute.lastSyncTime < syncedVarFields[i].Attribute.syncDelay)
683683
continue;
684684
//Big TODO. This will return true for reference objects. This NEEDS to be fixed. a better compare
685-
if (!syncedVarFields[i].FieldInfo.GetValue(this).Equals(syncedVarFields[i].FieldValue))
685+
if (!FieldTypeHelper.ObjectEqual(syncedVarFields[i].FieldInfo.GetValue(this).GetHashCode(), syncedVarFields[i].FieldValue))
686686
{
687687
syncedVarFields[i].Dirty = true; //This fields value is out of sync!
688688
syncedVarFields[i].Attribute.lastSyncTime = NetworkingManager.singleton.NetworkTime;

0 commit comments

Comments
 (0)