Page 1 of 1

Memory Editing & etc lua scripts

Posted: Sun Jan 02, 2022 10:44 am
by Frouk
This is collections of functions that will help you with memory editing and other
btw this post will keep updating (since i've opened this thread,i won't dump this)
Memory editing(byte inject)

Code: Select all

function MakeRangedNop(addr,addrTo)
    addr = getAddress(addr)
    addrTo = getAddress(addrTo)
    local size
    if addr == nil then
        return
    end
    if addrTo == nil then
        size = getInstructionSize(addr)
    end
    size = -(addr - addrTo)
    MakeNop(addr,size)
end

function MakeNop(addr,size)
    addr = getAddress(addr)
    size = tonumber(size)
    if addr == nil then
        return
    end
    if size == 0 then
        return
    end
    if size == nil then
        size = getInstructionSize(addr)
    end
    size = size - 1
    for i = 0,size do
        writeBytes(addr+i,0x90)
    end
end
Value types

Code: Select all

function readBoolean(addr)
    local result
    if addr == nil then
        return nil
    end
    result = readBytes(addr) == 1
    if readBytes(addr) > 1 then
        result = nil
    end
    return result
end

function writeBoolean(addr,bool)
    local result
    if addr == nil or bool == nil then
        return
    end
    result = bool and 1 or 0
    writeBytes(addr,result)
end

function rewriteString(addr,text,wideChar)
    if addr == nil then
        error("Address wasn't setted!")
        return
    end
    if text == nil then
        error("String wasn't aspected!")
        return
    end
    if wideChar == nil then
        wideChar = false
    end
    addr = getAddress(addr)
    if not addr then return end
    local size = #readString(addr)
    if wideChar then
        local size = size * 2
        for i = 0,size do
            writeBytes(addr+i,{0x00,0x00})
        end
    else
        for i = 0,size do
            writeBytes(addr+i,0)
        end
    end
    writeString(addr,text,wideChar)
end

Re: Memory Editing & etc lua scripts

Posted: Sun Jan 02, 2022 11:22 am
by STN
Frouk wrote:
Sun Jan 02, 2022 10:44 am
..
Didn't belong in tools section, moved to appropriate forum.

Re: Memory Editing & etc lua scripts

Posted: Sun Jan 02, 2022 12:52 pm
by ShyTwig16
The "rewriteString" won't work for wide character or unicode strings. For a zero terminated string all you need is a zero at the end. And doing a memory write for every byte can be a bit slow you'd be better off making a table of bytes then writing to memory once using the byte table. Something like this is all you really need for a zero terminated string.

Code: Select all

---- 
---- Writes a zero terminated string to the given address.
---- 
---- writeStringZT(address, text)
---- writeStringZT(address, text, wideChar)
---- 
---- Parameters:
---- 	address : number - string : 
---- 		The address to write to.
---- 	text : string : 
---- 		The string to write to memory.
---- 	wideChar (optional): boolean : 
---- 		Set to true for wide character strings.
function writeStringZT(address, text, wideChar)
	address = getAddressSafe(address)
	if not address then return end
	writeString(address, text, wideChar)
	local len = #text
	if wideChar then len = len * 2 end
	writeBytes(address + len, { 0x00, 0x00 })
end
registerLuaFunctionHighlight('writeStringZT')
And for local strings:

Code: Select all

---- 
---- Writes a zero terminated string to the given address targeting the CE process.
---- 
---- writeStringLocalZT(address, text)
---- writeStringLocalZT(address, text, wideChar)
---- 
---- Parameters:
---- 	address : number - string : 
---- 		The address to write to.
---- 	text : string : 
---- 		The string to write to memory.
---- 	wideChar (optional): boolean : 
---- 		Set to true for wide character strings.
function writeStringLocalZT(address, text, wideChar)
	address = getAddressSafe(address, true)
	if not address then return end
	writeStringLocal(address, text, wideChar)
	local len = #text
	if wideChar then len = len * 2 end
	writeBytesLocal(address + len, { 0x00, 0x00 })
end
registerLuaFunctionHighlight('writeStringLocalZT')
EDIT:
And the "PrintRangedBytes" seems like Ctrl+Alt+C in the memory view form does what you need. And you might at least want to use a 2 digit format for the printed bytes, and even maybe build the string then print. And a read for every byte will be slow again, instead you can just use something like readBytes(addr, size). Right now it outputs something like this:

Code: Select all

48 
8D 
D 
BF 
B4 
1A 
0 
Where as Ctrl+Alt+C will give you something like this:

Code: Select all

48 8D 0D BF B4 1A 00
EDIT 2:
And it took me forever to figure this out but when you throw an error you can give it a stack index to throw the error at the caller's location instead of inside your function. e.g.:

Code: Select all

local function errorTest(somePara)
      if not somePara then
         error('no parameter given')
      end
end
errorTest()
This will output an error message like this, note the line number:

Code: Select all

Error:[string "local function errorTest1(somePara)
..."]:3: no parameter given
Script Error
But with the stack index:

Code: Select all

local function errorTest(somePara)
      if not somePara then
         error('no parameter given', 2)
      end
end
errorTest()
It will output an error message like this, again note the line number:

Code: Select all

Error:[string "local function errorTest(somePara)
..."]:6: no parameter given
Script Error
If you're calling the function a lot this will help find where the error actually is.

Re: Memory Editing & etc lua scripts

Posted: Mon Jan 03, 2022 10:08 am
by Frouk
Well, i didn't had wide char string,so i decided to do without it

Re: Memory Editing & etc lua scripts

Posted: Mon Jan 03, 2022 6:08 pm
by LeFiXER
Frouk wrote:
Mon Jan 03, 2022 10:08 am
Well, i didn't had wide char string,so i decided to do without it
It doesn't hurt to provide that functionality. Widechar is a type used to deal with multi-byte characters.