xoreos  0.0.5
scriptman.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef ENGINES_AURORA_LUA_SCRIPTMAN_H
26 #define ENGINES_AURORA_LUA_SCRIPTMAN_H
27 
28 #include <cassert>
29 
30 #include <set>
31 #include <map>
32 
33 #include "src/common/singleton.h"
34 #include "src/common/ustring.h"
35 
36 #include "src/aurora/lua/types.h"
37 
38 namespace Aurora {
39 
40 namespace Lua {
41 
43 class ScriptManager : public Common::Singleton<ScriptManager> {
44 public:
45  ScriptManager();
47 
49  void init();
51  void deinit();
52 
54  bool ready() const;
55 
57  void executeFile(const Common::UString &path);
59  void executeString(const Common::UString &code);
60 
65  Variables callFunction(const Common::UString &name, const Variables &params);
67 
68  Variable getGlobalVariable(const Common::UString &name) const;
69  TableRef getGlobalTable(const Common::UString &name) const;
71 
73  void addIgnoredFile(const Common::UString &path);
75  void removeIgnoredFile(const Common::UString &path);
77  bool isIgnoredFile(const Common::UString &path) const;
78 
80  void declareClass(const Common::UString &name);
81 
83  void beginRegister();
85  void endRegister();
86 
88  void beginRegisterNamespace(const Common::UString &name);
90  void endRegisterNamespace();
91 
93  void beginRegisterClass(const Common::UString &name, const Common::UString &baseName = "",
94  lua_CFunction deleter = 0);
95 
97  void endRegisterClass();
98 
100  void registerConstant(const Common::UString &name, float value);
102  void registerVariable(const Common::UString &name, lua_CFunction getter, lua_CFunction setter = 0);
104  void registerFunction(const Common::UString &name, lua_CFunction func);
105 
107  int getUsedMemoryAmount() const;
108 
109  void setLuaInstanceForObject(void *object, const TableRef& luaInstance);
110  void unsetLuaInstanceForObject(void *object);
111  const TableRef &getLuaInstanceForObject(void *object) const;
112 
113  void injectNewIndexMetaEventIntoTable(const TableRef& table);
114 
115 private:
116  typedef std::map<void *, TableRef> ObjectLuaInstanceMap;
117 
119  lua_State *_luaState;
123  std::set<Common::UString> _ignoredFiles;
124 
126 
128  void openLuaState();
130  void closeLuaState();
131 
135  void requireDeclaredClass(const Common::UString &name) const;
136 
138  void executeDefaultCode();
139 
141  static int atPanic(lua_State *state);
142 
144  static int luaGetLua(lua_State *state);
145  static int luaPlayFile(lua_State *state);
146  static int luaSetGCInterval(lua_State *state);
147  static int luaRegisterSubst(lua_State *state);
148  static int luaUnregisterSubst(lua_State *state);
149  static int luaRegisterHandler(lua_State *state);
150 };
151 
152 } // End of namespace Lua
153 
154 } // End of namespace Aurora
155 
157 #define LuaScriptMan ::Aurora::Lua::ScriptManager::instance()
158 
159 #endif // ENGINES_AURORA_LUA_SCRIPTMAN_H
TableRef getGlobalTable(const Common::UString &name) const
Definition: scriptman.cpp:153
Class and macro for implementing singletons.
static int luaSetGCInterval(lua_State *state)
Definition: scriptman.cpp:399
void beginRegisterClass(const Common::UString &name, const Common::UString &baseName="", lua_CFunction deleter=0)
Begin registration of a class.
Definition: scriptman.cpp:215
static int luaPlayFile(lua_State *state)
Definition: scriptman.cpp:388
A class holding an UTF-8 string.
Definition: ustring.h:48
const TableRef & getLuaInstanceForObject(void *object) const
Definition: scriptman.cpp:277
void unsetLuaInstanceForObject(void *object)
Definition: scriptman.cpp:271
void closeLuaState()
Close the current Lua state.
Definition: scriptman.cpp:315
void endRegisterNamespace()
End registration of the current namespace.
Definition: scriptman.cpp:207
void removeIgnoredFile(const Common::UString &path)
Remove a file from the ignore list.
Definition: scriptman.cpp:165
static int atPanic(lua_State *state)
Handler of the Lua panic situations.
Definition: scriptman.cpp:373
static int luaRegisterSubst(lua_State *state)
Definition: scriptman.cpp:404
void declareClass(const Common::UString &name)
Declare a class with the given name.
Definition: scriptman.cpp:173
void registerConstant(const Common::UString &name, float value)
Register a constant.
Definition: scriptman.cpp:238
static int luaRegisterHandler(lua_State *state)
Definition: scriptman.cpp:435
void addIgnoredFile(const Common::UString &path)
Add a file to the ignore list.
Definition: scriptman.cpp:161
static int luaGetLua(lua_State *state)
Lua bindings.
Definition: scriptman.cpp:380
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
void executeString(const Common::UString &code)
Execute a script string.
Definition: scriptman.cpp:106
Lua script manager.
Definition: scriptman.h:43
void endRegister()
End registration of the entities.
Definition: scriptman.cpp:189
void registerVariable(const Common::UString &name, lua_CFunction getter, lua_CFunction setter=0)
Register a variable.
Definition: scriptman.cpp:245
void endRegisterClass()
End registration of the current class.
Definition: scriptman.cpp:230
static int luaUnregisterSubst(lua_State *state)
Definition: scriptman.cpp:420
std::map< void *, TableRef > ObjectLuaInstanceMap
Definition: scriptman.h:116
void beginRegisterNamespace(const Common::UString &name)
Begin registration of a namespace.
Definition: scriptman.cpp:197
std::vector< Variable > Variables
Definition: types.h:50
ObjectLuaInstanceMap _objectLuaInstances
Definition: scriptman.h:125
Lua types.
A reference to a Lua function.
Definition: function.h:35
void init()
Initialize the script subsystem.
Definition: scriptman.cpp:60
Unicode string handling.
bool isIgnoredFile(const Common::UString &path) const
Is this file in the ignore list?
Definition: scriptman.cpp:169
Variable getGlobalVariable(const Common::UString &name) const
Definition: scriptman.cpp:144
void executeFile(const Common::UString &path)
Execute a script file.
Definition: scriptman.cpp:82
int _regNestingLevel
The current nesting level of the registration process.
Definition: scriptman.h:121
void deinit()
Deinitialize the script subsystem.
Definition: scriptman.cpp:68
void requireDeclaredClass(const Common::UString &name) const
Check whether a class with the given name was declared.
Definition: scriptman.cpp:323
void injectNewIndexMetaEventIntoTable(const TableRef &table)
Definition: scriptman.cpp:290
void beginRegister()
Begin registration of the entities.
Definition: scriptman.cpp:180
lua_State * _luaState
The Lua state.
Definition: scriptman.h:119
std::set< Common::UString > _ignoredFiles
A list of files that the script subsystem ignores.
Definition: scriptman.h:123
bool ready() const
Was the script subsystem successfully initialized?
Definition: scriptman.cpp:78
A reference to a Lua table.
Definition: table.h:37
FunctionRef getGlobalFunction(const Common::UString &name) const
Definition: scriptman.cpp:157
int getUsedMemoryAmount() const
Return the amount of memory in use by Lua (in Kbytes).
Definition: scriptman.cpp:259
Variables callFunction(const Common::UString &name, const Variables &params)
Call a Lua function.
Definition: scriptman.cpp:115
void registerFunction(const Common::UString &name, lua_CFunction func)
Register a function.
Definition: scriptman.cpp:252
void setLuaInstanceForObject(void *object, const TableRef &luaInstance)
Definition: scriptman.cpp:263
void openLuaState()
Open and setup a new Lua state.
Definition: scriptman.cpp:296