1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Benjamin Knecht |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "Script.h" |
---|
29 | |
---|
30 | #include <fstream> |
---|
31 | |
---|
32 | extern "C" { |
---|
33 | #include "lualib.h" |
---|
34 | #include "lauxlib.h" |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | Script::Script() |
---|
41 | { |
---|
42 | fileLines_ = std::list<std::string>(); |
---|
43 | |
---|
44 | state_ = lua_open(); |
---|
45 | #if LUA_VERSION_NUM == 501 |
---|
46 | luaL_openlibs(state_); |
---|
47 | #else |
---|
48 | luaopen_base(state_); |
---|
49 | luaopen_table(state_); |
---|
50 | luaopen_io(state_); |
---|
51 | luaopen_string(state_); |
---|
52 | luaopen_math(state_); |
---|
53 | luaopen_debug(state_); |
---|
54 | #endif |
---|
55 | } |
---|
56 | |
---|
57 | Script::~Script() |
---|
58 | { |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
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 | |
---|
123 | } |
---|