Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/importer/CurseForgeImporter.java

52 lines
2.7 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.system.importer;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.launcher.model.curseforge.CurseforgeModpackManifest;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModMeta;
import io.gitlab.jfronny.inceptum.launcher.system.instance.ModPath;
import io.gitlab.jfronny.inceptum.launcher.system.source.*;
import io.gitlab.jfronny.inceptum.launcher.util.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
public class CurseForgeImporter extends Importer<CurseforgeModpackManifest> {
public CurseForgeImporter() {
super("CurseForge", "manifest.json", CurseforgeModpackManifest.class);
}
@Override
protected IntermediaryManifest validateManifest(CurseforgeModpackManifest manifest, Path sourceRoot) throws IOException {
if (manifest.manifestVersion != 1) throw new IOException("Unsupported CurseForge modpack manifest version");
if (!"minecraftModpack".equals(manifest.manifestType)) throw new IOException("Invalid input: not a minecraft modpack");
if (manifest.minecraft == null) throw new IOException("Modpack missing minecraft metadata");
if (manifest.minecraft.modLoaders == null) manifest.minecraft.modLoaders = Set.of();
if (manifest.minecraft.version == null) throw new IOException("Modpack missing minecraft version");
if (manifest.files == null) manifest.files = Set.of();
String fabric = null;
for (CurseforgeModpackManifest.Minecraft.ModLoader loader : manifest.minecraft.modLoaders) {
final String idPrefix = "fabric-";
if (!loader.id.startsWith(idPrefix)) throw new IOException("Unsupported mod loader");
fabric = loader.id.substring(idPrefix.length());
}
return new IntermediaryManifest(manifest.name, manifest.overrides, manifest.minecraft.version, fabric);
}
@Override
protected void downloadMods(CurseforgeModpackManifest manifest, Path instanceDir, ProcessState state) throws IOException {
Path modsPath = instanceDir.resolve("mods");
for (CurseforgeModpackManifest.File file : manifest.files) {
if (!file.required) continue;
CurseforgeModSource source = new CurseforgeModSource(file.projectID, file.fileID);
state.updateStep("Downloading " + source.getName());
ModDownload download = source.download();
ModMeta imod = ModMeta.of(download.sha1(), download.murmur2(), source, manifest.minecraft.version);
Files.createDirectories(modsPath);
JFiles.writeObject(modsPath.resolve(source.getShortName() + ModPath.EXT_IMOD), imod);
}
}
}