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;
|
2021-10-30 14:22:07 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.LoaderInfo;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.fabric.FabricVersionLoaderInfo;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.mojang.VersionInfo;
|
2021-10-29 22:50:42 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.model.mojang.VersionsList;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.mojang.VersionsListInfo;
|
2021-10-30 14:22:07 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.util.api.FabricMetaApi;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.api.McApi;
|
2021-10-29 23:16:03 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.util.Utils;
|
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;
|
2021-10-30 14:22:07 +02:00
|
|
|
import java.util.HashMap;
|
2021-10-28 20:19:09 +02:00
|
|
|
import java.util.List;
|
2021-10-30 14:22:07 +02:00
|
|
|
import java.util.Map;
|
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;
|
2021-10-30 14:22:07 +02:00
|
|
|
Map<VersionsListInfo, List<FabricVersionLoaderInfo>> loaderInfoCache = new HashMap<>();
|
|
|
|
FabricVersionLoaderInfo selectedFabric;
|
2021-10-29 23:16:03 +02:00
|
|
|
ImInt version = new ImInt(0);
|
2021-10-28 20:19:09 +02:00
|
|
|
ImString name = new ImString("", 128);
|
2021-10-30 14:22:07 +02:00
|
|
|
ImInt fabricVersion = new ImInt(0);
|
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-30 14:22:07 +02:00
|
|
|
List<FabricVersionLoaderInfo> versions = getFabricLoaderInfo();
|
|
|
|
for (int i = 0, fabricLoaderInfoSize = versions.size(); i < fabricLoaderInfoSize; i++) {
|
|
|
|
FabricVersionLoaderInfo version = versions.get(i);
|
|
|
|
if (version.loader.stable) {
|
|
|
|
selectedFabric = version;
|
|
|
|
fabricVersion.set(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 22:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw() {
|
2021-10-29 23:16:03 +02:00
|
|
|
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()));
|
|
|
|
}
|
2021-10-30 14:22:07 +02:00
|
|
|
if (fabric.get()) {
|
|
|
|
ImGui.sameLine();
|
|
|
|
List<FabricVersionLoaderInfo> versions = getFabricLoaderInfo();
|
|
|
|
if (ImGui.combo("Loader", fabricVersion, versions.stream().map(info -> info.loader.version).toArray(String[]::new))) {
|
|
|
|
selectedFabric = versions.get(fabricVersion.get());
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 23:16:03 +02:00
|
|
|
//TODO forge
|
|
|
|
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")) {
|
|
|
|
close();
|
|
|
|
try {
|
2021-10-30 14:22:07 +02:00
|
|
|
VersionInfo vi = McApi.getVersionInfo(selected);
|
|
|
|
if (fabric.get()) FabricMetaApi.addFabric(vi, selectedFabric.loader.version);
|
|
|
|
Inceptum.open(new InstanceCreateProcessWindow(new SetupStepInfo(vi,
|
|
|
|
fabric.get() ? new LoaderInfo(selectedFabric.loader) : LoaderInfo.NONE,
|
|
|
|
name.get())));
|
2021-10-29 23:16:03 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
Inceptum.LOGGER.error("Could not initialize instance creation", e);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-30 14:22:07 +02:00
|
|
|
private List<FabricVersionLoaderInfo> getFabricLoaderInfo() {
|
|
|
|
if (!loaderInfoCache.containsKey(selected))
|
|
|
|
loaderInfoCache.put(selected, FabricMetaApi.getLoaderVersions(selected));
|
|
|
|
return loaderInfoCache.get(selected);
|
2021-10-28 20:19:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 14:22:07 +02:00
|
|
|
private String getDefaultName(VersionsListInfo info, boolean fabric) {
|
|
|
|
return fabric ? "Fabric " + info.id : info.id;
|
2021-10-27 22:00:08 +02:00
|
|
|
}
|
|
|
|
}
|