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 


trouble with my lua code or logic inside C# plugin

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Biterider
How do I cheat?
Reputation: 0

Joined: 21 Oct 2016
Posts: 6

PostPosted: Sat Dec 04, 2021 1:24 am    Post subject: trouble with my lua code or logic inside C# plugin Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 183

Joined: 25 Jan 2006
Posts: 8370
Location: 127.0.0.1

PostPosted: Sat Dec 04, 2021 2:29 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Biterider
How do I cheat?
Reputation: 0

Joined: 21 Oct 2016
Posts: 6

PostPosted: Sat Dec 04, 2021 2:43 am    Post subject: Reply with quote

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
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 421

Joined: 09 May 2003
Posts: 24034
Location: The netherlands

PostPosted: Sat Dec 04, 2021 3:13 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Biterider
How do I cheat?
Reputation: 0

Joined: 21 Oct 2016
Posts: 6

PostPosted: Sat Dec 04, 2021 4:29 am    Post subject: Reply with quote

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
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 421

Joined: 09 May 2003
Posts: 24034
Location: The netherlands

PostPosted: Sat Dec 04, 2021 4:55 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Biterider
How do I cheat?
Reputation: 0

Joined: 21 Oct 2016
Posts: 6

PostPosted: Sat Dec 04, 2021 5:05 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting 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