public override int InstallModWithTask(Service service, GenericMod gameMod)
        {
            var game      = new TCAdmin.GameHosting.SDK.Objects.Game(service.GameId);
            var providers = CustomModBase.GetCustomModBases();
            var provider  = providers.SingleOrDefault(p => p.Id == 3);
            var config    = provider.GetConfigurationForGame(game).ToObject <Configurations.MinecraftModpacksConfiguration>();
            var modpack   = (MinecraftModpacksBrowser)gameMod;
            var installed = this.GetInstalledPlugins(service);

            if (installed.Count > 0)
            {
                var mpid = installed[0].Replace("MCMP", string.Empty);
                mpid = mpid.Substring(0, mpid.IndexOf(":"));
                var other = MinecraftModpacksBrowser.GetPack(int.Parse(mpid));
                if (other.Id != modpack.Id)
                {
                    //Get it from the
                    if (other.Status == "error")
                    {
                        other = MinecraftModpacksBrowser.GetCurseforgePack(int.Parse(mpid));
                    }
                    throw new Exception(string.Format("Only one modpack can be installed at a time. Please uninstall {0}.", other.Name));
                }
            }

            var task = new TCAdmin.TaskScheduler.ModuleApi.TaskInfo();

            task.DisplayName = string.Format("Install {0} on {1}", modpack.Name, service.ConnectionInfo);
            task.CreatedBy   = TCAdmin.SDK.Session.GetCurrentUser().UserId;
            task.UserId      = service.UserId;
            task.Source      = service.GetType().ToString();
            task.SourceId    = service.ServiceId.ToString();
            task.RunNow      = true;

            var arguments = new ModpackInfo()
            {
                ServiceId   = service.ServiceId,
                ModpackId   = int.Parse(modpack.Id),
                VersionId   = int.Parse(System.Web.HttpContext.Current.Request.Form["version"]),
                Type        = System.Web.HttpContext.Current.Request.Form["type"] ?? "ftb",
                ModLoader   = System.Web.HttpContext.Current.Request.Form["modloader"] ?? "auto",
                RedirectUrl = System.Web.HttpContext.Current.Request.Form["redirect"],
                JarVariable = config.JarVariableName ?? "customjar"
            };
            var taskstep = new TCAdmin.TaskScheduler.ModuleApi.StepInfo();

            taskstep.ModuleId    = "b48cfbc9-7acc-4980-89c4-2b6a1f784aa0";
            taskstep.ProcessId   = 1;
            taskstep.ServerId    = service.ServerId;
            taskstep.DisplayName = string.Empty;
            taskstep.Arguments   = TCAdmin.SDK.Misc.ObjectXml.ObjectToXml(arguments);
            task.AddStep(taskstep);

            return(task.CreateTask(service.ServerId).TaskId);
        }
        public override GenericMod GetMod(string s, ModSearchType modSearchType)
        {
            var mod = MinecraftModpacksBrowser.GetPack(int.Parse(s));

            if (mod.Status != "success")
            {
                mod = MinecraftModpacksBrowser.GetCurseforgePack(int.Parse(s));
            }

            return(mod);
        }
        public override DataSourceResult GetMods([DataSourceRequest] DataSourceRequest request)
        {
            var installed  = this.GetInstalledPlugins(TCAdmin.GameHosting.SDK.Objects.Service.GetSelectedService());
            var filters    = request.GetAllFilterDescriptors();
            var query      = string.Empty;
            var termFilter = filters.FirstOrDefault(x => x.Member == "Term");
            var sortBy     = (filters.FirstOrDefault(x => x.Member == "SortBy") ?? new Kendo.Mvc.FilterDescriptor("SortBy", Kendo.Mvc.FilterOperator.Contains, installed.Count > 0 ? "installed" : "modpack/featured")).Value.ToString();
            List <MinecraftModpacksBrowser> mods = null;

            if (sortBy == "installed")
            {
                mods = new List <MinecraftModpacksBrowser>();
                foreach (var modpack in installed)
                {
                    var pack = MinecraftModpacksBrowser.GetPack(int.Parse(modpack.Split(':')[0].Replace("MCMP", string.Empty)));
                    if (pack.Status == "success")
                    {
                        mods.Add(pack);
                    }
                    else
                    {
                        pack = MinecraftModpacksBrowser.GetCurseforgePack(int.Parse(modpack.Split(':')[0].Replace("MCMP", string.Empty)));
                        if (pack.Status == "success")
                        {
                            mods.Add(pack);
                        }
                    }
                }
            }
            else
            {
                if (termFilter != null && !string.IsNullOrEmpty(termFilter.Value.ToString()))
                {
                    sortBy = "modpack/search";
                    query  = termFilter.Value.ToString();
                }

                mods = MinecraftModpacksBrowser.Search(sortBy, query, request.Page, request.PageSize);
            }

            request.Filters = new List <IFilterDescriptor>();
            var dataSourceResult = mods.ToDataSourceResult(request);

            dataSourceResult.Total = 5000;
            dataSourceResult.Data  = mods;
            return(dataSourceResult);
        }
예제 #4
0
        private void InstallModpack()
        {
            var modpackinfo     = (ModpackInfo)TCAdmin.SDK.Misc.ObjectXml.XmlToObject(this.Arguments.Arguments, typeof(ModpackInfo));
            var service         = new TCAdmin.GameHosting.SDK.Objects.Service(modpackinfo.ServiceId);
            var original_status = service.Status;

            try
            {
                if (original_status.ServiceStatus == TCAdmin.Interfaces.Server.ServiceStatus.Running)
                {
                    service.Stop("Modpack installation");
                }

                var install_data = System.IO.Path.Combine(service.RootDirectory, String.Format("Modpack-{0}.data", modpackinfo.ModpackId));
                if (System.IO.File.Exists(install_data))
                {
                    UninstallModpack();
                }

                var server   = new TCAdmin.GameHosting.SDK.Objects.Server(service.ServerId);
                var provider = new TCAdminCustomMods.Providers.MinecraftModpacksProvider();
                MinecraftModpacksBrowser genericMod = (MinecraftModpacksBrowser)provider.GetMod(modpackinfo.ModpackId.ToString(), Providers.ModSearchType.Id);
                var filepath = "Shared/bin-extensions/MinecraftModpack-Install-Script.txt";
                var script   = string.Empty;
                var utility  = service.GetScriptUtility();
                utility.ScriptEngineManager.AddVariable("Script.WorkingDirectory", service.RootDirectory);
                utility.AddObject("ThisModpackInfo", modpackinfo);
                utility.AddObject("ThisApiInfo", genericMod);
                utility.AddObject("ThisTaskStep", this);
                if (System.IO.File.Exists(filepath))
                {
                    script = System.IO.File.ReadAllText(filepath);
                }
                else
                {
                    script = PythonScripts.MinecraftModpack_Install_Script;
                }
                utility.ScriptEngineManager.SetScript("ipy", script, null);
                DeleteModpackCmdLines(service);

                var createdfiles = new List <string>();
                using (var filewatcher = new System.IO.FileSystemWatcher(service.RootDirectory, "*"))
                {
                    filewatcher.InternalBufferSize    = 32768;
                    filewatcher.IncludeSubdirectories = true;
                    filewatcher.Created += (object sender, System.IO.FileSystemEventArgs e) =>
                    {
                        createdfiles.Add(e.FullPath);
                    };
                    filewatcher.EnableRaisingEvents = true;

                    utility.ScriptEngineManager.Execute();
                    provider.PostInstallMod(service, genericMod);
                }

                createdfiles.Reverse();
                using (var file = System.IO.File.OpenWrite(install_data))
                {
                    using (var writer = new System.IO.StreamWriter(file))
                    {
                        var i = 0;
                        foreach (var createdfile in createdfiles)
                        {
                            i += 1;
                            if (i > 1)
                            {
                                writer.Write("\n");
                            }
                            writer.Write(TCAdmin.SDK.Misc.Strings.ReplaceCaseInsensitive(createdfile, service.RootDirectory, string.Empty));
                        }
                    }
                }

                //remove current version if it exists
                foreach (var installed in provider.GetInstalledPlugins(service))
                {
                    var packid = int.Parse(installed.Split(':')[0].Replace("MCMP", string.Empty));
                    if (packid == modpackinfo.ModpackId)
                    {
                        var verid = int.Parse(installed.Split(':')[1]);
                        provider.RemoveInstalledPlugin(service, packid, verid);
                    }
                }

                provider.AddInstalledPlugin(service, modpackinfo.ModpackId, modpackinfo.VersionId);

                //Repair permissions
                this.WriteLog("Setting file permissions...");
                var usercfgfile = string.Format("Services/{0}/User.cfg", service.ServiceId);
                if (System.IO.File.Exists(usercfgfile))
                {
                    var usercfg = new TCAdmin.SDK.Database.XmlField();
                    usercfg.LoadFromFile(usercfgfile);

                    switch (server.OperatingSystem)
                    {
                    case TCAdmin.SDK.Objects.OperatingSystem.Linux:
                        TCAdmin.SDK.Misc.Linux.SetDirectoryOwner(service.RootDirectory, usercfg["Service.User"].ToString(), true);
                        break;

                    case TCAdmin.SDK.Objects.OperatingSystem.Windows:
                        server.GameHostingUtilitiesService.ConfigureGameAccountPermissions(usercfg["Service.User"].ToString(), service.RootDirectory);
                        break;

                    default:
                        throw new NotImplementedException(server.OperatingSystem.ToString());
                    }
                }
                service.Configure();

                var responsedata = new TCAdmin.SDK.Database.XmlField();
                responsedata.SetValue("Task.RedirectUrl", modpackinfo.RedirectUrl);
                responsedata.SetValue("Task.RedirectUrlMvc", modpackinfo.RedirectUrl);
                this.SetResponse(responsedata.ToString());
                this.UpdateProgress(100);
            }
            finally
            {
                if (original_status.ServiceStatus == TCAdmin.Interfaces.Server.ServiceStatus.Running)
                {
                    service.Start("Modpack installation");
                }
            }
        }