]> git.mxchange.org Git - simgear.git/commitdiff
Validate sound file paths in an XML file.
authorJames Turner <zakalawe@mac.com>
Sun, 5 Sep 2010 13:15:18 +0000 (14:15 +0100)
committerJames Turner <zakalawe@mac.com>
Sun, 5 Sep 2010 13:15:18 +0000 (14:15 +0100)
simgear/sound/sample_openal.cxx
simgear/sound/sample_openal.hxx
simgear/sound/soundmgr_openal.cxx
simgear/sound/xmlsound.cxx

index 0a69170c3328c83f8777716618067799cd110f5b..7b3550027c114e9ceb3cdab86cd3f6e511408121 100644 (file)
@@ -220,3 +220,13 @@ string SGSoundSample::random_string() {
       return rstr;
 }
 
+SGPath SGSoundSample::file_path() const
+{
+  if (!_is_file) {
+    return SGPath();
+  }
+  
+  return SGPath(_refname);
+}
+
+
index 75d0975285e7ad9e16639b1476cb520f5c03146b..491ba572ca220a2646f1b5302b72446371af5b93 100644 (file)
@@ -43,6 +43,8 @@
 #include <simgear/structure/SGSharedPtr.hxx>
 #include <simgear/math/SGMath.hxx>
 
+class SGPath;
+
 /**
  * manages everything we need to know for an individual audio sample
  */
@@ -89,6 +91,8 @@ public:
      */
     inline bool is_file() const { return _is_file; }
 
+    SGPath file_path() const;
+
     /**
      * Test if this audio sample configuration has changed since the last call.
      * Calling this function will reset the flag so calling it a second
index 369adede161aa2852d3b544a2882958a730bcfa2..c3eda45a0568e7cb2e9037a23b0f9a8cc9af6fb0 100644 (file)
@@ -470,11 +470,15 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample)
         if ( sample->is_file() ) {
             int freq, format;
             size_t size;
-            bool res;
-
-            res = load(sample_name, &sample_data, &format, &size, &freq);
-            if (res == false) return buffer;
 
+            try {
+              bool res = load(sample_name, &sample_data, &format, &size, &freq);
+              if (res == false) return buffer;
+            } catch (sg_exception& e) {
+              SG_LOG(SG_GENERAL, SG_ALERT, "failed to load sound buffer:" << e.getFormattedMessage());
+              return NO_BUFFER;
+            }
+            
             sample->set_frequency( freq );
             sample->set_format( format );
             sample->set_size( size );
index 8958af9108375c2ff68104eee4353b6719baa034..616d9514f59d976da1c4e4a7f32437929a8e8b3c 100644 (file)
@@ -32,7 +32,8 @@
 #include <simgear/debug/logstream.hxx>
 #include <simgear/props/condition.hxx>
 #include <simgear/math/SGMath.hxx>
-
+#include <simgear/structure/exception.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include "xmlsound.hxx"
 
@@ -272,6 +273,10 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node,
       _sgrp = sgrp;
    }
    _sample = new SGSoundSample( path.c_str(), node->getStringValue("path", ""));
+   if (!_sample->file_path().exists()) {
+      throw sg_io_exception("XML sound: couldn't find file: " + _sample->file_path().str());
+   }
+   
    _sample->set_relative_position( offset_pos );
    _sample->set_direction( dir );
    _sample->set_audio_cone( inner, outer, outer_gain );