How to Invoke a Method?

Memory scanning, code injection, debugger internals and other gamemodding related discussion
Post Reply
gideon25
Expert Cheater
Expert Cheater
Posts: 122
Joined: Mon Mar 20, 2017 1:42 am
Reputation: 37

How to Invoke a Method?

Post by gideon25 » Sun Jul 08, 2018 11:21 pm

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 [URL='https://wiki.cheatengine.org/index.php?title=Mono:Lua:mono_invoke_method&action=edit&redlink=1']mono_invoke_method[/URL] 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!

jgoemat
Table Makers
Table Makers
Posts: 19
Joined: Fri Jul 21, 2017 6:47 pm
Reputation: 2

How to Invoke a Method?

Post by jgoemat » Mon Jul 09, 2018 6:33 am

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=lua]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[/CODE]



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=cea][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]

[/code]
Last edited by Anonymous on Wed Jul 25, 2018 1:32 am, edited 3 times in total.

gideon25
Expert Cheater
Expert Cheater
Posts: 122
Joined: Mon Mar 20, 2017 1:42 am
Reputation: 37

How to Invoke a Method?

Post by gideon25 » Mon Jul 09, 2018 4:03 pm

Great thanks! I did it like this and it worked:





[CODE=cea][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]

[/CODE]



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:

[IMG]https://i.imgur.com/Rs2YB7u.jpg[/IMG]

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=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 hp = mono_invoke_method(domain, method, address, {})

{$asm}



[disable][/CODE]



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

[IMG]https://i.imgur.com/EY18JWJ.jpg[/IMG]



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!
Last edited by Anonymous on Wed Jul 25, 2018 1:32 am, edited 1 time in total.

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1178
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 232
Contact:

How to Invoke a Method?

Post by TheyCallMeTim13 » Fri Jul 13, 2018 4:39 am

Looking at the "monoscript.lua" file, it looks like the [I]args[/I] is a table with [B]type[/B] and [B]value[/B] keys/values.

Try the [B]mono_invoke_method_dialog[/B] if you are just testing stuff.



CE folder/autorun/monoscript.lua

line 1659

[CODE=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[/CODE]



line 1832

[CODE=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[/CODE]



line 1891

[CODE=lua]function mono_invoke_method(domain, method, object, args)[/CODE]

gideon25
Expert Cheater
Expert Cheater
Posts: 122
Joined: Mon Mar 20, 2017 1:42 am
Reputation: 37

How to Invoke a Method?

Post by gideon25 » Sun Jul 15, 2018 9:08 am

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][/CODE]



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!

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1178
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 232
Contact:

How to Invoke a Method?

Post by TheyCallMeTim13 » Sun Jul 15, 2018 11:26 am

From what I can tell you just need to set the [I]type[/I] and [I]value[/I] for each argument; The "set_AllTrials" only has 1 argument so something like this should work.

[CODE=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][/CODE]
Last edited by TheyCallMeTim13 on Thu Jan 01, 1970 12:00 am, edited 1 time in total.

User avatar
koderkrazy
Fearless Donors
Fearless Donors
Posts: 252
Joined: Sun Jun 17, 2018 2:14 pm
Reputation: 165

How to Invoke a Method?

Post by koderkrazy » Mon Jul 16, 2018 11:12 pm

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 [URL]https://fearlessrevolution.com/threads/hero-u-rogue-to-redemption-time-player-stats-god-mode.7417/[/URL]



[USER=91]@TheyCallMeTim13[/USER] I've managed to call most of the methods on the stat object except one:

[CODE=csharp] public Stat GetStat(string name)

{

return (Stat)(((object)Skills[name]) ?? ((object)Attributes[name]));

}[/CODE]



My Lua code:

[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!



[/CODE]

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?

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1178
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 232
Contact:

How to Invoke a Method?

Post by TheyCallMeTim13 » Tue Jul 17, 2018 2:00 am

[USER=18664]@koderkrazy[/USER]

Not really sure but maybe you need to use "MONO_TYPE_CHAR" as it is set to "vtString".

[CODE=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][/CODE]
Last edited by TheyCallMeTim13 on Thu Jan 01, 1970 12:00 am, edited 1 time in total.

User avatar
koderkrazy
Fearless Donors
Fearless Donors
Posts: 252
Joined: Sun Jun 17, 2018 2:14 pm
Reputation: 165

How to Invoke a Method?

Post by koderkrazy » Tue Jul 17, 2018 2:32 pm

MONO_TYPE_CHAR worked.

Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users