diff --git a/ModpackUpdater.Manager/ModpackInstaller.cs b/ModpackUpdater.Manager/ModpackInstaller.cs index 1f32809..11ec1c1 100644 --- a/ModpackUpdater.Manager/ModpackInstaller.cs +++ b/ModpackUpdater.Manager/ModpackInstaller.cs @@ -81,7 +81,10 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) var infos = await DownloadInstallInfos(); if (infos is not null && infos.Actions.Count != 0) + { result.Actions.AddRange(infos.Actions); + result.LatestVersion = infos.Version; + } else result.HasError = true; } @@ -176,32 +179,45 @@ public class ModpackInstaller(ModpackConfig updateConfig, string localPath) { Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)); - // Download - var fsDestinationPath = isZip ? Path.Combine(Path.GetTempPath(), $"mc_update_file_{DateTime.Now.ToBinary()}.zip") : destFilePath; - var sRemote = await httpClient.GetStreamAsync(sourceUrl); - var fs = new FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite); - await sRemote.CopyToAsync(fs); - sRemote.Close(); - fs.Close(); - - // Handle zip file - if (isZip) + if (!isZip || localZipCache.FirstOrDefault(n => n.DownloadUrl == sourceUrl) is not LocalZipCacheEntry cachedZipInfo) { + // Download + var fsDestinationPath = isZip ? Path.Combine(Path.GetTempPath(), $"mc_update_file_{DateTime.Now.ToBinary()}.zip") : destFilePath; + var sRemote = await httpClient.GetStreamAsync(sourceUrl); + var fs = new FileStream(fsDestinationPath, FileMode.Create, FileAccess.ReadWrite); + await sRemote.CopyToAsync(fs); + await fs.FlushAsync(); + sRemote.Close(); + fs.Close(); + // Extract - var zipDir = $"{Path.GetFileNameWithoutExtension(fsDestinationPath)}_extracted"; - ZipFile.ExtractToDirectory(fsDestinationPath, zipDir); - - // Copy content - var zipSrc = Path.Combine(zipDir, zipPath); - Extensions.CopyDirectory(zipSrc, destFilePath, true); - - // Delete/cache temporary files - File.Delete(fsDestinationPath); - localZipCache?.Add(new LocalZipCacheEntry + if (isZip) { - DownloadUrl = sourceUrl, - ExtractedZipPath = zipDir - }); + // Extract files + var zipDir = Path.Combine(Path.GetDirectoryName(fsDestinationPath), Path.GetFileNameWithoutExtension(fsDestinationPath)); + ZipFile.ExtractToDirectory(fsDestinationPath, zipDir); + + // Create cache entry + cachedZipInfo = new() + { + DownloadUrl = sourceUrl, + ExtractedZipPath = zipDir + }; + localZipCache.Add(cachedZipInfo); + + // Remofe temp zip file + File.Delete(fsDestinationPath); + } + else + cachedZipInfo = null; + } + + // Handle zip file content + if (cachedZipInfo != null) + { + // Copy content + var zipSrc = Path.Combine(cachedZipInfo.ExtractedZipPath, zipPath); + Extensions.CopyDirectory(zipSrc, destFilePath, true); } } } \ No newline at end of file diff --git a/ModpackUpdater.Model/InstallInfos.cs b/ModpackUpdater.Model/InstallInfos.cs index fc543d3..14d29f3 100644 --- a/ModpackUpdater.Model/InstallInfos.cs +++ b/ModpackUpdater.Model/InstallInfos.cs @@ -1,10 +1,13 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace ModpackUpdater.Model; public class InstallInfos { - public List Actions { get; private set; } = []; + [JsonConverter(typeof(VersionConverter))] + public Version Version { get; set; } + public List Actions { get; } = []; public static InstallInfos Parse(string content) { diff --git a/ModpackUpdater/Form1.cs b/ModpackUpdater/Form1.cs index c01b56e..9b39587 100644 --- a/ModpackUpdater/Form1.cs +++ b/ModpackUpdater/Form1.cs @@ -1,6 +1,7 @@ using ModpackUpdater.Manager; using ModpackUpdater.Model; using ModpackUpdater.My.Resources; +using System.Diagnostics; using Telerik.WinControls; using Telerik.WinControls.UI; @@ -142,9 +143,11 @@ public partial class Form1 else SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px); } - catch + catch(Exception) { SetStatus(LangRes.StatusText_ErrorWhileUpdateCheckOrUpdate, MySymbols.icons8_delete_16px); + if (Debugger.IsAttached) + throw; } finally { diff --git a/ModpackUpdater/Program.cs b/ModpackUpdater/Program.cs index a60a921..4444ff2 100644 --- a/ModpackUpdater/Program.cs +++ b/ModpackUpdater/Program.cs @@ -17,6 +17,7 @@ public static class Program MigrateLegacySettings(GetSettingsPath(null)); } + [STAThread] internal static void Main(string[] args) { var options = new Options(args);