diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj index 948bd46..8bc0fde 100644 --- a/MLAPI/MLAPI.csproj +++ b/MLAPI/MLAPI.csproj @@ -1,5 +1,5 @@  - + Debug @@ -73,7 +73,6 @@ - diff --git a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs index 0429961..2348cbd 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs @@ -134,9 +134,6 @@ namespace MLAPI private List syncedFieldValues = new List(); //A dirty field is a field that's not synced. public bool[] dirtyFields; - protected static ushort networkedBehaviourId; - //This is just for the unity editor. if you turn the editor to DEBUG mode you can see what the networkedBehaviourId is. - private ushort _networkedBehaviourId = networkedBehaviourId; internal void SyncVarInit() { FieldInfo[] sortedFields = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance).OrderBy(x => x.Name).ToArray(); @@ -274,7 +271,6 @@ namespace MLAPI { writer.Write(networkId); writer.Write(networkedObject.GetOrderIndex(this)); - writer.Write(networkedBehaviourId); writer.Write(i); //Index switch (syncedFieldTypes[i]) { @@ -364,7 +360,6 @@ namespace MLAPI { writer.Write(networkId); writer.Write(networkedObject.GetOrderIndex(this)); - writer.Write(networkedBehaviourId); writer.Write(i); //Index switch (syncedFieldTypes[i]) { diff --git a/MLAPI/MonoBehaviours/Core/NetworkedObject.cs b/MLAPI/MonoBehaviours/Core/NetworkedObject.cs index 4704b56..96a6cba 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkedObject.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkedObject.cs @@ -64,45 +64,53 @@ namespace MLAPI internal void InvokeBehaviourOnLostOwnership() { - NetworkedBehaviour[] netBehaviours = GetComponentsInChildren(); - for (int i = 0; i < netBehaviours.Length; i++) + for (int i = 0; i < childNetworkedBehaviours.Count; i++) { - //We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject. - if (netBehaviours[i].networkedObject == this) - { - netBehaviours[i].OnLostOwnership(); - } + childNetworkedBehaviours[i].OnLostOwnership(); } } internal void InvokeBehaviourOnGainedOwnership() { - NetworkedBehaviour[] netBehaviours = GetComponentsInChildren(); - for (int i = 0; i < netBehaviours.Length; i++) + for (int i = 0; i < childNetworkedBehaviours.Count; i++) { - //We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject. - if (netBehaviours[i].networkedObject == this) - { - netBehaviours[i].OnGainedOwnership(); - } + childNetworkedBehaviours[i].OnGainedOwnership(); } } internal void InvokeBehaviourNetworkSpawn() { - NetworkedBehaviour[] netBehaviours = GetComponentsInChildren(); - for (int i = 0; i < netBehaviours.Length; i++) + for (int i = 0; i < childNetworkedBehaviours.Count; i++) { //We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject. - if(netBehaviours[i].networkedObject == this && !netBehaviours[i].networkedStartInvoked) + if(!childNetworkedBehaviours[i].networkedStartInvoked) { - netBehaviours[i].NetworkStart(); + childNetworkedBehaviours[i].NetworkStart(); if (NetworkingManager.singleton.isServer) - netBehaviours[i].SyncVarInit(); + childNetworkedBehaviours[i].SyncVarInit(); } } } + private List _childNetworkedBehaviours; + internal List childNetworkedBehaviours + { + get + { + if(_childNetworkedBehaviours == null) + { + _childNetworkedBehaviours = new List(); + NetworkedBehaviour[] behaviours = GetComponentsInChildren(); + for (int i = 0; i < behaviours.Length; i++) + { + if (behaviours[i].networkedObject == this) + _childNetworkedBehaviours.Add(behaviours[i]); + } + } + return _childNetworkedBehaviours; + } + } + internal static List NetworkedBehaviours = new List(); internal static void InvokeSyncvarUpdate() { @@ -115,18 +123,17 @@ namespace MLAPI //Flushes all syncVars to client internal void FlushToClient(int clientId) { - for (int i = 0; i < NetworkedBehaviours.Count; i++) + for (int i = 0; i < childNetworkedBehaviours.Count; i++) { - NetworkedBehaviours[i].FlushToClient(clientId); + childNetworkedBehaviours[i].FlushToClient(clientId); } } internal ushort GetOrderIndex(NetworkedBehaviour instance) { - NetworkedBehaviour[] behaviours = GetComponentsInChildren(); - for (ushort i = 0; i < behaviours.Length; i++) + for (ushort i = 0; i < childNetworkedBehaviours.Count; i++) { - if (behaviours[i].networkedObject == this && behaviours[i] == instance) + if (childNetworkedBehaviours[i] == instance) return i; } return 0; @@ -134,8 +141,8 @@ namespace MLAPI internal NetworkedBehaviour GetBehaviourAtOrderIndex(ushort index) { - NetworkedBehaviour[] behaviours = GetComponentsInChildren(); - return behaviours[index]; + //TODO index out of bounds + return childNetworkedBehaviours[index]; } } } diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index 83092d7..d54061d 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -74,7 +74,6 @@ namespace MLAPI { NetworkConfig = netConfig; - SyncedVarManager.Init(); pendingClients = new HashSet(); connectedClients = new Dictionary(); messageBuffer = new byte[NetworkConfig.MessageBufferSize]; @@ -697,7 +696,6 @@ namespace MLAPI { uint netId = messageReader.ReadUInt32(); //NetId the syncvar is from ushort orderIndex = messageReader.ReadUInt16(); - ushort networkBehaviourId = messageReader.ReadUInt16(); byte fieldIndex = messageReader.ReadByte(); FieldType type = SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedFieldTypes[fieldIndex]; switch (type) diff --git a/MLAPI/NetworkingManagerComponents/SyncedVarManager.cs b/MLAPI/NetworkingManagerComponents/SyncedVarManager.cs deleted file mode 100644 index a9dcf6d..0000000 --- a/MLAPI/NetworkingManagerComponents/SyncedVarManager.cs +++ /dev/null @@ -1,35 +0,0 @@ -using MLAPI.Attributes; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using UnityEngine; - -namespace MLAPI.NetworkingManagerComponents -{ - internal static class SyncedVarManager - { - internal static void Init() - { - Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); - Assembly assembly = Assembly.GetExecutingAssembly(); - for (int i = 0; i < assemblies.Length; i++) - { - if (assemblies[i].FullName == "Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null") - { - assembly = assemblies[i]; - break; - } - } - IEnumerable types = from t in assembly.GetTypes() - where t.IsClass && t.IsSubclassOf(typeof(NetworkedBehaviour)) - select t; - List behaviourTypes = types.OrderBy(x => x.FullName).ToList(); - for (ushort i = 0; i < behaviourTypes.Count; i++) - { - FieldInfo networkedBehaviourId = behaviourTypes[i].GetField("networkedBehaviourId", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy); - networkedBehaviourId.SetValue(null, i); - } - } - } -}