2011-04-22 00:44:30 +02:00
|
|
|
/* The MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LUASCRIPT_H
|
|
|
|
#define LUASCRIPT_H
|
|
|
|
|
|
|
|
#include <prerequisites.h>
|
|
|
|
#include <script/scriptable.h>
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
#define reportFuncError(a) reportError(a, __FUNCTION__)
|
2011-04-22 20:48:02 +02:00
|
|
|
#define reportFuncErrorWithTraceback(a) reportErrorWithTraceback(a, __FUNCTION__)
|
2011-04-22 15:49:46 +02:00
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
struct lua_State;
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
class LuaScript
|
|
|
|
{
|
|
|
|
public:
|
2011-05-09 22:38:01 +02:00
|
|
|
LuaScript() : L(NULL) { }
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void terminate();
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-04-22 15:49:46 +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");
|
2011-04-22 15:49:46 +02:00
|
|
|
void reportError(const std::string& errorDesc, const char *funcName = NULL);
|
2011-04-22 20:48:02 +02:00
|
|
|
void reportErrorWithTraceback(const std::string& errorDesc, const char *funcName = NULL);
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
void collectGarbage();
|
2011-05-03 00:48:41 +02:00
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
int getStackSize();
|
2011-05-02 06:13:57 +02:00
|
|
|
void insert(int index);
|
2011-05-03 00:48:41 +02:00
|
|
|
void remove(int index);
|
2011-05-03 15:36:08 +02:00
|
|
|
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-05-02 06:13:57 +02:00
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void getScriptableField(const ScriptablePtr& scriptable, const std::string& field);
|
|
|
|
void setScriptableField(const ScriptablePtr& scriptable, const std::string& field);
|
|
|
|
|
|
|
|
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();
|
|
|
|
ScriptablePtr popClassInstance();
|
|
|
|
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-05-03 00:48:41 +02:00
|
|
|
void pushClassInstance(const ScriptablePtr& object);
|
2011-05-02 06:13:57 +02:00
|
|
|
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-04-22 15:49:46 +02:00
|
|
|
void callFunction(int numArgs = 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);
|
2011-05-02 06:13:57 +02:00
|
|
|
|
|
|
|
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);
|
2011-04-22 15:49:46 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern LuaScript g_lua;
|
|
|
|
|
|
|
|
#endif // LUASCRIPT_H
|