--- Execute mono method from LUA, uses first found instance if inst = 0
-- Based on: https://opencheattables.com/viewtopic.php?t=475
-- EX: valueReturned = fireMonoMethod("SaveManager", "get_playerData", {100, true})
-- fireMonoMethod( className: string OR number, methodName: string OR number, instance: number, args: table{any} )
-- @param className: name (string) or id (number) of the class containing the method
-- @param methodName: name (string) or id (number) of the method to execute
-- @param instance: address of the class instance to execute from/on
-- @param args: table of arguments to the method
-- @return Result of invoking the method
function fireMonoMethod(className, methodName, instance, args)
if type(args) ~= "table" then error("Args is not a table, use {}") end
local classId = type(className) == 'number' and className or mono_findClass("", className)
if not classId or classId == 0 then error("No class found") end
local methodId = type(methodName) == 'number' and methodName or mono_class_findMethod(classId, methodName)
if not methodId or methodId == 0 then error("No method found") end
instance = instance or 0
if instance == 0 then
instance = mono_class_findInstancesOfClassListOnly(nil, classId)[1]
end
if not instance or instance == 0 then error("No instance found") end
local params = mono_method_get_parameters(methodId).parameters
if #args ~= #params then error("Found " .. #args .. " args, need " .. #params .. " args") end
local newArgs = {}
for i, v in pairs(params) do
newArgs[i] = {
type = monoTypeToVartypeLookup[v.type],
value = args[i]
}
end
return mono_invoke_method(nil, methodId, instance, newArgs)
end
Comments