Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7963


Ignore:
Timestamp:
Feb 26, 2011, 6:44:25 AM (13 years ago)
Author:
rgrieder
Message:

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)
Location:
code/branches/usability/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/usability/src/libraries/core/Loader.cc

    r7648 r7963  
    147147        Returns true if successful.
    148148    */
    149     bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool verbose)
     149    bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool verbose, bool bRemoveLuaTags)
    150150    {
    151151        if (!file)
     
    155155
    156156        std::string xmlInput;
    157         if (file->getLuaSupport())
     157        if (file->getLuaSupport() && !bRemoveLuaTags)
    158158        {
    159159            // Use the LuaState to replace the XML tags (calls our function)
     
    172172            }
    173173            xmlInput = Resource::open(file->getFilename())->getAsString();
     174
     175            if (bRemoveLuaTags)
     176            {
     177                // Remove all Lua code.
     178                // Note: we only need this to speed up parsing of level files at the
     179                // start of the program.
     180                // Assumption: the LevelInfo tag does not use Lua scripting
     181                xmlInput = removeLuaTags(xmlInput);
     182            }
    174183        }
    175184
     
    271280    }
    272281
    273     std::string Loader::replaceLuaTags(const std::string& text)
    274     {
    275         // create map with all Lua tags
    276         std::map<size_t, bool> luaTags;
     282    bool Loader::getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags)
     283    {
     284        // fill map with all Lua tags
    277285        {
    278286            size_t pos = 0;
     
    328336            {
    329337                COUT(2) << "Warning: Error in level file" << std::endl;
    330                 // todo: errorhandling
    331                 return "";
    332             }
    333         }
     338                // TODO: error handling
     339                return false;
     340            }
     341        }
     342
     343        return true;
     344    }
     345
     346    std::string Loader::replaceLuaTags(const std::string& text)
     347    {
     348        // create a map with all lua tags
     349        std::map<size_t, bool> luaTags;
     350        if (!getLuaTags(text, luaTags))
     351            return "";
    334352
    335353        // Use a stringstream object to speed up the parsing
     
    421439        return output.str();
    422440    }
     441
     442    std::string Loader::removeLuaTags(const std::string& text)
     443    {
     444        // create a map with all lua tags
     445        std::map<size_t, bool> luaTags;
     446        if (!getLuaTags(text, luaTags))
     447            return "";
     448
     449        // Use a stringstream object to speed up the concatenation
     450        std::ostringstream output;
     451
     452        // cut the original string into pieces and only write the non Lua parts
     453        std::map<size_t, bool>::iterator it = luaTags.begin();
     454        bool bLuaCode = false;
     455        size_t start = 0;
     456        size_t end = 0;
     457
     458        do
     459        {
     460            if (it != luaTags.end())
     461                end = (it++)->first;
     462            else
     463                end = std::string::npos;
     464
     465            if (!bLuaCode)
     466            {
     467                output << text.substr(start, end - start);
     468                start = end + 5;
     469            }
     470            else
     471                start = end + 2;
     472
     473            bLuaCode = !bLuaCode;
     474        }
     475        while (end != std::string::npos);
     476
     477        return output.str();
     478    }
    423479}
  • code/branches/usability/src/libraries/core/Loader.h

    r7648 r7963  
    4242#include "CorePrereqs.h"
    4343
     44#include <map>
    4445#include <vector>
    4546#include "ClassTreeMask.h"
     
    6162            static bool reload(const ClassTreeMask& mask = ClassTreeMask(), bool verbose = true);
    6263
    63             static bool load(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask(), bool verbose = true);
     64            static bool load(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask(),
     65                             bool verbose = true, bool bRemoveLuaTags = false);
    6466            static void unload(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask());
    6567            static bool reload(const XMLFile* file, const ClassTreeMask& mask = ClassTreeMask(), bool verbose = true);
    6668
    6769            static std::string replaceLuaTags(const std::string& text);
     70            static std::string removeLuaTags(const std::string& text);
    6871
    6972            static ClassTreeMask currentMask_s;
    7073
    7174        private:
     75            static bool getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags);
     76
    7277            static std::vector<std::pair<const XMLFile*, ClassTreeMask> > files_s;
    7378    };
  • code/branches/usability/src/orxonox/LevelManager.cc

    r7839 r7963  
    253253                mask.exclude(ClassIdentifier<BaseObject>::getIdentifier());
    254254                mask.include(ClassIdentifier<LevelInfo>::getIdentifier());
    255                 Loader::load(&file, mask, false);
     255                Loader::load(&file, mask, false, true);
    256256                // Iterate over all LevelInfos.
    257257                for(ObjectList<LevelInfo>::iterator item = ObjectList<LevelInfo>::begin(); item != ObjectList<LevelInfo>::end(); ++item)
Note: See TracChangeset for help on using the changeset viewer.