Lua:registerAutoAssemblerCommand

From Cheat Engine
Jump to navigation Jump to search

<> Lua API Reference

function registerAutoAssemblerCommand(command, function) : void

Registers a custom Auto Assembler command.

When the Auto Assembler encounters the registered command, it calls the specified Lua function. The command line is then replaced by the string returned by that function.

The callback can be called twice: once during syntax check and symbol lookup, and once during actual assembly execution if the command has not been removed during the first phase.

Function Parameters[edit]

Parameter Type Description
command String The Auto Assembler command name to register.
function Function The Lua callback function that is called when the command is encountered.

Callback Function[edit]

<> Lua API Reference

function callback(parameters, syntaxcheckonly) : string

The callback receives the command parameters and a flag that indicates whether the Auto Assembler is currently doing a syntax check.

Callback Parameters[edit]

Parameter Type Description
parameters String The text passed to the custom Auto Assembler command.
syntaxcheckonly Boolean True when the callback is called during syntax check and symbol lookup. False when it is called during actual assembly execution.

Callback Returns[edit]

Return Value Result Description
String Replace command Replaces the command line in the Auto Assembler script with the returned string.
Multiline string Replace command with multiple lines Replaces the command line with multiple Auto Assembler lines.
nil, String Raise error Fails the Auto Assembler script with the given error message.

Returns[edit]

This function does not return a documented value.

Examples[edit]

 1 local t = translate
 2 local LineEnd = '\r\n'
 3 local ExitOnSyntaxCheck = false
 4 
 5 local function registerLabel(parameters, syntaxcheck)
 6     ---- registerLabel(name)
 7     if ExitOnSyntaxCheck and syntaxcheck then return end
 8     local le = LineEnd
 9     local name = parameters
10     if name == nil then
11         return nil, t('Wrong number of parameters, no "name", for "registerLabel" custom AA command.')
12     end
13     local str = 'label(' .. name .. ')' .. le
14     str = str .. 'registerSymbol(' .. name .. ')'
15     return str
16 end
17 
18 local function unregisterLabel(parameters, syntaxcheck)
19     ---- unregisterLabel(name)
20     if ExitOnSyntaxCheck and syntaxcheck then return end
21     local name = parameters
22     if name == nil then
23         return nil, t('Wrong number of parameters, no "name", for "unregisterLabel" custom AA command.')
24     end
25     local str = 'unregisterSymbol(' .. name .. ')'
26     return str
27 end
28 
29 registerAutoAssemblerCommand('registerLabel', registerLabel)  ---- Register the command
30 registerAutoAssemblerCommand('unregisterLabel', unregisterLabel)  ---- Register the command
31 
32 ---- Usage in an Auto Assembler script
33 ----------------------------------------------------
34 -- alloc(memTestMemory, 0x400)
35 -- registerSymbol(memTestMemory)
36 -- registerLabel(memTestMemoryEND)
37 -- memTestMemory:
38 --   db 90 90 90 90
39 --   memTestMemoryEND:
40 ----------------------------------------------------
 1 local t = translate
 2 local LineEnd = '\r\n'
 3 local ExitOnSyntaxCheck = false
 4 
 5 local function split(s, delimiter)
 6     result = {}
 7     for match in (s .. delimiter):gmatch('(.-)' .. delimiter) do
 8         table.insert(result, match)
 9     end
10     return result
11 end
12 
13 
14 function repeteBytes(parameters, syntaxcheck)
15     ---- repeteBytes(numberOfByte, arrayOfBytes)
16     if ExitOnSyntaxCheck and syntaxcheck then return end
17     local le = LineEnd
18     local args = split(parameters, ',')
19     local numberOfBytes = args[1]
20     if numberOfBytes == nil then
21         return nil, t('Wrong number of parameters, no "numberOfBytes", for "repeteBytes" custom AA command.')
22     end
23     local arrayOfBytes = args[2]
24     if arrayOfBytes == nil then
25         return nil, t('Wrong number of parameters, no "arrayOfBytes", for "repeteBytes" custom AA command.')
26     end
27     arrayOfBytes = arrayOfBytes:upper()
28     if arrayOfBytes:find('#') or arrayOfBytes:find('(INT)') 
29     or arrayOfBytes:find('(FLOAT)') or arrayOfBytes:find('(DOUBLE)') then
30         return nil, t('Wrong format of parameters, "arrayOfBytes" must be in hexadecimal format, for "repeteBytes" custom AA command.')
31     end
32     local bc = (#arrayOfBytes) / 2
33     numberOfBytes = numberOfBytes:upper():gsub('&nbsp;', &#39;&#39;)
34     if numberOfBytes:sub(1, 5) == '(INT)' then
35         numberOfBytes = tonumber(numberOfBytes:sub(6))
36     elseif numberOfBytes:sub(1, 1) == '#' then
37         numberOfBytes = tonumber(numberOfBytes:sub(2))
38     elseif numberOfBytes:sub(1, 1) == '$' then
39         numberOfBytes = tonumber(numberOfBytes:sub(2), 16)
40     elseif numberOfBytes:sub(1, 2) == '0X' then
41         numberOfBytes = tonumber(numberOfBytes:sub(3), 16)
42     else
43         numberOfBytes = tonumber(numberOfBytes, 16)
44     end
45     if type(numberOfBytes) ~= 'number' then
46         return nil, t('Could not parse "numberOfBytes" for "repeteBytes" custom AA command.')
47     end
48     local str = &#39;&#39;
49     for i = 1, numberOfBytes / bc do
50         str = str .. 'db ' .. arrayOfBytes .. le
51     end
52     return str
53 end
54 
55 registerAutoAssemblerCommand('repeteBytes', repeteBytes)  ---- Register the command
56 
57 ---- Usage in an Auto Assembler script
58 ----------------------------------------------------
59 -- alloc(memTestMemory, 0x400)
60 -- registerSymbol(memTestMemory)
61 -- memTestMemory:
62 --  repeteBytes(0x64, 90 52)
63 ----------------------------------------------------
64 
65 ---- Memory View Form copy of "memTestMemory"
66 ----------------------------------------------------
67 -- 0032FFFE -                       - ?? 
68 -- 0032FFFF -                       - ?? 
69 -- memTestMemory- 90                - nop 
70 -- 00330001 - 52                    - push edx
71 -- 00330002 - 90                    - nop 
72 -- 00330003 - 52                    - push edx
73 -- 00330004 - 90                    - nop 
74 -- ...
75 -- 00330060 - 90                    - nop 
76 -- 00330061 - 52                    - push edx
77 -- 00330062 - 90                    - nop 
78 -- 00330063 - 52                    - push edx
79 -- 00330064 - 00 00                 - add [eax],al
80 ----------------------------------------------------

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Assembler and Disassembler Related Lua Functions

Auto assembler, disassembly helpers, instruction size lookup, and assembler callbacks