2018-06-07 13:41:08 +00:00
|
|
|
#if ENABLE_UNET
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
2018-07-26 12:19:29 +00:00
|
|
|
using UnityEngine.Networking.NetworkSystem;
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
namespace UnityEngine.Networking
|
|
|
|
{
|
|
|
|
[RequireComponent(typeof(NetworkIdentity))]
|
|
|
|
[AddComponentMenu("")]
|
|
|
|
public class NetworkBehaviour : MonoBehaviour
|
|
|
|
{
|
2018-06-07 13:50:31 +00:00
|
|
|
ulong m_SyncVarDirtyBits; // ulong instead of uint for 64 instead of 32 SyncVar limit per component
|
2018-06-07 13:41:08 +00:00
|
|
|
float m_LastSendTime;
|
|
|
|
|
|
|
|
// this prevents recursion when SyncVar hook functions are called.
|
|
|
|
bool m_SyncVarGuard;
|
|
|
|
|
|
|
|
public bool localPlayerAuthority { get { return myView.localPlayerAuthority; } }
|
|
|
|
public bool isServer { get { return myView.isServer; } }
|
|
|
|
public bool isClient { get { return myView.isClient; } }
|
|
|
|
public bool isLocalPlayer { get { return myView.isLocalPlayer; } }
|
|
|
|
public bool hasAuthority { get { return myView.hasAuthority; } }
|
|
|
|
public NetworkInstanceId netId { get { return myView.netId; } }
|
|
|
|
public NetworkConnection connectionToServer { get { return myView.connectionToServer; } }
|
|
|
|
public NetworkConnection connectionToClient { get { return myView.connectionToClient; } }
|
|
|
|
public short playerControllerId { get { return myView.playerControllerId; } }
|
2018-06-07 13:50:31 +00:00
|
|
|
protected ulong syncVarDirtyBits { get { return m_SyncVarDirtyBits; } }
|
2018-06-07 13:41:08 +00:00
|
|
|
protected bool syncVarHookGuard { get { return m_SyncVarGuard; } set { m_SyncVarGuard = value; }}
|
|
|
|
|
|
|
|
internal NetworkIdentity netIdentity { get { return myView; } }
|
|
|
|
|
|
|
|
const float k_DefaultSendInterval = 0.1f;
|
|
|
|
|
|
|
|
NetworkIdentity m_MyView;
|
|
|
|
NetworkIdentity myView
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (m_MyView == null)
|
|
|
|
{
|
|
|
|
m_MyView = GetComponent<NetworkIdentity>();
|
|
|
|
if (m_MyView == null)
|
|
|
|
{
|
|
|
|
if (LogFilter.logError) { Debug.LogError("There is no NetworkIdentity on this object. Please add one."); }
|
|
|
|
}
|
|
|
|
return m_MyView;
|
|
|
|
}
|
|
|
|
return m_MyView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------- Commands --------------------------------
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2018-07-26 12:19:29 +00:00
|
|
|
protected void SendCommandInternal(int cmdHash, NetworkWriter writer, int channelId, string cmdName)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
// local players can always send commands, regardless of authority, other objects must have authority.
|
|
|
|
if (!(isLocalPlayer || hasAuthority))
|
|
|
|
{
|
|
|
|
if (LogFilter.logWarn) { Debug.LogWarning("Trying to send command for object without authority."); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClientScene.readyConnection == null)
|
|
|
|
{
|
|
|
|
if (LogFilter.logError) { Debug.LogError("Send command attempted with no client running [client=" + connectionToServer + "]."); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-26 12:19:29 +00:00
|
|
|
// construct the message
|
|
|
|
CommandMessage message = new CommandMessage();
|
|
|
|
message.netId = netId;
|
|
|
|
message.cmdHash = cmdHash;
|
|
|
|
message.payload = writer.ToArray();
|
|
|
|
|
|
|
|
ClientScene.readyConnection.SendByChannel((short)MsgType.Command, message, channelId);
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
UnityEditor.NetworkDetailStats.IncrementStat(
|
|
|
|
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
|
2018-07-19 19:03:00 +00:00
|
|
|
(short)MsgType.Command, cmdName, 1);
|
2018-06-07 13:41:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public virtual bool InvokeCommand(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
2018-06-11 08:44:32 +00:00
|
|
|
return InvokeCommandDelegate(cmdHash, reader);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------- Client RPCs --------------------------------
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2018-07-26 17:15:42 +00:00
|
|
|
protected void SendRPCInternal(int rpcHash, NetworkWriter writer, int channelId, string rpcName)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
// This cannot use NetworkServer.active, as that is not specific to this object.
|
|
|
|
if (!isServer)
|
|
|
|
{
|
|
|
|
if (LogFilter.logWarn) { Debug.LogWarning("ClientRpc call on un-spawned object"); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:15:42 +00:00
|
|
|
// construct the message
|
|
|
|
RpcMessage message = new RpcMessage();
|
|
|
|
message.netId = netId;
|
|
|
|
message.rpcHash = rpcHash;
|
|
|
|
message.payload = writer.ToArray();
|
|
|
|
|
|
|
|
NetworkServer.SendByChannelToReady(gameObject, (short)MsgType.Rpc, message, channelId);
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
UnityEditor.NetworkDetailStats.IncrementStat(
|
|
|
|
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
|
2018-07-19 19:03:00 +00:00
|
|
|
(short)MsgType.Rpc, rpcName, 1);
|
2018-06-07 13:41:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2018-07-26 17:33:38 +00:00
|
|
|
protected void SendTargetRPCInternal(NetworkConnection conn, int rpcHash, NetworkWriter writer, int channelId, string rpcName)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
// This cannot use NetworkServer.active, as that is not specific to this object.
|
|
|
|
if (!isServer)
|
|
|
|
{
|
|
|
|
if (LogFilter.logWarn) { Debug.LogWarning("TargetRpc call on un-spawned object"); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:33:38 +00:00
|
|
|
// construct the message
|
|
|
|
RpcMessage message = new RpcMessage();
|
|
|
|
message.netId = netId;
|
|
|
|
message.rpcHash = rpcHash;
|
|
|
|
message.payload = writer.ToArray();
|
2018-06-07 13:41:08 +00:00
|
|
|
|
2018-07-26 17:33:38 +00:00
|
|
|
conn.SendByChannel((short)MsgType.Rpc, message, channelId);
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
UnityEditor.NetworkDetailStats.IncrementStat(
|
|
|
|
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
|
2018-07-19 19:03:00 +00:00
|
|
|
(short)MsgType.Rpc, rpcName, 1);
|
2018-06-07 13:41:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public virtual bool InvokeRPC(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
2018-06-11 08:44:32 +00:00
|
|
|
return InvokeRpcDelegate(cmdHash, reader);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------- Sync Events --------------------------------
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
protected void SendEventInternal(NetworkWriter writer, int channelId, string eventName)
|
|
|
|
{
|
|
|
|
if (!NetworkServer.active)
|
|
|
|
{
|
|
|
|
if (LogFilter.logWarn) { Debug.LogWarning("SendEvent no server?"); }
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.FinishMessage();
|
2018-07-20 10:42:39 +00:00
|
|
|
NetworkServer.SendBytesToReady(gameObject, writer.ToArray(), channelId);
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
UnityEditor.NetworkDetailStats.IncrementStat(
|
|
|
|
UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
|
2018-07-19 19:03:00 +00:00
|
|
|
(short)MsgType.SyncEvent, eventName, 1);
|
2018-06-07 13:41:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public virtual bool InvokeSyncEvent(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
2018-06-11 08:44:32 +00:00
|
|
|
return InvokeSyncEventDelegate(cmdHash, reader);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------- Sync Lists --------------------------------
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public virtual bool InvokeSyncList(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
2018-06-11 08:44:32 +00:00
|
|
|
return InvokeSyncListDelegate(cmdHash, reader);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------- Code Gen Path Helpers --------------------------------
|
|
|
|
|
|
|
|
public delegate void CmdDelegate(NetworkBehaviour obj, NetworkReader reader);
|
|
|
|
protected delegate void EventDelegate(List<Delegate> targets, NetworkReader reader);
|
|
|
|
|
|
|
|
protected enum UNetInvokeType
|
|
|
|
{
|
|
|
|
Command,
|
|
|
|
ClientRpc,
|
|
|
|
SyncEvent,
|
|
|
|
SyncList
|
|
|
|
};
|
|
|
|
|
|
|
|
protected class Invoker
|
|
|
|
{
|
|
|
|
public UNetInvokeType invokeType;
|
|
|
|
public Type invokeClass;
|
|
|
|
public CmdDelegate invokeFunction;
|
|
|
|
|
|
|
|
public string DebugString()
|
|
|
|
{
|
2018-06-11 08:44:32 +00:00
|
|
|
return invokeType + ":" + invokeClass + ":" + invokeFunction.GetMethodName();
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static Dictionary<int, Invoker> s_CmdHandlerDelegates = new Dictionary<int, Invoker>();
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
static protected void RegisterCommandDelegate(Type invokeClass, int cmdHash, CmdDelegate func)
|
|
|
|
{
|
|
|
|
if (s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Invoker inv = new Invoker();
|
|
|
|
inv.invokeType = UNetInvokeType.Command;
|
|
|
|
inv.invokeClass = invokeClass;
|
|
|
|
inv.invokeFunction = func;
|
|
|
|
s_CmdHandlerDelegates[cmdHash] = inv;
|
|
|
|
if (LogFilter.logDev) { Debug.Log("RegisterCommandDelegate hash:" + cmdHash + " " + func.GetMethodName()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
static protected void RegisterRpcDelegate(Type invokeClass, int cmdHash, CmdDelegate func)
|
|
|
|
{
|
|
|
|
if (s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Invoker inv = new Invoker();
|
|
|
|
inv.invokeType = UNetInvokeType.ClientRpc;
|
|
|
|
inv.invokeClass = invokeClass;
|
|
|
|
inv.invokeFunction = func;
|
|
|
|
s_CmdHandlerDelegates[cmdHash] = inv;
|
|
|
|
if (LogFilter.logDev) { Debug.Log("RegisterRpcDelegate hash:" + cmdHash + " " + func.GetMethodName()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
static protected void RegisterEventDelegate(Type invokeClass, int cmdHash, CmdDelegate func)
|
|
|
|
{
|
|
|
|
if (s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Invoker inv = new Invoker();
|
|
|
|
inv.invokeType = UNetInvokeType.SyncEvent;
|
|
|
|
inv.invokeClass = invokeClass;
|
|
|
|
inv.invokeFunction = func;
|
|
|
|
s_CmdHandlerDelegates[cmdHash] = inv;
|
|
|
|
if (LogFilter.logDev) { Debug.Log("RegisterEventDelegate hash:" + cmdHash + " " + func.GetMethodName()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
static protected void RegisterSyncListDelegate(Type invokeClass, int cmdHash, CmdDelegate func)
|
|
|
|
{
|
|
|
|
if (s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Invoker inv = new Invoker();
|
|
|
|
inv.invokeType = UNetInvokeType.SyncList;
|
|
|
|
inv.invokeClass = invokeClass;
|
|
|
|
inv.invokeFunction = func;
|
|
|
|
s_CmdHandlerDelegates[cmdHash] = inv;
|
|
|
|
if (LogFilter.logDev) { Debug.Log("RegisterSyncListDelegate hash:" + cmdHash + " " + func.GetMethodName()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetInvoker(int cmdHash)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
|
|
|
return inv.DebugString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper fucntions for each type of network operation
|
|
|
|
internal static bool GetInvokerForHashCommand(int cmdHash, out Type invokeClass, out CmdDelegate invokeFunction)
|
|
|
|
{
|
|
|
|
return GetInvokerForHash(cmdHash, UNetInvokeType.Command, out invokeClass, out invokeFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool GetInvokerForHashClientRpc(int cmdHash, out Type invokeClass, out CmdDelegate invokeFunction)
|
|
|
|
{
|
|
|
|
return GetInvokerForHash(cmdHash, UNetInvokeType.ClientRpc, out invokeClass, out invokeFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool GetInvokerForHashSyncList(int cmdHash, out Type invokeClass, out CmdDelegate invokeFunction)
|
|
|
|
{
|
|
|
|
return GetInvokerForHash(cmdHash, UNetInvokeType.SyncList, out invokeClass, out invokeFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static bool GetInvokerForHashSyncEvent(int cmdHash, out Type invokeClass, out CmdDelegate invokeFunction)
|
|
|
|
{
|
|
|
|
return GetInvokerForHash(cmdHash, UNetInvokeType.SyncEvent, out invokeClass, out invokeFunction);
|
|
|
|
}
|
|
|
|
|
2018-07-19 09:38:39 +00:00
|
|
|
static bool GetInvokerForHash(int cmdHash, UNetInvokeType invokeType, out Type invokeClass, out CmdDelegate invokeFunction)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
Invoker invoker = null;
|
|
|
|
if (!s_CmdHandlerDelegates.TryGetValue(cmdHash, out invoker))
|
|
|
|
{
|
|
|
|
if (LogFilter.logDev) { Debug.Log("GetInvokerForHash hash:" + cmdHash + " not found"); }
|
|
|
|
invokeClass = null;
|
|
|
|
invokeFunction = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invoker == null)
|
|
|
|
{
|
|
|
|
if (LogFilter.logDev) { Debug.Log("GetInvokerForHash hash:" + cmdHash + " invoker null"); }
|
|
|
|
invokeClass = null;
|
|
|
|
invokeFunction = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invoker.invokeType != invokeType)
|
|
|
|
{
|
|
|
|
if (LogFilter.logError) { Debug.LogError("GetInvokerForHash hash:" + cmdHash + " mismatched invokeType"); }
|
|
|
|
invokeClass = null;
|
|
|
|
invokeFunction = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
invokeClass = invoker.invokeClass;
|
|
|
|
invokeFunction = invoker.invokeFunction;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void DumpInvokers()
|
|
|
|
{
|
|
|
|
Debug.Log("DumpInvokers size:" + s_CmdHandlerDelegates.Count);
|
|
|
|
foreach (var inv in s_CmdHandlerDelegates)
|
|
|
|
{
|
|
|
|
Debug.Log(" Invoker:" + inv.Value.invokeClass + ":" + inv.Value.invokeFunction.GetMethodName() + " " + inv.Value.invokeType + " " + inv.Key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool ContainsCommandDelegate(int cmdHash)
|
|
|
|
{
|
|
|
|
return s_CmdHandlerDelegates.ContainsKey(cmdHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool InvokeCommandDelegate(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
|
|
|
if (inv.invokeType != UNetInvokeType.Command)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-29 08:37:04 +00:00
|
|
|
// 'this' instance of invokeClass?
|
2018-06-29 03:47:33 +00:00
|
|
|
if (inv.invokeClass.IsInstanceOfType(this))
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-06-29 03:47:33 +00:00
|
|
|
inv.invokeFunction(this, reader);
|
|
|
|
return true;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
2018-06-29 03:47:33 +00:00
|
|
|
return false;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal bool InvokeRpcDelegate(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
|
|
|
if (inv.invokeType != UNetInvokeType.ClientRpc)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-29 08:37:04 +00:00
|
|
|
// 'this' instance of invokeClass?
|
2018-06-29 03:47:33 +00:00
|
|
|
if (inv.invokeClass.IsInstanceOfType(this))
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-06-29 03:47:33 +00:00
|
|
|
inv.invokeFunction(this, reader);
|
|
|
|
return true;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
2018-06-29 03:47:33 +00:00
|
|
|
return false;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal bool InvokeSyncEventDelegate(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
|
|
|
if (inv.invokeType != UNetInvokeType.SyncEvent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inv.invokeFunction(this, reader);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool InvokeSyncListDelegate(int cmdHash, NetworkReader reader)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
|
|
|
if (inv.invokeType != UNetInvokeType.SyncList)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetType() != inv.invokeClass)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inv.invokeFunction(this, reader);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static internal string GetCmdHashHandlerName(int cmdHash)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return cmdHash.ToString();
|
|
|
|
}
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
|
|
|
return inv.invokeType + ":" + inv.invokeFunction.GetMethodName();
|
|
|
|
}
|
|
|
|
|
|
|
|
static string GetCmdHashPrefixName(int cmdHash, string prefix)
|
|
|
|
{
|
|
|
|
if (!s_CmdHandlerDelegates.ContainsKey(cmdHash))
|
|
|
|
{
|
|
|
|
return cmdHash.ToString();
|
|
|
|
}
|
|
|
|
Invoker inv = s_CmdHandlerDelegates[cmdHash];
|
2018-06-11 08:44:32 +00:00
|
|
|
string name = inv.invokeFunction.GetMethodName();
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
int index = name.IndexOf(prefix);
|
|
|
|
if (index > -1)
|
|
|
|
{
|
|
|
|
name = name.Substring(prefix.Length);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetCmdHashCmdName(int cmdHash)
|
|
|
|
{
|
|
|
|
return GetCmdHashPrefixName(cmdHash, "InvokeCmd");
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetCmdHashRpcName(int cmdHash)
|
|
|
|
{
|
|
|
|
return GetCmdHashPrefixName(cmdHash, "InvokeRpc");
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetCmdHashEventName(int cmdHash)
|
|
|
|
{
|
|
|
|
return GetCmdHashPrefixName(cmdHash, "InvokeSyncEvent");
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static string GetCmdHashListName(int cmdHash)
|
|
|
|
{
|
|
|
|
return GetCmdHashPrefixName(cmdHash, "InvokeSyncList");
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------- Helpers --------------------------------
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2018-06-07 13:50:31 +00:00
|
|
|
protected void SetSyncVarGameObject(GameObject newGameObject, ref GameObject gameObjectField, ulong dirtyBit, ref NetworkInstanceId netIdField)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
if (m_SyncVarGuard)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NetworkInstanceId newGameObjectNetId = new NetworkInstanceId();
|
|
|
|
if (newGameObject != null)
|
|
|
|
{
|
|
|
|
var uv = newGameObject.GetComponent<NetworkIdentity>();
|
|
|
|
if (uv != null)
|
|
|
|
{
|
|
|
|
newGameObjectNetId = uv.netId;
|
|
|
|
if (newGameObjectNetId.IsEmpty())
|
|
|
|
{
|
|
|
|
if (LogFilter.logWarn) { Debug.LogWarning("SetSyncVarGameObject GameObject " + newGameObject + " has a zero netId. Maybe it is not spawned yet?"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkInstanceId oldGameObjectNetId = new NetworkInstanceId();
|
|
|
|
if (gameObjectField != null)
|
|
|
|
{
|
|
|
|
oldGameObjectNetId = gameObjectField.GetComponent<NetworkIdentity>().netId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newGameObjectNetId != oldGameObjectNetId)
|
|
|
|
{
|
|
|
|
if (LogFilter.logDev) { Debug.Log("SetSyncVar GameObject " + GetType().Name + " bit [" + dirtyBit + "] netfieldId:" + oldGameObjectNetId + "->" + newGameObjectNetId); }
|
|
|
|
SetDirtyBit(dirtyBit);
|
|
|
|
gameObjectField = newGameObject;
|
|
|
|
netIdField = newGameObjectNetId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2018-06-07 13:50:31 +00:00
|
|
|
protected void SetSyncVar<T>(T value, ref T fieldValue, ulong dirtyBit)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-06-11 08:44:32 +00:00
|
|
|
// newly initialized or changed value?
|
|
|
|
if ((value == null && fieldValue != null) ||
|
|
|
|
(value != null && !value.Equals(fieldValue)))
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
if (LogFilter.logDev) { Debug.Log("SetSyncVar " + GetType().Name + " bit [" + dirtyBit + "] " + fieldValue + "->" + value); }
|
|
|
|
SetDirtyBit(dirtyBit);
|
|
|
|
fieldValue = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// these are masks, not bit numbers, ie. 0x004 not 2
|
2018-06-07 13:50:31 +00:00
|
|
|
public void SetDirtyBit(ulong dirtyBit)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
m_SyncVarDirtyBits |= dirtyBit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearAllDirtyBits()
|
|
|
|
{
|
|
|
|
m_LastSendTime = Time.time;
|
2018-06-07 13:50:31 +00:00
|
|
|
m_SyncVarDirtyBits = 0L;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 14:23:31 +00:00
|
|
|
internal bool IsDirty()
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-07-26 10:54:18 +00:00
|
|
|
return
|
|
|
|
(Time.time - m_LastSendTime > GetNetworkSendInterval())
|
2018-07-23 14:23:31 +00:00
|
|
|
&& m_SyncVarDirtyBits != 0L;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool OnSerialize(NetworkWriter writer, bool initialState)
|
|
|
|
{
|
|
|
|
if (!initialState)
|
|
|
|
{
|
2018-06-07 13:50:31 +00:00
|
|
|
writer.WritePackedUInt64(0);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnDeserialize(NetworkReader reader, bool initialState)
|
|
|
|
{
|
|
|
|
if (!initialState)
|
|
|
|
{
|
2018-06-07 13:50:31 +00:00
|
|
|
reader.ReadPackedUInt64();
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public virtual void PreStartClient()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnNetworkDestroy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnStartServer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnStartClient()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnStartLocalPlayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnStartAuthority()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnStopAuthority()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool OnRebuildObservers(HashSet<NetworkConnection> observers, bool initialize)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void OnSetLocalVisibility(bool vis)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool OnCheckObserver(NetworkConnection conn)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual int GetNetworkChannel()
|
|
|
|
{
|
|
|
|
return Channels.DefaultReliable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual float GetNetworkSendInterval()
|
|
|
|
{
|
|
|
|
return k_DefaultSendInterval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif //ENABLE_UNET
|