Added support for writing the file path of a script to the lua package.path to help with loading scripts within lua.
git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14451 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
@@ -4343,3 +4343,40 @@ void LuaScriptEngine::assignClosure(const char* name, lua_CFunction fn) const
|
||||
lua_pushcclosure(_lua, fn, 1);
|
||||
lua_settable(_lua, -3);
|
||||
}
|
||||
|
||||
void LuaScriptEngine::addPaths(const osgDB::FilePathList& paths)
|
||||
{
|
||||
lua_getglobal( _lua, "package" );
|
||||
|
||||
lua_getfield( _lua, -1, "path" );
|
||||
std::string path = lua_tostring( _lua, -1 );
|
||||
lua_pop( _lua, 1 );
|
||||
|
||||
OSG_NOTICE<<"LuaScriptEngine::addPaths() original package.path = "<<path<<std::endl;
|
||||
|
||||
|
||||
for(osgDB::FilePathList::const_iterator itr = paths.begin();
|
||||
itr != paths.end();
|
||||
++itr)
|
||||
{
|
||||
OSG_NOTICE<<" Appending path ["<<*itr<<"]"<<std::endl;
|
||||
|
||||
path.append( ";" );
|
||||
path.append( *itr );
|
||||
path.append( "/?.lua" );
|
||||
}
|
||||
|
||||
OSG_NOTICE<<" path after = "<<path<<std::endl;
|
||||
|
||||
lua_pushstring( _lua, path.c_str() );
|
||||
lua_setfield( _lua, -2, "path" );
|
||||
|
||||
lua_pop( _lua, 1 ); // return stack to orignal
|
||||
}
|
||||
|
||||
void LuaScriptEngine::addPaths(const osgDB::Options* options)
|
||||
{
|
||||
if (!options) return;
|
||||
addPaths(options->getDatabasePathList());
|
||||
}
|
||||
|
||||
|
||||
@@ -239,6 +239,10 @@ class LuaScriptEngine : public osg::ScriptEngine
|
||||
std::string lookUpGLenumString(GLenum value) const;
|
||||
GLenum lookUpGLenumValue(const std::string& str) const;
|
||||
|
||||
|
||||
void addPaths(const osgDB::FilePathList& paths);
|
||||
void addPaths(const osgDB::Options* options);
|
||||
|
||||
protected:
|
||||
|
||||
void initialize();
|
||||
|
||||
@@ -28,6 +28,16 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
|
||||
virtual const char* className() const { return "Lua ScriptEngine plugin"; }
|
||||
|
||||
lua::LuaScriptEngine* createScriptEngine(const osgDB::ReaderWriter::Options* options) const
|
||||
{
|
||||
osg::ref_ptr<lua::LuaScriptEngine> se = new lua::LuaScriptEngine();
|
||||
|
||||
// add file paths
|
||||
if (options) se->addPaths(options);
|
||||
else se->addPaths(osgDB::Registry::instance()->getOptions());
|
||||
|
||||
return se.release();
|
||||
}
|
||||
|
||||
|
||||
virtual ReadResult readObjectFromScript(std::istream& fin, const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
@@ -42,7 +52,8 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
osg::Parameters inputParameters;
|
||||
osg::Parameters outputParameters;
|
||||
|
||||
osg::ref_ptr<lua::LuaScriptEngine> se = new lua::LuaScriptEngine();
|
||||
osg::ref_ptr<lua::LuaScriptEngine> se = createScriptEngine(options);
|
||||
|
||||
if (!se->run(script.get(), entryPoint, inputParameters, outputParameters)) return 0;
|
||||
|
||||
if (outputParameters.empty()) return 0;
|
||||
@@ -85,7 +96,10 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
|
||||
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
{
|
||||
if (file=="ScriptEngine.lua") return new lua::LuaScriptEngine();
|
||||
if (file=="ScriptEngine.lua")
|
||||
{
|
||||
return createScriptEngine(options);
|
||||
}
|
||||
|
||||
std::string ext = osgDB::getLowerCaseFileExtension(file);
|
||||
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
||||
@@ -93,10 +107,13 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
std::string fileName = osgDB::findDataFile( file, options );
|
||||
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
|
||||
|
||||
osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
|
||||
local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
|
||||
|
||||
osgDB::ifstream istream(fileName.c_str(), std::ios::in);
|
||||
if(!istream) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
return readObject(istream, options);
|
||||
return readObject(istream, local_opt.get());
|
||||
}
|
||||
|
||||
virtual ReadResult readImage(std::istream& fin, const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
@@ -112,10 +129,13 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
std::string fileName = osgDB::findDataFile( file, options );
|
||||
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
|
||||
|
||||
osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
|
||||
local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
|
||||
|
||||
osgDB::ifstream istream(fileName.c_str(), std::ios::in);
|
||||
if(!istream) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
return readImage(istream, options);
|
||||
return readImage(istream, local_opt.get());
|
||||
}
|
||||
|
||||
virtual ReadResult readNode(std::istream& fin, const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
@@ -123,7 +143,7 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
return readObjectFromScript(fin, options);
|
||||
}
|
||||
|
||||
virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
{
|
||||
std::string ext = osgDB::getLowerCaseFileExtension(file);
|
||||
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
||||
@@ -131,10 +151,13 @@ class ReaderWriterLua : public osgDB::ReaderWriter
|
||||
std::string fileName = osgDB::findDataFile( file, options );
|
||||
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
|
||||
|
||||
osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
|
||||
local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
|
||||
|
||||
osgDB::ifstream istream(fileName.c_str(), std::ios::in);
|
||||
if(!istream) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
return readNode(istream, options);
|
||||
return readNode(istream, local_opt.get());
|
||||
}
|
||||
|
||||
virtual ReadResult readScript(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const
|
||||
|
||||
Reference in New Issue
Block a user