示例#1
0
        static void GenHandler(bool flat, [NotNull] Player player, [CanBeNull] string param)
        {
            if (!player.CheckIfOp())
            {
                return;
            }

            string cmdName = (flat ? "GenFlat" : "Gen");

            if (String.IsNullOrEmpty(param))
            {
                PrintGenUsage(cmdName, player);
                return;
            }

            string[] args = param.Split(' ');

            ushort width, length, height;

            if (args.Length != 4 ||
                !UInt16.TryParse(args[0], out width) ||
                !UInt16.TryParse(args[1], out length) ||
                !UInt16.TryParse(args[2], out height))
            {
                PrintGenUsage(cmdName, player);
                return;
            }

            if (!IsPowerOfTwo(width) || !IsPowerOfTwo(length) || !IsPowerOfTwo(height) ||
                width < 16 || length < 16 || height < 16 ||
                width > 1024 || length > 1024 || height > 1024)
            {
                player.Message("{0}: Map dimensions should be powers-of-2 between 16 and 1024", cmdName);
                return;
            }

            string fileName = args[3];

            if (!fileName.EndsWith(".lvl"))
            {
                player.Message("Load: Filename must end with .lvl");
                return;
            }

            player.MessageNow("Generating a {0}x{1}x{2} map...", width, length, height);
            Map map;

            if (flat)
            {
                map = Map.CreateFlatgrass(width, length, height);
            }
            else
            {
                map = NotchyMapGenerator.Generate(width, length, height);
            }
            try {
                map.Save(fileName);
                player.Message("Map saved to {0}", Path.GetFileName(fileName));
            } catch (Exception ex) {
                player.Message("Could not save map: {0}: {1}", ex.GetType().Name, ex.Message);
                Logger.LogError("Failed to save map: {0}", ex);
            }
        }
示例#2
0
        static int Main()
        {
#if !DEBUG
            try {
#endif
            Console.Title = VersionString;
            Logger.Log("Starting {0}", VersionString);

            // load config
            Config.Load();
            Console.Title = Config.ServerName + " - " + VersionString;

            // prepare to accept players and fire up the heartbeat
            for (byte i = 1; i <= sbyte.MaxValue; i++)
            {
                FreePlayerIDs.Push(i);
            }
            UpdatePlayerList();
            Heartbeat.Start();

            // load player and IP lists
            Bans   = new PlayerNameSet(BansFileName);
            Ops    = new PlayerNameSet(OpsFileName);
            IPBans = new IPAddressSet(IPBanFileName);
            Logger.Log("Server: Tracking {0} bans, {1} ip-bans, and {2} ops.",
                       Bans.Count, IPBans.Count, Ops.Count);
            if (Config.UseWhitelist)
            {
                Whitelist = new PlayerNameSet(WhitelistFileName);
                Logger.Log("Using a whitelist ({0} players): {1}",
                           Whitelist.Count, Whitelist.GetCopy().JoinToString(", "));
            }

            // load or create map
            if (File.Exists(MapFileName))
            {
                Map = LvlMapConverter.Load(MapFileName);
                Logger.Log("Loaded map from {0}", MapFileName);
            }
            else
            {
                Logger.Log("Generating the map...");
                Map = NotchyMapGenerator.Generate(256, 256, 64);
                Map.Save(MapFileName);
            }
            Map.IsActive       = true;
            Player.Console.Map = Map;

            // start listening for incoming connections
            listener = new TcpListener(Config.IP, Config.Port);
            listener.Start();

            // start the scheduler thread
            Thread schedulerThread = new Thread(SchedulerLoop)
            {
                IsBackground = true
            };
            schedulerThread.Start();

            // listen for console input
            while (true)
            {
                string input = Console.ReadLine();
                if (input == null)
                {
                    Shutdown();
                    return(0);
                }
                try {
                    Player.Console.ProcessMessage(input.Trim());
                } catch (Exception ex) {
                    Logger.LogError("Could not process message: {0}", ex);
                }
            }

#if !DEBUG
        }

        catch (Exception ex) {
            Logger.LogError("Server crashed: {0}", ex);
            return(1);
        }
#endif
        }