Exemple #1
0
        static void ShutdownThread(bool restarting, string msg)
        {
            try {
                Logger.Log(LogType.SystemActivity, "Server shutting down ({0})", msg);
            } catch { }

            // Stop accepting new connections and disconnect existing sessions
            try {
                if (Listener != null)
                {
                    Listener.Close();
                }
            } catch (Exception ex) { Logger.LogError(ex); }

            try {
                Player[] players = PlayerInfo.Online.Items;
                foreach (Player p in players)
                {
                    p.Leave(msg);
                }
            } catch (Exception ex) { Logger.LogError(ex); }

            byte[] kick = Packet.Kick(msg, false);
            try {
                INetSocket[] pending = INetSocket.pending.Items;
                foreach (INetSocket p in pending)
                {
                    p.Send(kick, false);
                }
            } catch (Exception ex) { Logger.LogError(ex); }

            Plugin.UnloadAll();
            OnShuttingDownEvent.Call(restarting, msg);

            try {
                string  autoload = null;
                Level[] loaded   = LevelInfo.Loaded.Items;
                foreach (Level lvl in loaded)
                {
                    if (!lvl.SaveChanges)
                    {
                        continue;
                    }

                    autoload = autoload + lvl.name + "=" + lvl.physics + Environment.NewLine;
                    lvl.Save(false, true);
                    lvl.SaveBlockDBChanges();
                }

                if (Server.SetupFinished && !Server.Config.AutoLoadMaps)
                {
                    File.WriteAllText("text/autoload.txt", autoload);
                }
            } catch (Exception ex) { Logger.LogError(ex); }

            try {
                Logger.Log(LogType.SystemActivity, "Server shutdown completed");
            } catch { }

            try { FileLogger.Flush(null); } catch { }
            if (restarting)
            {
                Process.Start(RestartPath);
            }
            Environment.Exit(0);
        }