diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2010-05-21 01:00:01 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2010-05-21 01:10:56 +0200 |
commit | 010e0fda51faf69b2b0319e7e1500e13c780fb4d (patch) | |
tree | 9364cbcbd1e9f2342a3436a7079713137b78aa98 /src/scripting | |
parent | 3cab86bc3abf5bceaf8dde8c7b980049b48702aa (diff) | |
download | manaserv-010e0fda51faf69b2b0319e7e1500e13c780fb4d.tar.gz manaserv-010e0fda51faf69b2b0319e7e1500e13c780fb4d.tar.bz2 manaserv-010e0fda51faf69b2b0319e7e1500e13c780fb4d.tar.xz manaserv-010e0fda51faf69b2b0319e7e1500e13c780fb4d.zip |
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
Diffstat (limited to 'src/scripting')
-rw-r--r-- | src/scripting/lua.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
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 <lauxlib.h> } +#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 }, |