From 68c4eaaff12ebfd53950fde025239cc1b05a6964 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 17 Sep 2014 17:40:07 +0000 Subject: [PATCH] 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 --- src/osgPlugins/lua/LuaScriptEngine.cpp | 37 ++++++++++++++++++++++++++ src/osgPlugins/lua/LuaScriptEngine.h | 4 +++ src/osgPlugins/lua/ReaderWriterLua.cpp | 35 +++++++++++++++++++----- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index 0fd7cde13..a714954e5 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -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 = "<getDatabasePathList()); +} + diff --git a/src/osgPlugins/lua/LuaScriptEngine.h b/src/osgPlugins/lua/LuaScriptEngine.h index 20eb78094..37f2f7858 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.h +++ b/src/osgPlugins/lua/LuaScriptEngine.h @@ -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(); diff --git a/src/osgPlugins/lua/ReaderWriterLua.cpp b/src/osgPlugins/lua/ReaderWriterLua.cpp index e5a148ddb..b08d8def7 100644 --- a/src/osgPlugins/lua/ReaderWriterLua.cpp +++ b/src/osgPlugins/lua/ReaderWriterLua.cpp @@ -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 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 se = new lua::LuaScriptEngine(); + osg::ref_ptr 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 local_opt = options ? static_cast(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 local_opt = options ? static_cast(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 local_opt = options ? static_cast(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