More Fun with Table Files

Section's for general approaches on hacking various options in games. No online-related discussions/posts OR warez!
Post Reply
aSwedishMagyar
Table Makers
Table Makers
Posts: 690
Joined: Mon Jul 06, 2020 3:19 am
Reputation: 1262

More Fun with Table Files

Post by aSwedishMagyar »

So I tend to get carried away with making functions for my lua coding and wanted a way to clean things up. What I came up with is encrypting the functions and saving those strings in a table file with the function name. This way you can 'call' your function using the readFunc(name) down below. What I have done in the example table is put the functions I wanted to save in my autorun folder and then used saveFunc(name) to add them as a table file. The two scripts included allow you to input the name of the function you want to save or read.

Thought someone might find this useful or even just something fun to play around with in terms of cleaning up code.

save-read_FunctionFiles.ct
07-04-2021
(2.8 KiB) Downloaded 1671 times

Code: Select all

function saveFunc(funcName)
	local file = findTableFile(funcName)
    if file then file.Delete() end
	file = createTableFile(funcName)
	local bytes = stringToByteTable(encodeFunction(_G[funcName]))
	file.Stream.Position = 0
    file.Stream.write(bytes,#bytes)
end

Code: Select all

function readFunc(name)
	local file = findTableFile(name)
	if file == nil then return nil end
    local stringStream = createStringStream()
    stringStream.copyFrom(file.Stream,file.Stream.Size)
    local funcTxt = stringStream.DataString
    stringStream.destroy()
    return decodeFunction(funcTxt)
end
In addition to regular lua functions, I have a way of saving scripts as well which may not be the best way since I know you can save them as .CEA files but I thought it was cool all the same and maybe you will find it helpful. The example table has a file for an Inf Health script I use in my Chronicon table so you can see how you should be setting them up. Basically, the multi-line string for enable or disable is used in autoAssemble() for the two functions and you pass the script name to saveScript along with the two functions. After that all you need to do is store the result from readScript and call element 1 to enable the script and element 2 to disable the script. The way I have it setup, the result of the autoAssemble() is returned from the function so you can detect if there was an error in enabling/disabling.

save-read_ScriptFiles.CT
07-04-2021
(3.48 KiB) Downloaded 1672 times

Code: Select all

function saveScript(scriptName,funcEnable,funcDisable)
	if scriptName == nil or funcEnable == nil or funcDisable == nil then return nil end
    local file = findTableFile(scriptName)
    if file then file.Delete() end
    file = createTableFile(scriptName)
    local bytes = stringToByteTable(encodeFunction(funcEnable).."\n"..encodeFunction(funcDisable))
	file.Stream.Position = 0
    file.Stream.write(bytes,#bytes)
end

Code: Select all

function readScript(name)
	local file = findTableFile(name)
	if file == nil then return nil end
    local stringStream = createStringStream()
	local funcList = createStringList()
    stringStream.copyFrom(file.Stream,file.Stream.Size)
    funcList.text = stringStream.DataString
	local enable = decodeFunction(funcList[0])
	local disable = decodeFunction(funcList[1])
    stringStream.destroy()
	funcList.destroy()
    return {enable,disable}
end
Enjoy!

Post Reply

Who is online

Users browsing this forum: No registered users