Browse Source

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.
Alex Szpakowski 9 years ago
parent
commit
b56a0cfc3d

+ 3 - 0
src/modules/filesystem/physfs/File.cpp

@@ -57,6 +57,9 @@ bool File::open(Mode mode)
 	if (mode == MODE_CLOSED)
 	if (mode == MODE_CLOSED)
 		return true;
 		return true;
 
 
+	if (!PHYSFS_isInit())
+		throw love::Exception("PhysFS is not initialized.");
+
 	// File must exist if read mode.
 	// File must exist if read mode.
 	if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
 	if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str()))
 		throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
 		throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());

+ 38 - 0
src/modules/filesystem/physfs/Filesystem.cpp

@@ -571,6 +571,9 @@ std::string Filesystem::getSourceBaseDirectory() const
 
 
 std::string Filesystem::getRealDirectory(const char *filename) 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);
 	const char *dir = PHYSFS_getRealDir(filename);
 
 
 	if (dir == nullptr)
 	if (dir == nullptr)
@@ -581,11 +584,17 @@ std::string Filesystem::getRealDirectory(const char *filename) const
 
 
 bool Filesystem::exists(const char *path) const
 bool Filesystem::exists(const char *path) const
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 	return PHYSFS_exists(path) != 0;
 	return PHYSFS_exists(path) != 0;
 }
 }
 
 
 bool Filesystem::isDirectory(const char *dir) const
 bool Filesystem::isDirectory(const char *dir) const
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 #ifdef LOVE_USE_PHYSFS_2_1
 #ifdef LOVE_USE_PHYSFS_2_1
 	PHYSFS_Stat stat = {};
 	PHYSFS_Stat stat = {};
 	if (PHYSFS_stat(dir, &stat))
 	if (PHYSFS_stat(dir, &stat))
@@ -599,11 +608,17 @@ bool Filesystem::isDirectory(const char *dir) const
 
 
 bool Filesystem::isFile(const char *file) const
 bool Filesystem::isFile(const char *file) const
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 	return PHYSFS_exists(file) && !isDirectory(file);
 	return PHYSFS_exists(file) && !isDirectory(file);
 }
 }
 
 
 bool Filesystem::isSymlink(const char *filename) const
 bool Filesystem::isSymlink(const char *filename) const
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 #ifdef LOVE_USE_PHYSFS_2_1
 #ifdef LOVE_USE_PHYSFS_2_1
 	PHYSFS_Stat stat = {};
 	PHYSFS_Stat stat = {};
 	if (PHYSFS_stat(filename, &stat))
 	if (PHYSFS_stat(filename, &stat))
@@ -617,21 +632,29 @@ bool Filesystem::isSymlink(const char *filename) const
 
 
 bool Filesystem::createDirectory(const char *dir)
 bool Filesystem::createDirectory(const char *dir)
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 	if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
 	if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
 		return false;
 		return false;
 
 
 	if (!PHYSFS_mkdir(dir))
 	if (!PHYSFS_mkdir(dir))
 		return false;
 		return false;
+
 	return true;
 	return true;
 }
 }
 
 
 bool Filesystem::remove(const char *file)
 bool Filesystem::remove(const char *file)
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 	if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
 	if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
 		return false;
 		return false;
 
 
 	if (!PHYSFS_delete(file))
 	if (!PHYSFS_delete(file))
 		return false;
 		return false;
+
 	return true;
 	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)
 void Filesystem::getDirectoryItems(const char *dir, std::vector<std::string> &items)
 {
 {
+	if (!PHYSFS_isInit())
+		return;
+
 	char **rc = PHYSFS_enumerateFiles(dir);
 	char **rc = PHYSFS_enumerateFiles(dir);
 
 
+	if (rc == nullptr)
+		return;
+
 	for (char **i = rc; *i != 0; i++)
 	for (char **i = rc; *i != 0; i++)
 		items.push_back(*i);
 		items.push_back(*i);
 
 
@@ -681,6 +710,9 @@ int64 Filesystem::getLastModified(const char *filename) const
 {
 {
 	PHYSFS_sint64 time = -1;
 	PHYSFS_sint64 time = -1;
 
 
+	if (!PHYSFS_isInit())
+		return -1;
+
 #ifdef LOVE_USE_PHYSFS_2_1
 #ifdef LOVE_USE_PHYSFS_2_1
 	PHYSFS_Stat stat = {};
 	PHYSFS_Stat stat = {};
 	if (PHYSFS_stat(filename, &stat))
 	if (PHYSFS_stat(filename, &stat))
@@ -704,6 +736,9 @@ int64 Filesystem::getSize(const char *filename) const
 
 
 void Filesystem::setSymlinksEnabled(bool enable)
 void Filesystem::setSymlinksEnabled(bool enable)
 {
 {
+	if (!PHYSFS_isInit())
+		return;
+
 	if (!enable)
 	if (!enable)
 	{
 	{
 		PHYSFS_Version version = {};
 		PHYSFS_Version version = {};
@@ -720,6 +755,9 @@ void Filesystem::setSymlinksEnabled(bool enable)
 
 
 bool Filesystem::areSymlinksEnabled() const
 bool Filesystem::areSymlinksEnabled() const
 {
 {
+	if (!PHYSFS_isInit())
+		return false;
+
 	return PHYSFS_symbolicLinksPermitted() != 0;
 	return PHYSFS_symbolicLinksPermitted() != 0;
 }
 }