[colobot] 181/390: Saving code for CLevelParser

Didier Raboud odyx at moszumanska.debian.org
Fri Jun 12 14:21:43 UTC 2015


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch upstream/latest
in repository colobot.

commit 1477e72ab4fc5ceae3a3c3bba8234fd5e8170367
Author: krzys-h <krzys_h at interia.pl>
Date:   Mon Nov 10 11:41:05 2014 +0100

    Saving code for CLevelParser
---
 src/object/level/parser.cpp      | 19 ++++++++++--
 src/object/level/parser.h        |  2 +-
 src/object/level/parserline.cpp  | 10 +++---
 src/object/level/parserline.h    |  4 +--
 src/object/level/parserparam.cpp | 66 +++++++++++++++++++++++++++++++++++++++-
 src/object/level/parserparam.h   |  2 ++
 src/object/robotmain.cpp         | 18 +++++++++++
 7 files changed, 109 insertions(+), 12 deletions(-)

diff --git a/src/object/level/parser.cpp b/src/object/level/parser.cpp
index 3a0449a..2b0f630 100644
--- a/src/object/level/parser.cpp
+++ b/src/object/level/parser.cpp
@@ -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()
diff --git a/src/object/level/parser.h b/src/object/level/parser.h
index ca2bf6f..105c0d7 100644
--- a/src/object/level/parser.h
+++ b/src/object/level/parser.h
@@ -51,7 +51,7 @@ public:
     //! Load file
     void Load();
     //! Save file
-    void Save(std::string filename);
+    void Save();
     
     //! Get filename
     const std::string& GetFilename();
diff --git a/src/object/level/parserline.cpp b/src/object/level/parserline.cpp
index e0e8097..c838b35 100644
--- a/src/object/level/parserline.cpp
+++ b/src/object/level/parserline.cpp
@@ -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;
 }
\ No newline at end of file
diff --git a/src/object/level/parserline.h b/src/object/level/parserline.h
index 9881ac7..0af0f06 100644
--- a/src/object/level/parserline.h
+++ b/src/object/level/parserline.h
@@ -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;
diff --git a/src/object/level/parserparam.cpp b/src/object/level/parserparam.cpp
index 1f81f0e..6408e03 100644
--- a/src/object/level/parserparam.cpp
+++ b/src/object/level/parserparam.cpp
@@ -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;
-}
\ No newline at end of file
+}
+
+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();
+}
+
diff --git a/src/object/level/parserparam.h b/src/object/level/parserparam.h
index f59b40f..0ef350f 100644
--- a/src/object/level/parserparam.h
+++ b/src/object/level/parserparam.h
@@ -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);
diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp
index 7782284..393f71d 100644
--- a/src/object/robotmain.cpp
+++ b/src/object/robotmain.cpp
@@ -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;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list