load modpack info before modpack installer

This commit is contained in:
2024-06-20 15:05:01 +02:00
parent 604d35856f
commit c97a04c4ce
6 changed files with 64 additions and 33 deletions

View File

@@ -1,11 +0,0 @@
namespace ModpackUpdater.Manager;
public class MinecraftProfileChecker
{
public static bool CheckProfile(string folderPath)
{
// Todo: Adds some checks to verify, if the current folder is a minecraft folder.
return true;
}
}

View File

@@ -3,7 +3,7 @@ using System.IO.Compression;
namespace ModpackUpdater.Manager;
public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInfo)
{
private class LocalZipCacheEntry
{
@@ -40,7 +40,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
public async Task<UpdateCheckResult> Check(UpdateCheckOptions options)
{
var result = new UpdateCheckResult();
var hasConfig = ModpackInfo.HasModpackInfo(localPath);
var hasConfig = modpackInfo.Exists;
InstallInfos installInfos = null;
UpdateInfos updateInfos = null;
@@ -71,7 +71,6 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
if (options.AllowUpdaterAfterInstall)
{
updateInfos = await DownloadUpdateInfos();
var modpackInfo = ModpackInfo.HasModpackInfo(localPath) ? ModpackInfo.Load(localPath) : new();
if (updateInfos is not null && updateInfos.Updates.Count != 0)
{
@@ -108,18 +107,12 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
public async Task<bool?> Install(UpdateCheckResult checkResult)
{
ModpackInfo modpackInfo;
int processed = 0;
var localZipCache = new List<LocalZipCacheEntry>();
if (ModpackInfo.HasModpackInfo(localPath))
modpackInfo = ModpackInfo.Load(localPath);
else
modpackInfo = new();
foreach (InstallAction iaction in checkResult.Actions)
{
string destFilePath = Path.Combine(localPath, iaction.DestPath);
string destFilePath = Path.Combine(modpackInfo.LocaLPath, iaction.DestPath);
if (iaction is UpdateAction uaction)
{
@@ -141,7 +134,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
break;
case UpdateActionType.Copy:
{
var srcFilePath = Path.Combine(localPath, uaction.SrcPath);
var srcFilePath = Path.Combine(modpackInfo.LocaLPath, uaction.SrcPath);
if (uaction.IsDirectory)
{
if (Directory.Exists(srcFilePath))
@@ -156,7 +149,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
break;
case UpdateActionType.Move:
{
var srcFilePath = Path.Combine(localPath, uaction.SrcPath);
var srcFilePath = Path.Combine(modpackInfo.LocaLPath, uaction.SrcPath);
if (uaction.IsDirectory)
{
if (Directory.Exists(srcFilePath))
@@ -180,7 +173,8 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath)
// Save new modpack info
modpackInfo.Version = checkResult.LatestVersion;
modpackInfo.Save(localPath);
modpackInfo.ConfigUrl = updateConfig.ConfigUrl;
modpackInfo.Save();
// Delete cached zip files
foreach (var task in localZipCache)

View File

@@ -9,9 +9,14 @@ public class ModpackConfig
public string UpdateUrl { get; set; }
public string InstallUrl { get; set; }
[JsonIgnore]
public string ConfigUrl { get; set; }
public static ModpackConfig LoadFromUrl(string url)
{
string result = new HttpClient().GetStringAsync(url).Result;
return JsonConvert.DeserializeObject<ModpackConfig>(result);
var config = JsonConvert.DeserializeObject<ModpackConfig>(result);
config.ConfigUrl = url;
return config;
}
}

View File

@@ -10,15 +10,35 @@ public class ModpackInfo
[JsonConverter(typeof(VersionConverter))]
public Version Version { get; set; }
public string ConfigUrl { get; set; }
[JsonIgnore]
public string LocaLPath { get; private set; }
[JsonIgnore]
public bool Exists => Directory.Exists(LocaLPath);
public void Save()
{
File.WriteAllText(Conversions.ToString(GetFilePath(LocaLPath)), JsonConvert.SerializeObject(this));
}
public void Save(string mcRoot)
{
File.WriteAllText(Conversions.ToString(GetFilePath(mcRoot)), JsonConvert.SerializeObject(this));
LocaLPath = mcRoot;
Save();
}
public static ModpackInfo TryLoad(string mcRoot)
{
if (HasModpackInfo(mcRoot))
return Load(mcRoot);
return new();
}
public static ModpackInfo Load(string mcRoot)
{
return JsonConvert.DeserializeObject<ModpackInfo>(File.ReadAllText(Conversions.ToString(GetFilePath(mcRoot))));
var info = JsonConvert.DeserializeObject<ModpackInfo>(File.ReadAllText(GetFilePath(mcRoot)));
info.LocaLPath = mcRoot;
return info;
}
public static bool HasModpackInfo(string mcRoot)
@@ -26,7 +46,7 @@ public class ModpackInfo
return File.Exists(Conversions.ToString(GetFilePath(mcRoot)));
}
private static object GetFilePath(string mcRoot)
private static string GetFilePath(string mcRoot)
{
return Path.Combine(mcRoot, FILENAME_MODPACKINFO);
}

View File

@@ -10,6 +10,7 @@ namespace ModpackUpdater;
public partial class Form1
{
private ModpackInfo modpackInfo = null;
private ModpackConfig updateConfig = new();
private bool currentUpdating = false;
private UpdateCheckResult lastUpdateCheckResult = null;
@@ -65,7 +66,7 @@ public partial class Form1
{
bool CheckStatusRet;
if (!IsMinecaftProfileLoaded() || !MinecraftProfileChecker.CheckProfile(GetMinecraftProfilePath()))
if (!IsMinecaftProfileLoaded())
{
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
CheckStatusRet = false;
@@ -98,7 +99,21 @@ public partial class Form1
RadTextBoxControl_MinecraftProfileFolder.Text = folderPath;
if (IsUpdateConfigLoaded())
{
try
{
modpackInfo = ModpackInfo.TryLoad(folderPath);
}
catch
{
RadTextBoxControl_MinecraftProfileFolder.Text = string.Empty;
}
if (string.IsNullOrWhiteSpace(RadTextBoxControl_ModpackConfig.Text) && !string.IsNullOrWhiteSpace(modpackInfo?.ConfigUrl))
LoadUpdateConfigFile(modpackInfo.ConfigUrl);
else
RadButton_CheckForUpdates.PerformClick();
}
else
ClearStatus();
}
@@ -127,7 +142,7 @@ public partial class Form1
private async Task ExecuteUpdate(bool doInstall)
{
var updater = new ModpackInstaller(updateConfig, GetMinecraftProfilePath());
var updater = new ModpackInstaller(updateConfig, modpackInfo);
updater.InstallProgessUpdated += Update_InstallProgessUpdated;
updater.CheckingProgressUpdated += Updated_CheckingProgresssUpdated;

View File

@@ -73,8 +73,9 @@ public static class Program
private static void InstallWithoutGui(UpdateCheckOptionsAdv updateOptions, bool silent)
{
var config = ModpackConfig.LoadFromUrl(updateOptions.ModpackConfig);
var installer = new ModpackInstaller(config, updateOptions.ProfileFolder);
var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder);
var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig, info));
var installer = new ModpackInstaller(config, info);
var result = installer.Check(updateOptions).Result;
if (!silent && !updateOptions.NoUpdate && new AppUpdater().Check().Result)
@@ -89,4 +90,11 @@ public static class Program
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;
}
}