re-organze namespace
This commit is contained in:
123
ModpackUpdater.Apps.Client/Program.cs
Normal file
123
ModpackUpdater.Apps.Client/Program.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using ModpackUpdater.Manager;
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.Configuration;
|
||||
using System.Runtime.InteropServices;
|
||||
using Telerik.WinControls;
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user