some more bugfixes & cleanup

This commit is contained in:
2024-06-22 13:39:17 +02:00
parent 4b78ce1b6f
commit 11e9290fc8
5 changed files with 150 additions and 124 deletions

View File

@@ -2,7 +2,6 @@
namespace ModpackUpdater.Manager; namespace ModpackUpdater.Manager;
public static class Extensions public static class Extensions
{ {
public static bool IsSide(this Side @this, Side side) public static bool IsSide(this Side @this, Side side)
@@ -30,7 +29,7 @@ public static class Extensions
foreach (FileInfo @file in dir.GetFiles()) foreach (FileInfo @file in dir.GetFiles())
{ {
string targetFilePath = Path.Combine(destinationDir, @file.Name); string targetFilePath = Path.Combine(destinationDir, @file.Name);
@file.CopyTo(targetFilePath); @file.CopyTo(targetFilePath, true);
} }
// If recursive and copying subdirectories, recursively call this method // If recursive and copying subdirectories, recursively call this method
@@ -43,5 +42,4 @@ public static class Extensions
} }
} }
} }
} }

View File

@@ -42,6 +42,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
InstallInfos installInfos = null; InstallInfos installInfos = null;
UpdateInfos updateInfos = null; UpdateInfos updateInfos = null;
var result = new UpdateCheckResult(); var result = new UpdateCheckResult();
var exists = modpackInfo.Exists;
if (updateConfig.Maintenance && !options.IgnoreMaintenance) if (updateConfig.Maintenance && !options.IgnoreMaintenance)
{ {
@@ -55,7 +56,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
return result; return result;
} }
if (!modpackInfo.Exists) if (!exists)
{ {
installInfos = await DownloadInstallInfos(); installInfos = await DownloadInstallInfos();
@@ -66,6 +67,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
{ {
result.Actions.AddRange(installInfos.Actions); result.Actions.AddRange(installInfos.Actions);
result.LatestVersion = installInfos.Version; result.LatestVersion = installInfos.Version;
result.CurrentVersion = installInfos.Version;
} }
} }
@@ -73,7 +75,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
result.HasError = true; result.HasError = true;
} }
if (options.AllowUpdaterAfterInstall) if (exists || options.AllowUpdaterAfterInstall)
{ {
updateInfos = await DownloadUpdateInfos(); updateInfos = await DownloadUpdateInfos();
@@ -81,11 +83,13 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
{ {
var updatesOrderes = updateInfos.Updates.OrderByDescending(n => n.Version); var updatesOrderes = updateInfos.Updates.OrderByDescending(n => n.Version);
result.LatestVersion = updatesOrderes.First().Version; result.LatestVersion = updatesOrderes.First().Version;
result.CurrentVersion = modpackInfo.Version; if (exists)
result.CurrentVersion = modpackInfo.Version;
result.IsInstalled = true; result.IsInstalled = true;
var checkingVersionIndex = 0; var checkingVersionIndex = 0;
var checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex); var checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex);
var actionsZeroIndex = result.Actions.Count; // Ensure we insert update actions behind install actions
while (checkingVersion is not null && checkingVersion.Version > result.CurrentVersion) while (checkingVersion is not null && checkingVersion.Version > result.CurrentVersion)
{ {
@@ -97,7 +101,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
actionsToAdd.Add(action); actionsToAdd.Add(action);
} }
result.Actions.InsertRange(0, actionsToAdd); result.Actions.InsertRange(actionsZeroIndex, actionsToAdd);
checkingVersionIndex += 1; checkingVersionIndex += 1;
checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex); checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex);

View File

@@ -16,7 +16,7 @@ public class ModpackInfo
[JsonIgnore] [JsonIgnore]
public string LocaLPath { get; private set; } public string LocaLPath { get; private set; }
[JsonIgnore] [JsonIgnore]
public bool Exists => Directory.Exists(LocaLPath); public bool Exists => File.Exists(GetFilePath(LocaLPath));
public void Save() public void Save()
{ {

View File

@@ -22,6 +22,8 @@ public partial class Form1
if (!string.IsNullOrWhiteSpace(updateOptions.ProfileFolder)) if (!string.IsNullOrWhiteSpace(updateOptions.ProfileFolder))
LoadMinecraftProfile(updateOptions.ProfileFolder); LoadMinecraftProfile(updateOptions.ProfileFolder);
else if (!string.IsNullOrWhiteSpace(AppConfig.Instance.LastMinecraftProfilePath))
LoadMinecraftProfile(AppConfig.Instance.LastMinecraftProfilePath);
} }
public Form1() public Form1()
@@ -38,44 +40,49 @@ public partial class Form1
RadButton_PasteModpackConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SvgImageSize.Small); RadButton_PasteModpackConfig.SvgImage = AppSymbolFactory.Instance.GetSvgImage(AppSymbols.paste, SvgImageSize.Small);
} }
private bool IsMinecaftProfileLoaded() private void LoadMinecraftProfile(string folderPath)
{ {
return !string.IsNullOrEmpty(GetMinecraftProfilePath()); RadTextBoxControl_MinecraftProfileFolder.Text = folderPath;
} AppConfig.Instance.LastMinecraftProfilePath = folderPath;
private string GetMinecraftProfilePath() if (string.IsNullOrWhiteSpace(folderPath))
{ return;
return RadTextBoxControl_MinecraftProfileFolder.Text.Trim();
}
private bool IsUpdateConfigLoaded() try
{
return !string.IsNullOrEmpty(GetUpdateconfigPath());
}
private string GetUpdateconfigPath()
{
return RadTextBoxControl_ModpackConfig.Text.Trim();
}
private bool CheckStatus()
{
bool CheckStatusRet;
if (!IsMinecaftProfileLoaded())
{ {
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small)); modpackInfo = ModpackInfo.TryLoad(folderPath);
CheckStatusRet = false; RadTextBoxControl_ModpackConfig.Text = modpackInfo.ExtrasKey;
radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey;
} }
else if (!IsUpdateConfigLoaded()) catch
{ {
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
CheckStatusRet = false;
} }
else
CheckStatusRet = true;
return CheckStatusRet; CheckStatusAndUpdate();
}
private void LoadUpdateConfigFile(string filePath)
{
RadTextBoxControl_ModpackConfig.Text = filePath;
if (string.IsNullOrWhiteSpace(filePath))
return;
try
{
updateConfig = ModpackConfig.LoadFromUrl(filePath);
}
catch (Exception)
{
}
CheckStatusAndUpdate();
}
private void LoadInstallKey(string installKey)
{
radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey = installKey;
CheckStatusAndUpdate();
} }
private void SetStatus(string statusText, RadSvgImage image) private void SetStatus(string statusText, RadSvgImage image)
@@ -90,58 +97,47 @@ public partial class Form1
RadLabel_Status.SvgImage = null; RadLabel_Status.SvgImage = null;
} }
private void LoadMinecraftProfile(string folderPath) private void CheckStatusAndUpdate()
{ {
RadTextBoxControl_MinecraftProfileFolder.Text = folderPath; if (CheckStatus())
RadButton_CheckForUpdates.PerformClick();
if (IsMinecaftProfileLoaded())
{
try
{
modpackInfo = ModpackInfo.TryLoad(folderPath);
radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey;
}
catch
{
RadTextBoxControl_MinecraftProfileFolder.Text = string.Empty;
}
if (string.IsNullOrWhiteSpace(RadTextBoxControl_ModpackConfig.Text) && !string.IsNullOrWhiteSpace(modpackInfo?.ConfigUrl))
LoadUpdateConfigFile(modpackInfo.ConfigUrl);
else if (IsUpdateConfigLoaded())
RadButton_CheckForUpdates.PerformClick();
}
else
ClearStatus();
} }
private void LoadUpdateConfigFile(string filePath) private bool CheckStatus()
{ {
static ModpackConfig loadConfig(string filePath) if (modpackInfo == null || string.IsNullOrWhiteSpace(RadTextBoxControl_MinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/)
{ {
try SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
{ RadButton_PasteModpackConfig.Enabled = false;
return ModpackConfig.LoadFromUrl(filePath); radButton_PasteInstallKey.Enabled = false;
} RadButton_CheckForUpdates.Enabled = false;
catch (Exception) RadButton_Install.Enabled = false;
{ return false;
} }
return null; else if (updateConfig == null || string.IsNullOrWhiteSpace(RadTextBoxControl_ModpackConfig.Text))
{
SetStatus(LangRes.StatusText_ConfigIncompleteOrNotLoaded, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
RadButton_PasteModpackConfig.Enabled = true;
radButton_PasteInstallKey.Enabled = false;
RadButton_CheckForUpdates.Enabled = false;
RadButton_Install.Enabled = false;
return false;
}
else if (updateConfig.Maintenance && !updateOptions.IgnoreMaintenance)
{
SetStatus(LangRes.StatusText_Maintenance, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.services, SvgImageSize.Small));
RadButton_PasteModpackConfig.Enabled = true;
radButton_PasteInstallKey.Enabled = true;
RadButton_CheckForUpdates.Enabled = false;
RadButton_Install.Enabled = false;
return false;
} }
RadTextBoxControl_ModpackConfig.Text = filePath; RadButton_PasteModpackConfig.Enabled = true;
radButton_PasteInstallKey.Enabled = true;
if (!string.IsNullOrWhiteSpace(filePath) && loadConfig(filePath) is ModpackConfig modpackConfig) RadButton_CheckForUpdates.Enabled = true;
{ RadButton_Install.Enabled = false;
if (updateConfig.Maintenance && !updateOptions.IgnoreMaintenance) return true;
SetStatus(LangRes.StatusText_Maintenance, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.services, SvgImageSize.Small));
else
RadButton_CheckForUpdates.PerformClick();
}
else
{
ClearStatus();
}
} }
private async Task ExecuteUpdate(bool doInstall) private async Task ExecuteUpdate(bool doInstall)
@@ -150,63 +146,93 @@ public partial class Form1
updater.InstallProgessUpdated += Update_InstallProgessUpdated; updater.InstallProgessUpdated += Update_InstallProgessUpdated;
updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated; updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated;
void error()
{
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small));
currentUpdating = false;
}
void installing()
{
SetStatus(LangRes.StatusText_Installing, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small));
currentUpdating = true;
}
void updatesAvailable()
{
SetStatus(LangRes.StatusText_UpdateAvailable, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small));
}
void everythingOk()
{
SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small));
currentUpdating = false;
}
// Check only if not pressed "install", not really needed otherwise. // Check only if not pressed "install", not really needed otherwise.
if (lastUpdateCheckResult is null || !doInstall) if (lastUpdateCheckResult is null || !doInstall)
{ {
SetStatus(LangRes.StatusText_CheckingForUpdates, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small)); SetStatus(LangRes.StatusText_CheckingForUpdates, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small));
// Check for extras once again // Check for extras once again
modpackInfo.ExtrasKey = radTextBoxControl_InstallKey.Text.Trim();
updateOptions.IncludeExtras = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(modpackInfo, updateConfig)); updateOptions.IncludeExtras = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(modpackInfo, updateConfig));
try try
{ {
lastUpdateCheckResult = await updater.Check(updateOptions); lastUpdateCheckResult = await updater.Check(updateOptions);
} }
catch catch(Exception)
{ {
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); error();
if (Debugger.IsAttached)
throw;
} }
finally finally
{ {
} }
} }
// Error while update check
if (lastUpdateCheckResult is null || lastUpdateCheckResult.HasError) if (lastUpdateCheckResult is null || lastUpdateCheckResult.HasError)
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small));
else if (lastUpdateCheckResult.HasUpdates)
{ {
if (doInstall) error();
{ return;
SetStatus(LangRes.StatusText_Installing, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small)); }
currentUpdating = true;
// No updates available
try if (!lastUpdateCheckResult.HasUpdates)
{ {
if (await updater.Install(lastUpdateCheckResult) == true) everythingOk();
{ return;
lastUpdateCheckResult = null; // Reset last update check, a new one would be needed now. }
SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small));
} // Updates available (but don't install)
else if (!doInstall)
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small)); {
} updatesAvailable();
catch (Exception) return;
{ }
SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.close, SvgImageSize.Small));
if (Debugger.IsAttached) // Install updates
throw; installing();
} currentUpdating = true;
finally try
{ {
currentUpdating = false; // Install
} if (await updater.Install(lastUpdateCheckResult) == false)
} {
else error();
SetStatus(LangRes.StatusText_UpdateAvailable, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.software_installer, SvgImageSize.Small)); return;
}
// Success
lastUpdateCheckResult = null; // Reset last update check, a new one would be needed now.
everythingOk();
}
catch (Exception)
{
// Error
error();
if (Debugger.IsAttached)
throw;
} }
else
SetStatus(LangRes.StatusTest_EverythingOk, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.done, SvgImageSize.Small));
} }
private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs) private void Update_InstallProgessUpdated(UpdateCheckResult result, int processedSyncs)
@@ -238,7 +264,7 @@ public partial class Form1
{ {
if (modpackInfo == null) if (modpackInfo == null)
return; return;
radTextBoxControl_InstallKey.Text = modpackInfo.ExtrasKey = Clipboard.GetText(); LoadInstallKey(Clipboard.GetText());
} }
private void RadButton_RefreshConfig_Click(object sender, EventArgs e) private void RadButton_RefreshConfig_Click(object sender, EventArgs e)
@@ -249,8 +275,7 @@ public partial class Form1
private async void ButtonX_CheckForUpdates_Click(object sender, EventArgs e) private async void ButtonX_CheckForUpdates_Click(object sender, EventArgs e)
{ {
ClearStatus(); ClearStatus();
if (CheckStatus()) await ExecuteUpdate(false);
await ExecuteUpdate(false);
} }
private async void ButtonX_StartUpdate_Click(object sender, EventArgs e) private async void ButtonX_StartUpdate_Click(object sender, EventArgs e)
@@ -258,8 +283,7 @@ public partial class Form1
if (!currentUpdating) if (!currentUpdating)
{ {
ClearStatus(); ClearStatus();
if (CheckStatus()) await ExecuteUpdate(true);
await ExecuteUpdate(true);
} }
} }

View File

@@ -37,7 +37,7 @@ public static class Program
if (ThemeResolutionService.LoadPackageResource("ModpackUpdater.CustomThemes.Office2019DarkBluePurple.tssp")) if (ThemeResolutionService.LoadPackageResource("ModpackUpdater.CustomThemes.Office2019DarkBluePurple.tssp"))
ThemeResolutionService.ApplicationThemeName = "Office2019DarkBluePurple"; ThemeResolutionService.ApplicationThemeName = "Office2019DarkBluePurple";
Application.Run(new Form1(updateOptions)); Application.Run(new Form1(updateOptions));
} }
private static string GetSettingsPath(int? settingsVersion = 2) private static string GetSettingsPath(int? settingsVersion = 2)