Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. /*
  2.   A simple file io wrapper
  3.   by Nautical
  4. */
  5.  
  6. FileIO = {
  7.   baseDir = "gmodz";
  8. }
  9.  
  10. // File Reading & Writing
  11. function FileIO:WriteFile(name, contents)
  12.  
  13.   if (!file.Exists(self.baseDir, "DATA")) then
  14.     file.CreateDir(self.baseDir);
  15.   end
  16.  
  17.   local path = self.baseDir .. "/" .. name;
  18.   file.Write(path, contents);
  19. end
  20.  
  21. function FileIO:ReadFile(name)
  22.   local path = self.baseDir .. "/" .. name;
  23.  
  24.   if (!file.Exists(path, "DATA")) then
  25.     return ""
  26.   end
  27.  
  28.   return file.Read(path,"DATA");
  29. end
  30.  
  31. // JSON Encoding / Decoding
  32. function FileIO:Encode(name, table)
  33.   self:WriteFile(name, util.TableToJSON(table));
  34. end
  35.  
  36. function FileIO:Decode(name)
  37.   local contents = self:ReadFile(name);
  38.   if (contents == "") then
  39.     return {};
  40.   end
  41.   return util.JSONToTable(contents);
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement