mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-13 13:47:47 +02:00
feat(scripting): interface builder
This commit is contained in:
@ -58,7 +58,7 @@ class ModContext {
|
||||
val event = EventBus(this)
|
||||
val eventDispatcher = EventDispatcher(this)
|
||||
val native = NativeLib()
|
||||
val scriptRuntime by lazy { CoreScriptRuntime(log, androidContext.classLoader) }
|
||||
val scriptRuntime by lazy { CoreScriptRuntime(androidContext, log) }
|
||||
|
||||
val isDeveloper by lazy { config.scripting.developerMode.get() }
|
||||
|
||||
|
@ -13,5 +13,5 @@ abstract class IPCInterface {
|
||||
@Suppress("unused")
|
||||
fun emit(eventName: String) = emit(eventName, *emptyArray())
|
||||
@Suppress("unused")
|
||||
fun emit(channel: String, eventName: String) = broadcast(channel, eventName, *emptyArray())
|
||||
fun emit(channel: String, eventName: String) = broadcast(channel, eventName)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package me.rhunk.snapenhance.scripting
|
||||
|
||||
import me.rhunk.snapenhance.core.logger.AbstractLogger
|
||||
import android.os.Handler
|
||||
import android.widget.Toast
|
||||
import me.rhunk.snapenhance.scripting.ktx.contextScope
|
||||
import me.rhunk.snapenhance.scripting.ktx.putFunction
|
||||
import me.rhunk.snapenhance.scripting.ktx.scriptableObject
|
||||
@ -20,6 +21,7 @@ class JSModule(
|
||||
|
||||
fun load(block: ScriptableObject.() -> Unit) {
|
||||
contextScope {
|
||||
val classLoader = scriptRuntime.androidContext.classLoader
|
||||
moduleObject = initSafeStandardObjects()
|
||||
moduleObject.putConst("module", moduleObject, scriptableObject {
|
||||
putConst("info", this, scriptableObject {
|
||||
@ -53,12 +55,12 @@ class JSModule(
|
||||
|
||||
moduleObject.putFunction("findClass") {
|
||||
val className = it?.get(0).toString()
|
||||
scriptRuntime.classLoader.loadClass(className)
|
||||
classLoader.loadClass(className)
|
||||
}
|
||||
|
||||
moduleObject.putFunction("type") { args ->
|
||||
val className = args?.get(0).toString()
|
||||
val clazz = scriptRuntime.classLoader.loadClass(className)
|
||||
val clazz = classLoader.loadClass(className)
|
||||
|
||||
scriptableObject("JavaClassWrapper") {
|
||||
putFunction("newInstance") newInstance@{ args ->
|
||||
@ -90,6 +92,19 @@ class JSModule(
|
||||
Undefined.instance
|
||||
}
|
||||
|
||||
for (toastFunc in listOf("longToast", "shortToast")) {
|
||||
moduleObject.putFunction(toastFunc) { args ->
|
||||
Handler(scriptRuntime.androidContext.mainLooper).post {
|
||||
Toast.makeText(
|
||||
scriptRuntime.androidContext,
|
||||
args?.joinToString(" ") ?: "",
|
||||
if (toastFunc == "longToast") Toast.LENGTH_LONG else Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
Undefined.instance
|
||||
}
|
||||
}
|
||||
|
||||
block(moduleObject)
|
||||
evaluateString(moduleObject, content, moduleInfo.name, 1, null)
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.rhunk.snapenhance.scripting
|
||||
|
||||
import android.content.Context
|
||||
import me.rhunk.snapenhance.core.logger.AbstractLogger
|
||||
import me.rhunk.snapenhance.scripting.type.ModuleInfo
|
||||
import org.mozilla.javascript.ScriptableObject
|
||||
@ -8,8 +9,8 @@ import java.io.ByteArrayInputStream
|
||||
import java.io.InputStream
|
||||
|
||||
open class ScriptRuntime(
|
||||
val androidContext: Context,
|
||||
val logger: AbstractLogger,
|
||||
val classLoader: ClassLoader,
|
||||
) {
|
||||
var buildModuleObject: ScriptableObject.(JSModule) -> Unit = {}
|
||||
private val modules = mutableMapOf<String, JSModule>()
|
||||
|
@ -1,5 +1,6 @@
|
||||
package me.rhunk.snapenhance.scripting.core
|
||||
|
||||
import android.content.Context
|
||||
import me.rhunk.snapenhance.bridge.scripting.IPCListener
|
||||
import me.rhunk.snapenhance.bridge.scripting.IScripting
|
||||
import me.rhunk.snapenhance.core.logger.AbstractLogger
|
||||
@ -9,9 +10,9 @@ import me.rhunk.snapenhance.scripting.ScriptRuntime
|
||||
import me.rhunk.snapenhance.scripting.core.impl.ScriptHooker
|
||||
|
||||
class CoreScriptRuntime(
|
||||
androidContext: Context,
|
||||
logger: AbstractLogger,
|
||||
classLoader: ClassLoader,
|
||||
): ScriptRuntime(logger, classLoader) {
|
||||
): ScriptRuntime(androidContext, logger) {
|
||||
private val scriptHookers = mutableListOf<ScriptHooker>()
|
||||
|
||||
fun connect(scriptingInterface: IScripting) {
|
||||
@ -38,7 +39,7 @@ class CoreScriptRuntime(
|
||||
sendIPCMessage(channel, eventName, args)
|
||||
}
|
||||
})
|
||||
putConst("hooker", this, ScriptHooker(module.moduleInfo, logger, classLoader).also {
|
||||
putConst("hooker", this, ScriptHooker(module.moduleInfo, logger, androidContext.classLoader).also {
|
||||
scriptHookers.add(it)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user