예제 #1
0
        public void Init()
        {
            logger.log("Server initialized");
            //Init salt
            string saltChars  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~";
            Random rand       = new Random();
            int    saltLength = rand.Next(12, 16);

            for (int i = 0; i < saltLength; i++)
            {
                salt += saltChars[rand.Next(0, saltChars.Length - 1)];
            }

            //Load config
            LoadConfig();
            //Put server name in console title
            Console.Title = this.serverName;
            if (!this.isPublic)
            {
                Console.Title += " (PRIVATE)";
            }

            playerlist = new Player[maxPlayers + 2];  //Extra slot is for rejects, and another one (for some odd reason it's less by one)
            for (int i = 0; i < maxPlayers + 2; i++)
            {
                playerlist[i] = null;
            }

            //Load world and start save timer
            if (!Directory.Exists("maps"))
            {
                Directory.CreateDirectory("maps");
            }
            if (!File.Exists("maps/" + worldPath))
            {
                world = new World(worldPath, 256, 64, 256);
                world.Save();
            }
            else
            {
                world = new World(worldPath);
                //world.Save();
            }
            worldSaveTimer          = new System.Timers.Timer(60000.0);
            worldSaveTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate
            {
                world.Save();
            });

            physics         = new BasicPhysics(world, lavaSpongeEnabled);
            this.physThread = new System.Threading.Thread(PhysicsUpdateLoop);
            physThread.Start();
            logger.log("Started BasicPhysics Engine");

            //Intercept Ctrl+C
            Console.CancelKeyPress += new ConsoleCancelEventHandler(delegate
            {
                world.Save();
            });


            //Load Commands
            Command.Init();
            consolePlayer = new ConsolePlayer();

            //Load blocks
            Blocks.Init();

            //Load special chars
            Player.specialChars = new Dictionary <string, char>();
            Player.specialChars.Add(@"\:D", (char)2);        //☻
            Player.specialChars.Add(@"\<3", (char)3);        //♥
            Player.specialChars.Add(@"\<>", (char)4);        //♦
            Player.specialChars.Add(@"\club", (char)5);      //♣
            Player.specialChars.Add(@"\spade", (char)6);     //♠
            Player.specialChars.Add(@"\boy", (char)11);      //♂
            Player.specialChars.Add(@"\girl", (char)12);     //♀
            Player.specialChars.Add(@"\8note", (char)13);    //♪
            Player.specialChars.Add(@"\16note", (char)14);   //♫
            Player.specialChars.Add(@"\*", (char)15);        //☼
            Player.specialChars.Add(@"\>", (char)16);        //►
            Player.specialChars.Add(@"\<-", (char)27);       //←
            Player.specialChars.Add(@"\<", (char)17);        //◄
            Player.specialChars.Add(@"\updn", (char)18);     //↕
            Player.specialChars.Add(@"\2!", (char)19);       //‼
            Player.specialChars.Add(@"\p", (char)20);        //¶
            Player.specialChars.Add(@"\sec", (char)21);      //§
            Player.specialChars.Add(@"\|up", (char)24);      //↑
            Player.specialChars.Add(@"\|dn", (char)25);      //↓
            Player.specialChars.Add(@"\->", (char)26);       //→
            //** The ← symbol was moved before ◄ due to parsing order issues
            Player.specialChars.Add(@"\lr", (char)29);       //↔
            Player.specialChars.Add(@"\up", (char)30);       //▲
            Player.specialChars.Add(@"\dn", (char)31);       //▼

            //Load ranks
            playerRanksDict = new Dictionary <string, byte>();
            try
            {
                if (File.Exists("ranks.txt"))
                {
                    StreamReader sr = new StreamReader(File.OpenRead("ranks.txt"));
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (line != null && !line.Equals("") && line.IndexOf(':') >= 0)
                        {
                            string user = line.Substring(0, line.IndexOf(':'));
                            byte   rank = byte.Parse(line.Substring(line.IndexOf(':') + 1));
                            playerRanksDict.Add(user, rank);
                        }
                    }
                    sr.Close();
                }
            }
            catch (OverflowException)
            {
                logger.log("Error while loading ranks: rank cannot be higher than 255", Logger.LogType.Error);
                running = false;
                return;
            }
            catch (Exception e)
            {
                logger.log("Error while loading ranks:", Logger.LogType.Error);
                logger.log(e);
                running = false;
                return;
            }
            logger.log("Loaded ranks from ranks.txt");

            ipbanned = new List <string>();
            try
            {
                if (File.Exists("ipbans.txt"))
                {
                    StreamReader sr = new StreamReader(File.OpenRead("ipbans.txt"));
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (line != null && !line.Equals(""))
                        {
                            ipbanned.Add(line.Trim());
                        }
                    }
                    sr.Close();
                }
            }
            catch (Exception e)
            {
                logger.log("Error while loading ranks:", Logger.LogType.Error);
                logger.log(e);
                running = false;
                return;
            }
            logger.log("Loaded ipbans from ipbans.txt");
            //etc

            //Initialize heartbeat timer
            heartbeatTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate
            {
                beater.Beat(false);
            });
            heartbeatTimer.Start();
            beater.Beat(true);  //Initial heartbeat

            //Start TCP listener thread
            this.ListenThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate
            {
                this.listener = new TcpListener(IPAddress.Any, this.port);
                try
                {
                    listener.Start();
                    listener.BeginAcceptTcpClient(new AsyncCallback(ClientAccept), null);
                }
                catch (Exception e)
                {
                    listener.Stop();
                    logger.log(e);
                }
                logger.log("Started listener thread");
            }));
            this.ListenThread.IsBackground = true;
            this.ListenThread.Start();
        }