How to Invoke a Method?

gideon25

Expert Cheater
Mar 20, 2017
101
4
18
#1
Hi, I noticed that cheatengine mono has a function that invokes a method. Can someone show me a sample script where that is possible using the mono_invoke_method in the
mono LUA_Functions? Like use this method:
DebugMenu_Cheats:FlipMapCards

I need a script example because I want to make some options that invoke various debug cheats. I used the mono dissector but can't figure out how to get it into a script. Thanks!
 
Jul 21, 2017
13
6
3
#2
I'd never done it before, but here's a simple example that works with DeadAge. I find the combatant pointer with an AA script and this works to return the current HP, pretty cool...

Code:
local method = mono_findMethod('', 'Combatant', 'GetCurrentHP') -- no namespace
local address = 0x24173e70 -- address of main player I found
local domain = mono_enumDomains()[1] -- should only be one domain, but needed for call
return mono_invoke_method(domain, method, address, {}) -- empty table for args
In a script you would just put "{$lua}" before and "${asm}" after it and the lua code will run when you enable/disable the script, and only run if syntaxcheck isn't true. This little diddy speaks the current hit points out loud when enabled:

Code:
[enable]
{$lua}
if (syntaxcheck) then return end
local method = mono_findMethod('', 'Combatant', 'GetCurrentHP')
local address = readPointer('pCharacter') -- global pointer storing pointer to a 'Combatant' instance
local class = mono_method_getClass(method)
local domain = mono_enumDomains()[1]
local hp = mono_invoke_method(domain, method, address, {})
local name = readString(readPointer(readPointer('pCharacter')+0x18)+0x0c, 32, true)
speak(name..' currently has '..tostring(math.floor(hp))..' hit points')
{$asm}

[disable]
 
Last edited:

gideon25

Expert Cheater
Mar 20, 2017
101
4
18
#3
Great thanks! I did it like this and it worked:


Code:
[enable]

{$lua}
if (syntaxcheck) then return end
local method = mono_findMethod('', 'DebugMenu_Cheats', 'FlipMapCards')
local class = mono_method_getClass(method)
local domain = mono_enumDomains()[1]
local hp = mono_invoke_method(domain, method, address, {})
{$asm}

[disable]
Now what about methods that want you to set a bool or something? Like I can call these get and set but nothing happens. Take a look at this screenshot from Dnspy please:

It looks like its a method called set_AllTrials but there is a rachet which means its a "member" so maybe Im not coding it correctly? Also it looks like all of the racthets Members have a get and set method and I think they want a value. like set it to 1 to enable? Anyway this doesn't do anything:


Code:
[enable]

{$lua}
if (syntaxcheck) then return end
local method = mono_findMethod('', 'DebugMenu_Cheats', 'set_AllTrials')
local class = mono_method_getClass(method)
local domain = mono_enumDomains()[1]
local hp = mono_invoke_method(domain, method, address, {})
{$asm}

[disable]
And looking at this screenshot when I use cheat engine JIT to invoke it, it wants a bool:


Do I have to add extra commas because its a "member" and how do I set a bool in the script to 1 (just to try)?
Thanks a LOT, this will help me in the future!
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
430
48
28
#4
Looking at the "monoscript.lua" file, it looks like the args is a table with type and value keys/values.
Try the mono_invoke_method_dialog if you are just testing stuff.

CE folder/autorun/monoscript.lua
line 1659
Lua:
function mono_invoke_method_dialog(domain, method, address)
  --spawn a dialog where the user can fill in fields like: instance and parameter values
  --parameter fields will be of the proper type
line 1832
Lua:
    local params=mono_method_get_parameters(method)

    --use monoTypeToVartypeLookup to convert it to the type mono_method_invole likes it
    local args={}
    for i=1, #params.parameters do
      args[i]={}
      args[i].type=monoTypeToVartypeLookup[params.parameters[i].type]
      if args[i].type==vtString then
        args[i].value=mifinfo.parameters[i].edtVarText.Text
      else
        args[i].value=tonumber(mifinfo.parameters[i].edtVarText.Text)
      end

      if args[i].value==nil then
        messageDialog(translate('parameter ')..i..': "'..mifinfo.parameters[i].edtVarText.Text..'" '..translate('is not a valid value'), mtError, mbOK)
        return
      end
    end
    
    _G.args=args
    _G.instance=instance
    _G.method=method
    _G.bla=123
    
    local r=mono_invoke_method(domain, method, instance, args)
    if r then
      print(r)
    end
line 1891
Lua:
function mono_invoke_method(domain, method, object, args)
 

gideon25

Expert Cheater
Mar 20, 2017
101
4
18
#5
Yea I tried using the dialog to test some of them out and I guess some aren't working. But lets say I wanted to put in a boolean value directly into a script without using the dialog. Is that even possible? like what would it look like with this script here:

Code:
[enable]

{$lua}
if (syntaxcheck) then return end
local method = mono_findMethod('', 'DebugMenu_Cheats', 'set_AllTrials')
local class = mono_method_getClass(method)
local domain = mono_enumDomains()[1]
local hp = mono_invoke_method(domain, method, address, {})
{$asm}

[disable]
So I want to set_AllTrials to boolean =1. How would it be written? Also I see Domain, Method,Object,args. Also Set_AllTrials is a "member." Thanks!
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
430
48
28
#6
From what I can tell you just need to set the type and value for each argument; The "set_AllTrials" only has 1 argument so something like this should work.
CEA:
[enable]
{$lua}
if (syntaxcheck) then return end
local method = mono_findMethod('', 'DebugMenu_Cheats', 'set_AllTrials')
local class = mono_method_getClass(method)
local domain = mono_enumDomains()[1]
local args = {}
args[1] = {}
args[1].type = monoTypeToVartypeLookup[MONO_TYPE_BOOLEAN] -- vtByte
args[1].value = 1
local r = mono_invoke_method(domain, method, address, args)
if r then
    print(r)
end
{$asm}
[disable]
 

koderkrazy

Expert Cheater
Fearless Donors
Table Maker
Jun 17, 2018
118
69
28
#7
Thank you guys!
I've successfully called the getter and setter methods on player object after learning from above discussion.
Check my table Hero-U_TableV3 https://fearlessrevolution.com/threads/hero-u-rogue-to-redemption-time-player-stats-god-mode.7417/

@TheyCallMeTim13 I've managed to call most of the methods on the stat object except one:
C#:
    public Stat GetStat(string name)
    {
        return (Stat)(((object)Skills[name]) ?? ((object)Attributes[name]));
    }
My Lua code:
Bash:
[enable]
{$lua}
if (syntaxcheck) then return end
  print('Start!')

  local method = mono_findMethod('', 'CharacterSheet', 'GetStat')
  print(string.format('Method ptr: %02X', method))

  local address = readPointer('pCharSheet')

  local args={}
  args[1] = {}
  args[1].type = monoTypeToVartypeLookup[MONO_TYPE_STRING]
  --args[1].value='Stealth'     
  --args[1].value = getAddress(pSkillName) --utf 16 string  
  args[1].value = getAddress('vStringObject')
  print(string.format('String obj ptr: %02X', args[1].value))

  local r = mono_invoke_method(nil, method, address, args)
  print(r)

  print('Done!')

{$asm}
[disable]

Output Lua Engine Window:
Start! 
Method ptr: 184DB8C8 
String obj ptr: 1B80180 
 
Done!
I've tried to pass System.String object reference(literals that game uses), vStringObject is the object in above code, but still it's not working.
What am I missing here?

Also, when I start debugging mono features get de-activated. Is there any way to keep them both running?
 

TheyCallMeTim13

Wiki Monster
Talents
Fearless Donors
Mar 3, 2017
430
48
28
#8
@koderkrazy
Not really sure but maybe you need to use "MONO_TYPE_CHAR" as it is set to "vtString".
CEA:
[enable]
{$lua}
if (syntaxcheck) then return end
  print('Start!')

  local method = mono_findMethod('', 'CharacterSheet', 'GetStat')
  print(string.format('Method ptr: %02X', method))

  local address = readPointer('pCharSheet')

  local args={}
  args[1] = {}
  args[1].type = monoTypeToVartypeLookup[MONO_TYPE_CHAR] -- vtString
  args[1].value='Stealth'
  --print(string.format('String obj ptr: %02X', args[1].value))

  local r = mono_invoke_method(nil, method, address, args)
  print(r)

  print('Done!')

{$asm}
[disable]
 
Top Bottom