178 lines
8.8 KiB
Java
178 lines
8.8 KiB
Java
package io.gitlab.jfronny.inceptum.windows;
|
|
|
|
import imgui.ImGui;
|
|
import imgui.flag.ImGuiTableFlags;
|
|
import imgui.flag.ImGuiWindowFlags;
|
|
import imgui.type.ImBoolean;
|
|
import imgui.type.ImInt;
|
|
import io.gitlab.jfronny.inceptum.Inceptum;
|
|
import io.gitlab.jfronny.inceptum.model.InstanceMeta;
|
|
import io.gitlab.jfronny.inceptum.model.mojang.MinecraftArgument;
|
|
import io.gitlab.jfronny.inceptum.model.mojang.VersionInfo;
|
|
import io.gitlab.jfronny.inceptum.model.mojang.VersionsListInfo;
|
|
import io.gitlab.jfronny.inceptum.util.MapAppender;
|
|
import io.gitlab.jfronny.inceptum.util.Utils;
|
|
import io.gitlab.jfronny.inceptum.util.VersionInfoLibraryResolver;
|
|
import io.gitlab.jfronny.inceptum.util.McApi;
|
|
import io.gitlab.jfronny.inceptum.util.account.AccountManager;
|
|
import io.gitlab.jfronny.inceptum.util.account.AuthInfo;
|
|
import io.gitlab.jfronny.inceptum.util.account.MicrosoftAccount;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class MainWindow extends Window {
|
|
private final ImBoolean darkTheme = new ImBoolean(Inceptum.CONFIG.darkTheme);
|
|
private final ImBoolean debugTools = new ImBoolean(false);
|
|
private final List<InstanceMeta> instances; //TODO custom ordering
|
|
private final ImInt accountIndex = new ImInt(0);
|
|
public MainWindow() {
|
|
super("Inceptum");
|
|
List<InstanceMeta> instances = new ArrayList<>();
|
|
try {
|
|
for (Path path : Files.list(Inceptum.INSTANCE_DIR).filter(Files::isDirectory).toList()) {
|
|
if (Files.exists(path.resolve("instance.json"))) {
|
|
InstanceMeta im = Utils.loadObject(path.resolve("instance.json"), InstanceMeta.class);
|
|
im.instancePath = path;
|
|
instances.add(im);
|
|
} else {
|
|
Inceptum.LOGGER.error("Invalid instance (doesn't contain instance.json): " + path);
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
Inceptum.LOGGER.error("Could not list present instances", e);
|
|
}
|
|
this.instances = List.copyOf(instances);
|
|
}
|
|
|
|
@Override
|
|
public int getFlags() {
|
|
return ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.AlwaysAutoResize;
|
|
}
|
|
|
|
@Override
|
|
public void draw() {
|
|
ImGui.beginMenuBar();
|
|
if (ImGui.beginMenu("File")) {
|
|
if (ImGui.menuItem("New Instance")) Inceptum.open(new NewInstanceWindow());
|
|
if (ImGui.menuItem("Exit Inceptum")) Inceptum.exit();
|
|
ImGui.endMenu();
|
|
}
|
|
if (ImGui.beginMenu("Account")) {
|
|
if (ImGui.menuItem("New")) Inceptum.open(new MicrosoftLoginWindow());
|
|
AuthInfo selected = AccountManager.getSelectedAccount();
|
|
List<MicrosoftAccount> accounts = AccountManager.getAccounts();
|
|
for (int i = 0, accountsSize = accounts.size(); i < accountsSize; i++) {
|
|
MicrosoftAccount account = accounts.get(i);
|
|
if (selected.equals(account)) accountIndex.set(i);
|
|
if (ImGui.radioButton(account.minecraftUsername, accountIndex, i)) {
|
|
AccountManager.switchAccount(account);
|
|
}
|
|
ImGui.sameLine();
|
|
if (ImGui.button("X")) AccountManager.removeAccount(account);
|
|
}
|
|
ImGui.endMenu();
|
|
}
|
|
if (ImGui.beginMenu("Settings")) {
|
|
if (ImGui.checkbox("Dark Theme", darkTheme)) {
|
|
Inceptum.CONFIG.darkTheme = darkTheme.get();
|
|
Inceptum.saveConfig();
|
|
Inceptum.applyTheme();
|
|
}
|
|
ImGui.endMenu();
|
|
}
|
|
if (ImGui.beginMenu("Help")) {
|
|
if (ImGui.menuItem("About")) Inceptum.open(new AboutWindow());
|
|
if (ImGui.menuItem("Log")) Inceptum.open(new LogWindow(MapAppender.LOG));
|
|
ImGui.checkbox("Debug Tools", debugTools);
|
|
ImGui.endMenu();
|
|
}
|
|
ImGui.endMenuBar();
|
|
|
|
if (debugTools.get()) {
|
|
ImGui.showDemoWindow();
|
|
}
|
|
|
|
if (instances.isEmpty()) {
|
|
ImGui.text("You have not yet created an instance");
|
|
ImGui.text("Use File->New Instance to do so");
|
|
}
|
|
else {
|
|
if (ImGui.beginTable("Instances", 2, ImGuiTableFlags.Resizable | ImGuiTableFlags.Borders)) {
|
|
for (InstanceMeta instance : instances) {
|
|
ImGui.tableNextColumn();
|
|
if (ImGui.button(instance.instancePath.getFileName().toString())) {
|
|
//TODO implement fabric support
|
|
//TODO extract launch logic to separate class
|
|
for (VersionsListInfo version : McApi.getVersions().versions) {
|
|
if (version.id.equals(instance.version)) {
|
|
try {
|
|
List<String> args = new ArrayList<>();
|
|
args.add("/lib/jvm/java-17-openjdk/bin/java"); //TODO allow selecting vm, detect with System.getProperty("java.home")
|
|
VersionInfo info = McApi.getVersionInfo(version);
|
|
StringBuilder classPath = new StringBuilder();
|
|
for (VersionInfo.Library.Downloads.Artifact artifact : VersionInfoLibraryResolver.getRelevant(info)) {
|
|
classPath.append(Inceptum.LIBRARIES_DIR.resolve(artifact.path).toAbsolutePath());
|
|
classPath.append(':');
|
|
}
|
|
classPath.append(Inceptum.LIBRARIES_DIR.resolve("net/minecraft/minecraft").resolve(version.id + ".jar").toAbsolutePath());
|
|
Inceptum.LOGGER.info(classPath.toString());
|
|
//TODO -Xms{lowMem} -Xmx{maxMem}
|
|
args.addAll(parse(info.arguments.jvm, info, instance, classPath.toString()));
|
|
args.add(info.mainClass);
|
|
args.addAll(parse(info.arguments.game, info, instance, classPath.toString()));
|
|
ProcessBuilder pb = new ProcessBuilder(args.toArray(new String[0]));
|
|
pb.directory(instance.instancePath.toFile());
|
|
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
|
|
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
|
pb.start();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ImGui.tableNextColumn();
|
|
if (ImGui.button("Edit")) {
|
|
Inceptum.LOGGER.error("Editing not yet implemented"); //TODO allow changing name (moving), changing version, managing mods etc
|
|
}
|
|
}
|
|
ImGui.endTable();
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<String> parse(List<MinecraftArgument> arguments, VersionInfo info, InstanceMeta instance, String classPath) {
|
|
List<String> res = new ArrayList<>();
|
|
for (MinecraftArgument argument : arguments) {
|
|
for (String s : argument.arg()) {
|
|
//TODO support old versions
|
|
AuthInfo authInfo = AccountManager.getSelectedAccount();
|
|
res.add(s
|
|
// game args
|
|
.replace("${auth_player_name}", authInfo.name())
|
|
.replace("${version_name}", info.id) //TODO fabric support
|
|
.replace("${game_directory}", instance.instancePath.toAbsolutePath().toString())
|
|
.replace("${assets_root}", Inceptum.ASSETS_DIR.toAbsolutePath().toString())
|
|
.replace("${assets_index_name}", info.assets)
|
|
.replace("${auth_uuid}", authInfo.uuid())
|
|
.replace("${auth_access_token}", authInfo.accessToken())
|
|
.replace("${user_type}", authInfo.userType())
|
|
.replace("${version_type}", info.type)
|
|
.replace("${resolution_width}", "1920") //TODO has_custom_resolution
|
|
.replace("${resolution_height}", "1080") //TODO has_custom_resolution
|
|
// jvm args
|
|
.replace("${natives_directory}", Inceptum.INSTANCE_DIR.toAbsolutePath().toString())
|
|
.replace("${launcher_name}", "Inceptum")
|
|
.replace("${launcher_version}", Inceptum.VERSION.version) //TODO automatically fill in
|
|
.replace("${classpath}", classPath)
|
|
);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
}
|