Skip to content

Commit

Permalink
cf: qualify mapping of excluded slug to mod IDs (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Jan 15, 2025
1 parent ced7a0a commit febc4e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
22 changes: 3 additions & 19 deletions src/main/java/me/itzg/helpers/curseforge/CurseForgeApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

/**
* Implements parts of the <a href="https://docs.curseforge.com/rest-api">CurseForge REST API</a>
*/
@Slf4j
public class CurseForgeApiClient implements AutoCloseable {

Expand Down Expand Up @@ -185,25 +188,6 @@ public CurseForgeFile resolveModpackFile(
});
}

Mono<Integer> slugToId(CategoryInfo categoryInfo,
String slug
) {
return preparedFetch
.fetch(
uriBuilder.resolve("/v1/mods/search?gameId={gameId}&slug={slug}", gameId, slug)
)
.toObject(ModsSearchResponse.class)
.assemble()
.map(resp ->
resp.getData().stream()
.filter(curseForgeMod -> categoryInfo.contentClassIds.containsKey(curseForgeMod.getClassId()))
.findFirst()
.map(CurseForgeMod::getId)
.orElseThrow(() -> new GenericException("Unable to resolve slug into ID (no matches): " + slug))
)
.onErrorMap(FailedRequestException::isForbidden, this::errorMapForbidden);
}

public Mono<CurseForgeMod> getModInfo(
int projectID
) {
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/me/itzg/helpers/curseforge/CurseForgeInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,15 +637,18 @@ private ExcludeIncludeIds resolveExcludeIncludes(InstallContext context) {
final ExcludeIncludes specific =
excludeIncludes.getModpacks() != null ? excludeIncludes.getModpacks().get(context.slug) : null;

final int modsClassId = context.categoryInfo.getClassIdForSlug(CurseForgeApiClient.CATEGORY_MC_MODS);

return Mono.zip(
resolveFromSlugOrIds(
context, context.categoryInfo,
context,
modsClassId,
excludeIncludes.getGlobalExcludes(),
specific != null ? specific.getExcludes() : null
),
resolveFromSlugOrIds(
context, context.categoryInfo,
excludeIncludes.getGlobalForceIncludes(),
context,
modsClassId, excludeIncludes.getGlobalForceIncludes(),
specific != null ? specific.getForceIncludes() : null
)
)
Expand All @@ -656,8 +659,8 @@ private ExcludeIncludeIds resolveExcludeIncludes(InstallContext context) {
}

private Mono<Set<Integer>> resolveFromSlugOrIds(
InstallContext context, CategoryInfo categoryInfo,
Collection<String> global, Collection<String> specific
InstallContext context,
int modsClassId, Collection<String> global, Collection<String> specific
) {
log.trace("Resolving slug|id into IDs global={} specific={}", global, specific);

Expand All @@ -668,10 +671,12 @@ private Mono<Set<Integer>> resolveFromSlugOrIds(
)
.flatMap(s -> {
try {
// Is it an integer already? If not, it'll drop into catch-block below
final int id = Integer.parseInt(s);
return Mono.just(id);
} catch (NumberFormatException e) {
return context.cfApi.slugToId(categoryInfo, s);
return context.cfApi.searchMod(s, modsClassId)
.map(CurseForgeMod::getId);
}
})
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Maps data model for <a href="https://docs.curseforge.com/rest-api">CurseForge REST API</a>
*/
package me.itzg.helpers.curseforge.model;

0 comments on commit febc4e5

Please sign in to comment.