From ca4f1e85d2812ad715e21be96413efe155b58a84 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 26 Oct 2014 18:30:56 +0100 Subject: Support for %lvl% in all commands Except for TerrainInitTextures (I'm not sure what it does but it does something weird) --- src/common/resources/inputstreambuffer.cpp | 4 +++- src/common/resources/outputstreambuffer.cpp | 4 +++- src/common/resources/resourcemanager.cpp | 25 ++++++++++++++++--------- src/common/resources/resourcemanager.h | 2 ++ 4 files changed, 24 insertions(+), 11 deletions(-) (limited to 'src/common/resources') 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 #include @@ -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 #include @@ -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 #include +#include 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 CResourceManager::ListFiles(const std::string &director { std::vector 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 CResourceManager::ListDirectories(const std::string &di { std::vector 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); -- cgit v1.2.3-1-g7c22