From 00de4d8708955b327b6ac6ff055d031956f21902 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 20 Jun 2024 06:12:28 +0200 Subject: [PATCH] add installation without gui & pass arguments to gui --- ModpackUpdater.Model/ModpackConfig.cs | 2 +- ModpackUpdater/Form1.cs | 15 +++++++++++++-- ModpackUpdater/Options.cs | 6 ++++-- ModpackUpdater/Program.cs | 27 ++++++++++++++++++++------- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/ModpackUpdater.Model/ModpackConfig.cs b/ModpackUpdater.Model/ModpackConfig.cs index 30191a9..afaffd2 100644 --- a/ModpackUpdater.Model/ModpackConfig.cs +++ b/ModpackUpdater.Model/ModpackConfig.cs @@ -8,7 +8,7 @@ public class ModpackConfig public string UpdateUrl { get; set; } public string InstallUrl { get; set; } - public static object LoadFromUrl(string url) + public static ModpackConfig LoadFromUrl(string url) { string result = new HttpClient().GetStringAsync(url).Result; return JsonConvert.DeserializeObject(result); diff --git a/ModpackUpdater/Form1.cs b/ModpackUpdater/Form1.cs index 9b39587..ded2573 100644 --- a/ModpackUpdater/Form1.cs +++ b/ModpackUpdater/Form1.cs @@ -13,6 +13,15 @@ public partial class Form1 private bool currentUpdating = false; private UpdateCheckResult lastUpdateCheckResult = null; + public Form1(string modpackConfig, string profilePath) : this() + { + if (!string.IsNullOrWhiteSpace(modpackConfig)) + LoadUpdateConfigFile(modpackConfig); + + if (!string.IsNullOrWhiteSpace(profilePath)) + LoadMinecraftProfile(profilePath); + } + public Form1() { InitializeComponent(); @@ -84,10 +93,11 @@ public partial class Form1 private void LoadUpdateConfigFile(string filePath) { RadTextBoxControl_ModpackConfig.Text = filePath; + try { if (IsUpdateConfigLoaded()) - updateConfig = (ModpackConfig)ModpackConfig.LoadFromUrl(filePath); + updateConfig = ModpackConfig.LoadFromUrl(filePath); } catch { @@ -222,7 +232,8 @@ public partial class Form1 { if (Directory.Exists(AppConfig.Instance.LastMinecraftProfilePath)) LoadMinecraftProfile(AppConfig.Instance.LastMinecraftProfilePath); - LoadUpdateConfigFile(AppConfig.Instance.LastConfigFilePath); + if (!string.IsNullOrWhiteSpace(AppConfig.Instance.LastConfigFilePath)) + LoadUpdateConfigFile(AppConfig.Instance.LastConfigFilePath); } private async void Form1_Shown(object sender, EventArgs e) diff --git a/ModpackUpdater/Options.cs b/ModpackUpdater/Options.cs index 08e63c8..348e4fa 100644 --- a/ModpackUpdater/Options.cs +++ b/ModpackUpdater/Options.cs @@ -7,7 +7,8 @@ internal class Options private readonly List additionals = []; public IReadOnlyList Additionals => additionals; - public bool InstallWithoutUi { get; private set; } + public bool Silent { get; private set; } + public bool NoUi { get; private set; } public string ProfileFolder { get; private set; } public string ModpackConfig { get; private set; } @@ -15,7 +16,8 @@ internal class Options { var options = new OptionSet { - { "i", "Install without user interface.", n => InstallWithoutUi = n != null }, + { "s|silent", "Do not output anything.", s => Silent = s != null }, + { "n|noui", "Install without user interface.", n => NoUi = n != null }, { "p|profile=", "Sets the minecraft profile folder.", p => ProfileFolder = p }, { "c|config=", "Sets the minecraft profile folder.", c => ModpackConfig = c }, }; diff --git a/ModpackUpdater/Program.cs b/ModpackUpdater/Program.cs index 4444ff2..567d549 100644 --- a/ModpackUpdater/Program.cs +++ b/ModpackUpdater/Program.cs @@ -1,4 +1,6 @@ -using Mono.Options; +using ModpackUpdater.Manager; +using ModpackUpdater.Model; +using Mono.Options; using Newtonsoft.Json; using Pilz.Configuration; using Telerik.WinControls; @@ -21,13 +23,13 @@ public static class Program internal static void Main(string[] args) { var options = new Options(args); - if (options.InstallWithoutUi) - InstallWithoutGui(options.ProfileFolder, options.ModpackConfig); + if (options.NoUi) + InstallWithoutGui(options.ModpackConfig, options.ProfileFolder, options.Silent); else - RunApp(); + RunApp(options.ModpackConfig, options.ProfileFolder); } - private static void RunApp() + private static void RunApp(string modpackConfig, string profileFolder) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); @@ -36,7 +38,7 @@ public static class Program if (ThemeResolutionService.LoadPackageResource("ModpackUpdater.CustomThemes.Office2019DarkBluePurple.tssp")) ThemeResolutionService.ApplicationThemeName = "Office2019DarkBluePurple"; - Application.Run(new Form1()); + Application.Run(new Form1(modpackConfig, profileFolder)); } private static string GetSettingsPath(int? settingsVersion = 2) @@ -70,8 +72,19 @@ public static class Program File.Delete(settingsPath); } - private static void InstallWithoutGui(string profileFolder, string modpackConfig) + private static void InstallWithoutGui(string modpackConfig, string profileFolder, bool silent) { + var config = ModpackConfig.LoadFromUrl(modpackConfig); + var installer = new ModpackInstaller(config, profileFolder); + var result = installer.Check().Result; + if (result.HasUpdates) + { + var success = installer.Install(result).Result; + if (!silent) + Console.WriteLine($"Installation {(success ?? false ? "completed successfully" : "failed")}!"); + } + else if (!silent) + Console.WriteLine("No updates available"); } }