Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/util/ModManager.java

58 lines
2.8 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.util;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModDescription;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.rt.Instance;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.rt.Mod;
import io.gitlab.jfronny.inceptum.launcher.system.mds.ModsDirScanner;
import io.gitlab.jfronny.inceptum.launcher.system.source.ModDownload;
import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ModManager {
public static void delete(Instance instance, Mod md) throws IOException {
JFiles.deleteRecursive(md.path());
if (md.imod().isPresent() && Files.exists(md.imod().get())) Files.delete(md.imod().get());
if (md.mod().isPresent()) {
for (String dependency : md.mod().get().dependencies) {
Path dep = instance.modsDir().resolve(dependency);
Mod dmd = instance.mds().get(dep);
if (dmd.mod().isPresent()) {
dmd.mod().get().dependencies.remove(md.path().getFileName().toString());
if (dmd.mod().get().dependencies.isEmpty()) delete(instance, dmd);
else if (dmd.imod().isPresent()) JFiles.writeObject(dmd.imod().get(), dmd.mod().get());
}
}
}
}
public static DownloadMeta download(ModSource ms, Path metaFile, ModsDirScanner mds) throws IOException {
for (Mod value : mds.getMods()) {
if (value.mod().isEmpty()) continue;
for (ModSource source : value.mod().get().sources.keySet()) {
if (ms.equals(source)) {
return new DownloadMeta(new ModDownload(value.mod().get().sha1, value.mod().get().murmur2, value.path()), value.mod().get(), source, metaFile);
}
}
}
ModDownload md = ms.download();
ModDescription manifest = ModDescription.of(md.sha1(), md.murmur2(), ms, mds.getGameVersion());
for (ModSource dependency : ms.getDependencies(mds.getGameVersion())) {
DownloadMeta depMan = download(dependency, metaFile.getParent().resolve(dependency.getShortName() + ModPath.EXT_IMOD), mds);
depMan.description.dependents.add(md.file().getFileName().toString());
manifest.dependencies.add(depMan.download.file().getFileName().toString());
depMan.write();
}
return new DownloadMeta(md, manifest, ms, metaFile);
}
public record DownloadMeta(ModDownload download, ModDescription description, ModSource source, Path metaFile) {
public void write() throws IOException {
JFiles.writeObject(metaFile, description);
}
}
}