Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cookieDir = "/cookies"
- local boot = false
- -- Initialise le répertoire de stockage
- -- @return bool
- local function _reboot()
- if not boot then
- if not fs.isDir(cookieDir) then
- fs.makeDir(cookieDir)
- boot = fs.isDir(cookieDir)
- end
- end
- return boot
- end
- -- Retourne les données du fichier
- -- @param string label Nom du fichier
- -- @return table data
- function get(label)
- if not _reboot() then
- print("cookie.get() : Couldn't load "..cookieDir)
- return nil
- end
- local data = { }
- local path = cookieDir.."/"..label
- -- check file data
- if fs.exists(path) then
- local handle = assert(fs.open(path, "r"), "fs.open() : Couldn't load "..path) -- this file is opened in read mode
- local input = handle.readAll() -- input is now the serialized table
- handle.close()
- -- -- load data
- data = textutils.unserialize(input) -- this will decode our table
- return data
- else
- print("cookie.get() : Couldn't load "..path)
- return nil
- end
- end
- -- Consigne les données dans un fichier
- -- @param string label Nom du fichier
- -- @param table data Tableau des variables à stocker
- -- @return bool success
- function set(label, data)
- if not _reboot() then
- print("cookie.set() : Couldn't load "..cookieDir)
- return false
- end
- local path = cookieDir.."/"..label
- local output = textutils.serialize(data)
- local handle = assert(fs.open(path, "w"), "fs.open() : Couldn't save "..path) -- this will give you an error if something's gone wrong
- handle.write(output) -- this is where the magic happens, we're storing the entire "target" table
- handle.close()
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment