[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 | #ifndef _LuaState_H__ |
---|
| 31 | #define _LuaState_H__ |
---|
[1959] | 32 | |
---|
| 33 | #include "CorePrereqs.h" |
---|
| 34 | |
---|
[5654] | 35 | #include <map> |
---|
| 36 | #include <sstream> |
---|
[3196] | 37 | #include <string> |
---|
[3370] | 38 | #include <vector> |
---|
[5654] | 39 | #include <boost/shared_ptr.hpp> |
---|
[1959] | 40 | |
---|
[5654] | 41 | #include "util/ScopeGuard.h" |
---|
[5655] | 42 | #include "ToluaInterface.h" |
---|
[3370] | 43 | |
---|
[2710] | 44 | // tolua_begin |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
[5782] | 47 | //! Stores basic information about a File |
---|
| 48 | struct ResourceInfo |
---|
| 49 | { |
---|
| 50 | //! The file's fully qualified name |
---|
| 51 | std::string filename; |
---|
| 52 | //! Path name; separated by '/' and ending with '/' |
---|
| 53 | std::string path; |
---|
| 54 | //! Base filename |
---|
| 55 | std::string basename; |
---|
| 56 | }; |
---|
| 57 | |
---|
[5654] | 58 | /** |
---|
| 59 | @brief |
---|
| 60 | Representation of an interface to lua |
---|
| 61 | */ |
---|
| 62 | class _CoreExport LuaState |
---|
| 63 | { |
---|
[3370] | 64 | // tolua_end |
---|
[5654] | 65 | public: |
---|
| 66 | LuaState(); |
---|
| 67 | ~LuaState(); |
---|
[2710] | 68 | |
---|
[5782] | 69 | void doFile(const std::string& filename, bool bSearchOtherPaths = true); // tolua_export |
---|
[5654] | 70 | void doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>()); |
---|
[1959] | 71 | |
---|
[5782] | 72 | void includeFile(const std::string& filename, bool bSearchOtherPaths = true); // tolua_export |
---|
[5654] | 73 | void includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>()); |
---|
[1959] | 74 | |
---|
[5654] | 75 | void luaPrint(const std::string& str); // tolua_export |
---|
| 76 | void luaLog(unsigned int level, const std::string& message); // tolua_export |
---|
[5782] | 77 | bool fileExists(const std::string& filename, bool bSearchOtherPaths = true); // tolua_export |
---|
[2662] | 78 | |
---|
[5654] | 79 | const std::stringstream& getOutput() const { return output_; } |
---|
| 80 | void clearOutput() { output_.clear(); } // tolua_export |
---|
[1959] | 81 | |
---|
[5654] | 82 | void setIncludeParser(std::string (*function)(const std::string&)) { includeParseFunction_ = function; } |
---|
| 83 | lua_State* getInternalLuaState() { return luaState_; } |
---|
[1959] | 84 | |
---|
[5654] | 85 | static bool addToluaInterface(int (*function)(lua_State*), const std::string& name); |
---|
| 86 | static bool removeToluaInterface(const std::string& name); |
---|
| 87 | static void openToluaInterfaces(lua_State* state); |
---|
| 88 | static void closeToluaInterfaces(lua_State* state); |
---|
[1959] | 89 | |
---|
[5654] | 90 | private: |
---|
[5782] | 91 | shared_ptr<ResourceInfo> getFileInfo(const std::string& filename, bool bSearchOtherPaths); |
---|
| 92 | shared_ptr<ResourceInfo> getFileInfo(const std::string& filename); |
---|
| 93 | std::string loadFile(const std::string& filename); |
---|
[1959] | 94 | |
---|
[5654] | 95 | #if LUA_VERSION_NUM != 501 |
---|
| 96 | struct LoadS |
---|
| 97 | { |
---|
| 98 | const char* s; |
---|
| 99 | size_t size; |
---|
| 100 | }; |
---|
[2026] | 101 | |
---|
[5654] | 102 | static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size); |
---|
| 103 | #endif |
---|
[3370] | 104 | |
---|
[5654] | 105 | std::stringstream output_; |
---|
| 106 | lua_State* luaState_; |
---|
| 107 | bool bIsRunning_; |
---|
| 108 | shared_ptr<ResourceInfo> sourceFileInfo_; |
---|
| 109 | std::string (*includeParseFunction_)(const std::string&); |
---|
[1959] | 110 | |
---|
[5654] | 111 | typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap; |
---|
| 112 | static ToluaInterfaceMap toluaInterfaces_s; |
---|
| 113 | static std::vector<LuaState*> instances_s; |
---|
| 114 | }; // tolua_export |
---|
| 115 | } // tolua_export |
---|
[1959] | 116 | |
---|
[5654] | 117 | #endif /* _LuaState_H__ */ |
---|