Notes on Method Arguments

Number arguments in method calls

This applies to REManagedObject:call, obj:method(...), REMethodDefinition:call, sdk.call_native_func and ValueType:call.

A Lua number is converted to the type of the parameter it is passed to. It does not matter whether it is an integer or a float in Lua:

  • System.Single and System.Double parameters get a floating point value: 5 arrives as 5.0.
  • Integer, System.Boolean, System.Char and enum parameters get an integer. A float loses its fraction: 4 / 2 (which is 2.0 in Lua) arrives as 2.
  • out, ref and pointer parameters take an address. A Lua integer passed there is used as that address.
local sqrt = sdk.find_type_definition("System.Math"):get_method("Sqrt(System.Double)")
local root = sqrt:call(nil, 4) -- 2.0

local str = sdk.create_managed_string("ABCDEF")
local tail = str:call("Substring(System.Int32)", 4 / 2) -- "CDEF"

sdk.hook method arguments

ByRef parameters are not correctly supported by REFramework. ByRef parameters are essentially T** instead of T* type pointers. They are usually used for out parameters.

These will need to be manually dereferenced using a trick by instantiating a System.UInt64 and reading the mValue field to dereference it.

local function deref_ptr(ptr)
    local fake_int64 = sdk.to_valuetype(ptr, "System.UInt64")
    local deref = fake_int64:get_field("mValue")

    return deref
end

sdk.hook(sdk.find_type_definition("foo"):get_method("bar"),
    function(args)
        local deref = deref_ptr(args[6])
        local arg = sdk.to_managed_object(deref):add_ref()
    end,
    function(retval)
        return retval
    end
)

For out ref parameter, this can only be done inside the post hook.

sdk.hook(sdk.find_type_definition("foo"):get_method("bar"),
    function(args)
        local storage = thread.get_hook_storage()
        storage["ref_arg"] = args[3]
    end,
    function(retval)
        local ref_arg = thread.get_hook_storage()["ref_arg"]
        local deref = deref_ptr(ref_arg)
        local arg = sdk.to_managed_object(deref):add_ref()

        return retval
    end
)