Esempio n. 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();
        }
Esempio n. 2
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();
        }