Files
minecraft-modpack-updater/ModpackUpdater.Apps.Client/Program.cs
2024-10-03 12:46:39 +02:00

123 lines
4.3 KiB
C#

using ModpackUpdater.Manager;
using Newtonsoft.Json;
using Pilz;
using Pilz.Configuration;
using System.Runtime.InteropServices;
[assembly: AssemblyAppVersion("1.6.0.0")]
namespace ModpackUpdater.Apps.Client;
public static class Program
{
private static readonly SettingsManager settingsManager;
public static ISettings Settings => settingsManager.Instance;
[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)
{
var 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<AppConfig>(File.ReadAllText(settingsPath)) is not AppConfig legacyConfig)
return;
// Migrate
var newConfig = Settings.Get<AppConfig>();
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);
var result = installer.Check(updateOptions).Result;
if (!silent && !updateOptions.NoUpdate && new AppUpdater().Check().Result)
Console.WriteLine("A new version is available!");
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");
}
private static string CheckModpackConfigUrl(string configUrl, ModpackInfo info)
{
if (string.IsNullOrWhiteSpace(configUrl) && !string.IsNullOrWhiteSpace(info.ConfigUrl))
return info.ConfigUrl;
return configUrl;
}
}