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 


Converting a float script to work with big endian

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
TheKrzysiek
How do I cheat?
Reputation: 0

Joined: 29 Oct 2024
Posts: 2

PostPosted: Tue Oct 29, 2024 5:23 am    Post subject: Converting a float script to work with big endian Reply with quote

I've been making simple scripts that change the FOV in racing games based on the speed.
Recently I've been trying to do it on Xenia and RPCS3 but I've had issues with the script actually working properly, and after eliminating all other possible issues, I assumed that it's due to the Float Big Endian that those emulators use.
I've tried some fixes and ChatGPT, but it didn't do anything good and my own knowledge of this is pretty limited.

Here's the script:

Code:
local MINSPEED_value = 50
local SPEED_CAP_Value = 250
local DEFAULTFOV_value = 333
local SPEEDMULTIPLIER_value = 1.0

local FOV_address = 0x201A10044
local SPEED_address = 0x205371378

function updateValues()
    local SPEED_value = readFloat(SPEED_address)

    if SPEED_value > MINSPEED_value and SPEED_value < SPEED_CAP_Value then
        local FOV_value = (DEFAULTFOV_value + (MINSPEED_value * SPEEDMULTIPLIER_value) - SPEED_value * SPEEDMULTIPLIER_value)
        writeFloat(FOV_address, FOV_value)
    end
end


Any help is much appreciated.
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 59

Joined: 01 Oct 2008
Posts: 957

PostPosted: Tue Oct 29, 2024 9:17 am    Post subject: This post has 1 review(s) Reply with quote

IIRC, CE use Lua 5.3 since ver 6.5 and Lua 5.3 has string.pack string.unpack functions to convert different value type to and from a string, including big-endian numeric type.

We can apply the conversion on values we read from or write to memory.
Here an example :
Code:

function mem_struct(fms)
  local ok, sz, read, write = pcall(string.packsize,fms)
  if ok then
    function read(addr)
      local bs = readBytes(addr, sz, true)
      if bs then
        return string.unpack(fms, byteTableToString(bs))
      end
    end
    function write(addr, ...)
      local ok, bs = pcall(string.pack, fms, ...)
      if ok then
        local write_ok = writeBytes(addr, stringToByteTable(bs))
        return write_ok
      end
    end
    return { read = read, write = write }
  end
end

Now we can replace readFloat/writeFloat with this function, yours as example (not tested),
Code:

function updateValues(isBig) -- add a flag if use big-endian
    local mx, readFloat, writeFloat = mem_struct(isBig and '>f' or 'f')
    if not mx then return else readFloat, writeFloat = mx.read, mx.write end -- use the functions
-- your original code below
    local SPEED_value = readFloat(SPEED_address)

    if SPEED_value > MINSPEED_value and SPEED_value < SPEED_CAP_Value then
        local FOV_value = (DEFAULTFOV_value + (MINSPEED_value * SPEEDMULTIPLIER_value) - SPEED_value * SPEEDMULTIPLIER_value)
        writeFloat(FOV_address, FOV_value)
    end
end

Check the string.pack/unpack format from Lua 5.3 Manual
https://www.lua.org/manual/5.3/manual.html#6.4.2

_________________
- Retarded.
Back to top
View user's profile Send private message
TheKrzysiek
How do I cheat?
Reputation: 0

Joined: 29 Oct 2024
Posts: 2

PostPosted: Tue Oct 29, 2024 1:02 pm    Post subject: Reply with quote

It works now, thanks a lot!
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 Lua Scripting 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