[Lua] Toggle an entry in a table

Upload *YOUR* gamehacking tools/helpers here
Post Reply
User avatar
LeFiXER
Expert Cheater
Expert Cheater
Posts: 152
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 49

[Lua] Toggle an entry in a table

Post by LeFiXER »

Code: Select all

function getMemRec(s)
   local al = getAddressList()
   local s = al.getMemoryRecordByDescription(tostring(s))
   if s ~= nil then
      return s
   else
      error(showMessage("Couldn't find the script :("))
   end
end

function toggleScript(scr)
   local x = getMemRec(scr)
   if x ~= nil then
      x.Active = not x.Active
      if x.Active == true then
      	 -- Set the enabled colour here, this is a medium-green
         x.Color = 0x00AA00
      else
      	 -- Set the disabled colour here, this is a medium-red
         x.Color = 0x0000AA
      end
   end
end
Usage: in any other script

Code: Select all

[ENABLE]
...
{$LUA}
toggleScript('description of the script you wish to toggle here')
{$ASM}
[DISABLE]
...
{$LUA}
toggleScript('You get the idea')
This can be useful if you want to toggle another, or multiple scripts at the same time.

If this helps you in any way, please show your support by rating this post. Thanks :)

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1713
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 910

Re: [Lua] Toggle an entry in a table

Post by TheyCallMeTim13 »

Not that there was anything wrong with the script, but with these changes you can use the description or ID. Plus I think it makes it a tiny bit more readable, and makes use of the "AddressList" variable CE makes available.

Code: Select all

function getMemRec(id)
   local mr
   if type(id) == 'string' then
      mr = AddressList.getMemoryRecordByDescription(id)
   else
      mr = AddressList.getMemoryRecordByID(id)
   end
   if mr ~= nil then
      return mr
   else
      error(showMessage("Couldn't find the script :("))
   end
end

function toggleScript(id)
   local mr = getMemRec(id)
   if mr ~= nil then
      mr.Active = not mr.Active
      if mr.Active == true then
      	 -- Set the enabled colour here, this is a medium-green
         mr.Color = 0x00AA00
      else
      	 -- Set the disabled colour here, this is a medium-red
         mr.Color = 0x0000AA
      end
   end
end
And this post/topic might be better suited in the tools section, or maybe the tutorials section; I think tools is more fitting though. It would help keep it from just getting buried here. Let me know if you would like it moved.

User avatar
LeFiXER
Expert Cheater
Expert Cheater
Posts: 152
Joined: Wed Mar 24, 2021 9:35 am
Reputation: 49

Re: [Lua] Toggle an entry in a table

Post by LeFiXER »

I would not take offence to any improvement. I'm not a professional programmer by any means, I just like to share what I know and if anyone can improve that then I am all for it. I appreciate that you took the time to look at it and give excellent input/feedback.

If you feel that it would be more fitting in the tools section then by all means feel free to move it.

Thanks!

aSwedishMagyar
Table Makers
Table Makers
Posts: 622
Joined: Mon Jul 06, 2020 3:19 am
Reputation: 966

Re: [Lua] Toggle an entry in a table

Post by aSwedishMagyar »

TheyCallMeTim13 wrote:
Mon Aug 02, 2021 11:03 pm
Not that there was anything wrong with the script, but with these changes you can use the description or ID. Plus I think it makes it a tiny bit more readable, and makes use of the "AddressList" variable CE makes available.

And this post/topic might be better suited in the tools section, or maybe the tutorials section; I think tools is more fitting though. It would help keep it from just getting buried here. Let me know if you would like it moved.
I actually think it would be better to use the getAddressList() to preserve compatibility since I think AddressList is a feature that is only in v7.2+.

Also I like the idea of this going to tools so we can turn it into a repository of helper functions in lua.

Here are some of my contributions:

Simple function for disabling all your scripts at once.

Code: Select all

function disableScripts()
	local addList = getAddressList()
	for i = 0,addList.Count - 1 do 
		if addList[i].Type == vtAutoAssembler and addList[i].Description ~= 'Enable' then addList[i].Active = false end
	end
end
Just returns the Base and Size of a module which you can use in search limits.

Code: Select all

function getModuleLimits(moduleName)
	local modList = enumModules()
    local moduleTemp = nil
    local params = nil
    for i = 1,#modList do
    	if moduleName == modList[i].Name then
        	moduleTemp = modList[i]
        end
    end
	if moduleTemp then
    	params = {moduleTemp.Address,getModuleSize(moduleName)}
    end
    return params
end
Function for getting the address or parameter from an opcode. Useful for getting base addresses within a module or making sure your offsets are always correct when you have an AOB with wildcards.

Code: Select all

function getParamFromOpcode(opAddr)
	local disassembler = createDisassembler()
	disassembler.disassemble(opAddr)
    local disTable = disassembler.getLastDisassembleData()
    local modrm = disTable.modrmValue
    local param = disTable.parameterValue
    if modrm and modrm ~= 0 then return modrm
    elseif param and param ~= 0 then return param end
	return nil
end
Simple timer creation function to keep things organized:

Code: Select all

function createMyTimer(func,params,interval)
	if func == nil then return nil end
	local timer = createTimer()
	timer.Interval = interval
	timer.OnTimer = function() func(params) end
	return timer
end
Functions for quickly adding/removing records or headers to your table:

Code: Select all

function createRecord(base,desc,vType,topRec)
	local addList = getAddressList()
    local newRec = addList.createMemoryRecord()
    newRec.Address = base
    newRec.Description = desc
    newRec.Type = vType
    if topRec then newRec.appendToEntry(topRec) end
end
For the headers keep in mind that the offset list must be in descending order (top to bottom).

Code: Select all

function createHeader(base,desc,offList,topRec)
	local addList = getAddressList()
    local header = addList.createMemoryRecord()
    header.Address = base
    header.OffsetCount = #offList
    for i = 0,#offList - 1 do
        header.Offset[i] = offList[i+1]
    end
    header.Description = desc
    header.IsAddressGroupHeader = true
    header.options = '[moHideChildren]'
    if topRec then header.appendToEntry(topRec) end
    return header
end

Code: Select all

function removeRecords(mainRec)
    if mainRec ~= nil then
	    while mainRec.Count > 0 do
	       mainRec.Child[0]:Delete()
	    end
    end
end

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1713
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 910

Re: [Lua] Toggle an entry in a table

Post by TheyCallMeTim13 »

aSwedishMagyar wrote:
Mon Aug 02, 2021 11:42 pm
...
I actually think it would be better to use the getAddressList() to preserve compatibility since I think AddressList is a feature that is only in v7.2+.

Also I like the idea of this going to tools so we can turn it into a repository of helper functions in lua.
...
I think "AddressList" and "MainForm" was added in 6.3 but not really sure, I know it's in 6.8.3 as I still have that one installed and just checked the celua file. But this is a good point, and I should have mentioned this. So thanks for pointing it out. I have seen plenty on people using older versions of CE, and if they're not using the new features no reason to force them to update for something so trivial. But I have a habit of adding a fix for that to my tables. Just something simple, if nil then set them, at the start of my lua code. But I guess it's a bit of, six in on hand and half a dozen in the other.

And I've been thinking a Lua extensions section would be a good idea, but with the tools section now I'm not really sure. But a Lua functions/extensions topic in tools might not be a bad idea, I don't know something to think about I guess.

Post Reply

Who is online

Users browsing this forum: No registered users