Advertisement
briannovius

puplib

Jul 25th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. --Variables
  2. local func = {}
  3. --Functions
  4. local isGood = function(a,b)
  5. if ((not a) or (not b)) then
  6. error("pack/unpack <input> <output>")
  7. end
  8. if not fs.exists(a) then
  9. error("Invalid input location")
  10. end
  11. end
  12. local subList = function(l,a,b)
  13. local out = {}
  14. for i = a,b do
  15. out[#out+1] = l[i]
  16. end
  17. return out
  18. end
  19. local joinL = function(...)
  20. local lists = {...}
  21. local out = {}
  22. for a = 1,#lists do
  23. for b = 1,#lists[a] do
  24. out[#out+1] = lists[a][b]
  25. end
  26. end
  27. return out
  28. end
  29. local replList = function(a,i,b)
  30. local l1 = subList(a,1,i-1)
  31. local l2 = subList(a,i+1,#a)
  32. return joinL(l1,b,l2)
  33. end
  34. local getSub
  35. getSub = function(path,extra)
  36. local l = fs.list(path)
  37. local offset = 0
  38. for a = 1,#l do
  39. i = a+offset
  40. --print(fs.combine(path,l[i]),fs.isDir(fs.combine(path,l[i])))
  41. if fs.isDir(fs.combine(path,l[i])) then
  42. local oldlen = #l
  43. l = replList(l,i,getSub(fs.combine(path,l[i]),fs.combine(extra,l[i])))
  44. offset = offset+(#l-oldlen)
  45. else
  46. l[i] = fs.combine(extra,l[i])
  47. end
  48. end
  49. return l
  50. end
  51. func.pack = function(inp,out,data)
  52. isGood(inp,out)
  53. local files = getSub(inp,"")
  54. local output = {}
  55. output.meta = data
  56. for i = 1,#files do
  57. local f = fs.open(fs.combine(inp,files[i]),"r")
  58. local str = f.readAll()
  59. f.close()
  60. output[#output+1] = {files[i],str}
  61. end
  62. local f = fs.open(out,"w")
  63. f.write(textutils.serialize(output))
  64. f.close()
  65. end
  66. func.unpack = function(inp,out)
  67. isGood(inp,out)
  68. local f = fs.open(inp,"r")
  69. local tbl = textutils.unserialize(f.readAll())
  70. f.close()
  71. for i = 1,#tbl do
  72. local f = fs.open(fs.combine(out,tbl[i][1]),"w")
  73. f.write(tbl[i][2])
  74. f.close()
  75. end
  76. end
  77. func.uninstall = function(packed,root)
  78. local f = fs.open(packed,"r")
  79. tbl = textutils.unserialize(f.readAll())
  80. f.close()
  81. for i = 1,#tbl do
  82. fs.delete(fs.combine(root,tbl[i][1]))
  83. local path = fs.combine(root,tbl[i][1])
  84. while fs.getDir(path) ~= ".." and fs.getDir(path) ~= "" do
  85. if #fs.list(fs.getDir(path)) == 0 then
  86. fs.delete(fs.getDir(path))
  87. path = fs.getDir(path)
  88. else
  89. break
  90. end
  91. end
  92. end
  93. end
  94. func.getMeta = function(packed)
  95. local f = fs.open(packed,"r")
  96. local d = textutils.unserialize(f.readAll())
  97. f.close()
  98. return d.meta
  99. end
  100. --Code
  101. --func.subList = subList
  102. --func.replList = replList
  103. --func.getSub = getSub
  104. --func.joinL = joinL
  105. return func
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement