Skip to content

Instantly share code, notes, and snippets.

@Rami-Sabbagh
Last active July 5, 2020 17:03
Show Gist options
  • Select an option

  • Save Rami-Sabbagh/f656f5c1a118f77c3b7a08f4c65efaaf to your computer and use it in GitHub Desktop.

Select an option

Save Rami-Sabbagh/f656f5c1a118f77c3b7a08f4c65efaaf to your computer and use it in GitHub Desktop.

Revisions

  1. Rami-Sabbagh revised this gist Aug 25, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    local LuaTable = {
    _VERSION = 'LuaTable v1.0.1 2016/08/27',
    _VERSION = 'LuaTable v1.0.2 2016/08/27',
    _AUTHOR = 'RamiLego4Game',
    _URL = 'https://gist.github.com/RamiLego4Game/f656f5c1a118f77c3b7a08f4c65efaaf',
    _DESCRIPTION = 'A library that converts tables to Lua code that can be saved',
    @@ -72,6 +72,8 @@ note: all Userdata variables can't be dumped so they will be ignored.

    --[[Changelog:
    2017/08/25 V1.0.2: Fixed a typo in the library.
    2016/08/07 V1.0.1: All Credit goes to ivan [In Love2D forums] for this update
    1. Fixed a problem in LuaTable.isArray that causes arrays with gaps to encode as normal table.
    2. Improved LuaTable.encode_string to use string.format instead of multiline string.
    @@ -116,7 +118,7 @@ function LuaTable.decode(codeString,smart)
    if not (type(codeString)=="string") then return error("Bad argument #1 to 'LuaTable.decode' (string expected, got"..type(codeString)..")") end
    if not (type(smart)=="boolean" or type(smart)=="nil") then return error("Bad argument #2 to 'LuaTable.decode' (boolean/nil expected, got"..type(smart)..")") end
    local function combine(t1,t2) for k,v in pairs(t2) do t1[k] = v end return t1 end
    local codeFunc = loadstring(code)
    local codeFunc = loadstring(codeString)
    if smart then setfenv(codeFunc,smartGVars) else setfenv(codeFunc,normalGVars) end
    return codeFunc()
    end
    @@ -224,4 +226,4 @@ function LuaTable.encode_boolean(boolean)
    end
    end

    return LuaTable
    return LuaTable
  2. Rami-Sabbagh revised this gist Aug 27, 2016. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,9 @@ note: all Userdata variables can't be dumped so they will be ignored.

    --[[Changelog:
    2016/08/07 V1.0.1: Improved LuaTable.encode_string (Credit goes to ivan [In Love2D forums]) to use string.format instead of multiline string.
    2016/08/07 V1.0.1: All Credit goes to ivan [In Love2D forums] for this update
    1. Fixed a problem in LuaTable.isArray that causes arrays with gaps to encode as normal table.
    2. Improved LuaTable.encode_string to use string.format instead of multiline string.
    2016/08/26 V1.0.0: First Public Release
    @@ -119,13 +121,12 @@ function LuaTable.decode(codeString,smart)
    return codeFunc()
    end

    function LuaTable.isArray(table)
    for k,v in pairs(table) do
    if type(k) ~= "number" then
    return false
    end
    function LuaTable.isArray(tabl)
    local n = 0
    for _ in pairs(tabl) do
    n = n + 1
    end
    return true
    return #tabl == n
    end

    --[[Here you can add your own variable types.
  3. Rami-Sabbagh revised this gist Aug 27, 2016. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    local LuaTable = {
    _VERSION = 'LuaTable v1.0.0 2016/08/26',
    _VERSION = 'LuaTable v1.0.1 2016/08/27',
    _AUTHOR = 'RamiLego4Game',
    _URL = 'https://gist.github.com/RamiLego4Game/f656f5c1a118f77c3b7a08f4c65efaaf',
    _DESCRIPTION = 'A library that converts tables to Lua code that can be saved',
    @@ -69,6 +69,15 @@ list of available global vars for the table (Changable down):
    note: all Userdata variables can't be dumped so they will be ignored.
    ]]

    --[[Changelog:
    2016/08/07 V1.0.1: Improved LuaTable.encode_string (Credit goes to ivan [In Love2D forums]) to use string.format instead of multiline string.
    2016/08/26 V1.0.0: First Public Release
    ]]

    local function combine(t1,t2) for k,v in pairs(t2) do t1[k] = v end return t1 end

    --Config--
    @@ -198,8 +207,8 @@ function LuaTable.encode_nil()
    return "nil"
    end

    function LuaTable.encode_string(string)
    return '[['..tostring(string)..']]'
    function LuaTable.encode_string(str)
    return string.format("%q", str)
    end

    function LuaTable.encode_number(number)
  4. Rami-Sabbagh revised this gist Aug 26, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ local LuaTable = {
    _VERSION = 'LuaTable v1.0.0 2016/08/26',
    _AUTHOR = 'RamiLego4Game',
    _URL = 'https://gist.github.com/RamiLego4Game/f656f5c1a118f77c3b7a08f4c65efaaf',
    _DESCRIPTION = 'A library that coverts tables to Lua code that can be saved',
    _DESCRIPTION = 'A library that converts tables to Lua code that can be saved',
    _LICENSE = [[
    MIT LICENSE
    @@ -29,7 +29,7 @@ local LuaTable = {
    ]]
    }

    --[[A library that coverts tables to Lua code that can be saved.
    --[[A library that converts tables to Lua code that can be saved.
    Since it uses Lua to save the data, It's able to save functions ! by dumping them, But they won't be readable, only loadable, unless if you decompile them.
    And moreover you can do some hand written tables with smart data generation..
    The bad thing that it's less secure (you are running code) .., So load the files using this Library to sandbox the files
  5. Rami-Sabbagh revised this gist Aug 26, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ local LuaTable = {

    --[[A library that coverts tables to Lua code that can be saved.
    Since it uses Lua to save the data, It's able to save functions ! by dumping them, But they won't be readable, only loadable, unless if you decompile them.
    And moreover you can do some hand written tables with smart data generatation..
    And moreover you can do some hand written tables with smart data generation..
    The bad thing that it's less secure (you are running code) .., So load the files using this Library to sandbox the files
    usage:
  6. Rami-Sabbagh revised this gist Aug 26, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -92,6 +92,8 @@ function LuaTable.encode(var,funcBehave,standalone)
    end

    function LuaTable.encode_pretty(var,funcBehave,standalone)
    if not (type(funcBehave)=="string" or type(funcBehave)=="nil") then return error("Bad argument #2 to 'LuaTable.encode_pretty' (string/nil expected, got"..type(funcBehave)..")") end
    if not (type(standalone)=="boolean" or type(standalone)=="nil") then return error("Bad argument #3 to 'LuaTable.encode_pretty' (boolean/nil expected, got"..type(funcBehave)..")") end
    local code = "return "
    if standalone then code = "local function c(t1,t2)\n for k,v in pairs(t2) do\n t1[k] = v\n end\n return t1\nend\n\n"..code end
    local vx = "nil" if LuaTable["encode_"..type(var)] then vx = LuaTable["encode_"..type(var)](var,prettyTabbing,funcBehave) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    @@ -100,6 +102,8 @@ function LuaTable.encode_pretty(var,funcBehave,standalone)
    end

    function LuaTable.decode(codeString,smart)
    if not (type(codeString)=="string") then return error("Bad argument #1 to 'LuaTable.decode' (string expected, got"..type(codeString)..")") end
    if not (type(smart)=="boolean" or type(smart)=="nil") then return error("Bad argument #2 to 'LuaTable.decode' (boolean/nil expected, got"..type(smart)..")") end
    local function combine(t1,t2) for k,v in pairs(t2) do t1[k] = v end return t1 end
    local codeFunc = loadstring(code)
    if smart then setfenv(codeFunc,smartGVars) else setfenv(codeFunc,normalGVars) end
  7. Rami-Sabbagh revised this gist Aug 26, 2016. No changes.
  8. Rami-Sabbagh revised this gist Aug 26, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    local LuaTable = {
    _VERSION = 'LuaTable v1.0.0 2016/08/26',
    _AUTHOR = 'RamiLego4Game',
    _URL = 'https://github.com/RamiLego4Game/',
    _URL = 'https://gist.github.com/RamiLego4Game/f656f5c1a118f77c3b7a08f4c65efaaf',
    _DESCRIPTION = 'A library that coverts tables to Lua code that can be saved',
    _LICENSE = [[
    MIT LICENSE
  9. Rami-Sabbagh created this gist Aug 26, 2016.
    213 changes: 213 additions & 0 deletions LuaTable.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,213 @@
    local LuaTable = {
    _VERSION = 'LuaTable v1.0.0 2016/08/26',
    _AUTHOR = 'RamiLego4Game',
    _URL = 'https://github.com/RamiLego4Game/',
    _DESCRIPTION = 'A library that coverts tables to Lua code that can be saved',
    _LICENSE = [[
    MIT LICENSE
    Copyright (c) 2016 Rami Sabbagh
    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    ]]
    }

    --[[A library that coverts tables to Lua code that can be saved.
    Since it uses Lua to save the data, It's able to save functions ! by dumping them, But they won't be readable, only loadable, unless if you decompile them.
    And moreover you can do some hand written tables with smart data generatation..
    The bad thing that it's less secure (you are running code) .., So load the files using this Library to sandbox the files
    usage:
    luacode (string) = LuaTable.encode(<variable>,[funcBehave],[standalone])
    luacode (string) = LuaTable.encode_pretty(<variable>,[funcBehave],[standalone])
    variable = LuaTable.decode(<luacode>,[smart])
    variable (list above):
    Supported types: nil,boolean,string,number,table,function,userdata
    funcBehave (string or nil):
    "dump" to dump the functions so they can be loaded again, But not recomended.
    "name" to use tostring on the function, I guess it's useless, unless if it's used to debug something like the _G var.
    "skip" (default) to skip the functions and not include them in the table.
    standalone (boolean or nil):
    If this is true, the result code will include a function called c (short for combine) used to combine arrays with tables (to save file lenght).
    Enable this to make the result code run without using this library.
    luacode (string):
    The code made with LuaTable library.
    smart (boolean or nil):
    To give the table code the access to some usefull gloabl libraries (like math, string, ..[list above]).
    example:
    local LuaTable = require("LuaTable")
    love.filesystem.write("TestTable.lua",LuaTable.encode({test=true}))
    local TableCode = love.filesystem.read("TestTable.lua")
    local TabelData = LuaTable.decode(TableCode)
    list of available global vars for the table (Changable down):
    Normal: pairs, ipairs, c (combine function)
    Smart: pairs, ipairs, string, table, bit, math, c (combine function), love.math (if library is used in love with love.math enabled)
    note: all Userdata variables can't be dumped so they will be ignored.
    ]]
    local function combine(t1,t2) for k,v in pairs(t2) do t1[k] = v end return t1 end

    --Config--
    local prettyTabbing = "\t"

    --Change the global vars here--
    local normalGVars = {pairs=pairs,ipairs=ipairs,c=combine} --Used when smart argument is false or nil
    local smartGVars = {pairs=pairs,ipairs=ipairs,string=string,table=table,bit=bit,math=math,c=combine} --Used when smart argument is true
    if love and love.math then smartGVars.love = {math=love.math} end

    --Library code starts here--

    function LuaTable.encode(var,funcBehave,standalone)
    if not (type(funcBehave)=="string" or type(funcBehave)=="nil") then return error("Bad argument #2 to 'LuaTable.encode' (string/nil expected, got"..type(funcBehave)..")") end
    if not (type(standalone)=="boolean" or type(standalone)=="nil") then return error("Bad argument #3 to 'LuaTable.encode' (boolean/nil expected, got"..type(funcBehave)..")") end
    local code = "return "
    if standalone then code = "local function c(t1,t2) for k,v in pairs(t2) do t1[k] = v end return t1 end\n"..code end
    local vx = "nil" if LuaTable["encode_"..type(var)] then vx = LuaTable["encode_"..type(var)](var) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    code = code..vx
    return code
    end

    function LuaTable.encode_pretty(var,funcBehave,standalone)
    local code = "return "
    if standalone then code = "local function c(t1,t2)\n for k,v in pairs(t2) do\n t1[k] = v\n end\n return t1\nend\n\n"..code end
    local vx = "nil" if LuaTable["encode_"..type(var)] then vx = LuaTable["encode_"..type(var)](var,prettyTabbing,funcBehave) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    code = code..vx
    return code
    end

    function LuaTable.decode(codeString,smart)
    local function combine(t1,t2) for k,v in pairs(t2) do t1[k] = v end return t1 end
    local codeFunc = loadstring(code)
    if smart then setfenv(codeFunc,smartGVars) else setfenv(codeFunc,normalGVars) end
    return codeFunc()
    end

    function LuaTable.isArray(table)
    for k,v in pairs(table) do
    if type(k) ~= "number" then
    return false
    end
    end
    return true
    end

    --[[Here you can add your own variable types.
    Array is a numeric table.]]

    function LuaTable.encode_array(table,pretty,funcBehave)
    local s = "{"
    for i=1,#table do
    if pretty then
    local vx = "nil" if LuaTable["encode_"..type(table[i])] then vx = LuaTable["encode_"..type(table[i])](table[i],pretty.." ",funcBehave) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    if i==1 then
    s = s..pretty..vx
    else
    s = s..",\n"..pretty..vx
    end
    else
    local vx = "nil" if LuaTable["encode_"..type(table[i])] then vx = LuaTable["encode_"..type(table[i])](table[i],nil,funcBehave) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    if i==1 then
    s = s..vx
    else
    s = s..","..vx
    end
    end
    end
    return s.."}"
    end

    function LuaTable.encode_table(table,pretty,funcBehave)
    if LuaTable.isArray(table) then return LuaTable.encode_array(table,pretty) end
    local nums, first = {}, true
    local s = "{"
    for k,v in pairs(table) do
    if pretty then
    local vx = "nil" if LuaTable["encode_"..type(v)] then vx = LuaTable["encode_"..type(v)](v,pretty.." ",funcBehave) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    if type(k) == "number" then
    nums[k] = v
    elseif first then
    s = s.."\n"..pretty..tostring(k).." = "..vx
    first = false
    else
    s = s..",\n"..pretty..tostring(k).." = "..vx
    end
    else
    local vx = "nil" if LuaTable["encode_"..type(v)] then vx = LuaTable["encode_"..type(v)](v,nil,funcBehave) else print("[LuaTable] Warning, LuaTable doesn't support "..type(var)) end
    if type(k) == "number" then
    nums[k] = v
    elseif first then
    s = s..tostring(k).."="..vx
    first = false
    else
    s = s..","..tostring(k).."="..vx
    end
    end
    end
    if #nums > 0 then
    if pretty then
    s = "c("..s.."},"..LuaTable.encode_array(nums,pretty.." ")..")"
    else
    s = "c("..s.."},"..LuaTable.encode_array(nums)..")"
    end
    else
    s = s.."}"
    end
    return s
    end

    function LuaTable.encode_function(func,pretty,funcBehave)
    local fmode = funcBehave or "skip"
    if fmode == "dump" then return string.dump(func)
    elseif fmode == "name" then return '"'..tostring(func)..'"'
    else return "nil" end
    end

    function LuaTable.encode_userdata(ud,pretty,funcBehave)
    if funcBehave and (funcBehave == "name" or funcBehave == "dump") then return '"'..tostring(ud)..'"' else return "nil" end
    end

    function LuaTable.encode_nil()
    return "nil"
    end

    function LuaTable.encode_string(string)
    return '[['..tostring(string)..']]'
    end

    function LuaTable.encode_number(number)
    return tostring(number)
    end

    function LuaTable.encode_boolean(boolean)
    if boolean then
    return "true"
    else
    return "false"
    end
    end

    return LuaTable