Example #1
0
        public static void Main()
        {
            try
            {
                //Start the server clock
                serverClock = new Stopwatch();
                serverClock.Start();

                //Set the last player activity time to server start
                lastPlayerActivity = serverClock.ElapsedMilliseconds;

                //Periodic garbage collection
                long lastGarbageCollect = 0;

                //Periodic screenshot check
                long lastScreenshotExpiredCheck = 0;

                //Set universe directory and modfile path
                universeDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Universe");
                modFile           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DMPModControl.txt");

                CommandHandler commandHandler = new CommandHandler();

                //Register the server commands
                commandHandler.RegisterCommand("exit", Server.ShutDown, "Shuts down the server");
                commandHandler.RegisterCommand("quit", Server.ShutDown, "Shuts down the server");
                commandHandler.RegisterCommand("shutdown", Server.ShutDown, "Shuts down the server");
                commandHandler.RegisterCommand("restart", Server.Restart, "Restarts the server");
                commandHandler.RegisterCommand("kick", x => WorldManager.Instance.KickPlayer(x), "Kicks a player from the server");
                commandHandler.RegisterCommand("ban", x => WorldManager.Instance.BanPlayer(x), "Bans a player from the server");
                commandHandler.RegisterCommand("banip", x => WorldManager.Instance.BanIP(x), "Bans an IP Address from the server");
                commandHandler.RegisterCommand("bankey", x => WorldManager.Instance.BanPublicKey(x), "Bans a Guid from the server");
                commandHandler.RegisterCommand("pm", x => WorldManager.Instance.PMCommand(x), "Sends a message to a player");
                commandHandler.RegisterCommand("admin", x => WorldManager.Instance.AdminCommand(x), "Sets a player as admin/removes admin from the player");
                commandHandler.RegisterCommand("whitelist", x => WorldManager.Instance.WhitelistCommand(x), "Change the server whitelist");
                //Register the ctrl+c event
                Console.CancelKeyPress += new ConsoleCancelEventHandler(CatchExit);
                serverStarting          = true;

                //Fix kerbals from 0.23.5 to 0.24 (Now indexed by string, thanks Squad!
                BackwardsCompatibility.FixKerbals();

                //Remove player tokens
                BackwardsCompatibility.RemoveOldPlayerTokens();

                //Load plugins
                DMPPluginHandler.LoadPlugins();

                Console.Title = "DMPServer " + Common.PROGRAM_VERSION + ", protocol " + Common.PROTOCOL_VERSION;

                while (serverStarting || serverRestarting)
                {
                    serverRestarting = false;
                    DarkLog.Normal("Starting DMPServer " + Common.PROGRAM_VERSION + ", protocol " + Common.PROTOCOL_VERSION);

                    //Load settings
                    DarkLog.Normal("Loading universe... ");
                    CheckUniverse();
                    DarkLog.Normal("Done!");

                    DarkLog.Normal("Loading settings... ");
                    Settings.Load();
                    DarkLog.Normal("Done!");

                    DarkLog.Normal("Starting " + Settings.settingsStore.warpMode + " server on port " + Settings.settingsStore.port + "... ");



                    serverRunning = true;
                    DarkLog.Normal("Done!");

                    StartHTTPServer();
                    DarkLog.Normal("Done!");
                    DMPPluginHandler.FireOnServerStart();
                    while (serverRunning)
                    {
                        WorldManager.Instance.Update();
                        commandHandler.Run();

                        //Run a garbage collection every 2 minutes.
                        if ((serverClock.ElapsedMilliseconds - lastGarbageCollect) > 12000)
                        {
                            lastGarbageCollect = serverClock.ElapsedMilliseconds;
                            GC.Collect();
                        }
                        //Run the screenshot expire function every 10 minutes
                        if ((serverClock.ElapsedMilliseconds - lastScreenshotExpiredCheck) > 600000)
                        {
                            lastScreenshotExpiredCheck = serverClock.ElapsedMilliseconds;
                            ScreenshotExpire.ExpireCache();
                        }
                        Thread.Sleep(10);
                    }
                    DMPPluginHandler.FireOnServerStop();
                }
                DarkLog.Normal("Goodbye!");
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                DarkLog.Fatal("Error in main server thread, Exception: " + e);
                throw;
            }
        }