diff --git a/Aki.Core/InternalData.cs b/Aki.Core/InternalData.cs index 221ad2a..7672384 100644 --- a/Aki.Core/InternalData.cs +++ b/Aki.Core/InternalData.cs @@ -36,6 +36,11 @@ namespace SPT_AKI_Installer.Aki.Core /// public string AkiReleaseDownloadLink { get; set; } + /// + /// The release zip hash + /// + public string AkiReleaseHash { get; set; } = null; + /// /// The release download link for the patcher mirror list /// diff --git a/Aki.Core/Tasks/DownloadTask.cs b/Aki.Core/Tasks/DownloadTask.cs index 52815e5..7afa1fd 100644 --- a/Aki.Core/Tasks/DownloadTask.cs +++ b/Aki.Core/Tasks/DownloadTask.cs @@ -117,7 +117,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks var akiProgress = new Progress((d) => { Progress = (int)Math.Floor(d); }); - _data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki.zip", _data.AkiReleaseDownloadLink, akiProgress); + _data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki.zip", _data.AkiReleaseDownloadLink, akiProgress, _data.AkiReleaseHash); if (_data.AkiZipInfo == null) { diff --git a/Aki.Core/Tasks/ReleaseCheckTask.cs b/Aki.Core/Tasks/ReleaseCheckTask.cs index 865fd7d..76bfd05 100644 --- a/Aki.Core/Tasks/ReleaseCheckTask.cs +++ b/Aki.Core/Tasks/ReleaseCheckTask.cs @@ -1,7 +1,10 @@ using Gitea.Api; using Gitea.Client; +using Gitea.Model; using SPT_AKI_Installer.Aki.Core.Model; +using SPT_AKI_Installer.Aki.Helper; using System; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace SPT_AKI_Installer.Aki.Core.Tasks @@ -37,6 +40,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks _data.PatcherMirrorsLink = comparePatchToAki?.Assets[0].BrowserDownloadUrl; _data.AkiReleaseDownloadLink = latestAkiRelease.Assets[0].BrowserDownloadUrl; + _data.AkiReleaseHash = FileHashHelper.GetGiteaReleaseHash(latestAkiRelease); int IntAkiVersion = int.Parse(latestAkiVersion); int IntGameVersion = int.Parse(_data.OriginalGameVersion); diff --git a/Aki.Helper/DownloadCacheHelper.cs b/Aki.Helper/DownloadCacheHelper.cs index e1e3c04..5fcd2a8 100644 --- a/Aki.Helper/DownloadCacheHelper.cs +++ b/Aki.Helper/DownloadCacheHelper.cs @@ -13,6 +13,32 @@ namespace SPT_AKI_Installer.Aki.Helper private static string _cachePath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "spt-installer/cache"); + private static bool CheckCache(FileInfo cacheFile, string expectedHash = null) + { + try + { + cacheFile.Refresh(); + Directory.CreateDirectory(_cachePath); + + if (cacheFile.Exists) + { + if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash)) + { + return true; + } + + cacheFile.Delete(); + cacheFile.Refresh(); + } + + return false; + } + catch + { + return false; + } + } + private static async Task DownloadFile(FileInfo outputFile, string targetLink, IProgress progress, string expectedHash = null) { try @@ -45,19 +71,7 @@ namespace SPT_AKI_Installer.Aki.Helper { try { - cacheFile.Refresh(); - Directory.CreateDirectory(_cachePath); - - if (cacheFile.Exists) - { - if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash)) - { - return GenericResult.FromSuccess(); - } - - cacheFile.Delete(); - cacheFile.Refresh(); - } + if (CheckCache(cacheFile, expectedHash)) return GenericResult.FromSuccess(); using var patcherFileStream = cacheFile.Open(FileMode.Create); { @@ -83,19 +97,7 @@ namespace SPT_AKI_Installer.Aki.Helper { try { - cacheFile.Refresh(); - Directory.CreateDirectory(_cachePath); - - if (cacheFile.Exists) - { - if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash)) - { - return GenericResult.FromSuccess(); - } - - cacheFile.Delete(); - cacheFile.Refresh(); - } + if (CheckCache(cacheFile, expectedHash)) return GenericResult.FromSuccess(); return await DownloadFile(cacheFile, targetLink, progress, expectedHash); } diff --git a/Aki.Helper/FileHashHelper.cs b/Aki.Helper/FileHashHelper.cs index dead830..16d5cf8 100644 --- a/Aki.Helper/FileHashHelper.cs +++ b/Aki.Helper/FileHashHelper.cs @@ -1,12 +1,26 @@ -using System; +using Gitea.Model; +using System; using System.IO; using System.Linq; using System.Security.Cryptography; +using System.Text.RegularExpressions; namespace SPT_AKI_Installer.Aki.Helper { public static class FileHashHelper { + public static string GetGiteaReleaseHash(Release release) + { + var regex = Regex.Match(release.Body, @"Release Hash: (?\S+)"); + + if (regex.Success) + { + return regex.Groups["hash"].Value; + } + + return null; + } + public static bool CheckHash(FileInfo file, string expectedHash) { using (MD5 md5Service = MD5.Create())