83 lines
3.5 KiB
C#
83 lines
3.5 KiB
C#
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;
|
|
}
|
|
}
|