HenryEx Expert Cheater
Reputation: 2
Joined: 18 Dec 2011 Posts: 100
|
Posted: Sun Apr 06, 2025 4:25 am Post subject: Get fields from Mono class with Lua |
|
|
getMonoFields( string: className, string: nameSpace, int: img, boolean: static )
This will fetch you a Lua table of a class' fields. More specifically, it will fetch you a field's offset from the start of the class.
For example, you have a PlayerCharacter class in a game that has various fields like health, shield, stats, stamina etc. If you have the address of this player character structure, you can use this function to find the offset for a field on the fly.
Code: | local pc = [player character address here]
local fields = getMonoFields("PlayerCharacter")
local offsetHP = fields["health"][1]
writeInteger( pc + offsetHP, 9999 ) |
Even if a patch changes the offsets and fields, this will still work since you get the current offsets every time.
The function will return three attributes for a mono field: the offset, the CE vartype (integer, float, string etc.) and the Mono vartype. Since the mono and CE vartypes don't always match up 100%, you can use this info for edge cases.
You can access the table data like this:
Offset = table["fieldname"][1]
CE vartype = table["fieldname"][2]
Mono vartype = table["fieldname"][3]
If you want to call the function frequently, you can speed it up by supplying the mono image of the assembly that the class is in, so CE doesn't have to search all of them. And if you want a table with the class' static fields for some reason, set the 4th argument to true.
How to use or install
Either:
- Create a .lua file in the ..\Cheat Engine\autorun\ folder, then copy & paste the function into it for local use
- Copy & paste the function into the LUA script of a certain table if you want to distribute a table that uses the function.
Code: | --- Function to return a table of fields and their offsets for the specified class name
-- params:
-- className: name of the class to get a structure definition for
-- namespace (OPTIONAL): namespace of the class to get a structure definition for
-- img (OPTIONAL): mono image in which to search for class, speeds up the process
-- static (OPTIONAL): set to true if you want static fields
-- Returns table setup like this:
-- k: 'field name', v: { offset, CE vartype, mono vartype }
-- Get values like: fields["fieldName"][1] for offset
function getMonoFields(className, nameSpace, img, static)
nameSpace = type(nameSpace) == 'string' and nameSpace or ''
img = type(img) == 'number' and img or nil
static = static and true or false
local searchslow = className:find("+") ~= nil
local classId
if img then
if searchslow then
classId = mono_image_findClassSlow(img, nameSpace, className)
else
classId = mono_image_findClass(img, nameSpace, className)
end
else
classId = mono_findClass(nameSpace, className)
end
if classId==nil or classId==0 then return nil end
local fields=mono_class_enumFields(classId,true,true)
local tFields = {}
for i=1, #fields do
if fields[i].isStatic == static and not fields[i].isConst then
local fname = fields[i].name
local foffset = fields[i].offset
local fmonotype = fields[i].monotype
local fvartype
if _G.libmono then -- new functions in CE 7.6+
fvartype = mono_class_isEnum(mono_field_getClass( fields[i].field )) and vtDword or monoTypeToVarType(fmonotype)
else
fvartype = monoTypeToVarType(fmonotype)
end
tFields[fname] = {foffset, fvartype, fmonotype}
end
end
if next(tFields) == nil then tFields = nil end -- nil on empty table
return tFields
end |
edit: fixed a small bug related to static parameter
Last edited by HenryEx on Sat Apr 19, 2025 11:03 am; edited 1 time in total |
|