summaryrefslogtreecommitdiff
path: root/src/resources/resourcemanager.cpp
diff options
context:
space:
mode:
authorJared Adams <jaxad0127@gmail.com>2010-08-13 14:45:52 -0600
committerJared Adams <jaxad0127@gmail.com>2010-08-13 18:43:16 -0600
commit8eadc834ed3e8439836f7dc87390a56a1164ec11 (patch)
tree36cf9915914517b137d488a40d1f61265867723d /src/resources/resourcemanager.cpp
parent6d315d0e61f419636021476582a49ab0b1f1b5e9 (diff)
downloadMana-8eadc834ed3e8439836f7dc87390a56a1164ec11.tar.gz
Mana-8eadc834ed3e8439836f7dc87390a56a1164ec11.tar.bz2
Mana-8eadc834ed3e8439836f7dc87390a56a1164ec11.tar.xz
Mana-8eadc834ed3e8439836f7dc87390a56a1164ec11.zip
Simplify handling of compressed files
ResourceManager will now check for ".gz" and act appropriately (unless told not to). Compression handling functions are now in new utils/zlib files, along with a function to load a file from drive, uncompressing it if it ends in ".gz". Reviewed-by: Freeyorp
Diffstat (limited to 'src/resources/resourcemanager.cpp')
-rw-r--r--src/resources/resourcemanager.cpp45
1 files changed, 36 insertions, 9 deletions
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index f785f20a..c63b626e 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -31,6 +31,8 @@
#include "resources/soundeffect.h"
#include "resources/spritedef.h"
+#include "utils/zlib.h"
+
#include <physfs.h>
#include <SDL_image.h>
@@ -411,34 +413,59 @@ void ResourceManager::deleteInstance()
instance = NULL;
}
-void *ResourceManager::loadFile(const std::string &fileName, int &fileSize)
+void *ResourceManager::loadFile(const std::string &filename, int &filesize,
+ bool inflate)
{
// Attempt to open the specified file using PhysicsFS
- PHYSFS_file *file = PHYSFS_openRead(fileName.c_str());
+ PHYSFS_file *file = PHYSFS_openRead(filename.c_str());
// If the handler is an invalid pointer indicate failure
if (file == NULL)
{
logger->log("Warning: Failed to load %s: %s",
- fileName.c_str(), PHYSFS_getLastError());
+ filename.c_str(), PHYSFS_getLastError());
return NULL;
}
// Log the real dir of the file
- logger->log("Loaded %s/%s", PHYSFS_getRealDir(fileName.c_str()),
- fileName.c_str());
+ logger->log("Loaded %s/%s", PHYSFS_getRealDir(filename.c_str()),
+ filename.c_str());
// Get the size of the file
- fileSize = PHYSFS_fileLength(file);
+ filesize = PHYSFS_fileLength(file);
// Allocate memory and load the file
- void *buffer = malloc(fileSize);
- PHYSFS_read(file, buffer, 1, fileSize);
+ void *buffer = malloc(filesize);
+ PHYSFS_read(file, buffer, 1, filesize);
// Close the file and let the user deallocate the memory
PHYSFS_close(file);
- return buffer;
+ unsigned char *inflated;
+ unsigned int inflatedSize;
+
+ if (inflate && filename.find(".gz", filename.length() - 3)
+ != std::string::npos)
+ {
+ // Inflate the gzipped map data
+ inflatedSize =
+ inflateMemory((unsigned char*) buffer, filesize, inflated);
+ free(buffer);
+
+ if (inflated == NULL)
+ {
+ logger->log("Could not decompress file: %s",
+ filename.c_str());
+ return NULL;
+ }
+
+ filesize = inflatedSize;
+ return inflated;
+ }
+ else
+ {
+ return buffer;
+ }
}
bool ResourceManager::copyFile(const std::string &src, const std::string &dst)