Rename and move functions for texture path correction

deque
MiroslavR 11 years ago
parent 4873c4bd31
commit 4d403ed747

@ -3,7 +3,7 @@
#include <MyGUI_FactoryManager.h>
#include <MyGUI_ImageBox.h>
#include <components/nifogre/material.hpp>
#include <components/misc/resourcehelpers.hpp>
#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)));
}

@ -46,7 +46,7 @@ add_component_dir (esm
)
add_component_dir (misc
utf8stream stringops
utf8stream stringops resourcehelpers
)
add_component_dir (files

@ -0,0 +1,59 @@
#include "resourcehelpers.hpp"
#include <components/misc/stringops.hpp>
#include <OgreMaterialManager.h>
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);
}

@ -0,0 +1,16 @@
#ifndef MISC_RESOURCEHELPERS_H
#define MISC_RESOURCEHELPERS_H
#include <string>
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

@ -1,7 +1,7 @@
#include "material.hpp"
#include <components/nif/node.hpp>
#include <components/misc/stringops.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/settings/settings.hpp>
#include <components/nifoverrides/nifoverrides.hpp>
@ -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.");
}

@ -38,10 +38,6 @@ class NIFMaterialLoader {
static std::map<size_t,std::string> 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,

@ -46,6 +46,7 @@
#include <components/nif/node.hpp>
#include <components/misc/stringops.hpp>
#include <components/misc/resourcehelpers.hpp>
#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));
}
}

Loading…
Cancel
Save