Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/mds/FileScanTask.java

80 lines
3.4 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.launcher.system.mds;
2022-01-23 17:10:19 +01:00
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;
2022-04-28 23:13:37 +02:00
import io.gitlab.jfronny.gson.JsonParseException;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.launcher.model.fabric.FabricModJson;
2022-10-27 20:54:55 +02:00
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModMeta;
import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance;
2022-10-27 20:54:55 +02:00
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;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource;
2022-10-27 20:54:55 +02:00
import org.jetbrains.annotations.Nullable;
2022-01-23 17:10:19 +01:00
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
2022-09-04 21:21:24 +02:00
import java.nio.file.*;
2022-01-23 17:10:19 +01:00
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
2022-01-23 17:10:19 +01:00
2022-10-27 20:54:55 +02:00
public record FileScanTask(ProtoInstance instance, Path file, BiConsumer<Path, Mod> discovered, String gameVersion) implements Runnable {
2022-01-23 17:10:19 +01:00
@Override
public void run() {
if (!Files.exists(file)) return;
2022-10-27 20:54:55 +02:00
if (Files.isDirectory(file)) return; // Directories are not supported
2022-01-23 17:10:19 +01:00
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));
2022-01-23 17:10:19 +01:00
} 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);
}
2022-01-23 17:10:19 +01:00
boolean modified = false;
if (meta.initialize(gameVersion)) {
GC_ModMeta.write(meta, imodPath);
2022-01-23 17:10:19 +01:00
modified = true;
}
ModSource selectedSource = null;
for (ModSource source : meta.sources.keySet()) {
source.getUpdate(gameVersion);
if (!Files.exists(source.jarPath)) source.download();
2022-01-23 17:10:19 +01:00
selectedSource = source;
}
2022-10-27 20:54:55 +02:00
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;
2022-10-27 20:54:55 +02:00
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);
2022-01-23 17:10:19 +01:00
FabricModJson fmj;
try (FileSystem fs = Utils.openZipFile(jarPath, false)) {
2022-10-27 20:54:55 +02:00
Path fmjPath = fs.getPath("fabric.mod.json");
if (Files.exists(fmjPath)) fmj = GC_FabricModJson.read(fmjPath);
else fmj = null;
2022-10-27 20:54:55 +02:00
}
discovered.accept(imodPath, new MdsMod(instance, imodPath, jarPath, managed, meta, fmj));
2022-01-23 17:10:19 +01:00
}
}