HenryEx icon

getMonoFields

HenryEx | PRO | 08/18/26 12:08:04 PM UTC | 0 ⭐ | 3221 👁️ | Never ⏰ | []
Lua |

2.66 KB

|

Source Code

|

0 👍

/

0 👎

-- 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
 
-- ORIGINAL FUNCTION:
--function to return a table of fields and their offsets for the specified class name
--params:
--    namespace: namespace of the class to get a structure definition for
--    className: name of the class to get a structure definition for
-- Get values like: fields["fieldName"]
-- from: https://www.cheatengine.org/forum/viewtopic.php?t=608935
--function getMonoFields(namespace, className)
--   local classId = mono_findClass(namespace, className)
--   local struct = monoform_exportStructInternal(createStructure(''), classId,
--      true --[[recursive]], false --[[not static]], {'some struct map thing'}, false --[[not global]])
--
--   local fields = {}
--   for i=0, struct.Count-1 do
--      local e = struct.getElement(i)
--      fields[e.name] = e.offset
--   end
--   return fields
--end
 

Comments