Removed networkedBehaviourId

This commit is contained in:
Albin Corén 2018-03-08 09:47:57 +01:00
parent 0d47c6980b
commit fe88778c88
5 changed files with 34 additions and 70 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -73,7 +73,6 @@
<Compile Include="NetworkingManagerComponents\NetworkPoolManager.cs" /> <Compile Include="NetworkingManagerComponents\NetworkPoolManager.cs" />
<Compile Include="NetworkingManagerComponents\NetworkSceneManager.cs" /> <Compile Include="NetworkingManagerComponents\NetworkSceneManager.cs" />
<Compile Include="NetworkingManagerComponents\SpawnManager.cs" /> <Compile Include="NetworkingManagerComponents\SpawnManager.cs" />
<Compile Include="NetworkingManagerComponents\SyncedVarManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -134,9 +134,6 @@ namespace MLAPI
private List<object> syncedFieldValues = new List<object>(); private List<object> syncedFieldValues = new List<object>();
//A dirty field is a field that's not synced. //A dirty field is a field that's not synced.
public bool[] dirtyFields; 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() internal void SyncVarInit()
{ {
FieldInfo[] sortedFields = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance).OrderBy(x => x.Name).ToArray(); 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(networkId);
writer.Write(networkedObject.GetOrderIndex(this)); writer.Write(networkedObject.GetOrderIndex(this));
writer.Write(networkedBehaviourId);
writer.Write(i); //Index writer.Write(i); //Index
switch (syncedFieldTypes[i]) switch (syncedFieldTypes[i])
{ {
@ -364,7 +360,6 @@ namespace MLAPI
{ {
writer.Write(networkId); writer.Write(networkId);
writer.Write(networkedObject.GetOrderIndex(this)); writer.Write(networkedObject.GetOrderIndex(this));
writer.Write(networkedBehaviourId);
writer.Write(i); //Index writer.Write(i); //Index
switch (syncedFieldTypes[i]) switch (syncedFieldTypes[i])
{ {

View File

@ -64,45 +64,53 @@ namespace MLAPI
internal void InvokeBehaviourOnLostOwnership() internal void InvokeBehaviourOnLostOwnership()
{ {
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>(); for (int i = 0; i < childNetworkedBehaviours.Count; i++)
for (int i = 0; i < netBehaviours.Length; i++)
{ {
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject. childNetworkedBehaviours[i].OnLostOwnership();
if (netBehaviours[i].networkedObject == this)
{
netBehaviours[i].OnLostOwnership();
}
} }
} }
internal void InvokeBehaviourOnGainedOwnership() internal void InvokeBehaviourOnGainedOwnership()
{ {
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>(); for (int i = 0; i < childNetworkedBehaviours.Count; i++)
for (int i = 0; i < netBehaviours.Length; i++)
{ {
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject. childNetworkedBehaviours[i].OnGainedOwnership();
if (netBehaviours[i].networkedObject == this)
{
netBehaviours[i].OnGainedOwnership();
}
} }
} }
internal void InvokeBehaviourNetworkSpawn() internal void InvokeBehaviourNetworkSpawn()
{ {
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>(); for (int i = 0; i < childNetworkedBehaviours.Count; i++)
for (int i = 0; i < netBehaviours.Length; i++)
{ {
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject. //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) if (NetworkingManager.singleton.isServer)
netBehaviours[i].SyncVarInit(); childNetworkedBehaviours[i].SyncVarInit();
} }
} }
} }
private List<NetworkedBehaviour> _childNetworkedBehaviours;
internal List<NetworkedBehaviour> childNetworkedBehaviours
{
get
{
if(_childNetworkedBehaviours == null)
{
_childNetworkedBehaviours = new List<NetworkedBehaviour>();
NetworkedBehaviour[] behaviours = GetComponentsInChildren<NetworkedBehaviour>();
for (int i = 0; i < behaviours.Length; i++)
{
if (behaviours[i].networkedObject == this)
_childNetworkedBehaviours.Add(behaviours[i]);
}
}
return _childNetworkedBehaviours;
}
}
internal static List<NetworkedBehaviour> NetworkedBehaviours = new List<NetworkedBehaviour>(); internal static List<NetworkedBehaviour> NetworkedBehaviours = new List<NetworkedBehaviour>();
internal static void InvokeSyncvarUpdate() internal static void InvokeSyncvarUpdate()
{ {
@ -115,18 +123,17 @@ namespace MLAPI
//Flushes all syncVars to client //Flushes all syncVars to client
internal void FlushToClient(int clientId) 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) internal ushort GetOrderIndex(NetworkedBehaviour instance)
{ {
NetworkedBehaviour[] behaviours = GetComponentsInChildren<NetworkedBehaviour>(); for (ushort i = 0; i < childNetworkedBehaviours.Count; i++)
for (ushort i = 0; i < behaviours.Length; i++)
{ {
if (behaviours[i].networkedObject == this && behaviours[i] == instance) if (childNetworkedBehaviours[i] == instance)
return i; return i;
} }
return 0; return 0;
@ -134,8 +141,8 @@ namespace MLAPI
internal NetworkedBehaviour GetBehaviourAtOrderIndex(ushort index) internal NetworkedBehaviour GetBehaviourAtOrderIndex(ushort index)
{ {
NetworkedBehaviour[] behaviours = GetComponentsInChildren<NetworkedBehaviour>(); //TODO index out of bounds
return behaviours[index]; return childNetworkedBehaviours[index];
} }
} }
} }

View File

@ -74,7 +74,6 @@ namespace MLAPI
{ {
NetworkConfig = netConfig; NetworkConfig = netConfig;
SyncedVarManager.Init();
pendingClients = new HashSet<int>(); pendingClients = new HashSet<int>();
connectedClients = new Dictionary<int, NetworkedClient>(); connectedClients = new Dictionary<int, NetworkedClient>();
messageBuffer = new byte[NetworkConfig.MessageBufferSize]; messageBuffer = new byte[NetworkConfig.MessageBufferSize];
@ -697,7 +696,6 @@ namespace MLAPI
{ {
uint netId = messageReader.ReadUInt32(); //NetId the syncvar is from uint netId = messageReader.ReadUInt32(); //NetId the syncvar is from
ushort orderIndex = messageReader.ReadUInt16(); ushort orderIndex = messageReader.ReadUInt16();
ushort networkBehaviourId = messageReader.ReadUInt16();
byte fieldIndex = messageReader.ReadByte(); byte fieldIndex = messageReader.ReadByte();
FieldType type = SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedFieldTypes[fieldIndex]; FieldType type = SpawnManager.spawnedObjects[netId].GetBehaviourAtOrderIndex(orderIndex).syncedFieldTypes[fieldIndex];
switch (type) switch (type)

View File

@ -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<Type> types = from t in assembly.GetTypes()
where t.IsClass && t.IsSubclassOf(typeof(NetworkedBehaviour))
select t;
List<Type> 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);
}
}
}
}