Merge pull request 'Add gitea release hash check' (#11) from waffle.lord/SPT-AKI-Installer:master into master

Reviewed-on: CWX/SPT-AKI-Installer#11
This commit is contained in:
IsWaffle 2023-03-13 21:27:16 +00:00
commit 3ba2846d4b
5 changed files with 53 additions and 28 deletions

View File

@ -36,6 +36,11 @@ namespace SPT_AKI_Installer.Aki.Core
/// </summary> /// </summary>
public string AkiReleaseDownloadLink { get; set; } public string AkiReleaseDownloadLink { get; set; }
/// <summary>
/// The release zip hash
/// </summary>
public string AkiReleaseHash { get; set; } = null;
/// <summary> /// <summary>
/// The release download link for the patcher mirror list /// The release download link for the patcher mirror list
/// </summary> /// </summary>

View File

@ -117,7 +117,7 @@ namespace SPT_AKI_Installer.Aki.Core.Tasks
var akiProgress = new Progress<double>((d) => { Progress = (int)Math.Floor(d); }); var akiProgress = new Progress<double>((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) if (_data.AkiZipInfo == null)
{ {

View File

@ -1,7 +1,10 @@
using Gitea.Api; using Gitea.Api;
using Gitea.Client; using Gitea.Client;
using Gitea.Model;
using SPT_AKI_Installer.Aki.Core.Model; using SPT_AKI_Installer.Aki.Core.Model;
using SPT_AKI_Installer.Aki.Helper;
using System; using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SPT_AKI_Installer.Aki.Core.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.PatcherMirrorsLink = comparePatchToAki?.Assets[0].BrowserDownloadUrl;
_data.AkiReleaseDownloadLink = latestAkiRelease.Assets[0].BrowserDownloadUrl; _data.AkiReleaseDownloadLink = latestAkiRelease.Assets[0].BrowserDownloadUrl;
_data.AkiReleaseHash = FileHashHelper.GetGiteaReleaseHash(latestAkiRelease);
int IntAkiVersion = int.Parse(latestAkiVersion); int IntAkiVersion = int.Parse(latestAkiVersion);
int IntGameVersion = int.Parse(_data.OriginalGameVersion); int IntGameVersion = int.Parse(_data.OriginalGameVersion);

View File

@ -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 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<GenericResult> DownloadFile(FileInfo outputFile, string targetLink, IProgress<double> progress, string expectedHash = null) private static async Task<GenericResult> DownloadFile(FileInfo outputFile, string targetLink, IProgress<double> progress, string expectedHash = null)
{ {
try try
@ -45,19 +71,7 @@ namespace SPT_AKI_Installer.Aki.Helper
{ {
try try
{ {
cacheFile.Refresh(); if (CheckCache(cacheFile, expectedHash)) return GenericResult.FromSuccess();
Directory.CreateDirectory(_cachePath);
if (cacheFile.Exists)
{
if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash))
{
return GenericResult.FromSuccess();
}
cacheFile.Delete();
cacheFile.Refresh();
}
using var patcherFileStream = cacheFile.Open(FileMode.Create); using var patcherFileStream = cacheFile.Open(FileMode.Create);
{ {
@ -83,19 +97,7 @@ namespace SPT_AKI_Installer.Aki.Helper
{ {
try try
{ {
cacheFile.Refresh(); if (CheckCache(cacheFile, expectedHash)) return GenericResult.FromSuccess();
Directory.CreateDirectory(_cachePath);
if (cacheFile.Exists)
{
if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash))
{
return GenericResult.FromSuccess();
}
cacheFile.Delete();
cacheFile.Refresh();
}
return await DownloadFile(cacheFile, targetLink, progress, expectedHash); return await DownloadFile(cacheFile, targetLink, progress, expectedHash);
} }

View File

@ -1,12 +1,26 @@
using System; using Gitea.Model;
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace SPT_AKI_Installer.Aki.Helper namespace SPT_AKI_Installer.Aki.Helper
{ {
public static class FileHashHelper public static class FileHashHelper
{ {
public static string GetGiteaReleaseHash(Release release)
{
var regex = Regex.Match(release.Body, @"Release Hash: (?<hash>\S+)");
if (regex.Success)
{
return regex.Groups["hash"].Value;
}
return null;
}
public static bool CheckHash(FileInfo file, string expectedHash) public static bool CheckHash(FileInfo file, string expectedHash)
{ {
using (MD5 md5Service = MD5.Create()) using (MD5 md5Service = MD5.Create())