Skip to content

Commit

Permalink
添加Curseforge资源搜寻器,更新版本号
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Jan 31, 2024
1 parent 769dbcd commit 38e01fa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
1 change: 1 addition & 0 deletions MinecraftLaunch/Classes/Enums/LoaderType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace MinecraftLaunch.Classes.Enums;

public enum LoaderType {
Any,
Forge,
Quilt,
Fabric,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,4 @@ public sealed record CurseFileEntry {
public int ModId { get; set; }

public string DisplayDescription => $"{ModLoaderType} {McVersion}";
}

[JsonSerializable(typeof(CurseForgeResourceEntry))]
sealed partial class CurseForgeResourceEntryContext : JsonSerializerContext;
}
66 changes: 60 additions & 6 deletions MinecraftLaunch/Components/Fetcher/CurseForgeFetcher.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,79 @@
using System.Text.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Web;
using Flurl.Http;
using MinecraftLaunch.Classes.Enums;
using MinecraftLaunch.Extensions;
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Download;

namespace MinecraftLaunch.Components.Fetcher;

public sealed class CurseForgeFetcher : IFetcher<IEnumerable<CurseForgeResourceEntry>> {
public sealed class CurseForgeFetcher(string apiKey) : IFetcher<IEnumerable<CurseForgeResourceEntry>> {
private readonly string _key = apiKey;
private readonly string _api = "https://api.curseforge.com/v1/mods";

public IEnumerable<CurseForgeResourceEntry> Fetch() {
return FetchAsync().GetAwaiter().GetResult();
}

public ValueTask<IEnumerable<CurseForgeResourceEntry>> FetchAsync() {
throw new NotImplementedException();
public async ValueTask<IEnumerable<CurseForgeResourceEntry>> FetchAsync() {
var result = new List<CurseForgeResourceEntry>();
var payload = new {
gameId = 432,
excludedModIds = new[] { 0 },
gameVersionTypeId = null as string
};

try {
using var responseMessage = await $"{_api}/featured"
.WithHeader("x-api-key", _key)
.PostJsonAsync(payload);

var jsonNode = (await responseMessage.GetStringAsync())
.AsNode().Select("data");

var resources = jsonNode.GetEnumerable("featured")
.Union(jsonNode.GetEnumerable("popular"));

foreach (var resource in resources) {
result.Add(ResolveFromJsonNode(resource));
}
}
catch (Exception e) { }

return result;
}

public async ValueTask<IEnumerable<CurseForgeResourceEntry>> SearchResources(
string searchFilter,
int classId = 6,
int category = -1,
string gameVersion = null,
LoaderType modLoaderType = LoaderType.Any) {
var stringBuilder = new StringBuilder(_api);
stringBuilder.Append("/search?gameId=432");
stringBuilder.Append("&sortField=Featured");
stringBuilder.Append("&sortOrder=desc");
stringBuilder.Append($"&categoryId={category}&classId={classId}");
stringBuilder.Append($"modLoaderType={(int)modLoaderType}");
stringBuilder.Append($"gameVersion={gameVersion}");
stringBuilder.Append($"&searchFilter={HttpUtility.UrlEncode(searchFilter)}");

var jsonNode = (await stringBuilder.ToString()
.WithHeader("x-api-key", _key)
.GetStringAsync())
.AsNode();

return jsonNode.GetEnumerable("data").Select(ResolveFromJsonNode);
}

private CurseForgeResourceEntry ResolveFromJsonNode(JsonNode node) {
var entry = node.Deserialize<CurseForgeResourceEntry>();

entry.IconUrl = node["logo"]?.GetString("url");
entry.WebLink = node["links"]?.GetString("websiteUrl");
entry.IconUrl = node.Select("logo")?.GetString("url");
entry.WebLink = node.Select("links")?.GetString("websiteUrl");
entry.Authors = node?.GetEnumerable<string>("authors", "name");
entry.Categories = node?.GetEnumerable<string>("categories", "name");
entry.ScreenshotUrls = node?.GetEnumerable<string>("screenshots", "url");
Expand Down
2 changes: 1 addition & 1 deletion MinecraftLaunch/MinecraftLaunch.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>3.0.0-preview09</Version>
<Version>3.0.0-preview12</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 38e01fa

Please sign in to comment.