summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorkrzys-h <krzys_h@interia.pl>2014-10-26 18:30:56 +0100
committerkrzys-h <krzys_h@interia.pl>2014-10-26 18:35:13 +0100
commitca4f1e85d2812ad715e21be96413efe155b58a84 (patch)
tree134cc4346b7a7e674411fb2170f9087b3f76a513 /src/common
parent4485905e0f21cb4f87a1d9509aa1633035028b04 (diff)
downloadcolobot-ca4f1e85d2812ad715e21be96413efe155b58a84.tar.gz
colobot-ca4f1e85d2812ad715e21be96413efe155b58a84.tar.bz2
colobot-ca4f1e85d2812ad715e21be96413efe155b58a84.zip
Support for %lvl% in all commands
Except for TerrainInitTextures (I'm not sure what it does but it does something weird)
Diffstat (limited to 'src/common')
-rw-r--r--src/common/resources/inputstreambuffer.cpp4
-rw-r--r--src/common/resources/outputstreambuffer.cpp4
-rw-r--r--src/common/resources/resourcemanager.cpp25
-rw-r--r--src/common/resources/resourcemanager.h2
4 files changed, 24 insertions, 11 deletions
diff --git a/src/common/resources/inputstreambuffer.cpp b/src/common/resources/inputstreambuffer.cpp
index 7059d60..9ac1fec 100644
--- a/src/common/resources/inputstreambuffer.cpp
+++ b/src/common/resources/inputstreambuffer.cpp
@@ -19,6 +19,8 @@
#include "common/resources/inputstreambuffer.h"
+#include "common/resources/resourcemanager.h"
+
#include <stdexcept>
#include <sstream>
@@ -44,7 +46,7 @@ CInputStreamBuffer::~CInputStreamBuffer()
void CInputStreamBuffer::open(const std::string &filename)
{
if (PHYSFS_isInit())
- m_file = PHYSFS_openRead(filename.c_str());
+ m_file = PHYSFS_openRead(CResourceManager::CleanPath(filename).c_str());
}
diff --git a/src/common/resources/outputstreambuffer.cpp b/src/common/resources/outputstreambuffer.cpp
index f8b4100..157e17d 100644
--- a/src/common/resources/outputstreambuffer.cpp
+++ b/src/common/resources/outputstreambuffer.cpp
@@ -19,6 +19,8 @@
#include "common/resources/outputstreambuffer.h"
+#include "common/resources/resourcemanager.h"
+
#include <stdexcept>
#include <sstream>
@@ -40,7 +42,7 @@ COutputStreamBuffer::~COutputStreamBuffer()
void COutputStreamBuffer::open(const std::string &filename)
{
if (PHYSFS_isInit())
- m_file = PHYSFS_openWrite(filename.c_str());
+ m_file = PHYSFS_openWrite(CResourceManager::CleanPath(filename).c_str());
}
diff --git a/src/common/resources/resourcemanager.cpp b/src/common/resources/resourcemanager.cpp
index 42f9634..b825374 100644
--- a/src/common/resources/resourcemanager.cpp
+++ b/src/common/resources/resourcemanager.cpp
@@ -26,6 +26,7 @@
#include <physfs.h>
#include <boost/filesystem.hpp>
+#include <boost/regex.hpp>
namespace fs = boost::filesystem;
@@ -55,6 +56,11 @@ CResourceManager::~CResourceManager()
}
}
+std::string CResourceManager::CleanPath(const std::string& path)
+{
+ return boost::regex_replace(path, boost::regex("\\.\\./(.*)/"), "");
+}
+
bool CResourceManager::AddLocation(const std::string &location, bool prepend)
{
@@ -121,7 +127,7 @@ SDL_RWops* CResourceManager::GetSDLFileHandler(const std::string &filename)
return nullptr;
}
- PHYSFS_File *file = PHYSFS_openRead(filename.c_str());
+ PHYSFS_File *file = PHYSFS_openRead(CleanPath(filename).c_str());
if (!file)
{
SDL_FreeRW(handler);
@@ -141,32 +147,33 @@ SDL_RWops* CResourceManager::GetSDLFileHandler(const std::string &filename)
CSNDFile* CResourceManager::GetSNDFileHandler(const std::string &filename)
{
- return new CSNDFile(filename);
+ return new CSNDFile(CleanPath(filename));
}
bool CResourceManager::Exists(const std::string &filename)
{
- return PHYSFS_exists(filename.c_str());
+ return PHYSFS_exists(CleanPath(filename).c_str());
}
bool CResourceManager::DirectoryExists(const std::string& directory)
{
- return PHYSFS_exists(directory.c_str()) && PHYSFS_isDirectory(directory.c_str());
+ return PHYSFS_exists(CleanPath(directory).c_str()) && PHYSFS_isDirectory(CleanPath(directory).c_str());
}
bool CResourceManager::CreateDirectory(const std::string& directory)
{
- return PHYSFS_mkdir(directory.c_str());
+ return PHYSFS_mkdir(CleanPath(directory).c_str());
}
+//TODO: Don't use boost filesystem here
bool CResourceManager::RemoveDirectory(const std::string& directory)
{
bool success = true;
std::string writeDir = PHYSFS_getWriteDir();
try
{
- fs::remove_all(writeDir + "/" + directory);
+ fs::remove_all(writeDir + "/" + CleanPath(directory));
}
catch (std::exception & e)
{
@@ -179,7 +186,7 @@ std::vector<std::string> CResourceManager::ListFiles(const std::string &director
{
std::vector<std::string> result;
- char **files = PHYSFS_enumerateFiles(directory.c_str());
+ char **files = PHYSFS_enumerateFiles(CleanPath(directory).c_str());
for (char **i = files; *i != nullptr; i++)
{
@@ -195,11 +202,11 @@ std::vector<std::string> CResourceManager::ListDirectories(const std::string &di
{
std::vector<std::string> result;
- char **files = PHYSFS_enumerateFiles(directory.c_str());
+ char **files = PHYSFS_enumerateFiles(CleanPath(directory).c_str());
for (char **i = files; *i != nullptr; i++)
{
- std::string path = directory + "/" + (*i);
+ std::string path = CleanPath(directory) + "/" + (*i);
if (PHYSFS_isDirectory(path.c_str()))
{
result.push_back(*i);
diff --git a/src/common/resources/resourcemanager.h b/src/common/resources/resourcemanager.h
index 730cf3e..4d79e9b 100644
--- a/src/common/resources/resourcemanager.h
+++ b/src/common/resources/resourcemanager.h
@@ -30,6 +30,8 @@ class CResourceManager
public:
CResourceManager(const char *argv0);
~CResourceManager();
+
+ static std::string CleanPath(const std::string &path);
static bool AddLocation(const std::string &location, bool prepend = true);
static bool RemoveLocation(const std::string &location);