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

749 lines
19 KiB
C++
Raw Normal View History

2011-07-13 23:12:36 +02:00
#include <global.h>
2011-07-17 02:13:53 +02:00
#include <script/scriptcontext.h>
#include <script/scriptfunctions.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
2011-07-13 23:12:36 +02:00
#include <boost/algorithm/string.hpp>
2011-07-17 02:13:53 +02:00
ScriptContext g_lua;
2011-04-22 00:44:30 +02:00
2011-07-17 02:13:53 +02:00
void ScriptContext::init()
2011-04-22 00:44:30 +02:00
{
L = luaL_newstate();
if(!L)
2011-07-13 23:12:36 +02:00
fatal("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-07-17 02:13:53 +02:00
registerScriptFunctions();
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
void ScriptContext::terminate()
2011-04-22 00:44:30 +02:00
{
collectGarbage();
2011-04-22 00:44:30 +02:00
lua_close(L);
L = NULL;
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
void ScriptContext::loadAllModules()
{
2011-05-19 19:11:05 +02:00
std::list<std::string> modules = g_resources.listDirectoryFiles("modules");
foreach(const std::string& module, modules) {
2011-05-19 19:11:05 +02:00
std::list<std::string> moduleFiles = g_resources.listDirectoryFiles("modules/" + module);
foreach(const std::string& moduleFile, moduleFiles) {
if(boost::ends_with(moduleFile, ".lua"))
loadFile(std::string("modules/") + module + "/" + moduleFile);
}
}
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::loadFile(const std::string& fileName)
2011-04-22 00:44:30 +02:00
{
2011-05-19 19:11:05 +02:00
std::stringstream fin;
if(g_resources.loadFile(fileName, fin))
return loadBuffer(fin.str(), fileName);
else
2011-07-13 23:12:36 +02:00
error("ERROR: script file '", fileName, "' doesn't exist");
2011-05-19 19:11:05 +02:00
return false;
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::loadBuffer(const std::string& text, const std::string& what)
2011-04-22 00:44:30 +02:00
{
2011-05-19 19:11:05 +02:00
if(loadBufferAsFunction(text, what) && callFunction())
return true;
return false;
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::loadBufferAsFunction(const std::string& text, const std::string& what)
2011-04-22 00:44:30 +02:00
{
2011-05-19 19:11:05 +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){
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-07-17 02:13:53 +02:00
void ScriptContext::reportError(const std::string& errorDesc, const char *funcName)
{
std::stringstream ss;
ss << "LUA Script ERROR: ";
if(funcName)
ss << " in " << funcName << "(): ";
2011-05-03 00:48:41 +02:00
ss << errorDesc;
2011-07-13 23:12:36 +02:00
error(ss.str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::reportErrorWithTraceback(const std::string& errorDesc, const char* funcName)
{
pushString(errorDesc);
luaErrorHandler(L);
reportError(popString(), funcName);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::collectGarbage()
{
for(int i=0;i<2;i++)
lua_gc(L, LUA_GCCOLLECT, 0);
}
2011-07-17 02:13:53 +02:00
int ScriptContext::getStackSize()
2011-04-22 00:44:30 +02:00
{
return lua_gettop(L);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::insert(int index)
{
2011-05-19 19:11:05 +02:00
if(index != -1)
lua_insert(L, index);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::swap(int index)
2011-05-19 19:11:05 +02:00
{
insert(index);
pushValue(index+1);
remove(index);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::remove(int index)
{
2011-05-03 00:48:41 +02:00
lua_remove(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::next(int index)
{
return lua_next(L, index);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::releaseRef(int ref)
{
luaL_unref(L, LUA_REGISTRYINDEX, ref);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::newTable()
{
2011-05-03 00:48:41 +02:00
lua_newtable(L);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::setTable(int index)
2011-05-03 00:48:41 +02:00
{
lua_settable(L, index);
}
2011-07-17 02:13:53 +02:00
void *ScriptContext::newUserdata(int size)
2011-05-03 00:48:41 +02:00
{
return lua_newuserdata(L, size);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::newMetatable(const std::string& name)
2011-05-03 00:48:41 +02:00
{
luaL_newmetatable(L, name.c_str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::setMetatable(const std::string& name, int index)
2011-05-03 00:48:41 +02:00
{
luaL_getmetatable(L, name.c_str());
if(isNil())
2011-07-13 23:12:36 +02:00
reportError(make_string("could not retrive metatable", name));
2011-05-03 00:48:41 +02:00
else
lua_setmetatable(L, index < 0 ? index-1 : index);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::rawGet(const std::string& key)
2011-05-03 00:48:41 +02:00
{
pushString(key);
lua_rawget(L, -2);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::rawSet(const std::string& key)
2011-05-03 00:48:41 +02:00
{
pushString(key);
insert(-2);
lua_rawset(L, -3);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::getField(const std::string& key)
2011-05-03 00:48:41 +02:00
{
lua_getfield(L, -1, key.c_str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::setField(const std::string& key)
2011-05-03 00:48:41 +02:00
{
lua_setfield(L, -2, key.c_str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::getScriptObjectField(const ScriptObjectPtr& scriptobject, const std::string& field)
2011-05-03 00:48:41 +02:00
{
2011-07-17 02:13:53 +02:00
if(scriptobject) {
pushRef(scriptobject->getScriptTable());
2011-05-03 00:48:41 +02:00
getField(field);
remove(-2);
} else
pushNil();
}
2011-07-17 02:13:53 +02:00
void ScriptContext::setScriptObjectField(const ScriptObjectPtr& scriptobject, const std::string& field)
2011-05-03 00:48:41 +02:00
{
2011-07-17 02:13:53 +02:00
if(scriptobject) {
pushRef(scriptobject->getScriptTable());
2011-05-03 00:48:41 +02:00
insert(-2);
setField(field);
}
pop();
}
2011-07-17 02:13:53 +02:00
void ScriptContext::rawGetGlobalTableField(const std::string& globalTable, const std::string& key)
2011-05-03 00:48:41 +02:00
{
getGlobal(globalTable);
rawGet(key);
remove(-2);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::rawSetGlobalTableField(const std::string& globalTable, const std::string& key)
2011-05-03 00:48:41 +02:00
{
getGlobal(globalTable);
insert(-2);
rawSet(key);
pop();
}
2011-07-17 02:13:53 +02:00
void ScriptContext::getGlobal(const std::string& key)
2011-05-03 00:48:41 +02:00
{
lua_getfield(L, LUA_GLOBALSINDEX, key.c_str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::setGlobal(const std::string& key)
2011-05-03 00:48:41 +02:00
{
lua_setfield(L, LUA_GLOBALSINDEX, key.c_str());
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isNil(int index)
2011-05-03 00:48:41 +02:00
{
return lua_isnil(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isBoolean(int index)
2011-05-03 00:48:41 +02:00
{
return lua_isboolean(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isNumber(int index)
2011-05-03 00:48:41 +02:00
{
return lua_isnumber(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isString(int index)
2011-05-03 00:48:41 +02:00
{
return lua_isstring(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isTable(int index)
2011-05-03 00:48:41 +02:00
{
return lua_istable(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isUserdata(int index)
2011-05-03 00:48:41 +02:00
{
return lua_isuserdata(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isFunction(int index)
2011-05-03 00:48:41 +02:00
{
return lua_isfunction(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isCFunction(int index)
2011-05-03 00:48:41 +02:00
{
return lua_iscfunction(L, index);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::isLuaFunction(int index)
2011-05-03 00:48:41 +02:00
{
return isFunction(index) && !isCFunction(index);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::pop(int n)
2011-04-22 00:44:30 +02:00
{
lua_pop(L, n);
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::popBoolean()
2011-04-22 00:44:30 +02:00
{
bool b = (lua_toboolean(L, -1) != 0);
pop();
return b;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::popInteger()
2011-04-22 00:44:30 +02:00
{
double d = lua_tonumber(L, -1);
pop();
2011-04-22 00:44:30 +02:00
return (int)d;
}
2011-07-17 02:13:53 +02:00
std::string ScriptContext::popString()
2011-04-22 00:44:30 +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-07-17 02:13:53 +02:00
ScriptObjectPtr ScriptContext::popClassInstance()
2011-05-03 00:48:41 +02:00
{
2011-07-17 02:13:53 +02:00
ScriptObjectPtr object;
2011-05-03 00:48:41 +02:00
if(isUserdata()) { // instances are store as userdata
2011-07-17 02:13:53 +02:00
object = *((ScriptObjectPtr *)lua_touserdata(L, -1));
2011-05-03 00:48:41 +02:00
if(!object)
reportErrorWithTraceback("attempt to retrive class instance from a object that is already expired");
} else if(!isNil()) // we accept nil values
2011-07-17 02:13:53 +02:00
reportErrorWithTraceback("couldn't retrive class instance, the value is not a scriptobject type");
2011-05-03 00:48:41 +02:00
pop();
return object;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::popRef()
2011-05-03 00:48:41 +02:00
{
return luaL_ref(L, LUA_REGISTRYINDEX);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::pushNil()
2011-04-22 00:44:30 +02:00
{
lua_pushnil(L);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::pushBoolean(bool b)
2011-04-22 00:44:30 +02:00
{
lua_pushboolean(L, b);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::pushInteger(int32_t i)
2011-04-22 00:44:30 +02:00
{
lua_pushnumber(L, i);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::pushString(const std::string& str)
2011-04-22 00:44:30 +02:00
{
lua_pushstring(L, str.c_str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::pushClassInstance(const ScriptObjectPtr& object)
2011-04-22 00:44:30 +02:00
{
if(object) {
2011-07-17 02:13:53 +02:00
new(newUserdata(sizeof(ScriptObjectPtr))) ScriptObjectPtr(object);
setMetatable(std::string(object->getScriptObjectType()) + "_mt");
2011-05-03 00:48:41 +02:00
} else
pushNil();
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
void ScriptContext::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-07-17 02:13:53 +02:00
void ScriptContext::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-07-17 02:13:53 +02:00
std::string ScriptContext::getFunctionSourcePath(bool functionIsOnStack, int level)
{
2011-05-19 19:11:05 +02:00
std::string path;
lua_Debug ar;
memset(&ar, 0, sizeof(ar));
2011-05-21 20:40:06 +02:00
if(functionIsOnStack)
lua_getinfo(L, ">Sn", &ar);
else {
if(lua_getstack(L, level-1, &ar) == 1)
lua_getinfo(L, "Sn", &ar);
}
2011-05-19 19:11:05 +02:00
2011-05-21 20:40:06 +02:00
// c function, we must get information of a level above
2011-05-19 19:11:05 +02:00
if(strcmp("C", ar.what) == 0) {
memset(&ar, 0, sizeof(ar));
2011-05-21 20:40:06 +02:00
if(lua_getstack(L, level, &ar) == 1) {
2011-05-19 19:11:05 +02:00
lua_getinfo(L, "f", &ar);
2011-05-21 20:40:06 +02:00
return getFunctionSourcePath(true, level+1);
2011-05-19 19:11:05 +02:00
}
} else {
if(ar.source) {
std::string source = ar.source;
std::size_t pos = source.find_last_of('/');
if(source[0] == '@' && pos != std::string::npos)
path = source.substr(1, pos - 1);
} else {
2011-07-13 23:12:36 +02:00
error("no source");
2011-05-19 19:11:05 +02:00
}
}
return path;
}
2011-07-17 02:13:53 +02:00
bool ScriptContext::callFunction(int numArgs, int numRets)
2011-05-19 19:11:05 +02:00
{
pushValue(-numArgs - 1);
2011-05-21 20:40:06 +02:00
g_resources.pushCurrentPath(getFunctionSourcePath(true));
2011-05-19 19:11:05 +02:00
int size = getStackSize();
int errorIndex = -numArgs - 2;
2011-07-17 02:13:53 +02:00
lua_pushcfunction(L, &ScriptContext::luaErrorHandler);
2011-05-19 19:11:05 +02:00
insert(errorIndex);
2011-05-19 19:11:05 +02:00
int ret = lua_pcall(L, numArgs, numRets, errorIndex);
if(ret != 0) {
reportError(popString());
2011-05-19 19:11:05 +02:00
pop(); // pop error func
return false;
}
2011-05-19 19:11:05 +02:00
remove(-numRets - 1); // pop error func
2011-05-19 19:11:05 +02:00
if(getStackSize() != size - numArgs - 1 + numRets) {
reportError("stack size changed!");
2011-05-19 19:11:05 +02:00
return false;
}
g_resources.popCurrentPath();
return true;
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
void ScriptContext::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
}
2011-07-17 02:13:53 +02:00
void ScriptContext::setupPackageLoader()
2011-04-22 00:44:30 +02:00
{
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
2011-07-17 02:13:53 +02:00
lua_pushcfunction(L, &ScriptContext::luaPackageLoader); // luaPackageLoader, at, package.loaders
2011-05-03 00:48:41 +02:00
lua_rawset(L, -3); // package.loaders
lua_pop(L, 1); // (empty)
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
void ScriptContext::registerClass(const std::string& klass, const std::string& baseClass)
2011-04-22 00:44:30 +02:00
{
2011-05-03 00:48:41 +02:00
// mt = {}
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, (klass + "_mt").c_str());
2011-04-22 00:44:30 +02:00
// set __index metamethod
2011-07-17 02:13:53 +02:00
lua_pushcfunction(L, &ScriptContext::luaIndexMetaMethod);
lua_setfield(L, -2, "__index");
// set __newindex metamethod
2011-07-17 02:13:53 +02:00
lua_pushcfunction(L, &ScriptContext::luaNewIndexMetaMethod);
lua_setfield(L, -2, "__newindex");
2011-04-22 00:44:30 +02:00
// set __eq metamethod
2011-07-17 02:13:53 +02:00
lua_pushcfunction(L, &ScriptContext::luaEqualMetaMethod);
2011-04-22 00:44:30 +02:00
lua_setfield(L, -2, "__eq");
// set __gc metamethod, which collects userdata
2011-07-17 02:13:53 +02:00
lua_pushcfunction(L, &ScriptContext::luaGarbageCollectMetaMethod);
lua_setfield(L, -2, "__gc");
2011-04-22 00:44:30 +02:00
2011-05-03 00:48:41 +02:00
// klass = { }
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
lua_setfield(L, -2, "methods");
2011-05-03 00:48:41 +02:00
// klass_fieldmethods = { }
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
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-04-22 00:44:30 +02:00
lua_pop(L, 1);
2011-05-03 00:48:41 +02:00
m_currentClass = klass;
}
2011-04-22 00:44:30 +02:00
2011-07-17 02:13:53 +02:00
void ScriptContext::registerMemberField(const std::string& field, ScriptContext::LuaCFunction getFunction, ScriptContext::LuaCFunction setFunction)
{
if(getFunction) {
int functionId = m_functions.size();
m_functions.push_back(getFunction);
2011-04-22 00:44:30 +02:00
// push the class table
2011-05-03 00:48:41 +02:00
lua_getfield(L, LUA_GLOBALSINDEX, (m_currentClass + "_fieldmethods").c_str());
// push the function id
lua_pushnumber(L, functionId);
// store id in the closure
2011-07-17 02:13:53 +02:00
lua_pushcclosure(L, &ScriptContext::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
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());
// push the function id
lua_pushnumber(L, functionId);
// store id in the closure
2011-07-17 02:13:53 +02:00
lua_pushcclosure(L, &ScriptContext::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-07-17 02:13:53 +02:00
void ScriptContext::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
2011-07-17 02:13:53 +02:00
lua_pushcclosure(L, &ScriptContext::luaFunctionCallback, 1);
2011-04-22 00:44:30 +02:00
// store the function at the class field functionName
lua_setfield(L, -2, functionName.c_str());
// pop the table
lua_pop(L, 1);
}
2011-07-17 02:13:53 +02:00
void ScriptContext::registerGlobalFunction(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 function id
lua_pushnumber(L, functionId);
// store id in the closure
2011-07-17 02:13:53 +02:00
lua_pushcclosure(L, &ScriptContext::luaFunctionCallback, 1);
2011-04-22 00:44:30 +02:00
// store the function in the global lua table
lua_setfield(L, LUA_GLOBALSINDEX, functionName.c_str());
}
2011-07-17 02:13:53 +02:00
void ScriptContext::registerModule(const std::string& module)
2011-05-01 20:47:35 +02:00
{
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-07-17 02:13:53 +02:00
int ScriptContext::luaPackageLoader(lua_State* L)
2011-04-22 00:44:30 +02:00
{
std::string fileName = lua_tostring(L, -1);
fileName += ".lua";
if(g_resources.fileExists(fileName)) {
2011-05-19 19:11:05 +02:00
std::stringstream fin;
if(g_resources.loadFile(fileName, fin))
2011-07-17 08:56:57 +02:00
luaL_loadbuffer(L, fin.str().c_str(), fin.str().length(), ("@" + g_resources.resolvePath(fileName)).c_str());
2011-04-22 00:44:30 +02:00
} else {
lua_pushfstring(L, "\n\tcould not load lua script " LUA_QS, fileName.c_str());
}
return 1;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::luaIndexMetaMethod(lua_State* L)
{
2011-05-03 00:48:41 +02:00
// stack: key, obj
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
2011-05-19 19:11:05 +02:00
if(!g_lua.callFunction(1, 1))
lua_pushnil(L);
2011-05-03 00:48:41 +02:00
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
if(!lua_isnil(L, -1)) {
2011-05-19 19:11:05 +02:00
lua_insert(L, -3); // mt, obj, method
2011-05-03 00:48:41 +02:00
lua_pop(L, 2); // method
return 1;
}
2011-05-03 00:48:41 +02:00
lua_pop(L, 1); // mt, obj
}
2011-05-03 00:48:41 +02:00
lua_pop(L, 1); // obj
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()) {
2011-07-17 02:13:53 +02:00
g_lua.getScriptObjectField(g_lua.popClassInstance(), key);
2011-05-03 00:48:41 +02:00
} else {
g_lua.pop();
g_lua.pushNil();
2011-05-03 00:48:41 +02:00
g_lua.reportErrorWithTraceback("unknown type in newindex metamethod");
}
return 1;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::luaNewIndexMetaMethod(lua_State* L)
2011-04-22 00:44:30 +02:00
{
// stack: value, key, obj
2011-05-03 00:48:41 +02:00
lua_insert(L, -2); // key, value, obj
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
2011-05-19 19:11:05 +02:00
g_lua.callFunction(2);
2011-05-03 00:48:41 +02:00
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()) {
2011-07-17 02:13:53 +02:00
g_lua.setScriptObjectField(g_lua.popClassInstance(), key);
2011-05-03 00:48:41 +02:00
} else {
g_lua.pop(2);
g_lua.reportErrorWithTraceback("unknown type in index metamethod");
}
2011-04-22 00:44:30 +02:00
return 1;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::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-07-17 02:13:53 +02:00
ScriptObjectPtr *objectRef1 = (ScriptObjectPtr *)lua_touserdata(L, -1);
ScriptObjectPtr *objectRef2 = (ScriptObjectPtr *)lua_touserdata(L, -2);
2011-04-22 00:44:30 +02:00
lua_pop(L, 2);
if(objectRef1 == objectRef2)
2011-04-22 00:44:30 +02:00
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
}
return 1;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::luaGarbageCollectMetaMethod(lua_State* L)
{
2011-05-03 00:48:41 +02:00
if(lua_isuserdata(L, -1)) {
2011-07-17 02:13:53 +02:00
ScriptObjectPtr *objectRef = (ScriptObjectPtr *)lua_touserdata(L, -1);
2011-05-03 00:48:41 +02:00
objectRef->reset();
}
lua_pop(L, 1);
return 1;
}
2011-07-17 02:13:53 +02:00
int ScriptContext::luaFunctionCallback(lua_State* L)
2011-04-22 00:44:30 +02:00
{
// look for function id
2011-04-22 00:44:30 +02:00
int id = lua_tonumber(L, lua_upvalueindex(1));
2011-05-21 20:40:06 +02:00
g_resources.pushCurrentPath(g_lua.getFunctionSourcePath(false));
// call the function
2011-05-21 20:40:06 +02:00
int ret = (*(g_lua.m_functions[id]))();
g_resources.popCurrentPath();
return ret;
2011-04-22 00:44:30 +02:00
}
2011-07-17 02:13:53 +02:00
int ScriptContext::luaErrorHandler(lua_State *L)
{
// push debug
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if(!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}
// push debug.traceback
lua_getfield(L, -1, "traceback");
if(!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
// remove debug
lua_remove(L, -2);
// push additional error message
lua_pushvalue(L, -2);
// push level
lua_pushinteger(L, 2);
// call debug.traceback
lua_call(L, 2, 1);
// remove additional error message
lua_remove(L, -2);
// return, the complete error message is on the stack
return 1;
}