From e3b82ae5b6490aea13ea1d545f4b2dd65cf9b5f0 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 28 Jan 2022 09:50:05 +0900 Subject: [PATCH] console command loading from command list on the module directory --- .../torvald/terrarum/console/CommandDict.kt | 65 +++--- .../terrarum/console/CommandInterpreter.kt | 1 - src/net/torvald/terrarum/ui/ConsoleWindow.kt | 4 +- .../terrarum/ui/UIItemTextLineInput.kt | 221 +++++++++--------- 4 files changed, 141 insertions(+), 150 deletions(-) diff --git a/src/net/torvald/terrarum/console/CommandDict.kt b/src/net/torvald/terrarum/console/CommandDict.kt index b34e2ffea..7bc820969 100644 --- a/src/net/torvald/terrarum/console/CommandDict.kt +++ b/src/net/torvald/terrarum/console/CommandDict.kt @@ -41,55 +41,42 @@ object CommandDict { val packageConsole = "$packageRoot.console" printdbg(this, "Loading console commands from '${packageConsole}'") +// printdbg(this, commandsList.joinToString()) + commandsList.forEach { commandName -> + val canonicalName = "$packageConsole.$commandName" + val it = Class.forName(canonicalName) - // TODO load commands using commandsList + printdbg(this, "> Trying to instantiate ${it.canonicalName}") + try { + val instance = it.kotlin.objectInstance ?: it.kotlin.java.newInstance() - val stream = ClassLoader.getSystemClassLoader().getResourceAsStream(packageConsole.replace('.', '/')) + val aliases = instance.javaClass.getAnnotation(ConsoleAlias::class.java)?.aliasesCSV?.split(',')?.map { it.trim() } + val noexport = instance.javaClass.getAnnotation(ConsoleNoExport::class.java) + if (noexport == null) { - if (stream != null) { // not all modules have extra console commands - - val reader = BufferedReader(InputStreamReader(stream)) - - reader.lines() - .filter { it.endsWith(".class") && !it.contains('$') } - .map { Class.forName("$packageConsole.${it.substring(0, it.lastIndexOf('.'))}") } - .forEach { - - printdbg(this, "> Trying to instantiate ${it.canonicalName}") - - try { - val instance = it.kotlin.objectInstance ?: it.kotlin.java.newInstance() - - val aliases = instance.javaClass.getAnnotation(ConsoleAlias::class.java)?.aliasesCSV?.split(',')?.map { it.trim() } - val noexport = instance.javaClass.getAnnotation(ConsoleNoExport::class.java) - - if (noexport == null) { - - dict[instance.javaClass.simpleName.lowercase()] = instance as ConsoleCommand - aliases?.forEach { - dict[it] = instance as ConsoleCommand - } - - printdbg(this, "Class instantiated: ${instance.javaClass.simpleName}") - if (aliases != null) - printdbg(this, " Annotations: $aliases") - } - } - catch (e: ClassCastException) { - printdbgerr(this, "${it.canonicalName} is not a ConsoleCommand") - } - catch (e: InstantiationException) { - printdbgerr(this, "Could not instantiate ${it.canonicalName}") - e.printStackTrace(System.err) - } + dict[instance.javaClass.simpleName.lowercase()] = instance as ConsoleCommand + aliases?.forEach { + dict[it] = instance as ConsoleCommand } + printdbg(this, "Class instantiated: ${instance.javaClass.simpleName}") + if (aliases != null) + printdbg(this, " Annotations: $aliases") + } + } + catch (e: ClassCastException) { + printdbgerr(this, "${it.canonicalName} is not a ConsoleCommand") + } + catch (e: InstantiationException) { + printdbgerr(this, "Could not instantiate ${it.canonicalName}") + e.printStackTrace(System.err) + } } - } + } } diff --git a/src/net/torvald/terrarum/console/CommandInterpreter.kt b/src/net/torvald/terrarum/console/CommandInterpreter.kt index 0fc483fa0..1fccc6da6 100644 --- a/src/net/torvald/terrarum/console/CommandInterpreter.kt +++ b/src/net/torvald/terrarum/console/CommandInterpreter.kt @@ -64,7 +64,6 @@ internal object CommandInterpreter { } } catch (e: NullPointerException) { - e.printStackTrace() echoUnknownCmd(single_command.name) } } diff --git a/src/net/torvald/terrarum/ui/ConsoleWindow.kt b/src/net/torvald/terrarum/ui/ConsoleWindow.kt index 49dacd3fc..b2222aea3 100644 --- a/src/net/torvald/terrarum/ui/ConsoleWindow.kt +++ b/src/net/torvald/terrarum/ui/ConsoleWindow.kt @@ -55,7 +55,9 @@ class ConsoleWindow : UICanvas() { private var iMadeTheGameToPause = false - private val textinput = UIItemTextLineInput(this, 0, 0, this.width) + private val textinput = UIItemTextLineInput(this, 0, 0, this.width, keyFilter = { e -> + !e.keycodes.contains(Input.Keys.GRAVE) + }) private var clickLatched = false diff --git a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt index 4ea547b58..cbca58cf3 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt @@ -64,6 +64,7 @@ class UIItemTextLineInput( val maxLen: InputLenCap = InputLenCap(1000, InputLenCap.CharLenUnit.CODEPOINTS), // val enablePasteButton: Boolean = true, // val enableIMEButton: Boolean = true + var keyFilter: (TerrarumKeyboardEvent) -> Boolean = { true } ) : UIItem(parentUI, initialX, initialY) { init { @@ -203,138 +204,140 @@ class UIItemTextLineInput( val (eventType, char, headkey, repeatCount, keycodes) = e - try { - if (eventType == InputStrober.KEY_DOWN || eventType == InputStrober.KEY_CHANGE) { - fboUpdateLatch = true - forceLitCursor() - val ime = getIME() - val lowLayer = IME.getLowLayerByName(App.getConfigString("basekeyboardlayout")) + if (keyFilter(e)) { + try { + if (eventType == InputStrober.KEY_DOWN || eventType == InputStrober.KEY_CHANGE) { + fboUpdateLatch = true + forceLitCursor() + val ime = getIME() + val lowLayer = IME.getLowLayerByName(App.getConfigString("basekeyboardlayout")) - if (keycodes.contains(Input.Keys.V) && keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT)) { - endComposing() - paste(Clipboard.fetch().substringBefore('\n').substringBefore('\t').toCodePoints()) - } - else if (keycodes.contains(Input.Keys.C) && (keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT))) { - endComposing() - copyToClipboard() - } - else if (keycodes.contains(Input.Keys.BACKSPACE) || (keycodes.contains(Input.Keys.CAPS_LOCK) && lowLayer.capsMode == TerrarumKeyCapsMode.BACK)) { + if (keycodes.contains(Input.Keys.V) && keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT)) { + endComposing() + paste(Clipboard.fetch().substringBefore('\n').substringBefore('\t').toCodePoints()) + } + else if (keycodes.contains(Input.Keys.C) && (keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT))) { + endComposing() + copyToClipboard() + } + else if (keycodes.contains(Input.Keys.BACKSPACE) || (keycodes.contains(Input.Keys.CAPS_LOCK) && lowLayer.capsMode == TerrarumKeyCapsMode.BACK)) { -// printdbg(this, "BACKSPACE hit; ime.composing=${ime?.composing?.invoke()}; buflen=${textbuf.size}") + // printdbg(this, "BACKSPACE hit; ime.composing=${ime?.composing?.invoke()}; buflen=${textbuf.size}") - if (ime != null && ime.composing()) { - if (ime.config.mode == TerrarumIMEMode.CANDIDATES) { - candidates = ime.backspace().map { CodepointSequence(it.toCodePoints()) } - } - else if (ime.config.mode == TerrarumIMEMode.REWRITE) { - candidates = listOf() - val op = ime.backspace() - if (textbuf.isNotEmpty()) { - inputBackspaceOnce(1) + if (ime != null && ime.composing()) { + if (ime.config.mode == TerrarumIMEMode.CANDIDATES) { + candidates = ime.backspace().map { CodepointSequence(it.toCodePoints()) } } + else if (ime.config.mode == TerrarumIMEMode.REWRITE) { + candidates = listOf() + val op = ime.backspace() + if (textbuf.isNotEmpty()) { + inputBackspaceOnce(1) + } - if (op.size > 0) { - val codepoints = op[0].toCodePoints() - if (!maxLen.exceeds(textbuf, codepoints)) { - textbuf.addAll(cursorX, codepoints) + if (op.size > 0) { + val codepoints = op[0].toCodePoints() + if (!maxLen.exceeds(textbuf, codepoints)) { + textbuf.addAll(cursorX, codepoints) - moveCursorToEnd(codepoints.size) + moveCursorToEnd(codepoints.size) + } } } } - } - else if (cursorX <= 0) { - cursorX = 0 - cursorDrawX = 0 - cursorDrawScroll = 0 - } - else { - endComposing() - inputBackspaceOnce(2) - } - } - else if (keycodes.contains(Input.Keys.LEFT)) { - endComposing() - - if (cursorX > 0) { - cursorX -= 1 - cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) - tryCursorForward() - if (cursorX <= 0) { + else if (cursorX <= 0) { cursorX = 0 cursorDrawX = 0 cursorDrawScroll = 0 } - } - } - else if (keycodes.contains(Input.Keys.RIGHT)) { - endComposing() - - if (cursorX < textbuf.size) { - cursorX += 1 - cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) - tryCursorBack() - } - } - else if (keycodes.containsSome(Input.Keys.ENTER, Input.Keys.NUMPAD_ENTER)) { - endComposing() - -// println("END COMPOSING!!") - } - // accept: - // - literal "<" - // - keysymbol that does not start with "<" (not always has length of 1 because UTF-16) - else if (char != null && char.length > 0 && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) { - val shiftin = keycodes.containsSome(Input.Keys.SHIFT_LEFT, Input.Keys.SHIFT_RIGHT) - val altgrin = keycodes.contains(Input.Keys.ALT_RIGHT) || keycodes.containsAll(Input.Keys.ALT_LEFT, Input.Keys.CONTROL_LEFT) - - val codepoints = if (ime != null) { - if (ime.config.mode == TerrarumIMEMode.CANDIDATES) { - val newStatus = ime.acceptChar(headkey, shiftin, altgrin, char) - candidates = newStatus.first.map { CodepointSequence(it.toCodePoints()) } - - newStatus.second.toCodePoints() + else { + endComposing() + inputBackspaceOnce(2) } - else if (ime.config.mode == TerrarumIMEMode.REWRITE) { - candidates = listOf() - val op = ime.acceptChar(headkey, shiftin, altgrin, char) + } + else if (keycodes.contains(Input.Keys.LEFT)) { + endComposing() -// printdbg(this, "delcount: ${op.first[0].toInt()}, rewrite: '${op.second}'") - - repeat(op.first[0].toInt()) { - if (textbuf.isNotEmpty()) { -// printdbg(this, "") - inputBackspaceOnce(3) - } + if (cursorX > 0) { + cursorX -= 1 + cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) + tryCursorForward() + if (cursorX <= 0) { + cursorX = 0 + cursorDrawX = 0 + cursorDrawScroll = 0 } - - op.second.toCodePoints() } - else throw IllegalArgumentException("Unknown IME Operation mode: ${ime.config.mode}") } - else char.toCodePoints() + else if (keycodes.contains(Input.Keys.RIGHT)) { + endComposing() -// printdbg(this, "textinput codepoints: ${codepoints.map { it.toString(16) }.joinToString()}") - - if (!maxLen.exceeds(textbuf, codepoints)) { - textbuf.addAll(cursorX, codepoints) - - moveCursorToEnd(codepoints.size) + if (cursorX < textbuf.size) { + cursorX += 1 + cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) + tryCursorBack() + } } + else if (keycodes.containsSome(Input.Keys.ENTER, Input.Keys.NUMPAD_ENTER)) { + endComposing() + + // println("END COMPOSING!!") + } + // accept: + // - literal "<" + // - keysymbol that does not start with "<" (not always has length of 1 because UTF-16) + else if (char != null && char.length > 0 && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) { + val shiftin = keycodes.containsSome(Input.Keys.SHIFT_LEFT, Input.Keys.SHIFT_RIGHT) + val altgrin = keycodes.contains(Input.Keys.ALT_RIGHT) || keycodes.containsAll(Input.Keys.ALT_LEFT, Input.Keys.CONTROL_LEFT) + + val codepoints = if (ime != null) { + if (ime.config.mode == TerrarumIMEMode.CANDIDATES) { + val newStatus = ime.acceptChar(headkey, shiftin, altgrin, char) + candidates = newStatus.first.map { CodepointSequence(it.toCodePoints()) } + + newStatus.second.toCodePoints() + } + else if (ime.config.mode == TerrarumIMEMode.REWRITE) { + candidates = listOf() + val op = ime.acceptChar(headkey, shiftin, altgrin, char) + + // printdbg(this, "delcount: ${op.first[0].toInt()}, rewrite: '${op.second}'") + + repeat(op.first[0].toInt()) { + if (textbuf.isNotEmpty()) { + // printdbg(this, "") + inputBackspaceOnce(3) + } + } + + op.second.toCodePoints() + } + else throw IllegalArgumentException("Unknown IME Operation mode: ${ime.config.mode}") + } + else char.toCodePoints() + + // printdbg(this, "textinput codepoints: ${codepoints.map { it.toString(16) }.joinToString()}") + + if (!maxLen.exceeds(textbuf, codepoints)) { + textbuf.addAll(cursorX, codepoints) + + moveCursorToEnd(codepoints.size) + } + } + + // don't put innards of tryCursorBack/Forward here -- you absolutely don't want that behaviour } - - // don't put innards of tryCursorBack/Forward here -- you absolutely don't want that behaviour } - } - catch (e: IndexOutOfBoundsException) { - e.printStackTrace() - } - catch (e: NullPointerException) { - e.printStackTrace() - } + catch (e: IndexOutOfBoundsException) { + e.printStackTrace() + } + catch (e: NullPointerException) { + e.printStackTrace() + } - if (textbuf.size == 0) { - currentPlaceholderText = CodepointSequence(placeholder().toCodePoints()) + if (textbuf.size == 0) { + currentPlaceholderText = CodepointSequence(placeholder().toCodePoints()) + } } } else if (oldActive) { // just became deactivated