[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" |
---|
[2710] | 39 | #include "Core.h" |
---|
[5654] | 40 | #include "Resource.h" |
---|
[1959] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[5654] | 44 | LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s; |
---|
| 45 | std::vector<LuaState*> LuaState::instances_s; |
---|
[1959] | 46 | |
---|
[5654] | 47 | LuaState::LuaState() |
---|
| 48 | : bIsRunning_(false) |
---|
| 49 | , includeParseFunction_(NULL) |
---|
| 50 | { |
---|
| 51 | // Create new lua state and configure it |
---|
| 52 | luaState_ = lua_open(); |
---|
[1959] | 53 | #if LUA_VERSION_NUM == 501 |
---|
[5654] | 54 | luaL_openlibs(luaState_); |
---|
[1959] | 55 | #else |
---|
[5654] | 56 | luaopen_base(luaState_); |
---|
| 57 | luaopen_string(luaState_); |
---|
| 58 | luaopen_table(luaState_); |
---|
| 59 | luaopen_math(luaState_); |
---|
| 60 | luaopen_io(luaState_); |
---|
| 61 | luaopen_debug(luaState_); |
---|
[1959] | 62 | #endif |
---|
[3370] | 63 | |
---|
[5654] | 64 | // Open all available tolua interfaces |
---|
| 65 | this->openToluaInterfaces(luaState_); |
---|
[3370] | 66 | |
---|
[5654] | 67 | // Create dummy file info |
---|
| 68 | sourceFileInfo_.reset(new ResourceInfo()); |
---|
| 69 | sourceFileInfo_->group = "General"; |
---|
| 70 | sourceFileInfo_->size = 0; |
---|
[1959] | 71 | |
---|
[5654] | 72 | // Push this pointer |
---|
| 73 | tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState"); |
---|
| 74 | lua_setglobal(luaState_, "luaState"); |
---|
[3370] | 75 | |
---|
[5654] | 76 | // Parse init script |
---|
| 77 | // Note: We have to use a hard coded path because the script is required for the resource loading |
---|
| 78 | this->doString("dofile(\"" + Core::getDataPathString() + "lua/LuaStateInit.lua\")"); |
---|
| 79 | } |
---|
[1959] | 80 | |
---|
[5654] | 81 | LuaState::~LuaState() |
---|
| 82 | { |
---|
| 83 | lua_close(luaState_); |
---|
| 84 | } |
---|
[2710] | 85 | |
---|
[5654] | 86 | shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) |
---|
| 87 | { |
---|
| 88 | shared_ptr<ResourceInfo> sourceInfo; |
---|
| 89 | if (resourceGroup != "NoResourceGroupProvided") |
---|
| 90 | sourceInfo = Resource::getInfo(filename, resourceGroup); |
---|
[1959] | 91 | |
---|
[5654] | 92 | // Continue search if not explicitely forbidden |
---|
| 93 | if (bSearchOtherPaths && sourceInfo == NULL) |
---|
| 94 | { |
---|
| 95 | // Call might be relative to the file currently being processed |
---|
| 96 | sourceInfo = Resource::getInfo(sourceFileInfo_->path + filename, sourceFileInfo_->group); |
---|
| 97 | if (sourceInfo == NULL) |
---|
| 98 | { |
---|
| 99 | // Maybe find something in the same group but in the root path |
---|
| 100 | sourceInfo = Resource::getInfo(filename, sourceFileInfo_->group); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | return sourceInfo; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void LuaState::includeFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) |
---|
[1959] | 107 | { |
---|
[5654] | 108 | shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths); |
---|
| 109 | if (sourceInfo != NULL) |
---|
| 110 | this->includeString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo); |
---|
| 111 | else |
---|
| 112 | COUT(2) << "LuaState: Cannot include file '" << filename << "' in resource group '" |
---|
| 113 | << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl; |
---|
[1959] | 114 | } |
---|
| 115 | |
---|
[5654] | 116 | void LuaState::includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo) |
---|
| 117 | { |
---|
| 118 | // Parse string with provided include parser (otherwise don't preparse at all) |
---|
| 119 | std::string luaInput; |
---|
| 120 | if (includeParseFunction_ != NULL) |
---|
| 121 | luaInput = (*includeParseFunction_)(code); |
---|
| 122 | else |
---|
| 123 | luaInput = code; |
---|
[1959] | 124 | |
---|
[5654] | 125 | this->doString(luaInput, sourceFileInfo); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void LuaState::doFile(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths) |
---|
[1959] | 129 | { |
---|
[5654] | 130 | shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, resourceGroup, bSearchOtherPaths); |
---|
| 131 | if (sourceInfo != NULL) |
---|
| 132 | this->doString(Resource::open(sourceInfo->filename, sourceInfo->group)->getAsString(), sourceInfo); |
---|
| 133 | else |
---|
| 134 | COUT(2) << "LuaState: Cannot do file '" << filename << "' in resource group '" |
---|
| 135 | << (resourceGroup == "NoResourceGroupProvided" ? sourceFileInfo_->group : resourceGroup) << "': group not found." << std::endl; |
---|
[1959] | 136 | } |
---|
| 137 | |
---|
[5654] | 138 | void LuaState::doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo) |
---|
| 139 | { |
---|
| 140 | // Save the oold source file info |
---|
| 141 | shared_ptr<ResourceInfo> oldSourceFileInfo = sourceFileInfo_; |
---|
| 142 | // Only override if sourceFileInfo provides useful information |
---|
| 143 | if (sourceFileInfo != NULL) |
---|
| 144 | sourceFileInfo_ = sourceFileInfo; |
---|
[1959] | 145 | |
---|
[5654] | 146 | //if (!bIsRunning_) |
---|
| 147 | //{ |
---|
| 148 | // bIsRunning_ = true; |
---|
[1959] | 149 | |
---|
[5654] | 150 | int error = 0; |
---|
[1959] | 151 | #if LUA_VERSION_NUM != 501 |
---|
[5654] | 152 | LoadS ls; |
---|
| 153 | ls.s = code.c_str(); |
---|
| 154 | ls.size = code.size(); |
---|
| 155 | error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, code.c_str()); |
---|
| 156 | #else |
---|
| 157 | error = luaL_loadstring(luaState_, code.c_str()); |
---|
[1959] | 158 | #endif |
---|
[5654] | 159 | |
---|
| 160 | // execute the chunk |
---|
| 161 | if (error == 0) |
---|
| 162 | error = lua_pcall(luaState_, 0, 0, 0); |
---|
| 163 | if (error != 0) |
---|
| 164 | { |
---|
| 165 | std::string origin; |
---|
| 166 | if (sourceFileInfo != NULL) |
---|
| 167 | origin = " originating from " + sourceFileInfo_->filename; |
---|
| 168 | COUT(2) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | // bIsRunning_ = false; |
---|
| 172 | //} |
---|
| 173 | //else |
---|
| 174 | //{ |
---|
| 175 | // COUT(2) << "Warning: LuaState do function called while running!" << std::endl; |
---|
| 176 | //} |
---|
| 177 | |
---|
| 178 | // Load the old info again |
---|
| 179 | sourceFileInfo_ = oldSourceFileInfo; |
---|
[1959] | 180 | } |
---|
[5654] | 181 | |
---|
| 182 | void LuaState::luaPrint(const std::string& str) |
---|
[1959] | 183 | { |
---|
[5654] | 184 | output_ << str; |
---|
[1959] | 185 | } |
---|
| 186 | |
---|
[5654] | 187 | void LuaState::luaLog(unsigned int level, const std::string& message) |
---|
[1959] | 188 | { |
---|
[5654] | 189 | OutputHandler::getOutStream().setOutputLevel(level) << message << std::endl; |
---|
[1959] | 190 | } |
---|
[5654] | 191 | |
---|
| 192 | #if LUA_VERSION_NUM != 501 |
---|
| 193 | const char * LuaState::lua_Chunkreader(lua_State *L, void *data, size_t *size) |
---|
[1959] | 194 | { |
---|
[5654] | 195 | LoadS* ls = static_cast<LoadS*>(data); |
---|
| 196 | if (ls->size == 0) |
---|
| 197 | return NULL; |
---|
| 198 | *size = ls->size; |
---|
| 199 | ls->size = 0; |
---|
| 200 | return ls->s; |
---|
[1959] | 201 | } |
---|
[5654] | 202 | #endif |
---|
[1959] | 203 | |
---|
[5654] | 204 | /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name) |
---|
[1959] | 205 | { |
---|
[5654] | 206 | for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) |
---|
[1959] | 207 | { |
---|
[5654] | 208 | if (it->first == name || it->second == function) |
---|
| 209 | { |
---|
| 210 | COUT(2) << "Warning: Trying to add a Tolua interface with the same name or function." << std::endl; |
---|
| 211 | return true; |
---|
| 212 | } |
---|
[1959] | 213 | } |
---|
[5654] | 214 | toluaInterfaces_s[name] = function; |
---|
| 215 | |
---|
| 216 | // Open interface in all LuaStates |
---|
| 217 | for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it) |
---|
| 218 | (*function)((*it)->luaState_); |
---|
| 219 | |
---|
| 220 | // Return dummy bool |
---|
| 221 | return true; |
---|
[1959] | 222 | } |
---|
| 223 | |
---|
[5654] | 224 | /*static*/ bool LuaState::removeToluaInterface(const std::string& name) |
---|
[1959] | 225 | { |
---|
[5654] | 226 | ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name); |
---|
| 227 | if (it == toluaInterfaces_s.end()) |
---|
[1959] | 228 | { |
---|
[5654] | 229 | COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl; |
---|
| 230 | return true; |
---|
[1959] | 231 | } |
---|
| 232 | |
---|
[5654] | 233 | // Close interface in all LuaStates |
---|
| 234 | for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState) |
---|
[1959] | 235 | { |
---|
[5654] | 236 | lua_pushnil((*itState)->luaState_); |
---|
| 237 | lua_setglobal((*itState)->luaState_, it->first.c_str()); |
---|
[1959] | 238 | } |
---|
| 239 | |
---|
[5654] | 240 | // Remove entry |
---|
| 241 | toluaInterfaces_s.erase(it); |
---|
| 242 | |
---|
| 243 | // Return dummy bool |
---|
| 244 | return true; |
---|
[1959] | 245 | } |
---|
| 246 | |
---|
[5654] | 247 | /*static*/ void LuaState::openToluaInterfaces(lua_State* state) |
---|
| 248 | { |
---|
| 249 | for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) |
---|
| 250 | (*it->second)(state); |
---|
| 251 | } |
---|
[1959] | 252 | |
---|
[5654] | 253 | /*static*/ void LuaState::closeToluaInterfaces(lua_State* state) |
---|
[3370] | 254 | { |
---|
[5654] | 255 | for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) |
---|
| 256 | { |
---|
| 257 | lua_pushnil(state); |
---|
| 258 | lua_setglobal(state, it->first.c_str()); |
---|
| 259 | } |
---|
[3370] | 260 | } |
---|
[1959] | 261 | } |
---|