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 


It is possible to run lua during AA script?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
myocytebd2
Cheater
Reputation: 0

Joined: 23 Apr 2015
Posts: 33

PostPosted: Mon Oct 05, 2020 4:15 am    Post subject: It is possible to run lua during AA script? Reply with quote

I'd like to have lua run during AA script execution.
However, it seems that 1st pass of $lua runs before AA script is parsed, while 2nd pass of $lua runs before AA script is actually executed.

Is it possible?
Or the only way is to do everything in Lua and then call autoAssemble? (But then how to handle enable/disable and assign to cheat table?)

For example, this script will end up with exception "Failure determining what sa means". (CE7.1)
(f() can run in Lua console after AA script is enabled.)
Code:
[ENABLE]
{$asm}
alloc(sa, $4096)
registerSymbol(sa)
{$lua}
if syntaxcheck then return end
function f()
  print(string.format("sa=%x", getAddress("sa")))
  writeBytes("sa", 0xaa)
end
print(xpcall(f, debug.traceback))

[DISABLE]
//dealloc(sa)
unregisterSymbol(sa)
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 468

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

PostPosted: Mon Oct 05, 2020 4:20 am    Post subject: Reply with quote

Look at https://forum.cheatengine.org/viewtopic.php?t=615359 for an example of calling lua code from assemblercode
_________________
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
myocytebd2
Cheater
Reputation: 0

Joined: 23 Apr 2015
Posts: 33

PostPosted: Mon Oct 05, 2020 4:38 am    Post subject: Reply with quote

Dark Byte wrote:
Look at ... for an example of calling lua code from assemblercode

Thanks, but I meant during AA is being assembled or after AA is assembled, not during execution of target process.

Current flow seems like:
1. $lua execute 1st time (syntaxcheck=true)
2. AA parse
3. $lua execute 2nd time
4. AA execute -> How to run lua during this?
-> Or how to run lua after AA done, but without manual run lua in console?
(5. AA enabling or disabling completed)
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 468

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

PostPosted: Mon Oct 05, 2020 6:01 am    Post subject: Reply with quote

code inside {$LUA} blocks executes before the AA script is parsed, and the strings the {$lua} blocks return will be put in place where they used to be


That means that the alloc will be done after the script lua script has executed

example:
Code:

alloc(bla,4096)
bla:
nop
nop
{$lua}
return [[nop
nop
labelx:
nop]]
{$asm}
nop
jmp labelx


will allocate memory and write 6 nops there with a jmp to the 5th nop

_________________
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
myocytebd2
Cheater
Reputation: 0

Joined: 23 Apr 2015
Posts: 33

PostPosted: Mon Oct 05, 2020 6:19 am    Post subject: Reply with quote

Dark Byte wrote:
code inside {$LUA} blocks executes before the AA script is parsed, and the strings the {$lua} blocks return will be put in place where they used to be

I see, thanks.

resetLuaState() doesn't seems to work?
In Lua console, I did: (one line each time)
a = 1
print(a)
resetLuaState()
print(a) --> still 1
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 61

Joined: 01 Oct 2008
Posts: 958

PostPosted: Mon Oct 05, 2020 6:33 am    Post subject: Reply with quote

DISCALIMER: The following pretending I known everything about AA execution, but it is just for saving me writing in uncertain tune in some part. Please take it with a grain of salt.

The phase of AA:

1. PRE-AOB AA Prologue, registerAutoAssemblerPrologue(function(script, syntaxcheck), postAOB:boolean=false) with postAOB == false
2. LUA Block execution
3. Standard AA AOBScan Command
4. POST-AOB AA Prologue, postAOB == true
5. AA COMMAND / registerAutoAssemblerCommand (should respect SYNTAXCHECK)
--eg. define / alloc / registersymbol/... (for instance no actual alloc when syntaxcheck==true)
6. AA LINE Assembler / registerAssembler /// may repeat more than one pass
--eg. mov rax,123 / db 11 22 33 / Address:\nSymbol:
7. Write Assembled Code to memory (no writing if SYNTAXCHECK)

there are other processing under the hood , for instance collecting the local symbol etc.

Syntaxcheck is a flag set to true during saving AA script when pressing 'OK' of the AA edit window.
For instance lua function autoAssemble has no such syntaxcheck phase, it is a flag.
Recently added lua function autoAssembleCheck do what autoAssemble do with the syntaxcheck flag set to true.

Moreover, the syntaxcheck flag is only a part of the syntax check process of the AA execution, for instance, if it parse a line with 'UnknownCommand(a,b,c)' that UnknownCommand is not register by registerAutoAssemblerCommand, it will be error.

For your problem
Quote:
4. AA execute -> How to run lua during this?

--4.1 registerAutoAssemblerCommand
--4.2 registerAssembler

Both part can add Lua execution depend the purpose,

The following custom AA Command may see other AA Command result, and print when syntaxcheck or a custom Lua variable inDebug set to true:
Code:

function aa_printcheck(s,sc)
  if sc then print((s:gsub(';;;*','\r\n')))
  elseif inDebug then print((s:gsub(';;;*','\r\n')))end
end

local cmd = 'printcheck'
unregisterAutoAssemblerCommand(cmd)
registerAutoAssemblerCommand(cmd,aa_printcheck)


for instance, in the following AA code:
Code:

define(AVAR,124ABC)
printcheck(AVAR)

the printcheck will see 'AVAR' as '124ABC' (the s in input argument).


Note tho, Label Symbol is determined in 6. AA Line Assembler phase, so AA Custom Command cannot see Label Symbol, it may only use custom assembler instruction defined with registerAssembler.



---
Another custom aa command work like Luacall, but may accept returned string as AA code:
Code:

function aa_exec(s,sc)
  local ok,ret = pcall(load,s,'_',nil,_G)
  if ok and ret then ok,ret = pcall(ret)end
  if not ok then return nil,'exec error:'..s end
  if ret then return tostring(ret) end
end
local cmd = 'exec'
unregisterAutoAssemblerCommand(cmd)
registerAutoAssemblerCommand(cmd,aa_exec)

some note, aa command is one liner, no multi line, and { } symbol for table literal will treat as block comment, text in-between will be removed, just like luacall.


test code:
Code:

print(tostring(autoAssemble[=[
define(axxx,1234)
exec(return print'define(AYYYY,axxx)')
exec(return print[[define(AZZZZ,axxx)]])
luacall(print[[define(AYYYY,axxx)]])
]=]))
-- result:
define(AYYYY,axxx)
define(AZZZZ,1234)
define(1234,1234)
true

from result,
<delete>Luacall execute before AA Command, may be like Lua Block.</delete> not TRUE, it just because using '' instaead of [[...]]
common string quote may not pass AA symbol to custom lua function, may need to use [[ ... ]]

---
So, in the end, Luacall can execute lua code during AA command phase, no custom define need (what a wall of text :p )

_________________
- Retarded.
Back to top
View user's profile Send private message
myocytebd2
Cheater
Reputation: 0

Joined: 23 Apr 2015
Posts: 33

PostPosted: Mon Oct 05, 2020 8:03 am    Post subject: Reply with quote

panraven wrote:
So, in the end, Luacall can execute lua code during AA command phase, no custom define need (what a wall of text :p )

Thanks a lot.

(I want execute lua dynamically because I want previous AA state like symbols etc. are committed)
I also experimented a bit and symbols are indeed not available until registerAssembler.
Apparently it does not worth the trouble to hook assembler and parse the fake instructions in lua.
I would rather do everything in lua and leave an empty AA for enable/disable.
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 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