using Castle.Core.Logging; using ModpackUpdater.Manager; using Newtonsoft.Json; using Pilz; using Pilz.Configuration; using System.Runtime.InteropServices; [assembly: AssemblyAppVersion("1.6.6.0")] namespace ModpackUpdater.Apps.Client; public static class Program { private static readonly SettingsManager settingsManager; private static readonly ILogger log = new ConsoleLogger(); public static ISettings Settings => settingsManager.Instance; public static ILogger Log => log; internal static Options Options { get; private set; } [DllImport("kernel32.dll")] static extern nint GetConsoleWindow(); [DllImport("user32.dll")] static extern bool ShowWindow(nint hWnd, int nCmdShow); static Program() { settingsManager = new(GetSettingsPath(2), true); MigrateLegacySettings(GetSettingsPath(null)); } [STAThread] internal static void Main(string[] args) { Options = new Options(args); if (Options.Help) Options.DrawHelp(); else if (Options.NoUi) InstallWithoutGui(Options.UpdateOptions, Options.Silent); else { ShowWindow(GetConsoleWindow(), 0); RunApp(Options.UpdateOptions); } } private static void RunApp(UpdateCheckOptionsAdv updateOptions) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetHighDpiMode(HighDpiMode.PerMonitorV2); AppGlobals.Initialize(); Application.Run(new Form1(updateOptions)); } private static string GetSettingsPath(int? settingsVersion = 3) { const string AppDataDirectoryName = "MinecraftModpackUpdater"; var fileNamePostfix = settingsVersion == null ? string.Empty : $"V{settingsVersion}"; var SettingsFileName = $"Settings{fileNamePostfix}.json"; var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppDataDirectoryName); Directory.CreateDirectory(settingsPath); settingsPath = Path.Combine(settingsPath, SettingsFileName); return settingsPath; } private static void MigrateLegacySettings(string settingsPath) { // Try load legacy config file if (!File.Exists(settingsPath) || JsonConvert.DeserializeObject(File.ReadAllText(settingsPath)) is not AppConfig legacyConfig) return; // Migrate var newConfig = Settings.Get(); newConfig.LastMinecraftProfilePath = legacyConfig.LastMinecraftProfilePath; if (ModpackInfo.TryLoad(legacyConfig.LastMinecraftProfilePath) is ModpackInfo info) #pragma warning disable CS0612 // Typ oder Element ist veraltet info.ConfigUrl = legacyConfig.ConfigFilePath; // Ensure save settings settingsManager.Save(); // Delete legacy config file File.Delete(settingsPath); } private static void InstallWithoutGui(UpdateCheckOptionsAdv updateOptions, bool silent) { var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder); var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig, info)); var features = new ModpackFeatures(config); // Check features if (!string.IsNullOrWhiteSpace(updateOptions.ExtrasKey)) info.ExtrasKey = updateOptions.ExtrasKey; if (!string.IsNullOrWhiteSpace(info.ExtrasKey)) updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(info)); // Check for update var installer = new ModpackInstaller(config, info) { OverwriteRefTag = Options.RefTag, Log = Log, }; var result = installer.Check(updateOptions).Result; if (!silent && !updateOptions.NoUpdate && new AppUpdater().Check().Result) Log.Info("A new version is available!"); if (result.HasUpdates) { var success = installer.Install(result).Result; if (!silent) Log.Info($"Installation {(success ?? false ? "completed successfully" : "failed")}!"); } else if (!silent) Log.Info("No updates available"); } private static string CheckModpackConfigUrl(string configUrl, ModpackInfo info) { if (string.IsNullOrWhiteSpace(configUrl) && !string.IsNullOrWhiteSpace(info.ConfigUrl)) return info.ConfigUrl; return configUrl; } }