public virtual bool AddCacheLookup(NodeNetworkSceneTemplate pNodeTemplate) { if (pNodeTemplate == null) { return(false); } return(_scenesCached.Add(pNodeTemplate.SceneName, pNodeTemplate)); }
protected virtual void OnLookupScene_Server(RpcArgs pArgs) { byte[] data = pArgs.GetNext <byte[]>(); RPCServiceCallback callbackRPC = data.ByteArrayToObject <RPCServiceCallback>(); string sceneName = callbackRPC.data.ByteArrayToObject <string>(); NodeNetworkSceneTemplate nodeTemplate = (GetRegisteredScene(sceneName) ?? new NodeNetworkSceneTemplate(0, -1, String.Empty, RPCVector3.zero, null)); callbackRPC.data = nodeTemplate.ToByteArray(); networkObject.SendRpc(pArgs.Info.SendingPlayer, RPC_RECEIVE_LOOKUP_SCENE, nodeTemplate.NodeId, callbackRPC.ObjectToByteArray()); }
public virtual void FindNetworkSceneTemplate(string pSceneName) { if (!NodeManager.IsInitialized || !NodeManager.Instance.IsServer || _currentLookup != null || !_findSceneDelay.HasPassed) { return; } // Lookup if the scene resides on our Node if (_nodeTemplate == null) { NetworkSceneTemplate foundTemplate = NodeManager.Instance.FindNetworkSceneTemplate(pSceneName, false, true, true, false); if (foundTemplate != null) { _nodeTemplate = new NodeNetworkSceneTemplate(NodeManager.Instance.CurrentNode.NodeId, foundTemplate); return; } } // Lookup the NodeMap if there is a static NetworkScene we can find on another Node if (_nodeTemplate == null && NodeManager.Instance.NodeMapSO != null) { _nodeTemplate = NodeManager.Instance.NodeMapSO.nodeMap.GetNodeTemplateBySceneName(pSceneName); if (_nodeTemplate != null) { return; } } // Lookup if the scene is a dynamic NetworkScene on another Node if (_nodeTemplate == null && _enableLookup && _currentLookup == null) { _currentLookup = NodeManager.Instance.LookUpNetworkSceneTemplate(pSceneName); if (_currentLookup.State == ServiceCallbackStateEnum.RESPONSE_SUCCESS) { _nodeTemplate = _currentLookup.ResponseDataOfT; _currentLookup = null; } else if (_currentLookup.State == ServiceCallbackStateEnum.AWAITING_RESPONSE) { _currentLookup.OnResponseOfT += CurrentLookup_OnResponseOfT; _currentLookup.OnTimeout += CurrentLookup_OnTimeout; } else { _pendingObjects.Clear(); _currentLookup = null; } } _findSceneDelay.Start(_findSceneInterval); }
public virtual void InstantiatePendingObject(NodeNetworkSceneTemplate pNodeTemplate, NetworkBehavior pBehavior) { if (pNodeTemplate == null || pBehavior == null) { return; } INetworkSceneObject networkSceneObject = pBehavior as INetworkSceneObject; NetworkObject nObj = networkSceneObject.GetNetworkObject(); if (nObj == null) { return; } if (pNodeTemplate.NodeId == NodeManager.Instance.CurrentNode.NodeId) { NetworkBehavior behavior = NodeManager.Instance.InstantiateInScene(pNodeTemplate.SceneName, nObj.CreateCode, (pBehavior as IRPCSerializable), _teleportTarget.position, _teleportTarget.rotation); if (behavior == null) { return; } pBehavior.gameObject.SetActive(false); SendNetworkSceneToPlayer(networkSceneObject, pNodeTemplate); DestroyPendingObject(networkSceneObject); } else { ServiceCallback <RPCInstantiateInNode, ServiceCallbackStateEnum> callback = NodeManager.Instance.InstantiateInNode(pNodeTemplate.NodeId, pNodeTemplate.SceneName, nObj.CreateCode, (pBehavior as IRPCSerializable), _teleportTarget.position, _teleportTarget.rotation); if (callback.State == ServiceCallbackStateEnum.AWAITING_RESPONSE) { // While the request is progressing this NetworkSceneTeleporter could have been destroyed. NetworkSceneTeleporter tmpTeleporter = this; callback.OnResponseOfT += (pResponseTime, pResponseDataOfT, pSender) => { if (tmpTeleporter == null || pBehavior == null || networkSceneObject == null || pNodeTemplate == null) { return; } if (pResponseDataOfT == ServiceCallbackStateEnum.RESPONSE_SUCCESS) { pBehavior.gameObject.SetActive(false); tmpTeleporter.SendNetworkSceneToPlayer(networkSceneObject, pNodeTemplate); tmpTeleporter.DestroyPendingObject(networkSceneObject); } }; } } }
public virtual void TeleportPendingObjects(NodeNetworkSceneTemplate pNodeTemplate) { if (pNodeTemplate == null || !NodeManager.IsInitialized || !NodeManager.Instance.IsServer) { return; } foreach (var item in _pendingObjects.Values) { InstantiatePendingObject(pNodeTemplate, item); } _pendingObjects.Clear(); }
public static ServiceCallback <string, NodeNetworkSceneTemplate> LookupScene(uint pSourceNodeId, string pSceneName) { ServiceCallback <string, NodeNetworkSceneTemplate> callback = new ServiceCallback <string, NodeNetworkSceneTemplate>(0, pSourceNodeId, null, GameTime.fixedTime); if (string.IsNullOrEmpty(pSceneName)) { callback.State = ServiceCallbackStateEnum.ERROR_NO_DATA; return(callback); } if (!IsInitialized) { callback.State = ServiceCallbackStateEnum.ERROR_SERVICE_NOT_INITIALIZED; return(callback); } if (Instance.Manager == null || !Instance.Manager.HasNetworker || !Instance.Manager.Networker.IsConnected) { callback.State = ServiceCallbackStateEnum.ERROR_NO_CONNECTION; return(callback); } callback.RequestDataOfT = pSceneName; if (Instance.EnableLookupCaching) { NodeNetworkSceneTemplate nodeTemplate = Instance.GetCachedLookup(pSceneName); if (nodeTemplate != null) { callback.State = ServiceCallbackStateEnum.RESPONSE_SUCCESS; callback.ResponseTime = GameTime.fixedTime; callback.ResponseDataOfT = nodeTemplate; return(callback); } } callback.State = ServiceCallbackStateEnum.AWAITING_RESPONSE; Instance.AddPendingServiceCallback(callback); MainThreadManager.Run(() => { if (!IsInitialized || Instance.networkObject == null) { return; } Instance.networkObject.SendRpc(RPC_LOOKUP_SCENE, Receivers.Server, callback.ToByteArray()); }); return(callback); }
protected virtual void OnRegisterScene_Server(RpcArgs pArgs) { bool requireConfirmation = pArgs.GetNext <bool>(); byte[] data = pArgs.GetNext <byte[]>(); RPCServiceCallback callbackRPC = data.ByteArrayToObject <RPCServiceCallback>(); RPCNetworkSceneTemplate templateRPC = callbackRPC.data.ByteArrayToObject <RPCNetworkSceneTemplate>(); NodeNetworkSceneTemplate registeredTemplate = GetRegisteredScene(templateRPC.sceneName); bool addSuccess = (registeredTemplate != null && registeredTemplate.NodeId == callbackRPC.sourceNodeId) || AddRegisteredScene(callbackRPC.sourceNodeId, new NodeNetworkSceneTemplate(callbackRPC.sourceNodeId, templateRPC)); if (requireConfirmation) { //Only the callbackRPC.state is important for RPC_CONFIRM callbackRPC.state = (addSuccess) ? ServiceCallbackStateEnum.RESPONSE_SUCCESS : ServiceCallbackStateEnum.RESPONSE_FAILED; callbackRPC.data = null; networkObject.SendRpc(pArgs.Info.SendingPlayer, RPC_CONFIRM_SCENE, callbackRPC.ObjectToByteArray()); } }
protected virtual void CurrentLookup_OnResponseOfT(float pResponseTime, NodeNetworkSceneTemplate pResponseDataOfT, ServiceCallback <NodeNetworkSceneTemplate> pCallback) { _currentLookup.OnResponseOfT -= CurrentLookup_OnResponseOfT; _currentLookup.OnTimeout -= CurrentLookup_OnTimeout; _currentLookup = null; // Only assign our result if there has been nothing found in the meantime! if (_nodeTemplate == null && pCallback.State == ServiceCallbackStateEnum.RESPONSE_SUCCESS) { _nodeTemplate = pResponseDataOfT; } if (_nodeTemplate == null) { _pendingObjects.Clear(); } else { TeleportPendingObjects(_nodeTemplate); } }
public virtual bool AddRegisteredScene(uint pNodeId, NodeNetworkSceneTemplate pNodeTemplate) { if (pNodeTemplate == null) { return(false); } Dictionary <string, NodeNetworkSceneTemplate> templates; if (!_scenesRegistered.TryGetValue(pNodeId, out templates)) { templates = new Dictionary <string, NodeNetworkSceneTemplate>(); _scenesRegistered.Add(pNodeId, templates); } if (templates.ContainsKey(pNodeTemplate.SceneName)) { return(false); } templates.Add(pNodeTemplate.SceneName, pNodeTemplate); return(true); }
protected virtual void OnReceiveLookupScene_ServerClient(RpcArgs pArgs) { uint nodeId = pArgs.GetNext <uint>(); byte[] data = pArgs.GetNext <byte[]>(); RPCServiceCallback callbackRPC = data.ByteArrayToObject <RPCServiceCallback>(); RPCNetworkSceneTemplate templateRPC = callbackRPC.data.ByteArrayToObject <RPCNetworkSceneTemplate>(); NodeNetworkSceneTemplate nodeTemplate = new NodeNetworkSceneTemplate(nodeId, templateRPC); ServiceCallback callback; if (!_pendingServiceCallbacks.TryGetValue(callbackRPC.callbackId, out callback)) { return; } if (_enableLookupCaching && nodeTemplate.BuildIndex >= 0) { AddCacheLookup(nodeTemplate); } RemovePendingServiceCallback(callback.CallbackId); callback.RaiseResponse(GameTime.fixedTime, nodeTemplate.ObjectToByteArray()); }
public virtual bool SendNetworkSceneToPlayer(INetworkSceneObject pNetworkSceneObject, NodeNetworkSceneTemplate pNodeTemplate) { if (pNetworkSceneObject == null || pNetworkSceneObject.Manager == null || pNodeTemplate == null) { return(false); } INetworkScenePlayer networkScenePlayer = pNetworkSceneObject as INetworkScenePlayer; if (networkScenePlayer == null) { return(false); } return(pNetworkSceneObject.Manager.ChangePlayerNetworkScene(pNodeTemplate, networkScenePlayer.Player)); }