コード例 #1
0
ファイル: Program.cs プロジェクト: hereisderek/D2Moddin
        static void Main(string[] args)
        {
            var defaultConfig = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile + ".default";

            if (!File.Exists(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) && File.Exists(defaultConfig))
            {
                File.Copy(defaultConfig, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                ServerUpdater.RestartD2MP();
                return;
            }

            if (Settings.Default.serverName == "UNNAMED")
            {
                Settings.Default["serverName"] = "Unnamed Server";
            }

            Settings.Default.Save();

            Console.CancelKeyPress += (sender, arg) => ShutdownImmediately();

            XmlConfigurator.Configure();
            System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

            log.Info("D2MP server version " + Init.Version + " starting...");
            log.Debug("Connection address: " + Settings.Default.serverIP);
            var configPath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming).FilePath;

            log.Info("Server config path: " + configPath);

            Console.Title = string.Format("[{0}] D2MP Server", Init.Version);

            manager = new ServerManager();
            if (!manager.SetupEnvironment())
            {
                log.Fatal("Failed to setup the server, exiting.");
                return;
            }

            connection = new ServerConnection(manager);

            while (!shutdown && !Console.KeyAvailable)
            {
                Thread.Sleep(100);
            }
            ShutdownAll();
        }
コード例 #2
0
        private void ProcessMessage(string data)
        {
            string[] command = data.Split('|');
            SendPing(command[0]);
            Task.Factory.StartNew(() =>
            {
                try
                {
                    lock (ConcurrentLock)
                    {
                        switch (command[0])
                        {
                        case "shutdown":
                            {
                                Program.ShutdownAll();
                            }
                            break;

                        case "restart":
                            {
                                ServerUpdater.RestartD2MP();
                                Program.ShutdownAll();
                            }
                            break;

                        case "reinit":
                            {
                                SendInit();
                            }
                            break;

                        case "addonOps":
                            {
                                ThreadPool.QueueUserWorkItem(PerformAddonOps, command);
                            }
                            break;

                        case "updateMods":
                            {
                                var mods = command[1].Split(',');
                                log.Info("Updates pushed for mods: " + command[1]);
                                foreach (var mod in mods)
                                {
                                    log.Info("Killing all servers for mod " + mod);
                                    manager.ShutdownAllMod(mod);
                                }
                                SendInit();
                            }
                            break;

                        case "launchServer":
                            {
                                int id            = int.Parse(command[1]);
                                bool dev          = bool.Parse(command[2]);
                                string mod        = command[3];
                                string rconPass   = command[4];
                                string[] commands = command[5].Split('&');
                                string map        = command[6];
                                int port;
                                for (port = Settings.Default.portRangeStart;
                                     port < Settings.Default.portRangeEnd;
                                     port++)
                                {
                                    if (manager.IsPortFree(port) && Utils.IsPortOpen(port))
                                    {
                                        break;
                                    }
                                }
                                var serv = manager.LaunchServer(id, port, dev, mod, rconPass,
                                                                commands, map);
                                serv.OnReady +=
                                    (sender, args) =>
                                    Send(
                                        JObject.FromObject(new OnServerLaunched()
                                {
                                    id   = id,
                                    port = port
                                })
                                        .ToString(Formatting.None));
                                serv.OnShutdown +=
                                    (sender, args) =>
                                    Send(
                                        JObject.FromObject(new OnServerShutdown()
                                {
                                    id = id
                                })
                                        .ToString(Formatting.None));
                                break;
                            }

                        case "setMaxLobbies":
                            {
                                int max = int.Parse(command[1]);
                                Settings.Default["serverCount"] = max;
                                var configPath =
                                    ConfigurationManager.OpenExeConfiguration(
                                        ConfigurationUserLevel.PerUserRoaming).FilePath;
                                Settings.Default.Save();
                                log.Info("Server set the max lobby count to " + max);
                                log.Debug("Saved config: " + configPath);
                                break;
                            }

                        case "setServerRegion":
                            {
                                Settings.Default["serverRegion"] =
                                    (ServerRegion)int.Parse(command[1]);
                                Settings.Default.Save();
                                log.Debug("The server region was set to " + command[1]);
                                break;
                            }
                            ;

                        case "setServerName":
                            {
                                Settings.Default["serverName"] = command[1];
                                Settings.Default.Save();
                                log.Debug("Server set our name to " + command[1]);
                                break;
                            }

                        case "shutdownServer":
                            {
                                int id = int.Parse(command[1]);
                                manager.ShutdownServer(id);
                            }
                            break;

                        case "authFail":
                            log.Debug("Auth password is invalid.");
                            log.Fatal(
                                "Server doesn't like our init info (we're probably out of date), shutting down...");
                            infoValid = false;
                            Environment.Exit(0);
                            break;

                        case "outOfDate":
                            if (updating)
                            {
                                return;
                            }
                            log.Info("Server is out of date (current version is " +
                                     Init.Version +
                                     "), updating...");
                            updating = true;
                            client.Close();
                            if (!Settings.Default.disableUpdate)
                            {
                                var url = command[1];
                                log.Debug("Downloading update from " + url + "...");
                                ServerUpdater.UpdateFromURL(url);
                                break;
                            }
                            else
                            {
                                log.Fatal(
                                    "Server out of date but auto updating disabled. Exiting...");
                                Program.shutdown = true;
                            }
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Issue processing command", ex);
                }
            });
        }