| [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 |  | 
|---|
| [5782] | 32 | #include <boost/filesystem.hpp> | 
|---|
| [5654] | 33 | #include <tolua/tolua++.h> | 
|---|
| [2710] | 34 | extern "C" { | 
|---|
| [5654] | 35 | #include <lua.h> | 
|---|
| [2710] | 36 | #include <lualib.h> | 
|---|
 | 37 | } | 
|---|
 | 38 |  | 
|---|
| [3196] | 39 | #include "util/Debug.h" | 
|---|
| [6040] | 40 | #include "PathConfig.h" | 
|---|
| [5655] | 41 | #include "ToluaBindCore.h" | 
|---|
| [1959] | 42 |  | 
|---|
 | 43 | namespace orxonox | 
|---|
 | 44 | { | 
|---|
| [5654] | 45 |     LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s; | 
|---|
 | 46 |     std::vector<LuaState*> LuaState::instances_s; | 
|---|
| [1959] | 47 |  | 
|---|
| [5655] | 48 |     // Do this after declaring toluaInterfaces_s and instances_s to avoid larger problems | 
|---|
 | 49 |     DeclareToluaInterface(Core); | 
|---|
 | 50 |  | 
|---|
| [5654] | 51 |     LuaState::LuaState() | 
|---|
 | 52 |         : bIsRunning_(false) | 
|---|
 | 53 |         , includeParseFunction_(NULL) | 
|---|
 | 54 |     { | 
|---|
 | 55 |         // Create new lua state and configure it | 
|---|
 | 56 |         luaState_ = lua_open(); | 
|---|
| [1959] | 57 | #if LUA_VERSION_NUM == 501 | 
|---|
| [5654] | 58 |         luaL_openlibs(luaState_); | 
|---|
| [1959] | 59 | #else | 
|---|
| [5654] | 60 |         luaopen_base(luaState_); | 
|---|
 | 61 |         luaopen_string(luaState_); | 
|---|
 | 62 |         luaopen_table(luaState_); | 
|---|
 | 63 |         luaopen_math(luaState_); | 
|---|
 | 64 |         luaopen_io(luaState_); | 
|---|
 | 65 |         luaopen_debug(luaState_); | 
|---|
| [1959] | 66 | #endif | 
|---|
| [3370] | 67 |  | 
|---|
| [5654] | 68 |         // Open all available tolua interfaces | 
|---|
 | 69 |         this->openToluaInterfaces(luaState_); | 
|---|
| [3370] | 70 |  | 
|---|
| [5654] | 71 |         // Create dummy file info | 
|---|
 | 72 |         sourceFileInfo_.reset(new ResourceInfo()); | 
|---|
| [1959] | 73 |  | 
|---|
| [5759] | 74 |         // Push 'this' pointer | 
|---|
| [5654] | 75 |         tolua_pushusertype(luaState_, static_cast<void*>(this), "orxonox::LuaState"); | 
|---|
 | 76 |         lua_setglobal(luaState_, "luaState"); | 
|---|
| [3370] | 77 |  | 
|---|
| [5654] | 78 |         // Parse init script | 
|---|
| [5660] | 79 |         this->doFile("LuaStateInit.lua"); | 
|---|
| [5654] | 80 |     } | 
|---|
| [1959] | 81 |  | 
|---|
| [5654] | 82 |     LuaState::~LuaState() | 
|---|
 | 83 |     { | 
|---|
 | 84 |         lua_close(luaState_); | 
|---|
 | 85 |     } | 
|---|
| [2710] | 86 |  | 
|---|
| [5782] | 87 |     shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename, bool bSearchOtherPaths) | 
|---|
| [5654] | 88 |     { | 
|---|
 | 89 |         shared_ptr<ResourceInfo> sourceInfo; | 
|---|
| [5782] | 90 |         sourceInfo = this->getFileInfo(filename); | 
|---|
| [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 | 
|---|
| [5782] | 96 |             sourceInfo = this->getFileInfo(sourceFileInfo_->path + filename); | 
|---|
| [5654] | 97 |             if (sourceInfo == NULL) | 
|---|
 | 98 |             { | 
|---|
 | 99 |                 // Maybe find something in the same group but in the root path | 
|---|
| [5782] | 100 |                 sourceInfo = this->getFileInfo(filename); | 
|---|
| [5654] | 101 |             } | 
|---|
 | 102 |         } | 
|---|
 | 103 |         return sourceInfo; | 
|---|
 | 104 |     } | 
|---|
 | 105 |  | 
|---|
| [5782] | 106 |     shared_ptr<ResourceInfo> LuaState::getFileInfo(const std::string& filename) | 
|---|
| [1959] | 107 |     { | 
|---|
| [6040] | 108 |         boost::filesystem::path filepath = PathConfig::getDataPath() / "lua" / filename; | 
|---|
| [5782] | 109 |         if (boost::filesystem::exists(filepath)) | 
|---|
 | 110 |         { | 
|---|
 | 111 |             shared_ptr<ResourceInfo> info(new ResourceInfo()); | 
|---|
 | 112 |             info->filename = filepath.string(); | 
|---|
 | 113 |             info->path = filepath.branch_path().string(); | 
|---|
 | 114 |             info->basename = filepath.leaf(); | 
|---|
 | 115 |             return info; | 
|---|
 | 116 |         } | 
|---|
 | 117 |         else | 
|---|
 | 118 |             return shared_ptr<ResourceInfo>(); | 
|---|
 | 119 |     } | 
|---|
 | 120 |  | 
|---|
 | 121 |     std::string LuaState::loadFile(const std::string& filename) | 
|---|
 | 122 |     { | 
|---|
 | 123 |         std::ifstream file(filename.c_str()); | 
|---|
 | 124 |         std::ostringstream oss; | 
|---|
 | 125 |         oss << file.rdbuf(); | 
|---|
 | 126 |         return oss.str(); | 
|---|
 | 127 |     } | 
|---|
 | 128 |  | 
|---|
 | 129 |     void LuaState::includeFile(const std::string& filename, bool bSearchOtherPaths) | 
|---|
 | 130 |     { | 
|---|
 | 131 |         shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, bSearchOtherPaths); | 
|---|
| [5654] | 132 |         if (sourceInfo != NULL) | 
|---|
| [5782] | 133 |             this->includeString(this->loadFile(sourceInfo->filename), sourceInfo); | 
|---|
| [5654] | 134 |         else | 
|---|
| [5782] | 135 |             COUT(2) << "LuaState: Cannot include file '" << filename << std::endl; | 
|---|
| [1959] | 136 |     } | 
|---|
 | 137 |  | 
|---|
| [5654] | 138 |     void LuaState::includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo) | 
|---|
 | 139 |     { | 
|---|
 | 140 |         // Parse string with provided include parser (otherwise don't preparse at all) | 
|---|
 | 141 |         std::string luaInput; | 
|---|
 | 142 |         if (includeParseFunction_ != NULL) | 
|---|
 | 143 |             luaInput = (*includeParseFunction_)(code); | 
|---|
 | 144 |         else | 
|---|
 | 145 |             luaInput = code; | 
|---|
| [1959] | 146 |  | 
|---|
| [5654] | 147 |         this->doString(luaInput, sourceFileInfo); | 
|---|
 | 148 |     } | 
|---|
 | 149 |  | 
|---|
| [5782] | 150 |     void LuaState::doFile(const std::string& filename, bool bSearchOtherPaths) | 
|---|
| [1959] | 151 |     { | 
|---|
| [5782] | 152 |         shared_ptr<ResourceInfo> sourceInfo = this->getFileInfo(filename, bSearchOtherPaths); | 
|---|
| [5654] | 153 |         if (sourceInfo != NULL) | 
|---|
| [5782] | 154 |             this->doString(this->loadFile(sourceInfo->filename), sourceInfo); | 
|---|
| [5654] | 155 |         else | 
|---|
| [5782] | 156 |             COUT(2) << "LuaState: Cannot do file '" << filename << std::endl; | 
|---|
| [1959] | 157 |     } | 
|---|
 | 158 |  | 
|---|
| [5654] | 159 |     void LuaState::doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo) | 
|---|
 | 160 |     { | 
|---|
 | 161 |         // Save the oold source file info | 
|---|
 | 162 |         shared_ptr<ResourceInfo> oldSourceFileInfo = sourceFileInfo_; | 
|---|
 | 163 |         // Only override if sourceFileInfo provides useful information | 
|---|
 | 164 |         if (sourceFileInfo != NULL) | 
|---|
 | 165 |             sourceFileInfo_ = sourceFileInfo; | 
|---|
| [1959] | 166 |  | 
|---|
| [5654] | 167 |         int error = 0; | 
|---|
| [1959] | 168 | #if LUA_VERSION_NUM != 501 | 
|---|
| [5654] | 169 |         LoadS ls; | 
|---|
 | 170 |         ls.s = code.c_str(); | 
|---|
 | 171 |         ls.size = code.size(); | 
|---|
 | 172 |         error = lua_load(luaState_, &orxonox::LuaState::lua_Chunkreader, &ls, code.c_str()); | 
|---|
 | 173 | #else | 
|---|
 | 174 |         error = luaL_loadstring(luaState_, code.c_str()); | 
|---|
| [1959] | 175 | #endif | 
|---|
| [5654] | 176 |  | 
|---|
 | 177 |         // execute the chunk | 
|---|
 | 178 |         if (error == 0) | 
|---|
| [5661] | 179 |             error = lua_pcall(luaState_, 0, 1, 0); | 
|---|
| [5654] | 180 |         if (error != 0) | 
|---|
 | 181 |         { | 
|---|
 | 182 |             std::string origin; | 
|---|
 | 183 |             if (sourceFileInfo != NULL) | 
|---|
 | 184 |                 origin = " originating from " + sourceFileInfo_->filename; | 
|---|
 | 185 |             COUT(2) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl; | 
|---|
| [5661] | 186 |             // return value is nil | 
|---|
 | 187 |             lua_pushnil(luaState_); | 
|---|
| [5654] | 188 |         } | 
|---|
| [5661] | 189 |         // push return value because it will get lost since the return value of this function is void | 
|---|
 | 190 |         lua_setglobal(luaState_, "LuaStateReturnValue"); | 
|---|
| [5654] | 191 |  | 
|---|
 | 192 |         // Load the old info again | 
|---|
 | 193 |         sourceFileInfo_ = oldSourceFileInfo; | 
|---|
| [1959] | 194 |     } | 
|---|
| [5654] | 195 |  | 
|---|
 | 196 |     void LuaState::luaPrint(const std::string& str) | 
|---|
| [1959] | 197 |     { | 
|---|
| [5654] | 198 |         output_ << str; | 
|---|
| [1959] | 199 |     } | 
|---|
 | 200 |  | 
|---|
| [5654] | 201 |     void LuaState::luaLog(unsigned int level, const std::string& message) | 
|---|
| [1959] | 202 |     { | 
|---|
| [5654] | 203 |         OutputHandler::getOutStream().setOutputLevel(level) << message << std::endl; | 
|---|
| [1959] | 204 |     } | 
|---|
| [5654] | 205 |  | 
|---|
| [5782] | 206 |     bool LuaState::fileExists(const std::string& filename, bool bSearchOtherPaths) | 
|---|
| [5661] | 207 |     { | 
|---|
| [5782] | 208 |         shared_ptr<ResourceInfo> info =  this->getFileInfo(filename, bSearchOtherPaths); | 
|---|
| [5661] | 209 |         if (info == NULL) | 
|---|
 | 210 |             return false; | 
|---|
 | 211 |         else | 
|---|
 | 212 |             return true; | 
|---|
 | 213 |     } | 
|---|
 | 214 |  | 
|---|
| [5654] | 215 | #if LUA_VERSION_NUM != 501 | 
|---|
 | 216 |     const char * LuaState::lua_Chunkreader(lua_State *L, void *data, size_t *size) | 
|---|
| [1959] | 217 |     { | 
|---|
| [5654] | 218 |         LoadS* ls = static_cast<LoadS*>(data); | 
|---|
 | 219 |         if (ls->size == 0) | 
|---|
 | 220 |             return NULL; | 
|---|
 | 221 |         *size = ls->size; | 
|---|
 | 222 |         ls->size = 0; | 
|---|
 | 223 |         return ls->s; | 
|---|
| [1959] | 224 |     } | 
|---|
| [5654] | 225 | #endif | 
|---|
| [1959] | 226 |  | 
|---|
| [5654] | 227 |     /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name) | 
|---|
| [1959] | 228 |     { | 
|---|
| [5654] | 229 |         for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) | 
|---|
| [1959] | 230 |         { | 
|---|
| [5654] | 231 |             if (it->first == name || it->second == function) | 
|---|
 | 232 |             { | 
|---|
 | 233 |                 COUT(2) << "Warning: Trying to add a Tolua interface with the same name or function." << std::endl; | 
|---|
 | 234 |                 return true; | 
|---|
 | 235 |             } | 
|---|
| [1959] | 236 |         } | 
|---|
| [5654] | 237 |         toluaInterfaces_s[name] = function; | 
|---|
 | 238 |  | 
|---|
 | 239 |         // Open interface in all LuaStates | 
|---|
 | 240 |         for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it) | 
|---|
 | 241 |             (*function)((*it)->luaState_); | 
|---|
 | 242 |  | 
|---|
 | 243 |         // Return dummy bool | 
|---|
 | 244 |         return true; | 
|---|
| [1959] | 245 |     } | 
|---|
 | 246 |  | 
|---|
| [5654] | 247 |     /*static*/ bool LuaState::removeToluaInterface(const std::string& name) | 
|---|
| [1959] | 248 |     { | 
|---|
| [5654] | 249 |         ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name); | 
|---|
 | 250 |         if (it == toluaInterfaces_s.end()) | 
|---|
| [1959] | 251 |         { | 
|---|
| [5654] | 252 |             COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl; | 
|---|
 | 253 |             return true; | 
|---|
| [1959] | 254 |         } | 
|---|
 | 255 |  | 
|---|
| [5654] | 256 |         // Close interface in all LuaStates | 
|---|
 | 257 |         for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState) | 
|---|
| [1959] | 258 |         { | 
|---|
| [5654] | 259 |             lua_pushnil((*itState)->luaState_); | 
|---|
 | 260 |             lua_setglobal((*itState)->luaState_, it->first.c_str()); | 
|---|
| [1959] | 261 |         } | 
|---|
 | 262 |  | 
|---|
| [5654] | 263 |         // Remove entry | 
|---|
 | 264 |         toluaInterfaces_s.erase(it); | 
|---|
 | 265 |  | 
|---|
 | 266 |         // Return dummy bool | 
|---|
 | 267 |         return true; | 
|---|
| [1959] | 268 |     } | 
|---|
 | 269 |  | 
|---|
| [5654] | 270 |     /*static*/ void LuaState::openToluaInterfaces(lua_State* state) | 
|---|
 | 271 |     { | 
|---|
 | 272 |         for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) | 
|---|
 | 273 |             (*it->second)(state); | 
|---|
 | 274 |     } | 
|---|
| [1959] | 275 |  | 
|---|
| [5654] | 276 |     /*static*/ void LuaState::closeToluaInterfaces(lua_State* state) | 
|---|
| [3370] | 277 |     { | 
|---|
| [5654] | 278 |         for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it) | 
|---|
 | 279 |         { | 
|---|
 | 280 |             lua_pushnil(state); | 
|---|
 | 281 |             lua_setglobal(state, it->first.c_str()); | 
|---|
 | 282 |         } | 
|---|
| [3370] | 283 |     } | 
|---|
| [1959] | 284 | } | 
|---|