Esempio n. 1
0
        public static void Main(string[] args)
        {
#if DEBUG
            _debugMode = true;
#endif

            CreateNeededFiles();

            // can't use logger here since the logger config depends on if debug mode is on or off
            Console.WriteLine("Reading server configuration...");

            _gameServerConfiguration =
                LoadServerConfiguration(Path.Combine(_location, "Configuration", "serverSettings.xml"));
            if (!_debugMode)
            {
                _debugMode = _gameServerConfiguration.DebugMode;
            }

            Util.LoggerFactory = new LoggerFactory();
            if (_debugMode)
            {
                Util.LoggerFactory.AddProvider(new ConsoleLoggerProvider(LogLevel.Trace));
            }
            else
            {
                Util.LoggerFactory.AddProvider(new ConsoleLoggerProvider(LogLevel.Information));
            }
            _logger = Util.LoggerFactory.CreateLogger <ServerManager>();
            DoDebugWarning();

            // enable Sentry
            if (!_debugMode)
            {
                SentrySdk.Init(config =>
                {
                    config.Dsn = new Dsn("https://[email protected]/1320932");

                    // minidumps are only written on Windows as Linux has no such functionality
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        config.AddExceptionProcessor(new MiniDump());
                    }
                });

                SentrySdk.ConfigureScope(scope =>
                {
                    // add configuration to crash reports
                    scope.SetExtra("configuration", _gameServerConfiguration);

                    // if server is ran on Windows we attempt to find a local discord client
                    // we will then get the username of the current user and include this in the event
                    // assuming developers need more information about a crash they can contact the user
                    // (this can be disabled by settings 'AnonymousCrashes' to true in the server configuration)
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !_gameServerConfiguration.AnonymousCrashes)
                    {
                        var user = Discord.GetDiscordUser();
                        if (user != null)
                        {
                            scope.User.Id       = user.Id;
                            scope.User.Username = $"{user.Name}#{user.Discriminator}";
                        }
                    }
                });
            }

            if (_gameServerConfiguration.ServerVariables.Any(v => v.Key == "tickEvery"))
            {
                var tpsString = _gameServerConfiguration.ServerVariables.First(v => v.Key == "tickEvery").Value;
                if (!int.TryParse(tpsString, out _tickEvery))
                {
                    _logger.LogError(LogEvent.Setup,
                                     "Could not set ticks per second from server variable 'tps' (value is not an integer)");
                }
                else
                {
                    _logger.LogInformation(LogEvent.Setup,
                                           "Custom tick rate set. Will try to tick every " + _tickEvery + "ms");
                }
            }

            _logger.LogInformation(LogEvent.Setup, "Server preparing to start...");

            _gameServer = new GameServer(_gameServerConfiguration.Port, _gameServerConfiguration.ServerName,
                                         _gameServerConfiguration.GamemodeName, _debugMode, _gameServerConfiguration.UPnP)
            {
                Password             = _gameServerConfiguration.Password,
                AnnounceSelf         = _gameServerConfiguration.AnnounceSelf,
                AllowNicknames       = _gameServerConfiguration.AllowNicknames,
                AllowOutdatedClients = _gameServerConfiguration.AllowOutdatedClients,
                MaxPlayers           = _gameServerConfiguration.MaxClients,
                Motd         = _gameServerConfiguration.Motd,
                RconPassword = _gameServerConfiguration.RconPassword
            };

            // push master servers (backwards compatible)
            _gameServer.MasterServers.AddRange(
                new [] { _gameServerConfiguration.PrimaryMasterServer, _gameServerConfiguration.BackupMasterServer });

            _gameServer.Start();

            // Plugin Code
            _logger.LogInformation(LogEvent.Setup, "Loading plugins");
            //Plugins = PluginLoader.LoadPlugin("TestPlugin");
            foreach (var pluginName in _gameServerConfiguration.ServerPlugins)
            {
                foreach (var loadedPlugin in PluginLoader.LoadPlugin(pluginName))
                {
                    _plugins.Add(loadedPlugin);
                }
            }

            // TODO future refactor
            if (_gameServerConfiguration.UseGroups)
            {
                _userModule = new UserModule(_gameServer);
                _userModule.Start();

                _gameServer.PermissionProvider = _userModule;
            }
            _gameServer.Metrics = new PrometheusMetrics();

            RegisterCommands();

            _logger.LogInformation(LogEvent.Setup, "Plugins loaded. Enabling plugins...");
            foreach (var plugin in _plugins)
            {
                if (!plugin.OnEnable(_gameServer, false))
                {
                    _logger.LogWarning(LogEvent.Setup, "Plugin " + plugin.Name + " returned false when enabling, marking as disabled, although it may still have hooks registered and called.");
                }
            }

            // prepare console
            if (!_gameServerConfiguration.NoConsole)
            {
                _cancellationToken = new CancellationTokenSource();
                var console = new ConsoleThread
                {
                    CancellationToken = _cancellationToken.Token,
                    GameServer        = _gameServer
                };

                Thread c = new Thread(new ThreadStart(console.ThreadProc))
                {
                    Name = "Server console thread"
                };
                c.Start();
            }

            Console.CancelKeyPress += Console_CancelKeyPress;

            // ready
            _logger.LogInformation(LogEvent.Setup, "Starting server main loop, ready to accept connections.");

            _timer = new Timer(DoServerTick, _gameServer, 0, _tickEvery);
            _autoResetEvent.WaitOne();
        }
Esempio n. 2
0
        public static void Start()
        {
            Util.LoggerFactory = new LoggerFactory();
            Util.CreateHttpClient();

            if (_debugMode)
            {
                Util.LoggerFactory.AddProvider(new ConsoleLoggerProvider(LogLevel.Trace));
            }
            else
            {
                Util.LoggerFactory.AddProvider(new ConsoleLoggerProvider(LogLevel.Information));
            }
            _logger = Util.LoggerFactory.CreateLogger <ServerManager>();

            DoDebugWarning();

            if (_gameServerConfiguration.ServerVariables.Any(v => v.Key == "tickEvery"))
            {
                var tpsString = _gameServerConfiguration.ServerVariables.First(v => v.Key == "tickEvery").Value;
                if (!int.TryParse(tpsString, out _tickEvery))
                {
                    _logger.LogError(LogEvent.Setup,
                                     "Could not set ticks per second from server variable 'tps' (value is not an integer)");
                }
                else
                {
                    _logger.LogInformation(LogEvent.Setup,
                                           "Custom tick rate set. Will try to tick every " + _tickEvery + "ms");
                }
            }

            _logger.LogInformation(LogEvent.Setup, "Server preparing to start...");

            _gameServer = new GameServer(_gameServerConfiguration.Port, _gameServerConfiguration.ServerName,
                                         _gameServerConfiguration.GamemodeName, _debugMode, _gameServerConfiguration)
            {
                Password             = _gameServerConfiguration.Password,
                AnnounceSelf         = _gameServerConfiguration.AnnounceSelf,
                AllowNicknames       = _gameServerConfiguration.AllowNicknames,
                AllowOutdatedClients = false, //_gameServerConfiguration.AllowOutdatedClients,
                MaxPlayers           = _gameServerConfiguration.MaxClients,
                Motd         = _gameServerConfiguration.Motd,
                RconPassword = _gameServerConfiguration.RconPassword
            };

            // push master servers (backwards compatible)
            _gameServer.MasterServers.AddRange(
                new [] { _gameServerConfiguration.PrimaryMasterServer, _gameServerConfiguration.BackupMasterServer });

            _gameServer.Start();

            // Plugin Code
            _logger.LogInformation(LogEvent.Setup, "Loading plugins");
            //Plugins = PluginLoader.LoadPlugin("TestPlugin");
            foreach (var pluginName in _gameServerConfiguration.ServerPlugins)
            {
                foreach (var loadedPlugin in PluginLoader.LoadPlugin(pluginName))
                {
                    _plugins.Add(loadedPlugin);
                }
            }

            RegisterCommands();

#if !BUILD_WASM
            // TODO future refactor
            if (_gameServerConfiguration.UseGroups)
            {
                _userModule = new UserModule(_gameServer);
                _userModule.Start();

                _gameServer.PermissionProvider = _userModule;
            }
#endif
            _gameServer.Metrics = new PrometheusMetrics();

            _logger.LogInformation(LogEvent.Setup, "Plugins loaded. Enabling plugins...");
            foreach (var plugin in _plugins)
            {
                if (!plugin.OnEnable(_gameServer, false))
                {
                    _logger.LogWarning(LogEvent.Setup, "Plugin " + plugin.Name + " returned false when enabling, marking as disabled, although it may still have hooks registered and called.");
                }
            }

            // prepare console
            if (!_gameServerConfiguration.NoConsole)
            {
                _cancellationToken = new CancellationTokenSource();
                var console = new ConsoleThread
                {
                    CancellationToken = _cancellationToken.Token
                };

#if !BUILD_WASM
                Thread c = new Thread(new ThreadStart(console.ThreadProc))
                {
                    Name = "Server console thread"
                };
                c.Start();
#endif
            }

            // ready
            _logger.LogInformation(LogEvent.Setup, "Starting server main loop, ready to accept connections.");

            _timer = new Timer(DoServerTick, _gameServer, 0, _tickEvery);

#if !BUILD_WASM
            Console.CancelKeyPress += Console_CancelKeyPress;
            _autoResetEvent.WaitOne();
#endif
        }