-
Notifications
You must be signed in to change notification settings - Fork 0
/
urfs.lua
63 lines (51 loc) · 1.45 KB
/
urfs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- Copyright (C) 2022 Ross Grams
--
-- This module is free software; you can redistribute it and/or
-- modify it under the terms of the MIT license.
local M = {}
local ffi = require "ffi"
local C = ffi.os == "Windows" and ffi.load("love") or ffi.C
ffi.cdef[[
int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath);
int PHYSFS_unmount(const char * oldDir);
const char * PHYSFS_getWriteDir(void);
int PHYSFS_setWriteDir(const char * newDir);
const char * PHYSFS_getLastError(void);
]]
local mountedAs = {}
local function getLastErrorMessage()
return ffi.string(C.PHYSFS_getLastError())
end
function M.mount(archive, mountPoint, appendToPath)
if C.PHYSFS_mount(archive, mountPoint, appendToPath and 1 or 0) == 0 then
return false, getLastErrorMessage()
end
if not mountedAs[archive] then
mountedAs[archive] = mountPoint or ""
end
return true
end
function M.unmount(archive)
if C.PHYSFS_unmount(archive) == 0 then
return false, getLastErrorMessage()
end
mountedAs[archive] = nil
return true
end
function M.getMountPoint(archive)
return mountedAs[archive]
end
function M.setWriteDir(dir)
if C.PHYSFS_setWriteDir(dir) == 0 then
return false, getLastErrorMessage()
end
return true
end
function M.getWriteDir()
local r = C.PHYSFS_getWriteDir()
if r == nil then -- A NULL pointer returned from FFI is not falsy, but == nil.
return love.filesystem.getSaveDirectory()
end
return ffi.string(r)
end
return M