package io.gitlab.jfronny.glaunch.windows; import imgui.ImGui; import imgui.type.ImBoolean; import imgui.type.ImInt; import imgui.type.ImString; import io.gitlab.jfronny.glaunch.GLaunch; import io.gitlab.jfronny.glaunch.install.SetupStepInfo; import io.gitlab.jfronny.glaunch.install.Step; import io.gitlab.jfronny.glaunch.install.Steps; import io.gitlab.jfronny.glaunch.model.VersionsList; import io.gitlab.jfronny.glaunch.model.VersionsListInfo; import io.gitlab.jfronny.glaunch.util.Utils; import io.gitlab.jfronny.glaunch.util.mojang.McApi; import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; public class NewInstanceWindow extends Window { VersionsList manifest = McApi.getVersions(); VersionsListInfo selected; State state = State.Configure; ImInt version = new ImInt(0); //TODO select latest stable ImString name = new ImString("", 128); ImBoolean snapshots = new ImBoolean(GLaunch.CONFIG.snapshots); ImBoolean fabric = new ImBoolean(true); public NewInstanceWindow() { super("New Instance"); selected = getVersions(false).get(0); if (GLaunch.CONFIG.snapshots) version.set(manifest.versions.indexOf(selected)); name.set(getDefaultName(selected, fabric.get())); } @Override public void draw() { switch (state) { case Configure -> { if (ImGui.checkbox("Show snapshots", snapshots)) { boolean prev = GLaunch.CONFIG.snapshots; GLaunch.CONFIG.snapshots = snapshots.get(); GLaunch.saveConfig(); //fix version index int i = getVersions(GLaunch.CONFIG.snapshots).indexOf(getVersions(prev).get(version.get())); if (i == -1) version.set(0); else version.set(i); } List vil = getVersions(GLaunch.CONFIG.snapshots); if (ImGui.combo("Version", version, vil.stream().map(info -> info.id).toArray(String[]::new))) { VersionsListInfo prev = selected; selected = vil.get(version.get()); if (getDefaultName(prev, fabric.get()).equals(name.get())) name.set(getDefaultName(selected, fabric.get())); //TODO " (1)" etc if exists } if (ImGui.checkbox("Fabric support", fabric)) { if (getDefaultName(selected, !fabric.get()).equals(name.get())) name.set(getDefaultName(selected, fabric.get())); } //TODO forge //TODO implement fabric support ImGui.inputTextWithHint("Name", "Select a name", name); if (!Utils.VALID_FILENAME.matcher(name.get()).matches()) ImGui.text("Invalid name"); else if (Files.exists(GLaunch.INSTANCE_DIR.resolve(name.get()))) ImGui.text("Already exists"); else if (ImGui.button("OK")) state = State.Installing; } case Installing -> { if (Files.exists(GLaunch.INSTANCE_DIR.resolve(name.get()))) { ImGui.text("Already exists"); } ImGui.text("Installing..."); //TODO update UI during process try { SetupStepInfo stepInfo = new SetupStepInfo(McApi.getVersionInfo(selected), fabric.get() ? LoaderType.Fabric : LoaderType.None, name.get()); for (Step step : Steps.STEPS) { step.execute(stepInfo); } GLaunch.open(new AlertWindow("Successfully installed!")); close(); } catch (Throwable e) { GLaunch.LOGGER.error("Could not initialize instance", e); GLaunch.open(new AlertWindow("Could not initialize instance, look at the log for details")); close(); try { Utils.deleteRecursive(GLaunch.INSTANCE_DIR.resolve(name.get())); } catch (IOException ex) { GLaunch.LOGGER.error("Could not delete instance dir", e); } } } } } private List getVersions(boolean snapshots) { ArrayList res = new ArrayList<>(manifest.versions); res.removeIf(info -> !snapshots && !info.type.equals("release")); return res; } private String getDefaultName(VersionsListInfo info, boolean fabric) { return fabric ? "Fabric " + info.id : info.id; } enum State { Configure, Installing } public enum LoaderType { None, Fabric } }