tibia-client/src/framework/script/scriptcontext.h

116 lines
3.7 KiB
C
Raw Normal View History

2011-07-17 02:13:53 +02:00
#ifndef SCRIPTCONTEXT_H
#define SCRIPTCONTEXT_H
2011-04-22 00:44:30 +02:00
2011-07-13 23:12:36 +02:00
#include <global.h>
2011-07-17 02:13:53 +02:00
#include "scriptobject.h"
2011-04-22 00:44:30 +02:00
#define reportFuncError(a) reportError(a, __FUNCTION__)
#define reportFuncErrorWithTraceback(a) reportErrorWithTraceback(a, __FUNCTION__)
2011-05-03 00:48:41 +02:00
struct lua_State;
2011-07-17 02:13:53 +02:00
class ScriptContext
2011-04-22 00:44:30 +02:00
{
public:
2011-07-17 02:13:53 +02:00
ScriptContext() : L(NULL) { }
void init();
void terminate();
2011-04-22 00:44:30 +02:00
void loadAllModules();
2011-04-22 00:44:30 +02:00
bool loadFile(const std::string& fileName);
bool loadBuffer(const std::string& text, const std::string& what = "luaBuffer");
2011-05-03 00:48:41 +02:00
bool loadBufferAsFunction(const std::string& text, const std::string& what = "luaBuffer");
void reportError(const std::string& errorDesc, const char *funcName = NULL);
void reportErrorWithTraceback(const std::string& errorDesc, const char *funcName = NULL);
2011-04-22 00:44:30 +02:00
void collectGarbage();
2011-05-03 00:48:41 +02:00
2011-04-22 00:44:30 +02:00
int getStackSize();
void insert(int index);
2011-05-19 19:11:05 +02:00
void swap(int index);
2011-05-03 00:48:41 +02:00
void remove(int index);
bool next(int index = -2);
2011-05-03 00:48:41 +02:00
void releaseRef(int ref);
void newTable();
void setTable(int index = -3);
void *newUserdata(int size);
void newMetatable(const std::string& name);
void setMetatable(const std::string& name, int index = -1);
void rawGet(const std::string& key);
void rawSet(const std::string& key);
void getField(const std::string& key);
void setField(const std::string& key);
2011-07-17 02:13:53 +02:00
void getScriptObjectField(const ScriptObjectPtr& scriptobject, const std::string& field);
void setScriptObjectField(const ScriptObjectPtr& scriptobject, const std::string& field);
2011-05-03 00:48:41 +02:00
void rawGetGlobalTableField(const std::string& globalTable, const std::string& key);
void rawSetGlobalTableField(const std::string& globalTable, const std::string& key);
void getGlobal(const std::string& key);
void setGlobal(const std::string& key);
bool isNil(int index = -1);
bool isBoolean(int index = -1);
bool isNumber(int index = -1);
bool isString(int index = -1);
bool isTable(int index = -1);
bool isUserdata(int index = -1);
bool isFunction(int index = -1);
bool isCFunction(int index = -1);
bool isLuaFunction(int index = -1);
void pop(int n = 1);
bool popBoolean();
int popInteger();
std::string popString();
2011-07-17 02:13:53 +02:00
ScriptObjectPtr popClassInstance();
2011-05-03 00:48:41 +02:00
int popRef();
2011-04-22 00:44:30 +02:00
void pushNil();
void pushBoolean(bool b);
void pushInteger(int i);
void pushString(const std::string& str);
2011-07-17 02:13:53 +02:00
void pushClassInstance(const ScriptObjectPtr& object);
void pushValue(int index = -1);
2011-05-03 00:48:41 +02:00
void pushRef(int ref);
2011-04-22 00:44:30 +02:00
2011-05-21 20:40:06 +02:00
std::string getFunctionSourcePath(bool functionIsOnStack, int level = 1);
2011-05-19 19:11:05 +02:00
bool callFunction(int numArgs = 0, int numRets = 0);
2011-05-03 00:48:41 +02:00
void callModuleField(const std::string& module, const std::string& field);
2011-04-22 00:44:30 +02:00
2011-05-01 20:47:35 +02:00
typedef int (*LuaCFunction)();
2011-04-22 00:44:30 +02:00
void setupPackageLoader();
void registerClass(const std::string& klass, const std::string& baseClass = "");
2011-05-03 00:48:41 +02:00
void registerMemberField(const std::string& field, LuaCFunction getFunction, LuaCFunction setFunction = NULL);
void registerMemberFunction(const std::string& functionName, LuaCFunction function);
2011-04-22 00:44:30 +02:00
void registerGlobalFunction(const std::string& functionName, LuaCFunction function);
2011-05-01 20:47:35 +02:00
void registerModule(const std::string& module);
2011-04-22 00:44:30 +02:00
static int luaPackageLoader(lua_State* L);
static int luaIndexMetaMethod(lua_State* L);
static int luaNewIndexMetaMethod(lua_State* L);
static int luaEqualMetaMethod(lua_State* L);
static int luaGarbageCollectMetaMethod(lua_State* L);
static int luaFunctionCallback(lua_State* L);
static int luaErrorHandler(lua_State *L);
2011-04-22 00:44:30 +02:00
private:
std::vector<LuaCFunction> m_functions;
2011-05-03 00:48:41 +02:00
std::string m_currentClass;
2011-04-22 00:44:30 +02:00
lua_State *L;
};
2011-07-17 02:13:53 +02:00
extern ScriptContext g_lua;
2011-04-22 00:44:30 +02:00
2011-07-17 02:13:53 +02:00
#endif // SCRIPTCONTEXT_H