package io.gitlab.jfronny.inceptum.windows; import imgui.ImGui; import io.gitlab.jfronny.inceptum.Inceptum; import io.gitlab.jfronny.inceptum.install.SetupStepInfo; import io.gitlab.jfronny.inceptum.install.Step; import io.gitlab.jfronny.inceptum.install.Steps; import io.gitlab.jfronny.inceptum.util.MapAppender; import io.gitlab.jfronny.inceptum.util.Utils; import java.io.IOException; import java.nio.file.Files; import java.util.concurrent.atomic.AtomicBoolean; public class InstanceCreateProcessWindow extends Window { private final SetupStepInfo stepInfo; private final AtomicBoolean finalize = new AtomicBoolean(false); public InstanceCreateProcessWindow(SetupStepInfo stepInfo) { super("Creating Instance"); this.stepInfo = stepInfo; new Thread(this::creationThread).start(); } @Override public void draw() { if (finalize.get()) { close(); return; } String last = "Initializing"; for (String s : MapAppender.LOG_MESSAGES) last = s; ImGui.textUnformatted(last); if (ImGui.button("Cancel")) finalize.set(true); } private void creationThread() { if (Files.exists(Inceptum.INSTANCE_DIR.resolve(stepInfo.name()))) { finalize.set(true); return; } Inceptum.LOGGER.info("Starting install process"); try { for (Step step : Steps.STEPS) { if (finalize.get()) { cleanUp(); return; } step.execute(stepInfo, finalize); } finalize.set(true); Inceptum.open(new AlertWindow("Successfully installed", "The instance was successfully created. You can now launch it using the main menu")); close(); } catch (Throwable e) { Inceptum.LOGGER.error("Could not initialize instance", e); Inceptum.open(new AlertWindow("Could not create the instance", e.toString())); close(); cleanUp(); } } private void cleanUp() { try { Utils.deleteRecursive(Inceptum.INSTANCE_DIR.resolve(stepInfo.name())); } catch (IOException e) { Inceptum.LOGGER.error("Could not delete instance dir", e); } } }