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

            Settings.Reset();

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

            //Periodic garbage collection
            long lastGarbageCollect = 0;

            //Periodic screenshot check
            long lastScreenshotExpiredCheck = 0;

            //Periodic log check
            long lastLogExpiredCheck = 0;

            //Periodic day check
            long lastDayCheck = 0;

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

            if (!Directory.Exists(configDirectory))
            {
                Directory.CreateDirectory(configDirectory);
            }

            string oldSettingsFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DMPServerSettings.txt");
            string newSettingsFile = Path.Combine(Server.configDirectory, "Settings.txt");
            string oldGameplayFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DMPGameplaySettings.txt");
            string newGameplayFile = Path.Combine(Server.configDirectory, "GameplaySettings.txt");

            // Run the conversion
            BackwardsCompatibility.ConvertSettings(oldSettingsFile, newSettingsFile);
            if (File.Exists(oldGameplayFile))
            {
                if (!File.Exists(newGameplayFile))
                {
                    File.Move(oldGameplayFile, newGameplayFile);
                }
                File.Delete(oldGameplayFile);
            }

            //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", KickCommand.KickPlayer, "Kicks a player from the server");
            CommandHandler.RegisterCommand("ban", BanSystem.fetch.BanPlayer, "Bans a player from the server");
            CommandHandler.RegisterCommand("banip", BanSystem.fetch.BanIP, "Bans an IP Address from the server");
            CommandHandler.RegisterCommand("bankey", BanSystem.fetch.BanPublicKey, "Bans a Guid from the server");
            CommandHandler.RegisterCommand("pm", PMCommand.HandleCommand, "Sends a message to a player");
            CommandHandler.RegisterCommand("admin", AdminCommand.HandleCommand, "Sets a player as admin/removes admin from the player");
            CommandHandler.RegisterCommand("whitelist", WhitelistCommand.HandleCommand, "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();

            //Add new stock parts
            BackwardsCompatibility.UpdateModcontrolPartList();

            if (System.Net.Sockets.Socket.OSSupportsIPv6)
            {
                Settings.settingsStore.address = "::";
            }

            DarkLog.Debug("Loading settings...");
            Settings.Load();
            if (Settings.settingsStore.gameDifficulty == GameDifficulty.CUSTOM)
            {
                GameplaySettings.Reset();
                GameplaySettings.Load();
            }

            //Test compression
            if (Settings.settingsStore.compressionEnabled)
            {
                long testTime = Compression.TestSysIOCompression();
                Compression.compressionEnabled = true;
                DarkLog.Debug("System.IO compression works: " + Compression.sysIOCompressionWorks + ", test time: " + testTime + " ms.");
            }

            //Set day for log change
            day = DateTime.Now.Day;

            //Load plugins
            DMPPluginHandler.LoadPlugins();

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

            while (serverStarting || serverRestarting)
            {
                if (serverRestarting)
                {
                    DarkLog.Debug("Reloading settings...");
                    Settings.Reset();
                    Settings.Load();
                    if (Settings.settingsStore.gameDifficulty == GameDifficulty.CUSTOM)
                    {
                        DarkLog.Debug("Reloading gameplay settings...");
                        GameplaySettings.Reset();
                        GameplaySettings.Load();
                    }
                }

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

                if (Settings.settingsStore.gameDifficulty == GameDifficulty.CUSTOM)
                {
                    //Generate the config file by accessing the object.
                    DarkLog.Debug("Loading gameplay settings...");
                    GameplaySettings.Load();
                }

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

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

                serverRunning = true;
                Thread commandThread = new Thread(new ThreadStart(CommandHandler.ThreadMain));
                Thread clientThread  = new Thread(new ThreadStart(ClientHandler.ThreadMain));
                commandThread.Start();
                clientThread.Start();
                while (serverStarting)
                {
                    Thread.Sleep(500);
                }

                StartHTTPServer();
                DarkLog.Normal("Ready!");
                DMPPluginHandler.FireOnServerStart();
                while (serverRunning)
                {
                    //Run a garbage collection every 30 seconds.
                    if ((serverClock.ElapsedMilliseconds - lastGarbageCollect) > 30000)
                    {
                        lastGarbageCollect = serverClock.ElapsedMilliseconds;
                        GC.Collect();
                    }
                    //Run the screenshot expire function every 10 minutes
                    if ((serverClock.ElapsedMilliseconds - lastScreenshotExpiredCheck) > 600000)
                    {
                        lastScreenshotExpiredCheck = serverClock.ElapsedMilliseconds;
                        ScreenshotExpire.ExpireScreenshots();
                    }
                    //Run the log expire function every 10 minutes
                    if ((serverClock.ElapsedMilliseconds - lastLogExpiredCheck) > 600000)
                    {
                        lastLogExpiredCheck = serverClock.ElapsedMilliseconds;
                        LogExpire.ExpireLogs();
                    }
                    // Check if the day has changed, every minute
                    if ((serverClock.ElapsedMilliseconds - lastDayCheck) > 60000)
                    {
                        lastDayCheck = serverClock.ElapsedMilliseconds;
                        if (day != DateTime.Now.Day)
                        {
                            DarkLog.LogFilename = Path.Combine(DarkLog.LogFolder, "dmpserver " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".log");
                            DarkLog.WriteToLog("Continued from logfile " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".log");
                            day = DateTime.Now.Day;
                        }
                    }

                    Thread.Sleep(500);
                }
                DMPPluginHandler.FireOnServerStop();
                commandThread.Abort();
                clientThread.Join();
            }
            DarkLog.Normal("Goodbye!");
            Environment.Exit(0);
            #if !DEBUG
        }

        catch (Exception e)
        {
            DarkLog.Fatal("Error in main server thread, Exception: " + e);
            throw;
        }
            #endif
        }
Example #2
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;
            }
        }