Files
minecraft-modpack-updater/ModpackUpdater/Program.cs
2024-06-20 06:30:24 +02:00

91 lines
3.4 KiB
C#

using ModpackUpdater.Manager;
using ModpackUpdater.Model;
using Mono.Options;
using Newtonsoft.Json;
using Pilz.Configuration;
using Telerik.WinControls;
namespace ModpackUpdater;
public static class Program
{
private static readonly SettingsManager settingsManager;
public static ISettings Settings => settingsManager.Instance;
static Program()
{
settingsManager = new(GetSettingsPath(2), true);
MigrateLegacySettings(GetSettingsPath(null));
}
[STAThread]
internal static void Main(string[] args)
{
var options = new Options(args);
if (options.NoUi)
InstallWithoutGui(options.ModpackConfig, options.ProfileFolder, options.Silent, options.Side, options.AllowUpdaterAfterInstall);
else
RunApp(options.ModpackConfig, options.ProfileFolder, options.Side, options.AllowUpdaterAfterInstall, !options.NoUpdate);
}
private static void RunApp(string modpackConfig, string profileFolder, Side side, bool allowUpdaterAfterInstall, bool allowUpdateCheck)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
if (ThemeResolutionService.LoadPackageResource("ModpackUpdater.CustomThemes.Office2019DarkBluePurple.tssp"))
ThemeResolutionService.ApplicationThemeName = "Office2019DarkBluePurple";
Application.Run(new Form1(modpackConfig, profileFolder, side, allowUpdaterAfterInstall, allowUpdateCheck));
}
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;
newConfig.LastConfigFilePath = legacyConfig.LastConfigFilePath;
// Ensure save settings
settingsManager.Save();
// Delete legacy config file
File.Delete(settingsPath);
}
private static void InstallWithoutGui(string modpackConfig, string profileFolder, bool silent, Side side, bool allowUpdaterAfterInstall)
{
var config = ModpackConfig.LoadFromUrl(modpackConfig);
var installer = new ModpackInstaller(config, profileFolder);
var result = installer.Check(side, allowUpdaterAfterInstall).Result;
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");
}
}