This is just a couple of helpful functions to make saving and loading much easier.
How does it work?
It simply allows you to save and load a table of data. The table can also include sub-tables.
For example, maybe you have a table of player data you want to save:
What Lua data types are supported?
Booleans (true/false)
Strings (quotes are escaped for you)
Numbers
Sub-Tables of the above types
functions
userdata
Why just functions and not a complete user/admin/whatever script?
I realize there are plenty of complete systems out there for saving and such. Many of them have a specific way they think data, ranks, permissions, or whatever should be handled. This is intended to allow you to make your own system, or use it with an existing one. You can also use this outside of CS2D as it's just plain Lua. For example I use this externally to load reports and save them to a database so my community can manage them on the web.
How is this any better than X method?
It's faster for Lua to parse code using the loadfile() function than it is to use the io functions and parse data yourself. Being able to save recursively allows you to organize your data into sub-tables however you like and keep things clean. It's human-readable and indented, so it's easy to read or modify if needed.
Other Notes:
The salt.save() function writes data as it goes so that it can process a LOT of data if needed, making it more efficient and allowing it to support sub-tables without wasted memory. As long as the saved data returns a Lua table, it can be loaded with salt.load().
Because of how it writes as it goes, it will save unsupported types as nil. Keep this in mind if you are using OOP for your data and plan ahead. I have no intention of adding support for functions and I know it's possible.
I've made the code easy to read, but if you have any questions just ask. Let me know if you run into any issues. It's been tested but only in practical and intended uses.
Update 2017-08-15:
Source Code
The source code is now available at Github.
Changes
The functions are no longer stealing the `table` namespace, and instead follows the Lua standard for modules.
Installation
Installation is possible via LuaRocks (recommended):
1
luarocks install salt
or via git
1
git clone https://github.com/VaiN474/salt.git
Or download the zip here and extract it.
Then, either copy salt.lua to the directory your scripts are loaded from (i.e. sys/lua/) or add the path salt.lua is saved in to your package.path.
Example Usage
1
2
3
2
3
salt = require("salt") salt.save(my_table,"/path/to/file") my_table,err = salt.load("/path/to/file")
License
I've added a proper license, using the same license as Lua itself - MIT
edited 2×, last 16.08.17 05:04:58 am
Approved by Starkkz
Download
3 kb, 616 Downloads