From 81ef894926450659edd0395da414529832525b21 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Tue, 8 Mar 2022 00:17:23 +0000 Subject: [PATCH] Refactor + Restyle account manager --- .../gui/screens/AccountManagerUIScreen.kt | 95 +++++++++++-------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/src/main/kotlin/codes/som/hibiscus/gui/screens/AccountManagerUIScreen.kt b/src/main/kotlin/codes/som/hibiscus/gui/screens/AccountManagerUIScreen.kt index 0544310..501b20c 100644 --- a/src/main/kotlin/codes/som/hibiscus/gui/screens/AccountManagerUIScreen.kt +++ b/src/main/kotlin/codes/som/hibiscus/gui/screens/AccountManagerUIScreen.kt @@ -5,6 +5,7 @@ import codes.som.hibiscus.data.MinecraftAccount import codes.som.hibiscus.mixins.MixinExtMinecraftClient import imgui.ImGui import imgui.flag.ImGuiInputTextFlags +import imgui.flag.ImGuiWindowFlags import imgui.type.ImBoolean import imgui.type.ImInt import imgui.type.ImString @@ -36,13 +37,25 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu") override fun render(matrices: MatrixStack?, mouseX: Int, mouseY: Int, delta: Float) { renderBackground(matrices) - if (ImGui.begin("Accounts")) { + drawAccountsPanel() + drawCreateAccountPanel() + } + + private fun drawAccountsPanel() { + if (ImGui.begin("Accounts", ImGuiWindowFlags.AlwaysAutoResize)) { ImGui.text("Available accounts: ${accounts.size}") ImGui.text("Logged in as: ${client!!.session.username}") if (ImGui.button("Add Account")) { createAccountPanelOpen.set(true) } + ImGui.sameLine() + if (ImGui.button("Random")) { + selectedAccountId = accounts + .filter { it.id.value != selectedAccountId } + .randomOrNull() + ?.id?.value ?: -1 + } val calculatedHeight = (accounts.size.coerceIn(4, 20) * 28f) if (ImGui.beginListBox("Accounts", 0f, calculatedHeight)) { @@ -56,44 +69,51 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu") } accounts.firstOrNull { it.id.value == selectedAccountId }?.let { account -> - ImGui.text("Selected account: ${account.name}") - ImGui.text("Last known username: ${account.lastKnownDisplayName}") - if (ImGui.button("Log In")) { - try { - val loginFile = AuthenticationFile.read(account.loginData.byteInputStream(Charsets.UTF_8)) - val loginResult = Authenticator.of(loginFile).shouldAuthenticate().run() - if (loginResult.user.isPresent) { - val user = loginResult.user.get() - (client!! as MixinExtMinecraftClient).setSession(user.createSession()) - - val loginData = ByteArrayOutputStream() - .apply { loginResult.resultFile.write(this) } - .toString(Charsets.UTF_8) - - Hibiscus.data.txn { - account.lastKnownDisplayName = user.name - account.loginData = loginData - refreshAccounts() - } - } - } catch (e: Exception) { - e.printStackTrace() - } + signInToAccount(account) } ImGui.sameLine() if (ImGui.button("Remove")) { - // TODO: Some confirmation modal - Hibiscus.data.txn { - account.delete() - refreshAccounts() - } + removeAccount(account) } + + ImGui.text("Selected account: ${account.name}") + ImGui.text("Last known username: ${account.lastKnownDisplayName}") + } ?: run { + ImGui.text("No account selected.") } } ImGui.end() + } - drawCreateAccountPanel() + private fun signInToAccount(account: MinecraftAccount) { + try { + val loginFile = AuthenticationFile.read(account.loginData.byteInputStream(Charsets.UTF_8)) + val loginResult = Authenticator.of(loginFile).shouldAuthenticate().run() + if (loginResult.user.isPresent) { + val user = loginResult.user.get() + (client!! as MixinExtMinecraftClient).setSession(user.createSession()) + + val loginData = ByteArrayOutputStream() + .apply { loginResult.resultFile.write(this) } + .toString(Charsets.UTF_8) + + Hibiscus.data.txn { + account.lastKnownDisplayName = user.name + account.loginData = loginData + refreshAccounts() + } + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + private fun removeAccount(account: MinecraftAccount) { + Hibiscus.data.txn { + account.delete() + refreshAccounts() + } } private val loginType = ImInt(1) @@ -103,9 +123,7 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu") private fun drawCreateAccountPanel() { if (createAccountPanelOpen.get()) { - if (ImGui.begin("Add Account", createAccountPanelOpen)) { - ImGui.pushItemWidth(ImGui.getContentRegionMaxX() - ImGui.getWindowContentRegionMinX()) - + if (ImGui.begin("Add Account", createAccountPanelOpen, ImGuiWindowFlags.AlwaysAutoResize)) { ImGui.text("Login Type:") ImGui.combo( "Login Type", @@ -114,10 +132,11 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu") ) ImGui.separator() + ImGui.pushItemWidth(ImGui.getContentRegionMaxX() - ImGui.getWindowContentRegionMinX()) when (loginType.get()) { 0 -> { - ImGui.text("Login Name (Username or Email):") + ImGui.text("Username / Email Address:") ImGui.inputText("##new-account-login-name", yggdrasilUsername) ImGui.text("Password:") ImGui.inputText("##new-account-login-password", yggdrasilPassword, ImGuiInputTextFlags.Password) @@ -149,14 +168,14 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu") if (loginResult.user.isPresent) { val user = loginResult.user.get() - val loginData = ByteArrayOutputStream() - .apply { loginResult.resultFile.write(this) } - .toString(Charsets.UTF_8) - yggdrasilUsername.set("") yggdrasilPassword.set("") microsoftToken.set("") + val loginData = ByteArrayOutputStream() + .apply { loginResult.resultFile.write(this) } + .toString(Charsets.UTF_8) + Hibiscus.data.txn { MinecraftAccount.new { this.name = user.name