I have made small library for my game Robotics2D that converts tables to Lua code that can be saved, and Now I released it to public with MIT Licensing.
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.
Gist: https://gist.github.com/RamiLego4Game/f ... f4c65efaaf (Give me a star )
*The documentation is included with the gist
*The library can work with luajit alone
Feel free to give feedback
LuaTable A small library to convert tables back to lua code
- RamiLego4Game
- Citizen
- Posts: 73
- Joined: Tue Jun 10, 2014 7:41 pm
LuaTable A small library to convert tables back to lua code
- LIKO-12: An open source fantasy computer made using LÖVE: https://LIKO-12.github.io/
Re: LuaTable A small library to convert tables back to lua code
Hello there, and welcome to Love2D!
I think there may be a few parts in the lib that could be improved.
In order to handle "gaps" in numerically indexed tables, it might be better to write:
Also, it's probably safer to write:
Also, table.concat can be used instead of ".." in order to avoid a lot intermediate strings.
tostring works for nil, boolean and number too so that code could be simplified.
It's a good effort but need a little bit of work, good luck!
I think there may be a few parts in the lib that could be improved.
In order to handle "gaps" in numerically indexed tables, it might be better to write:
Code: Select all
function LuaTable.isArray(table)
local n = 0
for _ in pairs(table) do
n = n + 1
end
return #table == n
end
Code: Select all
function LuaTable.encode_string(sz)
return string.format("%q", sz)
end
tostring works for nil, boolean and number too so that code could be simplified.
It's a good effort but need a little bit of work, good luck!
- RamiLego4Game
- Citizen
- Posts: 73
- Joined: Tue Jun 10, 2014 7:41 pm
Re: LuaTable A small library to convert tables back to lua code
Good points, thanks for help I'll add them soon !ivan wrote:In order to handle "gaps" in numerically indexed tables, it might be better to write:Also, it's probably safer to write:Code: Select all
function LuaTable.isArray(table) local n = 0 for _ in pairs(table) do n = n + 1 end return #table == n end
Code: Select all
function LuaTable.encode_string(sz) return string.format("%q", sz) end
I didn't get it..ivan wrote:Also, table.concat can be used instead of ".." in order to avoid a lot intermediate strings.
I already know, But I want to give the library the ability to add more variables types (And make it possible to customize encoders for some variable types).ivan wrote:tostring works for nil, boolean and number too so that code could be simplified.
- LIKO-12: An open source fantasy computer made using LÖVE: https://LIKO-12.github.io/
- RamiLego4Game
- Citizen
- Posts: 73
- Joined: Tue Jun 10, 2014 7:41 pm
Re: LuaTable A small library to convert tables back to lua code
New release V1.0.1 2016/08/27, All credit goes for ivan in this update.
Changelog:
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.
Changelog:
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.
Last edited by RamiLego4Game on Sat Aug 27, 2016 2:56 pm, edited 2 times in total.
- LIKO-12: An open source fantasy computer made using LÖVE: https://LIKO-12.github.io/
Re: LuaTable A small library to convert tables back to lua code
For table.concat you insert all the strings you'd like to combine in a table and do one concatenation at the end instead of many in between using ..
It is a BIG performance booster in case you do lots of string merging. I've tried both in a compression algorithm and using table.concat it was a lot faster.
It is a BIG performance booster in case you do lots of string merging. I've tried both in a compression algorithm and using table.concat it was a lot faster.
- Centauri Soldier
- Prole
- Posts: 42
- Joined: Mon May 21, 2012 6:38 am
Re: LuaTable A small library to convert tables back to lua code
Hi there and thanks for sharing your code. I do have a question regarding it. Instead of parsing the table, why not just use loadstring() to bring a table into lua again?
Also, a suggestion if you do use this method, would be to (if already saved) look for meta info for each table when loading it back into lua.
Also, a suggestion if you do use this method, would be to (if already saved) look for meta info for each table when loading it back into lua.
Last bumped by RamiLego4Game on Tue Nov 07, 2017 4:07 pm.
Who is online
Users browsing this forum: No registered users and 1 guest