package io.gitlab.jfronny.inceptum.launcher.system.mds; import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.fabric.FabricModJson.GC_FabricModJson; import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModMeta.GC_ModMeta; import io.gitlab.jfronny.gson.JsonParseException; import io.gitlab.jfronny.inceptum.common.Utils; import io.gitlab.jfronny.inceptum.launcher.model.fabric.FabricModJson; import io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModMeta; import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance; import io.gitlab.jfronny.inceptum.launcher.system.instance.Mod; import io.gitlab.jfronny.inceptum.launcher.system.instance.ModPath; import io.gitlab.jfronny.inceptum.launcher.system.mds.noop.NoopMod; import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource; import org.jetbrains.annotations.Nullable; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.*; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Function; public record FileScanTask(ProtoInstance instance, Path file, BiConsumer discovered, String gameVersion) implements Runnable { @Override public void run() { if (!Files.exists(file)) return; if (Files.isDirectory(file)) return; // Directories are not supported try { if (ModPath.isJar(file)) discover(file, ModPath.appendImod(file)); else if (ModPath.isImod(file)) discover(ModPath.trimImod(file), file); else discovered.accept(file, new NoopMod(file)); } catch (IOException | URISyntaxException | JsonParseException e) { Utils.LOGGER.error("Could not scan file for mod info", e); } } private void discover(Path jarPath, Path imodPath) throws IOException, URISyntaxException { boolean managed = false; ModMeta meta; if (Files.exists(imodPath)) meta = GC_ModMeta.read(imodPath); else { meta = ModMeta.of(jarPath); GC_ModMeta.write(meta, imodPath); } boolean modified = false; if (meta.initialize(gameVersion)) { GC_ModMeta.write(meta, imodPath); modified = true; } ModSource selectedSource = null; for (ModSource source : meta.sources.keySet()) { source.getUpdate(gameVersion); if (!Files.exists(source.jarPath)) source.download(); selectedSource = source; } if (selectedSource != null) { if (Files.exists(jarPath)) { Files.delete(jarPath); Path newImod = imodPath.parent.resolve(selectedSource.shortName + ModPath.EXT_IMOD); Files.move(imodPath, newImod); imodPath = newImod; modified = true; } jarPath = selectedSource.jarPath; managed = true; } else if (!Files.exists(jarPath)) throw new IOException("Mod has no jar and no sources"); if (modified) meta = GC_ModMeta.read(imodPath); FabricModJson fmj; try (FileSystem fs = Utils.openZipFile(jarPath, false)) { Path fmjPath = fs.getPath("fabric.mod.json"); if (Files.exists(fmjPath)) fmj = GC_FabricModJson.read(fmjPath); else fmj = null; } discovered.accept(imodPath, new MdsMod(instance, imodPath, jarPath, managed, meta, fmj)); } }