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

137 lines
4.4 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.system.mds;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModMeta.GC_ModMeta;
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.*;
import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource;
import org.jetbrains.annotations.Nullable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Collectors;
public class MdsMod extends Mod {
private final ProtoInstance instance;
private final Path imodPath;
private final Path jarPath;
private final boolean managedJar;
private final ModMeta meta;
private final @Nullable FabricModJson fmj;
MdsMod(ProtoInstance instance, Path imodPath, Path jarPath, boolean managedJar, ModMeta meta, @Nullable FabricModJson fmj) {
this.instance = instance;
this.imodPath = imodPath;
this.jarPath = jarPath;
this.managedJar = managedJar;
this.meta = meta;
this.fmj = fmj;
}
@Override
public String getName() {
String fileName = imodPath.fileName.toString();
if (fmj == null) return fileName;
String name = fmj.name == null ? fmj.id == null ? fileName : fmj.id : fmj.name;
if (fmj.version != null) name += ' ' + fmj.version;
return name;
}
@Override
public String[] getDescription() {
if (fmj != null && fmj.description != null) return fmj.description.split("\n");
else return new String[]{"No description is available", "You may need to wait for its metadata to be scanned"};
}
@Override
public boolean getNeedsInject() {
return managedJar;
}
@Override
public Path getJarPath() {
return jarPath;
}
@Override
public Path getMetadataPath() {
return imodPath;
}
@Override
public void delete() throws IOException {
Files.delete(imodPath);
if (!managedJar) Files.delete(jarPath);
if (meta != null) {
for (Mod dependency : dependencies) dependency.removeDependent(this);
for (Mod dependent : dependents) dependent.removeDependency(this);
}
}
@Override
public Path update(ModSource update) throws IOException {
ModManager.DownloadMeta download = ModManager.download(update, imodPath, instance.mds);
Files.delete(imodPath);
download.write();
for (String dependency : meta.dependencies) {
if (!download.meta.dependencies.contains(dependency)) {
instance.mds[instance.modsDir.resolve(dependency)].removeDependent(this);
}
}
instance.mds.invalidate(this);
return download.metaFile();
}
@Override
public Set<Mod> getDependencies() {
return meta.dependencies.stream()
.map(instance.modsDir::resolve)
.map(instance.mds::get)
.collect(Collectors.toUnmodifiableSet());
}
@Override
public Set<Mod> getDependents() {
return meta.dependents.stream()
.map(instance.modsDir::resolve)
.map(instance.mds::get)
.collect(Collectors.toUnmodifiableSet());
}
@Override
public void removeDependency(Mod dependency) throws IOException {
meta.dependencies.remove(meta.dependencies.stream()
.filter(s -> dependency.equals(instance.mds[instance.modsDir.resolve(s)]))
.findFirst()
.orElseThrow(FileNotFoundException::new));
write();
}
@Override
public void removeDependent(Mod dependent) throws IOException {
meta.dependents.remove(meta.dependents.stream()
.filter(s -> dependent.equals(instance.mds[instance.modsDir.resolve(s)]))
.findFirst()
.orElseThrow(FileNotFoundException::new));
write();
if (!meta.explicit && meta.dependents.isEmpty) delete();
}
@Override
public ModMeta getMetadata() {
return meta;
}
@Override
public boolean isEnabled() {
return ModPath.isEnabled(imodPath);
}
private void write() throws IOException {
GC_ModMeta.write(meta, imodPath);
}
}