Inceptum/launcher-imgui/src/main/java/io/gitlab/jfronny/inceptum/imgui/control/InstanceView.java

76 lines
3.2 KiB
Java
Raw Normal View History

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<>();
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) {
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"))) {
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));
instance = metas.get(path).get();
2021-10-30 16:12:39 +02:00
} catch (IOException e) {
Utils.LOGGER.error("Could not load instance.json", e);
2021-10-30 16:12:39 +02:00
continue;
}
ImGui.tableNextColumn();
boolean runDisabled = false;
2022-01-04 12:51:51 +01:00
try {
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
}
if (runDisabled) ImGui.beginDisabled();
2021-11-24 19:53:06 +01:00
if (ImGui.button(path.getFileName().toString())) {
try {
Steps.reDownload(path, Steps.createProcessState());
} catch (IOException e) {
Utils.LOGGER.error("Could not redownload instance, trying to start anyways", e);
}
InstanceLauncher.launchClient(path, instance);
2021-11-24 19:53:06 +01:00
}
if (runDisabled) ImGui.endDisabled();
2021-10-30 16:12:39 +02:00
ImGui.tableNextColumn();
if (ImGui.button("Edit##" + path)) {
try {
2022-09-04 21:21:24 +02:00
GuiMain.open(new InstanceEditWindow(path, instance));
} catch (IOException e) {
Utils.LOGGER.error("Could not open instance edit window", e);
}
}
2021-10-30 16:12:39 +02:00
}
ImGui.endTable();
}
}
}