From 2467665cbb5f5cacd44e33dcb51e335f53189fd1 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Mon, 6 Mar 2023 18:28:02 -0500 Subject: [PATCH 1/3] add check for release hash in gitea release body --- Aki.Core/InternalData.cs | 2 ++ Aki.Core/Tasks/DownloadTask.cs | 2 +- Aki.Core/Tasks/ReleaseCheckTask.cs | 4 +++ Aki.Helper/DownloadCacheHelper.cs | 54 ++++++++++++++++-------------- Aki.Helper/FileHashHelper.cs | 16 ++++++++- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/Aki.Core/InternalData.cs b/Aki.Core/InternalData.cs index 221ad2a..6a72946 100644 --- a/Aki.Core/InternalData.cs +++ b/Aki.Core/InternalData.cs @@ -36,6 +36,8 @@ namespace SPT_AKI_Installer.Aki.Core /// public string AkiReleaseDownloadLink { get; set; } + 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()) From 398f1e38eb190e8e1d79f2f520fb938f0d6613fe Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Mon, 6 Mar 2023 18:29:33 -0500 Subject: [PATCH 2/3] forgot a summary --- Aki.Core/InternalData.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Aki.Core/InternalData.cs b/Aki.Core/InternalData.cs index 6a72946..c4de02b 100644 --- a/Aki.Core/InternalData.cs +++ b/Aki.Core/InternalData.cs @@ -36,6 +36,9 @@ namespace SPT_AKI_Installer.Aki.Core /// public string AkiReleaseDownloadLink { get; set; } + /// + /// The release zip hash + /// public string AkiReleaseHash { get; set; } = null; /// From ed2b64d3edb97c6b0df08fb5bb0f1114d3ca1920 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Mon, 6 Mar 2023 18:31:08 -0500 Subject: [PATCH 3/3] the weird line formatting was annoying me... --- Aki.Core/InternalData.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Aki.Core/InternalData.cs b/Aki.Core/InternalData.cs index c4de02b..7672384 100644 --- a/Aki.Core/InternalData.cs +++ b/Aki.Core/InternalData.cs @@ -37,8 +37,8 @@ namespace SPT_AKI_Installer.Aki.Core public string AkiReleaseDownloadLink { get; set; } /// - /// The release zip hash - /// + /// The release zip hash + /// public string AkiReleaseHash { get; set; } = null; ///