Ejemplo n.º 1
0
        public override void Init(string[] args)
        {
            Tick      = 0;
            StartTime = 0;
            SendRconCommandsClients = new Queue <int>();
            GameTypes       = new Dictionary <string, Type>();
            SnapshotIdPool  = new SnapshotIdPool();
            SnapshotBuilder = new SnapshotBuilder();

            Config        = Kernel.Get <BaseConfig>();
            GameContext   = Kernel.Get <BaseGameContext>();
            Storage       = Kernel.Get <BaseStorage>();
            NetworkServer = Kernel.Get <BaseNetworkServer>();
            Console       = Kernel.Get <BaseGameConsole>();
            Register      = Kernel.Get <BaseRegister>();
            NetworkBan    = Kernel.Get <BaseNetworkBan>();

            if (Config == null ||
                GameContext == null ||
                Storage == null ||
                NetworkServer == null ||
                Console == null)
            {
                throw new Exception("Register components fail");
            }

            Clients = new BaseServerClient[MaxClients];
            for (var i = 0; i < Clients.Length; i++)
            {
                Clients[i] = Kernel.Get <BaseServerClient>();
            }

            Storage.Init("TeeSharp", StorageType.Server);
            Config.Init(ConfigFlags.Server | ConfigFlags.Econ);
            Console.CommandAdded += ConsoleOnCommandAdded;
            Console.Init();
            PrintCallbackInfo = Console.RegisterPrintCallback(
                (OutputLevel)Config["ConsoleOutputLevel"].AsInt(), OnConsolePrint);
            NetworkServer.Init();

            GameContext.BeforeInit();

            var useDefaultConfig = args.Any(a => a == "--default" || a == "-d");

            if (useDefaultConfig)
            {
            }
            else
            {
                RegisterConsoleCommands();
                Console.ExecuteFile("autoexec.cfg");
                Console.ParseArguments(args);
            }

            Config.RestoreString();
        }
Ejemplo n.º 2
0
        public override void Run()
        {
            if (IsRunning)
            {
                return;
            }

            Debug.Log("server", "starting...");

            if (!LoadMap(Config["SvMap"]))
            {
                Debug.Error("server", $"failed to load map. mapname='{Config["SvMap"]}'");
                return;
            }

            if (!StartNetworkServer())
            {
                return;
            }

            Clients = new BaseServerClient[NetworkServer.ServerConfig.MaxClients];
            IdMap   = new int[Clients.Length * VANILLA_MAX_CLIENTS];

            for (var i = 0; i < Clients.Length; i++)
            {
                Clients[i] = Kernel.Get <BaseServerClient>();
            }

            NetworkServer.SetCallbacks(NewClientCallback, DelClientCallback);
            Console.Print(OutputLevel.STANDARD, "server", $"server name is '{Config["SvName"]}'");
            GameContext.OnInit();

            StartTime = Time.Get();
            IsRunning = true;

            _lastSent    = new int[NetworkServer.ServerConfig.MaxClients];
            _lastAsk     = new int[NetworkServer.ServerConfig.MaxClients];
            _lastAskTick = new int[NetworkServer.ServerConfig.MaxClients];

            while (IsRunning)
            {
                var now   = Time.Get();
                var ticks = 0;

                while (now > TickStartTime(Tick + 1))
                {
                    Tick++;
                    ticks++;

                    for (var clientId = 0; clientId < Clients.Length; clientId++)
                    {
                        if (Clients[clientId].State != ServerClientState.IN_GAME)
                        {
                            continue;
                        }

                        for (var inputIndex = 0; inputIndex < Clients[clientId].Inputs.Length; inputIndex++)
                        {
                            if (Clients[clientId].Inputs[inputIndex].Tick == Tick)
                            {
                                GameContext.OnClientPredictedInput(clientId,
                                                                   Clients[clientId].Inputs[inputIndex].PlayerInput);
                                break;
                            }
                        }
                    }

                    GameContext.OnTick();
                }

                if (ticks != 0)
                {
                    if (Tick % 2 == 0 || Config["SvHighBandwidth"])
                    {
                        DoSnapshot();
                    }
                    // UpdateClientRconCommands()
                }

                Register.RegisterUpdate(NetworkServer.NetType());
                PumpNetwork();

                Thread.Sleep(5);
            }

            for (var i = 0; i < Clients.Length; i++)
            {
                if (Clients[i].State != ServerClientState.EMPTY)
                {
                    NetworkServer.Drop(i, "Server shutdown");
                }
            }

            GameContext.OnShutdown();
        }