mirror of
https://github.com/EngineerSmith/nativefs.git
synced 2025-11-08 23:15:02 +00:00
mount, unmount, getDirectoryItems
This commit is contained in:
27
nativefs.lua
27
nativefs.lua
@@ -2,6 +2,9 @@ local ffi, bit = require('ffi'), require('bit')
|
|||||||
local nativefs = {}
|
local nativefs = {}
|
||||||
|
|
||||||
ffi.cdef([[
|
ffi.cdef([[
|
||||||
|
int PHYSFS_mount(const char* dir, const char* mountPoint, int appendToPath);
|
||||||
|
int PHYSFS_unmount(const char* dir);
|
||||||
|
|
||||||
typedef struct FILE FILE;
|
typedef struct FILE FILE;
|
||||||
|
|
||||||
FILE* fopen(const char* pathname, const char* mode);
|
FILE* fopen(const char* pathname, const char* mode);
|
||||||
@@ -19,6 +22,7 @@ local C = ffi.C
|
|||||||
local fclose, ftell, fseek, fflush, feof = C.fclose, C.ftell, C.fseek, C.fflush
|
local fclose, ftell, fseek, fflush, feof = C.fclose, C.ftell, C.fseek, C.fflush
|
||||||
local fread, fwrite, feof, setvbuf = C.fread, C.fwrite, C.feof, C.setvbuf
|
local fread, fwrite, feof, setvbuf = C.fread, C.fwrite, C.feof, C.setvbuf
|
||||||
local fopen, getcwd, chdir, unlink -- system specific
|
local fopen, getcwd, chdir, unlink -- system specific
|
||||||
|
local loveC = ffi.os == 'Windows' and ffi.load('love') or C
|
||||||
|
|
||||||
local BUFFERMODE = {
|
local BUFFERMODE = {
|
||||||
full = 0,
|
full = 0,
|
||||||
@@ -43,14 +47,13 @@ if ffi.os == 'Windows' then
|
|||||||
};
|
};
|
||||||
#pragma(pop)
|
#pragma(pop)
|
||||||
|
|
||||||
int MultiByteToWideChar(unsigned int CodePage, uint32_t dwFlags, const char* lpMultiByteStr, int cbMultiByte, const wchar_t* lpWideCharStr, int cchWideChar);
|
int MultiByteToWideChar(unsigned int cp, uint32_t flags, const char* mb, int cmb, const wchar_t* wc, int cwc);
|
||||||
int WideCharToMultiByte(unsigned int CodePage, uint32_t dwFlags, const wchar_t* lpWideCharStr, int cchWideChar, const char* lpMultiByteStr, int cchMultiByte, const char* default, int* used);
|
int WideCharToMultiByte(unsigned int cp, uint32_t flags, const wchar_t* wc, int cwc, const char* mb,
|
||||||
|
int cmb, const char* def, int* used);
|
||||||
int GetLogicalDrives(void);
|
int GetLogicalDrives(void);
|
||||||
void* FindFirstFileW(const wchar_t* lpFileName, struct WIN32_FIND_DATAW* lpFindFileData);
|
void* FindFirstFileW(const wchar_t* lpFileName, struct WIN32_FIND_DATAW* lpFindFileData);
|
||||||
bool FindNextFileW(HANDLE hFindFile, struct WIN32_FIND_DATAW* fd);
|
bool FindNextFileW(HANDLE hFindFile, struct WIN32_FIND_DATAW* fd);
|
||||||
bool FindClose(HANDLE hFindFile);
|
bool FindClose(HANDLE hFindFile);
|
||||||
|
|
||||||
int _wchdir(const wchar_t* path);
|
int _wchdir(const wchar_t* path);
|
||||||
wchar_t* _wgetcwd(wchar_t* buffer, int maxlen);
|
wchar_t* _wgetcwd(wchar_t* buffer, int maxlen);
|
||||||
FILE* _wfopen(const wchar_t* name, const wchar_t* mode);
|
FILE* _wfopen(const wchar_t* name, const wchar_t* mode);
|
||||||
@@ -284,10 +287,12 @@ function nativefs.newFileData(filepath)
|
|||||||
return love.filesystem.newFileData(data, filepath)
|
return love.filesystem.newFileData(data, filepath)
|
||||||
end
|
end
|
||||||
|
|
||||||
function nativefs.mount()
|
function nativefs.mount(archive, mountPoint, appendToPath)
|
||||||
|
return loveC.PHYSFS_mount(archive, mountPoint, appendToPath and 1 or 0) ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function nativefs.unmount()
|
function nativefs.unmount(archive)
|
||||||
|
return loveC.PHYSFS_unmount(archive) ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function nativefs.read(containerOrName, nameOrSize, sizeOrNil)
|
function nativefs.read(containerOrName, nameOrSize, sizeOrNil)
|
||||||
@@ -345,13 +350,17 @@ end
|
|||||||
|
|
||||||
function nativefs.load(name)
|
function nativefs.load(name)
|
||||||
local chunk, err = nativefs.read(name)
|
local chunk, err = nativefs.read(name)
|
||||||
if not chunk then
|
if not chunk then return nil, err end
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
return loadstring(chunk, name)
|
return loadstring(chunk, name)
|
||||||
end
|
end
|
||||||
|
|
||||||
function nativefs.getDirectoryItems(dir, callback)
|
function nativefs.getDirectoryItems(dir, callback)
|
||||||
|
if not nativefs.mount(dir, '__nativefs__temp__') then
|
||||||
|
return false, "Could not mount " .. dir
|
||||||
|
end
|
||||||
|
local items = love.filesystem.getDirectoryItems('__nativefs__temp__', callback)
|
||||||
|
nativefs.unmount(dir)
|
||||||
|
return items
|
||||||
end
|
end
|
||||||
|
|
||||||
function nativefs.getWorkingDirectory()
|
function nativefs.getWorkingDirectory()
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package.path = package.path .. ';../?.lua'
|
|||||||
|
|
||||||
local ffi = require('ffi')
|
local ffi = require('ffi')
|
||||||
local lu = require('luaunit')
|
local lu = require('luaunit')
|
||||||
local fs = require('nativefs')
|
|
||||||
local lfs = love.filesystem
|
local lfs = love.filesystem
|
||||||
|
local fs
|
||||||
|
|
||||||
local equals, notEquals = lu.assertEquals, lu.assertNotEquals
|
local equals, notEquals = lu.assertEquals, lu.assertNotEquals
|
||||||
local isError, containsError = lu.assertErrorMsgEquals, lu.assertErrorMsgContains
|
local isError, containsError = lu.assertErrorMsgEquals, lu.assertErrorMsgContains
|
||||||
@@ -40,6 +40,11 @@ function test_fs_newFileData()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function test_fs_mount()
|
function test_fs_mount()
|
||||||
|
equals(fs.mount('data', 'test_data'), true)
|
||||||
|
local data, size = love.filesystem.read('test_data/ümläüt.txt')
|
||||||
|
notEquals(data, nil)
|
||||||
|
notEquals(size, nil)
|
||||||
|
equals(fs.unmount('data'), true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_fs_read()
|
function test_fs_read()
|
||||||
@@ -102,6 +107,12 @@ function test_fs_load()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function test_fs_getDirectoryItems()
|
function test_fs_getDirectoryItems()
|
||||||
|
local items = fs.getDirectoryItems('data')
|
||||||
|
|
||||||
|
local map = {}
|
||||||
|
for i = 1, #items do map[items[i]] = true end
|
||||||
|
equals(map['ümläüt.txt'], true)
|
||||||
|
equals(map['test.lua'], true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_fs_setWorkingDirectory()
|
function test_fs_setWorkingDirectory()
|
||||||
@@ -197,5 +208,16 @@ end
|
|||||||
function test_File_flush()
|
function test_File_flush()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function test_xxx_globalsCheck()
|
||||||
|
for k, v in pairs(_G) do
|
||||||
|
equals(v, _globals[k])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
_globals = {}
|
||||||
|
for k, v in pairs(_G) do _globals[k] = v end
|
||||||
|
|
||||||
|
fs = require('nativefs')
|
||||||
|
|
||||||
lu.LuaUnit.new():runSuite('--verbose')
|
lu.LuaUnit.new():runSuite('--verbose')
|
||||||
love.event.quit()
|
love.event.quit()
|
||||||
|
|||||||
Reference in New Issue
Block a user