Exemplo n.º 1
0
 static void MapSaveCallback([NotNull] object unused)
 {
     try {
         if (Map.ChangedSinceSave)
         {
             Map.ChangedSinceSave = false;
             Map.Save(MapFileName);
             Logger.Log("Map saved to {0}", MapFileName);
         }
     } catch (Exception ex) {
         Logger.LogError("Failed to save map: {0}", ex);
     }
 }
Exemplo n.º 2
0
 static void Shutdown()
 {
     Logger.Log("Shutting down");
     lock ( PlayerListLock ) {
         foreach (Player player in PlayerIndex)
         {
             player.Kick("Server shutting down");
         }
     }
     Map.ChangedSinceSave = false;
     Map.Save(MapFileName);
     Thread.Sleep(1000);
 }
Exemplo n.º 3
0
 public static void ChangeMap([NotNull] Map newMap)
 {
     if (newMap == null)
     {
         throw new ArgumentNullException("newMap");
     }
     lock ( PlayerListLock ) {
         foreach (Player player in PlayerIndex)
         {
             player.ChangeMap(newMap);
         }
         Player.Console.Map = newMap;
         Map.IsActive       = false;
         Map          = newMap;
         Map.IsActive = true;
         Map.Save(MapFileName);
     }
 }
Exemplo n.º 4
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
        }