Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.