806 lines
25 KiB
C#
Raw Normal View History

2020-12-14 16:06:35 +00:00
using QuantumUNET.Components;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Messages;
using QuantumUNET.Transport;
2020-12-07 21:19:16 +00:00
using System.Collections.Generic;
2020-12-02 12:42:26 +00:00
using UnityEngine;
using UnityEngine.Networking;
2020-12-04 22:14:53 +00:00
namespace QuantumUNET
2020-12-02 12:42:26 +00:00
{
public class QSBClientScene
{
internal static void SetNotReady() => ready = false;
public static List<QSBPlayerController> localPlayers { get; private set; } = new List<QSBPlayerController>();
public static bool ready { get; private set; }
public static QSBNetworkConnection readyConnection { get; private set; }
public static int reconnectId { get; private set; } = -1;
2020-12-16 08:57:15 +00:00
public static Dictionary<NetworkInstanceId, QSBNetworkIdentity> Objects => s_NetworkScene.localObjects;
2020-12-02 12:42:26 +00:00
2020-12-16 08:45:58 +00:00
public static Dictionary<NetworkHash128, GameObject> Prefabs => QSBNetworkScene.guidToPrefab;
2020-12-02 12:42:26 +00:00
2020-12-16 08:45:58 +00:00
public static Dictionary<NetworkSceneId, QSBNetworkIdentity> SpawnableObjects { get; private set; }
2020-12-02 12:42:26 +00:00
internal static void Shutdown()
{
s_NetworkScene.Shutdown();
localPlayers = new List<QSBPlayerController>();
s_PendingOwnerIds = new List<PendingOwner>();
SpawnableObjects = null;
readyConnection = null;
ready = false;
s_IsSpawnFinished = false;
reconnectId = -1;
NetworkTransport.Shutdown();
NetworkTransport.Init();
}
internal static bool GetPlayerController(short playerControllerId, out QSBPlayerController player)
{
player = null;
bool result;
if (playerControllerId >= localPlayers.Count)
2020-12-02 12:42:26 +00:00
{
if (LogFilter.logWarn)
{
Debug.Log("ClientScene::GetPlayer: no local player found for: " + playerControllerId);
}
result = false;
}
else if (localPlayers[playerControllerId] == null)
2020-12-02 12:42:26 +00:00
{
if (LogFilter.logWarn)
{
Debug.LogWarning("ClientScene::GetPlayer: local player is null for: " + playerControllerId);
}
result = false;
}
else
{
player = localPlayers[playerControllerId];
2020-12-03 11:56:32 +00:00
result = (player.Gameobject != null);
2020-12-02 12:42:26 +00:00
}
return result;
}
internal static void InternalAddPlayer(QSBNetworkIdentity view, short playerControllerId)
{
if (LogFilter.logDebug)
{
Debug.LogWarning("ClientScene::InternalAddPlayer: playerControllerId : " + playerControllerId);
}
if (playerControllerId >= localPlayers.Count)
2020-12-02 12:42:26 +00:00
{
if (LogFilter.logWarn)
{
Debug.LogWarning("ClientScene::InternalAddPlayer: playerControllerId higher than expected: " + playerControllerId);
}
while (playerControllerId >= localPlayers.Count)
2020-12-02 12:42:26 +00:00
{
localPlayers.Add(new QSBPlayerController());
}
}
var playerController = new QSBPlayerController
{
2020-12-03 11:56:32 +00:00
Gameobject = view.gameObject,
PlayerControllerId = playerControllerId,
UnetView = view
2020-12-02 12:42:26 +00:00
};
localPlayers[playerControllerId] = playerController;
2020-12-02 12:42:26 +00:00
readyConnection.SetPlayerController(playerController);
}
public static bool AddPlayer(short playerControllerId) => AddPlayer(null, playerControllerId);
public static bool AddPlayer(QSBNetworkConnection readyConn, short playerControllerId) => AddPlayer(readyConn, playerControllerId, null);
2020-12-04 09:23:27 +00:00
public static bool AddPlayer(QSBNetworkConnection readyConn, short playerControllerId, QSBMessageBase extraMessage)
2020-12-02 12:42:26 +00:00
{
bool result;
if (playerControllerId < 0)
{
Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
2020-12-02 12:42:26 +00:00
result = false;
}
else if (playerControllerId > 32)
{
Debug.LogError(string.Concat(new object[]
2020-12-02 12:42:26 +00:00
{
"ClientScene::AddPlayer: playerControllerId of ",
playerControllerId,
" is too high, max is ",
32
}));
2020-12-02 12:42:26 +00:00
result = false;
}
else
{
if (playerControllerId > 16)
{
if (LogFilter.logWarn)
{
Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
}
}
while (playerControllerId >= localPlayers.Count)
2020-12-02 12:42:26 +00:00
{
localPlayers.Add(new QSBPlayerController());
}
if (readyConn == null)
{
if (!ready)
{
if (LogFilter.logError)
{
Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
}
return false;
}
}
else
{
ready = true;
readyConnection = readyConn;
}
if (readyConnection.GetPlayerController(playerControllerId, out var playerController))
{
2020-12-03 11:56:32 +00:00
if (playerController.IsValid && playerController.Gameobject != null)
2020-12-02 12:42:26 +00:00
{
if (LogFilter.logError)
{
Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
}
return false;
}
}
if (LogFilter.logDebug)
{
Debug.Log(string.Concat(new object[]
{
"ClientScene::AddPlayer() for ID ",
playerControllerId,
" called with connection [",
readyConnection,
"]"
}));
}
2020-12-13 22:25:23 +00:00
var addPlayerMessage = new QSBAddPlayerMessage
2020-12-02 12:42:26 +00:00
{
2020-12-13 22:25:23 +00:00
playerControllerId = playerControllerId
};
if (extraMessage != null)
2020-12-02 12:42:26 +00:00
{
2020-12-13 22:25:23 +00:00
var networkWriter = new QSBNetworkWriter();
extraMessage.Serialize(networkWriter);
addPlayerMessage.msgData = networkWriter.ToArray();
addPlayerMessage.msgSize = networkWriter.Position;
2020-12-02 12:42:26 +00:00
}
2020-12-13 22:25:23 +00:00
readyConnection.Send(37, addPlayerMessage);
result = true;
2020-12-02 12:42:26 +00:00
}
return result;
}
public static bool RemovePlayer(short playerControllerId)
{
if (LogFilter.logDebug)
{
Debug.Log(string.Concat(new object[]
{
"ClientScene::RemovePlayer() for ID ",
playerControllerId,
" called with connection [",
readyConnection,
"]"
}));
}
bool result;
if (readyConnection.GetPlayerController(playerControllerId, out var playerController))
{
2020-12-04 09:23:27 +00:00
var removePlayerMessage = new QSBRemovePlayerMessage
2020-12-02 12:42:26 +00:00
{
2020-12-04 09:23:27 +00:00
PlayerControllerId = playerControllerId
2020-12-02 12:42:26 +00:00
};
readyConnection.Send(38, removePlayerMessage);
readyConnection.RemovePlayerController(playerControllerId);
localPlayers[playerControllerId] = new QSBPlayerController();
Object.Destroy(playerController.Gameobject);
2020-12-02 12:42:26 +00:00
result = true;
}
else
{
if (LogFilter.logError)
{
Debug.LogError("Failed to find player ID " + playerControllerId);
}
result = false;
}
return result;
}
public static bool Ready(QSBNetworkConnection conn)
{
bool result;
if (ready)
{
if (LogFilter.logError)
{
Debug.LogError("A connection has already been set as ready. There can only be one.");
}
result = false;
}
else
{
if (LogFilter.logDebug)
{
Debug.Log("ClientScene::Ready() called with connection [" + conn + "]");
}
if (conn != null)
{
2020-12-04 09:23:27 +00:00
var msg = new QSBReadyMessage();
2020-12-02 12:42:26 +00:00
conn.Send(35, msg);
ready = true;
readyConnection = conn;
readyConnection.isReady = true;
result = true;
}
else
{
if (LogFilter.logError)
{
Debug.LogError("Ready() called with invalid connection object: conn=null");
}
result = false;
}
}
return result;
}
public static QSBNetworkClient ConnectLocalServer()
{
var localClient = new QSBLocalClient();
QSBNetworkServer.instance.ActivateLocalClientScene();
localClient.InternalConnectLocalServer(true);
return localClient;
}
internal static QSBNetworkClient ReconnectLocalServer()
{
var localClient = new QSBLocalClient();
QSBNetworkServer.instance.ActivateLocalClientScene();
localClient.InternalConnectLocalServer(false);
return localClient;
}
internal static void ClearLocalPlayers() => localPlayers.Clear();
internal static void HandleClientDisconnect(QSBNetworkConnection conn)
{
if (readyConnection == conn && ready)
{
ready = false;
readyConnection = null;
}
}
internal static void PrepareToSpawnSceneObjects()
{
2020-12-16 08:45:58 +00:00
SpawnableObjects = new Dictionary<NetworkSceneId, QSBNetworkIdentity>();
2020-12-02 12:42:26 +00:00
foreach (var networkIdentity in Resources.FindObjectsOfTypeAll<QSBNetworkIdentity>())
{
if (!networkIdentity.gameObject.activeSelf)
{
if (networkIdentity.gameObject.hideFlags != HideFlags.NotEditable && networkIdentity.gameObject.hideFlags != HideFlags.HideAndDontSave)
{
if (!networkIdentity.SceneId.IsEmpty())
{
SpawnableObjects[networkIdentity.SceneId] = networkIdentity;
if (LogFilter.logDebug)
{
Debug.Log("ClientScene::PrepareSpawnObjects sceneId:" + networkIdentity.SceneId);
}
}
}
}
}
}
2020-12-16 08:45:58 +00:00
internal static QSBNetworkIdentity SpawnSceneObject(NetworkSceneId sceneId)
2020-12-02 12:42:26 +00:00
{
QSBNetworkIdentity result;
if (SpawnableObjects.ContainsKey(sceneId))
{
var networkIdentity = SpawnableObjects[sceneId];
SpawnableObjects.Remove(sceneId);
result = networkIdentity;
}
else
{
result = null;
}
return result;
}
internal static void RegisterSystemHandlers(QSBNetworkClient client, bool localClient)
{
if (localClient)
{
client.RegisterHandlerSafe(1, new QSBNetworkMessageDelegate(OnLocalClientObjectDestroy));
client.RegisterHandlerSafe(13, new QSBNetworkMessageDelegate(OnLocalClientObjectHide));
client.RegisterHandlerSafe(3, new QSBNetworkMessageDelegate(OnLocalClientObjectSpawn));
client.RegisterHandlerSafe(10, new QSBNetworkMessageDelegate(OnLocalClientObjectSpawnScene));
client.RegisterHandlerSafe(15, new QSBNetworkMessageDelegate(OnClientAuthority));
2020-12-02 12:42:26 +00:00
}
else
{
client.RegisterHandlerSafe(3, new QSBNetworkMessageDelegate(OnObjectSpawn));
client.RegisterHandlerSafe(10, new QSBNetworkMessageDelegate(OnObjectSpawnScene));
client.RegisterHandlerSafe(12, new QSBNetworkMessageDelegate(OnObjectSpawnFinished));
client.RegisterHandlerSafe(1, new QSBNetworkMessageDelegate(OnObjectDestroy));
client.RegisterHandlerSafe(13, new QSBNetworkMessageDelegate(OnObjectDestroy));
client.RegisterHandlerSafe(8, new QSBNetworkMessageDelegate(OnUpdateVarsMessage));
client.RegisterHandlerSafe(4, new QSBNetworkMessageDelegate(OnOwnerMessage));
client.RegisterHandlerSafe(9, new QSBNetworkMessageDelegate(OnSyncListMessage));
client.RegisterHandlerSafe(40, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationClientMessage));
client.RegisterHandlerSafe(41, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersClientMessage));
client.RegisterHandlerSafe(15, new QSBNetworkMessageDelegate(OnClientAuthority));
}
client.RegisterHandlerSafe(2, new QSBNetworkMessageDelegate(OnRPCMessage));
client.RegisterHandlerSafe(7, new QSBNetworkMessageDelegate(OnSyncEventMessage));
client.RegisterHandlerSafe(42, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationTriggerClientMessage));
2020-12-02 12:42:26 +00:00
}
2020-12-16 08:45:58 +00:00
internal static string GetStringForAssetId(NetworkHash128 assetId)
2020-12-02 12:42:26 +00:00
{
string result;
if (QSBNetworkScene.GetPrefab(assetId, out var gameObject))
{
result = gameObject.name;
}
else if (QSBNetworkScene.GetSpawnHandler(assetId, out var func))
{
result = func.GetMethodName();
}
else
{
result = "unknown";
}
return result;
}
2020-12-16 08:45:58 +00:00
public static void RegisterPrefab(GameObject prefab, NetworkHash128 newAssetId) => QSBNetworkScene.RegisterPrefab(prefab, newAssetId);
2020-12-02 12:42:26 +00:00
public static void RegisterPrefab(GameObject prefab) => QSBNetworkScene.RegisterPrefab(prefab);
public static void RegisterPrefab(GameObject prefab, QSBSpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) => QSBNetworkScene.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
2020-12-02 12:42:26 +00:00
public static void UnregisterPrefab(GameObject prefab) => QSBNetworkScene.UnregisterPrefab(prefab);
public static void RegisterSpawnHandler(NetworkHash128 assetId, QSBSpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) => QSBNetworkScene.RegisterSpawnHandler(assetId, spawnHandler, unspawnHandler);
2020-12-02 12:42:26 +00:00
2020-12-16 08:45:58 +00:00
public static void UnregisterSpawnHandler(NetworkHash128 assetId) => QSBNetworkScene.UnregisterSpawnHandler(assetId);
2020-12-02 12:42:26 +00:00
public static void ClearSpawners() => QSBNetworkScene.ClearSpawners();
public static void DestroyAllClientObjects() => s_NetworkScene.DestroyAllClientObjects();
2020-12-16 08:57:15 +00:00
public static void SetLocalObject(NetworkInstanceId netId, GameObject obj) => s_NetworkScene.SetLocalObject(netId, obj, s_IsSpawnFinished, false);
2020-12-02 12:42:26 +00:00
2020-12-16 08:57:15 +00:00
public static GameObject FindLocalObject(NetworkInstanceId netId) => s_NetworkScene.FindLocalObject(netId);
2020-12-02 12:42:26 +00:00
2020-12-16 08:57:15 +00:00
private static void ApplySpawnPayload(QSBNetworkIdentity uv, Vector3 position, byte[] payload, NetworkInstanceId netId, GameObject newGameObject)
2020-12-02 12:42:26 +00:00
{
if (!uv.gameObject.activeSelf)
{
uv.gameObject.SetActive(true);
}
uv.transform.position = position;
if (payload != null && payload.Length > 0)
{
2020-12-04 09:23:27 +00:00
var reader = new QSBNetworkReader(payload);
2020-12-02 12:42:26 +00:00
uv.OnUpdateVars(reader, true);
}
if (!(newGameObject == null))
{
newGameObject.SetActive(true);
uv.SetNetworkInstanceId(netId);
SetLocalObject(netId, newGameObject);
if (s_IsSpawnFinished)
{
uv.OnStartClient();
CheckForOwner(uv);
}
}
}
private static void OnObjectSpawn(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectSpawnMessage>(s_ObjectSpawnMessage);
if (!s_ObjectSpawnMessage.assetId.IsValid())
{
2020-12-03 11:56:32 +00:00
Debug.LogError($"OnObjSpawn netId: {s_ObjectSpawnMessage.NetId} has invalid asset Id. {s_ObjectSpawnMessage.assetId}");
2020-12-02 12:42:26 +00:00
}
else
{
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_ObjectSpawnMessage.NetId, out var component))
2020-12-02 12:42:26 +00:00
{
2020-12-03 11:56:32 +00:00
ApplySpawnPayload(component, s_ObjectSpawnMessage.Position, s_ObjectSpawnMessage.Payload, s_ObjectSpawnMessage.NetId, null);
2020-12-02 12:42:26 +00:00
}
else if (QSBNetworkScene.GetPrefab(s_ObjectSpawnMessage.assetId, out var original))
{
var gameObject = Object.Instantiate<GameObject>(original, s_ObjectSpawnMessage.Position, s_ObjectSpawnMessage.Rotation);
2020-12-02 12:42:26 +00:00
component = gameObject.GetComponent<QSBNetworkIdentity>();
if (component == null)
{
2020-12-02 18:40:38 +00:00
Debug.LogError("Client object spawned for " + s_ObjectSpawnMessage.assetId + " does not have a NetworkIdentity");
2020-12-02 12:42:26 +00:00
}
else
{
component.Reset();
2020-12-03 11:56:32 +00:00
ApplySpawnPayload(component, s_ObjectSpawnMessage.Position, s_ObjectSpawnMessage.Payload, s_ObjectSpawnMessage.NetId, gameObject);
2020-12-02 12:42:26 +00:00
}
}
else if (QSBNetworkScene.GetSpawnHandler(s_ObjectSpawnMessage.assetId, out var spawnDelegate))
{
2020-12-03 11:56:32 +00:00
var gameObject2 = spawnDelegate(s_ObjectSpawnMessage.Position, s_ObjectSpawnMessage.assetId);
2020-12-02 12:42:26 +00:00
if (gameObject2 == null)
{
2020-12-02 18:40:38 +00:00
Debug.LogWarning("Client spawn handler for " + s_ObjectSpawnMessage.assetId + " returned null");
2020-12-02 12:42:26 +00:00
}
else
{
component = gameObject2.GetComponent<QSBNetworkIdentity>();
if (component == null)
{
2020-12-02 18:40:38 +00:00
Debug.LogError("Client object spawned for " + s_ObjectSpawnMessage.assetId + " does not have a network identity");
2020-12-02 12:42:26 +00:00
}
else
{
component.Reset();
component.SetDynamicAssetId(s_ObjectSpawnMessage.assetId);
2020-12-03 11:56:32 +00:00
ApplySpawnPayload(component, s_ObjectSpawnMessage.Position, s_ObjectSpawnMessage.Payload, s_ObjectSpawnMessage.NetId, gameObject2);
2020-12-02 12:42:26 +00:00
}
}
}
2020-12-02 18:40:38 +00:00
else
2020-12-02 12:42:26 +00:00
{
Debug.LogError(string.Concat(new object[]
{
2020-12-02 20:43:16 +00:00
"Failed to spawn server object, did you forget to add it to the QSBNetworkManager? assetId=",
2020-12-02 12:42:26 +00:00
s_ObjectSpawnMessage.assetId,
" netId=",
2020-12-03 11:56:32 +00:00
s_ObjectSpawnMessage.NetId
2020-12-02 12:42:26 +00:00
}));
}
}
}
private static void OnObjectSpawnScene(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectSpawnSceneMessage>(s_ObjectSpawnSceneMessage);
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_ObjectSpawnSceneMessage.NetId, out var networkIdentity))
2020-12-02 12:42:26 +00:00
{
2020-12-03 11:56:32 +00:00
ApplySpawnPayload(networkIdentity, s_ObjectSpawnSceneMessage.Position, s_ObjectSpawnSceneMessage.Payload, s_ObjectSpawnSceneMessage.NetId, networkIdentity.gameObject);
2020-12-02 12:42:26 +00:00
}
else
{
2020-12-03 11:56:32 +00:00
var networkIdentity2 = SpawnSceneObject(s_ObjectSpawnSceneMessage.SceneId);
2020-12-02 12:42:26 +00:00
if (networkIdentity2 == null)
{
if (LogFilter.logError)
{
2020-12-03 11:56:32 +00:00
Debug.LogError("Spawn scene object not found for " + s_ObjectSpawnSceneMessage.SceneId);
2020-12-02 12:42:26 +00:00
}
}
else
{
if (LogFilter.logDebug)
{
Debug.Log(string.Concat(new object[]
{
"Client spawn for [netId:",
2020-12-03 11:56:32 +00:00
s_ObjectSpawnSceneMessage.NetId,
2020-12-02 12:42:26 +00:00
"] [sceneId:",
2020-12-03 11:56:32 +00:00
s_ObjectSpawnSceneMessage.SceneId,
2020-12-02 12:42:26 +00:00
"] obj:",
networkIdentity2.gameObject.name
}));
}
2020-12-03 11:56:32 +00:00
ApplySpawnPayload(networkIdentity2, s_ObjectSpawnSceneMessage.Position, s_ObjectSpawnSceneMessage.Payload, s_ObjectSpawnSceneMessage.NetId, networkIdentity2.gameObject);
2020-12-02 12:42:26 +00:00
}
}
}
private static void OnObjectSpawnFinished(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectSpawnFinishedMessage>(s_ObjectSpawnFinishedMessage);
if (LogFilter.logDebug)
{
2020-12-03 11:56:32 +00:00
Debug.Log("SpawnFinished:" + s_ObjectSpawnFinishedMessage.State);
2020-12-02 12:42:26 +00:00
}
2020-12-03 11:56:32 +00:00
if (s_ObjectSpawnFinishedMessage.State == 0U)
2020-12-02 12:42:26 +00:00
{
PrepareToSpawnSceneObjects();
s_IsSpawnFinished = false;
}
else
{
foreach (var networkIdentity in Objects.Values)
{
if (!networkIdentity.IsClient)
{
networkIdentity.OnStartClient();
CheckForOwner(networkIdentity);
}
}
s_IsSpawnFinished = true;
}
}
private static void OnObjectDestroy(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectDestroyMessage>(s_ObjectDestroyMessage);
if (LogFilter.logDebug)
{
2020-12-03 11:56:32 +00:00
Debug.Log("ClientScene::OnObjDestroy netId:" + s_ObjectDestroyMessage.NetId);
2020-12-02 12:42:26 +00:00
}
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_ObjectDestroyMessage.NetId, out var networkIdentity))
2020-12-02 12:42:26 +00:00
{
networkIdentity.OnNetworkDestroy();
if (!QSBNetworkScene.InvokeUnSpawnHandler(networkIdentity.AssetId, networkIdentity.gameObject))
{
if (networkIdentity.SceneId.IsEmpty())
{
Object.Destroy(networkIdentity.gameObject);
2020-12-02 12:42:26 +00:00
}
else
{
networkIdentity.gameObject.SetActive(false);
SpawnableObjects[networkIdentity.SceneId] = networkIdentity;
}
}
2020-12-03 11:56:32 +00:00
s_NetworkScene.RemoveLocalObject(s_ObjectDestroyMessage.NetId);
2020-12-02 12:42:26 +00:00
networkIdentity.MarkForReset();
}
else if (LogFilter.logDebug)
{
2020-12-03 11:56:32 +00:00
Debug.LogWarning("Did not find target for destroy message for " + s_ObjectDestroyMessage.NetId);
2020-12-02 12:42:26 +00:00
}
}
private static void OnLocalClientObjectDestroy(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectDestroyMessage>(s_ObjectDestroyMessage);
if (LogFilter.logDebug)
{
2020-12-03 11:56:32 +00:00
Debug.Log("ClientScene::OnLocalObjectObjDestroy netId:" + s_ObjectDestroyMessage.NetId);
2020-12-02 12:42:26 +00:00
}
2020-12-03 11:56:32 +00:00
s_NetworkScene.RemoveLocalObject(s_ObjectDestroyMessage.NetId);
2020-12-02 12:42:26 +00:00
}
private static void OnLocalClientObjectHide(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectDestroyMessage>(s_ObjectDestroyMessage);
if (LogFilter.logDebug)
{
2020-12-03 11:56:32 +00:00
Debug.Log("ClientScene::OnLocalObjectObjHide netId:" + s_ObjectDestroyMessage.NetId);
2020-12-02 12:42:26 +00:00
}
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_ObjectDestroyMessage.NetId, out var networkIdentity))
2020-12-02 12:42:26 +00:00
{
networkIdentity.OnSetLocalVisibility(false);
}
}
private static void OnLocalClientObjectSpawn(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectSpawnMessage>(s_ObjectSpawnMessage);
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_ObjectSpawnMessage.NetId, out var networkIdentity))
2020-12-02 12:42:26 +00:00
{
networkIdentity.OnSetLocalVisibility(true);
}
}
private static void OnLocalClientObjectSpawnScene(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBObjectSpawnSceneMessage>(s_ObjectSpawnSceneMessage);
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_ObjectSpawnSceneMessage.NetId, out var networkIdentity))
2020-12-02 12:42:26 +00:00
{
networkIdentity.OnSetLocalVisibility(true);
}
}
private static void OnUpdateVarsMessage(QSBNetworkMessage netMsg)
{
2020-12-03 11:56:32 +00:00
var networkInstanceId = netMsg.Reader.ReadNetworkId();
2020-12-02 12:42:26 +00:00
if (s_NetworkScene.GetNetworkIdentity(networkInstanceId, out var networkIdentity))
{
2020-12-03 11:56:32 +00:00
networkIdentity.OnUpdateVars(netMsg.Reader, false);
2020-12-02 12:42:26 +00:00
}
else if (LogFilter.logWarn)
{
Debug.LogWarning("Did not find target for sync message for " + networkInstanceId);
}
}
private static void OnRPCMessage(QSBNetworkMessage netMsg)
{
2020-12-03 11:56:32 +00:00
var num = (int)netMsg.Reader.ReadPackedUInt32();
var networkInstanceId = netMsg.Reader.ReadNetworkId();
2020-12-02 12:42:26 +00:00
if (LogFilter.logDebug)
{
Debug.Log(string.Concat(new object[]
{
"ClientScene::OnRPCMessage hash:",
num,
" netId:",
networkInstanceId
}));
}
if (s_NetworkScene.GetNetworkIdentity(networkInstanceId, out var networkIdentity))
{
2020-12-03 11:56:32 +00:00
networkIdentity.HandleRPC(num, netMsg.Reader);
2020-12-02 12:42:26 +00:00
}
else if (LogFilter.logWarn)
{
var cmdHashHandlerName = QSBNetworkBehaviour.GetCmdHashHandlerName(num);
2020-12-02 12:42:26 +00:00
Debug.LogWarningFormat("Could not find target object with netId:{0} for RPC call {1}", new object[]
{
networkInstanceId,
cmdHashHandlerName
});
}
}
private static void OnSyncEventMessage(QSBNetworkMessage netMsg)
{
2020-12-03 11:56:32 +00:00
var cmdHash = (int)netMsg.Reader.ReadPackedUInt32();
var networkInstanceId = netMsg.Reader.ReadNetworkId();
2020-12-02 12:42:26 +00:00
if (LogFilter.logDebug)
{
Debug.Log("ClientScene::OnSyncEventMessage " + networkInstanceId);
}
if (s_NetworkScene.GetNetworkIdentity(networkInstanceId, out var networkIdentity))
{
2020-12-03 11:56:32 +00:00
networkIdentity.HandleSyncEvent(cmdHash, netMsg.Reader);
2020-12-02 12:42:26 +00:00
}
else if (LogFilter.logWarn)
{
Debug.LogWarning("Did not find target for SyncEvent message for " + networkInstanceId);
}
}
private static void OnSyncListMessage(QSBNetworkMessage netMsg)
{
2020-12-03 11:56:32 +00:00
var networkInstanceId = netMsg.Reader.ReadNetworkId();
var cmdHash = (int)netMsg.Reader.ReadPackedUInt32();
2020-12-02 12:42:26 +00:00
if (LogFilter.logDebug)
{
Debug.Log("ClientScene::OnSyncListMessage " + networkInstanceId);
}
if (s_NetworkScene.GetNetworkIdentity(networkInstanceId, out var networkIdentity))
{
2020-12-03 11:56:32 +00:00
networkIdentity.HandleSyncList(cmdHash, netMsg.Reader);
2020-12-02 12:42:26 +00:00
}
else if (LogFilter.logWarn)
{
Debug.LogWarning("Did not find target for SyncList message for " + networkInstanceId);
}
}
private static void OnClientAuthority(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBClientAuthorityMessage>(s_ClientAuthorityMessage);
if (LogFilter.logDebug)
{
Debug.Log(string.Concat(new object[]
{
"ClientScene::OnClientAuthority for connectionId=",
2020-12-03 11:56:32 +00:00
netMsg.Connection.connectionId,
2020-12-02 12:42:26 +00:00
" netId: ",
s_ClientAuthorityMessage.netId
}));
}
if (s_NetworkScene.GetNetworkIdentity(s_ClientAuthorityMessage.netId, out var networkIdentity))
{
networkIdentity.HandleClientAuthority(s_ClientAuthorityMessage.authority);
}
}
private static void OnOwnerMessage(QSBNetworkMessage netMsg)
{
netMsg.ReadMessage<QSBOwnerMessage>(s_OwnerMessage);
if (LogFilter.logDebug)
{
Debug.Log(string.Concat(new object[]
{
"ClientScene::OnOwnerMessage - connectionId=",
2020-12-03 11:56:32 +00:00
netMsg.Connection.connectionId,
2020-12-02 12:42:26 +00:00
" netId: ",
2020-12-03 11:56:32 +00:00
s_OwnerMessage.NetId
2020-12-02 12:42:26 +00:00
}));
}
2020-12-03 11:56:32 +00:00
if (netMsg.Connection.GetPlayerController(s_OwnerMessage.PlayerControllerId, out var playerController))
2020-12-02 12:42:26 +00:00
{
2020-12-03 11:56:32 +00:00
playerController.UnetView.SetNotLocalPlayer();
2020-12-02 12:42:26 +00:00
}
2020-12-03 11:56:32 +00:00
if (s_NetworkScene.GetNetworkIdentity(s_OwnerMessage.NetId, out var networkIdentity))
2020-12-02 12:42:26 +00:00
{
2020-12-03 11:56:32 +00:00
networkIdentity.SetConnectionToServer(netMsg.Connection);
networkIdentity.SetLocalPlayer(s_OwnerMessage.PlayerControllerId);
InternalAddPlayer(networkIdentity, s_OwnerMessage.PlayerControllerId);
2020-12-02 12:42:26 +00:00
}
else
{
var item = new PendingOwner
{
2020-12-03 11:56:32 +00:00
netId = s_OwnerMessage.NetId,
playerControllerId = s_OwnerMessage.PlayerControllerId
2020-12-02 12:42:26 +00:00
};
s_PendingOwnerIds.Add(item);
}
}
private static void CheckForOwner(QSBNetworkIdentity uv)
{
var i = 0;
while (i < s_PendingOwnerIds.Count)
{
var pendingOwner = s_PendingOwnerIds[i];
if (pendingOwner.netId == uv.NetId)
{
uv.SetConnectionToServer(readyConnection);
uv.SetLocalPlayer(pendingOwner.playerControllerId);
Debug.Log("ClientScene::OnOwnerMessage - player=" + uv.gameObject.name);
if (readyConnection.connectionId < 0)
{
if (LogFilter.logError)
{
Debug.LogError("Owner message received on a local client.");
}
break;
}
InternalAddPlayer(uv, pendingOwner.playerControllerId);
s_PendingOwnerIds.RemoveAt(i);
break;
}
else
{
i++;
}
}
}
private static bool s_IsSpawnFinished;
private static QSBNetworkScene s_NetworkScene = new QSBNetworkScene();
private static QSBObjectSpawnSceneMessage s_ObjectSpawnSceneMessage = new QSBObjectSpawnSceneMessage();
private static QSBObjectSpawnFinishedMessage s_ObjectSpawnFinishedMessage = new QSBObjectSpawnFinishedMessage();
private static QSBObjectDestroyMessage s_ObjectDestroyMessage = new QSBObjectDestroyMessage();
private static QSBObjectSpawnMessage s_ObjectSpawnMessage = new QSBObjectSpawnMessage();
private static QSBOwnerMessage s_OwnerMessage = new QSBOwnerMessage();
private static QSBClientAuthorityMessage s_ClientAuthorityMessage = new QSBClientAuthorityMessage();
public const int ReconnectIdInvalid = -1;
public const int ReconnectIdHost = 0;
private static List<PendingOwner> s_PendingOwnerIds = new List<PendingOwner>();
private struct PendingOwner
{
2020-12-16 08:57:15 +00:00
public NetworkInstanceId netId;
2020-12-02 12:42:26 +00:00
public short playerControllerId;
}
}
}