From 010e0fda51faf69b2b0319e7e1500e13c780fb4d Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Fri, 21 May 2010 01:00:01 +0200 Subject: Added a package loader that goes through the resource manager Fixes issues with finding Lua scripts after changing the way client and server data paths are set up. Lua scripts can now use 'require' with paths relative from the serverDataPath to include other Lua scripts. Reviewed-by: Jared Adams --- src/scripting/lua.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/scripting/lua.cpp') diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 86bf38f8..790d6d91 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -26,6 +26,7 @@ extern "C" { #include } +#include "common/resourcemanager.hpp" #include "game-server/accountconnection.hpp" #include "game-server/buysell.hpp" #include "game-server/character.hpp" @@ -1535,12 +1536,36 @@ static int item_drop(lua_State *s) } +static int require_loader(lua_State *s) +{ + // Add .lua extension (maybe only do this when it doesn't have it already) + std::string filename = luaL_checkstring(s, 1); + filename.append(".lua"); + + const std::string path = ResourceManager::resolve(filename); + if (!path.empty()) + luaL_loadfile(s, path.c_str()); + else + lua_pushstring(s, "File not found"); + + return 1; +} + + LuaScript::LuaScript(): nbArgs(-1) { mState = luaL_newstate(); luaL_openlibs(mState); + // Register package loader that goes through the resource manager + // table.insert(package.loaders, 2, require_loader) + lua_getglobal(mState, "package"); + lua_getfield(mState, -1, "loaders"); + lua_pushcfunction(mState, require_loader); + lua_rawseti(mState, -2, 2); + lua_pop(mState, 2); + // Put some callback functions in the scripting environment. static luaL_reg const callbacks[] = { { "npc_create", &npc_create }, -- cgit v1.2.3-70-g09d2