示例#1
0
        public void Start()
        {
            if (this.Running)
            {
                return;
            }
            //Refresh Variables
            this.bolEnableJavaOptimisations = Convert.ToBoolean(Database.GetSetting(this.ServerID, "ServerEnableOptimisations"));
            this.intAssignedMem             = Convert.ToInt32(Database.GetSetting(this.ServerID, "ServerAssignedMemory"));
            this.ServerTitle     = Convert.ToString(Database.GetSetting(this.ServerID, "ServerTitle"));
            this.ServerType      = Convert.ToString(Database.GetSetting(this.ServerID, "ServerType"));
            this.RestartWhenFree = false;
            this.RestartNeeded   = false;

            //What type of server are we running?
            string strFile = "";

            switch (this.ServerType)
            {
            case "vanilla":
                strFile = "minecraft_server.jar";
                break;

            case "bukkit":
                strFile = "craftbukkit.jar";
                break;

            case "pre":
                strFile = "minecraft_server_pre.jar";
                break;

            default:
                strFile = "minecraft_server.jar";
                break;
            }

            //First check if an update is waiting to be applied
            if (!Util.ReplaceFile(Core.RootFolder + "\\lib\\" + strFile, Core.RootFolder + "\\lib\\" + strFile + ".UPDATE"))
            {
                return;
            }

            //Also check if a new properties file is to be applied
            if (!Util.ReplaceFile(this.strWorkingDir + "server.properties", this.strWorkingDir + "server.properties.UPDATE"))
            {
                return;
            }

            this.prcMinecraft = new Process();

            try
            {
                var strArgs     = "";
                var strFileName = YAMS.Util.JavaPath() + "java.exe";

                //If we have enabled the java optimisations add the additional
                //arguments. See http://www.minecraftforum.net/viewtopic.php?f=1012&t=68128
                if (bolEnableJavaOptimisations && YAMS.Util.HasJDK())
                {
                    var intGCCores = Environment.ProcessorCount - 1;
                    if (intGCCores == 0)
                    {
                        intGCCores = 1;
                    }
                    strArgs    += "-server -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=" + intGCCores + " -XX:+AggressiveOpts";
                    strFileName = YAMS.Util.JavaPath("jdk") + "java.exe";
                }

                //Some specials for bukkit
                if (this.ServerType == "bukkit")
                {
                    strArgs += " -Djline.terminal=jline.UnsupportedTerminal";
                }

                //Basic arguments in all circumstances
                strArgs += " -Xmx" + intAssignedMem + "M -Xms" + intAssignedMem + @"M -jar " + "\"" + Core.RootFolder + "\\lib\\";
                strArgs += strFile;
                strArgs += "\" nogui";

                this.prcMinecraft.StartInfo.UseShellExecute        = false;
                this.prcMinecraft.StartInfo.FileName               = strFileName;
                this.prcMinecraft.StartInfo.Arguments              = strArgs;
                this.prcMinecraft.StartInfo.CreateNoWindow         = true;
                this.prcMinecraft.StartInfo.RedirectStandardError  = true;
                this.prcMinecraft.StartInfo.RedirectStandardInput  = true;
                this.prcMinecraft.StartInfo.RedirectStandardOutput = true;
                this.prcMinecraft.StartInfo.WorkingDirectory       = this.strWorkingDir;

                //Set up events
                this.prcMinecraft.OutputDataReceived += new DataReceivedEventHandler(ServerOutput);
                this.prcMinecraft.ErrorDataReceived  += new DataReceivedEventHandler(ServerError);
                this.prcMinecraft.EnableRaisingEvents = true;
                this.prcMinecraft.Exited += new EventHandler(ServerExited);

                //Finally start the thing
                this.prcMinecraft.Start();
                this.prcMinecraft.BeginOutputReadLine();
                this.prcMinecraft.BeginErrorReadLine();

                this.Running  = true;
                this.SafeStop = false;
                Database.AddLog("Server Started: " + strArgs, "server", "info", false, this.ServerID);

                //Try and open the firewall port
                Networking.OpenFirewallPort(this.Port, this.ServerTitle);
                Networking.OpenUPnP(this.Port, this.ServerTitle);

                //Save the process ID so we can kill if there is a crash
                this.PID = this.prcMinecraft.Id;
                Util.AddPID(this.prcMinecraft.Id);
            }
            catch (Exception e)
            {
                Database.AddLog("Failed to start Server: " + e.Message, "library", "error", false, this.ServerID);
            }
        }
示例#2
0
        public static void UpdateDB()
        {
            switch (Convert.ToInt32(GetSetting("DBSchema", "YAMS")))
            {
            case 1:
                //Update from Schema 1
                Database.SaveSetting("StoragePath", Core.RootFolder + @"\servers\");
                Database.SaveSetting("DBSchema", "2");
                goto case 2;

            case 2:
                //Update from Schema 2
                Database.SaveSetting("UsageData", "true");
                Database.SaveSetting("DBSchema", "3");
                goto case 3;

            case 3:
                Database.SaveSetting("EnablePortForwarding", "true");
                Database.SaveSetting("EnableOpenFirewall", "true");
                Database.SaveSetting("YAMSListenIP", Networking.GetListenIP().ToString());
                AddJob("update", -1, 0, "", 0);
                AddJob("backup", -1, 30, "", 1);
                Database.SaveSetting("DBSchema", "4");
                goto case 4;

            //goto case 3; //etc
            case 4:
                Database.SaveSetting("DNSName", "");
                Database.SaveSetting("DNSSecret", "");
                Database.SaveSetting("LastExternalIP", "");
                Database.SaveSetting("DBSchema", "5");
                goto case 5;

            case 5:
                Database.SaveSetting("EnablePublicSite", "true");
                Database.SaveSetting("DBSchema", "6");
                goto case 6;

            case 6:
                Database.SaveSetting("EnableTelnet", "false");
                Database.SaveSetting("TelnetPort", "56553");
                Database.SaveSetting("DBSchema", "7");
                goto case 7;

            case 7:
                Database.SaveSetting("BukkitBetaInstalled", "false");
                Database.SaveSetting("BukkitDevInstalled", "false");
                Database.SaveSetting("DBSchema", "8");
                goto case 8;

            case 8:
                Database.ExecuteSQL("ALTER TABLE MCServers ADD COLUMN ServerCustomJAR ntext");
                Database.ExecuteSQL("ALTER TABLE MCServers ADD COLUMN ServerWebBody ntext");
                Database.SaveSetting("DBSchema", "9");
                goto case 9;

            case 9:
                break;

            default:
                break;
            }
        }
示例#3
0
        //Control
        public static void Init()
        {
            //See if there is a new version of the web files waiting before we start the server
            if (File.Exists(Core.RootFolder + @"\web.zip"))
            {
                if (Directory.Exists(Core.RootFolder + @"\web\"))
                {
                    Directory.Delete(Core.RootFolder + @"\web\", true);
                }
                Directory.CreateDirectory(YAMS.Core.RootFolder + @"\web\");
                AutoUpdate.ExtractZip(YAMS.Core.RootFolder + @"\web.zip", YAMS.Core.RootFolder + @"\web\");
                File.Delete(Core.RootFolder + @"\web.zip");
            }

            adminServer = new Server();

            //Handle the requests for static files
            var adminModule = new FileModule();

            adminModule.Resources.Add(new FileResources("/assets/", YAMS.Core.RootFolder + "\\web\\assets\\"));
            adminServer.Add(adminModule);
            //Handle requests to API
            adminServer.Add(new Web.AdminAPI());

            adminServer.Add(HttpListener.Create(IPAddress.Any, Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS"))));
            adminServer.ErrorPageRequested += new EventHandler <ErrorPageEventArgs>(myServer_ErrorPageRequested);
            adminServerThread = new Thread(new ThreadStart(StartAdmin));
            adminServerThread.Start();

            //Open firewall ports
            if (Database.GetSetting("EnableOpenFirewall", "YAMS") == "true")
            {
                Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")), "Admin website");
            }

            if (Database.GetSetting("EnablePortForwarding", "YAMS") == "true")
            {
                Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("AdminListenPort", "YAMS")), "Admin website", YAMS.Database.GetSetting("YAMSListenIP", "YAMS"));
            }

            if (Database.GetSetting("EnablePublicSite", "YAMS") == "true")
            {
                //Add any server specific folders
                publicServer = new Server();
                var publicModule = new FileModule();
                publicModule.Resources.Add(new FileResources("/assets/", YAMS.Core.RootFolder + "\\web\\assets\\"));
                SqlCeDataReader readerServers = YAMS.Database.GetServers();
                while (readerServers.Read())
                {
                    var intServerID = readerServers["ServerID"].ToString();
                    if (!Directory.Exists(Core.StoragePath + intServerID + "\\renders\\"))
                    {
                        Directory.CreateDirectory(Core.StoragePath + intServerID + "\\renders\\");
                    }
                    publicModule.Resources.Add(new FileResources("/servers/" + intServerID + "/renders/", Core.StoragePath + intServerID + "\\renders\\"));
                    if (!Directory.Exists(Core.StoragePath + intServerID + "\\backups\\"))
                    {
                        Directory.CreateDirectory(Core.StoragePath + intServerID + "\\backups\\");
                    }
                    publicModule.Resources.Add(new FileResources("/servers/" + intServerID + "/backups/", Core.StoragePath + intServerID + "\\backups\\"));
                }
                publicServer.Add(publicModule);

                //Handle requests to API
                publicServer.Add(new Web.PublicAPI());
                publicServer.Add(HttpListener.Create(IPAddress.Any, Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS"))));
                publicServer.ErrorPageRequested += new EventHandler <ErrorPageEventArgs>(myServer_ErrorPageRequested);
                publicServerThread = new Thread(new ThreadStart(StartPublic));
                publicServerThread.Start();

                //Open firewall ports
                if (Database.GetSetting("EnableOpenFirewall", "YAMS") == "true")
                {
                    Networking.OpenFirewallPort(Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")), "Public website");
                }

                if (Database.GetSetting("EnablePortForwarding", "YAMS") == "true")
                {
                    Networking.OpenUPnP(Convert.ToInt32(YAMS.Database.GetSetting("PublicListenPort", "YAMS")), "Public website", YAMS.Database.GetSetting("YAMSListenIP", "YAMS"));
                }
            }
        }