Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Maega

Using hex in Lua scripts

This topic is 5753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I wrote a function in C that takes a DWORD parameter that is hexadecimal (read prefixed with 0x) Well, Lua dies when I include that function in the script. My other C functions work fine, but anything that uses hex just dies out. Any suggestions? The code is fine (I typecast the double returned by tonumber to a DWORD (perhaps that could be it..) ) If that is the problem, could someone explain to me or show me an example on how to add a user defined type to fix this problem.

Share this post


Link to post
Share on other sites
Advertisement
Well, a DWORD is a 32-bit int. So can you just convert your hex numbers to ints? Then you can easily type it with lua''s doubles ( i think they are doubles... ) Post some code if i didnt quite understand

Brian

Share this post


Link to post
Share on other sites
Oops.. just noticed I didn''t include why the numbers are hex

They are memory addresses. I am unsure that you could convert them to unsigned longs and type cast them correctly.

Share this post


Link to post
Share on other sites
So you''re exposing the function to Lua? Post the C function here, I think I can help.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
Ok..

Small C function.. =P

void WriteByte(DWORD address, BYTE value)
{
WriteProcessMemory(processhandle, (void *)address, (void *)&value, 1, NULL);
}


glue function looks like this

int l_WriteByte(lua_State* luaVM)
{
DWORD address = 0;
BYTE value = 0;


address = (DWORD)lua_tonumber(luaVM, 1);
value = (BYTE)lua_tonumber(luaVM, 2);

theScriptWindow->WriteByte(address, value);

return 1;
}

Share this post


Link to post
Share on other sites
hexidecimal is just another way to represent a number, just like binary or the "human-readable" way decimal. "0xD431" is exactly the same number as "54321" in decimal or "1101010000110001" in binary, when you type it in your code the computer sees it as binary anyway.
i don''t know lua but you can convert the numbers into decimal or whatever lua understands, and it will mean the same thing to the computer.

Share this post


Link to post
Share on other sites
The internal representation of numbers in Lua is as doubles. A double does not have enough precision to store an unsigned 32-bit integer. And anyway, that''s not what it''s for.

Pointers in Lua should be handled with userdatas, not numbers. If you''re using Lua 4, you can create userdatas with arbitrary pointers, so just pass in the pointer to lua_pushuserdata. In Lua 5, you could use light-userdatas to accomplish this, but a more robust solution (light userdatas are never supposed to be exposed to scripts) would be to put the pointer in a memory location pointed to by a userdata. Look at the macros lua_boxpointer and lua_unboxpointer for details.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
Pointers in lua I never figured out..

doesn''t really get explained enough for me to understand in the manual :/

Share this post


Link to post
Share on other sites
Basically, a "userdata" is a piece of memory that your program can use, but that is maintained by Lua. On the C side, you can obtain a pointer to this memory, if you need to read/write it. What boxpointer does is create a userdata that refers to a 4-byte area of memory, and then stores the pointer in that memory. So once you have the pointer to the userdata, you dereference it (using unboxpointer) to get the original pointer you stored.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
I''ll look into tomorrow.. my brain is popping and fizzling too much to grasp what you mean haha

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!