Skip to content

Commit

Permalink
Merge branch 'vfs_normalized_path_19' into 'master'
Browse files Browse the repository at this point in the history
Use normalized path for correctActorModelPath (#8138)

See merge request OpenMW/openmw!4443
  • Loading branch information
psi29a committed Nov 4, 2024
2 parents 6f9e84b + afa7694 commit 20f77ec
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 48 deletions.
2 changes: 1 addition & 1 deletion apps/navmeshtool/worldspacedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace NavMeshTool

for (CellRef& cellRef : cellRefs)
{
std::string model(getModel(esmData, cellRef.mRefId, cellRef.mType));
VFS::Path::Normalized model(getModel(esmData, cellRef.mRefId, cellRef.mType));
if (model.empty())
continue;

Expand Down
2 changes: 1 addition & 1 deletion apps/opencs/view/render/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace CSVRender
mBaseNode->removeChildren(0, mBaseNode->getNumChildren());

// Load skeleton
std::string skeletonModel = mActorData->getSkeleton();
VFS::Path::Normalized skeletonModel = mActorData->getSkeleton();
skeletonModel
= Misc::ResourceHelpers::correctActorModelPath(skeletonModel, mData.getResourceSystem()->getVFS());
loadSkeleton(skeletonModel);
Expand Down
5 changes: 2 additions & 3 deletions apps/openmw/mwphysics/physicssystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ namespace MWPhysics
return;

const VFS::Path::Normalized animationMesh = ptr.getClass().useAnim()
? VFS::Path::toNormalized(
Misc::ResourceHelpers::correctActorModelPath(mesh.value(), mResourceSystem->getVFS()))
? Misc::ResourceHelpers::correctActorModelPath(mesh, mResourceSystem->getVFS())
: VFS::Path::Normalized(mesh);
osg::ref_ptr<Resource::BulletShapeInstance> shapeInstance = mShapeManager->getInstance(animationMesh);
if (!shapeInstance || !shapeInstance->mCollisionShape)
Expand Down Expand Up @@ -564,7 +563,7 @@ namespace MWPhysics
void PhysicsSystem::addActor(const MWWorld::Ptr& ptr, VFS::Path::NormalizedView mesh)
{
const VFS::Path::Normalized animationMesh
= Misc::ResourceHelpers::correctActorModelPath(mesh.value(), mResourceSystem->getVFS());
= Misc::ResourceHelpers::correctActorModelPath(mesh, mResourceSystem->getVFS());
osg::ref_ptr<const Resource::BulletShape> shape = mShapeManager->getShape(animationMesh);

// Try to get shape from basic model as fallback for creatures
Expand Down
3 changes: 2 additions & 1 deletion apps/openmw/mwrender/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,8 @@ namespace MWRender
const bool werewolf = false;

defaultSkeleton = Misc::ResourceHelpers::correctActorModelPath(
getActorSkeleton(firstPerson, isFemale, isBeast, werewolf), mResourceSystem->getVFS());
VFS::Path::toNormalized(getActorSkeleton(firstPerson, isFemale, isBeast, werewolf)),
mResourceSystem->getVFS());
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/openmw/mwrender/npcanimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,14 @@ namespace MWRender
}

const std::string defaultSkeleton = Misc::ResourceHelpers::correctActorModelPath(
getActorSkeleton(is1stPerson, isFemale, isBeast, isWerewolf), mResourceSystem->getVFS());
VFS::Path::toNormalized(getActorSkeleton(is1stPerson, isFemale, isBeast, isWerewolf)),
mResourceSystem->getVFS());

std::string smodel = defaultSkeleton;
bool isCustomModel = false;
if (!is1stPerson && !isWerewolf && !mNpc->mModel.empty())
{
std::string model = Misc::ResourceHelpers::correctMeshPath(mNpc->mModel);
VFS::Path::Normalized model = Misc::ResourceHelpers::correctMeshPath(mNpc->mModel);
isCustomModel = !isDefaultActorSkeleton(model);
smodel = Misc::ResourceHelpers::correctActorModelPath(model, mResourceSystem->getVFS());
}
Expand Down
6 changes: 4 additions & 2 deletions apps/openmw/mwrender/objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ namespace MWRender
std::string animationMesh = mesh;
if (animated && !mesh.empty())
{
animationMesh = Misc::ResourceHelpers::correctActorModelPath(mesh, mResourceSystem->getVFS());
animationMesh = Misc::ResourceHelpers::correctActorModelPath(
VFS::Path::toNormalized(mesh), mResourceSystem->getVFS());
if (animationMesh == mesh && Misc::StringUtils::ciEndsWith(animationMesh, ".nif"))
animated = false;
}
Expand All @@ -96,7 +97,8 @@ namespace MWRender
ptr.getRefData().getBaseNode()->setNodeMask(Mask_Actor);

bool animated = true;
std::string animationMesh = Misc::ResourceHelpers::correctActorModelPath(mesh, mResourceSystem->getVFS());
std::string animationMesh
= Misc::ResourceHelpers::correctActorModelPath(VFS::Path::toNormalized(mesh), mResourceSystem->getVFS());
if (animationMesh == mesh && Misc::StringUtils::ciEndsWith(animationMesh, ".nif"))
animated = false;

Expand Down
4 changes: 2 additions & 2 deletions apps/openmw/mwscript/miscextensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1437,8 +1437,8 @@ namespace MWScript
osg::Vec3f pos(ptr.getRefData().getPosition().asVec3());
msg << "Coordinates: " << pos.x() << " " << pos.y() << " " << pos.z() << std::endl;
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS();
const VFS::Path::Normalized model(
::Misc::ResourceHelpers::correctActorModelPath(ptr.getClass().getCorrectedModel(ptr), vfs));
const VFS::Path::Normalized model = ::Misc::ResourceHelpers::correctActorModelPath(
VFS::Path::toNormalized(ptr.getClass().getCorrectedModel(ptr)), vfs);
msg << "Model: " << model.value() << std::endl;
if (!model.empty())
{
Expand Down
5 changes: 3 additions & 2 deletions apps/openmw/mwworld/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,9 @@ namespace MWWorld
void Scene::preload(const std::string& mesh, bool useAnim)
{
const VFS::Path::Normalized meshPath = useAnim
? Misc::ResourceHelpers::correctActorModelPath(mesh, mRendering.getResourceSystem()->getVFS())
: mesh;
? Misc::ResourceHelpers::correctActorModelPath(
VFS::Path::toNormalized(mesh), mRendering.getResourceSystem()->getVFS())
: VFS::Path::toNormalized(mesh);

if (mRendering.getResourceSystem()->getSceneManager()->checkLoaded(meshPath, mRendering.getReferenceTime()))
return;
Expand Down
55 changes: 22 additions & 33 deletions components/misc/resourcehelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,16 @@

namespace
{

struct MatchPathSeparator
{
bool operator()(char ch) const { return ch == '\\' || ch == '/'; }
};

std::string getBasename(std::string const& pathname)
{
return std::string(
std::find_if(pathname.rbegin(), pathname.rend(), MatchPathSeparator()).base(), pathname.end());
}

}

bool changeExtension(std::string& path, std::string_view ext)
{
std::string::size_type pos = path.rfind('.');
if (pos != std::string::npos && path.compare(pos, path.length() - pos, ext) != 0)
bool changeExtension(std::string& path, std::string_view ext)
{
path.replace(pos, path.length(), ext);
return true;
std::string::size_type pos = path.rfind('.');
if (pos != std::string::npos && path.compare(pos, path.length() - pos, ext) != 0)
{
path.replace(pos, path.length(), ext);
return true;
}
return false;
}
return false;
}

bool Misc::ResourceHelpers::changeExtensionToDds(std::string& path)
Expand Down Expand Up @@ -106,15 +93,16 @@ std::string Misc::ResourceHelpers::correctResourcePath(
// fall back to a resource in the top level directory if it exists
std::string fallback{ topLevelDirectories.front() };
fallback += '\\';
fallback += getBasename(correctedPath);
fallback += Misc::getFileName(correctedPath);

if (vfs->exists(fallback))
return fallback;

if (changedToDds)
{
fallback = topLevelDirectories.front();
fallback += '\\';
fallback += getBasename(origExt);
fallback += Misc::getFileName(origExt);
if (vfs->exists(fallback))
return fallback;
}
Expand Down Expand Up @@ -154,22 +142,23 @@ std::string Misc::ResourceHelpers::correctBookartPath(
return image;
}

std::string Misc::ResourceHelpers::correctActorModelPath(std::string_view resPath, const VFS::Manager* vfs)
VFS::Path::Normalized Misc::ResourceHelpers::correctActorModelPath(
VFS::Path::NormalizedView resPath, const VFS::Manager* vfs)
{
std::string mdlname(resPath);
std::string::size_type p = mdlname.find_last_of("/\\");
std::string mdlname(resPath.value());
std::string::size_type p = mdlname.find_last_of('/');
if (p != std::string::npos)
mdlname.insert(mdlname.begin() + p + 1, 'x');
mdlname.insert(mdlname.begin() + static_cast<std::string::difference_type>(p) + 1, 'x');
else
mdlname.insert(mdlname.begin(), 'x');
std::string kfname = mdlname;
if (Misc::StringUtils::ciEndsWith(kfname, ".nif"))
kfname.replace(kfname.size() - 4, 4, ".kf");

VFS::Path::Normalized kfname(mdlname);
if (Misc::getFileExtension(mdlname) == "nif")
kfname.changeExtension("kf");

if (!vfs->exists(kfname))
{
return std::string(resPath);
}
return VFS::Path::Normalized(resPath);

return mdlname;
}

Expand Down
2 changes: 1 addition & 1 deletion components/misc/resourcehelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Misc
std::string correctBookartPath(std::string_view resPath, int width, int height, const VFS::Manager* vfs);
/// Use "xfoo.nif" instead of "foo.nif" if "xfoo.kf" is available
/// Note that if "xfoo.nif" is actually unavailable, we can't fall back to "foo.nif". :(
std::string correctActorModelPath(std::string_view resPath, const VFS::Manager* vfs);
VFS::Path::Normalized correctActorModelPath(VFS::Path::NormalizedView resPath, const VFS::Manager* vfs);
std::string correctMaterialPath(std::string_view resPath, const VFS::Manager* vfs);

// Adds "meshes\\".
Expand Down

0 comments on commit 20f77ec

Please sign in to comment.