Skip to content

Commit

Permalink
add more CF/Modrinth APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Apr 13, 2024
1 parent f0bf316 commit 9a596fc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
10 changes: 10 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/ModrinthAPIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ await res.Content.ReadFromJsonAsync(ModrinthProjectDependencyInfoContext.Default

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
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 @@ -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; }
}
16 changes: 16 additions & 0 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public void Install()
InstallTaskAsync().Wait();
}

async ValueTask<(bool, DownloadFile?)> TryGuessModDownloadLink(long fileId, string downloadPath)
public static async ValueTask<(string? FileName, string? Url)> TryGuessModDownloadLink(long fileId)
{
try
{
var files = await CurseForgeAPIHelper.GetFiles(new[] { fileId });

if (files == null || files.Length == 0) return (false, null);
if (files == null || files.Length == 0) return default;

var file = files.FirstOrDefault(f => f.Id == fileId);

if (file == null || string.IsNullOrEmpty(file.FileName)) return (false, null);
if (file == null || string.IsNullOrEmpty(file.FileName)) return default;

var fileName = file.FileName;
var fileIdStr = fileId.ToString();
Expand All @@ -57,27 +57,36 @@ public void Install()

if (!checkRes.IsSuccessStatusCode) continue;

var df = new DownloadFile
{
DownloadPath = downloadPath,
DownloadUri = url,
FileName = fileName
};

df.Completed += WhenCompleted;

return (true, df);
return (fileName, url);
}

return (false, null);
return default;
}
catch (Exception e)
{
Debug.WriteLine(e);
return (false, null);
return default;
}
}

async ValueTask<(bool, DownloadFile?)> TryGuessModDownloadLink(long fileId, string downloadPath)
{
var pair = await TryGuessModDownloadLink(fileId);

if (string.IsNullOrEmpty(pair.FileName) || string.IsNullOrEmpty(pair.Url)) return (false, null);

var df = new DownloadFile
{
DownloadPath = downloadPath,
DownloadUri = pair.Url,
FileName = pair.FileName
};

df.Completed += WhenCompleted;

return (true, df);
}

public async Task InstallTaskAsync()
{
ArgumentException.ThrowIfNullOrEmpty(GameId);
Expand Down

0 comments on commit 9a596fc

Please sign in to comment.