Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 30, 2009, 2:22:00 AM (15 years ago)
Author:
rgrieder
Message:

Merged resource2 branch back to trunk.

IMPORTANT NOTE:
Upon this merge you need to specifically call your data directory "data_extern" when checking it out (when you don't provide a name, it will be just called 'trunk').
The new CMake variable is EXTERNAL_DATA_DIRECTORY. DATA_DIRECTORY now points to the one the source part of the repository.
UPDATE YOUR DATA DIRECTORY AS WELL!!!

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/Loader.cc

    r3370 r5695  
    2929#include "Loader.h"
    3030
     31#include <sstream>
    3132#include <tinyxml/ticpp.h>
     33#include <boost/scoped_ptr.hpp>
    3234
    3335#include "util/Debug.h"
    3436#include "util/Exception.h"
     37#include "util/StringUtils.h"
    3538#include "BaseObject.h"
    3639#include "Iterator.h"
    3740#include "ObjectList.h"
    38 #include "LuaBind.h"
     41#include "LuaState.h"
    3942#include "Namespace.h"
     43#include "Resource.h"
    4044#include "XMLFile.h"
    4145
     
    118122        Loader::currentMask_s = file->getMask() * mask;
    119123
    120         // let Lua work this out:
    121         LuaBind& lua = LuaBind::getInstance();
    122         lua.clearLuaOutput();
    123         lua.loadFile(file->getFilename(), true);
    124         lua.run();
     124        std::string xmlInput;
     125        if (file->getLuaSupport())
     126        {
     127            // Use the LuaState to replace the XML tags (calls our function)
     128            scoped_ptr<LuaState> luaState(new LuaState());
     129            luaState->setIncludeParser(&Loader::replaceLuaTags);
     130            luaState->includeFile(file->getFilename(), file->getResourceGroup(), false);
     131            xmlInput = luaState->getOutput().str();
     132        }
     133        else
     134        {
     135            shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename(), file->getResourceGroup());
     136            if (info == NULL)
     137            {
     138                COUT(1) << "Error: Could not find XML file '" << file->getFilename() << "'." << std::endl;
     139                return false;
     140            }
     141            xmlInput = Resource::open(file->getFilename(), file->getResourceGroup())->getAsString();
     142        }
    125143
    126144        try
     
    129147            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
    130148
    131             //ticpp::Document xmlfile(file->getFilename());
    132             //xmlfile.LoadFile();
    133             //ticpp::Element myelement(*Script::getFileString());
    134             ticpp::Document xmlfile;
    135             //xmlfile.ToDocument();
    136             xmlfile.Parse(lua.getLuaOutput(), true);
     149            ticpp::Document xmlfile(file->getFilename());
     150            xmlfile.Parse(xmlInput, true);
    137151
    138152            ticpp::Element rootElement;
     
    208222        return Loader::load(file, mask);
    209223    }
     224
     225    std::string Loader::replaceLuaTags(const std::string& text)
     226    {
     227        // chreate map with all Lua tags
     228        std::map<size_t, bool> luaTags;
     229        {
     230            size_t pos = 0;
     231            while ((pos = text.find("<?lua", pos)) != std::string::npos)
     232                luaTags[pos++] = true;
     233        }
     234        {
     235            size_t pos = 0;
     236            while ((pos = text.find("?>", pos)) != std::string::npos)
     237                luaTags[pos++] = false;
     238        }
     239
     240        // erase all tags from the map that are between two quotes
     241        {
     242            std::map<size_t, bool>::iterator it = luaTags.begin();
     243            std::map<size_t, bool>::iterator it2 = it;
     244            bool bBetweenQuotes = false;
     245            size_t pos = 0;
     246            while ((pos = getNextQuote(text, pos)) != std::string::npos)
     247            {
     248                while ((it != luaTags.end()) && (it->first < pos))
     249                {
     250                    if (bBetweenQuotes)
     251                    {
     252                        it2++;
     253                        if(it->second && !(it2->second) && it2->first < pos)
     254                            it = ++it2;
     255                        else
     256                            luaTags.erase(it++);
     257                    }
     258                    else
     259                        ++it;
     260                }
     261                bBetweenQuotes = !bBetweenQuotes;
     262                pos++;
     263            }
     264        }
     265
     266        // check whether on every opening <?lua tag a closing ?> tag follows
     267        {
     268            bool expectedValue = true;
     269            for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
     270            {
     271                if (it->second == expectedValue)
     272                    expectedValue = !expectedValue;
     273                else
     274                {
     275                    expectedValue = false;
     276                    break;
     277                }
     278            }
     279            if (!expectedValue)
     280            {
     281                COUT(2) << "Warning: Error in level file" << std::endl;
     282                // todo: errorhandling
     283                return "";
     284            }
     285        }
     286
     287        // Use a stringstream object to speed up the parsing
     288        std::ostringstream output;
     289
     290        // cut the original string into pieces and put them together with print() instead of lua tags
     291        {
     292            std::map<size_t, bool>::iterator it = luaTags.begin();
     293            bool bInPrintFunction = true;
     294            size_t start = 0;
     295            size_t end = 0;
     296
     297            do
     298            {
     299                if (it != luaTags.end())
     300                    end = (*(it++)).first;
     301                else
     302                    end = std::string::npos;
     303
     304                unsigned int equalSignCounter = 0;
     305
     306                if (bInPrintFunction)
     307                {
     308                    // count ['='[ and ]'='] and replace tags with print([[ and ]])
     309                    std::string temp = text.substr(start, end - start);
     310                    {
     311                    size_t pos = 0;
     312                    while ((pos = temp.find('[', pos)) != std::string::npos)
     313                    {
     314                        unsigned int tempCounter = 1;
     315                        size_t tempPos = pos++;
     316                        while(temp[++tempPos] == '=')
     317                        {
     318                            tempCounter++;
     319                        }
     320                        if(temp[tempPos] != '[')
     321                        {
     322                            tempCounter = 0;
     323                        }
     324                        else if(tempCounter == 0)
     325                        {
     326                            tempCounter = 1;
     327                        }
     328                        if (tempCounter > equalSignCounter)
     329                            equalSignCounter = tempCounter;
     330                        }
     331                    }
     332                    {
     333                        size_t pos = 0;
     334                        while ((pos = temp.find(']', pos)) != std::string::npos)
     335                        {
     336                            unsigned int tempCounter = 1;
     337                            size_t tempPos = pos++;
     338                            while(temp[++tempPos] == '=')
     339                            {
     340                                tempCounter++;
     341                            }
     342                            if(temp[tempPos] != ']')
     343                            {
     344                                tempCounter = 0;
     345                            }
     346                            else if(tempCounter == 0)
     347                            {
     348                                tempCounter = 1;
     349                            }
     350                            if (tempCounter > equalSignCounter)
     351                                equalSignCounter = tempCounter;
     352                        }
     353                    }
     354                    std::string equalSigns = "";
     355                    for(unsigned int i = 0; i < equalSignCounter; i++)
     356                    {
     357                        equalSigns += "=";
     358                    }
     359                    output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])";
     360                    start = end + 5;
     361                }
     362                else
     363                {
     364                    output << text.substr(start, end - start);
     365                    start = end + 2;
     366                }
     367
     368                bInPrintFunction = !bInPrintFunction;
     369            }
     370            while (end != std::string::npos);
     371        }
     372
     373        return output.str();
     374    }
    210375}
Note: See TracChangeset for help on using the changeset viewer.