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,44 +69,51 @@ 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 ->
ImGui.text("Selected account: ${account.name}")
ImGui.text("Last known username: ${account.lastKnownDisplayName}")
if (ImGui.button("Log In")) { if (ImGui.button("Log In")) {
try { signInToAccount(account)
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()
}
} }
ImGui.sameLine() ImGui.sameLine()
if (ImGui.button("Remove")) { if (ImGui.button("Remove")) {
// TODO: Some confirmation modal removeAccount(account)
Hibiscus.data.txn {
account.delete()
refreshAccounts()
}
} }
ImGui.text("Selected account: ${account.name}")
ImGui.text("Last known username: ${account.lastKnownDisplayName}")
} ?: run {
ImGui.text("No account selected.")
} }
} }
ImGui.end() 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) private val loginType = ImInt(1)
@ -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