tibia-client/src/framework/core/resources.cpp

175 lines
4.8 KiB
C++
Raw Normal View History

2011-07-13 23:12:36 +02:00
#include <global.h>
2011-07-17 02:13:53 +02:00
#include "resources.h"
#include "platform.h"
2011-07-13 23:12:36 +02:00
#include <boost/algorithm/string.hpp>
#include <physfs.h>
2011-04-06 21:46:58 +02:00
Resources g_resources;
2011-04-06 21:46:58 +02:00
void Resources::init(const char *argv0)
{
PHYSFS_init(argv0);
2011-05-19 19:11:05 +02:00
// try to find data directory
std::string dir;
std::string baseDir = PHYSFS_getBaseDir();
2011-07-13 23:12:36 +02:00
std::string possibleDirs[] = { "data",
baseDir + "data",
baseDir + "../data",
baseDir + "../share/otclient/data",
"" };
2011-05-19 19:11:05 +02:00
2011-05-19 20:46:40 +02:00
bool found = false;
foreach(dir, possibleDirs) {
if(g_resources.addToSearchPath(dir)) {
2011-07-13 23:12:36 +02:00
info("Using data directory: ", dir.c_str());
2011-05-19 20:46:40 +02:00
found = true;
2011-05-19 19:11:05 +02:00
break;
2011-05-19 20:46:40 +02:00
}
}
if(!found)
2011-07-13 23:12:36 +02:00
fatal("ERROR: could not find data directory");
2011-05-19 19:11:05 +02:00
// setup write directory
dir = Platform::getAppUserDir();
if(g_resources.setWriteDir(dir))
g_resources.addToSearchPath(dir);
else
2011-07-13 23:12:36 +02:00
error("ERROR: could not setup write directory");
}
2011-04-06 21:46:58 +02:00
void Resources::terminate()
{
PHYSFS_deinit();
}
2011-04-06 21:46:58 +02:00
bool Resources::setWriteDir(const std::string& path)
{
2011-05-19 19:11:05 +02:00
if(!PHYSFS_setWriteDir(path.c_str()))
return false;
return true;
}
2011-04-06 21:46:58 +02:00
bool Resources::addToSearchPath(const std::string& path, bool insertInFront /*= true*/)
{
2011-05-19 19:11:05 +02:00
if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1))
return false;
return true;
}
2011-05-19 19:11:05 +02:00
void Resources::addPackagesToSearchPath(const std::string &packagesDirectory, const std::string &packageExtension, bool append)
{
2011-05-19 19:11:05 +02:00
auto files = listDirectoryFiles(resolvePath(packagesDirectory));
foreach(const std::string& file, files) {
if(boost::ends_with(file, packageExtension))
addToSearchPath(packagesDirectory + "/" + file, !append);
}
}
2011-05-19 19:11:05 +02:00
bool Resources::fileExists(const std::string& fileName)
{
2011-05-19 19:11:05 +02:00
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str()));
}
2011-05-19 19:11:05 +02:00
bool Resources::directoryExists(const std::string& directoryName)
{
return (PHYSFS_exists(resolvePath(directoryName).c_str()) && PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
}
2011-05-19 19:11:05 +02:00
bool Resources::loadFile(const std::string& fileName, std::iostream& out)
{
2011-05-21 20:40:06 +02:00
std::string fullPath = resolvePath(fileName);
2011-05-19 19:11:05 +02:00
out.clear(std::ios::goodbit);
2011-05-21 20:40:06 +02:00
PHYSFS_file *file = PHYSFS_openRead(fullPath.c_str());
2011-05-19 19:11:05 +02:00
if(!file) {
2011-07-13 23:12:36 +02:00
error("ERROR: Failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError());
2011-05-19 19:11:05 +02:00
out.clear(std::ios::failbit);
return false;
} else {
int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) {
char *buffer = new char[fileSize];
PHYSFS_read(file, (void*)buffer, 1, fileSize);
out.write(buffer, fileSize);
2011-05-22 00:24:10 +02:00
delete[] buffer;
2011-05-19 19:11:05 +02:00
} else
out.clear(std::ios::eofbit);
PHYSFS_close(file);
out.seekg(0, std::ios::beg);
return true;
}
}
2011-04-06 22:18:00 +02:00
bool Resources::saveFile(const std::string &fileName, const uchar *data, uint size)
{
2011-05-19 19:11:05 +02:00
PHYSFS_file *file = PHYSFS_openWrite(resolvePath(fileName).c_str());
if(!file) {
2011-07-13 23:12:36 +02:00
error("ERROR: Failed to save file '",fileName,"': ",PHYSFS_getLastError());
return false;
}
PHYSFS_write(file, (void*)data, size, 1);
PHYSFS_close(file);
return true;
}
2011-05-19 19:11:05 +02:00
bool Resources::saveFile(const std::string &fileName, std::istream& in)
{
2011-05-19 19:11:05 +02:00
std::streampos oldPos = in.tellg();
in.seekg(0, std::ios::end);
std::streampos size = in.tellg();
in.seekg(0, std::ios::beg);
char *buffer = new char[size];
in.read(buffer, size);
bool ret = saveFile(fileName, (const uchar*)buffer, size);
delete[] buffer;
in.seekg(oldPos, std::ios::beg);
return ret;
}
bool Resources::deleteFile(const std::string& fileName)
{
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
}
2011-03-26 21:53:08 +01:00
2011-05-19 19:11:05 +02:00
std::list<std::string> Resources::listDirectoryFiles(const std::string& directoryPath)
2011-03-26 21:53:08 +01:00
{
std::list<std::string> files;
2011-05-19 19:11:05 +02:00
char **rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
2011-03-26 21:53:08 +01:00
for(char **i = rc; *i != NULL; i++)
files.push_back(*i);
PHYSFS_freeList(rc);
return files;
}
2011-05-19 19:11:05 +02:00
void Resources::pushCurrentPath(const std::string &currentPath)
{
m_currentPaths.push(currentPath);
}
void Resources::popCurrentPath()
{
m_currentPaths.pop();
}
std::string Resources::resolvePath(const std::string& path)
{
std::string fullPath;
if(boost::starts_with(path, "/"))
fullPath = path.substr(1);
else {
if(m_currentPaths.size() > 0) {
std::string currentPath = m_currentPaths.top();
if(currentPath.length() > 0)
fullPath += currentPath + "/";
}
fullPath += path;
}
return fullPath;
}