[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" |
---|
[6746] | 42 | #include "Functor.h" |
---|
[5655] | 43 | #include "ToluaInterface.h" |
---|
[3370] | 44 | |
---|
[6417] | 45 | namespace orxonox // tolua_export |
---|
| 46 | { // tolua_export |
---|
| 47 | class Functor; // tolua_export |
---|
| 48 | |
---|
| 49 | //! Functor subclass that simply executes code with 0 arguments. |
---|
| 50 | class _CoreExport LuaFunctor : public Functor |
---|
| 51 | { |
---|
| 52 | public: |
---|
| 53 | LuaFunctor(const std::string& code, LuaState* luaState); |
---|
| 54 | void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null); |
---|
| 55 | void evaluateParam(unsigned int index, MultiType& param) const {} |
---|
[7179] | 56 | const std::type_info& getHeaderIdentifier() const { return typeid(this); } |
---|
[6417] | 57 | |
---|
| 58 | private: |
---|
| 59 | std::string code_; |
---|
| 60 | LuaState* lua_; |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | |
---|
[5654] | 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Representation of an interface to lua |
---|
| 67 | */ |
---|
[6417] | 68 | class _CoreExport LuaState // tolua_export |
---|
| 69 | { // tolua_export |
---|
[5654] | 70 | public: |
---|
| 71 | LuaState(); |
---|
| 72 | ~LuaState(); |
---|
[2710] | 73 | |
---|
[6746] | 74 | bool doFile(const std::string& filename); // tolua_export |
---|
| 75 | bool doString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>()); |
---|
[1959] | 76 | |
---|
[6746] | 77 | bool includeFile(const std::string& filename); // tolua_export |
---|
| 78 | bool includeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>()); |
---|
[1959] | 79 | |
---|
[5654] | 80 | void luaPrint(const std::string& str); // tolua_export |
---|
| 81 | void luaLog(unsigned int level, const std::string& message); // tolua_export |
---|
[6417] | 82 | bool fileExists(const std::string& filename); // tolua_export |
---|
[6746] | 83 | std::string getSourceCode(const std::string& filename); // tolua_export |
---|
[2662] | 84 | |
---|
[5654] | 85 | const std::stringstream& getOutput() const { return output_; } |
---|
| 86 | void clearOutput() { output_.clear(); } // tolua_export |
---|
[1959] | 87 | |
---|
[5654] | 88 | void setIncludeParser(std::string (*function)(const std::string&)) { includeParseFunction_ = function; } |
---|
| 89 | lua_State* getInternalLuaState() { return luaState_; } |
---|
[1959] | 90 | |
---|
[6417] | 91 | void setDefaultResourceInfo(const shared_ptr<ResourceInfo>& sourceFileInfo) { this->sourceFileInfo_ = sourceFileInfo; } |
---|
| 92 | const shared_ptr<ResourceInfo>& getDefaultResourceInfo() { return this->sourceFileInfo_; } |
---|
| 93 | |
---|
| 94 | Functor* createLuaFunctor(const std::string& code) { return new LuaFunctor(code, this); } // tolua_export |
---|
[6746] | 95 | //! Tells about whether IOConsole was activated. The Lua debugger only works with a normal console. |
---|
| 96 | bool usingIOConsole() const; // tolua_export |
---|
[6417] | 97 | |
---|
[5654] | 98 | static bool addToluaInterface(int (*function)(lua_State*), const std::string& name); |
---|
| 99 | static bool removeToluaInterface(const std::string& name); |
---|
| 100 | static void openToluaInterfaces(lua_State* state); |
---|
| 101 | static void closeToluaInterfaces(lua_State* state); |
---|
[1959] | 102 | |
---|
[6763] | 103 | static const std::string ERROR_HANDLER_NAME; |
---|
| 104 | |
---|
[5654] | 105 | private: |
---|
[6417] | 106 | shared_ptr<ResourceInfo> getFileInfo(const std::string& filename); |
---|
[1959] | 107 | |
---|
[5654] | 108 | #if LUA_VERSION_NUM != 501 |
---|
| 109 | struct LoadS |
---|
| 110 | { |
---|
| 111 | const char* s; |
---|
| 112 | size_t size; |
---|
| 113 | }; |
---|
[2026] | 114 | |
---|
[5654] | 115 | static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size); |
---|
| 116 | #endif |
---|
[3370] | 117 | |
---|
[5654] | 118 | std::stringstream output_; |
---|
| 119 | lua_State* luaState_; |
---|
| 120 | bool bIsRunning_; |
---|
| 121 | shared_ptr<ResourceInfo> sourceFileInfo_; |
---|
[6746] | 122 | std::map<std::string, std::string> sourceCodeMap_; |
---|
[5654] | 123 | std::string (*includeParseFunction_)(const std::string&); |
---|
[1959] | 124 | |
---|
[5654] | 125 | typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap; |
---|
| 126 | static ToluaInterfaceMap toluaInterfaces_s; |
---|
| 127 | static std::vector<LuaState*> instances_s; |
---|
| 128 | }; // tolua_export |
---|
| 129 | } // tolua_export |
---|
[1959] | 130 | |
---|
[5654] | 131 | #endif /* _LuaState_H__ */ |
---|