public void Link(ulong networkId) { //this.NetworkId = networkId; SpawnManager.SpawnNetworkedObjectLocally(this, networkId, false, false, 0, null, false, 0, false, false); /*if (NetworkingManager.Singleton.IsServer) * SpawnManager.SpawnedObjects[NetworkId] = this;*/ Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkingManager.Singleton.HandleIncomingData(message.sender, message.channelName, new ArraySegment <byte>(message.payload.GetBuffer(), (int)message.payload.Position, (int)message.payload.Length), message.receiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } }
internal static void HandleConnectionApproved(ulong clientId, Stream stream, float receiveTime) { #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleConnectionApproved.Begin(); #endif using (var reader = PooledNetworkReader.Get(stream)) { NetworkManager.Singleton.LocalClientId = reader.ReadUInt64Packed(); uint sceneIndex = 0; Guid sceneSwitchProgressGuid = new Guid(); if (NetworkManager.Singleton.NetworkConfig.EnableSceneManagement) { sceneIndex = reader.ReadUInt32Packed(); sceneSwitchProgressGuid = new Guid(reader.ReadByteArray()); } bool sceneSwitch = NetworkManager.Singleton.NetworkConfig.EnableSceneManagement && NetworkSceneManager.HasSceneMismatch(sceneIndex); float netTime = reader.ReadSinglePacked(); NetworkManager.Singleton.UpdateNetworkTime(clientId, netTime, receiveTime, true); NetworkManager.Singleton.ConnectedClients.Add(NetworkManager.Singleton.LocalClientId, new NetworkClient { ClientId = NetworkManager.Singleton.LocalClientId }); void DelayedSpawnAction(Stream continuationStream) { using (var continuationReader = PooledNetworkReader.Get(continuationStream)) { if (!NetworkManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkManager.Singleton.NetworkConfig.UsePrefabSync) { NetworkSpawnManager.DestroySceneObjects(); } else { NetworkSpawnManager.ClientCollectSoftSyncSceneObjectSweep(null); } uint objectCount = continuationReader.ReadUInt32Packed(); for (int i = 0; i < objectCount; i++) { bool isPlayerObject = continuationReader.ReadBool(); ulong networkId = continuationReader.ReadUInt64Packed(); ulong ownerId = continuationReader.ReadUInt64Packed(); bool hasParent = continuationReader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = continuationReader.ReadUInt64Packed(); } ulong prefabHash; ulong instanceId; bool softSync; if (!NetworkManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkManager.Singleton.NetworkConfig.UsePrefabSync) { softSync = false; instanceId = 0; prefabHash = continuationReader.ReadUInt64Packed(); } else { softSync = continuationReader.ReadBool(); if (softSync) { instanceId = continuationReader.ReadUInt64Packed(); prefabHash = 0; } else { prefabHash = continuationReader.ReadUInt64Packed(); instanceId = 0; } } Vector3? pos = null; Quaternion?rot = null; if (continuationReader.ReadBool()) { pos = new Vector3(continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked()); rot = Quaternion.Euler(continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked()); } var networkObject = NetworkSpawnManager.CreateLocalNetworkObject(softSync, instanceId, prefabHash, parentNetworkId, pos, rot); NetworkSpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, softSync, isPlayerObject, ownerId, continuationStream, false, 0, true, false); Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkManager.Singleton.HandleIncomingData(message.SenderClientId, message.NetworkChannel, new ArraySegment <byte>(message.NetworkBuffer.GetBuffer(), (int)message.NetworkBuffer.Position, (int)message.NetworkBuffer.Length), message.ReceiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } NetworkSpawnManager.CleanDiffedSceneObjects(); NetworkManager.Singleton.IsConnectedClient = true; NetworkManager.Singleton.InvokeOnClientConnectedCallback(NetworkManager.Singleton.LocalClientId); } } if (sceneSwitch) { UnityAction <Scene, Scene> onSceneLoaded = null; var continuationBuffer = new NetworkBuffer(); continuationBuffer.CopyUnreadFrom(stream); continuationBuffer.Position = 0; void OnSceneLoadComplete() { SceneManager.activeSceneChanged -= onSceneLoaded; NetworkSceneManager.IsSpawnedObjectsPendingInDontDestroyOnLoad = false; DelayedSpawnAction(continuationBuffer); } onSceneLoaded = (oldScene, newScene) => { OnSceneLoadComplete(); }; SceneManager.activeSceneChanged += onSceneLoaded; NetworkSceneManager.OnFirstSceneSwitchSync(sceneIndex, sceneSwitchProgressGuid); } else { DelayedSpawnAction(stream); } } #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleConnectionApproved.End(); #endif }
internal static void HandleAddObject(ulong clientId, Stream stream) { #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleAddObject.Begin(); #endif using (var reader = PooledNetworkReader.Get(stream)) { bool isPlayerObject = reader.ReadBool(); ulong networkId = reader.ReadUInt64Packed(); ulong ownerId = reader.ReadUInt64Packed(); bool hasParent = reader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = reader.ReadUInt64Packed(); } ulong prefabHash; ulong instanceId; bool softSync; if (!NetworkManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkManager.Singleton.NetworkConfig.UsePrefabSync) { softSync = false; instanceId = 0; prefabHash = reader.ReadUInt64Packed(); } else { softSync = reader.ReadBool(); if (softSync) { instanceId = reader.ReadUInt64Packed(); prefabHash = 0; } else { prefabHash = reader.ReadUInt64Packed(); instanceId = 0; } } Vector3? pos = null; Quaternion?rot = null; if (reader.ReadBool()) { pos = new Vector3(reader.ReadSinglePacked(), reader.ReadSinglePacked(), reader.ReadSinglePacked()); rot = Quaternion.Euler(reader.ReadSinglePacked(), reader.ReadSinglePacked(), reader.ReadSinglePacked()); } bool hasPayload = reader.ReadBool(); int payLoadLength = hasPayload ? reader.ReadInt32Packed() : 0; var networkObject = NetworkSpawnManager.CreateLocalNetworkObject(softSync, instanceId, prefabHash, parentNetworkId, pos, rot); NetworkSpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, softSync, isPlayerObject, ownerId, stream, hasPayload, payLoadLength, true, false); Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkManager.Singleton.HandleIncomingData(message.SenderClientId, message.NetworkChannel, new ArraySegment <byte>(message.NetworkBuffer.GetBuffer(), (int)message.NetworkBuffer.Position, (int)message.NetworkBuffer.Length), message.ReceiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleAddObject.End(); #endif }
internal static void HandleConnectionApproved(ulong clientId, Stream stream, float receiveTime) { using (PooledBitReader reader = PooledBitReader.Get(stream)) { NetworkingManager.Singleton.LocalClientId = reader.ReadUInt64Packed(); uint sceneIndex = 0; Guid sceneSwitchProgressGuid = new Guid(); if (NetworkingManager.Singleton.NetworkConfig.EnableSceneManagement) { sceneIndex = reader.ReadUInt32Packed(); sceneSwitchProgressGuid = new Guid(reader.ReadByteArray()); } bool sceneSwitch = NetworkingManager.Singleton.NetworkConfig.EnableSceneManagement && NetworkSceneManager.HasSceneMismatch(sceneIndex); float netTime = reader.ReadSinglePacked(); NetworkingManager.Singleton.UpdateNetworkTime(clientId, netTime, receiveTime, true); NetworkingManager.Singleton.ConnectedClients.Add(NetworkingManager.Singleton.LocalClientId, new NetworkedClient() { ClientId = NetworkingManager.Singleton.LocalClientId }); void DelayedSpawnAction(Stream continuationStream) { using (PooledBitReader continuationReader = PooledBitReader.Get(continuationStream)) { if (!NetworkingManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkingManager.Singleton.NetworkConfig.UsePrefabSync) { SpawnManager.DestroySceneObjects(); } else { SpawnManager.ClientCollectSoftSyncSceneObjectSweep(null); } uint objectCount = continuationReader.ReadUInt32Packed(); for (int i = 0; i < objectCount; i++) { bool isPlayerObject = continuationReader.ReadBool(); ulong networkId = continuationReader.ReadUInt64Packed(); ulong ownerId = continuationReader.ReadUInt64Packed(); bool hasParent = continuationReader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = continuationReader.ReadUInt64Packed(); } ulong prefabHash; ulong instanceId; bool softSync; if (!NetworkingManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkingManager.Singleton.NetworkConfig.UsePrefabSync) { softSync = false; instanceId = 0; prefabHash = continuationReader.ReadUInt64Packed(); } else { softSync = continuationReader.ReadBool(); if (softSync) { instanceId = continuationReader.ReadUInt64Packed(); prefabHash = 0; } else { prefabHash = continuationReader.ReadUInt64Packed(); instanceId = 0; } } Vector3? pos = null; Quaternion?rot = null; if (continuationReader.ReadBool()) { pos = new Vector3(continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked()); rot = Quaternion.Euler(continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked()); } NetworkedObject netObject = SpawnManager.CreateLocalNetworkedObject(softSync, instanceId, prefabHash, parentNetworkId, pos, rot); SpawnManager.SpawnNetworkedObjectLocally(netObject, networkId, softSync, isPlayerObject, ownerId, continuationStream, false, 0, true, false); Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkingManager.Singleton.HandleIncomingData(message.sender, message.channelName, new ArraySegment <byte>(message.payload.GetBuffer(), (int)message.payload.Position, (int)message.payload.Length), message.receiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } // Clean up the diffed scene objects. I.E scene objects that have been destroyed if (SpawnManager.pendingSoftSyncObjects.Count > 0) { List <NetworkedObject> objectsToDestroy = new List <NetworkedObject>(); foreach (KeyValuePair <ulong, NetworkedObject> pair in SpawnManager.pendingSoftSyncObjects) { objectsToDestroy.Add(pair.Value); } for (int i = 0; i < objectsToDestroy.Count; i++) { MonoBehaviour.Destroy(objectsToDestroy[i].gameObject); } } NetworkingManager.Singleton.IsConnectedClient = true; NetworkingManager.Singleton.InvokeOnClientConnectedCallback(NetworkingManager.Singleton.LocalClientId); } } if (sceneSwitch) { UnityAction <Scene, Scene> onSceneLoaded = null; Serialization.BitStream continuationStream = new Serialization.BitStream(); continuationStream.CopyUnreadFrom(stream); continuationStream.Position = 0; void OnSceneLoadComplete() { SceneManager.activeSceneChanged -= onSceneLoaded; NetworkSceneManager.isSpawnedObjectsPendingInDontDestroyOnLoad = false; DelayedSpawnAction(continuationStream); } onSceneLoaded = (oldScene, newScene) => { OnSceneLoadComplete(); }; SceneManager.activeSceneChanged += onSceneLoaded; NetworkSceneManager.OnFirstSceneSwitchSync(sceneIndex, sceneSwitchProgressGuid); } else { DelayedSpawnAction(stream); } } }
private static void OnSceneUnloadClient(Guid switchSceneGuid, Stream objectStream) { if (!NetworkManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkManager.Singleton.NetworkConfig.UsePrefabSync) { NetworkSpawnManager.DestroySceneObjects(); using (var reader = PooledNetworkReader.Get(objectStream)) { uint newObjectsCount = reader.ReadUInt32Packed(); for (int i = 0; i < newObjectsCount; i++) { bool isPlayerObject = reader.ReadBool(); ulong networkId = reader.ReadUInt64Packed(); ulong owner = reader.ReadUInt64Packed(); bool hasParent = reader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = reader.ReadUInt64Packed(); } ulong prefabHash = reader.ReadUInt64Packed(); Vector3? position = null; Quaternion?rotation = null; if (reader.ReadBool()) { position = new Vector3(reader.ReadSinglePacked(), reader.ReadSinglePacked(), reader.ReadSinglePacked()); rotation = Quaternion.Euler(reader.ReadSinglePacked(), reader.ReadSinglePacked(), reader.ReadSinglePacked()); } var networkObject = NetworkSpawnManager.CreateLocalNetworkObject(false, 0, prefabHash, parentNetworkId, position, rotation); NetworkSpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, true, isPlayerObject, owner, objectStream, false, 0, true, false); var bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkManager.Singleton.HandleIncomingData(message.SenderClientId, message.NetworkChannel, new ArraySegment <byte>(message.NetworkBuffer.GetBuffer(), (int)message.NetworkBuffer.Position, (int)message.NetworkBuffer.Length), message.ReceiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } } } else { var networkObjects = MonoBehaviour.FindObjectsOfType <NetworkObject>(); NetworkSpawnManager.ClientCollectSoftSyncSceneObjectSweep(networkObjects); using (var reader = PooledNetworkReader.Get(objectStream)) { uint newObjectsCount = reader.ReadUInt32Packed(); for (int i = 0; i < newObjectsCount; i++) { bool isPlayerObject = reader.ReadBool(); ulong networkId = reader.ReadUInt64Packed(); ulong owner = reader.ReadUInt64Packed(); bool hasParent = reader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = reader.ReadUInt64Packed(); } ulong instanceId = reader.ReadUInt64Packed(); var networkObject = NetworkSpawnManager.CreateLocalNetworkObject(true, instanceId, 0, parentNetworkId, null, null); NetworkSpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, true, isPlayerObject, owner, objectStream, false, 0, true, false); var bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkManager.Singleton.HandleIncomingData(message.SenderClientId, message.NetworkChannel, new ArraySegment <byte>(message.NetworkBuffer.GetBuffer(), (int)message.NetworkBuffer.Position, (int)message.NetworkBuffer.Length), message.ReceiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } } } using (var buffer = PooledNetworkBuffer.Get()) using (var writer = PooledNetworkWriter.Get(buffer)) { writer.WriteByteArray(switchSceneGuid.ToByteArray()); InternalMessageSender.Send(NetworkManager.Singleton.ServerClientId, NetworkConstants.CLIENT_SWITCH_SCENE_COMPLETED, NetworkChannel.Internal, buffer); } s_IsSwitching = false; OnSceneSwitched?.Invoke(); }
private static void OnSceneUnloadClient(Guid switchSceneGuid, Stream objectStream) { if (!NetworkingManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkingManager.Singleton.NetworkConfig.UsePrefabSync) { SpawnManager.DestroySceneObjects(); using (PooledBitReader reader = PooledBitReader.Get(objectStream)) { uint newObjectsCount = reader.ReadUInt32Packed(); for (int i = 0; i < newObjectsCount; i++) { bool isPlayerObject = reader.ReadBool(); ulong networkId = reader.ReadUInt64Packed(); ulong owner = reader.ReadUInt64Packed(); bool hasParent = reader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = reader.ReadUInt64Packed(); } ulong prefabHash = reader.ReadUInt64Packed(); Vector3? position = null; Quaternion?rotation = null; if (reader.ReadBool()) { position = new Vector3(reader.ReadSinglePacked(), reader.ReadSinglePacked(), reader.ReadSinglePacked()); rotation = Quaternion.Euler(reader.ReadSinglePacked(), reader.ReadSinglePacked(), reader.ReadSinglePacked()); } NetworkedObject networkedObject = SpawnManager.CreateLocalNetworkedObject(false, 0, prefabHash, parentNetworkId, position, rotation); SpawnManager.SpawnNetworkedObjectLocally(networkedObject, networkId, true, isPlayerObject, owner, objectStream, false, 0, true, false); Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkingManager.Singleton.HandleIncomingData(message.sender, message.channelName, new ArraySegment <byte>(message.payload.GetBuffer(), (int)message.payload.Position, (int)message.payload.Length), message.receiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } } } else { NetworkedObject[] networkedObjects = MonoBehaviour.FindObjectsOfType <NetworkedObject>(); SpawnManager.ClientCollectSoftSyncSceneObjectSweep(networkedObjects); using (PooledBitReader reader = PooledBitReader.Get(objectStream)) { uint newObjectsCount = reader.ReadUInt32Packed(); for (int i = 0; i < newObjectsCount; i++) { bool isPlayerObject = reader.ReadBool(); ulong networkId = reader.ReadUInt64Packed(); ulong owner = reader.ReadUInt64Packed(); bool hasParent = reader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = reader.ReadUInt64Packed(); } ulong instanceId = reader.ReadUInt64Packed(); NetworkedObject networkedObject = SpawnManager.CreateLocalNetworkedObject(true, instanceId, 0, parentNetworkId, null, null); SpawnManager.SpawnNetworkedObjectLocally(networkedObject, networkId, true, isPlayerObject, owner, objectStream, false, 0, true, false); Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkingManager.Singleton.HandleIncomingData(message.sender, message.channelName, new ArraySegment <byte>(message.payload.GetBuffer(), (int)message.payload.Position, (int)message.payload.Length), message.receiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } } } using (PooledBitStream stream = PooledBitStream.Get()) { using (PooledBitWriter writer = PooledBitWriter.Get(stream)) { writer.WriteByteArray(switchSceneGuid.ToByteArray()); NetworkedObject networkedObject = null; InternalMessageSender.Send(NetworkingManager.Singleton.ServerClientId, MLAPIConstants.MLAPI_CLIENT_SWITCH_SCENE_COMPLETED, "MLAPI_INTERNAL", stream, SecuritySendFlags.None, networkedObject); } } isSwitching = false; if (OnSceneSwitched != null) { OnSceneSwitched(); } }