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 


Hooked Lua engine, need help on returning script errors?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Jul 24, 2020 1:14 pm    Post subject: Reply with quote

If lua_pcall fails, it returns a non-zero which can be used to determine the error type. You would generally check the Lua stack top for a string after pcall has failed to obtain the error message like this:

Code:

const auto err = lua_pcall(L, 0, LUA_MULTRET, 0);
if (err != 0)
{
    const auto msg = luaL_checkstring(L, -1);
    if (msg != nullptr)
        printf_s(err);
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
whimzala
How do I cheat?
Reputation: 0

Joined: 24 Jul 2020
Posts: 2

PostPosted: Sat Jul 25, 2020 12:12 pm    Post subject: Reply with quote

atom0s wrote:
If lua_pcall fails, it returns a non-zero which can be used to determine the error type. You would generally check the Lua stack top for a string after pcall has failed to obtain the error message like this:

Code:

const auto err = lua_pcall(L, 0, LUA_MULTRET, 0);
if (err != 0)
{
    const auto msg = luaL_checkstring(L, -1);
    if (msg != nullptr)
        printf_s(err);
}


So I got to this:
Code:

mov ecx,[esp+04] // lua state
push 0 // error
push -1 // results
push 0 // args
push ecx // push state
call lua_pcall
add esp,10
    // since eax seems to be either 0 or nonzero compare and call checkstring
cmp eax,0
je short exit
mov ecx,[esp+04]
push eax
push ecx
call "lua51.dll"+6300
add esp,0C


I've noticed after pcall is called that the EAX would be 0 if the script had no errors, but would return a nonzero if there was an error, so knowing this i'm calling checkstring and pushing eax which should be int narg in checkstring

Everything runs fine, no crashes, but i'm still lost on simply returning the string now? or the error in this case.

That's assuming what I have down is correct, and not complete gibberish..

Btw thanks for helping
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Jul 25, 2020 1:24 pm    Post subject: Reply with quote

luaL_checkstring is a wrapper around other Lua API:
Code:

#define luaL_checkstring(L,n)   (luaL_checklstring(L, (n), NULL))

Code:

LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l);


So you would want to call 'luaL_checklstring' with arg being -1 as the stack top index which should have the error string. The return from that will then be a pointer to the string if there was one on the stack top, otherwise, it'll return 0 (null).

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking 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