Skip to content

Commit addfc62

Browse files
committed
Update FFILib.kt
1 parent 45be8a9 commit addfc62

1 file changed

Lines changed: 41 additions & 24 deletions

File tree

  • src/main/kotlin/com/nekiplay/hypixelcry/features/lua/objects/misc

src/main/kotlin/com/nekiplay/hypixelcry/features/lua/objects/misc/FFILib.kt

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -278,31 +278,51 @@ class FFILib : LuaTable() {
278278
val returnType = parseCallbackReturnType(protoStr)
279279
val argTypes = parseCallbackArgTypes(protoStr)
280280

281-
val cb = object : Callback {
282-
@Suppress("UNCHECKED_CAST")
283-
override fun callback(args: Array<out Any>): Any {
284-
val luaArgs = mutableListOf<LuaValue>()
285-
for (i in argTypes.indices) {
286-
val arg = args[i]
287-
luaArgs.add(convertArgToLua(arg, argTypes[i]))
288-
}
281+
val callbackObj = createCallback(returnType, argTypes, luaFunc)
289282

290-
val res = try {
291-
luaFunc.invoke(LuaValue.varargsOf(luaArgs.toTypedArray()))
292-
} catch (e: Exception) {
293-
log("Callback error: ${e.message}")
294-
NIL
295-
}
283+
val ptr = CallbackReference.getFunctionPointer(callbackObj)
284+
callbackCache[ptr.hashCode().toLong()] = WeakReference(callbackObj)
285+
286+
val ptrType = typeRegistry["ptr"]!!
287+
return CData(ptr, ptrType, this@FFILib)
288+
}
296289

297-
return convertReturnFromLua(res, returnType)
290+
private fun createCallback(returnType: CType?, argTypes: List<CType>, luaFunc: LuaValue): Callback {
291+
val ffi = this@FFILib
292+
return when (argTypes.size) {
293+
0 -> object : Callback {
294+
fun invoke(): Any = invokeCallback(emptyList(), returnType, luaFunc, ffi)
295+
}
296+
1 -> object : Callback {
297+
@Suppress("UNCHECKED_CAST")
298+
fun invoke(a1: Any): Any = invokeCallback(listOf(a1), returnType, luaFunc, ffi)
299+
}
300+
2 -> object : Callback {
301+
@Suppress("UNCHECKED_CAST")
302+
fun invoke(a1: Any, a2: Any): Any = invokeCallback(listOf(a1, a2), returnType, luaFunc, ffi)
303+
}
304+
3 -> object : Callback {
305+
@Suppress("UNCHECKED_CAST")
306+
fun invoke(a1: Any, a2: Any, a3: Any): Any = invokeCallback(listOf(a1, a2, a3), returnType, luaFunc, ffi)
307+
}
308+
else -> object : Callback {
309+
@Suppress("UNCHECKED_CAST")
310+
fun invoke(a1: Any, a2: Any, a3: Any, a4: Any, a5: Any): Any = invokeCallback(listOf(a1, a2, a3, a4, a5), returnType, luaFunc, ffi)
298311
}
299312
}
313+
}
300314

301-
val ptr = CallbackReference.getFunctionPointer(cb)
302-
callbackCache[ptr.hashCode().toLong()] = WeakReference(cb)
315+
private fun invokeCallback(args: List<Any>, returnType: CType?, luaFunc: LuaValue, ffi: FFILib): Any {
316+
val luaArgs = args.map { convertArgToLua(it, null, ffi) }
303317

304-
val ptrType = typeRegistry["ptr"]!!
305-
return CData(ptr, ptrType, this@FFILib)
318+
val res = try {
319+
luaFunc.invoke(LuaValue.varargsOf(luaArgs.toTypedArray()))
320+
} catch (e: Exception) {
321+
log("Callback error: ${e.message}")
322+
NIL
323+
}
324+
325+
return convertReturnFromLua(res, returnType)
306326
}
307327

308328
private fun parseCallbackReturnType(proto: String): CType? {
@@ -327,16 +347,13 @@ class FFILib : LuaTable() {
327347
return listOf(typeRegistry["int"]!!, typeRegistry["int"]!!, typeRegistry["int"]!!)
328348
}
329349

330-
private fun convertArgToLua(arg: Any, type: CType?): LuaValue {
350+
private fun convertArgToLua(arg: Any, type: CType?, ffi: FFILib): LuaValue {
331351
return when (arg) {
332352
is Int -> valueOf(arg.toDouble())
333353
is Long -> valueOf(arg.toDouble())
334354
is Float -> valueOf(arg.toDouble())
335355
is Double -> valueOf(arg)
336-
is Pointer -> {
337-
if (type != null) CData(arg, type, this@FFILib)
338-
else CData(arg, typeRegistry["ptr"]!!, this@FFILib)
339-
}
356+
is Pointer -> CData(arg, typeRegistry["ptr"]!!, ffi)
340357
else -> NIL
341358
}
342359
}

0 commit comments

Comments
 (0)