Support for %lvl% in all commands

Except for TerrainInitTextures (I'm not sure what it does but it does something weird)
dev-mp
krzys-h 2014-10-26 18:30:56 +01:00
parent 4485905e0f
commit ca4f1e85d2
6 changed files with 62 additions and 30 deletions

View File

@ -19,6 +19,8 @@
#include "common/resources/inputstreambuffer.h" #include "common/resources/inputstreambuffer.h"
#include "common/resources/resourcemanager.h"
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
@ -44,7 +46,7 @@ CInputStreamBuffer::~CInputStreamBuffer()
void CInputStreamBuffer::open(const std::string &filename) void CInputStreamBuffer::open(const std::string &filename)
{ {
if (PHYSFS_isInit()) if (PHYSFS_isInit())
m_file = PHYSFS_openRead(filename.c_str()); m_file = PHYSFS_openRead(CResourceManager::CleanPath(filename).c_str());
} }

View File

@ -19,6 +19,8 @@
#include "common/resources/outputstreambuffer.h" #include "common/resources/outputstreambuffer.h"
#include "common/resources/resourcemanager.h"
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
@ -40,7 +42,7 @@ COutputStreamBuffer::~COutputStreamBuffer()
void COutputStreamBuffer::open(const std::string &filename) void COutputStreamBuffer::open(const std::string &filename)
{ {
if (PHYSFS_isInit()) if (PHYSFS_isInit())
m_file = PHYSFS_openWrite(filename.c_str()); m_file = PHYSFS_openWrite(CResourceManager::CleanPath(filename).c_str());
} }

View File

@ -26,6 +26,7 @@
#include <physfs.h> #include <physfs.h>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/regex.hpp>
namespace fs = boost::filesystem; 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) bool CResourceManager::AddLocation(const std::string &location, bool prepend)
{ {
@ -121,7 +127,7 @@ SDL_RWops* CResourceManager::GetSDLFileHandler(const std::string &filename)
return nullptr; return nullptr;
} }
PHYSFS_File *file = PHYSFS_openRead(filename.c_str()); PHYSFS_File *file = PHYSFS_openRead(CleanPath(filename).c_str());
if (!file) if (!file)
{ {
SDL_FreeRW(handler); SDL_FreeRW(handler);
@ -141,32 +147,33 @@ SDL_RWops* CResourceManager::GetSDLFileHandler(const std::string &filename)
CSNDFile* CResourceManager::GetSNDFileHandler(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) 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) 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) 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 CResourceManager::RemoveDirectory(const std::string& directory)
{ {
bool success = true; bool success = true;
std::string writeDir = PHYSFS_getWriteDir(); std::string writeDir = PHYSFS_getWriteDir();
try try
{ {
fs::remove_all(writeDir + "/" + directory); fs::remove_all(writeDir + "/" + CleanPath(directory));
} }
catch (std::exception & e) catch (std::exception & e)
{ {
@ -179,7 +186,7 @@ std::vector<std::string> CResourceManager::ListFiles(const std::string &director
{ {
std::vector<std::string> result; 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++) 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; 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++) for (char **i = files; *i != nullptr; i++)
{ {
std::string path = directory + "/" + (*i); std::string path = CleanPath(directory) + "/" + (*i);
if (PHYSFS_isDirectory(path.c_str())) if (PHYSFS_isDirectory(path.c_str()))
{ {
result.push_back(*i); result.push_back(*i);

View File

@ -30,6 +30,8 @@ class CResourceManager
public: public:
CResourceManager(const char *argv0); CResourceManager(const char *argv0);
~CResourceManager(); ~CResourceManager();
static std::string CleanPath(const std::string &path);
static bool AddLocation(const std::string &location, bool prepend = true); static bool AddLocation(const std::string &location, bool prepend = true);
static bool RemoveLocation(const std::string &location); static bool RemoveLocation(const std::string &location);

View File

@ -170,7 +170,7 @@ std::string CLevelParserParam::InjectLevelDir(std::string path, const std::strin
boost::replace_all(newPath, "%lvl%", lvlDir); boost::replace_all(newPath, "%lvl%", lvlDir);
std::string chapDir = CLevelParser::BuildSceneName(CRobotMain::GetInstancePointer()->GetSceneName(), CRobotMain::GetInstancePointer()->GetSceneRank()/100, 0, false); std::string chapDir = CLevelParser::BuildSceneName(CRobotMain::GetInstancePointer()->GetSceneName(), CRobotMain::GetInstancePointer()->GetSceneRank()/100, 0, false);
boost::replace_all(newPath, "%chap%", chapDir); boost::replace_all(newPath, "%chap%", chapDir);
if(newPath == path) if(newPath == path && !path.empty())
{ {
newPath = defaultDir + (!defaultDir.empty() ? "/" : "") + newPath; newPath = defaultDir + (!defaultDir.empty() ? "/" : "") + newPath;
} }

View File

@ -3864,7 +3864,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (line->GetCommand() == "CacheAudio" && !resetObject && m_version >= 2) if (line->GetCommand() == "CacheAudio" && !resetObject && m_version >= 2)
{ {
m_sound->CacheMusic(line->GetParam("filename")->AsPath("").c_str()); //TODO: don't make this relative to music/ m_sound->CacheMusic(std::string("../")+line->GetParam("filename")->AsPath("music"));
continue; continue;
} }
@ -3882,7 +3882,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
m_audioChange[i].powermax = line->GetParam("powermax")->AsFloat(100); m_audioChange[i].powermax = line->GetParam("powermax")->AsFloat(100);
m_audioChange[i].tool = line->GetParam("tool")->AsToolType(TOOL_OTHER); m_audioChange[i].tool = line->GetParam("tool")->AsToolType(TOOL_OTHER);
m_audioChange[i].drive = line->GetParam("drive")->AsDriveType(DRIVE_OTHER); m_audioChange[i].drive = line->GetParam("drive")->AsDriveType(DRIVE_OTHER);
strcpy(m_audioChange[i].music, line->GetParam("filename")->AsPath("").c_str()); //TODO: don't make this relative to music/ strcpy(m_audioChange[i].music, (std::string("../")+line->GetParam("filename")->AsPath("music")).c_str());
m_audioChange[i].repeat = line->GetParam("repeat")->AsBool(true); m_audioChange[i].repeat = line->GetParam("repeat")->AsBool(true);
m_audioChange[i].changed = false; m_audioChange[i].changed = false;
m_sound->CacheMusic(m_audioChange[i].music); m_sound->CacheMusic(m_audioChange[i].music);
@ -3906,14 +3906,26 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
} }
else else
{ {
m_audioTrack = line->GetParam("main")->AsPath("", ""); //TODO: don't make this relative to music/ if(line->GetParam("main")->IsDefined()) {
m_audioRepeat = line->GetParam("mainRepeat")->AsBool(true); m_audioTrack = std::string("../")+line->GetParam("main")->AsPath("music");
m_audioRepeat = line->GetParam("mainRepeat")->AsBool(true);
} else {
m_audioTrack = "";
}
m_satcomTrack = line->GetParam("satcom")->AsPath("", ""); //TODO: don't make this relative to music/ if(line->GetParam("satcom")->IsDefined()) {
m_satcomRepeat = line->GetParam("satcomRepeat")->AsBool(true); m_satcomTrack = std::string("../")+line->GetParam("satcom")->AsPath("music");
m_satcomRepeat = line->GetParam("satcomRepeat")->AsBool(true);
} else {
m_satcomTrack = "";
}
m_editorTrack = line->GetParam("editor")->AsPath("", ""); //TODO: don't make this relative to music/ if(line->GetParam("editor")->IsDefined()) {
m_editorRepeat = line->GetParam("editorRepeat")->AsBool(true); m_editorTrack = std::string("../")+line->GetParam("editor")->AsPath("music");
m_editorRepeat = line->GetParam("editorRepeat")->AsBool(true);
} else {
m_editorTrack = "";
}
} }
if (m_audioTrack != "") m_sound->CacheMusic(m_audioTrack); if (m_audioTrack != "") m_sound->CacheMusic(m_audioTrack);
if (m_satcomTrack != "") m_sound->CacheMusic(m_satcomTrack); if (m_satcomTrack != "") m_sound->CacheMusic(m_satcomTrack);
@ -3975,7 +3987,10 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (line->GetCommand() == "Background" && !resetObject) if (line->GetCommand() == "Background" && !resetObject)
{ {
m_engine->SetBackground(line->GetParam("image")->AsPath("", "").c_str(), //TODO: don't make this relative to textures/ std::string path = "";
if(line->GetParam("image")->IsDefined())
path = "../"+line->GetParam("image")->AsPath("textures");
m_engine->SetBackground(path.c_str(),
line->GetParam("up")->AsColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)), line->GetParam("up")->AsColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)),
line->GetParam("down")->AsColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)), line->GetParam("down")->AsColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)),
line->GetParam("cloudUp")->AsColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)), line->GetParam("cloudUp")->AsColor(Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f)),
@ -3996,17 +4011,17 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
line->GetParam("dim")->AsFloat(0.2f), line->GetParam("dim")->AsFloat(0.2f),
line->GetParam("speed")->AsFloat(0.0f), line->GetParam("speed")->AsFloat(0.0f),
line->GetParam("dir")->AsFloat(0.0f), line->GetParam("dir")->AsFloat(0.0f),
line->GetParam("image")->AsPath(""), //TODO: don't make this relative to textures/ "../"+line->GetParam("image")->AsPath("textures"),
Math::Point(uv1.x, uv1.z), Math::Point(uv1.x, uv1.z),
Math::Point(uv2.x, uv2.z), Math::Point(uv2.x, uv2.z),
line->GetParam("image")->AsPath("").find("planet") != std::string::npos // TODO: add transparent op or modify textures line->GetParam("image")->AsPath("textures").find("planet") != std::string::npos // TODO: add transparent op or modify textures
); );
continue; continue;
} }
if (line->GetCommand() == "ForegroundName" && !resetObject) if (line->GetCommand() == "ForegroundName" && !resetObject)
{ {
m_engine->SetForegroundName(line->GetParam("image")->AsPath("")); //TODO: don't make this relative to textures/ m_engine->SetForegroundName("../"+line->GetParam("image")->AsPath("textures"));
continue; continue;
} }
@ -4069,7 +4084,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
pos.z = pos.x; pos.z = pos.x;
m_water->Create(line->GetParam("air")->AsWaterType(Gfx::WATER_TT), m_water->Create(line->GetParam("air")->AsWaterType(Gfx::WATER_TT),
line->GetParam("water")->AsWaterType(Gfx::WATER_TT), line->GetParam("water")->AsWaterType(Gfx::WATER_TT),
line->GetParam("image")->AsPath(""), //TODO: don't make this relative to textures/ "../"+line->GetParam("image")->AsPath("textures"),
line->GetParam("diffuse")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)), line->GetParam("diffuse")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)),
line->GetParam("ambient")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)), line->GetParam("ambient")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)),
line->GetParam("level")->AsFloat(100.0f)*g_unit, line->GetParam("level")->AsFloat(100.0f)*g_unit,
@ -4088,7 +4103,10 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (line->GetCommand() == "TerrainCloud" && !resetObject) if (line->GetCommand() == "TerrainCloud" && !resetObject)
{ {
m_cloud->Create(line->GetParam("image")->AsPath("", ""), //TODO: don't make this relative to textures/ std::string path = "";
if(line->GetParam("image")->IsDefined())
path = "../"+line->GetParam("image")->AsPath("textures");
m_cloud->Create(path,
line->GetParam("diffuse")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)), line->GetParam("diffuse")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)),
line->GetParam("ambient")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)), line->GetParam("ambient")->AsColor(Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)),
line->GetParam("level")->AsFloat(500.0f)*g_unit); line->GetParam("level")->AsFloat(500.0f)*g_unit);
@ -4153,9 +4171,10 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (line->GetCommand() == "TerrainMaterial" && !resetObject) if (line->GetCommand() == "TerrainMaterial" && !resetObject)
{ {
std::string name = line->GetParam("image")->AsPath(""); //TODO: don't make this relative to textures/ std::string name = line->GetParam("image")->AsPath("textures");
if(name.find(".") == std::string::npos) if(name.find(".") == std::string::npos)
name += ".png"; name += ".png";
name = "../"+name;
/*TODO: ??? /*TODO: ???
if (strstr(name, "%user%") != 0) if (strstr(name, "%user%") != 0)
{ {
@ -4229,7 +4248,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
CBrain* brain = m_controller->GetBrain(); CBrain* brain = m_controller->GetBrain();
if (brain != nullptr) if (brain != nullptr)
{ {
std::string name = line->GetParam("script")->AsPath(""); //TODO: Don't make this relative to ai/ std::string name = "../"+line->GetParam("script")->AsPath("ai");
if (!name.empty()) if (!name.empty())
brain->SetScriptName(0, const_cast<char*>(name.c_str())); brain->SetScriptName(0, const_cast<char*>(name.c_str()));
brain->SetScriptRun(0); brain->SetScriptRun(0);
@ -4394,7 +4413,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
{ {
std::string op = "script"+boost::lexical_cast<std::string>(i+1); // script1..script10 std::string op = "script"+boost::lexical_cast<std::string>(i+1); // script1..script10
if(line->GetParam(op)->IsDefined()) { if(line->GetParam(op)->IsDefined()) {
brain->SetScriptName(i, const_cast<char*>(line->GetParam(op)->AsPath("").c_str())); //TODO: don't make this relative to ai/ brain->SetScriptName(i, const_cast<char*>(("../"+line->GetParam(op)->AsPath("ai")).c_str()));
} }
} }
@ -4539,7 +4558,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
if (m_mapImage) if (m_mapImage)
{ {
Math::Vector offset; Math::Vector offset;
strcpy(m_mapFilename, line->GetParam("filename")->AsPath("").c_str()); //TODO: don't make this relative to textures/ strcpy(m_mapFilename, ("../"+line->GetParam("filename")->AsPath("textures")).c_str());
offset = line->GetParam("offset")->AsPoint(Math::Vector(0.0f, 0.0f, 0.0f)); offset = line->GetParam("offset")->AsPoint(Math::Vector(0.0f, 0.0f, 0.0f));
m_map->SetFixParam(line->GetParam("zoom")->AsFloat(1.0f), m_map->SetFixParam(line->GetParam("zoom")->AsFloat(1.0f),
offset.x, offset.z, offset.x, offset.z,