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 

Some Additional MemoryView Commands

Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials
View previous topic :: View next topic  
Author Message
Newbie cheater
Reputation: 2

Joined: 13 Mar 2013
Posts: 11

PostPosted: Thu Oct 23, 2014 7:52 pm    Post subject: Some Additional MemoryView Commands Reply with quote

I mostly use Cheat Engine when dealing with emulators, so I don't really make very many trainers. As a result, over the last two years or so I've written a number of autorun scripts to extend the actual Cheat Engine UI. Unfortunately, that library has grown a bit large, and with everything being adhoc'ed in at random points, the individual components are way too interdependent.

That all being said, I decided to go ahead and try to extract some of the individual components for posting, starting with a small module to add some additional functionality to the Memory View form.


  Some additional helpers for the memory view form.
 @module memview
 @alias _M
local _M = { form = getMemoryViewForm() }
local on_hexselect = {}
local hexview = _M.form.HexadecimalView
local hexitems = hexview.PopupMenu:getItems()

_M.hex = {
   view = hexview,
   selected = false,
   multiple = false,
   addr1 = nil,
   addr2 = nil,
   onselect = function(handler)
      table.insert(on_hexselect, handler)
      return handler

_M.menus = {}
_M.menus.addItem = function(label, root_or_func, maybe_root)
   local func = root_or_func
   local root = maybe_root or nil
   if root == nil then
      if type(root_or_func) == 'function' then
         error('Must specify a root menu item!')
         root = func
         func = nil
   local mitem = createMenuItem(root)
   menuItem_setCaption(mitem, label)
   if func ~= nil then
      menuItem_onClick(mitem, func)
   menuItem_add(root, mitem)
   return mitem

_M.hex.addItem = function(label, maybe_func)
   return _M.menus.addItem(label, maybe_func, hexitems)

_M.dec2hex = function(nValue, maybe_size)
   local size = maybe_size or 0
   local fmt = '%'
   if type(nValue) == 'string' then nValue = tonumber(nValue) end
   if size ~= 0 then fmt = fmt .. '0' .. tostring(size) end
   fmt = fmt .. 'X'
   return string.format(fmt, nValue)

local miCopyAddr = _M.hex.addItem('Copy Address', function(sender)

local miCopySize = _M.hex.addItem('Copy Selected Size', function(sender)
   writeToClipboard(_M.hex.addr2 - _M.hex.addr1)

miCopySize.Visible = false

local oldOnByteSelect = hexview.OnByteSelect

hexview.OnByteSelect = function(hexadecimalView, address, address2)
   _M.hex.view = hexadecimalView
   _M.hex.addr1 = address
   _M.hex.addr2 = address2
   _M.hex.selected = address > 0
   _M.hex.multiple = _M.hex.selected and (address ~= address2)
   -- Use the original first, if there was any.
   if oldOnByteSelect ~= nil then
      oldOnByteSelect(hexadecimalView, address, address2)
   -- too lazy
   miCopySize.Visible = _M.hex.multiple
   for i,handler in ipairs(on_hexselect) do
      -- address and address2 are contained in the first arg,
      -- but we'll pass them along just to be safe. (though the first
      -- arg is functional incompatible with the original)
      handler(_M.hex, address, address2)

return _M

Currently, I have this nested under an 'lua' subfolder in Cheat Engine's programs folder. Depending on where you put it, here's an example of some startup code for adding your own menu items:


memview = require('memview')

local function genHandler(message)
   return function(sender)
      for k,v in pairs(memview.hex) do
         print(k, '=>', v)

local miNoSel = memview.hex.addItem('Test - No Selection', genHandler('miNoSel'))
local miSingle = memview.hex.addItem('Test - Single Selection', genHandler('miSingle'))
local miMultiple = memview.hex.addItem('Test - Single Selection', genHandler('miMultiple'))

miNoSel.Visible = not memview.hex.selected
miSingle.Visible = not memview.hex.multiple
miMultiple.Visible = memview.hex.multiple

local miHandler = memview.hex.onselect(function(hex, addr1, addr2)
   miNoSel.Visible = not hex.selected
   miSingle.Visible = not hex.multiple
   miMultiple.Visible = hex.multiple

Anyways, if anyone has any trouble, let me know. It's a bit hard to test with my current setup, but from what I can see, I eliminated all of the dependencies.
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