get a static field from a mono class

Want Cheat Engine to do something specific and no idea how to do that, ask here. (From simple scripts to full trainers and extensions)
Post Reply
ralfonat
Noobzor
Noobzor
Posts: 11
Joined: Thu Nov 03, 2022 10:41 am
Reputation: 1

get a static field from a mono class

Post by ralfonat »

Hi there,

I am trying to get a static field value displayed from the Mono ".NET Info" dialog via Lua.

Image

I have come up with this code:

Code: Select all

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

local domainId = mono_enumDomains()[1]
print('domainId = ' .. domainId)
local classId = mono_findClass('', 'CombatManager')
print('classId = ' .. classId)
local methodId = mono_class_findMethod(classId, 'SceneStart')
print('methodId = ' .. methodId)
methodId = mono_findMethod('', 'CombatManager', 'SceneStart')
print('methodId = ' .. methodId)

--print(dump(mono_class_enumFields(classId)))
--print(dump(mono_enumDomains()))
--print()
print(mono_class_getStaticFieldAddress(domainId, classId))
and this is the output

Code: Select all

domainId = 2248894136256 
classId = 2248888102816 
methodId = 2250105680784 
methodId = 2250105680784 
0 
So the mono_class_getStaticFieldAddress call returns 0....

What can I do? Am I on the wrong path?

Many thanks in advance.

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 488
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 250

Re: get a static field from a mono class

Post by LeFiXER »

ralfonat wrote:
Sun Jun 04, 2023 4:47 pm
Hi there,

I am trying to get a static field value displayed from the Mono ".NET Info" dialog via Lua.

Image

I have come up with this code:

Code: Select all

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

local domainId = mono_enumDomains()[1]
print('domainId = ' .. domainId)
local classId = mono_findClass('', 'CombatManager')
print('classId = ' .. classId)
local methodId = mono_class_findMethod(classId, 'SceneStart')
print('methodId = ' .. methodId)
methodId = mono_findMethod('', 'CombatManager', 'SceneStart')
print('methodId = ' .. methodId)

--print(dump(mono_class_enumFields(classId)))
--print(dump(mono_enumDomains()))
--print()
print(mono_class_getStaticFieldAddress(domainId, classId))
and this is the output

Code: Select all

domainId = 2248894136256 
classId = 2248888102816 
methodId = 2250105680784 
methodId = 2250105680784 
0 
So the mono_class_getStaticFieldAddress call returns 0....

What can I do? Am I on the wrong path?

Many thanks in advance.
You should be able to do that with this:

Code: Select all

local function getCombatManager()
		local c = mono_findClass('Assembly-CSharp.dll', 'CombatManager')
		local f = mono_class_enumFields(c, true)
		for i in pairs(f) do
			if f[i].isStatic and f[i].Name == 'instance' then
				local v = mono_class_getStaticFieldValue(c, f[i].field)
				return v
			end
		end
	  end

print(string.format('%X', getCombatManager()))

ralfonat
Noobzor
Noobzor
Posts: 11
Joined: Thu Nov 03, 2022 10:41 am
Reputation: 1

Re: get a static field from a mono class

Post by ralfonat »

Thank you so much for nudging in the right direction!

So the change that made stuff work was the true param in mono_class_enumFields which is "includeParents". That seems to make the static field appear.

Since your code didnt work, here is the small correction. As the field "name" is case sensitive, this is what made it work. (fields.name instead of fields.Name)

Code: Select all

local classId = mono_findClass('Assembly-CSharp.dll', 'CombatManager')
print(classId)
local fields = mono_class_enumFields(classId, true)
for i in pairs(fields) do
  if fields[i].isStatic and fields[i].name == 'instance' then
    local instance = mono_class_getStaticFieldValue(classId, fields[i].field)
    print(instance)

    local methodId = mono_class_findMethod(classId, 'SceneStart')
    print(methodId)

    mono_invoke_method('Assembly-CSharp.dll', methodId, instance, {})
  end
end
Again thank you so much for the quick help!

User avatar
LeFiXER
LeFixer
LeFixer
Posts: 488
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 250

Re: get a static field from a mono class

Post by LeFiXER »

ralfonat wrote:
Mon Jun 05, 2023 7:50 pm
Thank you so much for nudging in the right direction!

So the change that made stuff work was the true param in mono_class_enumFields which is "includeParents". That seems to make the static field appear.

Since your code didnt work, here is the small correction. As the field "name" is case sensitive, this is what made it work. (fields.name instead of fields.Name)

Code: Select all

local classId = mono_findClass('Assembly-CSharp.dll', 'CombatManager')
print(classId)
local fields = mono_class_enumFields(classId, true)
for i in pairs(fields) do
  if fields[i].isStatic and fields[i].name == 'instance' then
    local instance = mono_class_getStaticFieldValue(classId, fields[i].field)
    print(instance)

    local methodId = mono_class_findMethod(classId, 'SceneStart')
    print(methodId)

    mono_invoke_method('Assembly-CSharp.dll', methodId, instance, {})
  end
end
Again thank you so much for the quick help!
You're welcome :). Ah yes, my mistake. Force of habit with typing in camelCase :oops:

User avatar
Shaggalicious
Expert Cheater
Expert Cheater
Posts: 89
Joined: Thu Feb 29, 2024 7:35 pm
Reputation: 85

Re: get a static field from a mono class

Post by Shaggalicious »

ralfonat wrote:
Mon Jun 05, 2023 7:50 pm
LeFiXER wrote:
Sun Jun 04, 2023 8:15 pm
hey sorry to quote you out of the blue but since you're the ones who've written these scripts i hope you may have an answer, it seems to work for me in other games but, in this game i get a response that doesnt match the value that's shown in my .net window. would you or anyone here have an idea on why this happens and how to fix it?

Image

i'v tried reading it as a pointer, doesn't work, i've tried renaming .field to .value and even .address doesn't produce the same address as shown in the window

Post Reply

Who is online

Users browsing this forum: No registered users