Added Observer system
This commit is contained in:
parent
c8160868f6
commit
711a072d65
@ -9,6 +9,7 @@ namespace UnityEditor
|
||||
{
|
||||
private bool initialized;
|
||||
private NetworkedObject networkedObject;
|
||||
private bool showObservers;
|
||||
|
||||
private void Init()
|
||||
{
|
||||
@ -44,7 +45,23 @@ namespace UnityEditor
|
||||
EditorGUILayout.LabelField("isOwner: ", networkedObject.isOwner.ToString(), EditorStyles.label);
|
||||
EditorGUILayout.LabelField("isPoolObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label);
|
||||
EditorGUILayout.LabelField("isPlayerObject: ", networkedObject.isPlayerObject.ToString(), EditorStyles.label);
|
||||
//EditorGUILayout.LabelField("ServerOnly: ", networkedObject.ServerOnly.ToString(), EditorStyles.label);
|
||||
|
||||
if (networkedObject.observers != null && networkedObject.observers.Count > 0)
|
||||
{
|
||||
showObservers = EditorGUILayout.Foldout(showObservers, "Observers");
|
||||
if (showObservers)
|
||||
{
|
||||
EditorGUI.indentLevel += 1;
|
||||
foreach (var o in networkedObject.observers)
|
||||
{
|
||||
if (NetworkingManager.singleton.ConnectedClients[o].PlayerObject != null)
|
||||
EditorGUILayout.ObjectField("ClientId: " + o, NetworkingManager.singleton.ConnectedClients[o].PlayerObject, typeof(GameObject), false);
|
||||
else
|
||||
EditorGUILayout.TextField("ClientId: " + o, EditorStyles.label);
|
||||
}
|
||||
EditorGUI.indentLevel -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MLAPI.Data
|
||||
{
|
||||
|
@ -84,6 +84,7 @@
|
||||
<Compile Include="GlobalSuppressions.cs" />
|
||||
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
|
||||
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
|
||||
<Compile Include="MonoBehaviours\Prototyping\NetworkedProximity.cs" />
|
||||
<Compile Include="NetworkingManagerComponents\Binary\BitWriter.cs" />
|
||||
<Compile Include="NetworkingManagerComponents\Binary\BitReader.cs" />
|
||||
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
|
||||
|
@ -142,6 +142,28 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
|
||||
internal Dictionary<string, MethodInfo> cachedMethods = new Dictionary<string, MethodInfo>();
|
||||
|
||||
public virtual bool OnCheckObserver(uint clientId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool OnRebuildObservers(HashSet<uint> observers)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void RebuildObservers()
|
||||
{
|
||||
networkedObject.RebuildObservers();
|
||||
}
|
||||
|
||||
public virtual void OnSetLocalVisibility(bool visible)
|
||||
{
|
||||
Renderer[] renderers = GetComponentsInChildren<Renderer>();
|
||||
for (int i = 0; i < renderers.Length; i++)
|
||||
renderers[i].enabled = visible;
|
||||
}
|
||||
|
||||
private void CacheAttributedMethods()
|
||||
{
|
||||
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
|
||||
@ -193,7 +215,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
FieldTypeHelper.WriteFieldType(writer, methodParams[i], fieldType);
|
||||
}
|
||||
|
||||
InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), "MLAPI_COMMAND", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), "MLAPI_COMMAND", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -229,8 +251,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
writer.WriteBits((byte)fieldType, 5);
|
||||
FieldTypeHelper.WriteFieldType(writer, methodParams[i], fieldType);
|
||||
}
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize(), networkId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -265,8 +286,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
writer.WriteBits((byte)fieldType, 5);
|
||||
FieldTypeHelper.WriteFieldType(writer, methodParams[i], fieldType);
|
||||
}
|
||||
|
||||
InternalMessageHandler.Send(ownerClientId, "MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send(ownerClientId, "MLAPI_RPC", "MLAPI_INTERNAL", writer.Finalize(), networkId);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
@ -343,6 +363,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
|
||||
#region SYNC_VAR
|
||||
internal List<SyncedVarField> syncedVarFields = new List<SyncedVarField>();
|
||||
private HashSet<uint> OutOfSyncClients = new HashSet<uint>();
|
||||
private bool syncVarInit = false;
|
||||
internal void SyncVarInit()
|
||||
{
|
||||
@ -422,7 +443,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
writer.WriteByte(i); //FieldIndex
|
||||
FieldTypeHelper.WriteFieldType(writer, syncedVarFields[i].FieldInfo, this, syncedVarFields[i].FieldType);
|
||||
}
|
||||
InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize());
|
||||
bool observed = InternalMessageHandler.Send(clientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), networkId);
|
||||
if (observed)
|
||||
OutOfSyncClients.Remove(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -432,7 +455,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
if (!syncVarInit)
|
||||
SyncVarInit();
|
||||
SetDirtyness();
|
||||
if(NetworkingManager.singleton.NetworkTime - lastSyncTime >= SyncVarSyncDelay)
|
||||
if (NetworkingManager.singleton.NetworkTime - lastSyncTime >= SyncVarSyncDelay)
|
||||
{
|
||||
byte nonTargetDirtyCount = 0;
|
||||
byte totalDirtyCount = 0;
|
||||
@ -472,7 +495,12 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
syncedVarFields[i].Dirty = false;
|
||||
}
|
||||
}
|
||||
InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize());
|
||||
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), networkId);
|
||||
if (stillDirtyIds != null)
|
||||
{
|
||||
for (int i = 0; i < stillDirtyIds.Count; i++)
|
||||
OutOfSyncClients.Add(stillDirtyIds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -501,7 +529,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
}
|
||||
}
|
||||
InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); //Send only to target
|
||||
bool observing = !InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), networkId); //Send only to target
|
||||
if (!observing)
|
||||
OutOfSyncClients.Add(ownerClientId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -526,7 +556,12 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
syncedVarFields[i].Dirty = false;
|
||||
}
|
||||
}
|
||||
InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), ownerClientId); // Send to everyone except target.
|
||||
List<uint> stillDirtyIds = InternalMessageHandler.Send("MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize(), ownerClientId, networkId, null, null); // Send to everyone except target.
|
||||
if (stillDirtyIds != null)
|
||||
{
|
||||
for (int i = 0; i < stillDirtyIds.Count; i++)
|
||||
OutOfSyncClients.Add(stillDirtyIds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
lastSyncTime = NetworkingManager.singleton.NetworkTime;
|
||||
@ -571,7 +606,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data);
|
||||
InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -609,7 +644,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Server can not send messages to server.");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||
InternalMessageHandler.Send(NetId.ServerNetId.GetClientId(), messageType, channelName, data, null, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -630,7 +665,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
|
||||
protected void SendToLocalClient(string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -647,7 +682,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(ownerClientId, messageType, channelName, data);
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(ownerClientId, messageType, channelName, data, fromNetId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -657,9 +693,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToLocalClient<T>(string messageType, string channelName, T instance)
|
||||
protected void SendToLocalClient<T>(string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToLocalClient(messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToLocalClient(messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -685,7 +721,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(ownerClientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||
InternalMessageHandler.Send(ownerClientId, messageType, channelName, data, null, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>gh
|
||||
@ -706,7 +742,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
|
||||
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -723,7 +759,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(messageType, channelName, data, ownerClientId);
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(messageType, channelName, data, ownerClientId, fromNetId, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -733,9 +770,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToNonLocalClients<T>(string messageType, string channelName, T instance)
|
||||
protected void SendToNonLocalClients<T>(string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToNonLocalClients(messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToNonLocalClients(messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -744,7 +781,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data)
|
||||
protected void SendToNonLocalClientsTarget(string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -761,7 +798,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(messageType, channelName, data, ownerClientId, networkId, networkedObject.GetOrderIndex(this));
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(messageType, channelName, data, ownerClientId, fromNetId, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -771,9 +809,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToNonLocalClientsTarget<T>(string messageType, string channelName, T instance)
|
||||
protected void SendToNonLocalClientsTarget<T>(string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToNonLocalClientsTarget(messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToNonLocalClientsTarget(messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -783,7 +821,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClient(uint clientId, string messageType, string channelName, byte[] data)
|
||||
protected void SendToClient(uint clientId, string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -800,7 +838,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(clientId, messageType, channelName, data);
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(clientId, messageType, channelName, data, fromNetId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -811,9 +850,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClient<T>(int clientId, string messageType, string channelName, T instance)
|
||||
protected void SendToClient<T>(int clientId, string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClient(clientId, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClient(clientId, messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -823,7 +862,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClientTarget(uint clientId, string messageType, string channelName, byte[] data)
|
||||
protected void SendToClientTarget(uint clientId, string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -840,7 +879,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(clientId, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(clientId, messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -851,9 +891,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClientTarget<T>(int clientId, string messageType, string channelName, T instance)
|
||||
protected void SendToClientTarget<T>(int clientId, string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClientTarget(clientId, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClientTarget(clientId, messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -863,7 +903,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClients(uint[] clientIds, string messageType, string channelName, byte[] data)
|
||||
protected void SendToClients(uint[] clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -880,7 +920,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data);
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -891,9 +932,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClients<T>(int[] clientIds, string messageType, string channelName, T instance)
|
||||
protected void SendToClients<T>(int[] clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -903,7 +944,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClientsTarget(uint[] clientIds, string messageType, string channelName, byte[] data)
|
||||
protected void SendToClientsTarget(uint[] clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -920,7 +961,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -931,9 +973,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClientsTarget<T>(int[] clientIds, string messageType, string channelName, T instance)
|
||||
protected void SendToClientsTarget<T>(int[] clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -943,7 +985,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClients(List<uint> clientIds, string messageType, string channelName, byte[] data)
|
||||
protected void SendToClients(List<uint> clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -960,7 +1002,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data);
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -971,9 +1014,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClients<T>(List<int> clientIds, string messageType, string channelName, T instance)
|
||||
protected void SendToClients<T>(List<int> clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClients(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -983,7 +1026,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClientsTarget(List<uint> clientIds, string messageType, string channelName, byte[] data)
|
||||
protected void SendToClientsTarget(List<uint> clientIds, string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -1000,7 +1043,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(clientIds, messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1011,9 +1055,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClientsTarget<T>(List<uint> clientIds, string messageType, string channelName, T instance)
|
||||
protected void SendToClientsTarget<T>(List<uint> clientIds, string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClientsTarget(clientIds, messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1022,7 +1066,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClients(string messageType, string channelName, byte[] data)
|
||||
protected void SendToClients(string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -1039,7 +1083,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(messageType, channelName, data);
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(messageType, channelName, data, fromNetId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1049,9 +1094,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClients<T>(string messageType, string channelName, T instance)
|
||||
protected void SendToClients<T>(string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClients(messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClients(messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1060,7 +1105,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="data">The binary data to send</param>
|
||||
protected void SendToClientsTarget(string messageType, string channelName, byte[] data)
|
||||
protected void SendToClientsTarget(string messageType, string channelName, byte[] data, bool respectObservers = false)
|
||||
{
|
||||
if (!MessageManager.messageTypes.ContainsKey(messageType))
|
||||
{
|
||||
@ -1077,7 +1122,8 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
|
||||
return;
|
||||
}
|
||||
InternalMessageHandler.Send(messageType, channelName, data, networkId, networkedObject.GetOrderIndex(this));
|
||||
uint? fromNetId = respectObservers ? (uint?)networkId : null;
|
||||
InternalMessageHandler.Send(messageType, channelName, data, fromNetId, networkId, networkedObject.GetOrderIndex(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1087,9 +1133,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
/// <param name="messageType">User defined messageType</param>
|
||||
/// <param name="channelName">User defined channelName</param>
|
||||
/// <param name="instance">The instance to send</param>
|
||||
protected void SendToClientsTarget<T>(string messageType, string channelName, T instance)
|
||||
protected void SendToClientsTarget<T>(string messageType, string channelName, T instance, bool respectObservers = false)
|
||||
{
|
||||
SendToClientsTarget(messageType, channelName, BinarySerializer.Serialize<T>(instance));
|
||||
SendToClientsTarget(messageType, channelName, BinarySerializer.Serialize<T>(instance), respectObservers);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using MLAPI.Data;
|
||||
using MLAPI.NetworkingManagerComponents.Binary;
|
||||
using MLAPI.NetworkingManagerComponents.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -114,6 +115,73 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
internal bool _isSpawned = false;
|
||||
internal bool? sceneObject = null;
|
||||
|
||||
public HashSet<uint> observers = new HashSet<uint>();
|
||||
|
||||
internal void RebuildObservers(uint? clientId = null)
|
||||
{
|
||||
bool initial = clientId != null;
|
||||
if (initial)
|
||||
{
|
||||
bool shouldBeAdded = true;
|
||||
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
||||
{
|
||||
bool state = childNetworkedBehaviours[i].OnCheckObserver(clientId.Value);
|
||||
if (state == false)
|
||||
{
|
||||
shouldBeAdded = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (shouldBeAdded)
|
||||
observers.Add(clientId.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
HashSet<uint> previousObservers = new HashSet<uint>(observers);
|
||||
HashSet<uint> newObservers = new HashSet<uint>();
|
||||
bool update = false;
|
||||
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
||||
{
|
||||
bool changed = childNetworkedBehaviours[i].OnRebuildObservers(newObservers);
|
||||
if (changed)
|
||||
{
|
||||
observers = newObservers;
|
||||
update = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (update)
|
||||
{
|
||||
foreach (KeyValuePair<uint, NetworkedClient> pair in NetworkingManager.singleton.connectedClients)
|
||||
{
|
||||
if (new NetId(pair.Key).IsHost())
|
||||
continue;
|
||||
if ((previousObservers.Contains(pair.Key) && !newObservers.Contains(pair.Key)) ||
|
||||
(!previousObservers.Contains(pair.Key) && newObservers.Contains(pair.Key)))
|
||||
{
|
||||
//Something changed for this client.
|
||||
using (BitWriter writer = new BitWriter())
|
||||
{
|
||||
writer.WriteUInt(networkId);
|
||||
writer.WriteBool(observers.Contains(pair.Key));
|
||||
|
||||
InternalMessageHandler.Send(pair.Key, "MLAPI_SET_VISIBILITY", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
FlushToClient(pair.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SetLocalVisibility(bool visibility)
|
||||
{
|
||||
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
||||
{
|
||||
childNetworkedBehaviours[i].OnSetLocalVisibility(visibility);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (NetworkingManager.singleton != null)
|
||||
|
@ -384,6 +384,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
MessageManager.messageTypes.Add("MLAPI_COMMAND", 12);
|
||||
MessageManager.messageTypes.Add("MLAPI_RPC", 13);
|
||||
MessageManager.messageTypes.Add("MLAPI_TARGET", 14);
|
||||
MessageManager.messageTypes.Add("MLAPI_SET_VISIBILITY", 15);
|
||||
|
||||
List<MessageType> messageTypes = new List<MessageType>(NetworkConfig.MessageTypes)
|
||||
{
|
||||
@ -761,7 +762,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
if (NetworkConfig.ConnectionApproval)
|
||||
writer.WriteByteArray(NetworkConfig.ConnectionData);
|
||||
|
||||
InternalMessageHandler.Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer.Finalize(), null, null, true);
|
||||
InternalMessageHandler.Send(netId.GetClientId(), "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer.Finalize(), null, null, null, true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -1032,6 +1033,12 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
InternalMessageHandler.HandleTargetRpc(clientId, incommingData, channelId);
|
||||
}
|
||||
break;
|
||||
case 15:
|
||||
if (isClient)
|
||||
{
|
||||
InternalMessageHandler.HandleSetVisibility(clientId, incommingData, channelId);
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@ -1051,6 +1058,9 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
if (diffieHellmanPublicKeys.ContainsKey(clientId))
|
||||
diffieHellmanPublicKeys.Remove(clientId);
|
||||
|
||||
foreach (KeyValuePair<uint, NetworkedObject> pair in SpawnManager.spawnedObjects)
|
||||
pair.Value.observers.Remove(clientId);
|
||||
|
||||
NetId netId = new NetId(clientId);
|
||||
if (netId.IsHost() || netId.IsInvalid())
|
||||
return;
|
||||
@ -1080,10 +1090,13 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
|
||||
if (isServer)
|
||||
{
|
||||
foreach (KeyValuePair<uint, NetworkedObject> pair in SpawnManager.spawnedObjects)
|
||||
pair.Value.observers.Remove(clientId);
|
||||
|
||||
using (BitWriter writer = new BitWriter())
|
||||
{
|
||||
writer.WriteUInt(clientId);
|
||||
InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer.Finalize(), clientId);
|
||||
InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer.Finalize(), clientId, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1098,7 +1111,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
|
||||
byte[] buffer = writer.Finalize();
|
||||
foreach (KeyValuePair<uint, NetworkedClient> pair in connectedClients)
|
||||
InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", buffer);
|
||||
InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", buffer, null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1147,7 +1160,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
connectedClients[clientId].PlayerObject = go;
|
||||
}
|
||||
|
||||
int amountOfObjectsToSend = SpawnManager.spawnedObjects.Values.Count();
|
||||
int amountOfObjectsToSend = SpawnManager.spawnedObjects.Values.Count;
|
||||
|
||||
using (BitWriter writer = new BitWriter())
|
||||
{
|
||||
@ -1179,12 +1192,15 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
|
||||
foreach (KeyValuePair<uint, NetworkedObject> pair in SpawnManager.spawnedObjects)
|
||||
{
|
||||
pair.Value.RebuildObservers(clientId); //Rebuilds observers for the new client
|
||||
|
||||
writer.WriteBool(pair.Value.isPlayerObject);
|
||||
writer.WriteUInt(pair.Value.NetworkId);
|
||||
writer.WriteUInt(pair.Value.OwnerClientId);
|
||||
writer.WriteInt(NetworkConfig.NetworkPrefabIds[pair.Value.NetworkedPrefabName]);
|
||||
writer.WriteBool(pair.Value.gameObject.activeInHierarchy);
|
||||
writer.WriteBool(pair.Value.sceneObject == null ? true : pair.Value.sceneObject.Value);
|
||||
writer.WriteBool(pair.Value.observers.Contains(clientId));
|
||||
|
||||
writer.WriteFloat(pair.Value.transform.position.x);
|
||||
writer.WriteFloat(pair.Value.transform.position.y);
|
||||
@ -1196,7 +1212,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
}
|
||||
}
|
||||
|
||||
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer.Finalize(), null, null, true);
|
||||
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer.Finalize(), null, null, null, true);
|
||||
|
||||
if (OnClientConnectedCallback != null)
|
||||
OnClientConnectedCallback.Invoke(clientId);
|
||||
@ -1213,6 +1229,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
writer.WriteUInt(clientId);
|
||||
writer.WriteInt(-1);
|
||||
writer.WriteBool(false);
|
||||
writer.WriteBool(connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().observers.Contains(clientId));
|
||||
|
||||
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.x);
|
||||
writer.WriteFloat(connectedClients[clientId].PlayerObject.transform.position.y);
|
||||
@ -1227,7 +1244,7 @@ namespace MLAPI.MonoBehaviours.Core
|
||||
writer.WriteUInt(clientId);
|
||||
}
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), clientId);
|
||||
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), clientId, null);
|
||||
}
|
||||
//Flush syncvars:
|
||||
foreach (KeyValuePair<uint, NetworkedObject> networkedObject in SpawnManager.spawnedObjects)
|
||||
|
88
MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs
Normal file
88
MLAPI/MonoBehaviours/Prototyping/NetworkedProximity.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using MLAPI.MonoBehaviours.Core;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MLAPI.MonoBehaviours.Prototyping
|
||||
{
|
||||
//This class based on the code from the UNET HLAPI Proximity
|
||||
public class NetworkedProximity : NetworkedBehaviour
|
||||
{
|
||||
public enum CheckMethod
|
||||
{
|
||||
Physics3D,
|
||||
Physics2D
|
||||
};
|
||||
|
||||
[Tooltip("The maximum range that objects will be visible at.")]
|
||||
public int Range = 10;
|
||||
|
||||
[Tooltip("How often (in seconds) that this object should update the set of players that can see it.")]
|
||||
public float VisibilityUpdateInterval = 1.0f; // in seconds
|
||||
|
||||
[Tooltip("Which method to use for checking proximity of players.\n\nPhysics3D uses 3D physics to determine proximity.\n\nPhysics2D uses 2D physics to determine proximity.")]
|
||||
public CheckMethod CheckType = CheckMethod.Physics3D;
|
||||
|
||||
[Tooltip("Enable to force this object to be hidden from players.")]
|
||||
public bool ForceHidden = false;
|
||||
|
||||
private float lastUpdateTime;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isServer)
|
||||
return;
|
||||
|
||||
if (Time.time - lastUpdateTime > VisibilityUpdateInterval)
|
||||
{
|
||||
RebuildObservers();
|
||||
lastUpdateTime = NetworkingManager.singleton.NetworkTime;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OnCheckObserver(uint newClientId)
|
||||
{
|
||||
if (ForceHidden)
|
||||
return false;
|
||||
Vector3 pos = NetworkingManager.singleton.ConnectedClients[newClientId].PlayerObject.transform.position;
|
||||
return (pos - transform.position).magnitude < Range;
|
||||
}
|
||||
|
||||
public override bool OnRebuildObservers(HashSet<uint> observers)
|
||||
{
|
||||
if (ForceHidden)
|
||||
{
|
||||
// ensure player can still see themself
|
||||
if (networkedObject != null && networkedObject.isPlayerObject)
|
||||
observers.Add(networkedObject.OwnerClientId);
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (CheckType)
|
||||
{
|
||||
case CheckMethod.Physics3D:
|
||||
{
|
||||
var hits = Physics.OverlapSphere(transform.position, Range);
|
||||
for (int i = 0; i < hits.Length; i++)
|
||||
{
|
||||
var uv = hits[i].GetComponent<NetworkedObject>();
|
||||
if (uv != null && uv.isPlayerObject)
|
||||
observers.Add(uv.OwnerClientId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case CheckMethod.Physics2D:
|
||||
{
|
||||
var hits = Physics2D.OverlapCircleAll(transform.position, Range);
|
||||
for (int i = 0; i < hits.Length; i++)
|
||||
{
|
||||
var uv = hits[i].GetComponent<NetworkedObject>();
|
||||
if (uv != null && (uv.isPlayerObject))
|
||||
observers.Add(uv.OwnerClientId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -122,12 +122,13 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
||||
writer.Write(transform.position.x);
|
||||
writer.Write(transform.position.y);
|
||||
writer.Write(transform.position.z);
|
||||
|
||||
writer.Write(transform.rotation.eulerAngles.x);
|
||||
writer.Write(transform.rotation.eulerAngles.y);
|
||||
writer.Write(transform.rotation.eulerAngles.z);
|
||||
}
|
||||
if (isServer)
|
||||
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
|
||||
SendToClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true);
|
||||
else
|
||||
SendToServerTarget("MLAPI_OnRecieveTransformFromClient", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
|
||||
}
|
||||
@ -160,9 +161,11 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
||||
float xPos = reader.ReadSingle();
|
||||
float yPos = reader.ReadSingle();
|
||||
float zPos = reader.ReadSingle();
|
||||
|
||||
float xRot = reader.ReadSingle();
|
||||
float yRot = reader.ReadSingle();
|
||||
float zRot = reader.ReadSingle();
|
||||
|
||||
lerpStartPos = transform.position;
|
||||
lerpStartRot = transform.rotation;
|
||||
lerpEndPos = new Vector3(xPos, yPos, zPos);
|
||||
@ -181,9 +184,11 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
||||
float xPos = reader.ReadSingle();
|
||||
float yPos = reader.ReadSingle();
|
||||
float zPos = reader.ReadSingle();
|
||||
|
||||
float xRot = reader.ReadSingle();
|
||||
float yRot = reader.ReadSingle();
|
||||
float zRot = reader.ReadSingle();
|
||||
|
||||
if (InterpolateServer)
|
||||
{
|
||||
lerpStartPos = transform.position;
|
||||
@ -215,13 +220,13 @@ namespace MLAPI.MonoBehaviours.Prototyping
|
||||
{
|
||||
if (Vector3.Distance(NetworkingManager.singleton.connectedClients[i].PlayerObject.transform.position, transform.position) <= ProximityRange)
|
||||
{
|
||||
SendToClientTarget(NetworkingManager.singleton.connectedClients[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
|
||||
SendToClientTarget(NetworkingManager.singleton.connectedClients[i].ClientId, "MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer);
|
||||
SendToNonLocalClientsTarget("MLAPI_OnRecieveTransformFromServer", "MLAPI_POSITION_UPDATE", positionUpdateBuffer, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -98,6 +98,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
int prefabId = reader.ReadInt();
|
||||
bool isActive = reader.ReadBool();
|
||||
bool sceneObject = reader.ReadBool();
|
||||
bool visible = reader.ReadBool();
|
||||
|
||||
float xPos = reader.ReadFloat();
|
||||
float yPos = reader.ReadFloat();
|
||||
@ -109,13 +110,15 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
|
||||
if (isPlayerObject)
|
||||
{
|
||||
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
|
||||
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
|
||||
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
|
||||
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
|
||||
go.SetActive(isActive);
|
||||
}
|
||||
@ -143,6 +146,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
uint ownerId = reader.ReadUInt();
|
||||
int prefabId = reader.ReadInt();
|
||||
bool sceneObject = reader.ReadBool();
|
||||
bool visible = reader.ReadBool();
|
||||
|
||||
float xPos = reader.ReadFloat();
|
||||
float yPos = reader.ReadFloat();
|
||||
@ -155,12 +159,15 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
if (isPlayerObject)
|
||||
{
|
||||
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
|
||||
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
|
||||
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
|
||||
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
|
||||
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
|
||||
}
|
||||
}
|
||||
@ -348,6 +355,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
uint ownerId = reader.ReadUInt();
|
||||
int prefabId = reader.ReadInt();
|
||||
bool sceneObject = reader.ReadBool();
|
||||
bool visible = reader.ReadBool();
|
||||
|
||||
float xPos = reader.ReadFloat();
|
||||
float yPos = reader.ReadFloat();
|
||||
@ -360,12 +368,16 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
if (isPlayerObject)
|
||||
{
|
||||
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
|
||||
SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
|
||||
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
|
||||
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
|
||||
|
||||
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
|
||||
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
|
||||
}
|
||||
}
|
||||
@ -433,5 +445,13 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
object[] methodParams = FieldTypeHelper.ReadObjects(reader, paramCount);
|
||||
targetMethod.Invoke(behaviour, methodParams);
|
||||
}
|
||||
|
||||
internal static void HandleSetVisibility(uint clientId, byte[] incommingData, int channelId)
|
||||
{
|
||||
BitReader reader = new BitReader(incommingData);
|
||||
uint networkId = reader.ReadUInt();
|
||||
bool visibility = reader.ReadBool();
|
||||
SpawnManager.spawnedObjects[networkId].SetLocalVisibility(visibility);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,26 +48,30 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Send(uint clientId, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
|
||||
//RETURNS IF IT SUCCEDED OR FAILED BECAUSE OF NON-OBSERVER. ANY OTHER FAIL WILL RETURN TRUE
|
||||
internal static bool Send(uint clientId, string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null, bool skipQueue = false)
|
||||
{
|
||||
NetId netId = new NetId(clientId);
|
||||
if (netManager.isHost && netId.IsHost())
|
||||
{
|
||||
//Don't invoke the message on our own machine. Instant stack overflow.
|
||||
Debug.LogWarning("MLAPI: Cannot send message to own client");
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
else if (netId.IsHost())
|
||||
{
|
||||
//Client trying to send data to host
|
||||
netId = NetId.ServerNetId;
|
||||
}
|
||||
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we return
|
||||
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientId))
|
||||
return false;
|
||||
|
||||
bool isPassthrough = (!netManager.isServer && clientId != NetId.ServerNetId.GetClientId() && netManager.NetworkConfig.AllowPassthroughMessages);
|
||||
if (isPassthrough && !netManager.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType]))
|
||||
{
|
||||
Debug.LogWarning("MLAPI: The The MessageType " + messageType + " is not registered as an allowed passthrough message type.");
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
using (BitWriter writer = new BitWriter())
|
||||
@ -112,10 +116,12 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
NetworkTransport.Send(netId.HostId, netId.ConnectionId, MessageManager.channels[channelName], FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
|
||||
else
|
||||
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, MessageManager.channels[channelName], FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Send(uint[] clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
|
||||
internal static void Send(uint[] clientIds, string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
|
||||
{
|
||||
if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
|
||||
{
|
||||
@ -155,6 +161,10 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
netId = NetId.ServerNetId;
|
||||
}
|
||||
|
||||
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
|
||||
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientIds[i]))
|
||||
continue;
|
||||
|
||||
writer.Finalize(ref FinalMessageBuffer);
|
||||
|
||||
byte error;
|
||||
@ -163,7 +173,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Send(List<uint> clientIds, string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
|
||||
internal static void Send(List<uint> clientIds, string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
|
||||
{
|
||||
if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
|
||||
{
|
||||
@ -203,6 +213,10 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
netId = NetId.ServerNetId;
|
||||
}
|
||||
|
||||
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
|
||||
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientIds[i]))
|
||||
continue;
|
||||
|
||||
writer.Finalize(ref FinalMessageBuffer);
|
||||
|
||||
byte error;
|
||||
@ -211,15 +225,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Send(string messageType, string channelName, byte[] data, uint? networkId = null, ushort? orderId = null)
|
||||
//RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED
|
||||
internal static List<uint> Send(string messageType, string channelName, byte[] data, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
|
||||
{
|
||||
if (netManager.connectedClients.Count == 0)
|
||||
return;
|
||||
return null;
|
||||
if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName))
|
||||
{
|
||||
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
List<uint> nonObservedIds = new List<uint>();
|
||||
|
||||
using (BitWriter writer = new BitWriter())
|
||||
{
|
||||
writer.WriteUShort(MessageManager.messageTypes[messageType]);
|
||||
@ -252,22 +270,33 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
netId = NetId.ServerNetId;
|
||||
}
|
||||
|
||||
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
|
||||
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key))
|
||||
{
|
||||
nonObservedIds.Add(pair.Key);
|
||||
continue;
|
||||
}
|
||||
|
||||
writer.Finalize(ref FinalMessageBuffer);
|
||||
|
||||
byte error;
|
||||
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
|
||||
}
|
||||
return nonObservedIds;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? networkId = null, ushort? orderId = null)
|
||||
//RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED
|
||||
internal static List<uint> Send(string messageType, string channelName, byte[] data, uint clientIdToIgnore, uint? fromNetId, uint? networkId = null, ushort? orderId = null)
|
||||
{
|
||||
if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName))
|
||||
{
|
||||
Debug.LogWarning("MLAPI: Cannot send messages over encrypted channel to multiple clients.");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
List<uint> nonObservedIds = new List<uint>();
|
||||
|
||||
using (BitWriter writer = new BitWriter())
|
||||
{
|
||||
writer.WriteUShort(MessageManager.messageTypes[messageType]);
|
||||
@ -303,11 +332,19 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
netId = NetId.ServerNetId;
|
||||
}
|
||||
|
||||
//If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue
|
||||
if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key))
|
||||
{
|
||||
nonObservedIds.Add(pair.Key);
|
||||
continue;
|
||||
}
|
||||
|
||||
writer.Finalize(ref FinalMessageBuffer);
|
||||
|
||||
byte error;
|
||||
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channel, FinalMessageBuffer, (int)writer.GetFinalizeSize(), out error);
|
||||
}
|
||||
return nonObservedIds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
writer.WriteFloat(rotation.eulerAngles.y);
|
||||
writer.WriteFloat(rotation.eulerAngles.z);
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
return go;
|
||||
}
|
||||
@ -99,7 +99,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
{
|
||||
writer.WriteUInt(netObject.NetworkId);
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
{
|
||||
writer.WriteUInt(sceneNameToIndex[sceneName]);
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
writer.WriteUInt(netId);
|
||||
writer.WriteUInt(netObject.ownerClientId);
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
writer.WriteUInt(netId);
|
||||
writer.WriteUInt(clientId);
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_CHANGE_OWNER", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
writer.WriteFloat(sceneObjectsToSync[i].transform.rotation.eulerAngles.z);
|
||||
}
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_ADD_OBJECTS", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_ADD_OBJECTS", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,7 +211,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
writer.WriteFloat(netObject.transform.rotation.eulerAngles.z);
|
||||
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -265,7 +265,7 @@ namespace MLAPI.NetworkingManagerComponents.Core
|
||||
{
|
||||
writer.WriteUInt(networkId);
|
||||
|
||||
InternalMessageHandler.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", writer.Finalize());
|
||||
InternalMessageHandler.Send("MLAPI_DESTROY_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user