Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Get fields from Mono class with Lua

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions
View previous topic :: View next topic  
Author Message
HenryEx
Expert Cheater
Reputation: 2

Joined: 18 Dec 2011
Posts: 100

PostPosted: Sun Apr 06, 2025 4:25 am    Post subject: Get fields from Mono class with Lua Reply with quote

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:

  1. Create a .lua file in the ..\Cheat Engine\autorun\ folder, then copy & paste the function into it for local use
  2. 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
Back to top
View user's profile Send private message
Csimbi
I post too much
Reputation: 97

Joined: 14 Jul 2007
Posts: 3305

PostPosted: Tue Apr 08, 2025 12:22 pm    Post subject: Reply with quote

Pretty cool idea.
Although I doubt I will be using it because I dislike having mono enabled in prod environment.
Thanks for posting!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites