Inceptum/src/main/java/io/gitlab/jfronny/inceptum/windows/NewInstanceWindow.java

117 lines
4.8 KiB
Java

package io.gitlab.jfronny.inceptum.windows;
import imgui.ImGui;
import imgui.type.ImBoolean;
import imgui.type.ImInt;
import imgui.type.ImString;
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.model.mojang.VersionsList;
import io.gitlab.jfronny.inceptum.model.mojang.VersionsListInfo;
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.util.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(Inceptum.CONFIG.snapshots);
ImBoolean fabric = new ImBoolean(true);
public NewInstanceWindow() {
super("New Instance");
selected = getVersions(false).get(0);
if (Inceptum.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 = Inceptum.CONFIG.snapshots;
Inceptum.CONFIG.snapshots = snapshots.get();
Inceptum.saveConfig();
//fix version index
int i = getVersions(Inceptum.CONFIG.snapshots).indexOf(getVersions(prev).get(version.get()));
if (i == -1) version.set(0);
else version.set(i);
}
List<VersionsListInfo> vil = getVersions(Inceptum.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 "<ver> (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(Inceptum.INSTANCE_DIR.resolve(name.get())))
ImGui.text("Already exists");
else if (ImGui.button("OK"))
state = State.Installing;
}
case Installing -> {
if (Files.exists(Inceptum.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);
}
Inceptum.open(new AlertWindow("Successfully installed!"));
close();
} catch (Throwable e) {
Inceptum.LOGGER.error("Could not initialize instance", e);
Inceptum.open(new AlertWindow("Could not initialize instance, look at the log for details"));
close();
try {
Utils.deleteRecursive(Inceptum.INSTANCE_DIR.resolve(name.get()));
} catch (IOException ex) {
Inceptum.LOGGER.error("Could not delete instance dir", e);
}
}
}
}
}
private List<VersionsListInfo> getVersions(boolean snapshots) {
ArrayList<VersionsListInfo> 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
}
}