128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
using ModpackUpdater.Manager;
|
|
using ModpackUpdater.Model;
|
|
using Newtonsoft.Json;
|
|
using Pilz.Configuration;
|
|
using System.Runtime.InteropServices;
|
|
using Telerik.WinControls;
|
|
|
|
namespace ModpackUpdater;
|
|
|
|
public static class Program
|
|
{
|
|
private static readonly SettingsManager settingsManager;
|
|
|
|
public static ISettings Settings => settingsManager.Instance;
|
|
|
|
[DllImport("kernel32.dll")]
|
|
static extern IntPtr GetConsoleWindow();
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern bool ShowWindow(IntPtr 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);
|
|
|
|
if (ThemeResolutionService.LoadPackageResource("ModpackUpdater.CustomThemes.Office2019DarkBluePurple.tssp"))
|
|
ThemeResolutionService.ApplicationThemeName = "Office2019DarkBluePurple";
|
|
|
|
Application.Run(new Form1(updateOptions));
|
|
}
|
|
|
|
private static string GetSettingsPath(int? settingsVersion = 2)
|
|
{
|
|
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;
|
|
#pragma warning restore CS0612 // Typ oder Element ist veraltet
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|