[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 |
---|
[5654] | 24 | * Reto Grieder |
---|
[1959] | 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
[5654] | 30 | #include "LuaState.h" |
---|
[1959] | 31 | |
---|
[8351] | 32 | #include <tolua++.h> |
---|
[2710] | 33 | extern "C" { |
---|
[5654] | 34 | #include <lua.h> |
---|
[2710] | 35 | #include <lualib.h> |
---|
| 36 | } |
---|
[7266] | 37 | #include <loki/ScopeGuard.h> |
---|
[2710] | 38 | |
---|
[8858] | 39 | #include "util/Output.h" |
---|
[6746] | 40 | #include "util/Exception.h" |
---|
[5781] | 41 | #include "Resource.h" |
---|
[7284] | 42 | #include "command/IOConsole.h" |
---|
[1959] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[6763] | 46 | const std::string LuaState::ERROR_HANDLER_NAME = "errorHandler"; |
---|
| 47 | |
---|
[5654] | 48 | LuaState::LuaState() |
---|
| 49 | : bIsRunning_(false) |
---|
[11071] | 50 | , includeParseFunction_(nullptr) |
---|
[5654] | 51 | { |
---|
| 52 | // Create new lua state and configure it |
---|
| 53 | luaState_ = lua_open(); |
---|
[6746] | 54 | Loki::ScopeGuard luaStateGuard = Loki::MakeGuard(&lua_close, luaState_); |
---|
[5654] | 55 | luaL_openlibs(luaState_); |
---|
[3370] | 56 | |
---|
[5654] | 57 | // Open all available tolua interfaces |
---|
| 58 | this->openToluaInterfaces(luaState_); |
---|
[3370] | 59 | |
---|
[5654] | 60 | // Create dummy file info |
---|
| 61 | sourceFileInfo_.reset(new ResourceInfo()); |
---|
[5781] | 62 | sourceFileInfo_->group = "General"; |
---|
| 63 | sourceFileInfo_->size = 0; |
---|
[1959] | 64 | |
---|
[5759] | 65 | // Push 'this' pointer |
---|
[5654] | 66 | tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState"); |
---|
| 67 | lua_setglobal(luaState_, "luaState"); |
---|
[3370] | 68 | |
---|
[5654] | 69 | // Parse init script |
---|
[6746] | 70 | if (!this->doFile("LuaStateInit.lua")) |
---|
| 71 | ThrowException(InitialisationFailed, "Running LuaStateInit.lua failed"); |
---|
| 72 | |
---|
| 73 | luaStateGuard.Dismiss(); |
---|
[5654] | 74 | } |
---|
[1959] | 75 | |
---|
[5654] | 76 | LuaState::~LuaState() |
---|
| 77 | { |
---|
| 78 | lua_close(luaState_); |
---|
| 79 | } |
---|
[2710] | 80 | |
---|
[11071] | 81 | std::shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename) |
---|
[5654] | 82 | { |
---|
[6417] | 83 | // Look in the current directory first |
---|
[11071] | 84 | std::shared_ptr<ResourceInfo> sourceInfo = Resource::getInfo(sourceFileInfo_->path + filename); |
---|
[6417] | 85 | // Continue search in root directories |
---|
[11071] | 86 | if (sourceInfo == nullptr && !sourceFileInfo_->path.empty()) |
---|
[6417] | 87 | sourceInfo = Resource::getInfo(filename); |
---|
[5654] | 88 | return sourceInfo; |
---|
| 89 | } |
---|
| 90 | |
---|
[6746] | 91 | bool LuaState::includeFile(const std::string& filename) |
---|
[1959] | 92 | { |
---|
[11071] | 93 | std::shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename); |
---|
| 94 | if (sourceInfo != nullptr) |
---|
[6746] | 95 | return this->includeString(Resource::open(sourceInfo)->getAsString(), sourceInfo); |
---|
[5654] | 96 | else |
---|
[6746] | 97 | { |
---|
[8858] | 98 | orxout(internal_warning, context::lua) << "LuaState: Cannot include file '" << filename << "' (not found)." << endl; |
---|
[6746] | 99 | return false; |
---|
| 100 | } |
---|
[1959] | 101 | } |
---|
| 102 | |
---|
[11071] | 103 | bool LuaState::includeString(const std::string& code, const std::shared_ptr<ResourceInfo>& sourceFileInfo) |
---|
[5654] | 104 | { |
---|
| 105 | // Parse string with provided include parser (otherwise don't preparse at all) |
---|
| 106 | std::string luaInput; |
---|
[11071] | 107 | if (includeParseFunction_ != nullptr) |
---|
[5654] | 108 | luaInput = (*includeParseFunction_)(code); |
---|
| 109 | else |
---|
| 110 | luaInput = code; |
---|
[1959] | 111 | |
---|
[11071] | 112 | if (sourceFileInfo != nullptr) |
---|
[6746] | 113 | { |
---|
| 114 | // Also fill a map with the actual source code. This is just for the include* commands |
---|
| 115 | // where the content of sourceFileInfo->filename doesn't match 'code' |
---|
| 116 | this->sourceCodeMap_[sourceFileInfo->filename] = code; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | bool returnValue = this->doString(luaInput, sourceFileInfo); |
---|
| 120 | |
---|
[11071] | 121 | if (sourceFileInfo != nullptr) |
---|
[6746] | 122 | { |
---|
| 123 | // Delete source code entry |
---|
[11071] | 124 | if (sourceFileInfo != nullptr) |
---|
[6746] | 125 | this->sourceCodeMap_.erase(sourceFileInfo->filename); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return returnValue; |
---|
[5654] | 129 | } |
---|
| 130 | |
---|
[6746] | 131 | bool LuaState::doFile(const std::string& filename) |
---|
[1959] | 132 | { |
---|
[11071] | 133 | std::shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename); |
---|
| 134 | if (sourceInfo != nullptr) |
---|
[6746] | 135 | return this->doString(Resource::open(sourceInfo)->getAsString(), sourceInfo); |
---|
[5654] | 136 | else |
---|
[6746] | 137 | { |
---|
[8858] | 138 | orxout(internal_warning, context::lua) << "LuaState: Cannot do file '" << filename << "' (not found)." << endl; |
---|
[6746] | 139 | return false; |
---|
| 140 | } |
---|
[1959] | 141 | } |
---|
| 142 | |
---|
[11071] | 143 | bool LuaState::doString(const std::string& code, const std::shared_ptr<ResourceInfo>& sourceFileInfo) |
---|
[5654] | 144 | { |
---|
[6417] | 145 | // Save the old source file info |
---|
[11071] | 146 | std::shared_ptr<ResourceInfo> oldSourceFileInfo = sourceFileInfo_; |
---|
[5654] | 147 | // Only override if sourceFileInfo provides useful information |
---|
[11071] | 148 | if (sourceFileInfo != nullptr) |
---|
[5654] | 149 | sourceFileInfo_ = sourceFileInfo; |
---|
[1959] | 150 | |
---|
[6746] | 151 | std::string chunkname; |
---|
[11071] | 152 | if (sourceFileInfo != nullptr) |
---|
[6746] | 153 | { |
---|
| 154 | // Provide lua_load with the filename for debug purposes |
---|
| 155 | // The '@' is a Lua convention to identify the chunk name as filename |
---|
| 156 | chunkname = '@' + sourceFileInfo->filename; |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
| 160 | // Use the code string to identify the chunk |
---|
| 161 | chunkname = code; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // Push custom error handler that uses the debugger |
---|
[6763] | 165 | lua_getglobal(this->luaState_, ERROR_HANDLER_NAME.c_str()); |
---|
[6746] | 166 | int errorHandler = lua_gettop(luaState_); |
---|
| 167 | if (lua_isnil(this->luaState_, -1)) |
---|
| 168 | { |
---|
| 169 | lua_pop(this->luaState_, 1); |
---|
| 170 | errorHandler = 0; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | int error = luaL_loadbuffer(luaState_, code.c_str(), code.size(), chunkname.c_str()); |
---|
[5654] | 174 | |
---|
[6746] | 175 | switch (error) |
---|
| 176 | { |
---|
| 177 | case LUA_ERRSYNTAX: // Syntax error |
---|
[8858] | 178 | orxout(internal_error, context::lua) << "Lua syntax error: " << lua_tostring(luaState_, -1) << endl; |
---|
[6746] | 179 | break; |
---|
| 180 | case LUA_ERRMEM: // Memory allocation error |
---|
[8858] | 181 | orxout(internal_error, context::lua) << "Lua memory allocation error: Consult your dentist immediately!" << endl; |
---|
[6746] | 182 | break; |
---|
| 183 | } |
---|
| 184 | |
---|
[5654] | 185 | if (error == 0) |
---|
[6746] | 186 | { |
---|
| 187 | // Execute the chunk in protected mode with an error handler function (stack index) |
---|
| 188 | error = lua_pcall(luaState_, 0, 1, errorHandler); |
---|
| 189 | |
---|
| 190 | switch (error) |
---|
| 191 | { |
---|
| 192 | case LUA_ERRRUN: // Runtime error |
---|
| 193 | if (errorHandler) |
---|
| 194 | { |
---|
| 195 | // Do nothing (we already display the error in the |
---|
| 196 | // 'errorHandler' Lua function in LuaStateInit.lua) |
---|
| 197 | } |
---|
| 198 | else |
---|
| 199 | { |
---|
| 200 | std::string errorString = lua_tostring(this->luaState_, -1); |
---|
| 201 | if (errorString.find("Error propagation") == std::string::npos) |
---|
[8858] | 202 | orxout(internal_error, context::lua) << "Lua runtime error: " << errorString << endl; |
---|
[6746] | 203 | } |
---|
| 204 | break; |
---|
| 205 | case LUA_ERRERR: // Error in the error handler |
---|
[8858] | 206 | orxout(internal_error, context::lua) << "Lua error in error handler. No message available." << endl; |
---|
[6746] | 207 | break; |
---|
| 208 | case LUA_ERRMEM: // Memory allocation error |
---|
[8858] | 209 | orxout(internal_error, context::lua) << "Lua memory allocation error: Consult your dentist immediately!" << endl; |
---|
[6746] | 210 | break; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
[5654] | 214 | if (error != 0) |
---|
| 215 | { |
---|
[6746] | 216 | lua_pop(luaState_, 1); // Remove error message |
---|
| 217 | lua_pushnil(luaState_); // Push a nil return value |
---|
[5654] | 218 | } |
---|
[6746] | 219 | |
---|
| 220 | if (errorHandler != 0) |
---|
| 221 | lua_remove(luaState_, errorHandler); // Remove error handler from stack |
---|
| 222 | |
---|
| 223 | // Set return value to a global variable because we cannot return a table in this function |
---|
| 224 | // here. It would work for numbers, pointers and strings, but certainly not for Lua tables. |
---|
[5661] | 225 | lua_setglobal(luaState_, "LuaStateReturnValue"); |
---|
[5654] | 226 | |
---|
| 227 | // Load the old info again |
---|
| 228 | sourceFileInfo_ = oldSourceFileInfo; |
---|
[6746] | 229 | |
---|
| 230 | return (error == 0); |
---|
[1959] | 231 | } |
---|
[5654] | 232 | |
---|
| 233 | void LuaState::luaPrint(const std::string& str) |
---|
[1959] | 234 | { |
---|
[10264] | 235 | if (lineTrace_) |
---|
| 236 | { |
---|
| 237 | //Get lua debug info of second level in stack (level 1 is function print in LuaStateInit.lua) |
---|
| 238 | lua_Debug ar; |
---|
| 239 | lua_getstack(luaState_, 2, &ar); |
---|
| 240 | lua_getinfo(luaState_, "nSl", &ar); |
---|
| 241 | int line = ar.currentline; |
---|
| 242 | std::string filename(ar.short_src); |
---|
| 243 | |
---|
| 244 | //Add first line, which always exists |
---|
| 245 | //Note: due to newlines etc., it's possible that one line consists of parts of |
---|
| 246 | // multiple, different files |
---|
[11071] | 247 | std::vector<std::vector<std::pair<std::string, size_t>>>::reverse_iterator it = lineTrace_->rbegin(); |
---|
[10264] | 248 | std::pair<std::string, size_t> temppair = std::make_pair(filename, line); |
---|
| 249 | //Avoid duplicate entries. This could happen if there were lua blocks on the same line |
---|
| 250 | if (it->size() == 0 || std::find(it->begin(), it->end(), temppair) == it->end()) |
---|
| 251 | { |
---|
| 252 | it->push_back(temppair); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | //Add the rest of the lines, if there are any. Empty or not doesn't matter |
---|
| 256 | size_t newlinecount = std::count(str.begin(), str.end(), '\n'); |
---|
| 257 | //i newlines -> i+1 lines, first line already added |
---|
| 258 | for (size_t i = 1; i <= newlinecount; i++) |
---|
| 259 | { |
---|
| 260 | //Add the new line to the trace map |
---|
[11071] | 261 | lineTrace_->emplace_back(); |
---|
[10264] | 262 | //Add the source of the line at the end |
---|
[11071] | 263 | lineTrace_->rbegin()->emplace_back(filename, line + i); |
---|
[10264] | 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
[5654] | 267 | output_ << str; |
---|
[1959] | 268 | } |
---|
| 269 | |
---|
[8858] | 270 | void LuaState::luaOutput(OutputLevel level, const std::string& context, const std::string& message) |
---|
[1959] | 271 | { |
---|
[8858] | 272 | orxout(level, registerContext(context)) << message << endl; |
---|
[1959] | 273 | } |
---|
[5654] | 274 | |
---|
[8858] | 275 | void LuaState::luaOutput(OutputLevel level, const std::string& message) |
---|
| 276 | { |
---|
| 277 | orxout(level, context::lua) << message << endl; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | void LuaState::luaOutput(const std::string& message) |
---|
| 281 | { |
---|
| 282 | orxout(debug_output, context::lua) << message << endl; |
---|
| 283 | } |
---|
| 284 | |
---|
[6417] | 285 | bool LuaState::fileExists(const std::string& filename) |
---|
[5661] | 286 | { |
---|
[11071] | 287 | std::shared_ptr<ResourceInfo> info = this->getFileInfo(filename); |
---|
| 288 | if (info == nullptr) |
---|
[5661] | 289 | return false; |
---|
| 290 | else |
---|
| 291 | return true; |
---|
| 292 | } |
---|
| 293 | |
---|
[6746] | 294 | //! Returns the content of a file |
---|
| 295 | std::string LuaState::getSourceCode(const std::string& filename) |
---|
| 296 | { |
---|
| 297 | // Try the internal map first to get the actual Lua code |
---|
| 298 | // and not just some pseudo Lua-XML code when using include* commands |
---|
| 299 | std::map<std::string, std::string>::const_iterator it = this->sourceCodeMap_.find(filename); |
---|
| 300 | if (it != this->sourceCodeMap_.end()) |
---|
| 301 | return it->second; |
---|
[11071] | 302 | std::shared_ptr<ResourceInfo> info = Resource::getInfo(filename); |
---|
| 303 | if (info == nullptr) |
---|
[6746] | 304 | return ""; |
---|
| 305 | else |
---|
| 306 | return Resource::open(info)->getAsString(); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | bool LuaState::usingIOConsole() const |
---|
| 310 | { |
---|
| 311 | return IOConsole::exists(); |
---|
| 312 | } |
---|
| 313 | |
---|
[8729] | 314 | /*static*/ LuaState::ToluaInterfaceMap& LuaState::getToluaInterfaces() |
---|
| 315 | { |
---|
| 316 | static ToluaInterfaceMap p; |
---|
| 317 | return p; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | /*static*/ std::vector<LuaState*>& LuaState::getInstances() |
---|
| 321 | { |
---|
| 322 | static std::vector<LuaState*> p; |
---|
| 323 | return p; |
---|
| 324 | } |
---|
| 325 | |
---|
[5654] | 326 | /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name) |
---|
[1959] | 327 | { |
---|
[11071] | 328 | for (const auto& mapEntry : getToluaInterfaces()) |
---|
[1959] | 329 | { |
---|
[11071] | 330 | if (mapEntry.first == name || mapEntry.second == function) |
---|
[5654] | 331 | { |
---|
[8858] | 332 | orxout(internal_warning, context::lua) << "Trying to add a Tolua interface with the same name or function." << endl; |
---|
[5654] | 333 | return true; |
---|
| 334 | } |
---|
[1959] | 335 | } |
---|
[8729] | 336 | getToluaInterfaces()[name] = function; |
---|
[5654] | 337 | |
---|
| 338 | // Open interface in all LuaStates |
---|
[11071] | 339 | for (LuaState* state : getInstances()) |
---|
| 340 | (*function)(state->luaState_); |
---|
[5654] | 341 | |
---|
| 342 | // Return dummy bool |
---|
| 343 | return true; |
---|
[1959] | 344 | } |
---|
| 345 | |
---|
[5654] | 346 | /*static*/ bool LuaState::removeToluaInterface(const std::string& name) |
---|
[1959] | 347 | { |
---|
[8729] | 348 | ToluaInterfaceMap::iterator it = getToluaInterfaces().find(name); |
---|
| 349 | if (it == getToluaInterfaces().end()) |
---|
[1959] | 350 | { |
---|
[8858] | 351 | orxout(internal_warning, context::lua) << "Cannot remove Tolua interface '" << name << "': Not found" << endl; |
---|
[5654] | 352 | return true; |
---|
[1959] | 353 | } |
---|
| 354 | |
---|
[5654] | 355 | // Close interface in all LuaStates |
---|
[11071] | 356 | for (LuaState* state : getInstances()) |
---|
[1959] | 357 | { |
---|
[11071] | 358 | lua_pushnil(state->luaState_); |
---|
| 359 | lua_setglobal(state->luaState_, it->first.c_str()); |
---|
[1959] | 360 | } |
---|
| 361 | |
---|
[5654] | 362 | // Remove entry |
---|
[8729] | 363 | getToluaInterfaces().erase(it); |
---|
[5654] | 364 | |
---|
| 365 | // Return dummy bool |
---|
| 366 | return true; |
---|
[1959] | 367 | } |
---|
| 368 | |
---|
[5654] | 369 | /*static*/ void LuaState::openToluaInterfaces(lua_State* state) |
---|
| 370 | { |
---|
[11071] | 371 | for (const auto& mapEntry : getToluaInterfaces()) |
---|
| 372 | (mapEntry.second)(state); |
---|
[5654] | 373 | } |
---|
[1959] | 374 | |
---|
[5654] | 375 | /*static*/ void LuaState::closeToluaInterfaces(lua_State* state) |
---|
[3370] | 376 | { |
---|
[11071] | 377 | for (const auto& mapEntry : getToluaInterfaces()) |
---|
[5654] | 378 | { |
---|
| 379 | lua_pushnil(state); |
---|
[11071] | 380 | lua_setglobal(state, mapEntry.first.c_str()); |
---|
[5654] | 381 | } |
---|
[3370] | 382 | } |
---|
[6417] | 383 | |
---|
| 384 | |
---|
| 385 | LuaFunctor::LuaFunctor(const std::string& code, LuaState* luaState) |
---|
| 386 | { |
---|
| 387 | this->code_ = code; |
---|
| 388 | this->lua_ = luaState; |
---|
| 389 | } |
---|
| 390 | |
---|
[7284] | 391 | void LuaFunctor::operator()() |
---|
[6417] | 392 | { |
---|
| 393 | lua_->doString(this->code_); |
---|
| 394 | } |
---|
[1959] | 395 | } |
---|