[1115] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Benjamin Knecht |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Script.h" |
---|
| 30 | |
---|
| 31 | #include <fstream> |
---|
| 32 | #include <map> |
---|
| 33 | |
---|
| 34 | #include "CoreIncludes.h" |
---|
| 35 | |
---|
| 36 | extern "C" { |
---|
| 37 | #include <lualib.h> |
---|
| 38 | #include <lauxlib.h> |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | #include "tolua/tolua++.h" |
---|
| 42 | #include "tolua/tolua_bind.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | Script* Script::singletonRef = NULL; |
---|
| 47 | |
---|
| 48 | Script::Script() |
---|
| 49 | { |
---|
| 50 | luaState_ = lua_open(); |
---|
| 51 | luaSource_ = ""; |
---|
| 52 | #if LUA_VERSION_NUM == 501 |
---|
| 53 | luaL_openlibs(luaState_); |
---|
| 54 | #else |
---|
| 55 | luaopen_base(luaState_); |
---|
| 56 | luaopen_string(luaState_); |
---|
| 57 | luaopen_table(luaState_); |
---|
| 58 | luaopen_math(luaState_); |
---|
| 59 | luaopen_io(luaState_); |
---|
| 60 | luaopen_debug(luaState_); |
---|
| 61 | #endif |
---|
| 62 | tolua_orxonox_open(luaState_); |
---|
| 63 | output_ = ""; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void Script::luaPrint(std::string str) |
---|
| 67 | { |
---|
| 68 | output_ += str; |
---|
[1255] | 69 | // COUT(4) << "Lua_output!:" << std::endl << str << std::endl << "***" << std::endl; |
---|
| 70 | COUT(5) << str << std::endl; |
---|
[1115] | 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | @brief Loads the specified file line by line |
---|
| 75 | @param filename The filename of the file |
---|
| 76 | @param luaTags if true, the loaded file gets stripped off luaTags |
---|
| 77 | */ |
---|
| 78 | void Script::loadFile(std::string filename, bool luaTags) |
---|
| 79 | { |
---|
| 80 | std::ifstream file; |
---|
| 81 | file.open(filename.c_str(), std::fstream::in); |
---|
| 82 | |
---|
| 83 | if (!file.is_open()) |
---|
| 84 | { |
---|
| 85 | // some error msg |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | char line[1024]; |
---|
| 89 | std::string levelString = ""; |
---|
| 90 | |
---|
| 91 | while (file.good() && !file.eof()) |
---|
| 92 | { |
---|
| 93 | file.getline(line, 1024); |
---|
| 94 | levelString += line; |
---|
| 95 | levelString += "\n"; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | file.close(); |
---|
| 99 | //std::string output; |
---|
| 100 | |
---|
| 101 | if (luaTags) luaSource_ = replaceLuaTags(levelString); |
---|
[1255] | 102 | COUT(5) << "ParsedSourceCode: " << luaSource_ << std::endl; |
---|
[1115] | 103 | } |
---|
| 104 | |
---|
| 105 | #if LUA_VERSION_NUM != 501 |
---|
| 106 | const char * Script::lua_Chunkreader(lua_State *L, void *data, size_t *size) |
---|
| 107 | { |
---|
| 108 | LoadS* ls = ((LoadS*)data); |
---|
| 109 | if (ls->size == 0) return NULL; |
---|
| 110 | *size = ls->size; |
---|
| 111 | ls->size = 0; |
---|
| 112 | return ls->s; |
---|
| 113 | } |
---|
| 114 | #endif |
---|
| 115 | void Script::run() |
---|
| 116 | { |
---|
| 117 | int error = 0; |
---|
| 118 | std::string init = "local scr = orxonox.Script:getInstance()\nprint = function(s)\nscr:luaPrint(s)\nend\n"; |
---|
| 119 | init += luaSource_; |
---|
| 120 | #if LUA_VERSION_NUM == 501 |
---|
| 121 | error = luaL_loadstring(luaState_, init.c_str()); |
---|
| 122 | #else |
---|
| 123 | LoadS ls; |
---|
| 124 | ls.s = init.c_str(); |
---|
| 125 | ls.size = init.size(); |
---|
| 126 | error = lua_load(luaState_, &orxonox::Script::lua_Chunkreader, &ls, init.c_str()); |
---|
| 127 | #endif |
---|
| 128 | if (error == 0) |
---|
| 129 | error = lua_pcall(luaState_, 0, 0, 0); |
---|
| 130 | if (error != 0) COUT(2) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | unsigned int Script::getNextQuote(const std::string& text, unsigned int start) |
---|
| 134 | { |
---|
| 135 | unsigned int quote = start - 1; |
---|
| 136 | |
---|
| 137 | while ((quote = text.find('\"', quote + 1)) != std::string::npos) |
---|
| 138 | { |
---|
| 139 | unsigned int backslash = quote; |
---|
| 140 | unsigned int numbackslashes = 0; |
---|
| 141 | for (; backslash > 0; backslash--, numbackslashes++) |
---|
| 142 | if (text[backslash - 1] != '\\') |
---|
| 143 | break; |
---|
| 144 | |
---|
| 145 | if (numbackslashes % 2 == 0) |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return quote; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | std::string Script::replaceLuaTags(const std::string& text) |
---|
| 153 | { |
---|
| 154 | // chreate map with all Lua tags |
---|
| 155 | std::map<unsigned int, bool> luaTags; |
---|
| 156 | { |
---|
| 157 | unsigned int pos = 0; |
---|
| 158 | while ((pos = text.find("<?lua", pos)) != std::string::npos) |
---|
| 159 | luaTags[pos++] = true; |
---|
| 160 | } |
---|
| 161 | { |
---|
| 162 | unsigned int pos = 0; |
---|
| 163 | while ((pos = text.find("?>", pos)) != std::string::npos) |
---|
| 164 | luaTags[pos++] = false; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | // erase all tags from the map that are between two quotes |
---|
| 168 | { |
---|
| 169 | std::map<unsigned int, bool>::iterator it = luaTags.begin(); |
---|
| 170 | std::map<unsigned int, bool>::iterator it2 = it; |
---|
| 171 | bool bBetweenQuotes = false; |
---|
| 172 | unsigned int pos = 0; |
---|
| 173 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
---|
| 174 | { |
---|
| 175 | while ((it != luaTags.end()) && (it->first < pos)) |
---|
| 176 | { |
---|
| 177 | if (bBetweenQuotes) { |
---|
| 178 | it2++; |
---|
| 179 | if(it->second && !(it2->second) && it2->first < pos) |
---|
| 180 | it = ++it2; |
---|
| 181 | else |
---|
| 182 | luaTags.erase(it++); |
---|
| 183 | } |
---|
| 184 | else |
---|
| 185 | ++it; |
---|
| 186 | } |
---|
| 187 | bBetweenQuotes = !bBetweenQuotes; |
---|
| 188 | pos++; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | // check whether on every opening <?lua tag a closing ?> tag follows |
---|
| 193 | { |
---|
| 194 | bool expectedValue = true; |
---|
| 195 | for (std::map<unsigned int, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
---|
| 196 | { |
---|
| 197 | if ((*it).second == expectedValue) |
---|
| 198 | expectedValue = !expectedValue; |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | expectedValue = false; |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | if (!expectedValue) { |
---|
| 206 | // todo: errorhandling |
---|
| 207 | return ""; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | // cut the original string into pieces and put them together with print() instead of lua tags |
---|
| 212 | std::string output; |
---|
| 213 | { |
---|
| 214 | std::map<unsigned int, bool>::iterator it = luaTags.begin(); |
---|
| 215 | bool bInPrintFunction = true; |
---|
| 216 | unsigned int start = 0; |
---|
| 217 | unsigned int end = 0; |
---|
| 218 | |
---|
| 219 | do |
---|
| 220 | { |
---|
| 221 | if (it != luaTags.end()) |
---|
| 222 | end = (*(it++)).first; |
---|
| 223 | else |
---|
| 224 | end = std::string::npos; |
---|
| 225 | |
---|
| 226 | unsigned int equalSignCounter = 0; |
---|
| 227 | |
---|
| 228 | if (bInPrintFunction) |
---|
| 229 | { |
---|
| 230 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) |
---|
| 231 | std::string temp = text.substr(start, end - start); |
---|
| 232 | { |
---|
| 233 | unsigned int pos = 0; |
---|
| 234 | while ((pos = temp.find('[', pos)) != std::string::npos) |
---|
| 235 | { |
---|
| 236 | unsigned int tempCounter = 1; |
---|
| 237 | unsigned int tempPos = pos++; |
---|
| 238 | while(temp[++tempPos] == '=') { |
---|
| 239 | tempCounter++; |
---|
| 240 | } |
---|
| 241 | if(temp[tempPos] != '[') { |
---|
| 242 | tempCounter = 0; |
---|
| 243 | } |
---|
| 244 | else if(tempCounter == 0) { |
---|
| 245 | tempCounter = 1; |
---|
| 246 | } |
---|
| 247 | if (tempCounter > equalSignCounter) |
---|
| 248 | equalSignCounter = tempCounter; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | { |
---|
| 252 | unsigned int pos = 0; |
---|
| 253 | while ((pos = temp.find(']', pos)) != std::string::npos) |
---|
| 254 | { |
---|
| 255 | unsigned int tempCounter = 1; |
---|
| 256 | unsigned int tempPos = pos++; |
---|
| 257 | while(temp[++tempPos] == '=') { |
---|
| 258 | tempCounter++; |
---|
| 259 | } |
---|
| 260 | if(temp[tempPos] != ']') { |
---|
| 261 | tempCounter = 0; |
---|
| 262 | } |
---|
| 263 | else if(tempCounter == 0) { |
---|
| 264 | tempCounter = 1; |
---|
| 265 | } |
---|
| 266 | if (tempCounter > equalSignCounter) |
---|
| 267 | equalSignCounter = tempCounter; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | std::string equalSigns = ""; |
---|
| 271 | for(unsigned int i = 0; i < equalSignCounter; i++) { |
---|
| 272 | equalSigns += "="; |
---|
| 273 | } |
---|
| 274 | output += "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])"; |
---|
| 275 | start = end + 5; |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | output += text.substr(start, end - start); |
---|
| 280 | start = end + 2; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | bInPrintFunction = !bInPrintFunction; |
---|
| 284 | } |
---|
| 285 | while (end != std::string::npos); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | return output; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | } |
---|