summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/resourcemanager.cpp11
-rw-r--r--src/common/resourcemanager.hpp6
-rw-r--r--src/scripting/lua.cpp25
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 },