Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 956


Ignore:
Timestamp:
Mar 30, 2008, 4:03:19 AM (16 years ago)
Author:
bknecht
Message:

loads level from file and creates lua code from XML code. not tested yet though

Location:
code/branches/script
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/script/cmake/FindLua.cmake

    r954 r956  
    2323        /usr/local/lib)
    2424
    25 ADD_DEFINITIONS(-DLUA_VERSION=51)
    26 
    2725IF (NOT Lua_LIBRARIES AND Lua_INCLUDE_DIR)
    2826FIND_PATH(Lua_INCLUDE_DIR lua.h
     
    4240
    4341SET(Lua_LIBRARIES ${Lua_LIBRARIES} ${Lua_LIBRARY})
    44 ADD_DEFINITIONS(-DLUA_VERSION=50)
    4542
    4643ENDIF (NOT Lua_LIBRARIES AND Lua_INCLUDE_DIR)
  • code/branches/script/src/orxonox/script/Script.cc

    r954 r956  
    2727
    2828#include "Script.h"
     29
     30#include <fstream>
     31
    2932extern "C" {
    3033#include "lualib.h"
     
    3740  Script::Script()
    3841  {
     42    fileLines_ = std::list<std::string>();
     43
    3944    state_ = lua_open();
    40 #if Lua_VERSION == 51
     45#if LUA_VERSION_NUM == 501
    4146    luaL_openlibs(state_);
    4247#else
     
    5560  }
    5661
     62  /**
     63      @brief Loads the specified file line by line
     64      @param filename The filename of the file
     65  */
     66  void Script::loadFile(std::string filename)
     67  {
     68    std::ifstream file;
     69    file.open(filename.c_str(), std::fstream::in);
     70
     71    if (!file.is_open())
     72    {
     73      // some error msg
     74    }
     75
     76    char line[1024];
     77
     78    while (file.good() && !file.eof())
     79    {
     80      file.getline(line, 1024);
     81      this->fileLines_.push_back(line);
     82    }
     83
     84    // The last line is useless
     85    this->fileLines_.pop_back();
     86
     87    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())
     101      {
     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())
     111      {
     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("]])");
     121  }
     122
    57123}
  • code/branches/script/src/orxonox/script/Script.h

    r954 r956  
    1212}
    1313
     14#include <list>
     15#include <string>
     16
    1417namespace orxonox
    1518{
     
    2023    ~Script();
    2124
     25    void loadFile(std::string filename);
     26    void xmlToLua();
     27
    2228    inline lua_State* getLuaState() { return state_; };
    2329
    2430    private:
     31
     32      //std::list<std::string>& getLevelFileLines();
     33
     34      std::list<std::string> fileLines_;
    2535      lua_State* state_;
    2636
Note: See TracChangeset for help on using the changeset viewer.