137 lines
6.2 KiB
Java
137 lines
6.2 KiB
Java
package io.gitlab.jfronny.inceptum.gtk;
|
|
|
|
import io.gitlab.jfronny.commons.io.JFiles;
|
|
import io.gitlab.jfronny.inceptum.common.MetaHolder;
|
|
import io.gitlab.jfronny.inceptum.gtk.window.dialog.MicrosoftLoginDialog;
|
|
import io.gitlab.jfronny.inceptum.gtk.window.dialog.ProcessStateWatcherDialog;
|
|
import io.gitlab.jfronny.inceptum.gtk.window.settings.launcher.LauncherSettingsWindow;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.launch.*;
|
|
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
|
|
import org.gtk.gtk.Application;
|
|
import io.gitlab.jfronny.commons.ref.R;
|
|
import io.gitlab.jfronny.inceptum.common.Utils;
|
|
import io.gitlab.jfronny.inceptum.gtk.menu.MenuBuilder;
|
|
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
|
|
import io.gitlab.jfronny.inceptum.gtk.window.AboutWindow;
|
|
import io.gitlab.jfronny.inceptum.gtk.window.NewInstanceWindow;
|
|
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
|
|
import io.gitlab.jfronny.inceptum.launcher.api.account.AccountManager;
|
|
import io.gitlab.jfronny.inceptum.launcher.api.account.MicrosoftAccount;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.instance.InstanceList;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.setup.Steps;
|
|
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
|
|
public class GtkMenubar {
|
|
public static MenuBuilder accountsMenu;
|
|
public static MenuBuilder launchMenu;
|
|
|
|
public static void create(Application app) {
|
|
var menu = new MenuBuilder(app);
|
|
var file = menu.submenu("file");
|
|
file.button("new", () -> new NewInstanceWindow(app).show());
|
|
file.button("redownload", () -> {
|
|
ProcessState state = new ProcessState(3 + Steps.STEPS.size() * InstanceList.size(), "Initializing");
|
|
ProcessStateWatcherDialog.show(
|
|
GtkEnvBackend.INSTANCE.dialogParent,
|
|
"Reloading data",
|
|
"Could not execute refresh task",
|
|
state,
|
|
cancel -> {
|
|
state.incrementStep("Clearing cache directories");
|
|
JFiles.clearDirectory(MetaHolder.ASSETS_DIR);
|
|
JFiles.clearDirectory(MetaHolder.LIBRARIES_DIR, path -> !path.startsWith(MetaHolder.LIBRARIES_DIR.resolve("io/gitlab/jfronny")));
|
|
JFiles.clearDirectory(MetaHolder.NATIVES_DIR, path -> !path.startsWith(MetaHolder.NATIVES_DIR.resolve("forceload")));
|
|
JFiles.clearDirectory(MetaHolder.CACHE_DIR);
|
|
if (cancel.get()) return;
|
|
state.incrementStep("Reloading instance list");
|
|
InstanceList.reset();
|
|
InstanceList.forEach(instance -> {
|
|
if (cancel.get()) return;
|
|
Steps.reDownload(instance, state, cancel);
|
|
});
|
|
}, R::nop);
|
|
});
|
|
file.button("exit", app::quit);
|
|
launchMenu = menu.submenu("launch");
|
|
generateLaunchMenu();
|
|
accountsMenu = menu.submenu("account");
|
|
generateAccountsMenu(app);
|
|
var help = menu.submenu("help");
|
|
help.button("about", AboutWindow::createAndShow);
|
|
help.button("log", () -> {
|
|
//TODO
|
|
});
|
|
}
|
|
|
|
public static void generateLaunchMenu() {
|
|
Objects.requireNonNull(launchMenu);
|
|
launchMenu.clear();
|
|
try {
|
|
InstanceList.forEach(entry -> {
|
|
launchMenu.literalButton(entry.id() + ".launch", entry.toString(), () -> launch(entry, LaunchType.Client));
|
|
});
|
|
} catch (IOException e) {
|
|
Utils.LOGGER.error("Could not generate launch menu", e);
|
|
}
|
|
}
|
|
|
|
public static void launch(Instance instance, LaunchType launchType) {
|
|
if (instance.isSetupLocked) {
|
|
LauncherEnv.showError(I18n.get("instance.launch.locked.setup"), I18n.get("instance.launch.locked"));
|
|
} else if (instance.isRunningLocked) {
|
|
LauncherEnv.showOkCancel(
|
|
I18n.get("instance.launch.locked.running"),
|
|
I18n.get("instance.launch.locked"),
|
|
() -> forceLaunch(instance, launchType),
|
|
R::nop
|
|
);
|
|
} else forceLaunch(instance, launchType);
|
|
}
|
|
|
|
private static void forceLaunch(Instance instance, LaunchType launchType) {
|
|
ProcessState state = Steps.createProcessState();
|
|
ProcessStateWatcherDialog.show(
|
|
GtkEnvBackend.INSTANCE.dialogParent,
|
|
I18n.get("instance.launch.title"),
|
|
I18n.get("instance.launch.error"),
|
|
state,
|
|
cancel -> {
|
|
try {
|
|
Steps.reDownload(instance, state, cancel);
|
|
} catch (IOException e) {
|
|
Utils.LOGGER.error("Could not fetch instance, trying to start anyways", e);
|
|
}
|
|
if (cancel.get()) return;
|
|
state.updateStep("Starting Game");
|
|
try {
|
|
if (launchType == LaunchType.Client) InstanceLauncher.launchClient(instance);
|
|
else InstanceLauncher.launch(instance, launchType, false, AccountManager.NULL_AUTH);
|
|
} catch (Throwable e) {
|
|
LauncherEnv.showError("Could not start instance", e);
|
|
}
|
|
},
|
|
R::nop
|
|
);
|
|
}
|
|
|
|
public static void generateAccountsMenu(Application app) {
|
|
Objects.requireNonNull(accountsMenu);
|
|
accountsMenu.clear();
|
|
accountsMenu.button("new", () -> new MicrosoftLoginDialog(GtkEnvBackend.INSTANCE.dialogParent).show());
|
|
accountsMenu.button("manage", () -> {
|
|
var window = new LauncherSettingsWindow(app);
|
|
window.activePage = "settings.accounts";
|
|
window.show();
|
|
});
|
|
List<MicrosoftAccount> accounts = new ArrayList<>(AccountManager.accounts);
|
|
accounts.add(null);
|
|
accountsMenu.literalRadio("account", accounts.get(AccountManager.selectedIndex), accounts, (i, acc) -> {
|
|
if (acc == null) return I18n.get("account.none");
|
|
return acc.minecraftUsername;
|
|
}, AccountManager::switchAccount);
|
|
}
|
|
}
|