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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <prerequisites.h>
|
|
|
|
#include <script/luascript.h>
|
2011-05-01 20:47:35 +02:00
|
|
|
#include <script/luafunctions.h>
|
2011-04-22 00:44:30 +02:00
|
|
|
#include <core/resources.h>
|
2011-05-03 00:48:41 +02:00
|
|
|
#include <lua.hpp>
|
2011-04-22 00:44:30 +02:00
|
|
|
|
|
|
|
LuaScript g_lua;
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
void LuaScript::init()
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
|
|
|
L = luaL_newstate();
|
|
|
|
if(!L)
|
2011-04-22 20:48:02 +02:00
|
|
|
logFatal("FATAL ERROR: could not create lua context");
|
2011-04-22 00:44:30 +02:00
|
|
|
|
|
|
|
// load lua standard libraries
|
|
|
|
luaL_openlibs(L);
|
|
|
|
|
|
|
|
// setup custom package loader
|
|
|
|
setupPackageLoader();
|
|
|
|
|
2011-05-01 20:47:35 +02:00
|
|
|
registerLuaFunctions();
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:38:01 +02:00
|
|
|
void LuaScript::terminate()
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
2011-05-09 22:38:01 +02:00
|
|
|
collectGarbage();
|
2011-04-22 00:44:30 +02:00
|
|
|
lua_close(L);
|
2011-05-09 22:38:01 +02:00
|
|
|
L = NULL;
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
void LuaScript::loadAllModules()
|
|
|
|
{
|
|
|
|
std::list<std::string> modules = g_resources.getDirectoryFiles("modules");
|
|
|
|
foreach(const std::string& module, modules) {
|
|
|
|
std::list<std::string> moduleFiles = g_resources.getDirectoryFiles(std::string("modules/") + module);
|
|
|
|
foreach(const std::string& moduleFile, moduleFiles) {
|
|
|
|
if(boost::ends_with(moduleFile, ".lua"))
|
|
|
|
loadFile(std::string("modules/") + module + "/" + moduleFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
bool LuaScript::loadFile(const std::string& fileName)
|
|
|
|
{
|
|
|
|
if(!g_resources.fileExists(fileName)) {
|
2011-04-22 20:48:02 +02:00
|
|
|
flogError("ERROR: script file '%s' doesn't exist", fileName.c_str());
|
2011-04-22 00:44:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string fileContents = g_resources.loadTextFile(fileName);
|
|
|
|
return loadBuffer(fileContents, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::loadBuffer(const std::string& text, const std::string& what)
|
|
|
|
{
|
|
|
|
// load buffer
|
|
|
|
int ret = luaL_loadbuffer(L, text.c_str(), text.length(), what.c_str());
|
|
|
|
if(ret != 0){
|
2011-04-22 15:49:46 +02:00
|
|
|
reportError(popString());
|
2011-04-22 00:44:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
// check if is loaded as a function
|
2011-04-22 00:44:30 +02:00
|
|
|
if(lua_isfunction(L, -1) == 0) {
|
2011-05-03 00:48:41 +02:00
|
|
|
pop();
|
2011-04-22 00:44:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute it
|
|
|
|
ret = lua_pcall(L, 0, 0, 0);
|
|
|
|
if(ret != 0){
|
2011-04-22 15:49:46 +02:00
|
|
|
reportError(popString());
|
2011-04-22 00:44:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
bool LuaScript::loadBufferAsFunction(const std::string& text, const std::string& what)
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
2011-04-22 15:49:46 +02:00
|
|
|
int ret = luaL_loadbuffer(L, text.c_str(), text.length(), what.c_str());
|
2011-04-22 00:44:30 +02:00
|
|
|
if(ret != 0){
|
2011-04-22 15:49:46 +02:00
|
|
|
reportError(popString());
|
2011-05-03 00:48:41 +02:00
|
|
|
return false;
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
if(!isFunction()) {
|
|
|
|
pop();
|
|
|
|
return false;
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
return true;
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
void LuaScript::reportError(const std::string& errorDesc, const char *funcName)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2011-04-22 20:48:02 +02:00
|
|
|
ss << "LUA Script ERROR: ";
|
2011-04-22 15:49:46 +02:00
|
|
|
if(funcName)
|
2011-04-22 20:48:02 +02:00
|
|
|
ss << " in " << funcName << "(): ";
|
2011-05-03 00:48:41 +02:00
|
|
|
ss << errorDesc;
|
2011-04-22 20:48:02 +02:00
|
|
|
logError(ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::reportErrorWithTraceback(const std::string& errorDesc, const char* funcName)
|
|
|
|
{
|
|
|
|
pushString(errorDesc);
|
|
|
|
luaErrorHandler(L);
|
|
|
|
reportError(popString(), funcName);
|
2011-04-22 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
void LuaScript::collectGarbage()
|
|
|
|
{
|
|
|
|
for(int i=0;i<2;i++)
|
|
|
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
int LuaScript::getStackSize()
|
|
|
|
{
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
void LuaScript::insert(int index)
|
2011-04-22 20:48:02 +02:00
|
|
|
{
|
|
|
|
lua_insert(L, index);
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::remove(int index)
|
2011-05-02 06:13:57 +02:00
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_remove(L, index);
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 15:36:08 +02:00
|
|
|
bool LuaScript::next(int index)
|
|
|
|
{
|
|
|
|
return lua_next(L, index);
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::releaseRef(int ref)
|
2011-05-02 06:13:57 +02:00
|
|
|
{
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, ref);
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::newTable()
|
2011-05-02 06:13:57 +02:00
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::setTable(int index)
|
|
|
|
{
|
|
|
|
lua_settable(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *LuaScript::newUserdata(int size)
|
|
|
|
{
|
|
|
|
return lua_newuserdata(L, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::newMetatable(const std::string& name)
|
|
|
|
{
|
|
|
|
luaL_newmetatable(L, name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::setMetatable(const std::string& name, int index)
|
|
|
|
{
|
|
|
|
luaL_getmetatable(L, name.c_str());
|
|
|
|
if(isNil())
|
|
|
|
reportError(f("could not retrive metatable %d", name));
|
|
|
|
else
|
|
|
|
lua_setmetatable(L, index < 0 ? index-1 : index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::rawGet(const std::string& key)
|
|
|
|
{
|
|
|
|
pushString(key);
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::rawSet(const std::string& key)
|
|
|
|
{
|
|
|
|
pushString(key);
|
|
|
|
insert(-2);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::getField(const std::string& key)
|
|
|
|
{
|
|
|
|
lua_getfield(L, -1, key.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::setField(const std::string& key)
|
|
|
|
{
|
|
|
|
lua_setfield(L, -2, key.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::getScriptableField(const ScriptablePtr& scriptable, const std::string& field)
|
|
|
|
{
|
|
|
|
if(scriptable) {
|
|
|
|
pushRef(scriptable->getLuaTableRef());
|
|
|
|
getField(field);
|
|
|
|
remove(-2);
|
|
|
|
} else
|
|
|
|
pushNil();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::setScriptableField(const ScriptablePtr& scriptable, const std::string& field)
|
|
|
|
{
|
|
|
|
if(scriptable) {
|
|
|
|
pushRef(scriptable->getLuaTableRef());
|
|
|
|
insert(-2);
|
|
|
|
setField(field);
|
|
|
|
}
|
|
|
|
pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::rawGetGlobalTableField(const std::string& globalTable, const std::string& key)
|
|
|
|
{
|
|
|
|
getGlobal(globalTable);
|
|
|
|
rawGet(key);
|
|
|
|
remove(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::rawSetGlobalTableField(const std::string& globalTable, const std::string& key)
|
|
|
|
{
|
|
|
|
getGlobal(globalTable);
|
|
|
|
insert(-2);
|
|
|
|
rawSet(key);
|
|
|
|
pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::getGlobal(const std::string& key)
|
|
|
|
{
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, key.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::setGlobal(const std::string& key)
|
|
|
|
{
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, key.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isNil(int index)
|
|
|
|
{
|
|
|
|
return lua_isnil(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isBoolean(int index)
|
|
|
|
{
|
|
|
|
return lua_isboolean(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isNumber(int index)
|
|
|
|
{
|
|
|
|
return lua_isnumber(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isString(int index)
|
|
|
|
{
|
|
|
|
return lua_isstring(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isTable(int index)
|
|
|
|
{
|
|
|
|
return lua_istable(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isUserdata(int index)
|
|
|
|
{
|
|
|
|
return lua_isuserdata(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isFunction(int index)
|
|
|
|
{
|
|
|
|
return lua_isfunction(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isCFunction(int index)
|
|
|
|
{
|
|
|
|
return lua_iscfunction(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::isLuaFunction(int index)
|
|
|
|
{
|
|
|
|
return isFunction(index) && !isCFunction(index);
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
void LuaScript::pop(int n)
|
|
|
|
{
|
|
|
|
lua_pop(L, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaScript::popBoolean()
|
|
|
|
{
|
|
|
|
bool b = (lua_toboolean(L, -1) != 0);
|
|
|
|
pop();
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
int LuaScript::popInteger()
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
2011-04-22 15:49:46 +02:00
|
|
|
double d = lua_tonumber(L, -1);
|
2011-04-22 20:48:02 +02:00
|
|
|
pop();
|
2011-04-22 00:44:30 +02:00
|
|
|
return (int)d;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LuaScript::popString()
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
std::string str;
|
|
|
|
if(lua_isstring(L, -1))
|
|
|
|
str = lua_tostring(L, -1);
|
2011-04-22 00:44:30 +02:00
|
|
|
pop();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
ScriptablePtr LuaScript::popClassInstance()
|
|
|
|
{
|
|
|
|
ScriptablePtr object;
|
|
|
|
if(isUserdata()) { // instances are store as userdata
|
|
|
|
object = *((ScriptablePtr *)lua_touserdata(L, -1));
|
|
|
|
if(!object)
|
|
|
|
reportErrorWithTraceback("attempt to retrive class instance from a object that is already expired");
|
|
|
|
} else if(!isNil()) // we accept nil values
|
|
|
|
reportErrorWithTraceback("couldn't retrive class instance, the value is not a scriptable type");
|
|
|
|
pop();
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::popRef()
|
|
|
|
{
|
|
|
|
return luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
void LuaScript::pushNil()
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::pushBoolean(bool b)
|
|
|
|
{
|
|
|
|
lua_pushboolean(L, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::pushInteger(int32_t i)
|
|
|
|
{
|
|
|
|
lua_pushnumber(L, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::pushString(const std::string& str)
|
|
|
|
{
|
|
|
|
lua_pushstring(L, str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::pushClassInstance(const ScriptablePtr& object)
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
if(object) {
|
2011-05-03 00:48:41 +02:00
|
|
|
new(newUserdata(sizeof(ScriptablePtr))) ScriptablePtr(object);
|
|
|
|
setMetatable(std::string(object->getScriptableName()) + "_mt");
|
|
|
|
} else
|
|
|
|
pushNil();
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::pushValue(int index)
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_pushvalue(L, index);
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::pushRef(int ref)
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
|
2011-04-22 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::callFunction(int numArgs)
|
|
|
|
{
|
|
|
|
int size = lua_gettop(L);
|
2011-05-02 06:13:57 +02:00
|
|
|
int errorIndex = -(numArgs + 2);
|
2011-04-22 15:49:46 +02:00
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler);
|
|
|
|
lua_insert(L, errorIndex);
|
|
|
|
|
|
|
|
int ret = lua_pcall(L, numArgs, 0, errorIndex);
|
|
|
|
if(ret != 0) {
|
|
|
|
reportError(popString());
|
|
|
|
}
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
// pop error func
|
|
|
|
lua_pop(L, 1);
|
2011-04-22 15:49:46 +02:00
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
if(lua_gettop(L) != size - numArgs - 1)
|
2011-04-22 15:49:46 +02:00
|
|
|
reportError("stack size changed!");
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::callModuleField(const std::string& module, const std::string& field)
|
2011-05-01 20:47:35 +02:00
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
g_lua.rawGetGlobalTableField(module, field);
|
|
|
|
g_lua.callFunction();
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::setupPackageLoader()
|
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, "package"); // package
|
|
|
|
lua_getfield(L, -1, "loaders"); // package.loaders, package
|
|
|
|
lua_remove(L, -2); // package.loaders
|
|
|
|
lua_pushnil(L); // nil, package.loaders
|
2011-04-22 00:44:30 +02:00
|
|
|
int numLoaders = 0;
|
|
|
|
while(lua_next(L, -2) != 0) {
|
2011-05-03 00:48:41 +02:00
|
|
|
// value, key, package.loaders
|
|
|
|
lua_pop(L, 1); // key, package.loaders
|
2011-04-22 00:44:30 +02:00
|
|
|
numLoaders++;
|
|
|
|
}
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_pushinteger(L, numLoaders + 1); // at, package.loaders
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaPackageLoader); // luaPackageLoader, at, package.loaders
|
|
|
|
lua_rawset(L, -3); // package.loaders
|
|
|
|
lua_pop(L, 1); // (empty)
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::registerClass(const std::string& klass, const std::string& baseClass)
|
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
// mt = {}
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, LUA_REGISTRYINDEX, (klass + "_mt").c_str());
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
// set __index metamethod
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaIndexMetaMethod);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
|
|
|
|
// set __newindex metamethod
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaNewIndexMetaMethod);
|
|
|
|
lua_setfield(L, -2, "__newindex");
|
2011-04-22 00:44:30 +02:00
|
|
|
|
|
|
|
// set __eq metamethod
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_pushcfunction(L, &LuaScript::luaEqualMetaMethod);
|
2011-04-22 00:44:30 +02:00
|
|
|
lua_setfield(L, -2, "__eq");
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
// set __gc metamethod, which collects userdata
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaGarbageCollectMetaMethod);
|
|
|
|
lua_setfield(L, -2, "__gc");
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
// klass = { }
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, klass.c_str());
|
2011-05-03 00:48:41 +02:00
|
|
|
|
|
|
|
// mt.methods = klass
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_setfield(L, -2, "methods");
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
// klass_fieldmethods = { }
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, (klass + "_fieldmethods").c_str());
|
2011-05-03 00:48:41 +02:00
|
|
|
|
|
|
|
// mt.fieldmethods = klass_fieldmethods
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_setfield(L, -2, "fieldmethods");
|
|
|
|
|
|
|
|
if(baseClass.length()) {
|
2011-05-03 00:48:41 +02:00
|
|
|
// get klass
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, klass.c_str());
|
|
|
|
|
|
|
|
// redirect = { __index = baseClass }
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, baseClass.c_str());
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
|
|
|
|
// setmetatable(klass, redirect)
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
// get klass_fieldmethods
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (klass + "_fieldmethods").c_str());
|
|
|
|
|
|
|
|
// redirect = { __index = baseClass_fieldmethods }
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (baseClass + "_fieldmethods").c_str());
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
|
|
|
|
// setmetatable(klass_fieldmethods, redirect)
|
|
|
|
lua_setmetatable(L, -2);
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
2011-04-22 00:44:30 +02:00
|
|
|
|
|
|
|
lua_pop(L, 1);
|
2011-05-03 00:48:41 +02:00
|
|
|
|
|
|
|
m_currentClass = klass;
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::registerMemberField(const std::string& field, LuaScript::LuaCFunction getFunction, LuaScript::LuaCFunction setFunction)
|
2011-05-02 06:13:57 +02:00
|
|
|
{
|
|
|
|
if(getFunction) {
|
|
|
|
int functionId = m_functions.size();
|
|
|
|
m_functions.push_back(getFunction);
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
// push the class table
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (m_currentClass + "_fieldmethods").c_str());
|
2011-05-02 06:13:57 +02:00
|
|
|
// push the function id
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
// store id in the closure
|
|
|
|
lua_pushcclosure(L, &LuaScript::luaFunctionCallback, 1);
|
|
|
|
// store the function at the class field functionName
|
|
|
|
lua_setfield(L, -2, ("get_" + field).c_str());
|
|
|
|
// pop the table
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
if(setFunction) {
|
|
|
|
int functionId = m_functions.size();
|
|
|
|
m_functions.push_back(setFunction);
|
|
|
|
|
|
|
|
// push the class table
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (m_currentClass + "_fieldmethods").c_str());
|
2011-05-02 06:13:57 +02:00
|
|
|
// push the function id
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
// store id in the closure
|
|
|
|
lua_pushcclosure(L, &LuaScript::luaFunctionCallback, 1);
|
|
|
|
// store the function at the class field functionName
|
|
|
|
lua_setfield(L, -2, ("set_" + field).c_str());
|
|
|
|
// pop the table
|
2011-04-22 00:44:30 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
void LuaScript::registerMemberFunction(const std::string& functionName, LuaCFunction function)
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
|
|
|
int functionId = m_functions.size();
|
|
|
|
m_functions.push_back(function);
|
|
|
|
|
|
|
|
// push the class table
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, m_currentClass.c_str());
|
2011-04-22 00:44:30 +02:00
|
|
|
// push the function id
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
// store id in the closure
|
|
|
|
lua_pushcclosure(L, &LuaScript::luaFunctionCallback, 1);
|
|
|
|
// store the function at the class field functionName
|
|
|
|
lua_setfield(L, -2, functionName.c_str());
|
|
|
|
// pop the table
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaScript::registerGlobalFunction(const std::string& functionName, LuaCFunction function)
|
|
|
|
{
|
|
|
|
int functionId = m_functions.size();
|
|
|
|
m_functions.push_back(function);
|
|
|
|
|
|
|
|
// push the function id
|
|
|
|
lua_pushnumber(L, functionId);
|
|
|
|
// store id in the closure
|
|
|
|
lua_pushcclosure(L, &LuaScript::luaFunctionCallback, 1);
|
|
|
|
// store the function in the global lua table
|
|
|
|
lua_setfield(L, LUA_GLOBALSINDEX, functionName.c_str());
|
|
|
|
}
|
|
|
|
|
2011-05-01 20:47:35 +02:00
|
|
|
void LuaScript::registerModule(const std::string& module)
|
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
registerClass(module);
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, module.c_str());
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, (module + "_mt").c_str());
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
lua_pop(L, -1);
|
2011-05-01 20:47:35 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
int LuaScript::luaPackageLoader(lua_State* L)
|
|
|
|
{
|
|
|
|
std::string fileName = lua_tostring(L, -1);
|
|
|
|
fileName += ".lua";
|
|
|
|
if(g_resources.fileExists(fileName)) {
|
|
|
|
std::string fileContents = g_resources.loadTextFile(fileName);
|
|
|
|
luaL_loadbuffer(L, fileContents.c_str(), fileContents.length(), fileName.c_str());
|
|
|
|
} else {
|
|
|
|
lua_pushfstring(L, "\n\tcould not load lua script " LUA_QS, fileName.c_str());
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
int LuaScript::luaIndexMetaMethod(lua_State* L)
|
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
// stack: key, obj
|
2011-05-02 06:13:57 +02:00
|
|
|
std::string key = lua_tostring(L, -1); // key, obj
|
|
|
|
lua_pop(L, 1); // obj
|
|
|
|
lua_getmetatable(L, -1); // mt, obj
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_getfield(L, -1, "fieldmethods"); // fieldmethods, mt, obj
|
|
|
|
lua_getfield(L, -1, ("get_" + key).c_str()); // get_method, fieldmethods, mt, obj
|
|
|
|
lua_remove(L, -2); // get_method, mt, obj
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
lua_remove(L, -2); // get_method, obj
|
|
|
|
lua_insert(L, -2); // obj, get_method
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler); // errorfunc, obj,get_method
|
|
|
|
lua_insert(L, -3); // obj, get_method, errorfunc
|
|
|
|
int ret = lua_pcall(L, 1, 1, -3); // ret, errorfunc
|
|
|
|
if(ret != 0) {
|
|
|
|
g_lua.reportError(g_lua.popString()); // errofunc
|
|
|
|
lua_pop(L, 1); // (empty)
|
|
|
|
lua_pushnil(L); // nil
|
|
|
|
} else {
|
|
|
|
lua_remove(L, -2); // ret
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
lua_pop(L, 1); // mt, obj
|
|
|
|
lua_getfield(L, -1, "methods"); // methods, mt, obj
|
|
|
|
lua_getfield(L, -1, key.c_str()); // method, methods, mt, obj
|
|
|
|
lua_remove(L, -2); // method, mt, obj
|
2011-05-02 06:13:57 +02:00
|
|
|
if(!lua_isnil(L, -1)) {
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_insert(L, -3); // obj, mt, method
|
|
|
|
lua_pop(L, 2); // method
|
2011-05-02 06:13:57 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_pop(L, 1); // mt, obj
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_pop(L, 1); // obj
|
2011-05-02 06:13:57 +02:00
|
|
|
|
2011-05-03 00:48:41 +02:00
|
|
|
// stack: obj
|
|
|
|
if(g_lua.isTable()) {
|
|
|
|
g_lua.rawGet(key);
|
|
|
|
g_lua.remove(-2);
|
|
|
|
} else if(g_lua.isUserdata()) {
|
|
|
|
g_lua.getScriptableField(g_lua.popClassInstance(), key);
|
|
|
|
} else {
|
|
|
|
g_lua.pop();
|
2011-05-02 06:13:57 +02:00
|
|
|
g_lua.pushNil();
|
2011-05-03 00:48:41 +02:00
|
|
|
g_lua.reportErrorWithTraceback("unknown type in newindex metamethod");
|
|
|
|
}
|
2011-05-02 06:13:57 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::luaNewIndexMetaMethod(lua_State* L)
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
2011-05-02 06:13:57 +02:00
|
|
|
// stack: value, key, obj
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_insert(L, -2); // key, value, obj
|
2011-05-02 06:13:57 +02:00
|
|
|
std::string key = lua_tostring(L, -1);
|
2011-05-03 00:48:41 +02:00
|
|
|
lua_pop(L, 1); // value, obj
|
|
|
|
lua_getmetatable(L, -2); // mt, value, obj
|
|
|
|
lua_getfield(L, -1, "fieldmethods"); // fieldmethods, mt, value, obj
|
|
|
|
lua_getfield(L, -1, ("set_" + key).c_str()); // set_method, fieldmethods, mt, value, obj
|
|
|
|
lua_remove(L, -2); // set_method, mt, value, obj
|
|
|
|
if(!lua_isnil(L, -1)) {
|
|
|
|
lua_remove(L, -2); // set_method, value, obj
|
|
|
|
lua_insert(L, -3); // value, obj, set_method
|
|
|
|
lua_pushcfunction(L, &LuaScript::luaErrorHandler); // errorfunc, value, obj, set_method
|
|
|
|
lua_insert(L, -4); // value, obj, set_method, errorfunc
|
|
|
|
int ret = lua_pcall(L, 2, 0, -4); // errorfunc
|
|
|
|
if(ret != 0)
|
|
|
|
g_lua.reportError(g_lua.popString()); // errofunc
|
|
|
|
lua_pop(L, 1); // (empty)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pop(L, 2); // mt, value, obj
|
|
|
|
|
|
|
|
// stack: value, obj
|
|
|
|
g_lua.insert(-2);
|
|
|
|
if(g_lua.isTable()) {
|
|
|
|
g_lua.insert(-2);
|
|
|
|
g_lua.rawSet(key);
|
|
|
|
g_lua.pop();
|
|
|
|
} else if(g_lua.isUserdata()) {
|
|
|
|
g_lua.setScriptableField(g_lua.popClassInstance(), key);
|
|
|
|
} else {
|
|
|
|
g_lua.pop(2);
|
|
|
|
g_lua.reportErrorWithTraceback("unknown type in index metamethod");
|
2011-05-02 06:13:57 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
int LuaScript::luaEqualMetaMethod(lua_State* L)
|
2011-04-22 00:44:30 +02:00
|
|
|
{
|
|
|
|
if(!lua_isuserdata(L, -1) || !lua_isuserdata(L, -2)) {
|
|
|
|
lua_pop(L, 2);
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
} else {
|
2011-05-02 06:13:57 +02:00
|
|
|
ScriptablePtr *objectRef1 = (ScriptablePtr *)lua_touserdata(L, -1);
|
|
|
|
ScriptablePtr *objectRef2 = (ScriptablePtr *)lua_touserdata(L, -2);
|
2011-04-22 00:44:30 +02:00
|
|
|
lua_pop(L, 2);
|
2011-05-02 06:13:57 +02:00
|
|
|
if(objectRef1 == objectRef2)
|
2011-04-22 00:44:30 +02:00
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
else
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-02 06:13:57 +02:00
|
|
|
int LuaScript::luaGarbageCollectMetaMethod(lua_State* L)
|
|
|
|
{
|
2011-05-03 00:48:41 +02:00
|
|
|
if(lua_isuserdata(L, -1)) {
|
|
|
|
ScriptablePtr *objectRef = (ScriptablePtr *)lua_touserdata(L, -1);
|
|
|
|
objectRef->reset();
|
|
|
|
}
|
2011-05-02 06:13:57 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
int LuaScript::luaFunctionCallback(lua_State* L)
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
// look for function id
|
2011-04-22 00:44:30 +02:00
|
|
|
int id = lua_tonumber(L, lua_upvalueindex(1));
|
2011-04-22 20:48:02 +02:00
|
|
|
// call the function
|
2011-05-01 20:47:35 +02:00
|
|
|
return (*(g_lua.m_functions[id]))();
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
int LuaScript::luaErrorHandler(lua_State *L)
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
// push debug
|
2011-04-22 15:49:46 +02:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
|
|
|
|
if(!lua_istable(L, -1)) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-22 20:48:02 +02:00
|
|
|
// push debug.traceback
|
2011-04-22 15:49:46 +02:00
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
if(!lua_isfunction(L, -1)) {
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-22 20:48:02 +02:00
|
|
|
// remove debug
|
|
|
|
lua_remove(L, -2);
|
|
|
|
// push additional error message
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
// push level
|
2011-04-22 15:49:46 +02:00
|
|
|
lua_pushinteger(L, 2);
|
2011-04-22 20:48:02 +02:00
|
|
|
// call debug.traceback
|
2011-04-22 15:49:46 +02:00
|
|
|
lua_call(L, 2, 1);
|
2011-04-22 20:48:02 +02:00
|
|
|
// remove additional error message
|
|
|
|
lua_remove(L, -2);
|
|
|
|
// return, the complete error message is on the stack
|
2011-04-22 15:49:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|