private static bool InternalDestroySerializedObject([NotNull] QNetObjectBehaviour obj)
 {
     if (obj == null)
     {
         throw new ArgumentNullException(nameof(obj));
     }
     if (InternalSerializedAndInstancedObjects.Contains(obj))
     {
         InternalSerializedAndInstancedObjects.Remove(obj);
     }
     QNetObjectBehaviour.InternalDestroy(obj);
     return(true);
 }
        private IEnumerator InternalLoadServerLevelOnFly(string levelName)
        {
            JEMLogger.Log($"QNetUnity is loading map '{levelName}' on fly.");
            GameIsDeInitializing = true;
            GameIsInitializing   = true;
            GameInitialized      = false;

            QNetManager.Server.AcceptNewConnections = false;
            for (var index = 0; index < QNetPlayer.QNetPlayers.Length; index++)
            {
                var p = QNetPlayer.QNetPlayers[index];
                p.Loaded = false;
            }

            LastMapState = QNetMapState.Unloading;
            OnMapStateChanged?.Invoke(LastMapState);

            // destroy players
            yield return(QNetPlayer.DestroyAllQNetPlayers());

            // activate loading screen
            OnClientLoadingInfoUpdated?.Invoke(true, "UNLOADING WORLD",
                                               $"Destroying {QNetWorldSerializer.SerializedAndInstancedObjects.Count} objects.");

            // destroy world objects
            var isDestroyingWorldObjects = true;

            QNetWorldSerializer.DestroySerializedObjects(() => { isDestroyingWorldObjects = false; });
            while (isDestroyingWorldObjects)
            {
                yield return(new WaitForEndOfFrame());
            }

            // try to destroy rest of QNet objects just for sure
            if (QNetObjectBehaviour.SpawnedBehaviours.Length != 0)
            {
                JEMLogger.Log(
                    $"QNetUnity find and will destroy {QNetObjectBehaviour.SpawnedBehaviours.Length} additional objects that has been created not by QNetWorldSerializer.");
            }

            while (QNetObjectBehaviour.SpawnedBehaviours.Length > 0)
            {
                QNetObjectBehaviour.InternalDestroy(QNetObjectBehaviour.SpawnedBehaviours[0]);
                yield return(new WaitForEndOfFrame());
            }

            // clear behaviours just for sure
            QNetObjectBehaviour.ClearBehaviours();

            // activate loading screen
            OnClientLoadingInfoUpdated?.Invoke(true, "LOADING LEVEL", "Loading level.");

            LastMapState = QNetMapState.Loading;
            OnMapStateChanged?.Invoke(LastMapState);

            var isLevelLoading = true;

            QNetLevelLoader.Load(levelName, () => { isLevelLoading = false; });
            while (isLevelLoading)
            {
                yield return(new WaitForEndOfFrame());
            }

            LastMapState = QNetMapState.Loaded;
            OnMapStateChanged?.Invoke(LastMapState);

            if (QNetManager.IsServerActive)
            {
                // we are on server!
                // here, we need to send level change info to all clients
                //var writer = QNetManager.Server.GenerateOutgoingMessage((ushort)QNetUnityLocalHeader.LEVEL_LOAD_ON_FLY);
                //writer.WriteString(QNetLevelLoader.LevelName);
                //QNetManager.Server.SendToAll(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered, writer);
            }

            GameIsDeInitializing = false;
            GameIsInitializing   = false;
            GameInitialized      = true;
            QNetManager.Server.AcceptNewConnections = true;

            JEMLogger.Log($"QNetUnity has loaded map '{levelName}' on fly.");
        }
        private static IEnumerator InternalDeInitialize(Action onDone)
        {
            var sw = Stopwatch.StartNew();

            // the initialize client
            OnUnloadClientSideContent?.Invoke();

            // activate loading screen
            OnClientLoadingInfoUpdated?.Invoke(true, "UNLOADING WORLD", "Destroying players.");
            OnClientLoadingStart?.Invoke();

            LastMapState = QNetMapState.Unloading;
            OnMapStateChanged?.Invoke(LastMapState);

            // destroy players
            yield return(QNetPlayer.DestroyAllQNetPlayers());

            // activate loading screen
            OnClientLoadingInfoUpdated?.Invoke(true, "UNLOADING WORLD",
                                               $"Destroying {QNetWorldSerializer.SerializedAndInstancedObjects.Count} objects.");

            // destroy world objects
            var isDestroyingWorldObjects = true;

            QNetWorldSerializer.DestroySerializedObjects(() => { isDestroyingWorldObjects = false; });
            while (isDestroyingWorldObjects)
            {
                yield return(new WaitForEndOfFrame());
            }

            // try to destroy rest of QNet objects just for sure
            if (QNetObjectBehaviour.SpawnedBehaviours.Length != 0)
            {
                JEMLogger.Log(
                    $"QNetUnity find and will destroy {QNetObjectBehaviour.SpawnedBehaviours.Length} additional objects that has been created not by QNetWorldSerializer.");
            }

            while (QNetObjectBehaviour.SpawnedBehaviours.Length > 0)
            {
                QNetObjectBehaviour.InternalDestroy(QNetObjectBehaviour.SpawnedBehaviours[0]);
                yield return(new WaitForEndOfFrame());
            }

            // activate loading screen
            OnClientLoadingInfoUpdated?.Invoke(true, "UNLOADING LEVEL", "Unloading level.");

            // unload world
            var isLevelUnLoading = true;

            QNetLevelLoader.UnLoad(() => { isLevelUnLoading = false; });
            while (isLevelUnLoading)
            {
                yield return(new WaitForEndOfFrame());
            }

            // clear behaviours just for sure
            QNetObjectBehaviour.ClearBehaviours();

            LastMapState = QNetMapState.NotLoaded;
            OnMapStateChanged?.Invoke(LastMapState);

            GameIsDeInitializing = false;
            GameInitialized      = false;
            OnClientLoadingEnd?.Invoke();

            JEMLogger.Log($"QNetUnity DeInitialization main work took {sw.Elapsed.Milliseconds:0.00}ms.");
            onDone?.Invoke();
        }