From f9b34fd40deb4e089845f1967b8f719904a82983 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Fri, 25 Mar 2022 15:27:52 +0100 Subject: [PATCH] More thorough workaround for Char.code and Char(Int) crashing (#6419) --- .../com/unciv/ui/utils/KeyPressDispatcher.kt | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/core/src/com/unciv/ui/utils/KeyPressDispatcher.kt b/core/src/com/unciv/ui/utils/KeyPressDispatcher.kt index 0ff608f2ac..03e1d5c35c 100644 --- a/core/src/com/unciv/ui/utils/KeyPressDispatcher.kt +++ b/core/src/com/unciv/ui/utils/KeyPressDispatcher.kt @@ -42,7 +42,7 @@ data class KeyCharAndCode(val char: Char, val code: Int) { return when { char == Char.MIN_VALUE -> fixedKeysToString(code) this == ESC -> "ESC" - char < ' ' -> "Ctrl-" + Char(char.code+64) + char < ' ' -> "Ctrl-" + (char.toCode() + 64).makeChar() else -> "\"$char\"" } } @@ -63,16 +63,18 @@ data class KeyCharAndCode(val char: Char, val code: Int) { /** Guaranteed to be ignored by [KeyPressDispatcher.set] and never to be generated for an actual event, used as fallback to ensure no action is taken */ val UNKNOWN = KeyCharAndCode(Input.Keys.UNKNOWN) + // Kludges because we got crashes: java.lang.NoSuchMethodError: 'int kotlin.CharCodeKt.access$getCode$p(char)' + private fun Char.toCode() = + try { code } catch (ex: Throwable) { null } + ?: try { @Suppress("DEPRECATION") toInt() } catch (ex: Throwable) { null } + ?: 0 + private fun Int.makeChar() = + try { Char(this) } catch (ex: Throwable) { null } + ?: try { toChar() } catch (ex: Throwable) { null } + ?: Char.MIN_VALUE + /** mini-factory for control codes - case insensitive */ - fun ctrl(letter: Char) = KeyCharAndCode(Char( - try { - letter.code and 31 - } catch (ex: NoSuchMethodError) { - // Seems to happen on some Ubuntu systems - @Suppress("DEPRECATION") - letter.toInt() and 31 - } - ), 0) + fun ctrl(letter: Char) = KeyCharAndCode((letter.toCode() and 31).makeChar(),0) /** mini-factory for control codes from keyCodes */ fun ctrlFromCode(keyCode: Int): KeyCharAndCode {