English [SALT] Save and Load Tables icon

13 comments
16.02.15 09:11:02 am
like 12 like it!
3 kb, 392 Downloads
VaiN
User
Offline Off
I've seen many posts about people struggling to save and load data, and using all kinds of strange ways to go about it.

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:
More >


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):
Code:
1
luarocks install salt


or via git
Code:
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
Code:
1
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
More >
edited 2×, last 16.08.17 05:04:58 am
ok This file has been reviewed and approved by Starkkz (03.12.15 11:54:41 pm)

Comments

13 comments
Goto Page
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in
16.08.17 11:45:58 am
like I like it!
Up
Ajmin
User
Offline Off
I like it because i love it.
22.07.17 12:25:16 am
like I like it!
Up
Hajt
User
Offline Off
Pretty fast method of saving/loading data.
15.01.17 07:23:40 pm
like I like it!
Up
Gaios
Security Supporter
Offline Off
nice, thanks
14.12.16 11:12:55 am
like I like it!
Up
saschalois
User
Offline Off
Nice Stuff
26.09.16 08:10:09 pm
like I like it!
Up
Masea
Super User
Offline Off
What a good guy...
10.11.15 08:24:51 am
like I like it!
Up
THEMUD
User
Offline Off
Awesome! Very useful!
06.11.15 10:01:23 pm
like I like it!
Up
Inflexion
User
Offline Off
Finaally, something that could be useful.
06.11.15 07:13:16 pm
like I like it!
Up
TopNotch
User
Offline Off
This is just great.
16.10.15 12:58:21 am
like I like it!
Up
The Smoker
User
Offline Off
Very useful script
26.09.15 03:13:34 pm
like I like it!
Up
tontonEd
User
Offline Off
very underrated file, it's save a lot of time thanks.
25.02.15 06:27:36 pm
like I like it!
Up
Yates
Reviewer
Offline Off
Thaannkk you.
20.02.15 12:32:16 pm
Up
MikuAuahDark
User
Offline Off
I already did something similar before, with exception that it doesn't save, it returns string instead(like uneval does)
16.02.15 10:54:43 am
like I like it!
Up
_Yank
User
Offline Off
Although I'm not using this, this is a pretty good method to save and load stuff.
To the start Previous 1 Next To the start