Standardise path-handling in XML mode files

aircraft-dir/fg-data paths always work, and
paths relative to the location of the current XML
file always work.
This commit is contained in:
James Turner 2010-08-17 11:05:55 +01:00
parent d7bea0c4c6
commit a7439aa056
3 changed files with 51 additions and 39 deletions

View File

@ -57,7 +57,7 @@ using namespace simgear;
using namespace osg; using namespace osg;
static osg::Node * static osg::Node *
sgLoad3DModel_internal(const std::string& path, sgLoad3DModel_internal(const SGPath& path,
const osgDB::ReaderWriter::Options* options, const osgDB::ReaderWriter::Options* options,
SGPropertyNode *overlay = 0); SGPropertyNode *overlay = 0);
@ -82,9 +82,15 @@ SGReaderWriterXML::readNode(const std::string& fileName,
{ {
osg::Node *result=0; osg::Node *result=0;
try { try {
result=sgLoad3DModel_internal(fileName, options); SGPath p = SGModelLib::findDataFile(fileName);
} catch (const sg_throwable &t) { if (!p.exists()) {
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage()); return ReadResult::FILE_NOT_FOUND;
}
result=sgLoad3DModel_internal(p, options);
} catch (const sg_exception &t) {
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage()
<< "\n\tfrom:" << fileName);
result=new osg::Node; result=new osg::Node;
} }
if (result) if (result)
@ -193,17 +199,24 @@ void makeEffectAnimations(PropertyList& animation_nodes,
} }
static osg::Node * static osg::Node *
sgLoad3DModel_internal(const string &path, sgLoad3DModel_internal(const SGPath& path,
const osgDB::ReaderWriter::Options* options_, const osgDB::ReaderWriter::Options* options_,
SGPropertyNode *overlay) SGPropertyNode *overlay)
{ {
if (!path.exists()) {
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path.str() << "\"");
return NULL;
}
const SGReaderWriterXMLOptions* xmlOptions; const SGReaderWriterXMLOptions* xmlOptions;
xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_); xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
SGSharedPtr<SGPropertyNode> prop_root; SGSharedPtr<SGPropertyNode> prop_root;
osg::Node *(*load_panel)(SGPropertyNode *)=0; osg::Node *(*load_panel)(SGPropertyNode *)=0;
osg::ref_ptr<SGModelData> data; osg::ref_ptr<SGModelData> data;
SGPath modelpath; SGPath modelpath(path);
SGPath texturepath(path);
SGPath modelDir(modelpath.dir());
if (xmlOptions) { if (xmlOptions) {
prop_root = xmlOptions->getPropRoot(); prop_root = xmlOptions->getPropRoot();
@ -211,19 +224,10 @@ sgLoad3DModel_internal(const string &path,
data = xmlOptions->getModelData(); data = xmlOptions->getModelData();
} }
modelpath = SGModelLib::findDataFile(path);
if (!prop_root) { if (!prop_root) {
prop_root = new SGPropertyNode; prop_root = new SGPropertyNode;
} }
if (modelpath.str().empty()) {
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
return 0;
}
SGPath texturepath = modelpath;
osg::ref_ptr<osg::Node> model; osg::ref_ptr<osg::Node> model;
osg::ref_ptr<osg::Group> group; osg::ref_ptr<osg::Group> group;
SGPropertyNode_ptr props = new SGPropertyNode; SGPropertyNode_ptr props = new SGPropertyNode;
@ -232,7 +236,7 @@ sgLoad3DModel_internal(const string &path,
if (modelpath.extension() == "xml") { if (modelpath.extension() == "xml") {
try { try {
readProperties(modelpath.str(), props); readProperties(modelpath.str(), props);
} catch (const sg_throwable &t) { } catch (const sg_exception &t) {
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: " SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
<< t.getFormattedMessage()); << t.getFormattedMessage());
throw; throw;
@ -241,11 +245,9 @@ sgLoad3DModel_internal(const string &path,
copyProperties(overlay, props); copyProperties(overlay, props);
if (props->hasValue("/path")) { if (props->hasValue("/path")) {
modelpath = modelpath.dir(); modelpath = SGModelLib::findDataFile(props->getStringValue("/path"), NULL, modelDir);
modelpath.append(props->getStringValue("/path"));
if (props->hasValue("/texture-path")) { if (props->hasValue("/texture-path")) {
texturepath = texturepath.dir(); texturepath = SGModelLib::findDataFile(props->getStringValue("/texture-path"), NULL, modelDir);
texturepath.append(props->getStringValue("/texture-path"));
} }
} else { } else {
model = new osg::Node; model = new osg::Node;
@ -255,8 +257,7 @@ sgLoad3DModel_internal(const string &path,
if (mp && prop_root && prop_root->getParent()) if (mp && prop_root && prop_root->getParent())
copyProperties(mp, prop_root); copyProperties(mp, prop_root);
} else { } else {
SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: " // model without wrapper
<< modelpath.str());
} }
osg::ref_ptr<SGReaderWriterXMLOptions> options osg::ref_ptr<SGReaderWriterXMLOptions> options
@ -275,8 +276,8 @@ sgLoad3DModel_internal(const string &path,
= osgDB::Registry::instance()->readNode(modelpath.str(), = osgDB::Registry::instance()->readNode(modelpath.str(),
options.get()); options.get());
if (!modelResult.validNode()) if (!modelResult.validNode())
throw sg_io_exception("Failed to load 3D model", throw sg_io_exception("Failed to load 3D model:" + modelResult.message(),
sg_location(modelpath.str())); modelpath.str());
model = copyModel(modelResult.getNode()); model = copyModel(modelResult.getNode());
// Add an extra reference to the model stored in the database. // Add an extra reference to the model stored in the database.
// That is to avoid expiring the object from the cache even if // That is to avoid expiring the object from the cache even if
@ -334,25 +335,21 @@ sgLoad3DModel_internal(const string &path,
SGPath submodelpath; SGPath submodelpath;
osg::ref_ptr<osg::Node> submodel; osg::ref_ptr<osg::Node> submodel;
string submodelFileName = sub_props->getStringValue("path");
if (submodelFileName.size() > 2 SGPath submodelPath = SGModelLib::findDataFile(sub_props->getStringValue("path"),
&& !submodelFileName.compare(0, 2, "./" )) { NULL, modelDir);
submodelpath = modelpath.dir();
submodelpath.append( submodelFileName.substr( 2 ) );
} else {
submodelpath = submodelFileName;
}
osg::ref_ptr<SGReaderWriterXMLOptions> options; osg::ref_ptr<SGReaderWriterXMLOptions> options;
options = new SGReaderWriterXMLOptions(*options_); options = new SGReaderWriterXMLOptions(*options_);
options->setPropRoot(prop_root); options->setPropRoot(prop_root);
options->setLoadPanel(load_panel); options->setLoadPanel(load_panel);
try { try {
submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(), submodel = sgLoad3DModel_internal(submodelPath, options.get(),
sub_props->getNode("overlay")); sub_props->getNode("overlay"));
} catch (const sg_throwable &t) { } catch (const sg_exception &t) {
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage()); SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage()
throw; << "\n\tfrom:" << t.getOrigin());
} }
osg::ref_ptr<osg::Node> submodel_final = submodel; osg::ref_ptr<osg::Node> submodel_final = submodel;

View File

@ -73,8 +73,20 @@ void SGModelLib::setResolveFunc(resolve_func rf)
} }
std::string SGModelLib::findDataFile(const std::string& file, std::string SGModelLib::findDataFile(const std::string& file,
const osgDB::ReaderWriter::Options* opts) const osgDB::ReaderWriter::Options* opts,
SGPath currentPath)
{ {
// if we have a valid current path, first attempt to resolve relative
// to that path
if (currentPath.exists()) {
SGPath p = currentPath;
p.append(file);
if (p.exists()) {
return p.str();
}
}
// next try the resolve function if one has been defined
if (static_resolver) { if (static_resolver) {
SGPath p = static_resolver(file); SGPath p = static_resolver(file);
if (p.exists()) { if (p.exists()) {
@ -82,6 +94,7 @@ std::string SGModelLib::findDataFile(const std::string& file,
} }
} }
// finally hand on to standard OSG behaviour
return osgDB::findDataFile(file, opts); return osgDB::findDataFile(file, opts);
} }

View File

@ -70,7 +70,9 @@ public:
SGPropertyNode *prop_root = NULL, SGPropertyNode *prop_root = NULL,
SGModelData *data=0); SGModelData *data=0);
static std::string findDataFile(const std::string& file, const osgDB::ReaderWriter::Options* opts = NULL); static std::string findDataFile(const std::string& file,
const osgDB::ReaderWriter::Options* opts = NULL,
SGPath currentDir = SGPath());
protected: protected:
SGModelLib(); SGModelLib();
~SGModelLib (); ~SGModelLib ();