]> Dogcows Code - chaz/yoink/commitdiff
bugfix: resource file searching was broken
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 3 Mar 2010 21:44:13 +0000 (14:44 -0700)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 3 Mar 2010 21:44:13 +0000 (14:44 -0700)
19 files changed:
README
src/Animation.cc
src/Animation.hh
src/GameLayer.cc
src/Main.cc
src/Main.hh
src/Moof/Error.hh
src/Moof/Image.cc
src/Moof/Image.hh
src/Moof/ModalDialog.hh
src/Moof/Resource.cc
src/Moof/Resource.hh
src/Moof/Sound.cc
src/Moof/Sound.hh
src/Moof/Texture.cc
src/Moof/Texture.hh
src/Scene.cc
src/Scene.hh
src/TitleLayer.cc

diff --git a/README b/README
index e86d9406e741a93d9b2697da41894a54dce54164..7994e6958c1f8c67f0c3aace1368d2c8e0118d3a 100644 (file)
--- a/README
+++ b/README
@@ -123,5 +123,3 @@ I haven't tried building with cygwin or mingw32 on an actual Windows
 machine, and I certainly haven't tried to do it with Visual Studio.  You're
 on your own if you go that route.
 
-
-
index 2a3d03a5922e62a6ef68e4cf106dd3a0e8c1c99b..6df85562b7252505229c22838ae9beda7fbdbbda 100644 (file)
@@ -12,6 +12,7 @@
 #include <map>
 #include <vector>
 
+#include <Moof/Error.hh>
 #include <Moof/Manager.hh>
 #include <Moof/Log.hh>
 #include <Moof/Script.hh>
@@ -151,13 +152,18 @@ public:
                void init(const std::string& name)
                {
                        Mf::Script script;
-                       std::string filePath = Animation::getPath(name);
+                       std::string path(name);
+                       
+                       if (!Animation::getPath(path))
+                       {
+                               Mf::Error(Mf::Error::RESOURCE_NOT_FOUND).raise();
+                       }
 
                        script.importBaseLibrary();
                        importLogFunctions(script);
                        importAnimationBindings(script);
 
-                       if (script.doFile(filePath) != Mf::Script::SUCCESS)
+                       if (script.doFile(path) != Mf::Script::SUCCESS)
                        {
                                std::string str;
                                script[-1].get(str);
@@ -322,8 +328,8 @@ unsigned Animation::getFrame() const
  * the "animations" subdirectory of any of the search directories.
  */
 
-std::string Animation::getPath(const std::string& name)
+bool Animation::getPath(std::string& name)
 {
-       return Mf::Resource::getPath("animations/" + name + ".lua");
+       return Mf::Resource::getPath(name, "animations/", "lua");
 }
 
index ac00679809b7da11931aef0d29e393fe1b59417e..d8651dddf76a2032c0912584a60722d583202c79 100644 (file)
@@ -56,7 +56,7 @@ public:
        void update(Mf::Scalar t, Mf::Scalar dt);
        unsigned getFrame() const;
 
-       static std::string getPath(const std::string& name);
+       static bool getPath(std::string& name);
 };
 
 
index 0be8eb744f9fe1720acbb7ac2d9753bbf431987f..8f2f4ec006d953ae70bfe5083b38385d0eb4abc5 100644 (file)
@@ -28,13 +28,13 @@ void GameLayer::loadSceneLoader()
        mState.script.importStandardLibraries();
        importLogFunctions(mState.script);
 
-       std::string loaderPath = Scene::getPath("loader");
-       if (loaderPath == "")
+       std::string path("loader");
+       if (!Scene::getPath(path))
        {
                throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader");
        }
 
-       Mf::Script::Result status = mState.script.doFile(loaderPath);
+       Mf::Script::Result status = mState.script.doFile(path);
        if (status != Mf::Script::SUCCESS)
        {
                std::string str;
@@ -284,6 +284,8 @@ void GameLayer::setProjection()
 
 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
 {
-       mState.camera.setProjection(cml::rad(45.0), width / height, 1.0, 200.0);
+       mState.camera.setProjection(cml::rad(45.0),
+                                                               width / height,
+                                                               SCALAR(1.0), SCALAR(200.0));
 }
 
index 7b31ae4f83283ccbf65a91fe32e9e999f65ea626..fc84ceb38fcc3171743faec7eeca3a6c750f7f78 100644 (file)
@@ -128,7 +128,8 @@ std::string Main::getConfigPath()
        // 3. $HOME/.yoinkrc
        // 4. YOINKRC (environment)
 
-       std::string path = Mf::Resource::getPath("yoinkrc");
+       std::string path("yoinkrc");
+       Mf::Resource::getPath(path);
 
 #if !defined(_WIN32)
        path += ":/etc/yoinkrc";
index a5ce29336493c95c0115152ef2b0119e3b7116ab..b6a6684aa46ffa4db559b1aaeefe96a427ccefa8 100644 (file)
 #include <Moof/View.hh>
 
 
-namespace Mf {
-class Settings;
-class View;
+namespace Mf
+{
+       class Settings;
+       class View;
 }
 
+
 class Main;
 typedef boost::shared_ptr<Main> MainP;
 
index 78c894f3178f4583fde3ccb47583be9c08070573..494dd60f8cf925c65f38ed8762e1218d31632980 100644 (file)
@@ -30,8 +30,8 @@ public:
                NONE = 0,                                       // -
                ALC_INIT,                                       // description
                FASTEVENTS_INIT,                        // description
-               OPENAL_INIT,                            // description
                FILE_NOT_FOUND,                         // path of missing file
+               OPENAL_INIT,                            // description
                RESOURCE_NOT_FOUND,                     // name of missing resource
                SCRIPT_ERROR,                           // description
                SDL_INIT,                                       // description
@@ -46,10 +46,10 @@ public:
        }
        virtual ~Error() throw() {}
 
-       void init(unsigned code, const std::string& what = "")
+       void init(unsigned code = NONE, const std::string& what = "")
        {
-               mWhat[sizeof(mWhat)-1] = '\0';
                strncpy(mWhat, what.c_str(), sizeof(mWhat)-1);
+               mWhat[sizeof(mWhat)-1] = '\0';
                mCode = code;
        }
 
index 54e9972d6264be65021286e3e2068340794527f7..2237c88bf1bb059b208907c41e48ca3bb590d12e 100644 (file)
@@ -70,13 +70,13 @@ public:
        }
 
 
-       bool init(const std::string& name, bool flipped = false)
+       void init(const std::string& name, bool flipped = false)
        {
-               std::string path = Image::getPath(name);
+               std::string path(name);
 
                logInfo << "opening image file " << path << std::endl;
-               FILE* fp = fopen(path.c_str(), "rb");
-               if (!fp) return false;
+               FILE* fp = Image::openFile(path);
+               if (!fp) return;
 
                png_byte        signature[8];
                size_t          bytesRead;
@@ -217,8 +217,6 @@ public:
                                                                pngInfo    ? &pngInfo    : 0,
                                                                pngInfoEnd ? &pngInfoEnd : 0);
                fclose(fp);
-
-               return mContext;
        }
 
 
@@ -300,19 +298,14 @@ void Image::setAsIcon() const
 }
 
 
-std::string Image::getPath(const std::string& name)
+bool Image::getPath(std::string& name)
 {
-       if (boost::find_last(name, ".png"))
-       {
-               return Resource::getPath(name);
-       }
-       else
-       {
-               std::string path("images/");
-               path += name;
-               path += ".png";
-               return Resource::getPath(path);
-       }
+       return Resource::getPath(name, "images/", "png");
+}
+
+FILE* Image::openFile(std::string& name)
+{
+       return Resource::openFile(name, "images/", "png");
 }
 
 
index d923b0c7c2f2780ca63b0906ee6a77d72402e68c..6c36cddcd827a45b6e945a037269e8f742db629a 100644 (file)
@@ -53,10 +53,12 @@ public:
 
        void setAsIcon() const;
 
-       static std::string getPath(const std::string& name);
+       static bool getPath(std::string& name);
 
 private:
 
+       static FILE* openFile(std::string& name);
+
        class Impl;
        boost::shared_ptr<Impl> mImpl;
 };
index d56d8ea08f7dc217611bf17453574116975d9110..1e96dd455be197b86a7e7599e0331291a58670ec 100644 (file)
@@ -132,10 +132,13 @@ struct ModalDialog
                                                                                                 "%s", text2.c_str());
                gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
 
-               std::string iconPath = Resource::getPath(PACKAGE".png");
-               GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(),
-                                                                                                                NULL);
-               gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
+               std::string iconPath(PACKAGE".png");
+               if (Resource::getPath(iconPath))
+               {
+                       GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(),
+                                                                                                                        NULL);
+                       gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
+               }
 
                gtk_dialog_run(GTK_DIALOG(dialog));
                gtk_widget_destroy(dialog);
@@ -169,9 +172,12 @@ struct ModalDialog
                dialog.setInformativeText(text2.c_str());
                dialog.setStandardButtons(QMessageBox::Close);
 
-               std::string iconPath = Resource::getPath(PACKAGE".png");
-               QIcon icon(iconPath.c_str());
-               dialog.setWindowIcon(icon);
+               std::string iconPath(PACKAGE".png");
+               if (Resource::getPath(iconPath))
+               {
+                       QIcon icon(iconPath.c_str());
+                       dialog.setWindowIcon(icon);
+               }
 
                dialog.exec();
 
index b04acd78f0c9d7e00424577602926fbf39151d3c..d1dc5fac425d1f3f808eb66357b44706a5d4f3a0 100644 (file)
@@ -9,8 +9,6 @@
 *
 **************************************************************************/
 
-#include <unistd.h>
-
 #include <boost/algorithm/string.hpp>
 
 #include "Log.hh"
@@ -24,62 +22,114 @@ namespace Mf {
 std::vector<std::string> Resource::gSearchPaths;
 
 
-void Resource::addSearchPaths(const std::string& path)
+void Resource::addSearchPaths(const std::string& paths)
 {
-       std::vector<std::string> paths;
-       boost::split(paths, path, boost::is_any_of(":"));
+       std::vector<std::string> pathList;
+       boost::split(pathList, paths, boost::is_any_of(":"));
 
-       addSearchPaths(paths);
+       addSearchPaths(pathList);
 }
 
-void Resource::addSearchPaths(const std::vector<std::string>& path)
+void Resource::addSearchPaths(const std::vector<std::string>& pathList)
 {
        std::vector<std::string>::const_iterator it;
-
-       for (it = path.begin(); it != path.end(); ++it)
+       for (it = pathList.begin(); it != pathList.end(); ++it)
        {
-               std::string onePath(*it);
+               std::string path(*it);
 
-               ASSERT(!onePath.empty() && "empty search path string");
+               ASSERT(!path.empty() && "empty search path string");
 
                // add a slash if there isn't one already
-               if (*onePath.rbegin() != '/')
-               {
-                       onePath += '/';
-               }
+               if (*path.rbegin() != '/') path += '/';
 
 #if defined(_WIN32)
-               boost::replace_all(onePath, "/", "\\");
+               boost::replace_all(path, "/", "\\");
 #endif
 
-               gSearchPaths.push_back(onePath);
-               logInfo << "added search path " << onePath << std::endl;
+               gSearchPaths.push_back(path);
+               logInfo << "added search path " << path << std::endl;
        }
 }
 
 
-std::string Resource::getPath(const std::string& name)
+std::string Resource::getPath(const std::string& path,
+                                                         const std::string& prefix,
+                                                         const std::string& extension)
 {
-       std::vector<std::string>::iterator it;
+       std::string realPath(path);
+       if (getPath(realPath, prefix, extension)) return realPath;
+
+       return std::string();
+}
 
-       std::string path(name);
+bool Resource::getPath(std::string& path,
+                                          const std::string& prefix,
+                                          const std::string& extension)
+{
+       FILE* file = openFile(path, prefix, extension);
+       if (file)
+       {
+               fclose(file);
+               return true;
+       }
 
+       return false;
+}
+
+FILE* Resource::openFile(std::string& path,
+                                                std::string prefix,
+                                                const std::string& extension,
+                                                const std::string& mode)
+{
 #if defined(_WIN32)
+       // Windows always has to be the odd one.
        boost::replace_all(path, "/", "\\");
+       boost::replace_all(prefix, "/", "\\");
 #endif
 
+       std::vector<std::string> preList;
+       boost::split(preList, prefix, boost::is_any_of(":"));
+       std::vector<std::string> postList;
+       boost::split(postList, extension, boost::is_any_of(":"));
+
+       std::vector<std::string>::iterator it;
        for (it = gSearchPaths.begin(); it != gSearchPaths.end(); ++it)
        {
-               std::string fullPath(*it);
-               fullPath += path;
+               std::vector<std::string>::iterator jt;
+               for (jt = preList.begin(); jt != preList.end(); ++jt)
+               {
+                       std::vector<std::string>::iterator kt;
+                       for (kt = postList.begin(); kt != postList.end(); ++kt)
+                       {
+                               std::string realPath(*it);
+                               realPath += *jt;
+                               realPath += path;
+                               realPath += ".";
+                               realPath += *kt;
+
+                               FILE* file = fopen(realPath.c_str(), mode.c_str());
+                               if (file)
+                               {
+                                       path = realPath;
+                                       return file;
+                               }
+                       }
+               }
 
-               if (access(fullPath.c_str(), R_OK) == 0) return fullPath;
-       }
+               // check path relative to search path
+               std::string realPath(*it);
+               realPath += path;
 
-       logWarning << "cannot find resource " << name << std::endl;
+               FILE* file = fopen(realPath.c_str(), mode.c_str());
+               if (file)
+               {
+                       path = realPath;
+                       return file;
+               }
+       }
 
-       // empty string
-       return std::string();
+       // last ditch effort; maybe it's already a path to a valid resource
+       return fopen(path.c_str(), mode.c_str());
 }
 
 
index 8446d0896b715cd626a2444e97172903a6c00fa6..db6fe138e37f748d1d6747f667f8b93876c871dd 100644 (file)
@@ -17,6 +17,7 @@
  * Interface for textures, sounds, and other types of resources.
  */
 
+#include <cstdio>
 #include <string>
 #include <vector>
 
@@ -40,8 +41,8 @@ public:
         * @param directory Path to a directory.
         */
 
-       static void addSearchPaths(const std::string& path);
-       static void addSearchPaths(const std::vector<std::string>& path);
+       static void addSearchPaths(const std::string& paths);
+       static void addSearchPaths(const std::vector<std::string>& pathList);
 
        /**
         * Get the path to a resource of a given name.
@@ -50,7 +51,18 @@ public:
         * @return The first path found which resolves to a file.
         */
 
-       static std::string getPath(const std::string& name);
+       static std::string getPath(const std::string& path,
+                                                          const std::string& prefix = "",
+                                                          const std::string& extension = "");
+
+       static bool getPath(std::string& path,
+                                               const std::string& prefix = "",
+                                               const std::string& extension = "");
+
+       static FILE* openFile(std::string& path,
+                                                 std::string prefix = "",
+                                                 const std::string& extension = "",
+                                                 const std::string& mode = "rb");
 
 private:
 
index c78b35c613b972d919b51de87db0c4cbc215d503..ffa270e9e9876768669cd7ca3bdf850d46b5b09c 100644 (file)
@@ -75,12 +75,14 @@ public:
                                mOggStream.datasource = 0;
                        }
 
-                       std::string path = Sound::getPath(name);
-                       int result = ov_fopen((char*)path.c_str(), &mOggStream);
+                       std::string path(name);
+                       if (!Sound::getPath(path))
+                       {
+                               Error(Error::RESOURCE_NOT_FOUND, path).raise();
+                       }
 
-                       if (result < 0)
+                       if (ov_fopen((char*)path.c_str(), &mOggStream) < 0)
                        {
-                               logWarning << "couldn't load sound: " << path << std::endl;
                                Error(Error::UNKNOWN_AUDIO_FORMAT, path).raise();
                        }
 
@@ -572,19 +574,9 @@ void Sound::setListenerOrientation(const Vector3& forward,
 }
 
 
-std::string Sound::getPath(const std::string& name)
+bool Sound::getPath(std::string& name)
 {
-       if (boost::find_last(name, ".ogg"))
-       {
-               return Resource::getPath(name);
-       }
-       else
-       {
-               std::string path("sounds/");
-               path += name;
-               path += ".ogg";
-               return Resource::getPath(path);
-       }
+       return Resource::getPath(name, "sounds/", "ogg");
 }
 
 
index d3cfa895f4743e747a89fa7f984d8f80c6c6a562..bbfef127434d19b3884399f6b63a43176bec497e 100644 (file)
@@ -70,7 +70,7 @@ public:
        static void setListenerOrientation(const Vector3& forward,
                                                                           const Vector3& up);
 
-       static std::string getPath(const std::string& name);
+       static bool getPath(std::string& name);
 
 protected:
 
index 7d82e2d093416cf5190475a803de81873405d662..3ca067af4ebe72dcc6552b92b3aa2c1aab09f07a 100644 (file)
@@ -215,7 +215,9 @@ public:
 
        void init(const std::string& name)
        {
-               std::string path = Texture::getPath(name);
+               std::string path(name);
+               
+               Texture::getPath(path);
 
                mImage = Image::alloc(path);
                if (!mImage->isValid())
@@ -395,8 +397,8 @@ public:
 GLuint Texture::Impl::gObject = 0;
 
 
-Texture::Texture(const std::string& name) :
-       Image(Texture::getPath(name)),
+Texture::Texture(const std::string& name) : // TODO hmm..
+       Image(name),
        // pass through
        mImpl(Texture::Impl::getInstance(name)) {}
 
@@ -490,19 +492,9 @@ bool Texture::getTileCoords(TileIndex index, Scalar coords[8],
 }
 
 
-std::string Texture::getPath(const std::string& name)
+bool Texture::getPath(std::string& name)
 {
-       if (boost::find_last(name, ".png"))
-       {
-               return Resource::getPath(name);
-       }
-       else
-       {
-               std::string path("textures/");
-               path += name;
-               path += ".png";
-               return Resource::getPath(path);
-       }
+       return Resource::getPath(name, "textures/", "png");
 }
 
 
index 27ec414369c8bdb2493c30f25c1563fbaa4479db..f3525500a39e349eaef2edf52f0551ea3435eb61 100644 (file)
@@ -100,7 +100,7 @@ public:
                                           Orientation what) const;
 
 
-       static std::string getPath(const std::string& name);
+       static bool getPath(std::string& name);
 
 private:
 
index bfc788af1a75f46ed0f5adfa19ac963be5420c74..afbfa440f9f915f949feda5e459b7c4fa1d80c2e 100644 (file)
@@ -195,15 +195,15 @@ struct Scene::Impl : public Mf::Manager<Impl>
 
        Mf::Script::Result load(Mf::Settings& settings, Mf::Script& script)
        {
-               std::string filePath = Scene::getPath(getName());
-               if (filePath == "")
+               std::string path(getName());
+               if (!Scene::getPath(path))
                {
                        script.push("the scene file could not be found");
                        return Mf::Script::FILE_ERROR;
                }
 
                importSceneBindings(settings, script);
-               return script.doFile(filePath);
+               return script.doFile(path);
        }
 
 
@@ -609,8 +609,8 @@ bool Scene::checkForCollision(Character& character)
 }
 
 
-std::string Scene::getPath(const std::string& name)
+bool Scene::getPath(std::string& name)
 {
-       return Mf::Resource::getPath("scenes/" + name + ".lua");
+       return Mf::Resource::getPath(name, "scenes/", "lua");
 }
 
index 31a4ecd919f8cba737c8849fea1cbf9d22e5926a..d92784a2a0e2e7babc76f0054d893af25ac8ab50 100644 (file)
@@ -58,7 +58,7 @@ public:
                        std::list<Mf::Ray<2>::Contact>& hits) const;
        bool checkForCollision(Character& character);
 
-       static std::string getPath(const std::string& name);
+       static bool getPath(std::string& name);
 };
 
 
index b865be05eff68ec0230c3c42f2ef7a3e33829865..9e33c361f532b0345fc52910375e0be3dfbde477 100644 (file)
@@ -45,7 +45,7 @@ bool TitleLayer::handleEvent(const Mf::Event& event)
                case SDL_KEYUP:
                        if (event.key.keysym.sym == SDLK_ESCAPE)
                        {
-                               parent().removeChild(this);
+                               parent().stop();
                                return true;
                        }
 
This page took 0.049669 seconds and 4 git commands to generate.