[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 | |
---|
[5654] | 32 | #include <tolua/tolua++.h> |
---|
[2710] | 33 | extern "C" { |
---|
[5654] | 34 | #include <lua.h> |
---|
[2710] | 35 | #include <lualib.h> |
---|
| 36 | } |
---|
| 37 | |
---|
[3196] | 38 | #include "util/Debug.h" |
---|
[5781] | 39 | #include "Resource.h" |
---|
[5655] | 40 | #include "ToluaBindCore.h" |
---|
[1959] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[5654] | 44 | LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s; |
---|
| 45 | std::vector<LuaState*> LuaState::instances_s; |
---|
[1959] | 46 | |
---|
[5655] | 47 | // Do this after declaring toluaInterfaces_s and instances_s to avoid larger problems |
---|
| 48 | DeclareToluaInterface(Core); |
---|
| 49 | |
---|
[5654] | 50 | LuaState::LuaState() |
---|
| 51 | : bIsRunning_(false) |
---|
| 52 | , includeParseFunction_(NULL) |
---|
| 53 | { |
---|
| 54 | // Create new lua state and configure it |
---|
| 55 | luaState_ = lua_open(); |
---|
[1959] | 56 | #if LUA_VERSION_NUM == 501 |
---|
[5654] | 57 | luaL_openlibs(luaState_); |
---|
[1959] | 58 | #else |
---|
[5654] | 59 | luaopen_base(luaState_); |
---|
| 60 | luaopen_string(luaState_); |
---|
| 61 | luaopen_table(luaState_); |
---|
| 62 | luaopen_math(luaState_); |
---|
| 63 | luaopen_io(luaState_); |
---|
| 64 | luaopen_debug(luaState_); |
---|
[1959] | 65 | #endif |
---|
[3370] | 66 | |
---|
[5654] | 67 | // Open all available tolua interfaces |
---|
| 68 | this->openToluaInterfaces(luaState_); |
---|
[3370] | 69 | |
---|
[5654] | 70 | // Create dummy file info |
---|
| 71 | sourceFileInfo_.reset(new ResourceInfo()); |
---|
[5781] | 72 | sourceFileInfo_->group = "General"; |
---|
| 73 | sourceFileInfo_->size = 0; |
---|
[1959] | 74 | |
---|
[5759] | 75 | // Push 'this' pointer |
---|
[5654] | 76 | tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState"); |
---|
| 77 | lua_setglobal(luaState_, "luaState"); |
---|
[3370] | 78 | |
---|
[5654] | 79 | // Parse init script |
---|
[5660] | 80 | this->doFile("LuaStateInit.lua"); |
---|
[5654] | 81 | } |
---|
[1959] | 82 | |
---|
[5654] | 83 | LuaState::~LuaState() |
---|
| 84 | { |
---|
| 85 | lua_close(luaState_); |
---|
| 86 | } |
---|
[2710] | 87 | |
---|
[6417] | 88 | shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename) |
---|
[5654] | 89 | { |
---|
[6417] | 90 | // Look in the current directory first |
---|
| 91 | shared_ptr<ResourceInfo> sourceInfo = Resource::getInfo(sourceFileInfo_->path + filename); |
---|
| 92 | // Continue search in root directories |
---|
| 93 | if (sourceInfo == NULL && !sourceFileInfo_->path.empty()) |
---|
| 94 | sourceInfo = Resource::getInfo(filename); |
---|
[5654] | 95 | return sourceInfo; |
---|
| 96 | } |
---|
| 97 | |
---|
[6417] | 98 | void LuaState::includeFile(const std::string& filename) |
---|
[1959] | 99 | { |
---|
[6417] | 100 | shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename); |
---|
[5654] | 101 | if (sourceInfo != NULL) |
---|
[6417] | 102 | this->includeString(Resource::open(sourceInfo)->getAsString(), sourceInfo); |
---|
[5654] | 103 | else |
---|
[6417] | 104 | COUT(2) << "LuaState: Cannot include file '" << filename << "'." << std::endl; |
---|
[1959] | 105 | } |
---|
| 106 | |
---|
[6417] | 107 | void LuaState::includeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo) |
---|
[5654] | 108 | { |
---|
| 109 | // Parse string with provided include parser (otherwise don't preparse at all) |
---|
| 110 | std::string luaInput; |
---|
| 111 | if (includeParseFunction_ != NULL) |
---|
| 112 | luaInput = (*includeParseFunction_)(code); |
---|
| 113 | else |
---|
| 114 | luaInput = code; |
---|
[1959] | 115 | |
---|
[6655] | 116 | if (sourceFileInfo != NULL) |
---|
| 117 | { |
---|
| 118 | // Also fill a map with the actual source code. This is just for the include* commands |
---|
| 119 | // where the content of sourceFileInfo->filename doesn't match 'code' |
---|
| 120 | this->sourceCodeMap_[sourceFileInfo->filename] = code; |
---|
| 121 | } |
---|
| 122 | |
---|
[5654] | 123 | this->doString(luaInput, sourceFileInfo); |
---|
[6655] | 124 | |
---|
| 125 | if (sourceFileInfo != NULL) |
---|
| 126 | { |
---|
| 127 | // Delete source code entry |
---|
| 128 | if (sourceFileInfo != NULL) |
---|
| 129 | this->sourceCodeMap_.erase(sourceFileInfo->filename); |
---|
| 130 | } |
---|
[5654] | 131 | } |
---|
| 132 | |
---|
[6417] | 133 | void LuaState::doFile(const std::string& filename) |
---|
[1959] | 134 | { |
---|
[6417] | 135 | shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename); |
---|
[5654] | 136 | if (sourceInfo != NULL) |
---|
[6417] | 137 | this->doString(Resource::open(sourceInfo)->getAsString(), sourceInfo); |
---|
[5654] | 138 | else |
---|
[6417] | 139 | COUT(2) << "LuaState: Cannot do file '" << filename << "'." << std::endl; |
---|
[1959] | 140 | } |
---|
| 141 | |
---|
[6417] | 142 | void LuaState::doString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo) |
---|
[5654] | 143 | { |
---|
[6417] | 144 | // Save the old source file info |
---|
[5654] | 145 | shared_ptr<ResourceInfo> oldSourceFileInfo = sourceFileInfo_; |
---|
| 146 | // Only override if sourceFileInfo provides useful information |
---|
| 147 | if (sourceFileInfo != NULL) |
---|
| 148 | sourceFileInfo_ = sourceFileInfo; |
---|
[1959] | 149 | |
---|
[6625] | 150 | std::string chunkname; |
---|
[6626] | 151 | if (sourceFileInfo != NULL) |
---|
[6625] | 152 | { |
---|
| 153 | // Provide lua_load with the filename for debug purposes |
---|
| 154 | // The '@' is a Lua convention to identify the chunk name as filename |
---|
[6626] | 155 | chunkname = '@' + sourceFileInfo->filename; |
---|
[6625] | 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
[6626] | 159 | // Use the code string to identify the chunk |
---|
| 160 | chunkname = code; |
---|
[6625] | 161 | } |
---|
| 162 | |
---|
[5654] | 163 | int error = 0; |
---|
[1959] | 164 | #if LUA_VERSION_NUM != 501 |
---|
[5654] | 165 | LoadS ls; |
---|
| 166 | ls.s = code.c_str(); |
---|
| 167 | ls.size = code.size(); |
---|
[6625] | 168 | error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, chunkname.c_str()); |
---|
[5654] | 169 | #else |
---|
[6625] | 170 | error = luaL_loadbuffer(luaState_, code.c_str(), code.size(), chunkname.c_str()); |
---|
[1959] | 171 | #endif |
---|
[5654] | 172 | |
---|
| 173 | // execute the chunk |
---|
| 174 | if (error == 0) |
---|
[5661] | 175 | error = lua_pcall(luaState_, 0, 1, 0); |
---|
[5654] | 176 | if (error != 0) |
---|
| 177 | { |
---|
| 178 | std::string origin; |
---|
| 179 | if (sourceFileInfo != NULL) |
---|
| 180 | origin = " originating from " + sourceFileInfo_->filename; |
---|
[6417] | 181 | COUT(1) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl; |
---|
[6629] | 182 | // return value is true (not nil!) |
---|
| 183 | lua_pushboolean(luaState_, 1); |
---|
[5654] | 184 | } |
---|
[6629] | 185 | if (lua_isnil(luaState_, -1)) |
---|
| 186 | { |
---|
| 187 | // Nil return values cause problems |
---|
| 188 | lua_pop(luaState_, 1); |
---|
| 189 | lua_pushboolean(luaState_, 1); |
---|
| 190 | } |
---|
| 191 | // Push return value because it will get lost since the return value of this function is void |
---|
[5661] | 192 | lua_setglobal(luaState_, "LuaStateReturnValue"); |
---|
[5654] | 193 | |
---|
| 194 | // Load the old info again |
---|
| 195 | sourceFileInfo_ = oldSourceFileInfo; |
---|
[1959] | 196 | } |
---|
[5654] | 197 | |
---|
| 198 | void LuaState::luaPrint(const std::string& str) |
---|
[1959] | 199 | { |
---|
[5654] | 200 | output_ << str; |
---|
[1959] | 201 | } |
---|
| 202 | |
---|
[5654] | 203 | void LuaState::luaLog(unsigned int level, const std::string& message) |
---|
[1959] | 204 | { |
---|
[6105] | 205 | OutputHandler::getOutStream(level) << message << std::endl; |
---|
[1959] | 206 | } |
---|
[5654] | 207 | |
---|
[6417] | 208 | bool LuaState::fileExists(const std::string& filename) |
---|
[5661] | 209 | { |
---|
[6417] | 210 | shared_ptr<ResourceInfo> info = this->getFileInfo(filename); |
---|
[5661] | 211 | if (info == NULL) |
---|
| 212 | return false; |
---|
| 213 | else |
---|
| 214 | return true; |
---|
| 215 | } |
---|
| 216 | |
---|
[6626] | 217 | //! Returns the content of a file |
---|
| 218 | std::string LuaState::getSourceCode(const std::string& filename) |
---|
| 219 | { |
---|
[6627] | 220 | // Try the internal map first to get the actual Lua code |
---|
| 221 | // and not just some pseudo Lua-XML code when using include* commands |
---|
| 222 | std::map<std::string, std::string>::const_iterator it = this->sourceCodeMap_.find(filename); |
---|
| 223 | if (it != this->sourceCodeMap_.end()) |
---|
| 224 | return it->second; |
---|
[6626] | 225 | shared_ptr<ResourceInfo> info = Resource::getInfo(filename); |
---|
| 226 | if (info == NULL) |
---|
| 227 | return ""; |
---|
| 228 | else |
---|
| 229 | return Resource::open(info)->getAsString(); |
---|
| 230 | } |
---|
| 231 | |
---|
[5654] | 232 | #if LUA_VERSION_NUM != 501 |
---|
| 233 | const char * LuaState::lua_Chunkreader(lua_State *L, void *data, size_t *size) |
---|
[1959] | 234 | { |
---|
[5654] | 235 | LoadS* ls = static_cast<LoadS*>(data); |
---|
| 236 | if (ls->size == 0) |
---|
| 237 | return NULL; |
---|
| 238 | *size = ls->size; |
---|
| 239 | ls->size = 0; |
---|
| 240 | return ls->s; |
---|
[1959] | 241 | } |
---|
[5654] | 242 | #endif |
---|
[1959] | 243 | |
---|
[5654] | 244 | /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name) |
---|
[1959] | 245 | { |
---|
[5654] | 246 | for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) |
---|
[1959] | 247 | { |
---|
[5654] | 248 | if (it->first == name || it->second == function) |
---|
| 249 | { |
---|
| 250 | COUT(2) << "Warning: Trying to add a Tolua interface with the same name or function." << std::endl; |
---|
| 251 | return true; |
---|
| 252 | } |
---|
[1959] | 253 | } |
---|
[5654] | 254 | toluaInterfaces_s[name] = function; |
---|
| 255 | |
---|
| 256 | // Open interface in all LuaStates |
---|
| 257 | for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it) |
---|
| 258 | (*function)((*it)->luaState_); |
---|
| 259 | |
---|
| 260 | // Return dummy bool |
---|
| 261 | return true; |
---|
[1959] | 262 | } |
---|
| 263 | |
---|
[5654] | 264 | /*static*/ bool LuaState::removeToluaInterface(const std::string& name) |
---|
[1959] | 265 | { |
---|
[5654] | 266 | ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name); |
---|
| 267 | if (it == toluaInterfaces_s.end()) |
---|
[1959] | 268 | { |
---|
[5654] | 269 | COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl; |
---|
| 270 | return true; |
---|
[1959] | 271 | } |
---|
| 272 | |
---|
[5654] | 273 | // Close interface in all LuaStates |
---|
| 274 | for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState) |
---|
[1959] | 275 | { |
---|
[5654] | 276 | lua_pushnil((*itState)->luaState_); |
---|
| 277 | lua_setglobal((*itState)->luaState_, it->first.c_str()); |
---|
[1959] | 278 | } |
---|
| 279 | |
---|
[5654] | 280 | // Remove entry |
---|
| 281 | toluaInterfaces_s.erase(it); |
---|
| 282 | |
---|
| 283 | // Return dummy bool |
---|
| 284 | return true; |
---|
[1959] | 285 | } |
---|
| 286 | |
---|
[5654] | 287 | /*static*/ void LuaState::openToluaInterfaces(lua_State* state) |
---|
| 288 | { |
---|
| 289 | for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) |
---|
| 290 | (*it->second)(state); |
---|
| 291 | } |
---|
[1959] | 292 | |
---|
[5654] | 293 | /*static*/ void LuaState::closeToluaInterfaces(lua_State* state) |
---|
[3370] | 294 | { |
---|
[5654] | 295 | for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) |
---|
| 296 | { |
---|
| 297 | lua_pushnil(state); |
---|
| 298 | lua_setglobal(state, it->first.c_str()); |
---|
| 299 | } |
---|
[3370] | 300 | } |
---|
[6417] | 301 | |
---|
| 302 | |
---|
| 303 | LuaFunctor::LuaFunctor(const std::string& code, LuaState* luaState) |
---|
| 304 | { |
---|
| 305 | this->code_ = code; |
---|
| 306 | this->lua_ = luaState; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | void LuaFunctor::operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) |
---|
| 310 | { |
---|
| 311 | lua_->doString(this->code_); |
---|
| 312 | } |
---|
[1959] | 313 | } |
---|