More Fun with Table Files
Posted: Wed Apr 07, 2021 8:54 am
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.
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.
Enjoy!
Thought someone might find this useful or even just something fun to play around with in terms of cleaning up code.
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
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