package io.gitlab.jfronny.inceptum.frontend.gui.control; import imgui.ImGui; import imgui.flag.ImGuiTableFlags; import io.gitlab.jfronny.inceptum.Inceptum; import io.gitlab.jfronny.inceptum.InceptumGui; import io.gitlab.jfronny.inceptum.frontend.gui.window.edit.InstanceEditWindow; import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta; import io.gitlab.jfronny.inceptum.util.InstanceLauncher; import io.gitlab.jfronny.inceptum.util.InstanceLock; import io.gitlab.jfronny.inceptum.util.Utils; import io.gitlab.jfronny.inceptum.util.cache.CachingGsonFile; import io.gitlab.jfronny.inceptum.util.install.Steps; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; public class InstanceView { private static final Map> metas = new HashMap<>(); public static void draw(List paths) { if (ImGui.beginTable("Instances", 2, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Borders)) { for (Path path : paths) { if (InstanceLock.isSetupLocked(path)) { 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); continue; } InstanceMeta instance; try { if (!metas.containsKey(path)) metas.put(path, new CachingGsonFile<>(path.resolve("instance.json"), InstanceMeta.class)); instance = metas.get(path).get(); } catch (IOException e) { Utils.LOGGER.error("Could not load instance.json", e); continue; } ImGui.tableNextColumn(); boolean runDisabled = false; try { if (InstanceLock.isRunningLocked(path)) runDisabled = true; } catch (IOException e) { continue; } if (runDisabled) ImGui.beginDisabled(); 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); } if (runDisabled) ImGui.endDisabled(); ImGui.tableNextColumn(); if (ImGui.button("Edit##" + path)) { try { InceptumGui.open(new InstanceEditWindow(path, instance)); } catch (IOException e) { Utils.LOGGER.error("Could not open instance edit window", e); } } } ImGui.endTable(); } } }