mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
filesystem restructure work.
Add mount/unmountCommonPath, mount/unmountFullPath, getFullCommonPath. Reimplement identity and save directory code in terms of new commonpath code. Add internal createRealDirectory function (not exposed, currently).
This commit is contained in:
@@ -28,8 +28,10 @@
|
||||
|
||||
#if defined(LOVE_MACOS) || defined(LOVE_IOS)
|
||||
#include "common/apple.h"
|
||||
#include <unistd.h>
|
||||
#elif defined(LOVE_WINDOWS)
|
||||
#include <windows.h>
|
||||
#include <fileapi.h>
|
||||
#include "common/utf8.h"
|
||||
#elif defined(LOVE_LINUX)
|
||||
#include <unistd.h>
|
||||
@@ -68,6 +70,14 @@ FileData *Filesystem::newFileData(const void *data, size_t size, const char *fil
|
||||
}
|
||||
|
||||
bool Filesystem::isRealDirectory(const std::string &path) const
|
||||
{
|
||||
FileType ftype = FILETYPE_MAX_ENUM;
|
||||
if (!getRealPathType(path, ftype))
|
||||
return false;
|
||||
return ftype == FILETYPE_DIRECTORY;
|
||||
}
|
||||
|
||||
bool Filesystem::getRealPathType(const std::string &path, FileType &ftype) const
|
||||
{
|
||||
#ifdef LOVE_WINDOWS
|
||||
// make sure non-ASCII paths work.
|
||||
@@ -77,15 +87,85 @@ bool Filesystem::isRealDirectory(const std::string &path) const
|
||||
if (_wstat(wpath.c_str(), &buf) != 0)
|
||||
return false;
|
||||
|
||||
return (buf.st_mode & _S_IFDIR) == _S_IFDIR;
|
||||
if ((buf.st_mode & _S_IFREG) == _S_IFREG)
|
||||
ftype = FILETYPE_FILE;
|
||||
else if ((buf.st_mode & _S_IFDIR) == _S_IFDIR)
|
||||
ftype = FILETYPE_DIRECTORY;
|
||||
else if ((buf.st_mode & _S_IFLNK) == _S_IFLNK)
|
||||
ftype = FILETYPE_SYMLINK;
|
||||
else
|
||||
ftype = FILETYPE_OTHER;
|
||||
#else
|
||||
// Assume POSIX support...
|
||||
struct stat buf;
|
||||
if (stat(path.c_str(), &buf) != 0)
|
||||
return false;
|
||||
|
||||
return S_ISDIR(buf.st_mode) != 0;
|
||||
if (S_ISREG(buf.st_mode))
|
||||
ftype = FILETYPE_FILE;
|
||||
else if (S_ISDIR(buf.st_mode))
|
||||
ftype = FILETYPE_DIRECTORY;
|
||||
else if (S_ISLNK(buf.st_mode))
|
||||
ftype = FILETYPE_SYMLINK;
|
||||
else
|
||||
ftype = FILETYPE_OTHER;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool getContainingDirectory(const std::string &path, std::string &newpath)
|
||||
{
|
||||
size_t index = path.find_last_of("/\\");
|
||||
|
||||
if (index == std::string::npos)
|
||||
return false;
|
||||
|
||||
newpath = path.substr(0, index);
|
||||
|
||||
// Bail if the root has been stripped out.
|
||||
return newpath.find("/\\") != std::string::npos;
|
||||
}
|
||||
|
||||
static bool createDirectoryRaw(const std::string &path)
|
||||
{
|
||||
#ifdef LOVE_WINDOWS
|
||||
std::wstring wpath = to_widestr(path);
|
||||
return CreateDirectoryW(wpath, nullptr) != 0;
|
||||
#else
|
||||
return mkdir(path.c_str(), S_IRWXU) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Filesystem::createRealDirectory(const std::string &path)
|
||||
{
|
||||
FileType ftype = FILETYPE_MAX_ENUM;
|
||||
if (getRealPathType(path, ftype))
|
||||
return ftype == FILETYPE_DIRECTORY;
|
||||
|
||||
std::vector<std::string> createpaths = {path};
|
||||
|
||||
// Find the deepest subdirectory in the given path that actually exists.
|
||||
while (true)
|
||||
{
|
||||
std::string subpath;
|
||||
if (!getContainingDirectory(createpaths[0], subpath))
|
||||
break;
|
||||
|
||||
if (isRealDirectory(subpath))
|
||||
break;
|
||||
|
||||
createpaths.insert(createpaths.begin(), subpath);
|
||||
}
|
||||
|
||||
// Try to create missing subdirectories starting from that existing one.
|
||||
for (const std::string &p : createpaths)
|
||||
{
|
||||
if (!createDirectoryRaw(p))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Filesystem::getExecutablePath() const
|
||||
@@ -127,9 +207,8 @@ STRINGMAP_CLASS_END(Filesystem, Filesystem::FileType, Filesystem::FILETYPE_MAX_E
|
||||
|
||||
STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::CommonPath, Filesystem::COMMONPATH_MAX_ENUM, commonPath)
|
||||
{
|
||||
{ "appidentity", Filesystem::COMMONPATH_APP_IDENTITY },
|
||||
{ "appsavedir", Filesystem::COMMONPATH_APP_SAVEDIR },
|
||||
{ "appdocuments", Filesystem::COMMONPATH_APP_DOCUMENTS },
|
||||
{ "apptemp", Filesystem::COMMONPATH_APP_TEMP },
|
||||
{ "userhome", Filesystem::COMMONPATH_USER_HOME },
|
||||
{ "userappdata", Filesystem::COMMONPATH_USER_APPDATA },
|
||||
{ "userdesktop", Filesystem::COMMONPATH_USER_DESKTOP },
|
||||
|
||||
@@ -73,9 +73,8 @@ public:
|
||||
|
||||
enum CommonPath
|
||||
{
|
||||
COMMONPATH_APP_IDENTITY,
|
||||
COMMONPATH_APP_SAVEDIR,
|
||||
COMMONPATH_APP_DOCUMENTS,
|
||||
COMMONPATH_APP_TEMP,
|
||||
COMMONPATH_USER_HOME,
|
||||
COMMONPATH_USER_APPDATA,
|
||||
COMMONPATH_USER_DESKTOP,
|
||||
@@ -163,6 +162,7 @@ public:
|
||||
virtual bool unmount(const char *archive) = 0;
|
||||
virtual bool unmount(Data *data) = 0;
|
||||
virtual bool unmount(CommonPath path) = 0;
|
||||
virtual bool unmountFullPath(const char *fullpath) = 0;
|
||||
|
||||
/**
|
||||
* Creates a new file.
|
||||
@@ -177,6 +177,9 @@ public:
|
||||
**/
|
||||
virtual FileData *newFileData(const void *data, size_t size, const char *filename) const;
|
||||
|
||||
/**
|
||||
* Gets the full path for the given common path.
|
||||
*/
|
||||
virtual std::string getFullCommonPath(CommonPath path) = 0;
|
||||
|
||||
/**
|
||||
@@ -199,7 +202,7 @@ public:
|
||||
/**
|
||||
* Gets the full path of the save folder.
|
||||
**/
|
||||
virtual const char *getSaveDirectory() = 0;
|
||||
virtual std::string getSaveDirectory() = 0;
|
||||
|
||||
/**
|
||||
* Gets the full path to the directory containing the game source.
|
||||
@@ -285,6 +288,11 @@ public:
|
||||
**/
|
||||
virtual bool isRealDirectory(const std::string &path) const;
|
||||
|
||||
/**
|
||||
* Recursively creates a directory at the given full OS-dependent path.
|
||||
**/
|
||||
virtual bool createRealDirectory(const std::string &path);
|
||||
|
||||
/**
|
||||
* Gets the full platform-dependent path to the executable.
|
||||
**/
|
||||
@@ -296,6 +304,8 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
bool getRealPathType(const std::string &path, FileType &ftype) const;
|
||||
|
||||
// Should we save external or internal for Android
|
||||
bool useExternal;
|
||||
|
||||
|
||||
@@ -69,14 +69,13 @@ bool File::open(Mode mode)
|
||||
throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
|
||||
|
||||
// Check whether the write directory is set.
|
||||
if ((mode == MODE_APPEND || mode == MODE_WRITE) && (PHYSFS_getWriteDir() == nullptr) && !setupWriteDirectory())
|
||||
if ((mode == MODE_APPEND || mode == MODE_WRITE) && !setupWriteDirectory())
|
||||
throw love::Exception("Could not set write directory.");
|
||||
|
||||
// File already open?
|
||||
if (file != nullptr)
|
||||
return false;
|
||||
|
||||
PHYSFS_getLastErrorCode();
|
||||
PHYSFS_File *handle = nullptr;
|
||||
|
||||
switch (mode)
|
||||
|
||||
@@ -71,25 +71,6 @@ namespace filesystem
|
||||
namespace physfs
|
||||
{
|
||||
|
||||
static size_t getDriveDelim(const std::string &input)
|
||||
{
|
||||
for (size_t i = 0; i < input.size(); ++i)
|
||||
if (input[i] == '/' || input[i] == '\\')
|
||||
return i;
|
||||
// Something's horribly wrong
|
||||
return 0;
|
||||
}
|
||||
|
||||
static std::string getDriveRoot(const std::string &input)
|
||||
{
|
||||
return input.substr(0, getDriveDelim(input)+1);
|
||||
}
|
||||
|
||||
static std::string skipDriveRoot(const std::string &input)
|
||||
{
|
||||
return input.substr(getDriveDelim(input)+1);
|
||||
}
|
||||
|
||||
static std::string normalize(const std::string &input)
|
||||
{
|
||||
std::stringstream out;
|
||||
@@ -105,13 +86,18 @@ static std::string normalize(const std::string &input)
|
||||
return out.str();
|
||||
}
|
||||
|
||||
static const Filesystem::CommonPath appCommonPaths[] =
|
||||
{
|
||||
Filesystem::COMMONPATH_APP_SAVEDIR,
|
||||
Filesystem::COMMONPATH_APP_DOCUMENTS
|
||||
};
|
||||
|
||||
static bool isAppCommonPath(Filesystem::CommonPath path)
|
||||
{
|
||||
switch (path)
|
||||
{
|
||||
case Filesystem::COMMONPATH_APP_IDENTITY:
|
||||
case Filesystem::COMMONPATH_APP_SAVEDIR:
|
||||
case Filesystem::COMMONPATH_APP_DOCUMENTS:
|
||||
case Filesystem::COMMONPATH_APP_TEMP:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
@@ -119,8 +105,12 @@ static bool isAppCommonPath(Filesystem::CommonPath path)
|
||||
}
|
||||
|
||||
Filesystem::Filesystem()
|
||||
: fused(false)
|
||||
: appendIdentityToPath(false)
|
||||
, fused(false)
|
||||
, fusedSet(false)
|
||||
, fullPaths()
|
||||
, commonPathMountInfo()
|
||||
, saveDirectoryNeedsMounting(false)
|
||||
{
|
||||
requirePath = {"?.lua", "?/init.lua"};
|
||||
cRequirePath = {"??"};
|
||||
@@ -166,65 +156,65 @@ bool Filesystem::setIdentity(const char *ident, bool appendToPath)
|
||||
if (!PHYSFS_isInit())
|
||||
return false;
|
||||
|
||||
std::string old_save_path = save_path_full;
|
||||
if (ident == nullptr || strlen(ident) == 0)
|
||||
return false;
|
||||
|
||||
// Store the save directory.
|
||||
save_identity = std::string(ident);
|
||||
// Validate whether re-mounting will work.
|
||||
for (CommonPath p : appCommonPaths)
|
||||
{
|
||||
if (!commonPathMountInfo[p].mounted)
|
||||
continue;
|
||||
|
||||
// Generate the relative path to the game save folder.
|
||||
if (fused)
|
||||
save_path_relative = std::string(LOVE_APPDATA_PREFIX) + save_identity;
|
||||
else
|
||||
save_path_relative = std::string(LOVE_APPDATA_PREFIX LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR) + save_identity;
|
||||
// If a file is still open, unmount will fail.
|
||||
std::string fullPath = getFullCommonPath(p);
|
||||
if (!fullPath.empty() && !PHYSFS_canUnmount(fullPath.c_str()))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Generate the full path to the game save folder.
|
||||
save_path_full = std::string(getAppdataDirectory()) + std::string(LOVE_PATH_SEPARATOR) + save_path_relative;
|
||||
save_path_full = normalize(save_path_full);
|
||||
|
||||
#ifdef LOVE_ANDROID
|
||||
if (save_identity == "")
|
||||
save_identity = "unnamed";
|
||||
bool oldMountedCommonPaths[COMMONPATH_MAX_ENUM] = {false};
|
||||
|
||||
std::string storage_path;
|
||||
if (isAndroidSaveExternal())
|
||||
storage_path = SDL_AndroidGetExternalStoragePath();
|
||||
else
|
||||
storage_path = SDL_AndroidGetInternalStoragePath();
|
||||
// We don't want old save paths to accumulate when we set a new identity.
|
||||
for (CommonPath p : appCommonPaths)
|
||||
{
|
||||
oldMountedCommonPaths[p] = commonPathMountInfo[p].mounted;
|
||||
if (commonPathMountInfo[p].mounted)
|
||||
unmount(p);
|
||||
}
|
||||
|
||||
std::string save_directory = storage_path + "/save";
|
||||
// These will be re-populated by getFullCommonPath.
|
||||
for (CommonPath p : appCommonPaths)
|
||||
fullPaths[p].clear();
|
||||
|
||||
save_path_full = storage_path + std::string("/save/") + save_identity;
|
||||
// Store the save directory. getFullCommonPath(COMMONPATH_APP_*) uses this.
|
||||
saveIdentity = std::string(ident);
|
||||
appendIdentityToPath = appendToPath;
|
||||
|
||||
if (!love::android::directoryExists(save_path_full.c_str()) &&
|
||||
!love::android::mkdir(save_path_full.c_str()))
|
||||
SDL_Log("Error: Could not create save directory %s!", save_path_full.c_str());
|
||||
#endif
|
||||
// Try to mount as readwrite without creating missing directories in the
|
||||
// path hierarchy. If this fails, setupWriteDirectory will attempt to create
|
||||
// them and try again.
|
||||
// This is done so the save directory is only created on-demand.
|
||||
if (!mountCommonPathInternal(COMMONPATH_APP_SAVEDIR, nullptr, MOUNT_PERMISSIONS_READWRITE, appendToPath, false))
|
||||
saveDirectoryNeedsMounting = true;
|
||||
|
||||
// We now have something like:
|
||||
// save_identity: game
|
||||
// save_path_relative: ./LOVE/game
|
||||
// save_path_full: C:\Documents and Settings\user\Application Data/LOVE/game
|
||||
|
||||
// We don't want old read-only save paths to accumulate when we set a new
|
||||
// identity.
|
||||
if (!old_save_path.empty())
|
||||
PHYSFS_unmount(old_save_path.c_str());
|
||||
|
||||
// Try to add the save directory to the search path.
|
||||
// (No error on fail, it means that the path doesn't exist).
|
||||
PHYSFS_mount(save_path_full.c_str(), nullptr, appendToPath);
|
||||
|
||||
// HACK: This forces setupWriteDirectory to be called the next time a file
|
||||
// is opened for writing - otherwise it won't be called at all if it was
|
||||
// already called at least once before.
|
||||
PHYSFS_setWriteDir(nullptr);
|
||||
// Mount any other app common paths with directory creation immediately
|
||||
// instead of on-demand, since to get to this point they would have to be
|
||||
// explicitly mounted already beforehand.
|
||||
for (CommonPath p : appCommonPaths)
|
||||
{
|
||||
if (oldMountedCommonPaths[p] && p != COMMONPATH_APP_SAVEDIR)
|
||||
{
|
||||
// TODO: error handling?
|
||||
auto info = commonPathMountInfo[p];
|
||||
mountCommonPathInternal(p, info.mountPoint.c_str(), info.permissions, appendToPath, true);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *Filesystem::getIdentity() const
|
||||
{
|
||||
return save_identity.c_str();
|
||||
return saveIdentity.c_str();
|
||||
}
|
||||
|
||||
bool Filesystem::setSource(const char *source)
|
||||
@@ -233,7 +223,7 @@ bool Filesystem::setSource(const char *source)
|
||||
return false;
|
||||
|
||||
// Check whether directory is already set.
|
||||
if (!game_source.empty())
|
||||
if (!gameSource.empty())
|
||||
return false;
|
||||
|
||||
std::string new_search_path = source;
|
||||
@@ -275,14 +265,14 @@ bool Filesystem::setSource(const char *source)
|
||||
#endif
|
||||
|
||||
// Save the game source.
|
||||
game_source = new_search_path;
|
||||
gameSource = new_search_path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *Filesystem::getSource() const
|
||||
{
|
||||
return game_source.c_str();
|
||||
return gameSource.c_str();
|
||||
}
|
||||
|
||||
bool Filesystem::setupWriteDirectory()
|
||||
@@ -290,54 +280,19 @@ bool Filesystem::setupWriteDirectory()
|
||||
if (!PHYSFS_isInit())
|
||||
return false;
|
||||
|
||||
// These must all be set.
|
||||
if (save_identity.empty() || save_path_full.empty() || save_path_relative.empty())
|
||||
if (!saveDirectoryNeedsMounting)
|
||||
return true;
|
||||
|
||||
if (saveIdentity.empty())
|
||||
return false;
|
||||
|
||||
// We need to make sure the write directory is created. To do that, we also
|
||||
// need to make sure all its parent directories are also created.
|
||||
std::string temp_writedir = getDriveRoot(save_path_full);
|
||||
std::string temp_createdir = skipDriveRoot(save_path_full);
|
||||
|
||||
// On some sandboxed platforms, physfs will break when its write directory
|
||||
// is the root of the drive and it tries to create a folder (even if the
|
||||
// folder's path is in a writable location.) If the user's home folder is
|
||||
// in the save path, we'll try starting from there instead.
|
||||
if (save_path_full.find(getUserDirectory()) == 0)
|
||||
{
|
||||
temp_writedir = getUserDirectory();
|
||||
temp_createdir = save_path_full.substr(getUserDirectory().length());
|
||||
|
||||
// Strip leading '/' characters from the path we want to create.
|
||||
size_t startpos = temp_createdir.find_first_not_of('/');
|
||||
if (startpos != std::string::npos)
|
||||
temp_createdir = temp_createdir.substr(startpos);
|
||||
}
|
||||
|
||||
// Set either '/' or the user's home as a writable directory.
|
||||
// (We must create the save folder before mounting it).
|
||||
if (!PHYSFS_setWriteDir(temp_writedir.c_str()))
|
||||
// Only the save directory is mounted on-demand if it doesn't exist yet.
|
||||
// Other app common paths are immediately re-mounted in setIdentity.
|
||||
bool createdir = true;
|
||||
if (!mountCommonPathInternal(COMMONPATH_APP_SAVEDIR, nullptr, MOUNT_PERMISSIONS_READWRITE, appendIdentityToPath, createdir))
|
||||
return false;
|
||||
|
||||
// Create the save folder. (We're now "at" either '/' or the user's home).
|
||||
if (!createDirectory(temp_createdir.c_str()))
|
||||
{
|
||||
// Clear the write directory in case of error.
|
||||
PHYSFS_setWriteDir(nullptr);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the final write directory.
|
||||
if (!PHYSFS_setWriteDir(save_path_full.c_str()))
|
||||
return false;
|
||||
|
||||
// Add the directory. (Will not be readded if already present).
|
||||
if (!PHYSFS_mount(save_path_full.c_str(), nullptr, 0))
|
||||
{
|
||||
PHYSFS_setWriteDir(nullptr); // Clear the write directory in case of error.
|
||||
return false;
|
||||
}
|
||||
|
||||
saveDirectoryNeedsMounting = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -374,7 +329,7 @@ bool Filesystem::mount(const char *archive, const char *mountpoint, bool appendT
|
||||
|
||||
// Always disallow mounting of files inside the game source, since it
|
||||
// won't work anyway if the game source is a zipped .love file.
|
||||
if (realPath.find(game_source) == 0)
|
||||
if (realPath.find(gameSource) == 0)
|
||||
return false;
|
||||
|
||||
realPath += LOVE_PATH_SEPARATOR;
|
||||
@@ -386,30 +341,40 @@ bool Filesystem::mount(const char *archive, const char *mountpoint, bool appendT
|
||||
|
||||
bool Filesystem::mountFullPath(const char *archive, const char *mountpoint, MountPermissions permissions, bool appendToPath)
|
||||
{
|
||||
if (!PHYSFS_isInit() || !archive || !mountpoint)
|
||||
if (!PHYSFS_isInit() || !archive)
|
||||
return false;
|
||||
|
||||
if (permissions == MOUNT_PERMISSIONS_READWRITE && strlen(mountpoint) == 0)
|
||||
return false;
|
||||
if (permissions == MOUNT_PERMISSIONS_READWRITE)
|
||||
return PHYSFS_mountRW(archive, mountpoint, appendToPath) != 0;
|
||||
|
||||
// TODO: readwrite mount
|
||||
return PHYSFS_mount(archive, mountpoint, appendToPath) != 0;
|
||||
}
|
||||
|
||||
bool Filesystem::mountCommonPath(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath)
|
||||
bool Filesystem::mountCommonPathInternal(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath, bool createDir)
|
||||
{
|
||||
std::string fullpath = getFullCommonPath(path);
|
||||
if (fullpath.empty())
|
||||
return false;
|
||||
|
||||
bool success = mountFullPath(fullpath.c_str(), mountpoint, permissions, appendToPath);
|
||||
|
||||
if (!success && isAppCommonPath(path))
|
||||
if (createDir && isAppCommonPath(path) && !isRealDirectory(fullpath))
|
||||
{
|
||||
|
||||
if (!createRealDirectory(fullpath))
|
||||
return false;
|
||||
}
|
||||
|
||||
return success;
|
||||
if (mountFullPath(fullpath.c_str(), mountpoint, permissions, appendToPath))
|
||||
{
|
||||
std::string mp = mountpoint != nullptr ? mountpoint : "/";
|
||||
commonPathMountInfo[path] = {true, mp, permissions};
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Filesystem::mountCommonPath(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath)
|
||||
{
|
||||
return mountCommonPathInternal(path, mountpoint, permissions, appendToPath, true);
|
||||
}
|
||||
|
||||
bool Filesystem::mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath)
|
||||
@@ -439,8 +404,13 @@ bool Filesystem::unmount(const char *archive)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (PHYSFS_getRealDir(archive) != nullptr)
|
||||
return PHYSFS_unmount(archive) != 0;
|
||||
auto it = std::find(allowedMountPaths.begin(), allowedMountPaths.end(), archive);
|
||||
if (it != allowedMountPaths.end())
|
||||
return unmountFullPath(archive);
|
||||
|
||||
std::string sourceBase = getSourceBaseDirectory();
|
||||
if (isFused() && sourceBase.compare(archive) == 0)
|
||||
return unmountFullPath(archive);
|
||||
|
||||
if (strlen(archive) == 0 || strstr(archive, "..") || strcmp(archive, "/") == 0)
|
||||
return false;
|
||||
@@ -459,13 +429,25 @@ bool Filesystem::unmount(const char *archive)
|
||||
return PHYSFS_unmount(realPath.c_str()) != 0;
|
||||
}
|
||||
|
||||
bool Filesystem::unmountFullPath(const char *fullpath)
|
||||
{
|
||||
if (!PHYSFS_isInit() || !fullpath)
|
||||
return false;
|
||||
|
||||
return PHYSFS_unmount(fullpath) != 0;
|
||||
}
|
||||
|
||||
bool Filesystem::unmount(CommonPath path)
|
||||
{
|
||||
std::string fullpath = getFullCommonPath(path);
|
||||
if (fullpath.empty())
|
||||
return false;
|
||||
|
||||
return unmount(fullpath.c_str());
|
||||
if (!fullpath.empty() && unmountFullPath(fullpath.c_str()))
|
||||
{
|
||||
commonPathMountInfo[path].mounted = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Filesystem::unmount(Data *data)
|
||||
@@ -489,34 +471,60 @@ love::filesystem::File *Filesystem::newFile(const char *filename) const
|
||||
|
||||
std::string Filesystem::getFullCommonPath(CommonPath path)
|
||||
{
|
||||
if (!fullCommonPaths[path].empty())
|
||||
return fullCommonPaths[path];
|
||||
if (!fullPaths[path].empty())
|
||||
return fullPaths[path];
|
||||
|
||||
if (path == COMMONPATH_APP_IDENTITY || path == COMMONPATH_APP_DOCUMENTS || path == COMMONPATH_APP_TEMP)
|
||||
if (isAppCommonPath(path))
|
||||
{
|
||||
|
||||
if (saveIdentity.empty())
|
||||
return fullPaths[path];
|
||||
|
||||
std::string rootpath;
|
||||
switch (path)
|
||||
{
|
||||
case COMMONPATH_APP_SAVEDIR:
|
||||
rootpath = getFullCommonPath(COMMONPATH_USER_APPDATA);
|
||||
break;
|
||||
case COMMONPATH_APP_DOCUMENTS:
|
||||
rootpath = getFullCommonPath(COMMONPATH_USER_DOCUMENTS);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (rootpath.empty())
|
||||
return fullPaths[path];
|
||||
|
||||
std::string suffix;
|
||||
if (isFused())
|
||||
suffix = std::string(LOVE_PATH_SEPARATOR) + saveIdentity;
|
||||
else
|
||||
suffix = std::string(LOVE_PATH_SEPARATOR LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR) + saveIdentity;
|
||||
|
||||
fullPaths[path] = normalize(rootpath + suffix);
|
||||
|
||||
return fullPaths[path];
|
||||
}
|
||||
|
||||
#if defined(LOVE_MACOS) || defined(LOVE_IOS)
|
||||
|
||||
switch (path)
|
||||
{
|
||||
case COMMONPATH_APP_IDENTITY:
|
||||
case COMMONPATH_APP_SAVEDIR:
|
||||
case COMMONPATH_APP_DOCUMENTS:
|
||||
case COMMONPATH_APP_TEMP:
|
||||
// Handled above.
|
||||
break;
|
||||
case COMMONPATH_USER_HOME:
|
||||
fullCommonPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_HOME);
|
||||
fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_HOME);
|
||||
break;
|
||||
case COMMONPATH_USER_APPDATA:
|
||||
fullCommonPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_APPSUPPORT);
|
||||
fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_APPSUPPORT);
|
||||
break;
|
||||
case COMMONPATH_USER_DESKTOP:
|
||||
fullCommonPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_DESKTOP);
|
||||
fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_DESKTOP);
|
||||
break;
|
||||
case COMMONPATH_USER_DOCUMENTS:
|
||||
fullCommonPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_DOCUMENTS);
|
||||
fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_DOCUMENTS);
|
||||
break;
|
||||
case COMMONPATH_MAX_ENUM:
|
||||
break;
|
||||
@@ -529,9 +537,8 @@ std::string Filesystem::getFullCommonPath(CommonPath path)
|
||||
|
||||
switch (path)
|
||||
{
|
||||
case COMMONPATH_APP_IDENTITY:
|
||||
case COMMONPATH_APP_SAVEDIR:
|
||||
case COMMONPATH_APP_DOCUMENTS:
|
||||
case COMMONPATH_APP_TEMP:
|
||||
// Handled above.
|
||||
break;
|
||||
case COMMONPATH_USER_HOME:
|
||||
@@ -552,40 +559,66 @@ std::string Filesystem::getFullCommonPath(CommonPath path)
|
||||
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
fullCommonPaths[path] = to_utf8(winpath);
|
||||
fullPaths[path] = to_utf8(winpath);
|
||||
CoTaskMemFree(winpath);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#elif defined(LOVE_ANDROID)
|
||||
|
||||
std::string storagepath;
|
||||
if (isAndroidSaveExternal())
|
||||
storagepath = SDL_AndroidGetExternalStoragePath();
|
||||
else
|
||||
storagepath = SDL_AndroidGetInternalStoragePath();
|
||||
|
||||
switch (path)
|
||||
{
|
||||
case COMMONPATH_APP_SAVEDIR:
|
||||
case COMMONPATH_APP_DOCUMENTS:
|
||||
// Handled above.
|
||||
break;
|
||||
case COMMONPATH_USER_HOME:
|
||||
fullPaths[path] = normalize(PHYSFS_getUserDir());
|
||||
break;
|
||||
case COMMONPATH_USER_APPDATA:
|
||||
fullPaths[path] = normalize(storagepath + "/save/");
|
||||
break;
|
||||
case COMMONPATH_USER_DESKTOP:
|
||||
// No such thing on Android?
|
||||
break;
|
||||
case COMMONPATH_USER_DOCUMENTS:
|
||||
// TODO: something more idiomatic / useful?
|
||||
fullPaths[path] = normalize(storagepath + "/Documents/");
|
||||
break;
|
||||
case COMMONPATH_MAX_ENUM:
|
||||
break;
|
||||
}
|
||||
|
||||
#elif defined(LOVE_LINUX)
|
||||
|
||||
const char *xdgdir = nullptr;
|
||||
|
||||
switch (path)
|
||||
{
|
||||
case COMMONPATH_APP_IDENTITY:
|
||||
case COMMONPATH_APP_SAVEDIR:
|
||||
case COMMONPATH_APP_DOCUMENTS:
|
||||
case COMMONPATH_APP_TEMP:
|
||||
// Handled above.
|
||||
break;
|
||||
case COMMONPATH_USER_HOME:
|
||||
fullCommonPaths[path] = normalize(PHYSFS_getUserDir());
|
||||
fullPaths[path] = normalize(PHYSFS_getUserDir());
|
||||
break;
|
||||
case COMMONPATH_USER_APPDATA:
|
||||
xdgdir = getenv("XDG_DATA_HOME");
|
||||
if (!xdgdir)
|
||||
fullCommonPaths[path] = normalize(std::string(getUserDirectory()) + "/.local/share/");
|
||||
fullPaths[path] = normalize(std::string(getUserDirectory()) + "/.local/share/");
|
||||
else
|
||||
fullCommonPaths[path] = xdgdir;
|
||||
fullPaths[path] = xdgdir;
|
||||
break;
|
||||
case COMMONPATH_USER_DESKTOP:
|
||||
fullPaths[path] = normalize(std::string(getUserDirectory()) + "/Desktop/");
|
||||
break;
|
||||
case COMMONPATH_USER_DOCUMENTS:
|
||||
fullPaths[path] = normalize(std::string(getUserDirectory()) + "/Documents/");
|
||||
break;
|
||||
case COMMONPATH_MAX_ENUM:
|
||||
break;
|
||||
@@ -593,7 +626,7 @@ std::string Filesystem::getFullCommonPath(CommonPath path)
|
||||
|
||||
#endif
|
||||
|
||||
return fullCommonPaths[path];
|
||||
return fullPaths[path];
|
||||
}
|
||||
|
||||
const char *Filesystem::getWorkingDirectory()
|
||||
@@ -621,58 +654,22 @@ const char *Filesystem::getWorkingDirectory()
|
||||
|
||||
std::string Filesystem::getUserDirectory()
|
||||
{
|
||||
#if defined(LOVE_IOS) || defined(LOVE_MACOS)
|
||||
// PHYSFS_getUserDir doesn't give exactly the path we want on iOS.
|
||||
static std::string userDir = normalize(apple::getUserDirectory(apple::USER_DIRECTORY_HOME));
|
||||
#else
|
||||
static std::string userDir = normalize(PHYSFS_getUserDir());
|
||||
#endif
|
||||
|
||||
return userDir;
|
||||
return getFullCommonPath(COMMONPATH_USER_HOME);
|
||||
}
|
||||
|
||||
std::string Filesystem::getAppdataDirectory()
|
||||
{
|
||||
if (appdata.empty())
|
||||
{
|
||||
#ifdef LOVE_WINDOWS_UWP
|
||||
appdata = getUserDirectory();
|
||||
#elif defined(LOVE_WINDOWS)
|
||||
PWSTR path = nullptr;
|
||||
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &path)))
|
||||
{
|
||||
appdata = to_utf8(path);
|
||||
CoTaskMemFree(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
wchar_t *w_appdata = _wgetenv(L"APPDATA");
|
||||
appdata = to_utf8(w_appdata);
|
||||
}
|
||||
replace_char(appdata, '\\', '/');
|
||||
#elif defined(LOVE_MACOS) || defined(LOVE_IOS)
|
||||
appdata = normalize(apple::getUserDirectory(apple::USER_DIRECTORY_APPSUPPORT));
|
||||
#elif defined(LOVE_LINUX)
|
||||
char *xdgdatahome = getenv("XDG_DATA_HOME");
|
||||
if (!xdgdatahome)
|
||||
appdata = normalize(std::string(getUserDirectory()) + "/.local/share/");
|
||||
else
|
||||
appdata = xdgdatahome;
|
||||
#else
|
||||
appdata = getUserDirectory();
|
||||
#endif
|
||||
}
|
||||
return appdata;
|
||||
return getFullCommonPath(COMMONPATH_USER_APPDATA);
|
||||
}
|
||||
|
||||
const char *Filesystem::getSaveDirectory()
|
||||
std::string Filesystem::getSaveDirectory()
|
||||
{
|
||||
return save_path_full.c_str();
|
||||
return getFullCommonPath(COMMONPATH_APP_SAVEDIR);
|
||||
}
|
||||
|
||||
std::string Filesystem::getSourceBaseDirectory() const
|
||||
{
|
||||
size_t source_len = game_source.length();
|
||||
size_t source_len = gameSource.length();
|
||||
|
||||
if (source_len == 0)
|
||||
return "";
|
||||
@@ -681,9 +678,9 @@ std::string Filesystem::getSourceBaseDirectory() const
|
||||
// symbols (i.e. '..' and '.')
|
||||
#ifdef LOVE_WINDOWS
|
||||
// In windows, delimiters can be either '/' or '\'.
|
||||
size_t base_end_pos = game_source.find_last_of("/\\", source_len - 2);
|
||||
size_t base_end_pos = gameSource.find_last_of("/\\", source_len - 2);
|
||||
#else
|
||||
size_t base_end_pos = game_source.find_last_of('/', source_len - 2);
|
||||
size_t base_end_pos = gameSource.find_last_of('/', source_len - 2);
|
||||
#endif
|
||||
|
||||
if (base_end_pos == std::string::npos)
|
||||
@@ -693,7 +690,7 @@ std::string Filesystem::getSourceBaseDirectory() const
|
||||
if (base_end_pos == 0)
|
||||
base_end_pos = 1;
|
||||
|
||||
return game_source.substr(0, base_end_pos);
|
||||
return gameSource.substr(0, base_end_pos);
|
||||
}
|
||||
|
||||
std::string Filesystem::getRealDirectory(const char *filename) const
|
||||
@@ -739,7 +736,7 @@ bool Filesystem::createDirectory(const char *dir)
|
||||
if (!PHYSFS_isInit())
|
||||
return false;
|
||||
|
||||
if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
|
||||
if (!setupWriteDirectory())
|
||||
return false;
|
||||
|
||||
if (!PHYSFS_mkdir(dir))
|
||||
@@ -753,7 +750,7 @@ bool Filesystem::remove(const char *file)
|
||||
if (!PHYSFS_isInit())
|
||||
return false;
|
||||
|
||||
if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
|
||||
if (!setupWriteDirectory())
|
||||
return false;
|
||||
|
||||
if (!PHYSFS_delete(file))
|
||||
|
||||
@@ -69,6 +69,7 @@ public:
|
||||
bool unmount(const char *archive) override;
|
||||
bool unmount(Data *data) override;
|
||||
bool unmount(CommonPath path) override;
|
||||
bool unmountFullPath(const char *fullpath) override;
|
||||
|
||||
love::filesystem::File *newFile(const char *filename) const override;
|
||||
|
||||
@@ -76,7 +77,7 @@ public:
|
||||
const char *getWorkingDirectory() override;
|
||||
std::string getUserDirectory() override;
|
||||
std::string getAppdataDirectory() override;
|
||||
const char *getSaveDirectory() override;
|
||||
std::string getSaveDirectory() override;
|
||||
std::string getSourceBaseDirectory() const override;
|
||||
|
||||
std::string getRealDirectory(const char *filename) const override;
|
||||
@@ -103,26 +104,26 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
struct CommonPathMountInfo
|
||||
{
|
||||
bool mounted;
|
||||
std::string mountPoint;
|
||||
MountPermissions permissions;
|
||||
};
|
||||
|
||||
bool mountCommonPathInternal(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath, bool createDir);
|
||||
|
||||
// Contains the current working directory (UTF8).
|
||||
std::string cwd;
|
||||
|
||||
// %APPDATA% on Windows.
|
||||
std::string appdata;
|
||||
|
||||
// This name will be used to create the folder
|
||||
// in the appdata/userdata folder.
|
||||
std::string save_identity;
|
||||
|
||||
// Full and relative paths of the game save folder.
|
||||
// (Relative to the %APPDATA% folder, meaning that the
|
||||
// relative string will look something like: ./LOVE/game)
|
||||
std::string save_path_relative, save_path_full;
|
||||
// This name will be used to create the folder in the appdata folder.
|
||||
std::string saveIdentity;
|
||||
bool appendIdentityToPath;
|
||||
|
||||
// The full path to the source of the game.
|
||||
std::string game_source;
|
||||
std::string gameSource;
|
||||
|
||||
// Allow saving outside of the LOVE_APPDATA_FOLDER
|
||||
// for release 'builds'
|
||||
// Allow saving outside of the LOVE_APPDATA_FOLDER for release 'builds'
|
||||
bool fused;
|
||||
bool fusedSet;
|
||||
|
||||
@@ -134,7 +135,11 @@ private:
|
||||
|
||||
std::map<std::string, StrongRef<Data>> mountedData;
|
||||
|
||||
std::string fullCommonPaths[COMMONPATH_MAX_ENUM];
|
||||
std::string fullPaths[COMMONPATH_MAX_ENUM];
|
||||
|
||||
CommonPathMountInfo commonPathMountInfo[COMMONPATH_MAX_ENUM];
|
||||
|
||||
bool saveDirectoryNeedsMounting;
|
||||
|
||||
}; // Filesystem
|
||||
|
||||
|
||||
@@ -205,6 +205,13 @@ int w_unmount(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_unmountFullPath(lua_State *L)
|
||||
{
|
||||
const char *fullpath = luaL_checkstring(L, 1);
|
||||
luax_pushboolean(L, instance()->unmountFullPath(fullpath));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_unmountCommonPath(lua_State *L)
|
||||
{
|
||||
const char *commonpathstr = luaL_checkstring(L, 1);
|
||||
@@ -415,7 +422,7 @@ int w_getAppdataDirectory(lua_State *L)
|
||||
|
||||
int w_getSaveDirectory(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, instance()->getSaveDirectory());
|
||||
luax_pushstring(L, instance()->getSaveDirectory());
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -907,11 +914,12 @@ static const luaL_Reg functions[] =
|
||||
{ "getSource", w_getSource },
|
||||
{ "mount", w_mount },
|
||||
{ "mountFullPath", w_mountFullPath },
|
||||
// { "mountCommonPath", w_mountCommonPath },
|
||||
{ "mountCommonPath", w_mountCommonPath },
|
||||
{ "unmount", w_unmount },
|
||||
// { "unmountCommonPath", w_unmountCommonPath },
|
||||
{ "unmountFullPath", w_unmountFullPath },
|
||||
{ "unmountCommonPath", w_unmountCommonPath },
|
||||
{ "newFile", w_newFile },
|
||||
// { "getFullCommonPath", w_getFullCommonPath },
|
||||
{ "getFullCommonPath", w_getFullCommonPath },
|
||||
{ "getWorkingDirectory", w_getWorkingDirectory },
|
||||
{ "getUserDirectory", w_getUserDirectory },
|
||||
{ "getAppdataDirectory", w_getAppdataDirectory },
|
||||
|
||||
Reference in New Issue
Block a user