Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 996


Ignore:
Timestamp:
Apr 5, 2008, 9:47:12 PM (16 years ago)
Author:
bknecht
Message:

tolua++ works now. A level will now be sent through lua, before getting loaded by tinyxml.

Location:
code/branches/script
Files:
4 added
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/script/CMakeLists.txt

    r946 r996  
    7272FIND_PACKAGE(ZLIB)
    7373FIND_PACKAGE(Lua)
     74FIND_PACKAGE(ToLua)
    7475
    7576#Sets the search paths for the linking
  • code/branches/script/bin/levels/sample.oxw

    r888 r996  
    88                        <ogg src="tha_silent_partner_-_void" />
    99                </ambient>
    10   </audio>
     10  </audio>
     11
    1112-->
    1213
  • code/branches/script/bin/plugins.cfg-init

    r790 r996  
    22
    33# Define plugin folder
    4 PluginFolder=/usr/lib/OGRE
     4PluginFolder=/usr/local/lib/OGRE
    55
    66# Define plugins
  • code/branches/script/src/orxonox/CMakeLists.txt

    r946 r996  
    3333  objects/weapon/WeaponStation.cc
    3434  script/Script.cc
     35  script/tolua_script.cc
    3536)
    3637
     
    4344  ${OIS_LIBRARIES}
    4445  ${Lua_LIBRARIES}
     46  ${ToLua_LIBRARIES}
    4547  util
    4648  core
  • code/branches/script/src/orxonox/core/Loader.cc

    r871 r996  
    3333#include "Debug.h"
    3434#include "CoreIncludes.h"
     35#include "../script/Script.h"
    3536
    3637#include "util/tinyxml/ticpp.h"
     
    107108        Loader::currentMask_s = level->getMask() * mask;
    108109
     110        // let Lua work this out:
     111        //Script* lua;
     112        Script::loadFile(level->getFile(), true);
     113        Script::init(Script::getLuaState());
     114        Script::run();
     115
    109116        try
    110117        {
     
    112119            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
    113120
    114             ticpp::Document xmlfile(level->getFile());
    115             xmlfile.LoadFile();
     121            //ticpp::Document xmlfile(level->getFile());
     122            //xmlfile.LoadFile();
     123            //ticpp::Element myelement(*Script::getFileString());
     124            ticpp::Document xmlfile;
     125            //xmlfile.ToDocument();
     126            xmlfile.Parse(*Script::getFileString(), true);
    116127
    117128            for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ )
  • code/branches/script/src/orxonox/script/Script.cc

    r956 r996  
    2929
    3030#include <fstream>
     31#include <map>
     32
     33#include "../core/CoreIncludes.h"
    3134
    3235extern "C" {
     
    3538}
    3639
     40#include "tolua++.h"
     41#include "tolua_script.h"
    3742
    3843namespace orxonox
    3944{
     45
     46  lua_State* Script::luaState_ = lua_open();
     47  //lua_State* Script::luaState_ = NULL;
     48  std::string Script::fileString_ = "";
     49
    4050  Script::Script()
    4151  {
    42     fileLines_ = std::list<std::string>();
    43 
    44     state_ = lua_open();
     52  }
     53
     54  Script::~Script()
     55  {
     56
     57  }
     58
     59  /**
     60      @brief Initializes a lua state
     61      @param state_ the pointer of the lua_state to initialise
     62  */
     63  void Script::init(lua_State *state_)
     64  {
     65    tolua_script_open(state_);
     66
     67    /*
    4568#if LUA_VERSION_NUM == 501
    4669    luaL_openlibs(state_);
     
    5376    luaopen_debug(state_);
    5477#endif
    55   }
    56 
    57   Script::~Script()
    58   {
    59 
     78    */
     79  }
     80
     81  void Script::luaPrint(std::string str)
     82  {
     83    fileString_ = str;
    6084  }
    6185
     
    6387      @brief Loads the specified file line by line
    6488      @param filename The filename of the file
     89      @param luaTags if true, the loaded file gets stripped off luaTags
    6590  */
    66   void Script::loadFile(std::string filename)
     91  void Script::loadFile(std::string filename, bool luaTags)
    6792  {
    6893    std::ifstream file;
     
    79104    {
    80105      file.getline(line, 1024);
    81       this->fileLines_.push_back(line);
    82     }
    83 
    84     // The last line is useless
    85     this->fileLines_.pop_back();
     106      fileString_ += line;
     107    }
    86108
    87109    file.close();
    88   }
    89 
    90   /**
    91       @brief Parses the level file to correct Lua code
    92   */
    93   void Script::xmlToLua()
    94   {
    95     // We will iterate through all the lines and replace things.
    96     std::list<std::string>::iterator it;
    97     for(it = this->fileLines_.begin(); it != this->fileLines_.end(); ++it)
    98     {
    99       int pos = (*it).find("<?lua");
    100       while (pos < (int)(*it).length())
     110    //std::string output;
     111
     112    if (luaTags) fileString_ = replaceLuaTags(Script::fileString_);
     113  }
     114
     115  void Script::run()
     116  {
     117    int error = 0;
     118    std::string init = "local scr = orxonox.Script:new()\n";
     119    init += fileString_;
     120    error = luaL_loadstring(luaState_, init.c_str());
     121    if (error == 0)
     122      error = lua_pcall(luaState_, 0, 0, 0);
     123    if (error != 0) COUT(0) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl;
     124  }
     125
     126
     127  unsigned int Script::getNextQuote(const std::string& text, unsigned int start)
     128  {
     129    unsigned int quote = start - 1;
     130
     131    while ((quote = text.find('\"', quote + 1)) != std::string::npos)
     132    {
     133      unsigned int backslash = quote;
     134      unsigned int numbackslashes = 0;
     135      for (; backslash > 0; backslash--, numbackslashes++)
     136        if (text[backslash - 1] != '\\')
     137          break;
     138
     139      if (numbackslashes % 2 == 0)
     140        break;
     141    }
     142
     143    return quote;
     144  }
     145
     146  std::string Script::replaceLuaTags(const std::string& text)
     147  {
     148    // chreate map with all Lua tags
     149    std::map<unsigned int, bool> luaTags;
     150    {
     151      unsigned int pos = 0;
     152      while ((pos = text.find("<?lua", pos)) != std::string::npos)
     153        luaTags[pos++] = true;
     154    }
     155    {
     156      unsigned int pos = 0;
     157      while ((pos = text.find("?>", pos)) != std::string::npos)
     158        luaTags[pos++] = false;
     159    }
     160
     161    // erase all tags from the map that are between two quotes
     162    {
     163      std::map<unsigned int, bool>::iterator it = luaTags.begin();
     164      bool bBetweenQuotes = false;
     165      unsigned int pos = 0;
     166      while ((pos = getNextQuote(text, pos)) != std::string::npos)
    101167      {
    102         // We found a lua tag
    103         std::string front = (*it).substr(0,pos);
    104         std::string back = (*it).substr(pos + 5);
    105         (*it) = front + "]])" + back;
    106         pos = (*it).find("<?lua");
    107       }
    108 
    109       pos = (*it).find("?>");
    110       while (pos < (int)(*it).length())
     168        while ((it != luaTags.end()) && ((*it).first < pos))
     169        {
     170          if (bBetweenQuotes)
     171            luaTags.erase(it++);
     172          else
     173            ++it;
     174        }
     175        bBetweenQuotes = !bBetweenQuotes;
     176        pos++;
     177      }
     178    }
     179
     180    // check whether on every opening <?lua tag a closing ?> tag follows
     181    {
     182      bool expectedValue = true;
     183      for (std::map<unsigned int, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
    111184      {
    112         // We found a lua tag
    113         std::string front = (*it).substr(0,pos);
    114         std::string back = (*it).substr(pos + 2);
    115         (*it) = front + "print([[" + back;
    116         pos = (*it).find("?>");
    117       }
    118     }
    119     this->fileLines_.push_front("print([[");
    120     this->fileLines_.push_back("]])");
     185        if ((*it).second == expectedValue)
     186          expectedValue = !expectedValue;
     187        else
     188        {
     189          expectedValue = false;
     190          break;
     191        }
     192      }
     193      if (!expectedValue) {
     194        // todo: errorhandling
     195        return "";
     196      }
     197    }
     198
     199    // cut the original string into pieces and put them together with print() instead of lua tags
     200    std::string output;
     201    {
     202      std::map<unsigned int, bool>::iterator it = luaTags.begin();
     203      bool bInPrintFunction = true;
     204      unsigned int start = 0;
     205      unsigned int end = 0;
     206
     207      do
     208      {
     209        if (it != luaTags.end())
     210          end = (*(it++)).first;
     211        else
     212          end = std::string::npos;
     213
     214        unsigned int equalSignCounter = 0;
     215
     216        if (bInPrintFunction)
     217        {
     218          // count ['='[ and ]'='] and replace tags with print([[ and ]])
     219          std::string temp = text.substr(start, end - start);
     220          {
     221            unsigned int pos = 0;
     222            while ((pos = temp.find('[', pos)) != std::string::npos)
     223            {
     224              unsigned int tempCounter = 1;
     225              unsigned int tempPos = pos++;
     226              while(temp[++tempPos] == '=') {
     227                tempCounter++;
     228              }
     229              if(temp[tempPos] != '[') {
     230                tempCounter = 0;
     231              }
     232              else if(tempCounter == 0) {
     233                tempCounter = 1;
     234              }
     235              if (tempCounter > equalSignCounter)
     236                equalSignCounter = tempCounter;
     237            }
     238          }
     239          {
     240            unsigned int pos = 0;
     241            while ((pos = temp.find(']', pos)) != std::string::npos)
     242            {
     243              unsigned int tempCounter = 1;
     244              unsigned int tempPos = pos++;
     245              while(temp[++tempPos] == '=') {
     246                tempCounter++;
     247              }
     248              if(temp[tempPos] != ']') {
     249                tempCounter = 0;
     250              }
     251              else if(tempCounter == 0) {
     252                tempCounter = 1;
     253              }
     254              if (tempCounter > equalSignCounter)
     255                equalSignCounter = tempCounter;
     256            }
     257          }
     258          std::string equalSigns = "";
     259          for(unsigned int i = 0; i < equalSignCounter; i++) {
     260            equalSigns += "=";
     261          }
     262          output += "scr:luaPrint([" + equalSigns + "[" + temp + "]" + equalSigns +"])\n";
     263          start = end + 5;
     264        }
     265        else
     266        {
     267          output += text.substr(start, end - start);
     268          start = end + 2;
     269        }
     270
     271        bInPrintFunction = !bInPrintFunction;
     272      }
     273      while (end != std::string::npos);
     274    }
     275
     276    return output;
    121277  }
    122278
  • code/branches/script/src/orxonox/script/Script.h

    r956 r996  
    11/**
    22 @file  script.h
    3  @brief Representation of a lua script
     3 @brief Representation of an interface to lua
    44 @author Benjamin Knecht <beni_at_orxonox.net>
    55 */
     
    1515#include <string>
    1616
    17 namespace orxonox
    18 {
     17namespace orxonox // tolua_export
     18{ // tolua_export
    1919
    20   class Script
    21   {
    22     Script();
     20  class Script // tolua_export
     21  { // tolua_export
     22    public:
     23    Script(); // tolua_export
    2324    ~Script();
    2425
    25     void loadFile(std::string filename);
    26     void xmlToLua();
     26    static void loadFile(std::string filename, bool luaTags);
     27    static void init(lua_State *state_);
     28    //void xmlToLua();
     29    static void run();
     30    void luaPrint(std::string str); // tolua_export
    2731
    28     inline lua_State* getLuaState() { return state_; };
     32    inline static lua_State* getLuaState() { return luaState_; };
     33    inline static std::string* getFileString() { return &fileString_; };
     34
     35    static unsigned int getNextQuote(const std::string& text, unsigned int start);
     36    static std::string replaceLuaTags(const std::string& text);
    2937
    3038    private:
     
    3240      //std::list<std::string>& getLevelFileLines();
    3341
    34       std::list<std::string> fileLines_;
    35       lua_State* state_;
     42      static std::string fileString_;
     43      static lua_State* luaState_;
    3644
    37   };
    38 }
     45  }; // tolua_export
     46} // tolua_export
    3947#endif /* _Script_H__ */
Note: See TracChangeset for help on using the changeset viewer.