[Orxonox-commit 3272] r7963 - in code/branches/usability/src: libraries/core orxonox

rgrieder at orxonox.net rgrieder at orxonox.net
Sat Feb 26 06:44:26 CET 2011


Author: rgrieder
Date: 2011-02-26 06:44:25 +0100 (Sat, 26 Feb 2011)
New Revision: 7963

Modified:
   code/branches/usability/src/libraries/core/Loader.cc
   code/branches/usability/src/libraries/core/Loader.h
   code/branches/usability/src/orxonox/LevelManager.cc
Log:
Added "replaceLuaTags" function to the Loader. That allows to load files without parsing it as Lua file.
Bottom line:
- Compiling the level list is about 30 times faster and takes less than half a second now
- We need to write level files that are XML-valid even if all the Lua stuff is removed.
  (or: somebody find a better implementation for speeding up that LevelInfo loading)

Modified: code/branches/usability/src/libraries/core/Loader.cc
===================================================================
--- code/branches/usability/src/libraries/core/Loader.cc	2011-02-26 05:40:44 UTC (rev 7962)
+++ code/branches/usability/src/libraries/core/Loader.cc	2011-02-26 05:44:25 UTC (rev 7963)
@@ -146,7 +146,7 @@
     @return
         Returns true if successful.
     */
-    bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool verbose)
+    bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool verbose, bool bRemoveLuaTags)
     {
         if (!file)
             return false;
@@ -154,7 +154,7 @@
         Loader::currentMask_s = file->getMask() * mask;
 
         std::string xmlInput;
-        if (file->getLuaSupport())
+        if (file->getLuaSupport() && !bRemoveLuaTags)
         {
             // Use the LuaState to replace the XML tags (calls our function)
             scoped_ptr<LuaState> luaState(new LuaState());
@@ -171,6 +171,15 @@
                 return false;
             }
             xmlInput = Resource::open(file->getFilename())->getAsString();
+
+            if (bRemoveLuaTags)
+            {
+                // Remove all Lua code.
+                // Note: we only need this to speed up parsing of level files at the
+                // start of the program.
+                // Assumption: the LevelInfo tag does not use Lua scripting
+                xmlInput = removeLuaTags(xmlInput);
+            }
         }
 
         try
@@ -270,10 +279,9 @@
         return Loader::load(file, mask, verbose);
     }
 
-    std::string Loader::replaceLuaTags(const std::string& text)
+    bool Loader::getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags)
     {
-        // create map with all Lua tags
-        std::map<size_t, bool> luaTags;
+        // fill map with all Lua tags
         {
             size_t pos = 0;
             while ((pos = text.find("<?lua", pos)) != std::string::npos)
@@ -327,11 +335,21 @@
             if (!expectedValue)
             {
                 COUT(2) << "Warning: Error in level file" << std::endl;
-                // todo: errorhandling
-                return "";
+                // TODO: error handling
+                return false; 
             }
         }
 
+        return true;
+    }
+
+    std::string Loader::replaceLuaTags(const std::string& text)
+    {
+        // create a map with all lua tags
+        std::map<size_t, bool> luaTags;
+        if (!getLuaTags(text, luaTags))
+            return "";
+
         // Use a stringstream object to speed up the parsing
         std::ostringstream output;
 
@@ -420,4 +438,42 @@
 
         return output.str();
     }
+
+    std::string Loader::removeLuaTags(const std::string& text)
+    {
+        // create a map with all lua tags
+        std::map<size_t, bool> luaTags;
+        if (!getLuaTags(text, luaTags))
+            return "";
+
+        // Use a stringstream object to speed up the concatenation
+        std::ostringstream output;
+
+        // cut the original string into pieces and only write the non Lua parts
+        std::map<size_t, bool>::iterator it = luaTags.begin();
+        bool bLuaCode = false;
+        size_t start = 0;
+        size_t end = 0;
+
+        do
+        {
+            if (it != luaTags.end())
+                end = (it++)->first;
+            else
+                end = std::string::npos;
+
+            if (!bLuaCode)
+            {
+                output << text.substr(start, end - start);
+                start = end + 5;
+            }
+            else
+                start = end + 2;
+
+            bLuaCode = !bLuaCode;
+        }
+        while (end != std::string::npos);
+
+        return output.str();
+    }
 }

Modified: code/branches/usability/src/libraries/core/Loader.h
===================================================================
--- code/branches/usability/src/libraries/core/Loader.h	2011-02-26 05:40:44 UTC (rev 7962)
+++ code/branches/usability/src/libraries/core/Loader.h	2011-02-26 05:44:25 UTC (rev 7963)
@@ -41,6 +41,7 @@
 
 #include "CorePrereqs.h"
 
+#include <map>
 #include <vector>
 #include "ClassTreeMask.h"
 
@@ -60,15 +61,19 @@
             static void unload(const ClassTreeMask& mask = ClassTreeMask());
             static bool reload(const ClassTreeMask& mask = ClassTreeMask(), bool verbose = true);
 
-            static bool load(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask(), bool verbose = true);
+            static bool load(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask(),
+                             bool verbose = true, bool bRemoveLuaTags = false);
             static void unload(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask());
             static bool reload(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask(), bool verbose = true);
 
             static std::string replaceLuaTags(const std::string& text);
+            static std::string removeLuaTags(const std::string& text);
 
             static ClassTreeMask currentMask_s;
 
         private:
+            static bool getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags);
+
             static std::vector<std::pair<const XMLFile*, ClassTreeMask> > files_s;
     };
 }

Modified: code/branches/usability/src/orxonox/LevelManager.cc
===================================================================
--- code/branches/usability/src/orxonox/LevelManager.cc	2011-02-26 05:40:44 UTC (rev 7962)
+++ code/branches/usability/src/orxonox/LevelManager.cc	2011-02-26 05:44:25 UTC (rev 7963)
@@ -252,7 +252,7 @@
                 ClassTreeMask mask = ClassTreeMask();
                 mask.exclude(ClassIdentifier<BaseObject>::getIdentifier());
                 mask.include(ClassIdentifier<LevelInfo>::getIdentifier());
-                Loader::load(&file, mask, false);
+                Loader::load(&file, mask, false, true);
                 // Iterate over all LevelInfos.
                 for(ObjectList<LevelInfo>::iterator item = ObjectList<LevelInfo>::begin(); item != ObjectList<LevelInfo>::end(); ++item)
                 {




More information about the Orxonox-commit mailing list