[1959] | 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 "LuaBind.h" |
---|
| 30 | |
---|
| 31 | #include <fstream> |
---|
| 32 | #include <map> |
---|
| 33 | |
---|
| 34 | #include "lua/lua.hpp" |
---|
| 35 | #include "tolua/tolua++.h" |
---|
| 36 | #include "tolua/tolua_bind.h" |
---|
| 37 | #include "util/String.h" |
---|
| 38 | #include "CoreIncludes.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | LuaBind* LuaBind::singletonRef = NULL; |
---|
| 43 | |
---|
| 44 | LuaBind::LuaBind() |
---|
| 45 | { |
---|
| 46 | luaState_ = lua_open(); |
---|
| 47 | luaSource_ = ""; |
---|
| 48 | #if LUA_VERSION_NUM == 501 |
---|
| 49 | luaL_openlibs(luaState_); |
---|
| 50 | #else |
---|
| 51 | luaopen_base(luaState_); |
---|
| 52 | luaopen_string(luaState_); |
---|
| 53 | luaopen_table(luaState_); |
---|
| 54 | luaopen_math(luaState_); |
---|
| 55 | luaopen_io(luaState_); |
---|
| 56 | luaopen_debug(luaState_); |
---|
| 57 | #endif |
---|
| 58 | tolua_Core_open(luaState_); |
---|
| 59 | output_ = ""; |
---|
| 60 | isRunning_ = false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void LuaBind::luaPrint(std::string str) |
---|
| 64 | { |
---|
| 65 | output_ += str; |
---|
| 66 | // COUT(4) << "Lua_output!:" << std::endl << str << std::endl << "***" << std::endl; |
---|
| 67 | COUT(5) << str; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | @brief Loads the specified file line by line |
---|
| 72 | @param filename The filename of the file |
---|
| 73 | @param luaTags if true, the loaded file gets stripped off luaTags |
---|
| 74 | */ |
---|
| 75 | void LuaBind::loadFile(std::string filename, bool luaTags) |
---|
| 76 | { |
---|
| 77 | output_ = ""; |
---|
| 78 | std::ifstream file; |
---|
| 79 | file.open(filename.c_str(), std::fstream::in); |
---|
| 80 | |
---|
| 81 | if (!file.is_open()) |
---|
| 82 | { |
---|
| 83 | // some error msg |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | char line[1024]; |
---|
| 87 | std::string levelString = ""; |
---|
| 88 | |
---|
| 89 | while (file.good() && !file.eof()) |
---|
| 90 | { |
---|
| 91 | file.getline(line, 1024); |
---|
| 92 | levelString += line; |
---|
| 93 | levelString += "\n"; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | file.close(); |
---|
| 97 | //std::string output; |
---|
| 98 | |
---|
| 99 | if (luaTags) luaSource_ = replaceLuaTags(levelString); |
---|
| 100 | COUT(5) << "ParsedSourceCode: " << luaSource_ << std::endl; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void LuaBind::loadString(std::string code) |
---|
| 104 | { |
---|
| 105 | luaSource_ = code; |
---|
| 106 | output_ = ""; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | #if LUA_VERSION_NUM != 501 |
---|
| 110 | const char * LuaBind::lua_Chunkreader(lua_State *L, void *data, size_t *size) |
---|
| 111 | { |
---|
| 112 | LoadS* ls = ((LoadS*)data); |
---|
| 113 | if (ls->size == 0) return NULL; |
---|
| 114 | *size = ls->size; |
---|
| 115 | ls->size = 0; |
---|
| 116 | return ls->s; |
---|
| 117 | } |
---|
| 118 | #endif |
---|
| 119 | void LuaBind::run() |
---|
| 120 | { |
---|
| 121 | if (!isRunning_) |
---|
| 122 | { |
---|
| 123 | isRunning_ = true; |
---|
| 124 | int error = 0; |
---|
[2026] | 125 | std::string init = "local scr = orxonox.LuaBind:getInstance()\nlocal debug = print\nprint = function(s)\nscr:luaPrint(s)\nend\ninclude = function(f)\nfile = assert(io.open(\"" + this->includePath_ + "\"..\"/\"..f))\ncontent = file:read(\"*a\")\nfile:close()\nsource = scr:replaceLuaTags(content)\nassert(loadstring(source))()\nend\n"; |
---|
[1959] | 126 | init += luaSource_; |
---|
| 127 | #if LUA_VERSION_NUM == 501 |
---|
| 128 | error = luaL_loadstring(luaState_, init.c_str()); |
---|
| 129 | #else |
---|
| 130 | LoadS ls; |
---|
| 131 | ls.s = init.c_str(); |
---|
| 132 | ls.size = init.size(); |
---|
| 133 | error = lua_load(luaState_, &orxonox::LuaBind::lua_Chunkreader, &ls, init.c_str()); |
---|
| 134 | #endif |
---|
| 135 | if (error == 0) |
---|
| 136 | { |
---|
| 137 | error = lua_pcall(luaState_, 0, 0, 0); |
---|
| 138 | } |
---|
| 139 | if (error != 0) |
---|
| 140 | { |
---|
| 141 | COUT(2) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl; |
---|
| 142 | } |
---|
| 143 | isRunning_ = false; |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | { |
---|
| 147 | COUT(2) << "Warning: Lua's run is called while running!" << std::endl; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | std::string LuaBind::replaceLuaTags(const std::string& text) |
---|
| 152 | { |
---|
| 153 | // chreate map with all Lua tags |
---|
| 154 | std::map<size_t, bool> luaTags; |
---|
| 155 | { |
---|
| 156 | size_t pos = 0; |
---|
| 157 | while ((pos = text.find("<?lua", pos)) != std::string::npos) |
---|
| 158 | luaTags[pos++] = true; |
---|
| 159 | } |
---|
| 160 | { |
---|
| 161 | size_t pos = 0; |
---|
| 162 | while ((pos = text.find("?>", pos)) != std::string::npos) |
---|
| 163 | luaTags[pos++] = false; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // erase all tags from the map that are between two quotes |
---|
| 167 | { |
---|
| 168 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
| 169 | std::map<size_t, bool>::iterator it2 = it; |
---|
| 170 | bool bBetweenQuotes = false; |
---|
| 171 | size_t pos = 0; |
---|
| 172 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
---|
| 173 | { |
---|
| 174 | while ((it != luaTags.end()) && (it->first < pos)) |
---|
| 175 | { |
---|
| 176 | if (bBetweenQuotes) { |
---|
| 177 | it2++; |
---|
| 178 | if(it->second && !(it2->second) && it2->first < pos) |
---|
| 179 | it = ++it2; |
---|
| 180 | else |
---|
| 181 | luaTags.erase(it++); |
---|
| 182 | } |
---|
| 183 | else |
---|
| 184 | ++it; |
---|
| 185 | } |
---|
| 186 | bBetweenQuotes = !bBetweenQuotes; |
---|
| 187 | pos++; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | // check whether on every opening <?lua tag a closing ?> tag follows |
---|
| 192 | { |
---|
| 193 | bool expectedValue = true; |
---|
| 194 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
---|
| 195 | { |
---|
| 196 | if (it->second == expectedValue) |
---|
| 197 | expectedValue = !expectedValue; |
---|
| 198 | else |
---|
| 199 | { |
---|
| 200 | expectedValue = false; |
---|
| 201 | break; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | if (!expectedValue) { |
---|
| 205 | COUT(2) << "Warning: Error in level file" << std::endl; |
---|
| 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<size_t, bool>::iterator it = luaTags.begin(); |
---|
| 215 | bool bInPrintFunction = true; |
---|
| 216 | size_t start = 0; |
---|
[1961] | 217 | size_t end = 0; |
---|
[1959] | 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 | size_t pos = 0; |
---|
| 234 | while ((pos = temp.find('[', pos)) != std::string::npos) |
---|
| 235 | { |
---|
| 236 | unsigned int tempCounter = 1; |
---|
| 237 | size_t 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 | size_t pos = 0; |
---|
| 253 | while ((pos = temp.find(']', pos)) != std::string::npos) |
---|
| 254 | { |
---|
| 255 | unsigned int tempCounter = 1; |
---|
| 256 | size_t 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 | } |
---|