Advertisement
NoNickNameTriggers

Files.lua

Mar 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. local filesystem = require("filesystem")
  2. local serialization = require("serialization")
  3. local files = {}
  4.  
  5. ----------------------------------------------------------------------------------------------------------------------------
  6.  
  7. function files.loadTableFromFile(path)
  8.     local file = io.open(path, "r")
  9.     local data = serialization.unserialize(file:read("*a"))
  10.     file:close()
  11.     return data
  12. end
  13.  
  14. function files.saveTableToFile(path, tableToSave)
  15.     filesystem.makeDirectory(filesystem.path(path) or "")
  16.     local file = io.open(path, "w")
  17.     file:write(serialization.serialize(tableToSave))
  18.     file:close()
  19. end
  20.  
  21. -- Открыть файл для чтения в байтном режиме
  22. function files.openForReadingBytes(path)
  23.     local myFileStream = {}
  24.  
  25.     myFileStream.luaFileStream = io.open(path, "rb")
  26.  
  27.     function myFileStream.read(count)
  28.         return myFileStream.luaFileStream:read(count)
  29.     end
  30.  
  31.     function myFileStream.readByteAsString()
  32.         return myFileStream.luaFileStream:read(1)
  33.     end
  34.  
  35.     function myFileStream.readByteAsDec()
  36.         local readedByte = myFileStream.luaFileStream:read(1)
  37.         if readedByte then return string.byte(readedByte) else return nil end
  38.     end
  39.  
  40.     function myFileStream.readByteAsHex()
  41.         local readedByte = myFileStream.luaFileStream:read(1)
  42.         if readedByte then return string.format("%02X", string.byte(readedByte)) else return nil end
  43.     end
  44.  
  45.     function myFileStream.readByteAsDecimal()
  46.         return myFileStream.readByteAsDec()
  47.     end
  48.  
  49.     function myFileStream.readByteAsHexadecimal()
  50.         return myFileStream.readByteAsHex()
  51.     end
  52.  
  53.     function myFileStream.close()
  54.         myFileStream.luaFileStream:close()
  55.     end
  56.  
  57.     return myFileStream
  58. end
  59.  
  60. ----------------------------------------------------------------------------------------------------------------------------
  61.  
  62. -- ecs.prepareToExit()
  63.  
  64. -- local file = files.open("test.txt", "rb")
  65. -- for i = 1, 100 do
  66. --  print(file.readByteAsHex())
  67. -- end
  68. -- file:close()
  69.  
  70.  
  71. return files
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement