View previous topic :: View next topic |
Author |
Message |
Biterider How do I cheat? Reputation: 0
Joined: 21 Oct 2016 Posts: 6
|
Posted: Sat Dec 04, 2021 1:24 am Post subject: trouble with my lua code or logic inside C# plugin |
|
|
Hi Guys,
Been a very long time since I had to post here for help but I have wasted to many hours now trying to figure this out on my own and googling was not much help. Bit of a long read but trying to give as much detail as possible.
So I recently decided to learn c# and while I was deciding what kind of projects to practice on I noticed the c# plugin dll template inside cheat engine and decided this will be perfect.
I have written a few that do very basic things but I have gotten stuck now on something I am trying to do using lua.
What I am trying to do:
I am trying to use lua to find all the open cheat engine forms, save them to a list. Then hide all those forms, clicking a button should loop through that list and unhide the forms I hid.
At first I had trouble finding only open forms as the getForm would return all forms even if they were not open but I eventually figured out how to return the forms I am looking for.
Now for my current problem, looping through the list, only unhides the first form, not sure why. I have verified that the list contains more than one element.
I am assuming there is a problem with either my lua logic or c# logic as I am very new to both(Python guy). Below is my c# code containing the lua commands.
Code: |
var lua = CESDK.CESDK.currentPlugin.sdk.lua;
if (checkBoxShowHide.Checked == true)
{
labelWindowStatus.ForeColor = System.Drawing.Color.Red;
labelWindowStatus.Text = "Status: All Windows are hidden";
lua.DoString(@"list = createStringlist()
list.Sorted=true
for i = 0, getFormCount() - 1 do
local frm = getForm(i)
if tostring(frm.getVisible())=='true' then
list.add(frm.Caption)
getForm(i).hide()
end
end");
}
|
The above code seems to work perfect, I can see all items are saved in the list. The below code only unhides one form
Code: |
else
{
labelWindowStatus.ForeColor = System.Drawing.Color.Black;
labelWindowStatus.Text = "Status:";
lua.DoString(@"for i = 0,list.Count-1 do
local frm = getForm(i)
getForm(frm).show()
end
list.clear()");
}
|
Any insight will be greatly appreciated.
Thanks.[/code]
|
|
Back to top |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8517 Location: 127.0.0.1
|
Posted: Sat Dec 04, 2021 2:29 am Post subject: |
|
|
Your code is mostly fine, but it will potentially run into some issues given that a forms Caption is not guaranteed to be unique.
You can make two forms with the same caption and run into a collision trying to specifically only re-enable one or the other. Or potentially never re-enable both.
Another means you could do is abuse Lua's userdata type and store the actual address of the objects. This requires a little bit of a hack since the given userdata when calling into Lua is only a temporary. So each time you call, for example 'getForm' the return will be different even for the same object. Instead, you can read into the userdata object and pull the true pointer out like this:
Code: |
local function hideForms()
local ret = {};
local cnt = getFormCount();
for x = 0, cnt - 1 do
local frm = getForm(x);
if (frm ~= nil and frm.getVisible()) then
-- Hide the form..
frm.hide();
-- Hack: Get the real userdata object pointer..
local addr = readIntegerLocal(tonumber(tostring(frm):sub(11), 16));
-- Store the address for our return to re-enable hidden forms..
table.insert(ret, addr);
end
end
return ret;
end
|
This also bypasses the need to make and use a string table. This just uses a normal Lua table for holding the hidden form addresses the function processed.
Then to unhide them with this setup, you can do this:
Code: |
local function showForms(t)
local function hasval(t, val)
for _, v in pairs(t) do
if (v == val) then
return true;
end
end
return false;
end
local cnt = getFormCount();
for x = 0, cnt - 1 do
local frm = getForm(x);
if (frm ~= nil) then
local addr = readIntegerLocal(tonumber(tostring(frm):sub(11), 16));
if (hasval(t, addr)) then
frm.show();
end
end
end
end
|
Then using them would be like this:
Code: |
-- Hide visible forms..
local f = hideForms();
-- Make forms visible again..
showForms(f);
|
_________________
- Retired. |
|
Back to top |
|
|
Biterider How do I cheat? Reputation: 0
Joined: 21 Oct 2016 Posts: 6
|
Posted: Sat Dec 04, 2021 2:43 am Post subject: |
|
|
atom0s wrote: | Your code is mostly fine, but it will potentially run into some issues given that a forms Caption is not guaranteed to be unique.
You can make two forms with the same caption and run into a collision trying to specifically only re-enable one or the other. Or potentially never re-enable both.
Another means you could do is abuse Lua's userdata type and store the actual address of the objects. This requires a little bit of a hack since the given userdata when calling into Lua is only a temporary. So each time you call, for example 'getForm' the return will be different even for the same object. Instead, you can read into the userdata object and pull the true pointer out like this:
|
Thank you very much atom0s. I will give this a try and report back.
Appreciate the detailed code and explanation!
|
|
Back to top |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25287 Location: The netherlands
|
Posted: Sat Dec 04, 2021 3:13 am Post subject: |
|
|
also note that getForm()'s index is based on the z-order of the windows (getForm(0) is always the top one)
What I suggest is first create a table to hold all the forms, and then go through that list to hide them all
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
|
Biterider How do I cheat? Reputation: 0
Joined: 21 Oct 2016 Posts: 6
|
Posted: Sat Dec 04, 2021 4:29 am Post subject: |
|
|
Ok, the code works if I run it directly from lua in cheat engine. But from c# I am getting the below error.
I suspect the data must be getting cleared or not saved.
I am not allowed to post images as it contains link. So here is the error message I am getting.
"PCall failed with error 2 ([string "local f = hideForms();"]:1: attempt to call a nil value (global 'hideForms'))"
|
|
Back to top |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25287 Location: The netherlands
|
Posted: Sat Dec 04, 2021 4:55 am Post subject: |
|
|
remove the local part from the function declaration
local only functions within the current script, you're on a new script
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
|
Biterider How do I cheat? Reputation: 0
Joined: 21 Oct 2016 Posts: 6
|
Posted: Sat Dec 04, 2021 5:05 am Post subject: |
|
|
Dark Byte wrote: | remove the local part from the function declaration
local only functions within the current script, you're on a new script |
Awesome, that did the trick. Thank You Dark Byte and thank you atom0s.
My c# WindowsForm is returning the correct results now.
|
|
Back to top |
|
|
|