193 lines
6.2 KiB
C#
193 lines
6.2 KiB
C#
using Castle.Core.Logging;
|
|
using Gtk;
|
|
using Microsoft.Win32;
|
|
using ModpackUpdater.Manager;
|
|
using Newtonsoft.Json;
|
|
using Pilz;
|
|
using Pilz.Configuration;
|
|
using Pilz.UI.Gtk.Symbols;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
[assembly: AssemblyAppVersion("1.8.0.0")]
|
|
|
|
namespace ModpackUpdater.Apps.Client;
|
|
|
|
public static class Program
|
|
{
|
|
private static readonly SettingsManager settingsManager;
|
|
private static readonly ILogger log = new ConsoleLogger();
|
|
private static Application uiApp;
|
|
|
|
public static IGtkSymbolFactory<AppSymbols> Symbols { get; } = new AppSymbolFactory();
|
|
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
|
|
{
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
ShowWindow(GetConsoleWindow(), 0);
|
|
RunApp(Options.UpdateOptions);
|
|
}
|
|
}
|
|
|
|
private static void RunApp(UpdateCheckOptionsAdv updateOptions)
|
|
{
|
|
if (UsesWindowsDarkTheme())
|
|
Environment.SetEnvironmentVariable("GTK_THEME", "Adwaita:dark");
|
|
|
|
Application.Init();
|
|
|
|
uiApp = new Application("ModpackUpdater.Apps.Client", GLib.ApplicationFlags.None);
|
|
uiApp.Register(GLib.Cancellable.Current);
|
|
|
|
var win = new MainWindow(updateOptions);
|
|
uiApp.AddWindow(win);
|
|
win.Show();
|
|
|
|
Application.Run();
|
|
}
|
|
|
|
public static void Restart()
|
|
{
|
|
var process = Process.GetCurrentProcess();
|
|
var arguments = Environment.GetCommandLineArgs();
|
|
|
|
ProcessStartInfo currentStartInfo = new()
|
|
{
|
|
FileName = Environment.ProcessPath,
|
|
};
|
|
|
|
if (arguments.Length >= 2)
|
|
{
|
|
var sb = new StringBuilder((arguments.Length - 1) * 16);
|
|
|
|
for (int argumentIndex = 1; argumentIndex < arguments.Length; argumentIndex++)
|
|
sb.Append($"\"{arguments[argumentIndex]}\" ");
|
|
|
|
currentStartInfo.Arguments = sb.ToString(0, sb.Length - 1);
|
|
}
|
|
|
|
// Close the current application
|
|
Application.Quit();
|
|
|
|
// Start a new process
|
|
Process.Start(currentStartInfo);
|
|
|
|
// Close this proccess
|
|
Environment.Exit(0);
|
|
}
|
|
|
|
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)
|
|
{
|
|
OverwriteRefTag = Options.RefTag,
|
|
OverwriteVersion = Options.Version,
|
|
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;
|
|
}
|
|
|
|
private static bool UsesWindowsDarkTheme()
|
|
{
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
try
|
|
{
|
|
return Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize")?.GetValue("AppsUseLightTheme") as bool? ?? default;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
return default;
|
|
}
|
|
}
|