HenryEx

fireMonoMethod

Aug 18th, 2026
4,560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | Source Code | 0 0
  1. --- Execute mono method from LUA, uses first found instance if inst = 0
  2. -- Based on: https://opencheattables.com/viewtopic.php?t=475
  3. -- EX: valueReturned = fireMonoMethod("SaveManager", "get_playerData", {100, true})
  4. -- fireMonoMethod( className: string OR number, methodName: string OR number, instance: number, args: table{any} )
  5. -- @param className: name (string) or id (number) of the class containing the method
  6. -- @param methodName: name (string) or id (number) of the method to execute
  7. -- @param instance: address of the class instance to execute from/on
  8. -- @param args: table of arguments to the method
  9. -- @return Result of invoking the method
  10.  
  11. function fireMonoMethod(className, methodName, instance, args)
  12.   if type(args) ~= "table" then error("Args is not a table, use {}") end
  13.  
  14.   local classId = type(className) == 'number' and className or mono_findClass("", className)
  15.   if not classId or classId == 0 then error("No class found") end
  16.  
  17.   local methodId = type(methodName) == 'number' and methodName or mono_class_findMethod(classId, methodName)
  18.   if not methodId or methodId == 0 then error("No method found") end
  19.  
  20.   instance = instance or 0
  21.   if instance == 0 then
  22.     instance = mono_class_findInstancesOfClassListOnly(nil, classId)[1]
  23.   end
  24.   if not instance or instance == 0 then error("No instance found") end
  25.  
  26.   local params = mono_method_get_parameters(methodId).parameters
  27.   if #args ~= #params then error("Found " .. #args .. " args, need " .. #params .. " args") end
  28.  
  29.   local newArgs = {}
  30.   for i, v in pairs(params) do
  31.     newArgs[i] = {
  32.       type = monoTypeToVartypeLookup[v.type],
  33.       value = args[i]
  34.     }
  35.   end
  36.  
  37.   return mono_invoke_method(nil, methodId, instance, newArgs)
  38. end
Advertisement
Add Comment
Please, Sign In to add comment