2022-09-04 21:21:24 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.imgui.control;
|
2021-10-30 16:12:39 +02:00
|
|
|
|
|
|
|
import imgui.ImGui;
|
|
|
|
import imgui.flag.ImGuiTableFlags;
|
2022-09-04 21:21:24 +02:00
|
|
|
import io.gitlab.jfronny.commons.cache.FileBackedRef;
|
|
|
|
import io.gitlab.jfronny.inceptum.common.Utils;
|
|
|
|
import io.gitlab.jfronny.inceptum.imgui.GuiMain;
|
|
|
|
import io.gitlab.jfronny.inceptum.imgui.window.edit.InstanceEditWindow;
|
|
|
|
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
|
|
|
|
import io.gitlab.jfronny.inceptum.launcher.system.launch.InstanceLauncher;
|
|
|
|
import io.gitlab.jfronny.inceptum.launcher.util.InstanceLock;
|
|
|
|
import io.gitlab.jfronny.inceptum.launcher.system.install.Steps;
|
2021-10-30 16:12:39 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2022-09-04 21:21:24 +02:00
|
|
|
import java.util.*;
|
2021-10-30 16:12:39 +02:00
|
|
|
|
|
|
|
public class InstanceView {
|
2022-09-04 21:21:24 +02:00
|
|
|
private static final Map<Path, FileBackedRef<InstanceMeta>> metas = new HashMap<>();
|
2022-01-24 19:21:23 +01:00
|
|
|
|
2021-10-30 16:12:39 +02:00
|
|
|
public static void draw(List<Path> paths) {
|
|
|
|
if (ImGui.beginTable("Instances", 2, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Borders)) {
|
|
|
|
for (Path path : paths) {
|
2022-01-04 13:00:15 +01:00
|
|
|
if (InstanceLock.isSetupLocked(path)) {
|
2021-10-30 16:12:39 +02:00
|
|
|
ImGui.tableNextColumn();
|
|
|
|
ImGui.text("Setting up");
|
|
|
|
ImGui.tableNextColumn();
|
|
|
|
ImGui.text("This instance is currently being set up");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!Files.exists(path.resolve("instance.json"))) {
|
2022-01-18 18:43:39 +01:00
|
|
|
Utils.LOGGER.error("Invalid instance (doesn't contain instance.json): " + path);
|
2021-10-30 16:12:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
InstanceMeta instance;
|
|
|
|
try {
|
2022-09-04 21:21:24 +02:00
|
|
|
if (!metas.containsKey(path))
|
|
|
|
metas.put(path, new FileBackedRef<>(path.resolve("instance.json"), InstanceMeta.class));
|
2022-01-24 19:21:23 +01:00
|
|
|
instance = metas.get(path).get();
|
2021-10-30 16:12:39 +02:00
|
|
|
} catch (IOException e) {
|
2022-01-18 18:43:39 +01:00
|
|
|
Utils.LOGGER.error("Could not load instance.json", e);
|
2021-10-30 16:12:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ImGui.tableNextColumn();
|
2022-01-04 13:00:15 +01:00
|
|
|
boolean runDisabled = false;
|
2022-01-04 12:51:51 +01:00
|
|
|
try {
|
2022-01-04 13:00:15 +01:00
|
|
|
if (InstanceLock.isRunningLocked(path))
|
|
|
|
runDisabled = true;
|
2022-01-04 12:51:51 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
continue;
|
2021-10-30 16:12:39 +02:00
|
|
|
}
|
2022-01-04 13:00:15 +01:00
|
|
|
if (runDisabled) ImGui.beginDisabled();
|
2021-11-24 19:53:06 +01:00
|
|
|
if (ImGui.button(path.getFileName().toString())) {
|
2021-11-25 17:55:50 +01:00
|
|
|
try {
|
2022-01-19 19:14:13 +01:00
|
|
|
Steps.reDownload(path, Steps.createProcessState());
|
2021-11-25 17:55:50 +01:00
|
|
|
} catch (IOException e) {
|
2022-01-27 17:57:13 +01:00
|
|
|
Utils.LOGGER.error("Could not redownload instance, trying to start anyways", e);
|
2021-11-25 17:55:50 +01:00
|
|
|
}
|
2022-01-19 19:14:13 +01:00
|
|
|
InstanceLauncher.launchClient(path, instance);
|
2021-11-24 19:53:06 +01:00
|
|
|
}
|
2022-01-04 13:00:15 +01:00
|
|
|
if (runDisabled) ImGui.endDisabled();
|
2021-10-30 16:12:39 +02:00
|
|
|
ImGui.tableNextColumn();
|
2022-01-24 19:21:23 +01:00
|
|
|
if (ImGui.button("Edit##" + path)) {
|
|
|
|
try {
|
2022-09-04 21:21:24 +02:00
|
|
|
GuiMain.open(new InstanceEditWindow(path, instance));
|
2022-01-24 19:21:23 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
Utils.LOGGER.error("Could not open instance edit window", e);
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 16:12:39 +02:00
|
|
|
}
|
|
|
|
ImGui.endTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|