2022-10-27 20:54:55 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.launcher.system.instance;
|
2022-09-26 19:09:02 +02:00
|
|
|
|
|
|
|
import io.gitlab.jfronny.commons.io.JFiles;
|
2022-11-02 00:38:04 +01:00
|
|
|
import io.gitlab.jfronny.commons.throwable.*;
|
2022-09-26 19:09:02 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.common.MetaHolder;
|
2022-10-19 21:26:06 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.common.Utils;
|
2022-11-02 00:38:04 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.GC_InstanceMeta;
|
2022-09-26 19:09:02 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
|
2022-11-02 00:38:04 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.launcher.util.FileBackedRef;
|
2022-09-26 19:09:02 +02:00
|
|
|
|
2022-10-19 21:26:06 +02:00
|
|
|
import java.io.Closeable;
|
2022-09-26 19:09:02 +02:00
|
|
|
import java.io.IOException;
|
2022-11-02 00:38:04 +01:00
|
|
|
import java.nio.file.*;
|
2022-09-26 19:09:02 +02:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class InstanceList {
|
|
|
|
private static final Map<Path, IEntry> metas = new LinkedHashMap<>();
|
|
|
|
|
2022-10-19 21:26:06 +02:00
|
|
|
public static void reset() {
|
|
|
|
synchronized (metas) {
|
|
|
|
for (IEntry value : metas.values()) {
|
|
|
|
try {
|
|
|
|
value.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Utils.LOGGER.error("Could not close reference to instance meta", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metas.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <TEx extends Exception> void forEach(ThrowingConsumer<Instance, TEx> target) throws IOException, TEx {
|
2022-09-26 19:09:02 +02:00
|
|
|
Objects.requireNonNull(target);
|
|
|
|
if (!Files.exists(MetaHolder.INSTANCE_DIR)) Files.createDirectories(MetaHolder.INSTANCE_DIR);
|
2022-10-19 21:26:06 +02:00
|
|
|
try {
|
|
|
|
JFiles.listTo(MetaHolder.INSTANCE_DIR, path -> {
|
|
|
|
if (!Files.isDirectory(path)) return;
|
2022-11-04 14:40:32 +01:00
|
|
|
if (!Files.exists(path.resolve("instance.json"))) return;
|
2022-10-19 21:26:06 +02:00
|
|
|
target.accept(read(path));
|
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
//noinspection unchecked
|
|
|
|
throw (TEx) e;
|
|
|
|
}
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 18:07:18 +02:00
|
|
|
public static boolean isEmpty() throws IOException {
|
|
|
|
if (!Files.exists(MetaHolder.INSTANCE_DIR)) return true;
|
|
|
|
return JFiles.list(MetaHolder.INSTANCE_DIR, Files::isDirectory).isEmpty();
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 21:26:06 +02:00
|
|
|
public static Instance read(Path instancePath) throws IOException {
|
2022-09-26 19:09:02 +02:00
|
|
|
Objects.requireNonNull(instancePath);
|
|
|
|
synchronized (metas) {
|
|
|
|
if (!metas.containsKey(instancePath)) {
|
|
|
|
metas.put(instancePath, new IEntry(
|
|
|
|
instancePath,
|
2022-11-02 00:38:04 +01:00
|
|
|
new FileBackedRef<>(instancePath.resolve("instance.json"), GC_InstanceMeta::read)
|
2022-09-26 19:09:02 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
return metas.get(instancePath).toPub();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:26:06 +02:00
|
|
|
private record IEntry(Path path, FileBackedRef<InstanceMeta> meta) implements Closeable {
|
2022-09-26 19:09:02 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return path.getFileName().toString();
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:26:06 +02:00
|
|
|
public Instance toPub() throws IOException {
|
|
|
|
return new Instance(path, meta.get());
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-19 21:26:06 +02:00
|
|
|
public void close() throws IOException {
|
|
|
|
meta.close();
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|