浏览代码

Added the game resource path to LUA_PATH to fix Lua path resolution when searching for scripts on certain platforms.

Steve Grenier 12 年之前
父节点
当前提交
f331f868e0
共有 1 个文件被更改,包括 23 次插入0 次删除
  1. 23 0
      gameplay/src/ScriptController.cpp

+ 23 - 0
gameplay/src/ScriptController.cpp

@@ -692,6 +692,26 @@ static const char* lua_dofile_function =
     "    end\n"
     "end\n";
 
+void appendLuaPath(lua_State* state, const char* path)
+{
+    lua_getglobal(state, "package");
+
+    // Get the current path string from top of stack
+    lua_getfield(state, -1, "path");
+    std::string cur_path = lua_tostring(state, -1);
+    lua_pop(state, 1);
+
+    // Append our game resource path to the path
+    cur_path += ';';
+    cur_path += path;
+    cur_path += "?.lua";
+
+    // Push the new path
+    lua_pushstring(state, cur_path.c_str());
+    lua_setfield(state, -2, "path");
+    lua_pop(state, 1);
+}
+
 void ScriptController::initialize()
 {
     _lua = luaL_newstate();
@@ -704,6 +724,9 @@ void ScriptController::initialize()
     ScriptUtil::registerFunction("convert", ScriptController::convert);
 #endif
 
+    // Append to the LUA_PATH to allow scripts to be found in the resource folder on all platforms
+    appendLuaPath(_lua, FileSystem::getResourcePath());
+
     // Create our own print() function that uses gameplay::print.
     if (luaL_dostring(_lua, lua_print_function))
         GP_ERROR("Failed to load custom print() function with error: '%s'.", lua_tostring(_lua, -1));