Avoid hard crashes when calling love.filesystem functions if love.filesystem.init was never called.

This only applies when liblove is used directly, as love.filesystem.init is called automatically when the love executable starts up.
This commit is contained in:
Alex Szpakowski
2015-12-24 22:53:48 -05:00
parent 67fe29b30e
commit b56a0cfc3d
2 changed files with 41 additions and 0 deletions
+3
View File
@@ -57,6 +57,9 @@ bool File::open(Mode mode)
if (mode == MODE_CLOSED)
return true;
if (!PHYSFS_isInit())
throw love::Exception("PhysFS is not initialized.");
// File must exist if read mode.
if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
@@ -571,6 +571,9 @@ std::string Filesystem::getSourceBaseDirectory() const
std::string Filesystem::getRealDirectory(const char *filename) const
{
if (!PHYSFS_isInit())
throw love::Exception("PhysFS is not initialized.");
const char *dir = PHYSFS_getRealDir(filename);
if (dir == nullptr)
@@ -581,11 +584,17 @@ std::string Filesystem::getRealDirectory(const char *filename) const
bool Filesystem::exists(const char *path) const
{
if (!PHYSFS_isInit())
return false;
return PHYSFS_exists(path) != 0;
}
bool Filesystem::isDirectory(const char *dir) const
{
if (!PHYSFS_isInit())
return false;
#ifdef LOVE_USE_PHYSFS_2_1
PHYSFS_Stat stat = {};
if (PHYSFS_stat(dir, &stat))
@@ -599,11 +608,17 @@ bool Filesystem::isDirectory(const char *dir) const
bool Filesystem::isFile(const char *file) const
{
if (!PHYSFS_isInit())
return false;
return PHYSFS_exists(file) && !isDirectory(file);
}
bool Filesystem::isSymlink(const char *filename) const
{
if (!PHYSFS_isInit())
return false;
#ifdef LOVE_USE_PHYSFS_2_1
PHYSFS_Stat stat = {};
if (PHYSFS_stat(filename, &stat))
@@ -617,21 +632,29 @@ bool Filesystem::isSymlink(const char *filename) const
bool Filesystem::createDirectory(const char *dir)
{
if (!PHYSFS_isInit())
return false;
if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
return false;
if (!PHYSFS_mkdir(dir))
return false;
return true;
}
bool Filesystem::remove(const char *file)
{
if (!PHYSFS_isInit())
return false;
if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
return false;
if (!PHYSFS_delete(file))
return false;
return true;
}
@@ -669,8 +692,14 @@ void Filesystem::append(const char *filename, const void *data, int64 size) cons
void Filesystem::getDirectoryItems(const char *dir, std::vector<std::string> &items)
{
if (!PHYSFS_isInit())
return;
char **rc = PHYSFS_enumerateFiles(dir);
if (rc == nullptr)
return;
for (char **i = rc; *i != 0; i++)
items.push_back(*i);
@@ -681,6 +710,9 @@ int64 Filesystem::getLastModified(const char *filename) const
{
PHYSFS_sint64 time = -1;
if (!PHYSFS_isInit())
return -1;
#ifdef LOVE_USE_PHYSFS_2_1
PHYSFS_Stat stat = {};
if (PHYSFS_stat(filename, &stat))
@@ -704,6 +736,9 @@ int64 Filesystem::getSize(const char *filename) const
void Filesystem::setSymlinksEnabled(bool enable)
{
if (!PHYSFS_isInit())
return;
if (!enable)
{
PHYSFS_Version version = {};
@@ -720,6 +755,9 @@ void Filesystem::setSymlinksEnabled(bool enable)
bool Filesystem::areSymlinksEnabled() const
{
if (!PHYSFS_isInit())
return false;
return PHYSFS_symbolicLinksPermitted() != 0;
}