コード例 #1
0
        /// <summary>
        /// Inits this instance.
        /// </summary>
        public static void Init()
        {
            Running = true;

            Logger.WriteLog("--------- Server Started at " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " ---------");
            Logger.Log("Debug mode started", LogType.Debug);
            //TODO Add debug messages
            //TODO load the level if it exists
            Block.InIt();
            Logger.Log("Starting update timer", LogType.Debug);
            UpdateTimer = new System.Threading.Timer((e) => Update(), null, 0, 100);
            Logger.Log("Log timer started", LogType.Debug);
            Logger.Log("Loading DLL's", LogType.Debug);
            LoadAllDlls.Init();
            Logger.Log("Finished loading DLL's", LogType.Debug);
            Logger.Log("Sending Heartbeat..", LogType.Debug);
            Logger.Log("Auto-Updater Starting..", LogType.Debug);
            Updater.InIt();
            HeartThread = new Thread(new ThreadStart(Heartbeat.ActivateHeartBeat));
            HeartThread.Start();
            Logger.Log("Starting Physics Tick..", LogType.Debug);
            PhysicsBlock.InIt();
            CmdReloadCmds reload = new CmdReloadCmds();

            reload.Initialize();

            Groups.PlayerGroup.Load();
            VerifyGroup = Groups.PlayerGroup.Find(ServerSettings.GetSetting("VerifyGroup"));
            Mainlevel   = Level.LoadLevel(ServerSettings.GetSetting("Main-Level"));
            if (Mainlevel == null)
            {
                Mainlevel = Level.CreateLevel(new Vector3S(256, 128, 64), Level.LevelTypes.Flat);
                ServerSettings.SetSetting("Main-Level", null, "main");
            }
            Level.Levels.Add(Mainlevel);
            if (ServerSettings.GetSettingBoolean("LoadAllLevels"))
            {
                Level.LoadAllLevels();
            }
            BlockQueue.Start();
            Backup.StartBackup();

            Database.Init();

            CreateCoreFiles();

            InternetUtils = new InetUtils();
            InetUtils.InternetAvailable = InetUtils.CanConnectToInternet();

            Logger.Log("Loading Bans", LogType.Debug);
            Logger.Log("IPBANS", LogType.Debug);
            IPBans = new List <string>(File.ReadAllLines("bans/IPBans.txt"));
            Logger.Log("IPBANS", LogType.Debug);
            UsernameBans = new List <string>(File.ReadAllLines("bans/NameBans.txt"));
            StartListening();
            Started = true;

            if (OnServerFinishSetup != null)
            {
                OnServerFinishSetup();
            }
            blockThread = new Thread(new ThreadStart(delegate
            {
                while (true)
                {
                    Thread.Sleep(60000);
                    Level.Levels.ForEach(delegate(Level l)
                    {
                        try
                        {
                            l.SaveToBinary();
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(e);
                        }
                    });
                }
            }));
            blockThread.Start();
            Logger.Log("[Important]: Server Started.", Color.Black, Color.White);
            if (!ServerSettings.GetSettingBoolean("VerifyNames"))
            {
                Logger.Log("[Important]: The server is running with verify names off! This could lead to bad things! Please turn on verify names if you dont know the risk and dont want these bad things to happen!", LogType.Critical);
            }

            if (ServerSettings.GetSettingBoolean("IRC-Enabled"))
            {
                try
                {
                    IRC = new ServerChat();
                    IRC.Connect();
                }
                catch (Exception e)
                {
                    Logger.LogError(e);
                }
            }

            if (ServerSettings.GetSettingBoolean("GC-Enabled"))
            {
                GC = new GlobalChat();
                try
                {
                    GC.Connect();
                }
                catch (Exception e)
                {
                    Logger.LogError(e);
                }
            }

            try
            {
                RC = new ConsoleListener();
                RC.Start();
            }
            catch { }

            PlayerConnectionTimeoutChecker = new Thread(() => {
                int sleep = ServerSettings.GetSettingInt("AutoTimeout");
                if (sleep < 0)
                {
                    sleep = 30;
                }
                if (sleep != 0)
                {
                    while (Running)
                    {
                        for (int i = 0; i < sleep && Running; i++)
                        {
                            Thread.Sleep(1000);
                        }
                        if (!Running)
                        {
                            break;
                        }
                        foreach (Player p in Players)
                        {
                            if (p.lastReceived.AddSeconds(sleep) < DateTime.Now)
                            {
#if !DEBUG
                                p.Kick("The connection timed out");
#endif
                            }
                        }
                    }
                }
            });
            PlayerConnectionTimeoutChecker.Start();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Logger.Init();
            ServerSettings.Init();
            bool checker = CheckArgs(args);

            Console.Title = ServerSettings.GetSetting("ServerName") + " - MCForge 6"; //TODO: Do know what MCForge version we are using yet.
            if (!checker)
            {
                new Thread(new ThreadStart(Server.Init)).Start();
            }
            else
            {
                Logger.Log("Aborting Setup..", LogType.Critical);
            }

            //declare the Hooks
            //Error Logging
            Logger.OnRecieveErrorLog += new EventHandler <ErrorLogEventArgs>(Logger_OnRecieveErrorLog);
            //Normal Logs
            Logger.OnRecieveLog += new EventHandler <LogEventArgs>(Logger_OnRecieveLog);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            ConsoleKeyInfo keyInfo;

            if (Console.CursorLeft < 3)
            {
                WriteInputLine();
            }
            while ((keyInfo = Console.ReadKey()) != null)
            {
                char key = keyInfo.KeyChar;
                //handles escape
                if (keyInfo.Key == ConsoleKey.Escape)
                {
                    continue;
                }
                // Ignore if Alt or Ctrl is pressed
                if ((keyInfo.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt)
                {
                    continue;
                }
                if ((keyInfo.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
                {
                    continue;
                }
                // Ignore if KeyChar value is \u0000.
                if (keyInfo.KeyChar == '\u0000')
                {
                    continue;
                }
                // Ignore tab key.
                if (keyInfo.Key == ConsoleKey.Tab)
                {
                    continue;
                }
                // handle function keys
                if (keyInfo.Key >= ConsoleKey.F1 && keyInfo.Key <= ConsoleKey.F24)
                {
                    continue;
                }

                if (keyInfo.Key != ConsoleKey.Enter)
                {
                    if (keyInfo.Key == ConsoleKey.Backspace && input.Length > 0)
                    {
                        ClearConsoleLine(Console.CursorTop);
                        Console.SetCursorPosition(0, Console.CursorTop);
                        if (!(input.Length < 1))
                        {
                            input = input.Remove(input.Length - 1, 1);
                            WriteInputLine("> " + input);
                        }
                    }
                    else if (Char.IsLetterOrDigit(key) || Char.IsPunctuation(key) || Char.IsSymbol(key) || key == ' ')
                    {
                        input += key.ToString();
                    }
                    continue;
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(input))
                    {
                        WriteInputLine();
                        continue;
                    }
                    WriteLine("");
                    if (Handle(input))
                    {
                        return;
                    }
                    input = "";
                    WriteInputLine();
                }
            }
        }
コード例 #3
0
        public void Start()
        {
            server    = ServerSettings.GetSetting("IRC-Server");
            port      = ServerSettings.GetSettingInt("IRC-Port");
            nickname  = ServerSettings.GetSetting("IRC-Nickname");
            channel   = ServerSettings.GetSetting("IRC-Channel");
            opChannel = ServerSettings.GetSetting("IRC-OPChannel");
            password  = ServerSettings.GetSetting("IRC-NickServ");
            if (nickname == "" || server == "" || channel == "#" || channel == "" || port == -1 || !ServerSettings.GetSettingBoolean("IRC-Enabled"))
            {
                return;
            }
            ircControllers = LoadIrcControllers();
            Logger.Log("Connecting to IRC...");
            botOn = true;
            try
            {
                // Connect
                irc     = new TcpClient(server, port);
                sstream = irc.GetStream();
                sread   = new StreamReader(sstream);
                swrite  = new StreamWriter(sstream);
            }
            catch (Exception e)
            {
                Logger.Log("Error connecting to " + server + ": " + e);
                return;
            }

            // Identify
            swrite.WriteLine("USER {0} {0} {0} :{1}", nickname, nickname);
            swrite.Flush();
            swrite.WriteLine("NICK {0}", nickname);
            swrite.Flush();

            cp = new ConsolePlayer(cio);

            while (botOn)
            {
                try
                {
                    if ((line = sread.ReadLine()) != null && botOn)
                    {
                        if (debug)
                        {
                            Logger.Log(line);
                        }
                        splitLine = line.Split(' ');

                        if (splitLine.Length > 0)
                        {
                            if (splitLine[1] == "376" || splitLine[1] == "422")
                            {
                                swrite.WriteLine("JOIN {0}", channel);
                                swrite.Flush();
                                if (opChannel != "#" || opChannel != "")
                                {
                                    swrite.WriteLine("JOIN {0}", opChannel);
                                    swrite.Flush();
                                }
                                swrite.WriteLine("NS IDENTIFY {0} {1}", nickname, password);
                                swrite.Flush();
                                connected = true;
                                Logger.Log("Connected!");
                            }

                            if (splitLine[0] == "PING")
                            {
                                swrite.WriteLine("PONG {0}", splitLine[1]);
                                swrite.Flush();
                            }
                        }
                        string replyChannel = "";
                        if (line.Split(' ')[2] != channel && line.Split(' ')[2] != opChannel)
                        {
                            replyChannel = line.Split('!')[0].Remove(0, 1);
                        }
                        else
                        {
                            replyChannel = line.Split(' ')[2];
                        }
                        line = line.Replace("%", "&");
                        if (GetSpokenLine(line).Equals("!players"))
                        {
                            swrite.WriteLine("PRIVMSG {0} :" + "There are " + Server.Players.Count + " player(s) online:",
                                             replyChannel);
                            swrite.Flush();
                            string playerString = "";
                            Server.Players.ForEach(delegate(Player pl)
                            {
                                playerString = playerString + pl.Username + ", ";
                            });
                            swrite.WriteLine("PRIVMSG {0} :" + playerString.Remove(playerString.Length - 2, 2),
                                             replyChannel);
                            swrite.Flush();
                        }
                        else if (GetSpokenLine(line).Equals("!url"))
                        {
                            swrite.WriteLine("PRIVMSG {0} :" + "The url for the server is: " + Server.URL,
                                             replyChannel);
                            swrite.Flush();
                        }
                        else if (GetSpokenLine(line).StartsWith("!"))
                        {
                            if (GetSpokenLine(line).Trim().Length > 1)
                            {
                                string   name = GetSpokenLine(line).Trim().Substring(1);
                                ICommand c    = Command.Find(name);
                                if (c != null)
                                {
                                    int i = GetSpokenLine(line).Trim().IndexOf(" ");
                                    if (i > 0)
                                    {
                                        string[] cargs = GetSpokenLine(line).Trim().Substring(i + 1).Split(' ');
                                        cp.replyChannel = replyChannel;
                                        c.Use(cp, cargs);
                                    }
                                    else
                                    {
                                        cp.replyChannel = replyChannel;
                                        c.Use(cp, new string[0]);
                                    }
                                }
                            }
                        }
                        else if (line.ToLower().Contains("privmsg") && splitLine[1] != "005")
                        {
                            if (replyChannel != opChannel)
                            {
                                try
                                {
                                    Player.UniversalChat("[IRC] <" + GetUsernameSpeaking(line) + ">: " + IRCToClassic(GetSpokenLine(line)));
                                    Logger.Log("[IRC] <" + GetUsernameSpeaking(line) + ">: " + IRCToClassic(GetSpokenLine(line)));
                                }
                                catch { }
                            }
                            else if (replyChannel == opChannel)
                            {
                                try
                                {
                                    Player.UniversalChatOps("[OPIRC] <" + GetUsernameSpeaking(line) + ">: " + IRCToClassic(GetSpokenLine(line)));
                                    Logger.Log("[OPIRC] <" + GetUsernameSpeaking(line) + ">: " + IRCToClassic(GetSpokenLine(line)));
                                }
                                catch { }
                            }
                        }
                    }
                }
                catch { }
            }
            // Clean up
            connected = false;
            swrite.Close();
            sread.Close();
            irc.Close();
        }
コード例 #4
0
        //UNDONE Windows needs to update
        private static object Tick()
        {
            using (WebClient wc = new WebClient()) {
                if (checkmisc)
                {
                    bool updated = false;
                    if (!cmdupdate)
                    {
                        //Check commands first
                        Logger.Log("Checking Commands for updates", LogType.Debug);
                        Version cmdv     = LoadAllDlls.LoadFile("Plugins.dll").GetName().Version;
                        Version clastest = new Version(wc.DownloadString("http://update.mcforge.net/cmdv.txt"));
                        if (clastest > cmdv)
                        {
                            updated = true;
                            Logger.Log("Updates found, updating", LogType.Debug);
                            if (silentupdate)
                            {
                                wc.DownloadFile("http://update.mcforge.net/DLL/Commands.dll", "Commands.dll");
                            }
                            else if (autoupdate)
                            {
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "An update for the Core Commands are available!");
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "Downloading update..");
                                wc.DownloadFile("http://update.mcforge.net/DLL/Commands.dll", "Commands.dll");
                            }
                            else if (askbeforemisc)
                            {
                                cmdupdate = true;
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "An update for the Core Commands are available!");
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "To update, type /update");
                            }
                        }
                    }
                    if (!plugupdate)
                    {
                        //Check plugin system
                        Logger.Log("Checking Plugins for updates", LogType.Debug);
                        Version plugv    = LoadAllDlls.LoadFile("Plugins.dll").GetName().Version;
                        Version plastest = new Version(wc.DownloadString("http://update.mcforge.net/plugv.txt"));
                        if (plastest > plugv)
                        {
                            updated = true;
                            Logger.Log("Updates found, updating", LogType.Debug);
                            if (silentupdate)
                            {
                                wc.DownloadFile("http://update.mcforge.net/DLL/Plugins.dll", "Plugins.dll");
                            }
                            else if (autoupdate)
                            {
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "An update for the Core Plugins are available!");
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "Downloading update..");
                                wc.DownloadFile("http://update.mcforge.net/DLL/Commands.dll", "Plugins.dll");
                            }
                            else if (askbeforemisc)
                            {
                                plugupdate = true;
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "An update for the Core Plugins are available!");
                                Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "To update, type /update");
                            }
                        }
                    }

                    //Reload the system if new updates were installed
                    if ((silentupdate || autoupdate) && updated)
                    {
                        ReloadCommands();
                    }
                }

                if (checkcore && !coreupdated)
                {
                    Logger.Log("Checking Core for updates", LogType.Debug);
                    //Check core
                    if (coreupdate && silentcoreupdate)
                    {
                        if (Server.PlayerCount < (int)((double)(ServerSettings.GetSettingInt("MaxPlayers") / 4)))
                        {
                            if (Server.OnMono)
                            {
                                string args = wc.DownloadString("http://update.mcforge.net/Patch/args.txt");
                                if (allowpatch && args != "")
                                {
                                    Process process = new Process();
                                    process.StartInfo.FileName  = "Updater.exe";
                                    process.StartInfo.Arguments = args;
                                    process.Start();
                                    System.Environment.Exit(0);
                                }
                                else
                                {
                                    wc.DownloadFile("http://update.mcforge.net/DLL/Core.dll", "MCForge.dll");
                                }
                            }
                            else
                            {
                                string args = "";
                                if (allowpatch)
                                {
                                    args = wc.DownloadString("http://update.mcforge.net/Patch/args.txt");
                                }
                                Process process = new Process();
                                process.StartInfo.FileName  = "Updater.exe";
                                process.StartInfo.Arguments = args;
                                process.Start();
                                System.Environment.Exit(0);
                            }
                        }
                    }
                    Version corel = new Version(wc.DownloadString("http://update.mcforge.net/corev.txt"));
                    if (corel > Version)
                    {
                        Logger.Log("Updates found, updating", LogType.Debug);
                        coreupdate = true;
                        if (silentcoreupdate)
                        {
                            if (Server.PlayerCount < (int)((double)(ServerSettings.GetSettingInt("MaxPlayers") / 4)))
                            {
                                if (Server.OnMono)
                                {
                                    string args = wc.DownloadString("http://update.mcforge.net/Patch/args.txt");
                                    if (allowpatch && args != "")
                                    {
                                        Process process = new Process();
                                        process.StartInfo.FileName  = "Updater.exe";
                                        process.StartInfo.Arguments = args;
                                        process.Start();
                                        System.Environment.Exit(0);
                                    }
                                    else
                                    {
                                        wc.DownloadFile("http://update.mcforge.net/DLL/Core.dll", "MCForge.dll");
                                    }
                                }
                                else
                                {
                                    string args = "";
                                    if (allowpatch)
                                    {
                                        args = wc.DownloadString("http://update.mcforge.net/Patch/args.txt");
                                    }
                                    Process process = new Process();
                                    process.StartInfo.FileName  = "Updater.exe";
                                    process.StartInfo.Arguments = args;
                                    process.Start();
                                    System.Environment.Exit(0);
                                }
                            }
                        }
                        else if (autoupdate)
                        {
                            Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "An update for the Core is available!");
                            Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "Downloading update..");
                            if (Server.OnMono)
                            {
                                string args = wc.DownloadString("http://update.mcforge.net/Patch/args.txt");
                                if (allowpatch && args != "")
                                {
                                    Process process = new Process();
                                    process.StartInfo.FileName  = "Updater.exe";
                                    process.StartInfo.Arguments = args;
                                    process.Start();
                                    System.Environment.Exit(0);
                                }
                                else
                                {
                                    wc.DownloadFile("http://update.mcforge.net/DLL/Core.dll", "MCForge.dll");
                                    Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "MCForge has been updated!");
                                    Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "Updates will be applied next restart.");
                                    Logger.Log("MCForge has been updated!", LogType.Critical);
                                    Logger.Log("Updates will be applied next restart.", LogType.Critical);
                                    coreupdated = true;
                                }
                            }
                            else
                            {
                                string args = "";
                                if (allowpatch)
                                {
                                    args = wc.DownloadString("http://update.mcforge.net/Patch/args.txt");
                                }
                                Process process = new Process();
                                process.StartInfo.FileName  = "Updater.exe";
                                process.StartInfo.Arguments = args;
                                process.Start();
                                System.Environment.Exit(0);
                            }
                        }
                        else
                        {
                            Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "An update for the Core is available!");
                            Player.UniversalChatOps("&2[Updater] " + Server.DefaultColor + "To update, type /update");
                        }
                    }
                }
            }
            return(null);
        }