Posted 15 April 2014 - 06:55 PM
There was not much in the function that serialize tables to a savable string, That can handle recursive entries. So I made one. This will output a human-readable string like ComputerCraft 1.6, but if you want it will output a single line like any version of ComputerCraft before 1.6. I have included a few options at the top of the API which fits in the API, the comments has been formatted to fit on screen of the terminal. This code is heavily based in textutils.serialize, but modified to work as described.
Benefits over textutils.serialize is it handles recursive entrees better then textutils.serialize which does not. This will display all recursive entries that do not cause a loop in the table. If mode is set to read and not save it will tell you where it loops from and will not follow though with the loop.
To Use:
Options:
On lines 3-19 hold the options that can be set. This just has a bunch of true false questions that will set up the API for your needs. default shown below
To use the function:
For the sake of this I will assume that overwrite is still set to false, but if true it will apply to textutils.serialize as well.
mode:
[indent=1]"save": This works just like textutils.serialize in how it handels certin variable types like functions.[/indent]
[indent=1]"read": This will write stuff like functions as "** FUNCTON **", and loops caused by recursive entries will display the entry its referring to and not loop though it.[/indent]
readable:
[indent=1]true: This will work just like 1.6 and return a human-readable string[/indent]
[indent=1]false: This will write it all out to a single line[/indent]
basename: This is just a string you pass to have it refer to the base of the table, so when recursive loops happen, it will refer to the table as if this was the variable it was stored in, and provide keys based on that.
[indent=1]example: the code below will output "Tablebase["a"]" for that loop[/indent]
[indent=1]What it Returns:
Example Code that made this:
mode = save, readable = true
mode = save, readable = false
mode = read, readable = true
mode = read, readable = false
Benefits over textutils.serialize is it handles recursive entrees better then textutils.serialize which does not. This will display all recursive entries that do not cause a loop in the table. If mode is set to read and not save it will tell you where it loops from and will not follow though with the loop.
To Use:
Spoiler
to install
pastepin get TpMLFHV2 BS
Options:
On lines 3-19 hold the options that can be set. This just has a bunch of true false questions that will set up the API for your needs. default shown below
-- Do you want the output to be human-readable?
-- (T/F) (default: true)
default.layout = true
-- Do you want the default mode to allow the output
-- to be with unserialize by default?
-- (T/F) (default: true)
default.mode = true
-- Do You want a different default name for
-- referring to the supplied table?
-- (default: "TableBase")
default.name = "TableBase"
-- Do you want to overwrite textutils.serialize()?
-- (T/F) (default: false)
default.overwrite = false
To use the function:
For the sake of this I will assume that overwrite is still set to false, but if true it will apply to textutils.serialize as well.
bs.serialize(table, mode, readable, basename)
table: This is the variable you want to serialize. does not have to be a table, but was designed for tables.mode:
[indent=1]"save": This works just like textutils.serialize in how it handels certin variable types like functions.[/indent]
[indent=1]"read": This will write stuff like functions as "** FUNCTON **", and loops caused by recursive entries will display the entry its referring to and not loop though it.[/indent]
readable:
[indent=1]true: This will work just like 1.6 and return a human-readable string[/indent]
[indent=1]false: This will write it all out to a single line[/indent]
basename: This is just a string you pass to have it refer to the base of the table, so when recursive loops happen, it will refer to the table as if this was the variable it was stored in, and provide keys based on that.
[indent=1]example: the code below will output "Tablebase["a"]" for that loop[/indent]
[indent=1]
a={}
a.a{}
a.a.a={}
a.a.a.a=a.a
[/indent]Spoiler
API used in example code pastebin. Please not the button API in this example works enough for the following outputs but is not done. Only included to show all code that created the following outputs.Example Code that made this:
Spoiler
-- create new button contoler
local a = button.new("right")
-- create table saver
local function save(table,name,mode,layout)
local file = fs.open(name,"w")
file.write(bs.serialize(table,mode,layout))
file.close()
end
-- create function for buttons
local function test(J)
print(tostring(J).." was clicked")
end
--trickery so not to produse errors,
-- because of functions
mon = a.mon
a.mon = nil
a:new("a","test",3,13,3,5,nil)
a:new("b","test",15,25,3,5,nil)
a:new("c","test",3,13,7,9,nil)
save(a,"sp","save",true) -- save pretty (default behaver)
save(a,"sn","save",false) -- save not pretty
-- restore proper layout
a.mon = mon
a:new("a","test",3,13,3,5,test,{argPass="a"})
a:new("b","test",15,25,3,5,test,{argPass="b"})
a:new("c","test",3,13,7,9,test,{argPass="c"})
a:new("d","test",15,25,7,9,test,{argPass="d"})
save(a,"rp","read",true) -- read pretty
save(a,"rn","read",false) -- read not pretty
a:run()
Spoiler
{
["side"] = "right",
["scale"] = 1,
["color"] = {
["mon"] = {
["text"] = 1,
["back"] = 32768,
},
["off"] = {
["text"] = 1,
["back"] = 16384,
},
["on"] = {
["text"] = 1,
["back"] = 32,
},
},
["Data"] = {
["d"] = {
["text"] = "test",
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["display"] = true,
["y"] = {
["min"] = 7,
["max"] = 9,
["mid"] = 8,
},
["x"] = {
["min"] = 15,
["max"] = 25,
["mid"] = 3,
},
},
["a"] = {
["text"] = "test",
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["display"] = true,
["y"] = {
["min"] = 3,
["max"] = 5,
["mid"] = 4,
},
["x"] = {
["min"] = 3,
["max"] = 13,
["mid"] = 3,
},
},
["c"] = {
["text"] = "test",
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["display"] = true,
["y"] = {
["min"] = 7,
["max"] = 9,
["mid"] = 8,
},
["x"] = {
["min"] = 3,
["max"] = 13,
["mid"] = 3,
},
},
["b"] = {
["text"] = "test",
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["display"] = true,
["y"] = {
["min"] = 3,
["max"] = 5,
["mid"] = 4,
},
["x"] = {
["min"] = 15,
["max"] = 25,
["mid"] = 3,
},
},
},
["size"] = {
["y"] = 12,
["x"] = 50,
},
}
Spoiler
{["side"]="right",["scale"]=1,["color"]={["mon"]={["text"]=1,["back"]=32768,},["off"]={["text"]=1,["back"]=16384,},["on"]={["text"]=1,["back"]=32,},},["Data"]={["d"]={["text"]="test",["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["display"]=true,["y"]={["min"]=7,["max"]=9,["mid"]=8,},["x"]={["min"]=15,["max"]=25,["mid"]=3,},},["a"]={["text"]="test",["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["display"]=true,["y"]={["min"]=3,["max"]=5,["mid"]=4,},["x"]={["min"]=3,["max"]=13,["mid"]=3,},},["c"]={["text"]="test",["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["display"]=true,["y"]={["min"]=7,["max"]=9,["mid"]=8,},["x"]={["min"]=3,["max"]=13,["mid"]=3,},},["b"]={["text"]="test",["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["display"]=true,["y"]={["min"]=3,["max"]=5,["mid"]=4,},["x"]={["min"]=15,["max"]=25,["mid"]=3,},},},["size"]={["y"]=12,["x"]=50,},}
Spoiler
{
["side"] = "right",
["scale"] = 1,
["color"] = {
["mon"] = {
["text"] = 1,
["back"] = 32768,
},
["off"] = {
["text"] = 1,
["back"] = 16384,
},
["on"] = {
["text"] = 1,
["back"] = 32,
},
},
["Data"] = {
["d"] = {
["text"] = "test",
["func"] = ** FUNCTION **,
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["argPass"] = "d",
["display"] = true,
["y"] = {
["min"] = 7,
["max"] = 9,
["mid"] = 8,
},
["x"] = {
["min"] = 15,
["max"] = 25,
["mid"] = 3,
},
},
["a"] = {
["text"] = "test",
["func"] = ** FUNCTION **,
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["argPass"] = "a",
["display"] = true,
["y"] = {
["min"] = 3,
["max"] = 5,
["mid"] = 4,
},
["x"] = {
["min"] = 3,
["max"] = 13,
["mid"] = 3,
},
},
["c"] = {
["text"] = "test",
["func"] = ** FUNCTION **,
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["argPass"] = "c",
["display"] = true,
["y"] = {
["min"] = 7,
["max"] = 9,
["mid"] = 8,
},
["x"] = {
["min"] = 3,
["max"] = 13,
["mid"] = 3,
},
},
["b"] = {
["text"] = "test",
["func"] = ** FUNCTION **,
["color"] = {
["off"] = {
["text"] = 0,
["back"] = 0,
},
["on"] = {
["text"] = 0,
["back"] = 0,
},
},
["click"] = true,
["argPass"] = "b",
["display"] = true,
["y"] = {
["min"] = 3,
["max"] = 5,
["mid"] = 4,
},
["x"] = {
["min"] = 15,
["max"] = 25,
["mid"] = 3,
},
},
},
["size"] = {
["y"] = 12,
["x"] = 50,
},
["mon"] = {
["write"] = ** FUNCTION **,
["setBackgroundColor"] = ** FUNCTION **,
["getCursorPos"] = ** FUNCTION **,
["clear"] = ** FUNCTION **,
["getSize"] = ** FUNCTION **,
["setBackgroundColour"] = ** FUNCTION **,
["setTextColor"] = ** FUNCTION **,
["scroll"] = ** FUNCTION **,
["setCursorPos"] = ** FUNCTION **,
["setTextColour"] = ** FUNCTION **,
["setCursorBlink"] = ** FUNCTION **,
["isColour"] = ** FUNCTION **,
["isColor"] = ** FUNCTION **,
["clearLine"] = ** FUNCTION **,
["setTextScale"] = ** FUNCTION **,
},
}
Spoiler
{["side"]="right",["scale"]=1,["color"]={["mon"]={["text"]=1,["back"]=32768,},["off"]={["text"]=1,["back"]=16384,},["on"]={["text"]=1,["back"]=32,},},["Data"]={["d"]={["text"]="test",["func"]=** FUNCTION **,["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["argPass"]="d",["display"]=true,["y"]={["min"]=7,["max"]=9,["mid"]=8,},["x"]={["min"]=15,["max"]=25,["mid"]=3,},},["a"]={["text"]="test",["func"]=** FUNCTION **,["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["argPass"]="a",["display"]=true,["y"]={["min"]=3,["max"]=5,["mid"]=4,},["x"]={["min"]=3,["max"]=13,["mid"]=3,},},["c"]={["text"]="test",["func"]=** FUNCTION **,["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["argPass"]="c",["display"]=true,["y"]={["min"]=7,["max"]=9,["mid"]=8,},["x"]={["min"]=3,["max"]=13,["mid"]=3,},},["b"]={["text"]="test",["func"]=** FUNCTION **,["color"]={["off"]={["text"]=0,["back"]=0,},["on"]={["text"]=0,["back"]=0,},},["click"]=true,["argPass"]="b",["display"]=true,["y"]={["min"]=3,["max"]=5,["mid"]=4,},["x"]={["min"]=15,["max"]=25,["mid"]=3,},},},["size"]={["y"]=12,["x"]=50,},["mon"]={["write"]=** FUNCTION **,["setBackgroundColor"]=** FUNCTION **,["getCursorPos"]=** FUNCTION **,["clear"]=** FUNCTION **,["getSize"]=** FUNCTION **,["setBackgroundColour"]=** FUNCTION **,["setTextColor"]=** FUNCTION **,["scroll"]=** FUNCTION **,["setCursorPos"]=** FUNCTION **,["setTextColour"]=** FUNCTION **,["setCursorBlink"]=** FUNCTION **,["isColour"]=** FUNCTION **,["isColor"]=** FUNCTION **,["clearLine"]=** FUNCTION **,["setTextScale"]=** FUNCTION **,},}