Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/export/ModrinthExporter.java

68 lines
3.0 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.inceptum.launcher.system.export;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.rt.Instance;
import io.gitlab.jfronny.inceptum.launcher.model.modrinth.ModrinthModpackManifest;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.rt.Mod;
import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource;
import io.gitlab.jfronny.inceptum.launcher.system.source.ModrinthModSource;
import io.gitlab.jfronny.inceptum.launcher.util.ModPath;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Set;
public class ModrinthExporter extends Exporter<ModrinthModpackManifest> {
public ModrinthExporter() {
super("Modrinth", "mrpack", "overrides");
}
@Override
protected ModrinthModpackManifest generateManifests(Path root, Instance instance, String version) throws IOException {
ModrinthModpackManifest manifest = new ModrinthModpackManifest();
manifest.formatVersion = 1;
manifest.game = "minecraft";
manifest.versionId = version;
manifest.name = instance.getName();
manifest.files = new ArrayList<>();
manifest.dependencies = new ModrinthModpackManifest.Dependencies();
manifest.dependencies.minecraft = instance.meta().getMinecraftVersion();
if (instance.isFabric()) {
manifest.dependencies.fabricLoader = instance.meta().getLoaderVersion();
}
JFiles.writeObject(root.resolve("modrinth.index.json"), manifest);
return manifest;
}
@Override
protected void addMods(Path root, Instance instance, Iterable<Mod> mods, ModrinthModpackManifest manifest, Path modsOverrides) throws IOException {
modsLoop: for(Mod mod : mods) {
if (ModPath.isImod(mod.path())) {
Set<ModSource> sources = mod.mod().orElseThrow().sources.keySet();
for (ModSource source : sources) {
if (source instanceof ModrinthModSource cms) {
manifest.files.add(cms.toManifest());
continue modsLoop;
}
}
// Not available on modrinth
for (ModSource source : sources) {
Files.createDirectories(modsOverrides);
Files.copy(source.getJarPath(), modsOverrides.resolve(mod.path().getFileName().toString()));
continue modsLoop;
}
} else {
Files.createDirectories(modsOverrides);
Files.copy(mod.path(), modsOverrides.resolve(mod.path().getFileName().toString()));
continue modsLoop;
}
throw new FileNotFoundException("Could not find mod file for " + mod.path());
}
JFiles.writeObject(root.resolve("modrinth.index.json"), manifest);
}
}