Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. ------------------------------------------------------------------------------
  2. -- Serialization library for computercraft by Hea3veN
  3. -- Version 0.1
  4. ------------------------------------------------------------------------------
  5. -- Example:
  6. --
  7. -- -- The following script counts the amount of times it has been run
  8. -- loadfile("serialize.lua")()
  9. --
  10. -- PROG_DATA_NAME = "myProgData"
  11. --
  12. -- myData = unserialize(PROG_DATA_NAME)
  13. -- if myData == nil then
  14. -- myData = {}
  15. -- myData.runCount = 1
  16. -- end
  17. --
  18. -- print("This program has been run "..tostring(myData.runCount).." time(s)")
  19. --
  20. -- myData.runCount = myData.runCount + 1
  21. -- serialize(PROG_DATA_NAME)
  22. --
  23. -- Interface:
  24. --
  25. -- serialize(data, name)
  26. -- Save the table *data* to a file with *name* in the directory /var.
  27. --
  28. -- unserialize(name)
  29. -- Read and load a saved table from a file in the directory /var with
  30. -- *name* and return it.
  31. --
  32.  
  33. function serialize(data, name)
  34. if not fs.exists('/data') then
  35. fs.makeDir('/data')
  36. end
  37. local f = fs.open('/data/'..name, 'w')
  38. f.write(textutils.serialize(data))
  39. f.close()
  40. end
  41.  
  42. function unserialize(name)
  43. if fs.exists('/data/'..name) then
  44. local f = fs.open('/data/'..name, 'r')
  45. data = textutils.unserialize(f.readAll())
  46. f.close()
  47. end
  48. return data
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement