mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-12 13:17:42 +02:00
feat(core/scripting): primitive value handler
This commit is contained in:
@ -42,7 +42,7 @@ class JSModule(
|
||||
val value = args[2]
|
||||
val field = obj.unwrap().javaClass.declaredFields.find { it.name == name } ?: return@putFunction Undefined.instance
|
||||
field.isAccessible = true
|
||||
field.set(obj.unwrap(), value)
|
||||
field.set(obj.unwrap(), value.toPrimitiveValue(lazy { field.type.name }))
|
||||
Undefined.instance
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
package me.rhunk.snapenhance.scripting
|
||||
|
||||
fun Any?.toPrimitiveValue(type: Lazy<String>) = when (this) {
|
||||
is Number -> when (type.value) {
|
||||
"byte" -> this.toByte()
|
||||
"short" -> this.toShort()
|
||||
"int" -> this.toInt()
|
||||
"long" -> this.toLong()
|
||||
"float" -> this.toFloat()
|
||||
"double" -> this.toDouble()
|
||||
"boolean" -> this.toByte() != 0.toByte()
|
||||
"char" -> this.toInt().toChar()
|
||||
else -> this
|
||||
}
|
||||
is Boolean -> if (type.value == "boolean") this.toString().toBoolean() else this
|
||||
else -> this
|
||||
}
|
@ -6,6 +6,7 @@ import me.rhunk.snapenhance.hook.HookStage
|
||||
import me.rhunk.snapenhance.hook.Hooker
|
||||
import me.rhunk.snapenhance.hook.hook
|
||||
import me.rhunk.snapenhance.hook.hookConstructor
|
||||
import me.rhunk.snapenhance.scripting.toPrimitiveValue
|
||||
import me.rhunk.snapenhance.scripting.type.ModuleInfo
|
||||
import org.mozilla.javascript.annotations.JSGetter
|
||||
import org.mozilla.javascript.annotations.JSSetter
|
||||
@ -19,7 +20,12 @@ class ScriptHookCallback(
|
||||
) {
|
||||
var result
|
||||
@JSGetter("result") get() = hookAdapter.getResult()
|
||||
@JSSetter("result") set(result) = hookAdapter.setResult(result)
|
||||
@JSSetter("result") set(result) = hookAdapter.setResult(result.toPrimitiveValue(lazy {
|
||||
when (val member = hookAdapter.method()) {
|
||||
is Method -> member.returnType.name
|
||||
else -> "void"
|
||||
}
|
||||
}))
|
||||
|
||||
val thisObject
|
||||
@JSGetter("thisObject") get() = hookAdapter.nullableThisObject<Any>()
|
||||
@ -43,29 +49,14 @@ class ScriptHookCallback(
|
||||
fun arg(index: Int) = hookAdapter.argNullable<Any>(index)
|
||||
|
||||
fun setArg(index: Int, value: Any) {
|
||||
val parameterType by lazy { parameterTypes[index] }
|
||||
|
||||
if (value is Number && parameterType.isPrimitive) {
|
||||
hookAdapter.setArg(index, when (parameterType.name) {
|
||||
"byte" -> value.toByte()
|
||||
"short" -> value.toShort()
|
||||
"int" -> value.toInt()
|
||||
"long" -> value.toLong()
|
||||
"float" -> value.toFloat()
|
||||
"double" -> value.toDouble()
|
||||
"boolean" -> value.toByte() != 0.toByte()
|
||||
"char" -> value.toInt().toChar()
|
||||
else -> value
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
hookAdapter.setArg(index, value)
|
||||
hookAdapter.setArg(index, value.toPrimitiveValue(lazy { parameterTypes[index].name }))
|
||||
}
|
||||
|
||||
fun invokeOriginal() = hookAdapter.invokeOriginal()
|
||||
|
||||
fun invokeOriginal(args: Array<Any>) = hookAdapter.invokeOriginal(args)
|
||||
fun invokeOriginal(args: Array<Any>) = hookAdapter.invokeOriginal(args.map {
|
||||
it.toPrimitiveValue(lazy { parameterTypes[args.indexOf(it)].name }) ?: it
|
||||
}.toTypedArray())
|
||||
|
||||
override fun toString(): String {
|
||||
return "ScriptHookCallback(\n" +
|
||||
|
Reference in New Issue
Block a user