2012-04-29 03:07:47 +02:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2012-04-29 03:07:47 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "resourcemanager.h"
|
|
|
|
#include "filestream.h"
|
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
#include <framework/core/application.h>
|
|
|
|
#include <framework/luaengine/luainterface.h>
|
2012-04-29 03:07:47 +02:00
|
|
|
|
|
|
|
#include <physfs.h>
|
2012-07-17 02:30:15 +02:00
|
|
|
#include <boost/filesystem.hpp>
|
2012-04-29 03:07:47 +02:00
|
|
|
|
|
|
|
ResourceManager g_resources;
|
|
|
|
|
|
|
|
void ResourceManager::init(const char *argv0)
|
|
|
|
{
|
|
|
|
PHYSFS_init(argv0);
|
2012-10-24 22:03:15 +02:00
|
|
|
PHYSFS_permitSymbolicLinks(1);
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceManager::terminate()
|
|
|
|
{
|
|
|
|
PHYSFS_deinit();
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
bool ResourceManager::discoverWorkDir(const std::string& existentFile)
|
2012-06-20 02:15:56 +02:00
|
|
|
{
|
|
|
|
// search for modules directory
|
2012-10-24 22:03:15 +02:00
|
|
|
std::string possiblePaths[] = { g_resources.getCurrentDir(),
|
2013-01-08 22:31:41 +01:00
|
|
|
g_resources.getBaseDir(),
|
2013-01-23 16:05:18 +01:00
|
|
|
g_resources.getBaseDir() + "../",
|
2013-01-23 16:43:39 +01:00
|
|
|
g_resources.getBaseDir() + "../share/" + g_app.getCompactName() + "/" };
|
2012-06-20 02:15:56 +02:00
|
|
|
bool found = false;
|
|
|
|
for(const std::string& dir : possiblePaths) {
|
2012-10-24 22:03:15 +02:00
|
|
|
if(!PHYSFS_addToSearchPath(dir.c_str(), 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(PHYSFS_exists(existentFile.c_str())) {
|
2012-06-20 02:15:56 +02:00
|
|
|
g_logger.debug(stdext::format("Found work dir at '%s'", dir.c_str()));
|
|
|
|
m_workDir = dir;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-24 22:03:15 +02:00
|
|
|
PHYSFS_removeFromSearchPath(dir.c_str());
|
2012-06-20 02:15:56 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
return found;
|
2012-06-20 02:15:56 +02:00
|
|
|
}
|
|
|
|
|
2012-07-16 21:35:14 +02:00
|
|
|
bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
|
|
|
std::string userDir = PHYSFS_getUserDir();
|
2012-06-20 02:15:56 +02:00
|
|
|
std::string dirName;
|
|
|
|
#ifndef WIN32
|
|
|
|
dirName = stdext::format(".%s", appWriteDirName);
|
|
|
|
#else
|
|
|
|
dirName = appWriteDirName;
|
|
|
|
#endif
|
2012-04-29 03:07:47 +02:00
|
|
|
std::string writeDir = userDir + dirName;
|
2013-01-08 22:31:41 +01:00
|
|
|
|
2012-10-24 22:03:15 +02:00
|
|
|
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
|
|
|
|
if(!PHYSFS_setWriteDir(userDir.c_str()) || !PHYSFS_mkdir(dirName.c_str())) {
|
|
|
|
g_logger.error(stdext::format("Unable to create write directory '%s': %s", writeDir, PHYSFS_getLastError()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 21:35:14 +02:00
|
|
|
return setWriteDir(writeDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::setWriteDir(const std::string& writeDir, bool create)
|
|
|
|
{
|
2012-04-29 03:07:47 +02:00
|
|
|
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
|
2012-07-16 21:35:14 +02:00
|
|
|
g_logger.error(stdext::format("Unable to set write directory '%s': %s", writeDir, PHYSFS_getLastError()));
|
|
|
|
return false;
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
2012-07-16 21:35:14 +02:00
|
|
|
|
|
|
|
if(!m_writeDir.empty())
|
|
|
|
removeSearchPath(m_writeDir);
|
|
|
|
|
|
|
|
m_writeDir = writeDir;
|
|
|
|
|
|
|
|
if(!addSearchPath(writeDir))
|
2012-07-17 21:31:55 +02:00
|
|
|
g_logger.error(stdext::format("Unable to add write '%s' directory to search path", writeDir));
|
2012-07-16 21:35:14 +02:00
|
|
|
|
2012-04-29 03:07:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:35:14 +02:00
|
|
|
bool ResourceManager::addSearchPath(const std::string& path, bool pushFront)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
2012-08-23 09:17:19 +02:00
|
|
|
std::string savePath = path;
|
|
|
|
if(!PHYSFS_addToSearchPath(path.c_str(), pushFront ? 0 : 1)) {
|
|
|
|
bool found = false;
|
|
|
|
for(std::string searchPath : m_searchPaths) {
|
|
|
|
std::string newPath = searchPath + path;
|
|
|
|
if(PHYSFS_addToSearchPath(newPath.c_str(), pushFront ? 0 : 1)) {
|
|
|
|
savePath = newPath;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found) {
|
2012-10-24 22:03:15 +02:00
|
|
|
//g_logger.error(stdext::format("Could not add '%s' to directory search path. Reason %s", path, PHYSFS_getLastError()));
|
2012-08-23 09:17:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 21:35:14 +02:00
|
|
|
if(pushFront)
|
2012-08-23 09:17:19 +02:00
|
|
|
m_searchPaths.push_front(savePath);
|
2012-07-16 21:35:14 +02:00
|
|
|
else
|
2012-08-23 09:17:19 +02:00
|
|
|
m_searchPaths.push_back(savePath);
|
2012-04-29 03:07:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:35:14 +02:00
|
|
|
bool ResourceManager::removeSearchPath(const std::string& path)
|
2012-06-19 10:46:49 +02:00
|
|
|
{
|
|
|
|
if(!PHYSFS_removeFromSearchPath(path.c_str()))
|
|
|
|
return false;
|
2012-07-16 21:35:14 +02:00
|
|
|
auto it = std::find(m_searchPaths.begin(), m_searchPaths.end(), path);
|
|
|
|
assert(it != m_searchPaths.end());
|
|
|
|
m_searchPaths.erase(it);
|
2012-06-19 10:46:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:35:14 +02:00
|
|
|
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
2012-07-16 21:35:14 +02:00
|
|
|
auto files = listDirectoryFiles(packagesDir);
|
|
|
|
for(auto it = files.rbegin(); it != files.rend(); ++it) {
|
|
|
|
const std::string& file = *it;
|
2012-08-01 09:49:09 +02:00
|
|
|
if(!stdext::ends_with(file, packageExt))
|
2012-07-16 21:35:14 +02:00
|
|
|
continue;
|
|
|
|
std::string package = getRealDir(packagesDir) + "/" + file;
|
|
|
|
if(!addSearchPath(package, true))
|
|
|
|
g_logger.error(stdext::format("Unable to read package '%s': %s", package, PHYSFS_getLastError()));
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::fileExists(const std::string& fileName)
|
|
|
|
{
|
|
|
|
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::directoryExists(const std::string& directoryName)
|
|
|
|
{
|
|
|
|
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
void ResourceManager::readFileStream(const std::string& fileName, std::iostream& out)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
2013-01-08 22:31:41 +01:00
|
|
|
std::string buffer = readFileContents(fileName);
|
|
|
|
if(buffer.length() == 0) {
|
|
|
|
out.clear(std::ios::eofbit);
|
|
|
|
return;
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
2013-01-08 22:31:41 +01:00
|
|
|
out.clear(std::ios::goodbit);
|
|
|
|
out.write(&buffer[0], buffer.length());
|
|
|
|
out.seekg(0, std::ios::beg);
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
std::string ResourceManager::readFileContents(const std::string& fileName)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
2013-01-08 22:31:41 +01:00
|
|
|
std::string fullPath = resolvePath(fileName);
|
|
|
|
|
|
|
|
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
|
|
|
|
if(!file)
|
|
|
|
stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
|
|
|
|
|
|
|
|
int fileSize = PHYSFS_fileLength(file);
|
|
|
|
std::string buffer(fileSize, 0);
|
|
|
|
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
|
|
|
|
PHYSFS_close(file);
|
|
|
|
|
|
|
|
return buffer;
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
bool ResourceManager::writeFileBuffer(const std::string& fileName, const uchar* data, uint size)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
|
|
|
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
|
2012-06-26 00:42:38 +02:00
|
|
|
if(!file) {
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.error(PHYSFS_getLastError());
|
2012-04-29 03:07:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHYSFS_write(file, (void*)data, size, 1);
|
|
|
|
PHYSFS_close(file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
bool ResourceManager::writeFileStream(const std::string& fileName, std::iostream& in)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
|
|
|
std::streampos oldPos = in.tellg();
|
|
|
|
in.seekg(0, std::ios::end);
|
|
|
|
std::streampos size = in.tellg();
|
|
|
|
in.seekg(0, std::ios::beg);
|
|
|
|
std::vector<char> buffer(size);
|
|
|
|
in.read(&buffer[0], size);
|
2013-01-08 22:31:41 +01:00
|
|
|
bool ret = writeFileBuffer(fileName, (const uchar*)&buffer[0], size);
|
2012-04-29 03:07:47 +02:00
|
|
|
in.seekg(oldPos, std::ios::beg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
bool ResourceManager::writeFileContents(const std::string& fileName, const std::string& data)
|
2012-04-29 03:07:47 +02:00
|
|
|
{
|
2013-01-08 22:31:41 +01:00
|
|
|
return writeFileBuffer(fileName, (const uchar*)data.c_str(), data.size());
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStreamPtr ResourceManager::openFile(const std::string& fileName)
|
|
|
|
{
|
|
|
|
std::string fullPath = resolvePath(fileName);
|
2013-01-08 22:31:41 +01:00
|
|
|
|
2012-04-29 03:07:47 +02:00
|
|
|
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
|
2012-06-26 00:42:38 +02:00
|
|
|
if(!file)
|
|
|
|
stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
|
2012-06-23 23:30:54 +02:00
|
|
|
return FileStreamPtr(new FileStream(fullPath, file, false));
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
|
|
|
|
{
|
|
|
|
PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str());
|
2012-06-26 00:42:38 +02:00
|
|
|
if(!file)
|
|
|
|
stdext::throw_exception(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError()));
|
2012-06-23 23:30:54 +02:00
|
|
|
return FileStreamPtr(new FileStream(fileName, file, true));
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStreamPtr ResourceManager::createFile(const std::string& fileName)
|
|
|
|
{
|
|
|
|
PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str());
|
2012-06-26 00:42:38 +02:00
|
|
|
if(!file)
|
|
|
|
stdext::throw_exception(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError()));
|
2012-06-23 23:30:54 +02:00
|
|
|
return FileStreamPtr(new FileStream(fileName, file, true));
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::deleteFile(const std::string& fileName)
|
|
|
|
{
|
|
|
|
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceManager::makeDir(const std::string directory)
|
|
|
|
{
|
|
|
|
return PHYSFS_mkdir(directory.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
|
|
|
|
{
|
|
|
|
std::list<std::string> files;
|
|
|
|
auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
|
|
|
|
|
|
|
|
for(int i = 0; rc[i] != NULL; i++)
|
|
|
|
files.push_back(rc[i]);
|
|
|
|
|
|
|
|
PHYSFS_freeList(rc);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ResourceManager::resolvePath(const std::string& path)
|
|
|
|
{
|
|
|
|
std::string fullPath;
|
2012-08-01 09:49:09 +02:00
|
|
|
if(stdext::starts_with(path, "/"))
|
2012-04-29 03:07:47 +02:00
|
|
|
fullPath = path;
|
|
|
|
else {
|
|
|
|
std::string scriptPath = "/" + g_lua.getCurrentSourcePath();
|
|
|
|
if(!scriptPath.empty())
|
|
|
|
fullPath += scriptPath + "/";
|
|
|
|
fullPath += path;
|
|
|
|
}
|
2012-08-01 09:49:09 +02:00
|
|
|
if(!(stdext::starts_with(fullPath, "/")))
|
2012-06-01 22:39:23 +02:00
|
|
|
g_logger.traceWarning(stdext::format("the following file path is not fully resolved: %s", path));
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::replace_all(fullPath, "//", "/");
|
2012-04-29 03:07:47 +02:00
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
|
2012-06-20 02:15:56 +02:00
|
|
|
std::string ResourceManager::getRealDir(const std::string& path)
|
|
|
|
{
|
|
|
|
std::string dir;
|
2012-07-16 21:35:14 +02:00
|
|
|
const char *cdir = PHYSFS_getRealDir(resolvePath(path).c_str());
|
2012-06-20 02:15:56 +02:00
|
|
|
if(cdir)
|
|
|
|
dir = cdir;
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2012-10-24 22:03:15 +02:00
|
|
|
std::string ResourceManager::getCurrentDir()
|
|
|
|
{
|
2013-01-08 22:31:41 +01:00
|
|
|
return boost::filesystem::current_path().generic_string<std::string>() + "/";
|
2012-10-24 22:03:15 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 03:07:47 +02:00
|
|
|
std::string ResourceManager::getBaseDir()
|
|
|
|
{
|
2013-01-08 22:31:41 +01:00
|
|
|
return PHYSFS_getBaseDir();
|
2012-04-29 03:07:47 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 21:01:47 +01:00
|
|
|
std::string ResourceManager::guessFileType(const std::string& filename, const std::string& type)
|
|
|
|
{
|
|
|
|
if(g_resources.fileExists(filename))
|
|
|
|
return filename;
|
|
|
|
return filename + "." + type;
|
2013-01-08 22:31:41 +01:00
|
|
|
}
|