Skip to content

Instantly share code, notes, and snippets.

@zorggn
Last active October 27, 2017 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zorggn/1c5298c5a10cf20152f4 to your computer and use it in GitHub Desktop.
Save zorggn/1c5298c5a10cf20152f4 to your computer and use it in GitHub Desktop.
Provide a function to voluntarily set the writable directory to either the user's save directory, or the executable's source directory (when fused) in löve. (0.9.1+) P.S.: Probably unsafe/useless on linux.
local ffi = require "ffi"
local liblove = ffi.os == "Windows" and ffi.load "love" or ffi.C
ffi.cdef[[
int PHYSFS_setWriteDir(const char* newDir);
]] -- This call will fail (and fail to change the write dir) if the current write dir still has files open in it.
local usrPath = love.filesystem.getSaveDirectory()
local srcPath = love.filesystem.getSourceBaseDirectory()
local dir = 'usr'
local t = {}
t.getCurrentWriteDirectory = function()
return dir
end
t.setCurrentWriteDirectory = function(d)
assert(string.lower(d) == 'usr' or string.lower(d) == 'src',"The given parameter is not accepted!")
if dir == d then return true end
dir = d
local success
if dir == 'usr' then
success = liblove.PHYSFS_setWriteDir(usrPath)
elseif dir == 'src' then
if not love.filesystem.isFused() then error "Can't set the write directory to the source; The game is not fused!" end
success = liblove.PHYSFS_setWriteDir(srcPath)
end
return success ~= 0 and true or false,success
end
return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment