1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version.  The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #include <osgDB/FileNameUtils>
15 #include <osgDB/FileUtils>
16 #include <osgDB/Registry>
17 
18 #include "PythonScriptEngine.h"
19 
20 class ReaderWriterPython : public osgDB::ReaderWriter
21 {
22     public:
23 
ReaderWriterPython()24         ReaderWriterPython()
25         {
26             supportsExtension("python","python script");
27         }
28 
className() const29         virtual const char* className() const { return "Python ScriptEngine plugin"; }
30 
readObject(std::istream & fin,const osgDB::ReaderWriter::Options * options=NULL) const31         virtual ReadResult readObject(std::istream& fin, const osgDB::ReaderWriter::Options* options =NULL) const
32         {
33             return readScript(fin);
34         }
35 
readObject(const std::string & file,const osgDB::ReaderWriter::Options * options=NULL) const36         virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
37         {
38             if (file=="ScriptEngine.python") return new python::PythonScriptEngine();
39 
40             return readScript(file, options);
41         }
42 
readScript(std::istream & fin,const osgDB::ReaderWriter::Options * options=NULL) const43         virtual ReadResult readScript(std::istream& fin, const osgDB::ReaderWriter::Options* options =NULL) const
44         {
45             osg::ref_ptr<osg::Script> script = new osg::Script;
46             script->setLanguage("python");
47 
48             std::string str;
49             while(fin)
50             {
51                 int c = fin.get();
52                 if (c>=0 && c<=255)
53                 {
54                     str.push_back(c);
55                 }
56             }
57             script->setScript(str);
58 
59             return script.release();
60         }
61 
62 
readScript(const std::string & file,const osgDB::ReaderWriter::Options * options=NULL) const63         virtual ReadResult readScript(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
64         {
65             if (file=="ScriptEngine.python") return new python::PythonScriptEngine();
66             std::string ext = osgDB::getLowerCaseFileExtension(file);
67             if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
68 
69             std::string fileName = osgDB::findDataFile( file, options );
70             if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
71 
72             osgDB::ifstream istream(fileName.c_str(), std::ios::in);
73             if(!istream) return ReadResult::FILE_NOT_HANDLED;
74 
75             return readScript(istream, options);
76         }
77 
78 };
79 
80 // now register with Registry to instantiate the above
81 // reader/writer.
82 REGISTER_OSGPLUGIN(python, ReaderWriterPython)
83