diff --git a/apps/openmw/mwgui/itemwidget.cpp b/apps/openmw/mwgui/itemwidget.cpp index fd4a748306..3e109384b6 100644 --- a/apps/openmw/mwgui/itemwidget.cpp +++ b/apps/openmw/mwgui/itemwidget.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include "../mwworld/class.hpp" @@ -65,7 +65,7 @@ namespace MWGui void ItemWidget::setIcon(const MWWorld::Ptr &ptr) { - setIcon(NifOgre::NIFMaterialLoader::findIconName(ptr.getClass().getInventoryIcon(ptr))); + setIcon(Misc::ResourceHelpers::correctIconPath(ptr.getClass().getInventoryIcon(ptr))); } diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index f343ca8ed5..21e062fc02 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -46,7 +46,7 @@ add_component_dir (esm ) add_component_dir (misc - utf8stream stringops + utf8stream stringops resourcehelpers ) add_component_dir (files diff --git a/components/misc/resourcehelpers.cpp b/components/misc/resourcehelpers.cpp new file mode 100644 index 0000000000..e357ac189d --- /dev/null +++ b/components/misc/resourcehelpers.cpp @@ -0,0 +1,59 @@ +#include "resourcehelpers.hpp" + +#include + +#include + +std::string Misc::ResourceHelpers::correctResourcePath(const std::string &topLevelDirectory, const std::string &filename) +{ + /* Bethesda at some point converted all their BSA + * textures from tga to dds for increased load speed, but all + * texture file name references were kept as .tga. + */ + std::string path = topLevelDirectory + '\\'; + std::string path2 = topLevelDirectory + '/'; + + std::string texname = filename; + Misc::StringUtils::toLower(texname); + + // Apparently, leading separators are allowed + while (texname.size() && (texname[0] == '/' || texname[0] == '\\')) + texname.erase(0, 1); + + if(texname.compare(0, path.size()-1, path.data()) != 0 && + texname.compare(0, path2.size()-1, path2.data()) != 0) + texname = path + texname; + + Ogre::String::size_type pos = texname.rfind('.'); + if(pos != Ogre::String::npos && texname.compare(pos, texname.length() - pos, ".dds") != 0) + { + // since we know all (GOTY edition or less) textures end + // in .dds, we change the extension + texname.replace(pos, texname.length(), ".dds"); + + // if it turns out that the above wasn't true in all cases (not for vanilla, but maybe mods) + // verify, and revert if false (this call succeeds quickly, but fails slowly) + if(!Ogre::ResourceGroupManager::getSingleton().resourceExistsInAnyGroup(texname)) + { + texname = filename; + Misc::StringUtils::toLower(texname); + if(texname.compare(0, path.size()-1, path.data()) != 0 && + texname.compare(0, path2.size()-1, path2.data()) != 0) + texname = path + texname; + } + } + + return texname; +} + +std::string Misc::ResourceHelpers::correctTexturePath(const std::string &filename) +{ + static const std::string dir = "textures"; + return correctResourcePath(dir, filename); +} + +std::string Misc::ResourceHelpers::correctIconPath(const std::string &filename) +{ + static const std::string dir = "icons"; + return correctResourcePath(dir, filename); +} diff --git a/components/misc/resourcehelpers.hpp b/components/misc/resourcehelpers.hpp new file mode 100644 index 0000000000..42702fda85 --- /dev/null +++ b/components/misc/resourcehelpers.hpp @@ -0,0 +1,16 @@ +#ifndef MISC_RESOURCEHELPERS_H +#define MISC_RESOURCEHELPERS_H + +#include + +namespace Misc +{ + namespace ResourceHelpers + { + std::string correctResourcePath(const std::string &topLevelDirectory, const std::string &filename); + std::string correctTexturePath(const std::string &filename); + std::string correctIconPath(const std::string &filename); + } +} + +#endif diff --git a/components/nifogre/material.cpp b/components/nifogre/material.cpp index e216fe9848..5ca58da3bd 100644 --- a/components/nifogre/material.cpp +++ b/components/nifogre/material.cpp @@ -1,7 +1,7 @@ #include "material.hpp" #include -#include +#include #include #include @@ -54,61 +54,6 @@ static const char *getTestMode(int mode) return "less_equal"; } - -std::string NIFMaterialLoader::findMaterialName(const std::string &topLevelDirectory, const std::string &filename) -{ - /* Bethesda at some point converted all their BSA - * textures from tga to dds for increased load speed, but all - * texture file name references were kept as .tga. - */ - std::string path = topLevelDirectory + '\\'; - std::string path2 = topLevelDirectory + '/'; - - std::string texname = filename; - Misc::StringUtils::toLower(texname); - - // Apparently, leading separators are allowed - while (texname.size() && (texname[0] == '/' || texname[0] == '\\')) - texname.erase(0, 1); - - if(texname.compare(0, path.size()-1, path.data()) != 0 && - texname.compare(0, path2.size()-1, path2.data()) != 0) - texname = path + texname; - - Ogre::String::size_type pos = texname.rfind('.'); - if(pos != Ogre::String::npos && texname.compare(pos, texname.length() - pos, ".dds") != 0) - { - // since we know all (GOTY edition or less) textures end - // in .dds, we change the extension - texname.replace(pos, texname.length(), ".dds"); - - // if it turns out that the above wasn't true in all cases (not for vanilla, but maybe mods) - // verify, and revert if false (this call succeeds quickly, but fails slowly) - if(!Ogre::ResourceGroupManager::getSingleton().resourceExistsInAnyGroup(texname)) - { - texname = filename; - Misc::StringUtils::toLower(texname); - if(texname.compare(0, path.size()-1, path.data()) != 0 && - texname.compare(0, path2.size()-1, path2.data()) != 0) - texname = path + texname; - } - } - - return texname; -} - -std::string NIFMaterialLoader::findTextureName(const std::string &filename) -{ - static const std::string dir = "textures"; - return findMaterialName(dir, filename); -} - -std::string NIFMaterialLoader::findIconName(const std::string &filename) -{ - static const std::string dir = "icons"; - return findMaterialName(dir, filename); -} - Ogre::String NIFMaterialLoader::getMaterial(const Nif::ShapeData *shapedata, const Ogre::String &name, const Ogre::String &group, const Nif::NiTexturingProperty *texprop, @@ -158,7 +103,7 @@ Ogre::String NIFMaterialLoader::getMaterial(const Nif::ShapeData *shapedata, const Nif::NiSourceTexture *st = texprop->textures[i].texture.getPtr(); if(st->external) - texName[i] = findTextureName(st->filename); + texName[i] = Misc::ResourceHelpers::correctTexturePath(st->filename); else warn("Found internal texture, ignoring."); } diff --git a/components/nifogre/material.hpp b/components/nifogre/material.hpp index df37ce164b..fc978549eb 100644 --- a/components/nifogre/material.hpp +++ b/components/nifogre/material.hpp @@ -38,10 +38,6 @@ class NIFMaterialLoader { static std::map sMaterialMap; public: - static std::string findMaterialName(const std::string &topLevelDirectory, const std::string &filename); - static std::string findTextureName(const std::string &filename); - static std::string findIconName(const std::string &filename); - static Ogre::String getMaterial(const Nif::ShapeData *shapedata, const Ogre::String &name, const Ogre::String &group, const Nif::NiTexturingProperty *texprop, diff --git a/components/nifogre/ogrenifloader.cpp b/components/nifogre/ogrenifloader.cpp index 750fa81677..be362e6ac0 100644 --- a/components/nifogre/ogrenifloader.cpp +++ b/components/nifogre/ogrenifloader.cpp @@ -46,6 +46,7 @@ #include #include +#include #include "skeleton.hpp" #include "material.hpp" @@ -169,7 +170,7 @@ public: const Nif::NiSourceTexture* tex = ctrl->mSources[i].getPtr(); if (!tex->external) std::cerr << "Warning: Found internal texture, ignoring." << std::endl; - mTextures.push_back(NIFMaterialLoader::findTextureName(tex->filename)); + mTextures.push_back(Misc::ResourceHelpers::correctTexturePath(tex->filename)); } }