Skip to content

Commit

Permalink
added possibility to add a path to the lua search path (package.path/…
Browse files Browse the repository at this point in the history
…LUA_PATH)
  • Loading branch information
Guillaume Maury committed Aug 1, 2011
1 parent 61f29e9 commit 6446346
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cocos2dx/include/CCScriptSupport.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class CC_DLL CCScriptEngineProtocol


// execute a schedule function // execute a schedule function
virtual bool executeSchedule(const char* pszFuncName, ccTime t) = 0; virtual bool executeSchedule(const char* pszFuncName, ccTime t) = 0;
// add a search path
virtual bool addSearchPath(const char* pszPath) = 0;
}; };


/** /**
Expand Down
6 changes: 6 additions & 0 deletions lua/cocos2dx_support/LuaEngine.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ bool LuaEngine::executeSchedule(const char* pszFuncName, ccTime t)
{ {
return CCLuaScriptModule::sharedLuaScriptModule()->executeSchedule(pszFuncName, t); return CCLuaScriptModule::sharedLuaScriptModule()->executeSchedule(pszFuncName, t);
} }

bool LuaEngine::addSearchPath(const char* pszPath)
{
return CCLuaScriptModule::sharedLuaScriptModule()->addSearchPath(pszPath);
}

2 changes: 2 additions & 0 deletions lua/cocos2dx_support/LuaEngine.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class LUA_DLL LuaEngine : public cocos2d::CCScriptEngineProtocol


// execute a schedule function // execute a schedule function
virtual bool executeSchedule(const char* pszFuncName, cocos2d::ccTime t); virtual bool executeSchedule(const char* pszFuncName, cocos2d::ccTime t);
// add a search path
virtual bool addSearchPath(const char* pszPath);
}; };


#endif // __LUA_ENGINE_H__ #endif // __LUA_ENGINE_H__
17 changes: 17 additions & 0 deletions lua/cocos2dx_support/LuaEngineImpl.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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 Execute script file
*************************************************************************/ *************************************************************************/
Expand Down
10 changes: 9 additions & 1 deletion lua/cocos2dx_support/LuaEngineImpl.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ class CCLuaScriptModule : public cocos2d::CCObject
@brief Destructor for LuaScriptModule class. @brief Destructor for LuaScriptModule class.
*/ */
virtual ~CCLuaScriptModule(); 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 Script Execution Functions
Expand Down

0 comments on commit 6446346

Please sign in to comment.