ModLoader: load IME unconditionally when found

This commit is contained in:
minjaesong
2023-09-08 15:31:11 +09:00
parent 64ef30e445
commit cc55e563f0
2 changed files with 71 additions and 4 deletions

View File

@@ -2,11 +2,13 @@ package net.torvald.terrarum
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.utils.JsonValue
import net.torvald.terrarum.App.*
import net.torvald.terrarum.App.setToGameConfig
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.blockproperties.WireCodex
import net.torvald.terrarum.gamecontroller.IME
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.itemproperties.ItemCodex
@@ -15,6 +17,7 @@ import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.utils.CSVFetcher
import net.torvald.terrarum.utils.JsonFetcher
import net.torvald.terrarum.utils.forEachSiblings
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
@@ -217,6 +220,12 @@ object ModMgr {
GameLanguageLoader(moduleName)
}
// add keylayouts if exists
if (hasFile(moduleName, "keylayout")) {
printdbg(this, "Trying to load Keyboard Layouts on ${moduleName}")
GameIMELoader(moduleName)
}
// run entry script in entry point
if (entryPoint.isNotBlank()) {
var newClass: Class<*>? = null
@@ -519,6 +528,56 @@ object ModMgr {
}
}
object GameIMELoader {
const val keebPath = "keylayout/"
@JvmStatic operator fun invoke(module: String) {
val FILE = getFile(module, keebPath)
FILE.listFiles { file, s -> s.endsWith(".${IME.KEYLAYOUT_EXTENSION}") }.sortedBy { it.name }.forEach {
printdbg(this, "Registering Low layer ${it.nameWithoutExtension.lowercase()}")
IME.registerLowLayer(it.nameWithoutExtension.lowercase(), IME.parseKeylayoutFile(it))
}
FILE.listFiles { file, s -> s.endsWith(".${IME.IME_EXTENSION}") }.sortedBy { it.name }.forEach {
printdbg(this, "Registering High layer ${it.nameWithoutExtension.lowercase()}")
IME.registerHighLayer(it.nameWithoutExtension.lowercase(), IME.parseImeFile(it))
}
val iconFile = getFile(module, keebPath + "icons.tga").let {
if (it.exists()) it else getFile(module, keebPath + "icons.png")
}
if (iconFile.exists()) {
val iconSheet = TextureRegionPack(iconFile.path, 20, 20)
val iconPixmap = Pixmap(Gdx.files.internal(iconFile.path))
for (k in 0 until iconPixmap.height step 20) {
val langCode = StringBuilder()
for (c in 0 until 20) {
val x = c
var charnum = 0
for (b in 0 until 7) {
val y = k + b
if (iconPixmap.getPixel(x, y) and 255 != 0) {
charnum = charnum or (1 shl b)
}
}
if (charnum != 0) langCode.append(charnum.toChar())
}
if (langCode.isNotEmpty()) {
printdbg(this, "Icon order #${(k+1) / 20} - icons[\"$langCode\"] = iconSheet.get(1, ${k/20})")
IME.icons["$langCode"] = iconSheet.get(1, k / 20).also { it.flip(false, false) }
}
}
App.disposables.add(iconSheet)
iconPixmap.dispose()
}
}
}
object GameMaterialLoader {
const val matePath = "materials/"

View File

@@ -97,12 +97,12 @@ object IME {
File(KEYLAYOUT_DIR).listFiles { file, s -> s.endsWith(".$KEYLAYOUT_EXTENSION") }.sortedBy { it.name }.forEach {
printdbg(this, "Registering Low layer ${it.nameWithoutExtension.lowercase()}")
lowLayers[it.nameWithoutExtension.lowercase()] = parseKeylayoutFile(it)
registerLowLayer(it.nameWithoutExtension.lowercase(), parseKeylayoutFile(it))
}
File(KEYLAYOUT_DIR).listFiles { file, s -> s.endsWith(".$IME_EXTENSION") }.sortedBy { it.name }.forEach {
printdbg(this, "Registering High layer ${it.nameWithoutExtension.lowercase()}")
highLayers[it.nameWithoutExtension.lowercase()] = parseImeFile(it)
registerHighLayer(it.nameWithoutExtension.lowercase(), parseImeFile(it))
}
@@ -132,6 +132,14 @@ object IME {
iconPixmap.dispose()
}
fun registerLowLayer(name: String, layout: TerrarumKeyLayout) {
lowLayers[name] = layout
}
fun registerHighLayer(name: String, ime: TerrarumIME) {
highLayers[name] = ime
}
@JvmStatic fun invoke() {}
fun getLowLayerByName(name: String): TerrarumKeyLayout {
@@ -164,7 +172,7 @@ object IME {
else -> throw IllegalArgumentException("Unknown candidates mode: $this")
}
private fun parseKeylayoutFile(file: File): TerrarumKeyLayout {
fun parseKeylayoutFile(file: File): TerrarumKeyLayout {
val src = file.readText(Charsets.UTF_8)
val jsval = context.eval("js", "'use strict';Object.freeze($src)")
val name = jsval.getMember("n").asString()
@@ -200,7 +208,7 @@ object IME {
else -> throw IllegalArgumentException("Unknown operation mode: $this")
}
private fun parseImeFile(file: File): TerrarumIME {
fun parseImeFile(file: File): TerrarumIME {
val code = file.readText(Charsets.UTF_8)
val jsval = context.eval("js", "\"use strict\";(function(){$code})()")
val name = jsval.getMember("n").asString()