Saving code for CLevelParser

dev-mp
krzys-h 2014-11-10 11:41:05 +01:00
parent b6eec3d8c6
commit 1477e72ab4
7 changed files with 109 additions and 12 deletions

View File

@ -24,6 +24,7 @@
#include "common/resources/resourcemanager.h"
#include "common/resources/inputstream.h"
#include "common/resources/outputstream.h"
#include "object/level/parserexceptions.h"
@ -219,9 +220,23 @@ void CLevelParser::Load()
file.close();
}
void CLevelParser::Save(std::string filename)
void CLevelParser::Save()
{
assert(false); //TODO
COutputStream file;
file.open(m_filename);
if(!file.is_open())
throw CLevelParserException("Failed to open file: "+m_filename);
for(CLevelParserLine* line : m_lines) {
file << line->GetCommand();
for(auto param : line->GetParams()) {
file << " " << param.first << "=" << param.second->GetValue();
}
file << "\n";
}
file.close();
}
const std::string& CLevelParser::GetFilename()

View File

@ -51,7 +51,7 @@ public:
//! Load file
void Load();
//! Save file
void Save(std::string filename);
void Save();
//! Get filename
const std::string& GetFilename();

View File

@ -43,11 +43,6 @@ CLevelParserLine::~CLevelParserLine()
}
}
std::string CLevelParserLine::GetLine()
{
assert(false); //TODO
}
int CLevelParserLine::GetLineNumber()
{
return m_lineNumber;
@ -87,4 +82,9 @@ void CLevelParserLine::AddParam(std::string name, CLevelParserParam* value)
{
value->SetLine(this);
m_params[name] = value;
}
const std::map<std::string, CLevelParserParam*>& CLevelParserLine::GetParams()
{
return m_params;
}

View File

@ -37,9 +37,6 @@ public:
CLevelParserLine(std::string command);
~CLevelParserLine();
//! Get line to be saved in level file
std::string GetLine();
//! Get line number
int GetLineNumber();
@ -53,6 +50,7 @@ public:
CLevelParserParam* GetParam(std::string name);
void AddParam(std::string name, CLevelParserParam* value);
const std::map<std::string, CLevelParserParam*>& GetParams();
private:
CLevelParser* m_level;

View File

@ -939,6 +939,18 @@ void CLevelParserParam::ParseArray()
}
}
void CLevelParserParam::LoadArray()
{
m_value = "";
bool first = true;
for(auto& value : m_array) {
if(!first)
m_value += ";";
m_value += value->GetValue();
first = false;
}
}
const std::vector<CLevelParserParam*>& CLevelParserParam::AsArray()
{
if(m_empty)
@ -947,4 +959,56 @@ const std::vector<CLevelParserParam*>& CLevelParserParam::AsArray()
ParseArray();
return m_array;
}
}
CLevelParserParam::CLevelParserParam(int value)
{
m_value = boost::lexical_cast<std::string>(value);
}
CLevelParserParam::CLevelParserParam(float value)
{
m_value = boost::lexical_cast<std::string>(value);
}
CLevelParserParam::CLevelParserParam(std::string value)
{
m_value = "\""+value+"\"";
}
CLevelParserParam::CLevelParserParam(bool value)
{
m_value = value ? "true" : "false";
}
CLevelParserParam::CLevelParserParam(Gfx::Color value)
{
m_array.push_back(new CLevelParserParam(value.r));
m_array.push_back(new CLevelParserParam(value.g));
m_array.push_back(new CLevelParserParam(value.b));
m_array.push_back(new CLevelParserParam(value.a));
LoadArray();
}
CLevelParserParam::CLevelParserParam(Math::Point value)
{
m_array.push_back(new CLevelParserParam(value.x));
m_array.push_back(new CLevelParserParam(value.y));
LoadArray();
}
CLevelParserParam::CLevelParserParam(Math::Vector value)
{
m_array.push_back(new CLevelParserParam(value.x));
m_array.push_back(new CLevelParserParam(value.y));
m_array.push_back(new CLevelParserParam(value.z));
LoadArray();
}
CLevelParserParam::CLevelParserParam(ObjectType value)
{
m_value = FromObjectType(value);
}
CLevelParserParam::CLevelParserParam(Gfx::CameraType value)
{
m_value = FromCameraType(value);
}
CLevelParserParam::CLevelParserParam(const std::vector<CLevelParserParam*>& value)
{
m_array = value;
LoadArray();
}

View File

@ -45,6 +45,7 @@ public:
CLevelParserParam(bool value);
CLevelParserParam(Gfx::Color value);
CLevelParserParam(Math::Point value);
CLevelParserParam(Math::Vector value);
CLevelParserParam(ObjectType value);
CLevelParserParam(Gfx::CameraType value);
CLevelParserParam(const std::vector<CLevelParserParam*>& value);
@ -110,6 +111,7 @@ public:
private:
void ParseArray();
void LoadArray();
template<typename T> T Cast(std::string value, std::string requestedType);
template<typename T> T Cast(std::string requestedType);

View File

@ -5767,6 +5767,24 @@ void CRobotMain::IOWriteObject(FILE *file, CObject* obj, const char *cmd)
//! Saves the current game
bool CRobotMain::IOWriteScene(const char *filename, const char *filecbot, char *info)
{
std::string fnstr = filename;
std::string savedir = CResourceManager::GetSaveLocation()+"/";
boost::replace_all(fnstr, "\\", "/");
boost::replace_all(savedir, "\\", "/");
boost::replace_all(fnstr, savedir, ""); //TODO: Refactor to get physfs path here
/*CLevelParser* level = new CLevelParser(fnstr);
CLevelParserLine* line1 = new CLevelParserLine("TestCommand");
line1->AddParam("test", new CLevelParserParam(1.0f));
level->AddLine(line1);
CLevelParserLine* line2 = new CLevelParserLine("TestCommand2");
line2->AddParam("testStr", new CLevelParserParam("12345"));
line2->AddParam("testBool", new CLevelParserParam(true));
level->AddLine(line2);
level->Save();
delete level;
return true;*/
FILE* file = fopen(filename, "w");
if (file == NULL) return false;