Skip to content

Commit

Permalink
modrinth: add support for NeoForge as a modpack's modloader (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Feb 7, 2024
1 parent 4eb1814 commit 0b1b210
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .run/modrinth-modpack (slug).run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</envs>
<option name="MAIN_CLASS_NAME" value="me.itzg.helpers.McImageHelper" />
<module name="mc-image-helper.main" />
<option name="PROGRAM_PARAMETERS" value="install-modrinth-modpack --project=p87Jiw2q --version=1.6.1 --output-directory=tmp\modrinth-modpack --loader=forge --results-file=tmp\modrinth-modpack\.results.env --ignore-missing-files=Licensing.md" />
<option name="PROGRAM_PARAMETERS" value="install-modrinth-modpack --project=datacraft --output-directory=$USER_HOME$/tmp/modrinth-modpack" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="me.itzg.helpers.env.*" />
Expand Down
6 changes: 6 additions & 0 deletions dev/modrinth.http
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ GET https://api.modrinth.com/v2/project/cobblemon-fabric
###
GET https://api.modrinth.com/v2/project/nerbvival

###
GET https://api.modrinth.com/v2/project/datacraft

### Nerbvival v5
GET https://api.modrinth.com/v2/version/ayspYmZQ

Expand All @@ -20,6 +23,9 @@ GET https://api.modrinth.com/v2/projects?ids=["fabric-api","cloth-config"]
###
GET https://api.modrinth.com/v2/project/ae2/version?loaders=fabric&game_versions=1.20.1

###
GET https://api.modrinth.com/v2/project/datacraft/version?loaders=neoforge&game_versions=1.20.1

###
GET https://api.modrinth.com/v2/project/9s6osm5g/version?loaders=%5B%22fabric%22%5D&game_versions=%5B%221.19.2%22%5D

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.Callable;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import me.itzg.helpers.McImageHelper;
import me.itzg.helpers.files.Manifests;
Expand All @@ -12,6 +14,7 @@
import me.itzg.helpers.http.SharedFetch;
import me.itzg.helpers.http.SharedFetchArgs;
import me.itzg.helpers.modrinth.model.VersionType;
import org.jetbrains.annotations.VisibleForTesting;
import picocli.CommandLine;
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -107,11 +110,7 @@ public Integer call() throws IOException {
newManifest = buildModpackFetcher(apiClient, projectRef)
.fetchModpack(prevManifest)
.flatMap(fetchedPack ->
new ModrinthPackInstaller(
apiClient, this.sharedFetchArgs.options(),
fetchedPack.getMrPackFile(), this.outputDirectory, this.resultsFile,
this.forceModloaderReinstall
)
installerFactory.create(apiClient, fetchedPack.getMrPackFile())
.setExcludeFiles(excludeFiles)
.processModpack(sharedFetch)
.flatMap(installation -> {
Expand Down Expand Up @@ -141,6 +140,22 @@ public Integer call() throws IOException {
return ExitCode.OK;
}

@VisibleForTesting
@FunctionalInterface
interface ModrinthModpackInstallerFactory {

ModrinthPackInstaller create(ModrinthApiClient apiClient, Path mrPackFile);
}

@VisibleForTesting
@Setter(AccessLevel.PACKAGE)
private ModrinthModpackInstallerFactory installerFactory = (apiClient, mrPackFile) ->
new ModrinthPackInstaller(
apiClient, this.sharedFetchArgs.options(),
mrPackFile, this.outputDirectory, this.resultsFile,
this.forceModloaderReinstall
);

private Mono<Installation> processResultsFile(FetchedPack fetchedPack, Installation installation) {
return Mono.fromCallable(() -> {
try (ResultsFileWriter results = new ResultsFileWriter(resultsFile, true)) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/itzg/helpers/modrinth/ModpackLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public enum ModpackLoader {
fabric,
forge,
quilt;
quilt,
neoforge;

public Loader asLoader() {
return Loader.valueOf(this.name());
Expand Down
108 changes: 75 additions & 33 deletions src/main/java/me/itzg/helpers/modrinth/ModrinthPackInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -19,6 +21,7 @@
import me.itzg.helpers.files.IoStreams;
import me.itzg.helpers.forge.ForgeInstaller;
import me.itzg.helpers.forge.ForgeInstallerResolver;
import me.itzg.helpers.forge.NeoForgeInstallerResolver;
import me.itzg.helpers.http.SharedFetch;
import me.itzg.helpers.http.SharedFetch.Options;
import me.itzg.helpers.json.ObjectMappers;
Expand All @@ -28,6 +31,7 @@
import me.itzg.helpers.modrinth.model.ModpackIndex;
import me.itzg.helpers.quilt.QuiltInstaller;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.VisibleForTesting;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
Expand All @@ -44,6 +48,20 @@ public class ModrinthPackInstaller {
@Setter
private List<String> excludeFiles;

@FunctionalInterface
interface ModloaderPreparer {

void prepare(SharedFetch sharedFetch, String minecraftVersion, String version);
}

private final Map<DependencyId, ModloaderPreparer> modloaderPreparers = new HashMap<>();
{
modloaderPreparers.put(DependencyId.forge, this::prepareForge);
modloaderPreparers.put(DependencyId.neoforge, this::prepareNeoForge);
modloaderPreparers.put(DependencyId.fabricLoader, this::prepareFabric);
modloaderPreparers.put(DependencyId.quiltLoader, this::prepareQuilt);
}

public ModrinthPackInstaller(
ModrinthApiClient apiClient, Options sharedFetchOpts,
Path zipFile, Path outputDirectory, Path resultsFile,
Expand Down Expand Up @@ -209,44 +227,68 @@ private void applyModLoader(SharedFetch sharedFetch, Map<DependencyId, String> d
"Modpack dependencies missing minecraft version: " + dependencies);
}

final String forgeVersion = dependencies.get(DependencyId.forge);
if (forgeVersion != null) {
new ForgeInstaller(
new ForgeInstallerResolver(sharedFetch, minecraftVersion, forgeVersion)
)
.install(
this.outputDirectory,
this.resultsFile,
this.forceModloaderReinstall,
"Forge"
);
return;
for (final Entry<DependencyId, ModloaderPreparer> entry : modloaderPreparers.entrySet()) {
final String version = dependencies.get(entry.getKey());
if (version != null) {
entry.getValue().prepare(sharedFetch, minecraftVersion, version);
return;
}
}

final String fabricVersion = dependencies.get(DependencyId.fabricLoader);
if (fabricVersion != null) {
new FabricLauncherInstaller(this.outputDirectory)
.setResultsFile(this.resultsFile)
.installUsingVersions(
minecraftVersion,
fabricVersion,
null
);
return;
}
throw new GenericException("Unsupported or missing modloader in dependencies: " + dependencies);
}

final String quiltVersion = dependencies.get(DependencyId.quiltLoader);
if (quiltVersion != null) {
try (QuiltInstaller installer =
new QuiltInstaller(QuiltInstaller.DEFAULT_REPO_URL,
this.sharedFetchOpts,
this.outputDirectory,
minecraftVersion)
.setResultsFile(this.resultsFile)) {
private void prepareQuilt(SharedFetch sharedFetch, String minecraftVersion, String quiltVersion) {
try (QuiltInstaller installer =
new QuiltInstaller(QuiltInstaller.DEFAULT_REPO_URL,
this.sharedFetchOpts,
this.outputDirectory,
minecraftVersion
)
.setResultsFile(this.resultsFile)) {

installer.installWithVersion(null, quiltVersion);
}
installer.installWithVersion(null, quiltVersion);
}
}

@VisibleForTesting
ModrinthPackInstaller modifyModLoaderPreparer(DependencyId modLoaderId, ModloaderPreparer preparer) {
modloaderPreparers.put(modLoaderId, preparer);
return this;
}

private void prepareFabric(SharedFetch sharedFetch, String minecraftVersion, String fabricVersion) {
new FabricLauncherInstaller(this.outputDirectory)
.setResultsFile(this.resultsFile)
.installUsingVersions(
minecraftVersion,
fabricVersion,
null
);
}

private void prepareForge(SharedFetch sharedFetch, String minecraftVersion, String version) {
new ForgeInstaller(
new ForgeInstallerResolver(sharedFetch, minecraftVersion, version)
)
.install(
this.outputDirectory,
this.resultsFile,
this.forceModloaderReinstall,
"Forge"
);
}

private void prepareNeoForge(SharedFetch sharedFetch, String minecraftVersion, String version) {
new ForgeInstaller(
new NeoForgeInstallerResolver(sharedFetch, minecraftVersion, version)
)
.install(
this.outputDirectory,
this.resultsFile,
this.forceModloaderReinstall,
"NeoForge"
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum DependencyId {
@JsonProperty("fabric-loader")
fabricLoader,
@JsonProperty("quilt-loader")
quiltLoader
quiltLoader,
neoforge
}
Loading

0 comments on commit 0b1b210

Please sign in to comment.