diff --git a/cocos2dx/include/CCScriptSupport.h b/cocos2dx/include/CCScriptSupport.h index f2174c33dc..5b14e602af 100644 --- a/cocos2dx/include/CCScriptSupport.h +++ b/cocos2dx/include/CCScriptSupport.h @@ -65,6 +65,8 @@ public: // execute a schedule function virtual bool executeSchedule(const char* pszFuncName, ccTime t) = 0; + // add a search path + virtual bool addSearchPath(const char* pszPath) = 0; }; /** diff --git a/lua/cocos2dx_support/LuaEngine.cpp b/lua/cocos2dx_support/LuaEngine.cpp index bc47acd47a..53bcff4ed0 100644 --- a/lua/cocos2dx_support/LuaEngine.cpp +++ b/lua/cocos2dx_support/LuaEngine.cpp @@ -81,3 +81,9 @@ bool LuaEngine::executeSchedule(const char* pszFuncName, ccTime t) { return CCLuaScriptModule::sharedLuaScriptModule()->executeSchedule(pszFuncName, t); } + +bool LuaEngine::addSearchPath(const char* pszPath) +{ + return CCLuaScriptModule::sharedLuaScriptModule()->addSearchPath(pszPath); +} + diff --git a/lua/cocos2dx_support/LuaEngine.h b/lua/cocos2dx_support/LuaEngine.h index 14c2ecd76b..f636c540a6 100644 --- a/lua/cocos2dx_support/LuaEngine.h +++ b/lua/cocos2dx_support/LuaEngine.h @@ -49,6 +49,8 @@ public: // execute a schedule function virtual bool executeSchedule(const char* pszFuncName, cocos2d::ccTime t); + // add a search path + virtual bool addSearchPath(const char* pszPath); }; #endif // __LUA_ENGINE_H__ diff --git a/lua/cocos2dx_support/LuaEngineImpl.cpp b/lua/cocos2dx_support/LuaEngineImpl.cpp index fd9a4cdf8d..b88d994241 100644 --- a/lua/cocos2dx_support/LuaEngineImpl.cpp +++ b/lua/cocos2dx_support/LuaEngineImpl.cpp @@ -100,6 +100,23 @@ CCLuaScriptModule::~CCLuaScriptModule() } + +/************************************************************************* + Add a path to find lua files in (equivalent to LUA_PATH) + *************************************************************************/ +bool CCLuaScriptModule::addSearchPath(const std::string& path) +{ + lua_getglobal( d_state, "package" ); + lua_getfield( d_state, -1, "path" ); // get field "path" from table at top of stack (-1) + const char* cur_path = lua_tostring( d_state, -1 ); // grab path string from top of stack + lua_pop( d_state, 1 ); // get rid of the string on the stack we just pushed on line 5 + lua_pushfstring(d_state, "%s;%s/?.lua", cur_path, path.c_str()); + lua_setfield( d_state, -2, "path" ); // set the field "path" in table at -2 with value at top of stack + lua_pop( d_state, 1 ); // get rid of package table from top of stack + return 0; // all done! +} + + /************************************************************************* Execute script file *************************************************************************/ diff --git a/lua/cocos2dx_support/LuaEngineImpl.h b/lua/cocos2dx_support/LuaEngineImpl.h index e0721b06c9..a08e32ea87 100644 --- a/lua/cocos2dx_support/LuaEngineImpl.h +++ b/lua/cocos2dx_support/LuaEngineImpl.h @@ -60,7 +60,15 @@ public: @brief Destructor for LuaScriptModule class. */ virtual ~CCLuaScriptModule(); - + + /************************************************************************* + Seting Functions + *************************************************************************/ + /** + @brief Add a path to find lua files in + @param path to be added to the Lua path + */ + bool addSearchPath(const std::string& path); /************************************************************************* Script Execution Functions