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 |
---|
24 | * Reto Grieder |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef _LuaState_H__ |
---|
31 | #define _LuaState_H__ |
---|
32 | |
---|
33 | #include "CorePrereqs.h" |
---|
34 | |
---|
35 | #include <map> |
---|
36 | #include <sstream> |
---|
37 | #include <string> |
---|
38 | #include <vector> |
---|
39 | #include <boost/shared_ptr.hpp> |
---|
40 | |
---|
41 | #include "util/ScopeGuard.h" |
---|
42 | #include "core/Functor.h" |
---|
43 | #include "ToluaInterface.h" |
---|
44 | |
---|
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 {} |
---|
56 | const std::type_info& getHeaderIdentifier() const { return typeid(this); } |
---|
57 | |
---|
58 | private: |
---|
59 | std::string code_; |
---|
60 | LuaState* lua_; |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | @brief |
---|
66 | Representation of an interface to lua |
---|
67 | */ |
---|
68 | class _CoreExport LuaState // tolua_export |
---|
69 | { // tolua_export |
---|
70 | public: |
---|
71 | LuaState(); |
---|
72 | ~LuaState(); |
---|
73 | |
---|
74 | void doFile(const std::string& filename); // tolua_export |
---|
75 | void doString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>()); |
---|
76 | |
---|
77 | void includeFile(const std::string& filename); // tolua_export |
---|
78 | void includeString(const std::string& code, const shared_ptr<ResourceInfo>& sourceFileInfo = shared_ptr<ResourceInfo>()); |
---|
79 | |
---|
80 | void luaPrint(const std::string& str); // tolua_export |
---|
81 | void luaLog(unsigned int level, const std::string& message); // tolua_export |
---|
82 | bool fileExists(const std::string& filename); // tolua_export |
---|
83 | |
---|
84 | const std::stringstream& getOutput() const { return output_; } |
---|
85 | void clearOutput() { output_.clear(); } // tolua_export |
---|
86 | |
---|
87 | void setIncludeParser(std::string (*function)(const std::string&)) { includeParseFunction_ = function; } |
---|
88 | lua_State* getInternalLuaState() { return luaState_; } |
---|
89 | |
---|
90 | void setDefaultResourceInfo(const shared_ptr<ResourceInfo>& sourceFileInfo) { this->sourceFileInfo_ = sourceFileInfo; } |
---|
91 | const shared_ptr<ResourceInfo>& getDefaultResourceInfo() { return this->sourceFileInfo_; } |
---|
92 | |
---|
93 | Functor* createLuaFunctor(const std::string& code) { return new LuaFunctor(code, this); } // tolua_export |
---|
94 | |
---|
95 | static bool addToluaInterface(int (*function)(lua_State*), const std::string& name); |
---|
96 | static bool removeToluaInterface(const std::string& name); |
---|
97 | static void openToluaInterfaces(lua_State* state); |
---|
98 | static void closeToluaInterfaces(lua_State* state); |
---|
99 | |
---|
100 | private: |
---|
101 | shared_ptr<ResourceInfo> getFileInfo(const std::string& filename); |
---|
102 | |
---|
103 | #if LUA_VERSION_NUM != 501 |
---|
104 | struct LoadS |
---|
105 | { |
---|
106 | const char* s; |
---|
107 | size_t size; |
---|
108 | }; |
---|
109 | |
---|
110 | static const char * lua_Chunkreader(lua_State *L, void *data, size_t *size); |
---|
111 | #endif |
---|
112 | |
---|
113 | std::stringstream output_; |
---|
114 | lua_State* luaState_; |
---|
115 | bool bIsRunning_; |
---|
116 | shared_ptr<ResourceInfo> sourceFileInfo_; |
---|
117 | std::string (*includeParseFunction_)(const std::string&); |
---|
118 | |
---|
119 | typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap; |
---|
120 | static ToluaInterfaceMap toluaInterfaces_s; |
---|
121 | static std::vector<LuaState*> instances_s; |
---|
122 | }; // tolua_export |
---|
123 | } // tolua_export |
---|
124 | |
---|
125 | #endif /* _LuaState_H__ */ |
---|