some more work

This commit is contained in:
2024-09-25 12:08:38 +02:00
parent bc6c483ba6
commit 2f9d60f1a8
26 changed files with 2434 additions and 60 deletions

View File

@@ -1,9 +1,5 @@
using Modrinth;
using Modrinth.Extensions;
using Newtonsoft.Json;
using Octokit;
using Newtonsoft.Json;
using System.IO.Compression;
using System.Text.RegularExpressions;
using FileMode = System.IO.FileMode;
namespace ModpackUpdater.Manager;
@@ -24,19 +20,11 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
public delegate void CheckingProgressUpdatedEventHandler(int toCheck, int processed);
private readonly HttpClient http = new();
private readonly GitHubClient github = new(new ProductHeaderValue("MinecraftModpackUpdater"));
private readonly CurseForge.APIClient.ApiClient curseForge = new("b67bd218-e011-4963-ac8a-ffd025e1091f", "pilzinsel64@gmx.de");
private readonly ModrinthClient modrinth = new(new ModrinthClientConfig
{
ModrinthToken = "mrp_zUlDSET5actMUdTU3FK242TXgvlWgaErSSEFuegNG7thLgkC50IiK2NCGOzW",
UserAgent = "Pilz.ModpackUpater",
});
private readonly ModpackFactory factory = new();
~ModpackInstaller()
{
http.Dispose();
curseForge.Dispose();
modrinth.Dispose();
}
private async Task<UpdateInfos> DownloadUpdateInfos()
@@ -75,7 +63,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
// Check install actions
if (!exists)
{
if (installInfos is not null && installInfos.Actions.Count != 0)
if (installInfos is not null && installInfos.IsPublic && installInfos.Actions.Count != 0)
{
var actions = installInfos.Actions.Where(n => n.Side.IsSide(options.Side) && (!n.IsExtra || options.IncludeExtras));
if (actions.Any())
@@ -97,7 +85,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
if (updateInfos is not null && updateInfos.Updates.Count != 0)
{
var updatesOrderes = updateInfos.Updates.OrderByDescending(n => n.Version);
var updatesOrderes = updateInfos.Updates.Where(n => n.IsPublic || options.IncludeNonPublic).OrderByDescending(n => n.Version);
result.LatestVersion = updatesOrderes.First().Version;
if (exists)
result.CurrentVersion = modpackInfo.Version;
@@ -153,7 +141,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
foreach (InstallAction iaction in checkResult.Actions)
{
var destFilePath = Path.Combine(modpackInfo.LocaLPath, iaction.DestPath);
var sourceUrl = await ResolveSourceUrl(iaction);
var sourceUrl = await factory.ResolveSourceUrl(iaction);
if (iaction is UpdateAction uaction)
{
@@ -269,36 +257,4 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
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.LastOrDefault(asset => Regex.IsMatch(asset.Name, action.SourceRegex)) is ReleaseAsset asset)
return asset.BrowserDownloadUrl;
}
else if (action.SourceType == SourceType.GitLab)
{
throw new NotImplementedException("To be implemented soon.");
}
else if (action.SourceType == SourceType.CurseForge)
{
var res = await curseForge.GetModFileDownloadUrlAsync(Convert.ToInt32(action.SourceName), Convert.ToInt32(action.SourceTag));
if (!string.IsNullOrWhiteSpace(res.Data))
return res.Data;
}
else if (action.SourceType == SourceType.Modrinth)
{
var res = await modrinth.VersionFile.GetVersionByHashAsync(action.SourceTag);
var file = res.Files.Length == 1 ? res.Files[0] : res.Files.FirstOrDefault(n => Regex.IsMatch(n.FileName, action.SourceRegex));
if (file != null)
return file.Url;
}
return action.SourceUrl;
}
}