Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple fixes and improvements #141

Merged
merged 8 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions ProjBobcat/ProjBobcat/Class/Helper/CurseForgeAPIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ namespace ProjBobcat.Class.Helper;

record AddonInfoReqModel(IEnumerable<long> modIds);
record FileInfoReqModel(IEnumerable<long> fileIds);
record FuzzyFingerPrintReqModel(IEnumerable<long> fingerprints);

[JsonSerializable(typeof(AddonInfoReqModel))]
[JsonSerializable(typeof(FileInfoReqModel))]
[JsonSerializable(typeof(FuzzyFingerPrintReqModel))]
[JsonSerializable(typeof(DataModelWithPagination<CurseForgeAddonInfo[]>))]
[JsonSerializable(typeof(DataModel<CurseForgeAddonInfo>))]
[JsonSerializable(typeof(DataModel<CurseForgeAddonInfo[]>))]
[JsonSerializable(typeof(DataModel<CurseForgeLatestFileModel[]>))]
[JsonSerializable(typeof(DataModel<CurseForgeSearchCategoryModel[]>))]
[JsonSerializable(typeof(DataModel<CurseForgeFeaturedAddonModel>))]
[JsonSerializable(typeof(DataModel<CurseForgeFuzzySearchResponseModel>))]
[JsonSerializable(typeof(DataModel<string>))]
partial class CurseForgeModelContext : JsonSerializerContext
{

}
partial class CurseForgeModelContext : JsonSerializerContext;

#endregion

Expand Down Expand Up @@ -109,7 +109,7 @@ public static void SetApiKey(string apiKey)

public static async Task<CurseForgeLatestFileModel[]?> GetFiles(IEnumerable<long> fileIds)
{
var reqUrl = $"{BaseUrl}/mods/files";
const string reqUrl = $"{BaseUrl}/mods/files";
var data = JsonSerializer.Serialize(new FileInfoReqModel(fileIds),
CurseForgeModelContext.Default.FileInfoReqModel);

Expand Down Expand Up @@ -173,4 +173,21 @@ public static void SetApiKey(string apiKey)

return (await res.Content.ReadFromJsonAsync(CurseForgeModelContext.Default.DataModelString))?.Data;
}

public static async Task<CurseForgeFuzzySearchResponseModel?> TryFuzzySearchFile(long fingerprint, int gameId = 432)
{
var reqUrl = $"{BaseUrl}/fingerprints/{gameId}";

var data = JsonSerializer.Serialize(new FuzzyFingerPrintReqModel([fingerprint]),
CurseForgeModelContext.Default.FuzzyFingerPrintReqModel);

using var req = Req(HttpMethod.Post, reqUrl);
req.Content = new StringContent(data, Encoding.UTF8, "application/json");

using var res = await Client.SendAsync(req);
res.EnsureSuccessStatusCode();

return (await res.Content.ReadFromJsonAsync(CurseForgeModelContext.Default
.DataModelCurseForgeFuzzySearchResponseModel))?.Data;
}
}
32 changes: 31 additions & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/ModrinthAPIHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Net.Http;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
using ProjBobcat.Class.Model;
using ProjBobcat.Class.Model.Modrinth;

namespace ProjBobcat.Class.Helper;
Expand Down Expand Up @@ -80,4 +82,32 @@ await res.Content.ReadFromJsonAsync(ModrinthProjectDependencyInfoContext.Default

return resModel;
}

public static async Task<ModrinthVersionInfo?> TryMatchVersionFileByHash(
string hash,
HashType hashType)
{
var para = hashType switch
{
HashType.SHA1 => "?algorithm=sha1",
HashType.SHA512 => "?algorithm=sha512",
_ => throw new ArgumentOutOfRangeException(nameof(hashType), hashType, null)
};
var reqUrl = $"{BaseUrl}/version_file/{hash}{para}";

using var res = await Get(reqUrl);
var resModel = await res.Content.ReadFromJsonAsync(ModrinthVersionInfoContext.Default.ModrinthVersionInfo);

return resModel;
}

public static async Task<ModrinthVersionInfo?> GetVersionInfo(string projectId, string versionId)
{
var reqUrl = $"{BaseUrl}/project/{projectId}/version/{versionId}";

using var res = await Get(reqUrl);
var resModel = await res.Content.ReadFromJsonAsync(ModrinthVersionInfoContext.Default.ModrinthVersionInfo);

return resModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public class SearchOptions
{
public int? CategoryId { get; init; }
public int? ClassId { get; init; }
public int? ParentCategoryId { get; init; }
public int? GameId { get; init; }
public string? GameVersion { get; init; }
public int? Index { get; init; }
Expand All @@ -27,8 +29,10 @@ public override string ToString()
result += $"&gameVersion={GameVersion}";
if (!string.IsNullOrEmpty(SearchFilter))
result += $"&searchFilter={SearchFilter}";
if (ClassId != null && ParentCategoryId is null)
result += $"&classId={ClassId}";
if (CategoryId != null)
result += $"&classId={CategoryId}";
result += $"&categoryId={CategoryId}";

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model.CurseForge;
Expand All @@ -20,7 +21,7 @@ public class CurseForgeAddonInfo
[JsonPropertyName("gameId")] public int GameId { get; set; }

[JsonPropertyName("summary")] public string? Summary { get; set; }
[JsonPropertyName("links")] public IReadOnlyDictionary<string, string> Links { get; set; } = new Dictionary<string, string>();
[JsonPropertyName("links")] public IReadOnlyDictionary<string, string> Links { get; set; } = ImmutableDictionary<string, string>.Empty;

[JsonPropertyName("defaultFileId")] public int DefaultFileId { get; set; }
[JsonPropertyName("releaseType")] public int ReleaseType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model.CurseForge;

public class CurseForgeFuzzySearchFileModel
{
[JsonPropertyName("id")]
public long Id { get; set; }

[JsonPropertyName("file")]
public CurseForgeLatestFileModel? File { get; set; }
}

public class CurseForgeFuzzySearchResponseModel
{
[JsonPropertyName("isCacheBuilt")]
public bool IsCacheBuilt { get; set; }

[JsonPropertyName("exactMatches")]
public CurseForgeFuzzySearchFileModel[]? ExactMatches { get; set; }

[JsonPropertyName("exactFingerprints")]
public long[]? ExactFingerprints { get; set; }

[JsonPropertyName("partialMatches")]
public CurseForgeFuzzySearchFileModel[]? PartialMatches { get; set; }

[JsonPropertyName("partialMatchFingerprints")]
public JsonElement? PartialMatchFingerprints { get; set; }

[JsonPropertyName("installedFingerprints")]
public long[]? InstalledFingerprints { get; set; }

[JsonPropertyName("unmatchedFingerprints")]
public long[]? UnmatchedFingerprints { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CurseForgeLatestFileModel

[JsonPropertyName("fileStatus")] public int FileStatus { get; set; }

[JsonPropertyName("downloadUrl")] public required string DownloadUrl { get; init; }
[JsonPropertyName("downloadUrl")] public string? DownloadUrl { get; set; }

[JsonPropertyName("isAlternate")] public bool IsAlternate { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ public class CurseForgeManifestModel
}

[JsonSerializable(typeof(CurseForgeManifestModel))]
partial class CurseForgeManifestModelContext : JsonSerializerContext
{
}
public partial class CurseForgeManifestModelContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public class CurseForgeSearchCategoryModel

[JsonPropertyName("gameId")] public int? GameId { get; set; }

[JsonPropertyName("classId")] public int? ClassId { get; set; }

[JsonPropertyName("isClass")] public bool? IsClass { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model.Forge;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class ForgeInstallProfile

[JsonPropertyName("data")]
public IReadOnlyDictionary<string, ForgeInstallProfileData> Data { get; set; } =
new Dictionary<string, ForgeInstallProfileData>();
ImmutableDictionary<string, ForgeInstallProfileData>.Empty;

[JsonPropertyName("processors")] public ForgeInstallProfileProcessor[] Processors { get; set; } = [];

Expand Down
7 changes: 3 additions & 4 deletions ProjBobcat/ProjBobcat/Class/Model/GameRulesModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model;
Expand All @@ -8,11 +9,9 @@ public class GameRules
[JsonPropertyName("action")] public required string Action { get; init; }

[JsonPropertyName("features")]
public IReadOnlyDictionary<string, bool> Features { get; set; } = new Dictionary<string, bool>();
public IReadOnlyDictionary<string, bool> Features { get; set; } = ImmutableDictionary<string, bool>.Empty;
}

[JsonSerializable(typeof(GameRules))]
[JsonSerializable(typeof(GameRules[]))]
partial class GameRulesContext : JsonSerializerContext
{
}
partial class GameRulesContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model.Modrinth;
Expand All @@ -8,7 +9,7 @@ public class ModrinthModPackFileModel
[JsonPropertyName("path")] public string? Path { get; set; }

[JsonPropertyName("hashes")]
public IReadOnlyDictionary<string, string> Hashes { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> Hashes { get; set; } = ImmutableDictionary<string, string>.Empty;

[JsonPropertyName("downloads")] public string[] Downloads { get; set; } = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace ProjBobcat.Class.Model.Modrinth;
Expand All @@ -18,10 +19,8 @@ public class ModrinthModPackIndexModel
[JsonPropertyName("files")] public ModrinthModPackFileModel[] Files { get; set; } = [];

[JsonPropertyName("dependencies")]
public IReadOnlyDictionary<string, string> Dependencies { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> Dependencies { get; set; } = ImmutableDictionary<string, string>.Empty;
}

[JsonSerializable(typeof(ModrinthModPackIndexModel))]
partial class ModrinthModPackIndexModelContext : JsonSerializerContext
{
}
public partial class ModrinthModPackIndexModelContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ public class ModrinthProjectDependencyInfo
}

[JsonSerializable(typeof(ModrinthProjectDependencyInfo))]
partial class ModrinthProjectDependencyInfoContext : JsonSerializerContext
{
}
partial class ModrinthProjectDependencyInfoContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,4 @@ public class ModrinthProjectInfo : ModrinthProjectInfoBase
}

[JsonSerializable(typeof(ModrinthProjectInfo))]
partial class ModrinthProjectInfoContext : JsonSerializerContext
{
}
partial class ModrinthProjectInfoContext : JsonSerializerContext;
22 changes: 19 additions & 3 deletions ProjBobcat/ProjBobcat/Class/Model/Modrinth/ModrinthVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

namespace ProjBobcat.Class.Model.Modrinth;

public class ModrinthDependencyModelComparer : IEqualityComparer<ModrinthDependency>
{
public bool Equals(ModrinthDependency? x, ModrinthDependency? y)
{
if (x == null && y == null) return false;
if (x == null || y == null) return false;

return x.ProjectId == y.ProjectId;
}

public int GetHashCode(ModrinthDependency obj)
{
return (obj.ProjectId ?? string.Empty).GetHashCode();
}
}

public class ModrinthFileInfo
{
[JsonPropertyName("hashes")] public IReadOnlyDictionary<string, string>? Hashes { get; set; }
Expand All @@ -13,6 +29,8 @@ public class ModrinthFileInfo
[JsonPropertyName("filename")] public required string FileName { get; init; }

[JsonPropertyName("primary")] public bool Primary { get; set; }

[JsonPropertyName("size")] public long Size { get; set; }
}

public class ModrinthDependency
Expand Down Expand Up @@ -59,6 +77,4 @@ public class ModrinthVersionInfo

[JsonSerializable(typeof(ModrinthVersionInfo))]
[JsonSerializable(typeof(ModrinthVersionInfo[]))]
partial class ModrinthVersionInfoContext : JsonSerializerContext
{
}
partial class ModrinthVersionInfoContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public class UserProfilePropertyValue
}

[JsonSerializable(typeof(UserProfilePropertyValue))]
public partial class UserProfilePropertyValueContext : JsonSerializerContext{}
public partial class UserProfilePropertyValueContext : JsonSerializerContext;
4 changes: 1 addition & 3 deletions ProjBobcat/ProjBobcat/Class/Model/RawVersionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,4 @@ public class RawVersionModel
}

[JsonSerializable(typeof(RawVersionModel))]
public partial class RawVersionModelContext : JsonSerializerContext
{
}
public partial class RawVersionModelContext : JsonSerializerContext;
2 changes: 2 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Model/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class VersionInfo

public required string DirName { get; init; }

public required string? InheritsFrom { get; set; }

public required string GameBaseVersion { get; init; }

public JavaVersionModel? JavaVersion { get; set; }
Expand Down
Loading