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 | |
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')
-rw-r--r-- | src/common/resourcemanager.cpp | 11 | ||||
-rw-r--r-- | src/common/resourcemanager.hpp | 6 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 25 |
3 files changed, 41 insertions, 1 deletions
diff --git a/src/common/resourcemanager.cpp b/src/common/resourcemanager.cpp index 5c3261f0..5a56479b 100644 --- a/src/common/resourcemanager.cpp +++ b/src/common/resourcemanager.cpp @@ -55,8 +55,17 @@ bool ResourceManager::exists(const std::string &path) return PHYSFS_exists(path.c_str()); } +std::string ResourceManager::resolve(const std::string &path) +{ + const char *realDir = PHYSFS_getRealDir(path.c_str()); + if (realDir) + return std::string(realDir) + "/" + path; + + return std::string(); +} + char *ResourceManager::loadFile(const std::string &fileName, int &fileSize, - bool removeBOM) + bool removeBOM) { // Attempt to open the specified file using PhysicsFS PHYSFS_file* file = PHYSFS_openRead(fileName.c_str()); diff --git a/src/common/resourcemanager.hpp b/src/common/resourcemanager.hpp index 74c41891..f5b1b1ef 100644 --- a/src/common/resourcemanager.hpp +++ b/src/common/resourcemanager.hpp @@ -36,6 +36,12 @@ namespace ResourceManager bool exists(const std::string &path); /** + * Returns the real file-system path of the resource with the given + * resource path. + */ + std::string resolve(const std::string &path); + + /** * Allocates data into a buffer pointer for raw data loading. The * returned data is expected to be freed using <code>free()</code>. * 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 }, |