Refactor + Restyle account manager

main
Charlotte Som 2022-03-08 00:17:23 +00:00
parent 57323b3ac6
commit 81ef894926
1 changed files with 57 additions and 38 deletions

View File

@ -5,6 +5,7 @@ import codes.som.hibiscus.data.MinecraftAccount
import codes.som.hibiscus.mixins.MixinExtMinecraftClient import codes.som.hibiscus.mixins.MixinExtMinecraftClient
import imgui.ImGui import imgui.ImGui
import imgui.flag.ImGuiInputTextFlags import imgui.flag.ImGuiInputTextFlags
import imgui.flag.ImGuiWindowFlags
import imgui.type.ImBoolean import imgui.type.ImBoolean
import imgui.type.ImInt import imgui.type.ImInt
import imgui.type.ImString 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) { override fun render(matrices: MatrixStack?, mouseX: Int, mouseY: Int, delta: Float) {
renderBackground(matrices) 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("Available accounts: ${accounts.size}")
ImGui.text("Logged in as: ${client!!.session.username}") ImGui.text("Logged in as: ${client!!.session.username}")
if (ImGui.button("Add Account")) { if (ImGui.button("Add Account")) {
createAccountPanelOpen.set(true) 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) val calculatedHeight = (accounts.size.coerceIn(4, 20) * 28f)
if (ImGui.beginListBox("Accounts", 0f, calculatedHeight)) { if (ImGui.beginListBox("Accounts", 0f, calculatedHeight)) {
@ -56,10 +69,24 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu")
} }
accounts.firstOrNull { it.id.value == selectedAccountId }?.let { account -> accounts.firstOrNull { it.id.value == selectedAccountId }?.let { account ->
if (ImGui.button("Log In")) {
signInToAccount(account)
}
ImGui.sameLine()
if (ImGui.button("Remove")) {
removeAccount(account)
}
ImGui.text("Selected account: ${account.name}") ImGui.text("Selected account: ${account.name}")
ImGui.text("Last known username: ${account.lastKnownDisplayName}") ImGui.text("Last known username: ${account.lastKnownDisplayName}")
} ?: run {
ImGui.text("No account selected.")
}
}
ImGui.end()
}
if (ImGui.button("Log In")) { private fun signInToAccount(account: MinecraftAccount) {
try { try {
val loginFile = AuthenticationFile.read(account.loginData.byteInputStream(Charsets.UTF_8)) val loginFile = AuthenticationFile.read(account.loginData.byteInputStream(Charsets.UTF_8))
val loginResult = Authenticator.of(loginFile).shouldAuthenticate().run() val loginResult = Authenticator.of(loginFile).shouldAuthenticate().run()
@ -81,20 +108,13 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu")
e.printStackTrace() e.printStackTrace()
} }
} }
ImGui.sameLine()
if (ImGui.button("Remove")) { private fun removeAccount(account: MinecraftAccount) {
// TODO: Some confirmation modal
Hibiscus.data.txn { Hibiscus.data.txn {
account.delete() account.delete()
refreshAccounts() refreshAccounts()
} }
} }
}
}
ImGui.end()
drawCreateAccountPanel()
}
private val loginType = ImInt(1) private val loginType = ImInt(1)
private val yggdrasilUsername = ImString(320) private val yggdrasilUsername = ImString(320)
@ -103,9 +123,7 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu")
private fun drawCreateAccountPanel() { private fun drawCreateAccountPanel() {
if (createAccountPanelOpen.get()) { if (createAccountPanelOpen.get()) {
if (ImGui.begin("Add Account", createAccountPanelOpen)) { if (ImGui.begin("Add Account", createAccountPanelOpen, ImGuiWindowFlags.AlwaysAutoResize)) {
ImGui.pushItemWidth(ImGui.getContentRegionMaxX() - ImGui.getWindowContentRegionMinX())
ImGui.text("Login Type:") ImGui.text("Login Type:")
ImGui.combo( ImGui.combo(
"Login Type", "Login Type",
@ -114,10 +132,11 @@ object AccountManagerUIScreen : Screen(Text.of("account management hacker menu")
) )
ImGui.separator() ImGui.separator()
ImGui.pushItemWidth(ImGui.getContentRegionMaxX() - ImGui.getWindowContentRegionMinX())
when (loginType.get()) { when (loginType.get()) {
0 -> { 0 -> {
ImGui.text("Login Name (Username or Email):") ImGui.text("Username / Email Address:")
ImGui.inputText("##new-account-login-name", yggdrasilUsername) ImGui.inputText("##new-account-login-name", yggdrasilUsername)
ImGui.text("Password:") ImGui.text("Password:")
ImGui.inputText("##new-account-login-password", yggdrasilPassword, ImGuiInputTextFlags.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) { if (loginResult.user.isPresent) {
val user = loginResult.user.get() val user = loginResult.user.get()
val loginData = ByteArrayOutputStream()
.apply { loginResult.resultFile.write(this) }
.toString(Charsets.UTF_8)
yggdrasilUsername.set("") yggdrasilUsername.set("")
yggdrasilPassword.set("") yggdrasilPassword.set("")
microsoftToken.set("") microsoftToken.set("")
val loginData = ByteArrayOutputStream()
.apply { loginResult.resultFile.write(this) }
.toString(Charsets.UTF_8)
Hibiscus.data.txn { Hibiscus.data.txn {
MinecraftAccount.new { MinecraftAccount.new {
this.name = user.name this.name = user.name