mirror of
https://github.com/EngineerSmith/nativefs.git
synced 2025-11-08 23:15:02 +00:00
create FileData from ByteData if possible
This commit is contained in:
16
nativefs.lua
16
nativefs.lua
@@ -23,6 +23,11 @@ SOFTWARE.
|
||||
local ffi, bit = require('ffi'), require('bit')
|
||||
local C = ffi.C
|
||||
|
||||
local fopen, getcwd, chdir, unlink, mkdir, rmdir
|
||||
local BUFFERMODE, MODEMAP
|
||||
local ByteArray = ffi.typeof('unsigned char[?]')
|
||||
local function _ptr(p) return p ~= nil and p or nil end -- NULL pointer to nil
|
||||
|
||||
local File = {
|
||||
getBuffer = function(self) return self._bufferMode, self._bufferSize end,
|
||||
getFilename = function(self) return self._name end,
|
||||
@@ -30,11 +35,6 @@ local File = {
|
||||
isOpen = function(self) return self._mode ~= 'c' and self._handle ~= nil end,
|
||||
}
|
||||
|
||||
local fopen, getcwd, chdir, unlink, mkdir, rmdir
|
||||
local BUFFERMODE, MODEMAP
|
||||
local ByteArray = ffi.typeof('unsigned char[?]')
|
||||
local function _ptr(p) return p ~= nil and p or nil end -- NULL pointer to nil
|
||||
|
||||
function File:open(mode)
|
||||
if self._mode ~= 'c' then return false, "File " .. self._name .. " is already open" end
|
||||
if not MODEMAP[mode] then return false, "Invalid open mode for " .. self._name .. ": " .. mode end
|
||||
@@ -114,6 +114,12 @@ function File:read(containerOrBytes, bytes)
|
||||
local data = love.data.newByteData(bytes)
|
||||
local r = tonumber(C.fread(data:getFFIPointer(), 1, bytes, self._handle))
|
||||
|
||||
if container == 'data' then
|
||||
-- FileData from ByteData requires LÖVE 11.4+
|
||||
local ok, fd = pcall(love.filesystem.newFileData, data, self._name)
|
||||
if ok then return fd end
|
||||
end
|
||||
|
||||
local str = data:getString()
|
||||
data:release()
|
||||
data = container == 'data' and love.filesystem.newFileData(str, self._name) or str
|
||||
|
||||
Reference in New Issue
Block a user