From ad6175c78a809f0acd5d547a1444bfebd99cb80b Mon Sep 17 00:00:00 2001 From: Michael Papageorgiou Date: Thu, 8 Mar 2012 23:06:52 +0200 Subject: [PATCH] SoundManager: Set up the priority for file look up right and take care of a corner case --- apps/openmw/mwsound/soundmanager.cpp | 16 +++++---------- components/files/filelibrary.cpp | 29 +++++++++++++++++----------- components/files/filelibrary.hpp | 1 + 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 70118ea54..5f48081f2 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -71,9 +71,6 @@ namespace MWSound { if(useSound) { - // Temporary list of all sound directories - Files::PathContainer soundDirs; - // The music library will accept these filetypes // If none is given then it will accept all filetypes std::vector acceptableExtensions; @@ -82,16 +79,14 @@ namespace MWSound acceptableExtensions.push_back(".ogg"); acceptableExtensions.push_back(".flac"); - for (Files::PathContainer::const_iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) + // Makes a list of all sound files, searches in reverse for priority reasons + for (Files::PathContainer::const_reverse_iterator it = dataDirs.rbegin(); it != dataDirs.rend(); ++it) { - soundDirs.push_back( *it / std::string("Sound")); - } - for (Files::PathContainer::const_iterator it = soundDirs.begin(); it != soundDirs.end(); ++it) - { - Files::FileLister(*it, mSoundFiles, true); + Files::FileLister(*it / std::string("Sound"), mSoundFiles, true); } - for (Files::PathContainer::const_iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) + // Makes a FileLibrary of all music files, searches in reverse for priority reasons + for (Files::PathContainer::const_reverse_iterator it = dataDirs.rbegin(); it != dataDirs.rend(); ++it) { mMusicLibrary.add(*it / std::string("Music"), true, mFSStrict, acceptableExtensions); } @@ -292,7 +287,6 @@ namespace MWSound void SoundManager::streamMusic(const std::string& filename) { - std::cout << filename << std::endl; std::string filePath = mMusicLibrary.locate(filename, mFSStrict).string(); if(!filePath.empty()) { diff --git a/components/files/filelibrary.cpp b/components/files/filelibrary.cpp index b896379d5..baefd79eb 100644 --- a/components/files/filelibrary.cpp +++ b/components/files/filelibrary.cpp @@ -2,6 +2,7 @@ #include +#include #include namespace Files @@ -22,13 +23,24 @@ namespace Files void FileLibrary::add(const boost::filesystem::path &root, bool recursive, bool strict, const StringVector &acceptableExtensions) { - PathContainer list; + if (!boost::filesystem::exists(root)) + { + std::cout << "Warning " << root.string() << " does not exist.\n"; + return; + } + std::string fileExtension; std::string type; - FileLister(root, list, recursive); - for (PathContainer::iterator listIter = list.begin(); - listIter != list.end(); ++listIter) + // remember the last location of the priority list when listing new items + int length = mPriorityList.size(); + + // First makes a list of all candidate files + FileLister(root, mPriorityList, recursive); + + // Then sort these files into sections according to the folder they belong to + for (PathContainer::iterator listIter = mPriorityList.begin() + length; + listIter != mPriorityList.end(); ++listIter) { if( !acceptableExtensions.empty() ) { @@ -43,7 +55,7 @@ namespace Files boost::algorithm::to_lower(type); mMap[type].push_back(*listIter); - //std::cout << "Added path: " << listIter->string() << " in section "<< type <string() << " in section "<< type <second, boost::filesystem::path(item), strict); - if (result != boost::filesystem::path("")) - return result; - } + return FileListLocator(mPriorityList, boost::filesystem::path(item), strict); } else { diff --git a/components/files/filelibrary.hpp b/components/files/filelibrary.hpp index 9013dbb37..55978084f 100644 --- a/components/files/filelibrary.hpp +++ b/components/files/filelibrary.hpp @@ -17,6 +17,7 @@ namespace Files private: StringPathContMap mMap; PathContainer mEmptyPath; + PathContainer mPriorityList; public: /// Searches a path and adds the results to the library