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 


Traverse MenuItem

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials
View previous topic :: View next topic  
Author Message
panraven
Grandmaster Cheater
Reputation: 26

Joined: 01 Oct 2008
Posts: 652

PostPosted: Wed Nov 19, 2014 2:46 am    Post subject: Traverse MenuItem Reply with quote

The idea started from this thread , which one of the request is to "save the table".

The CE menu actually has a menu command do this: "save the cheat table as last file open", this is the menu item with caption 'Save'.

In my CE, the lua is like this:
Code:
local saveMenuItem = getMainForm().Menu.Items[0][5]
print(saveMenuItem.Caption)
saveMenuItem.doClick()  -- action same as clicking 'File/Save'


The number [0][5] means the 1st MenuItem of MainMenu, then under this menuitem the 6th MenuItem. note: some CE array is index-zero-based.

But number index may not be necessary always the same for all version of ce.

So here the TraverseMenuItem function is made:
Code:
local function packAtLast(i,...) local t = {...} table.insert(t,i) return t end
local function traverseMenuItem(m,f,...)
  local stop
  if select('#',...) > 0 and f then
    stop = f(m,...)
  end
  if stop ~= true then
    for i=0,m.Count-1 do
      local p = packAtLast(i,...)
      stop = traverseMenuItem( m[i], f, unpack(p))
      if stop == true then break end
    end
  end
  return stop
end


The traverse function can be used to build more concrete function, eg dumpMenu and findMenuItemByCaption
Code:
local function dumpMenu(mm)
  traverseMenuItem(mm, function(m,...)
    print('['..table.concat({...},'][')..']  ', m.Caption)
  end)
end

local function findMenuItemByCaption(mm,n)
  local result
  traverseMenuItem(mm,function(m,...)
    if m.Caption == n then
       result = m
       return true  -- send stop == true to traverseMenuItem
    end
  end)
  return result
end


Usage of above 2 function:
Code:
local mm = getMainForm().Menu.Items
dumpMenu(mm)
local save = findMenuItemByCaption(mm, 'Save')
if save then print(save.Caption) end
save.doClick()
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 339

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

PostPosted: Wed Nov 19, 2014 6:10 am    Post subject: Reply with quote

While it has nothing to do with traversing a menu, here's another tip:
Menu items are also accessible using their 'Name' property

In the case of Save, it's name property is miSave
so:
Code:

local save=getMainForm().miSave
save.doClick()


and for those interested, if you like to just get the last save filename(undocumented and could change in the future)
Code:

getMainForm().SaveDialog1.Filename


And of course, if you just wish to save your table using lua you can use saveTable
e.g:
Code:

local filename=getMainForm().SaveDialog1.Filename
if filename~='' then
  saveTable(filename)
end

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Back to top
View user's profile Send private message MSN Messenger
panraven
Grandmaster Cheater
Reputation: 26

Joined: 01 Oct 2008
Posts: 652

PostPosted: Mon Feb 02, 2015 12:08 pm    Post subject: Reply with quote

Add another one which work more like a lua-for-iterator. Also work for memory record and ui component.

Code:
tunpack = table.unpack or unpack
function getCount(r) return r.Count or r.ComponentCount end
function getChild(r,i) return r[i] or r.Component[i] end
indexGet = function(r,i,...) return type(i) ~= 'number' and r or i >= 0 and  i < getCount(r) and indexGet(getChild(r,i),...) or nil end
index2Str = function(...) return '['..table.concat({...},'][')..']' end
traverse = function(root,recursive)
  recursive = recursive ~= false
  local ip = {0}
  return function()
    if #ip > 0 then
      local cm = indexGet(root,tunpack(ip))
      while #ip>0 and cm == nil do
        table.remove(ip)
        if #ip > 0 then
          ip[#ip] = ip[#ip] + 1
          cm = indexGet(root,tunpack(ip))
        end
      end
      if #ip > 0 then
        local pip = {tunpack(ip)}
        if getCount(cm) > 0 and recursive then table.insert(ip,0) else ip[#ip] = ip[#ip] + 1 end
        return cm, pip
      end
    end
  end
end
-- === test
--local mm = getAddressList() --(for memory-record @ AddressList) | getMainForm().Menu.Items  (for main-menu)
--local mm = getMainForm().Menu.Items  --(for main-menu)
local mm = getMainForm()
for m,i in traverse(mm) do print(index2Str(tunpack(i)),' = [',m.Caption or m.Description or m.Name,'] (',m.ClassName or 'No-class-name',')') end
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 Tutorials -> LUA Tutorials 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 cannot download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites