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

117 lines
4.8 KiB
Java
Raw Normal View History

2021-10-29 22:50:42 +02:00
package io.gitlab.jfronny.inceptum.windows;
2021-10-27 22:00:08 +02:00
import imgui.ImGui;
2021-10-28 20:19:09 +02:00
import imgui.type.ImBoolean;
import imgui.type.ImInt;
2021-10-27 22:00:08 +02:00
import imgui.type.ImString;
2021-10-29 22:50:42 +02:00
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;
2021-10-27 22:00:08 +02:00
import java.io.IOException;
2021-10-28 20:19:09 +02:00
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
2021-10-27 22:00:08 +02:00
public class NewInstanceWindow extends Window {
VersionsList manifest = McApi.getVersions();
2021-10-28 20:19:09 +02:00
VersionsListInfo selected;
State state = State.Configure;
ImInt version = new ImInt(0); //TODO select latest stable
ImString name = new ImString("", 128);
2021-10-29 22:50:42 +02:00
ImBoolean snapshots = new ImBoolean(Inceptum.CONFIG.snapshots);
2021-10-28 20:19:09 +02:00
ImBoolean fabric = new ImBoolean(true);
2021-10-27 22:00:08 +02:00
public NewInstanceWindow() {
super("New Instance");
2021-10-28 20:19:09 +02:00
selected = getVersions(false).get(0);
2021-10-29 22:50:42 +02:00
if (Inceptum.CONFIG.snapshots)
2021-10-28 20:19:09 +02:00
version.set(manifest.versions.indexOf(selected));
name.set(getDefaultName(selected, fabric.get()));
2021-10-27 22:00:08 +02:00
}
@Override
public void draw() {
switch (state) {
2021-10-28 20:19:09 +02:00
case Configure -> {
if (ImGui.checkbox("Show snapshots", snapshots)) {
2021-10-29 22:50:42 +02:00
boolean prev = Inceptum.CONFIG.snapshots;
Inceptum.CONFIG.snapshots = snapshots.get();
Inceptum.saveConfig();
2021-10-28 20:19:09 +02:00
//fix version index
2021-10-29 22:50:42 +02:00
int i = getVersions(Inceptum.CONFIG.snapshots).indexOf(getVersions(prev).get(version.get()));
2021-10-28 20:19:09 +02:00
if (i == -1) version.set(0);
else version.set(i);
2021-10-27 22:00:08 +02:00
}
2021-10-29 22:50:42 +02:00
List<VersionsListInfo> vil = getVersions(Inceptum.CONFIG.snapshots);
2021-10-28 20:19:09 +02:00
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
2021-10-27 22:00:08 +02:00
}
2021-10-28 20:19:09 +02:00
if (ImGui.checkbox("Fabric support", fabric)) {
if (getDefaultName(selected, !fabric.get()).equals(name.get()))
name.set(getDefaultName(selected, fabric.get()));
2021-10-27 22:00:08 +02:00
}
2021-10-28 20:19:09 +02:00
//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");
2021-10-29 22:50:42 +02:00
else if (Files.exists(Inceptum.INSTANCE_DIR.resolve(name.get())))
2021-10-28 20:19:09 +02:00
ImGui.text("Already exists");
else if (ImGui.button("OK"))
2021-10-27 22:00:08 +02:00
state = State.Installing;
}
case Installing -> {
2021-10-29 22:50:42 +02:00
if (Files.exists(Inceptum.INSTANCE_DIR.resolve(name.get()))) {
2021-10-28 20:19:09 +02:00
ImGui.text("Already exists");
}
ImGui.text("Installing..."); //TODO update UI during process
2021-10-27 22:00:08 +02:00
try {
2021-10-28 20:19:09 +02:00
SetupStepInfo stepInfo = new SetupStepInfo(McApi.getVersionInfo(selected), fabric.get() ? LoaderType.Fabric : LoaderType.None, name.get());
2021-10-27 22:00:08 +02:00
for (Step step : Steps.STEPS) {
step.execute(stepInfo);
}
2021-10-29 22:50:42 +02:00
Inceptum.open(new AlertWindow("Successfully installed!"));
2021-10-28 20:19:09 +02:00
close();
} catch (Throwable e) {
2021-10-29 22:50:42 +02:00
Inceptum.LOGGER.error("Could not initialize instance", e);
Inceptum.open(new AlertWindow("Could not initialize instance, look at the log for details"));
2021-10-27 22:00:08 +02:00
close();
2021-10-28 20:19:09 +02:00
try {
2021-10-29 22:50:42 +02:00
Utils.deleteRecursive(Inceptum.INSTANCE_DIR.resolve(name.get()));
2021-10-28 20:19:09 +02:00
} catch (IOException ex) {
2021-10-29 22:50:42 +02:00
Inceptum.LOGGER.error("Could not delete instance dir", e);
2021-10-28 20:19:09 +02:00
}
2021-10-27 22:00:08 +02:00
}
}
}
}
2021-10-28 20:19:09 +02:00
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;
}
2021-10-27 22:00:08 +02:00
enum State {
2021-10-28 20:19:09 +02:00
Configure,
2021-10-27 22:00:08 +02:00
Installing
}
public enum LoaderType {
None,
Fabric
}
}