2021-10-30 19:26:59 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.windows.control;
|
|
|
|
|
|
|
|
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.model.fabric.FabricVersionLoaderInfo;
|
2021-11-04 11:49:40 +01:00
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta;
|
2021-10-30 19:26:59 +02:00
|
|
|
import io.gitlab.jfronny.inceptum.model.inceptum.LoaderInfo;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.mojang.VersionInfo;
|
|
|
|
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.api.FabricMetaApi;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.api.McApi;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
public class InstanceManageControls {
|
|
|
|
public static final int MAX_NAME_LENGTH = 128;
|
|
|
|
private final VersionsList manifest = McApi.getVersions();
|
|
|
|
private final Map<VersionsListInfo, List<FabricVersionLoaderInfo>> loaderInfoCache = new HashMap<>();
|
|
|
|
private final ImInt version = new ImInt(0);
|
|
|
|
private final ImString name = new ImString("", MAX_NAME_LENGTH);
|
|
|
|
private final ImInt fabricVersion = new ImInt(0);
|
|
|
|
private final ImBoolean snapshots = new ImBoolean(Inceptum.CONFIG.snapshots);
|
|
|
|
private final ImBoolean fabric = new ImBoolean(true);
|
|
|
|
private VersionsListInfo selected;
|
|
|
|
private FabricVersionLoaderInfo selectedFabric;
|
|
|
|
|
2021-11-04 11:49:40 +01:00
|
|
|
public InstanceManageControls(InstanceMeta meta) {
|
2021-10-30 19:26:59 +02:00
|
|
|
selected = getVersions(false).get(0);
|
2021-11-11 20:42:22 +01:00
|
|
|
if (meta != null) {
|
|
|
|
for (VersionsListInfo ver : getVersions(true)) {
|
|
|
|
if (ver.id.equals(meta.getMinecraftVersion()))
|
|
|
|
selected = ver;
|
|
|
|
}
|
2021-11-04 11:49:40 +01:00
|
|
|
}
|
|
|
|
version.set(getVersions(snapshots.get()).indexOf(selected));
|
2021-10-30 19:26:59 +02:00
|
|
|
name.set(getDefaultName(selected, fabric.get()));
|
2021-11-04 11:49:40 +01:00
|
|
|
fabric.set(meta == null || meta.isFabric());
|
2021-10-30 19:26:59 +02:00
|
|
|
List<FabricVersionLoaderInfo> versions = getFabricLoaderInfo();
|
|
|
|
for (int i = 0, fabricLoaderInfoSize = versions.size(); i < fabricLoaderInfoSize; i++) {
|
|
|
|
FabricVersionLoaderInfo version = versions.get(i);
|
2021-11-04 11:49:40 +01:00
|
|
|
if (meta != null && meta.isFabric()
|
|
|
|
? version.loader.version.equals(meta.getLoaderVersion())
|
|
|
|
: version.loader.stable) {
|
2021-10-30 19:26:59 +02:00
|
|
|
selectedFabric = version;
|
|
|
|
fabricVersion.set(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void nameBox(String okText, Consumer<String> ok) {
|
|
|
|
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(okText)) {
|
|
|
|
ok.accept(name.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void snapshotsBox() {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void versionBox(Consumer<String> modifiedVersion) {
|
|
|
|
List<VersionsListInfo> vil = getVersions(Inceptum.CONFIG.snapshots);
|
|
|
|
String originalStr = null;
|
|
|
|
try {
|
|
|
|
originalStr = getVersionInfo().id;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Inceptum.LOGGER.error("Could not get version string", e);
|
|
|
|
}
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getFabricLoaderInfo().isEmpty()) {
|
|
|
|
if (fabric.get() && getDefaultName(selected, true).equals(name.get())) {
|
|
|
|
name.set(getDefaultName(selected, false));
|
|
|
|
}
|
|
|
|
fabric.set(false);
|
|
|
|
} else {
|
|
|
|
if (ImGui.checkbox("Fabric support", fabric)) {
|
|
|
|
if (getDefaultName(selected, !fabric.get()).equals(name.get())) {
|
|
|
|
name.set(getDefaultName(selected, fabric.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (originalStr != null && !originalStr.equals(getVersionInfo().id))
|
|
|
|
modifiedVersion.accept(getVersionInfo().id);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Inceptum.LOGGER.error("Could not compare version string", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public VersionInfo getVersionInfo() throws IOException {
|
2021-10-30 22:05:24 +02:00
|
|
|
VersionInfo vi = McApi.getVersionInfo(selected);
|
|
|
|
if (fabric.get()) FabricMetaApi.addFabric(vi, selectedFabric.loader.version, FabricMetaApi.FabricVersionInfoType.Both);
|
|
|
|
return vi;
|
2021-10-30 19:26:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public LoaderInfo getLoaderInfo() {
|
|
|
|
return fabric.get() ? new LoaderInfo(selectedFabric.loader) : LoaderInfo.NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<FabricVersionLoaderInfo> getFabricLoaderInfo() {
|
|
|
|
if (!loaderInfoCache.containsKey(selected))
|
|
|
|
loaderInfoCache.put(selected, FabricMetaApi.getLoaderVersions(selected));
|
|
|
|
return loaderInfoCache.get(selected);
|
|
|
|
}
|
|
|
|
}
|