evannik23

Filesysatem

Jul 10th, 2022 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. local HTTPS = game:GetService('HttpService')
  2. local BL = {'exe', 'scr', 'bat', 'com', 'csh', 'msi', 'vb', 'vbs', 'vbe', 'ws', 'wsf', 'wsh', 'ps1', 'psy'}
  3. local function hasformat(str)
  4. if not (type(str) == 'string' and str:match('%.')) then
  5. return false
  6. end
  7. local split = str:split('.')
  8. local ext = split[#split]
  9. return (ext:match('[%a%d]+') == ext and table.find(BL, ext) == nil)
  10. end
  11.  
  12. local function validate(val, t)
  13. if type(val) ~= t then
  14. error(('invalid argument #1 (%s expected, got nil)'):format(t), 2)
  15. end
  16. end
  17.  
  18. local file = {}
  19. file.__index = file
  20.  
  21. function file.new(path, ct) -->> file.new // Creates a referable file; you can refer to this as a variable and call it later on.
  22. if path and ct and hasformat(path) and type(ct) == 'string' then
  23. writefile(path, ct)
  24. end
  25. return setmetatable({
  26. _path = path,
  27. }, file)
  28. end
  29.  
  30. function file.open(path) -- file.open // Connect to any file in your Synapse X Workspace without having to create new ones.
  31. if path and hasformat(path) then
  32. return setmetatable({
  33. _path = path,
  34. }, file)
  35. end
  36. end
  37.  
  38. function file:read()
  39. if hasformat(self._path) and isfile(self._path) then
  40. return readfile(self._path)
  41. end
  42. end
  43.  
  44. function file:has(str)
  45. if hasformat(self._path) and isfile(self._path) and str and type(str) == 'string' then
  46. local FileData = readfile(self._path)
  47. return string.match(FileData, str)
  48. end
  49. end
  50.  
  51. function file:append(ct)
  52. if self._path and ct and hasformat(self._path) and isfile(self._path) and type(ct) == 'string' then
  53. appendfile(self._path, ct)
  54. end
  55. end
  56.  
  57. function file:write(ct)
  58. if self._path and ct and hasformat(self._path) and isfile(self._path) and type(ct) == 'string' then
  59. writefile(self._path, ct)
  60. end
  61. end
  62.  
  63. function file:readJSON()
  64. if self._path and hasformat(self._path) and isfile(self._path) then
  65. local Decoded = HTTPS:JSONDecode(readfile(self._path))
  66. validate(Decoded, 'table')
  67. return Decoded
  68. end
  69. end
  70.  
  71. function file:writeJSON(tbl)
  72. validate(tbl, 'table')
  73. if self._path and hasformat(self._path) and isfile(self._path) and tbl and type(tbl) == 'table' then
  74. writefile(self._path, HTTPS:JSONEncode(tbl))
  75. end
  76. end
  77.  
  78. function file:appendJSON(tbl)
  79. validate(tbl, 'table')
  80. if hasformat(self._path) and isfile(self._path) and tbl and type(tbl) == 'table' then
  81. appendfile(self._path, HTTPS:JSONEncode(tbl))
  82. end
  83. end
  84.  
  85. function file:del()
  86. if hasformat(self._path) and isfile(self._path) then
  87. delfile(self._path)
  88. end
  89. end
  90.  
  91. function file:delete()
  92. if hasformat(self._path) and isfile(self._path) then
  93. delfile(self._path)
  94. end
  95. end
  96.  
  97. return file
Add Comment
Please, Sign In to add comment