add GitHub as source & allow inheritance
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
using ModpackUpdater.Model;
|
using ModpackUpdater.Model;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Octokit;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using FileMode = System.IO.FileMode;
|
||||||
|
|
||||||
namespace ModpackUpdater.Manager;
|
namespace ModpackUpdater.Manager;
|
||||||
|
|
||||||
@@ -18,22 +22,23 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
|
|
||||||
public delegate void CheckingProgressUpdatedEventHandler(int toCheck, int processed);
|
public delegate void CheckingProgressUpdatedEventHandler(int toCheck, int processed);
|
||||||
|
|
||||||
private readonly HttpClient httpClient = new();
|
private readonly HttpClient http = new();
|
||||||
|
private readonly GitHubClient github = new(new ProductHeaderValue("MinecraftModpackUpdater"));
|
||||||
|
|
||||||
~ModpackInstaller()
|
~ModpackInstaller()
|
||||||
{
|
{
|
||||||
httpClient.Dispose();
|
http.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<UpdateInfos> DownloadUpdateInfos()
|
private async Task<UpdateInfos> DownloadUpdateInfos()
|
||||||
{
|
{
|
||||||
var content = await httpClient.GetStringAsync(updateConfig.UpdateUrl);
|
var content = await http.GetStringAsync(updateConfig.UpdateUrl);
|
||||||
return UpdateInfos.Parse(content);
|
return UpdateInfos.Parse(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<InstallInfos> DownloadInstallInfos()
|
private async Task<InstallInfos> DownloadInstallInfos()
|
||||||
{
|
{
|
||||||
var content = await httpClient.GetStringAsync(updateConfig.InstallUrl);
|
var content = await http.GetStringAsync(updateConfig.InstallUrl);
|
||||||
return InstallInfos.Parse(content);
|
return InstallInfos.Parse(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +61,11 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!exists)
|
|
||||||
{
|
|
||||||
installInfos = await DownloadInstallInfos();
|
installInfos = await DownloadInstallInfos();
|
||||||
|
|
||||||
|
// Check install actions
|
||||||
|
if (!exists)
|
||||||
|
{
|
||||||
if (installInfos is not null && installInfos.Actions.Count != 0)
|
if (installInfos is not null && installInfos.Actions.Count != 0)
|
||||||
{
|
{
|
||||||
var actions = installInfos.Actions.Where(n => n.Side.IsSide(options.Side) && (!n.IsExtra || options.IncludeExtras));
|
var actions = installInfos.Actions.Where(n => n.Side.IsSide(options.Side) && (!n.IsExtra || options.IncludeExtras));
|
||||||
@@ -75,6 +81,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
result.HasError = true;
|
result.HasError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check update actions
|
||||||
if (exists || options.AllowUpdaterAfterInstall)
|
if (exists || options.AllowUpdaterAfterInstall)
|
||||||
{
|
{
|
||||||
updateInfos = await DownloadUpdateInfos();
|
updateInfos = await DownloadUpdateInfos();
|
||||||
@@ -97,6 +104,11 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
|
|
||||||
foreach (var action in checkingVersion.Actions)
|
foreach (var action in checkingVersion.Actions)
|
||||||
{
|
{
|
||||||
|
// Resolve inherits
|
||||||
|
if (!string.IsNullOrWhiteSpace(action.InheritFrom) && installInfos.Actions.FirstOrDefault(a => a.Name == action.InheritFrom) is InstallAction iaction)
|
||||||
|
JsonConvert.PopulateObject(JsonConvert.SerializeObject(iaction), action);
|
||||||
|
|
||||||
|
// Check & add
|
||||||
if (action.Side.IsSide(options.Side) && (!action.IsExtra || options.IncludeExtras) && !result.Actions.Any(n => n.DestPath == action.DestPath))
|
if (action.Side.IsSide(options.Side) && (!action.IsExtra || options.IncludeExtras) && !result.Actions.Any(n => n.DestPath == action.DestPath))
|
||||||
actionsToAdd.Add(action);
|
actionsToAdd.Add(action);
|
||||||
}
|
}
|
||||||
@@ -116,19 +128,20 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
|
|
||||||
public async Task<bool?> Install(UpdateCheckResult checkResult)
|
public async Task<bool?> Install(UpdateCheckResult checkResult)
|
||||||
{
|
{
|
||||||
int processed = 0;
|
var processed = 0;
|
||||||
var localZipCache = new List<LocalZipCacheEntry>();
|
var localZipCache = new List<LocalZipCacheEntry>();
|
||||||
|
|
||||||
foreach (InstallAction iaction in checkResult.Actions)
|
foreach (InstallAction iaction in checkResult.Actions)
|
||||||
{
|
{
|
||||||
string destFilePath = Path.Combine(modpackInfo.LocaLPath, iaction.DestPath);
|
var destFilePath = Path.Combine(modpackInfo.LocaLPath, iaction.DestPath);
|
||||||
|
var sourceUrl = await ResolveSourceUrl(iaction);
|
||||||
|
|
||||||
if (iaction is UpdateAction uaction)
|
if (iaction is UpdateAction uaction)
|
||||||
{
|
{
|
||||||
switch (uaction.Type)
|
switch (uaction.Type)
|
||||||
{
|
{
|
||||||
case UpdateActionType.Update:
|
case UpdateActionType.Update:
|
||||||
await InstallFile(destFilePath, uaction.DownloadUrl, uaction.IsZip, uaction.ZipPath, localZipCache);
|
await InstallFile(destFilePath, sourceUrl, uaction.IsZip, uaction.ZipPath, localZipCache);
|
||||||
break;
|
break;
|
||||||
case UpdateActionType.Delete:
|
case UpdateActionType.Delete:
|
||||||
{
|
{
|
||||||
@@ -174,7 +187,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
await InstallFile(destFilePath, iaction.DownloadUrl, iaction.IsZip, iaction.ZipPath, localZipCache);
|
await InstallFile(destFilePath, sourceUrl, iaction.IsZip, iaction.ZipPath, localZipCache);
|
||||||
|
|
||||||
processed += 1;
|
processed += 1;
|
||||||
InstallProgessUpdated?.Invoke(checkResult, processed);
|
InstallProgessUpdated?.Invoke(checkResult, processed);
|
||||||
@@ -200,7 +213,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
{
|
{
|
||||||
// Download
|
// Download
|
||||||
var fsDestinationPath = isZip ? Path.Combine(Path.GetTempPath(), $"mc_update_file_{DateTime.Now.ToBinary()}.zip") : destFilePath;
|
var fsDestinationPath = isZip ? Path.Combine(Path.GetTempPath(), $"mc_update_file_{DateTime.Now.ToBinary()}.zip") : destFilePath;
|
||||||
var sRemote = await httpClient.GetStreamAsync(sourceUrl);
|
var sRemote = await http.GetStreamAsync(sourceUrl);
|
||||||
var fs = new FileStream(fsDestinationPath, FileMode.Create, FileAccess.ReadWrite);
|
var fs = new FileStream(fsDestinationPath, FileMode.Create, FileAccess.ReadWrite);
|
||||||
await sRemote.CopyToAsync(fs);
|
await sRemote.CopyToAsync(fs);
|
||||||
await fs.FlushAsync();
|
await fs.FlushAsync();
|
||||||
@@ -237,4 +250,19 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
|
|||||||
Extensions.CopyDirectory(zipSrc, destFilePath, true);
|
Extensions.CopyDirectory(zipSrc, destFilePath, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<string> ResolveSourceUrl(InstallAction action)
|
||||||
|
{
|
||||||
|
if (action.SourceType == SourceType.GitHub)
|
||||||
|
{
|
||||||
|
var repo = await github.Repository.Get(action.SourceOwner, action.SourceName);
|
||||||
|
var release = await github.Repository.Release.Get(repo.Id, action.SourceTag);
|
||||||
|
var assets = await github.Repository.Release.GetAllAssets(repo.Id, release.Id);
|
||||||
|
|
||||||
|
if (assets.FirstOrDefault(asset => Regex.IsMatch(asset.Name, action.SourceRegex)) is ReleaseAsset asset)
|
||||||
|
return asset.BrowserDownloadUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return action.SourceUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Octokit" Version="13.0.1" />
|
||||||
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
|
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
|
||||||
<PackageReference Include="Unleash.Client" Version="4.1.11" />
|
<PackageReference Include="Unleash.Client" Version="4.1.11" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,11 +1,38 @@
|
|||||||
namespace ModpackUpdater.Model;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace ModpackUpdater.Model;
|
||||||
|
|
||||||
public class InstallAction
|
public class InstallAction
|
||||||
{
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string InheritFrom { get; set; }
|
||||||
|
|
||||||
public bool IsZip { get; set; }
|
public bool IsZip { get; set; }
|
||||||
|
|
||||||
public string ZipPath { get; set; }
|
public string ZipPath { get; set; }
|
||||||
|
|
||||||
public string DestPath { get; set; }
|
public string DestPath { get; set; }
|
||||||
public string DownloadUrl { get; set; }
|
|
||||||
|
public string SourceUrl { get; set; }
|
||||||
|
|
||||||
|
public SourceType SourceType { get; set; }
|
||||||
|
|
||||||
|
public string SourceOwner { get; set; }
|
||||||
|
|
||||||
|
public string SourceName { get; set; }
|
||||||
|
|
||||||
|
public string SourceRegex { get; set; }
|
||||||
|
|
||||||
|
public string SourceTag { get; set; }
|
||||||
|
|
||||||
public Side Side { get; set; } = Side.Both;
|
public Side Side { get; set; } = Side.Both;
|
||||||
|
|
||||||
public bool IsExtra { get; set; }
|
public bool IsExtra { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty, Obsolete]
|
||||||
|
private string DownloadUrl
|
||||||
|
{
|
||||||
|
set => SourceUrl = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
7
ModpackUpdater.Model/SourceType.cs
Normal file
7
ModpackUpdater.Model/SourceType.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace ModpackUpdater.Model;
|
||||||
|
|
||||||
|
public enum SourceType
|
||||||
|
{
|
||||||
|
DirectLink,
|
||||||
|
GitHub,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user