Пример #1
0
        private static void Main(string[] args)
        {
            ConsoleUtils.TryEnableUnicode();

            // Try to render Jazz2 logo
            if (ConsoleImage.RenderFromManifestResource("ConsoleImage.udl", out int imageTop) && imageTop >= 0)
            {
                int width = Console.BufferWidth;

                // Show version number in the right corner
                string appVersion = "v" + Game.App.AssemblyVersion;

                int currentCursorTop = Console.CursorTop;
                Console.SetCursorPosition(width - appVersion.Length - 2, imageTop + 1);
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine(appVersion);
                Console.ResetColor();
                Console.CursorTop = currentCursorTop;
            }

            // Override working directory
            try {
                Environment.CurrentDirectory = Jazz2.Game.App.AssemblyPath;
            } catch (Exception ex) {
                Log.Write(LogType.Warning, "Cannot override working directory: " + ex);
            }

            // Process parameters
            int port;

            if (!TryRemoveArg(ref args, "/port:", out port))
            {
                port = 0;
            }

            string overrideHostname;

            if (!TryRemoveArg(ref args, "/override-hostname:", out overrideHostname))
            {
                overrideHostname = null;
            }

            string name;

            if (!TryRemoveArg(ref args, "/name:", out name) || string.IsNullOrWhiteSpace(name))
            {
                name = "Unnamed server";
            }

            int minPlayers;

            if (!TryRemoveArg(ref args, "/min-players:", out minPlayers))
            {
                minPlayers = 0;
            }

            int maxPlayers;

            if (!TryRemoveArg(ref args, "/max-players:", out maxPlayers))
            {
                maxPlayers = 0;
            }

            string levelName;

            if (!TryRemoveArg(ref args, "/level:", out levelName))
            {
                levelName = "unknown/battle2";
            }

            string levelTypeRaw;
            MultiplayerLevelType levelType;

            if (!TryRemoveArg(ref args, "/level-type:", out levelTypeRaw) || !Enum.TryParse(levelTypeRaw, out levelType))
            {
                levelType = MultiplayerLevelType.Battle;
            }

            string configPath;

            if (!TryRemoveArg(ref args, "/config:", out configPath))
            {
                if (File.Exists("Jazz2.Server.default"))
                {
                    configPath = "Jazz2.Server.default";
                }
                else
                {
                    configPath = null;
                }
            }

            bool startUnloaded = TryRemoveArg(ref args, "/unloaded");
            bool isPrivate     = TryRemoveArg(ref args, "/private");
            bool enableUPnP    = TryRemoveArg(ref args, "/upnp");

            // Initialization
            Version v           = Assembly.GetEntryAssembly().GetName().Version;
            byte    neededMajor = (byte)v.Major;
            byte    neededMinor = (byte)v.Minor;
            byte    neededBuild = (byte)v.Build;

            Log.Write(LogType.Info, "Starting server...");
            Log.PushIndent();

            // Start game server
            DualityApp.Init(DualityApp.ExecutionContext.Server, null, args);

            AsyncManager.Init();

            gameServer = new GameServer();

            if (overrideHostname != null)
            {
                try {
                    gameServer.OverrideHostname(overrideHostname);
                } catch {
                    Log.Write(LogType.Error, "Cannot set override public hostname!");
                }
            }

            gameServer.Run(configPath, port, name, minPlayers, maxPlayers, isPrivate, enableUPnP, neededMajor, neededMinor, neededBuild);

            Log.PopIndent();

            if (!startUnloaded)
            {
                if (gameServer.ActivePlaylistIndex == -1)
                {
                    gameServer.ChangeLevel(levelName, levelType);
                }
                else
                {
                    gameServer.ChangeLevelFromPlaylist(gameServer.ActivePlaylistIndex);
                }

                Log.Write(LogType.Info, "Ready to play!");
            }
            else
            {
                Log.Write(LogType.Verbose, "Server is unloaded.");
            }

            Log.Write(LogType.Info, "");

            // Check for updates
            Updater.CheckUpdates(OnCheckUpdates);

            // Processing of console commands
            ProcessConsoleCommands();

            // Shutdown
            Log.Write(LogType.Info, "Closing...");

            gameServer.Dispose();
        }
Пример #2
0
        private static bool HandleCommandSet(string input)
        {
            string key = GetPartFromInput(ref input);

            switch (key)
            {
            case "name": {
                if (!string.IsNullOrWhiteSpace(input))
                {
                    gameServer.Name = input;
                    Log.Write(LogType.Info, "Server name was set to \"" + input + "\"!");
                }
                else
                {
                    Log.Write(LogType.Error, "Cannot set server name to \"" + input + "\"!");
                }
                break;
            }

            case "level": {
                string value = GetPartFromInput(ref input);
                if (gameServer.ChangeLevel(value, MultiplayerLevelType.Battle))
                {
                    Log.Write(LogType.Info, "Level was changed to \"" + value + "\"!");
                }
                else
                {
                    Log.Write(LogType.Error, "Cannot load level \"" + value + "\"!");
                }
                break;
            }

            case "only_unique_clients": {
                string value   = GetPartFromInput(ref input);
                bool   enabled = (value == "true" || value == "yes" || value == "1");
                gameServer.AllowOnlyUniqueClients = enabled;
                Log.Write(LogType.Info, "Allow only unique clients is " + (enabled ? "enabled" : "disabled") + "!");
                break;
            }

            case "player_health": {
                string value = GetPartFromInput(ref input);
                byte   health;
                if (byte.TryParse(value, out health) && health > 0 && health < 20)
                {
                    gameServer.SpawnedPlayerHealth = health;
                    Log.Write(LogType.Info, "Player health was changed to " + health + "!");
                }
                else
                {
                    Log.Write(LogType.Info, "Invalid value provided!");
                }
                break;
            }

            case "spawning": {
                string value   = GetPartFromInput(ref input);
                bool   enabled = (value == "true" || value == "yes" || value == "1");
                gameServer.IsPlayerSpawningEnabled = enabled;
                Log.Write(LogType.Info, "Player spawning is " + (enabled ? "enabled" : "disabled") + "!");
                break;
            }

            default: {
                if (string.IsNullOrEmpty(key))
                {
                    Log.Write(LogType.Info, "name = " + gameServer.Name);
                    Log.Write(LogType.Info, "level = " + gameServer.CurrentLevel);
                    Log.Write(LogType.Info, "only_unique_clients = " + gameServer.AllowOnlyUniqueClients);
                    Log.Write(LogType.Info, "player_health = " + gameServer.SpawnedPlayerHealth);
                    Log.Write(LogType.Info, "spawning = " + gameServer.IsPlayerSpawningEnabled);
                    Log.Write(LogType.Info, "");
                }
                else
                {
                    HandleUnknownCommand();
                }
                break;
            }
            }

            return(true);
        }
Пример #3
0
        private static bool HandleCommandSet(string input)
        {
            string key = GetPartFromInput(ref input);

            switch (key)
            {
            case "name": {
                if (!string.IsNullOrWhiteSpace(input))
                {
                    gameServer.Name = input;
                    Log.Write(LogType.Info, "Server name was set to \"" + input + "\"!");
                }
                else
                {
                    Log.Write(LogType.Error, "Cannot set server name to \"" + input + "\"!");
                }
                break;
            }

            case "level": {
                string levelName    = GetPartFromInput(ref input);
                string levelTypeRaw = GetPartFromInput(ref input);
                MultiplayerLevelType levelType;
                switch (levelTypeRaw)
                {
                default:
                case "battle":
                case "b":
                case "Battle":
                    levelType = MultiplayerLevelType.Battle; break;

                case "team-battle":
                case "tb":
                case "TeamBattle":
                    levelType = MultiplayerLevelType.TeamBattle; break;

                case "capture-the-flag":
                case "ctf":
                case "CaptureTheFlag":
                    levelType = MultiplayerLevelType.CaptureTheFlag; break;

                case "race":
                case "r":
                case "Race":
                    levelType = MultiplayerLevelType.Race; break;

                case "treasure-hunt":
                case "th":
                case "TreasureHunt":
                    levelType = MultiplayerLevelType.TreasureHunt; break;
                    //case "coop-story":
                    //case "cs":
                    //    levelType = MultiplayerLevelType.CoopStory; break;
                }

                if (gameServer.ChangeLevel(levelName, levelType))
                {
                    Log.Write(LogType.Info, "Level was changed to \"" + levelName + "\" with " + levelType + " mode!");
                }
                else
                {
                    Log.Write(LogType.Error, "Cannot load level \"" + levelName + "\"!");
                }
                break;
            }

            case "only_unique_clients": {
                string value   = GetPartFromInput(ref input);
                bool   enabled = (value == "true" || value == "yes" || value == "1");
                gameServer.AllowOnlyUniqueClients = enabled;
                Log.Write(LogType.Info, "Allow only unique clients is " + (enabled ? "enabled" : "disabled") + "!");
                break;
            }

            case "player_health": {
                string value = GetPartFromInput(ref input);
                byte   health;
                if (byte.TryParse(value, out health) && health > 0 && health < 20)
                {
                    gameServer.SpawnedPlayerHealth = health;
                    Log.Write(LogType.Info, "Player health was changed to " + health + "!");
                }
                else
                {
                    Log.Write(LogType.Info, "Invalid value provided!");
                }
                break;
            }

            case "spawning": {
                string value   = GetPartFromInput(ref input);
                bool   enabled = (value == "true" || value == "yes" || value == "1");
                gameServer.IsPlayerSpawningEnabled = enabled;
                Log.Write(LogType.Info, "Player spawning is " + (enabled ? "enabled" : "disabled") + "!");
                break;
            }

            default: {
                if (string.IsNullOrEmpty(key))
                {
                    Log.Write(LogType.Info, "name = " + gameServer.Name);
                    Log.Write(LogType.Info, "level = " + gameServer.CurrentLevel + " (" + gameServer.CurrentLevelType + ")");
                    Log.Write(LogType.Info, "only_unique_clients = " + gameServer.AllowOnlyUniqueClients);
                    Log.Write(LogType.Info, "player_health = " + gameServer.SpawnedPlayerHealth);
                    Log.Write(LogType.Info, "spawning = " + gameServer.IsPlayerSpawningEnabled);
                    Log.Write(LogType.Info, "");
                }
                else
                {
                    HandleUnknownCommand();
                }
                break;
            }
            }

            return(true);
        }
Пример #4
0
        private static void Main(string[] args)
        {
            ConsoleUtils.TryEnableUnicode();

            int imageTop;

            if (ConsoleImage.RenderFromManifestResource("ConsoleImage.udl", out imageTop) && imageTop >= 0)
            {
                int width = Console.BufferWidth;

                // Show version number in the right corner
                string appVersion = "v" + Game.App.AssemblyVersion;

                int currentCursorTop = Console.CursorTop;
                Console.SetCursorPosition(width - appVersion.Length - 2, imageTop + 1);
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine(appVersion);
                Console.ResetColor();
                Console.CursorTop = currentCursorTop;
            }

            // Process parameters
            int port;

            if (!TryRemoveArg(ref args, "/port:", out port))
            {
                port = 10666;
            }

            string overrideHostname;

            if (!TryRemoveArg(ref args, "/override-hostname:", out overrideHostname))
            {
                overrideHostname = null;
            }

            string name;

            if (!TryRemoveArg(ref args, "/name:", out name) || string.IsNullOrWhiteSpace(name))
            {
                name = "Unnamed server";
            }

            int maxPlayers;

            if (!TryRemoveArg(ref args, "/players:", out maxPlayers))
            {
                maxPlayers = 64;
            }

            string levelName;

            if (!TryRemoveArg(ref args, "/level:", out levelName))
            {
                levelName = "unknown/battle2";
            }

            bool isPrivate  = TryRemoveArg(ref args, "/private");
            bool enableUPnP = TryRemoveArg(ref args, "/upnp");

            // Initialization
            Version v           = Assembly.GetEntryAssembly().GetName().Version;
            byte    neededMajor = (byte)v.Major;
            byte    neededMinor = (byte)v.Minor;
            byte    neededBuild = (byte)v.Build;

            Log.Write(LogType.Info, "Starting server...", true);

            // Start game server
            gameServer = new GameServer();

            if (overrideHostname != null)
            {
                try {
                    gameServer.OverrideHostname(overrideHostname);
                } catch {
                    Log.Write(LogType.Error, "Cannot set override public hostname!");
                }
            }

            gameServer.Run(port, name, maxPlayers, isPrivate, enableUPnP, neededMajor, neededMinor, neededBuild);

            Log.PopIndent();

            gameServer.ChangeLevel(levelName, MultiplayerLevelType.Battle);

            Log.Write(LogType.Info, "Ready!");
            Log.Write(LogType.Info, "");

            // Processing of console commands
            ProcessConsoleCommands();

            // Shutdown
            Log.Write(LogType.Info, "Closing...");

            gameServer.Dispose();
        }