Page 1 of 1
get a static field from a mono class
Posted: Sun Jun 04, 2023 4:47 pm
by ralfonat
Hi there,
I am trying to get a static field value displayed from the Mono ".NET Info" dialog via Lua.
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.
Re: get a static field from a mono class
Posted: Sun Jun 04, 2023 8:15 pm
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.
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()))
Re: get a static field from a mono class
Posted: Mon Jun 05, 2023 7:50 pm
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!
Re: get a static field from a mono class
Posted: Mon Jun 05, 2023 9:10 pm
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