some more work
This commit is contained in:
82
ModpackUpdater.Manager/ModpackFactory.cs
Normal file
82
ModpackUpdater.Manager/ModpackFactory.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Modrinth;
|
||||
using Octokit;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ModpackUpdater.Manager;
|
||||
|
||||
public class ModpackFactory
|
||||
{
|
||||
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",
|
||||
});
|
||||
|
||||
~ModpackFactory()
|
||||
{
|
||||
curseForge.Dispose();
|
||||
modrinth.Dispose();
|
||||
}
|
||||
|
||||
public 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;
|
||||
}
|
||||
|
||||
public async Task<KeyValuePair<string, string>[]> FindUpdates(InstallAction action, ModLoader modLoader = ModLoader.Any, string? gameVersion = null)
|
||||
{
|
||||
if (action.SourceType == SourceType.GitHub)
|
||||
{
|
||||
var repo = await github.Repository.Get(action.SourceOwner, action.SourceName);
|
||||
var releases = await github.Repository.Release.GetAll(repo.Id);
|
||||
|
||||
return releases.Select(r => new KeyValuePair<string, string>(r.Name, r.TagName)).ToArray();
|
||||
}
|
||||
else if (action.SourceType == SourceType.GitLab)
|
||||
{
|
||||
throw new NotImplementedException("To be implemented soon.");
|
||||
}
|
||||
else if (action.SourceType == SourceType.CurseForge)
|
||||
{
|
||||
var res = await curseForge.GetModFilesAsync(Convert.ToInt32(action.SourceName), gameVersion: "1.7.10", modLoaderType: CurseForge.APIClient.Models.Mods.ModLoaderType.Forge);
|
||||
if (res.Data != null)
|
||||
return res.Data.Select(n => new KeyValuePair<string, string>(n.DisplayName, n.Id.ToString())).ToArray();
|
||||
}
|
||||
else if (action.SourceType == SourceType.Modrinth)
|
||||
{
|
||||
var res = await modrinth.Version.GetProjectVersionListAsync(action.SourceName);
|
||||
return res.Select(v => new KeyValuePair<string, string>($"{v.VersionNumber} {v.ProjectVersionType} {v.Name}", v.Id)).ToArray();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>true</ImplicitUsings>
|
||||
<Nullable>annotations</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
public class UpdateCheckOptions
|
||||
{
|
||||
public bool IgnoreMaintenance { get; set; }
|
||||
public bool IncludeNonPublic { get; set; }
|
||||
public bool AllowUpdaterAfterInstall { get; set; } = true;
|
||||
public Side Side { get; set; } = Side.Client;
|
||||
public bool IncludeExtras { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user