This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Advert's profile picture

[1.31-] Vault (alpha) - save your datas.

Started by Advert, 29 March 2012 - 08:33 AM
Advert #1
Posted 29 March 2012 - 10:33 AM
What's this for?
Saving data, without having to deal with files, or all that other crap.

Here's all you have to do to save data:

os.loadAPI("vault")
vault.setDir("/.vault") -- The default, but overriding it will not cause it to revert once the program ends, so you more or less have to use this, or openFile.
local db = vault.open("myprogram.dat")
-- Now use db as a table to save stuff. Note that vault does not support tables yet, and will error upon any bad datatype (function, table).
db[1] = "cake!"
db.cake = true
db[true] = "cake"
-- The next time you open the vault, your data will* be there.
* Assuming no bugs and files not deleted.



Functions:
  • v = vault.open(name) or vault.openFile(name, file)
  • Creates a new vault object to store data in.
  • vault.flush(v)
  • 're'-saves the data using the internal representation, removing any void entries.
  • vault.setMode(v, mode) [none, append, hybrid]
  • Mode explanation, and reasons for usage: none: There isn't really any reason to use this over getRaw(). append: Will save data every line, by appending it to the file. hybrid: Same as append, but once HybridN changes have been made, it'll flush.
  • t = vault.getRaw(v)
  • Returns the raw table of data.
  • dir = vault.getDir()
  • Returns the directory used to store data.
  • vault.setDir(dir)
  • Changes the directory used to store data * Existing objects will not change their directory.
  • vault.setHybridN(v, n)
  • Sets the number of changes before a flush, when using the hybrid mode.
  • vault.reload(v)
  • Re-loads a vault from a file. (any changes to the raw table will be lost)
Color legend: string, number, (vault) table




Todo list:
0) Sleep.
1) Finish testing.
1.1) Perhaps some examples?
2) Re-evaluate names.
3) Think about table support; I have some ideas.
4) Anything not covered by 0-3



Known bugs:
  • Error messages are unhelpful (assumes correct arguments 100% of the time). I'm working on this.
  • Errors unless you use vault.setDir() first. (if the directory doesn't exist)


Lience: http://www.apache.or...ICENSE-2.0.html

Downloads: pastie, pastebin (pastebin get vhAGg4VB) Zip with limited test script: [attachment=122:vault.zip]
cant_delete_account #2
Posted 29 March 2012 - 04:31 PM
Nice program, might be using this!
Espen #3
Posted 29 March 2012 - 06:10 PM
Hey Advert, nice work.
This should make changing whole program states on the fly a breeze.
Kudos! *thumbs up*
Advert #4
Posted 29 March 2012 - 06:25 PM
Nice program, might be using this!
Hey Advert, nice work.
This should make changing whole program states on the fly a breeze.
Kudos! *thumbs up*

Thanks!

I'm working on an update that'll change the default mode to hybrid, and provide useful error messages for all of the API functions.
cant_delete_account #5
Posted 10 April 2012 - 02:04 AM
Thanks!

I'm working on an update that'll change the default mode to hybrid, and provide useful error messages for all of the API functions.
Do you think I can use this in my OS that I'm working on? I'll credit you. (of course)
Advert #6
Posted 10 April 2012 - 10:01 AM
Go ahead :P/>/>
I'd recommend fixing the bug, though (more or less just a setDir("/.vault") at the end of the API, or after creating the API. I haven't had that much time to work on this, but I think I'll re-write it from scratch, since I have some new ideas :D/>/>
FuZi0nHD #7
Posted 11 April 2012 - 01:52 AM
Hi,
this is awesome but i cant quite figure out how to read from the file(example plz)
i cant write but not read…
but awesome mod!!!! :P/>/> :D/>/> :)/>/>
Advert #8
Posted 11 April 2012 - 03:09 AM
Hi,
this is awesome but i cant quite figure out how to read from the file(example plz)
i cant write but not read…
but awesome mod!!!! :P/>/> :D/>/> :)/>/>

You don't have to read from the file. Open it like you would a new one, and everything should be as it was before.
You will need to add vault.setDir(vault.getDir()) to the end of the API, or call it in your code, though, due to the bug.
MStef94 #9
Posted 02 August 2012 - 09:52 AM
Hey I written this code:

os.loadAPI("vault")
dir = vault.getDir()
vault.setDir(dir)
local db = vault.open("database.dat")
db[1] = "thing 1"
db[2] = 3

and I have an error: "File not found", but the file is already there and it has the database in it with these two values above… :/

Can anyone help me? I can't read values…