public override int UninstallModWithTask(Service service, GenericMod gameMod)
        {
            var provider = new TCAdminCustomMods.Providers.MinecraftModpacksProvider();
            var modpack  = (MinecraftModpacksBrowser)gameMod;
            var task     = new TCAdmin.TaskScheduler.ModuleApi.TaskInfo();

            task.DisplayName = string.Format("Uninstall {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(provider.GetInstalledPlugins(service).SingleOrDefault(mp => mp.StartsWith(string.Format("MCMP{0}:", modpack.Id))).Split(':')[1]),
                Type        = System.Web.HttpContext.Current.Request.Form["type"] ?? "ftb",
                RedirectUrl = System.Web.HttpContext.Current.Request.Form["redirect"]
            };
            var taskstep = new TCAdmin.TaskScheduler.ModuleApi.StepInfo();

            taskstep.ModuleId    = "b48cfbc9-7acc-4980-89c4-2b6a1f784aa0";
            taskstep.ProcessId   = 2;
            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);
        }
Beispiel #2
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");
                }
            }
        }
Beispiel #3
0
        private void UninstallModpack()
        {
            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 uninstall");
                }

                var responsedata = new TCAdmin.SDK.Database.XmlField();
                responsedata.SetValue("Task.RedirectUrl", modpackinfo.RedirectUrl);
                responsedata.SetValue("Task.RedirectUrlMvc", modpackinfo.RedirectUrl);
                var datafile   = System.IO.Path.Combine(service.RootDirectory, string.Format("Modpack-{0}.data", modpackinfo.ModpackId));
                var provider   = new TCAdminCustomMods.Providers.MinecraftModpacksProvider();
                var genericMod = provider.GetMod(modpackinfo.ModpackId.ToString(), Providers.ModSearchType.Id);

                DeleteModpackCmdLines(service);

                if (!System.IO.File.Exists(datafile))
                {
                    this.WriteLog("Modpack data file does not exist so files can't be deleted.");
                    provider.RemoveInstalledPlugin(service, modpackinfo.ModpackId, modpackinfo.VersionId);
                    provider.PostUninstallMod(service, genericMod);
                    System.Threading.Thread.Sleep(5000);
                    this.SetResponse(responsedata.ToString());
                    this.UpdateProgress(100);
                    return;
                }

                this.WriteLog("Reading install data...");
                var files = new List <string>();
                using (var reader = System.IO.File.OpenText(datafile))
                {
                    string line;
                    while (!string.IsNullOrEmpty((line = reader.ReadLine())))
                    {
                        if (line.IndexOf("..") == -1)
                        {
                            var path = System.IO.Path.Combine(service.RootDirectory, line);
                            if (System.IO.File.Exists(path) | System.IO.Directory.Exists(path))
                            {
                                files.Add(line);
                            }
                        }
                    }
                }

                this.WriteLog("Deleting files...");

                if (files.Count > 0)
                {
                    var progress_increase = ((float)100) / files.Count;
                    var current_progress  = (float)0;
                    foreach (var file in files)
                    {
                        //this.WriteLog(string.Format("Deleting {0}...", file), short.Parse(Math.Round(current_progress, 0).ToString()));
                        this.UpdateProgress(short.Parse(Math.Round(current_progress, 0).ToString()));
                        var fullpath = System.IO.Path.Combine(service.RootDirectory, file);
                        if (System.IO.File.Exists(fullpath))
                        {
                            try
                            {
                                System.IO.File.Delete(fullpath);
                            }
                            catch (Exception ex) { this.WriteDebugLog(ex.ToString()); }
                        }
                        else if (System.IO.Directory.Exists(fullpath))
                        {
                            try
                            {
                                System.IO.Directory.Delete(fullpath);
                            }
                            catch (Exception ex) { this.WriteDebugLog(ex.ToString()); }
                        }
                        current_progress += progress_increase;
                    }
                }

                System.IO.File.Delete(datafile);

                provider.GetInstalledPlugins(service);

                provider.RemoveInstalledPlugin(service, modpackinfo.ModpackId, modpackinfo.VersionId);
                provider.PostUninstallMod(service, genericMod);

                this.SetResponse(responsedata.ToString());
                this.UpdateProgress(100);
            }
            finally
            {
                if (original_status.ServiceStatus == TCAdmin.Interfaces.Server.ServiceStatus.Running)
                {
                    service.Start("Modpack uninstall");
                }
            }
        }