1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#include "resourcehelpers.hpp"
#include <algorithm>
#include <sstream>
#include <string_view>
#include <components/esm/common.hpp>
#include <components/esm/refid.hpp>
#include <components/misc/pathhelpers.hpp>
#include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/lower.hpp>
#include <components/vfs/manager.hpp>
#include <components/vfs/pathutil.hpp>
namespace
{
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)
{
path.replace(pos, path.length(), ext);
return true;
}
return false;
}
}
bool Misc::ResourceHelpers::changeExtensionToDds(std::string& path)
{
return changeExtension(path, ".dds");
}
// If `ext` is not empty we first search file with extension `ext`, then if not found fallback to original extension.
std::string Misc::ResourceHelpers::correctResourcePath(std::span<const std::string_view> topLevelDirectories,
std::string_view resPath, const VFS::Manager* vfs, std::string_view ext)
{
std::string correctedPath = Misc::StringUtils::lowerCase(resPath);
// Flatten slashes
std::replace(correctedPath.begin(), correctedPath.end(), '/', '\\');
auto bothSeparators = [](char a, char b) { return a == '\\' && b == '\\'; };
correctedPath.erase(std::unique(correctedPath.begin(), correctedPath.end(), bothSeparators), correctedPath.end());
// Remove leading separator
if (!correctedPath.empty() && correctedPath[0] == '\\')
correctedPath.erase(0, 1);
// Handle top level directory
bool needsPrefix = true;
for (std::string_view potentialTopLevelDirectory : topLevelDirectories)
{
if (correctedPath.starts_with(potentialTopLevelDirectory)
&& correctedPath.size() > potentialTopLevelDirectory.size()
&& correctedPath[potentialTopLevelDirectory.size()] == '\\')
{
needsPrefix = false;
break;
}
else
{
std::string topLevelPrefix = std::string{ potentialTopLevelDirectory } + '\\';
size_t topLevelPos = correctedPath.find('\\' + topLevelPrefix);
if (topLevelPos != std::string::npos)
{
correctedPath.erase(0, topLevelPos + 1);
needsPrefix = false;
break;
}
}
}
if (needsPrefix)
correctedPath = std::string{ topLevelDirectories.front() } + '\\' + correctedPath;
std::string origExt = correctedPath;
// replace extension if `ext` is specified (used for .tga -> .dds, .wav -> .mp3)
bool isExtChanged = !ext.empty() && changeExtension(correctedPath, ext);
if (vfs->exists(correctedPath))
return correctedPath;
// fall back to original extension
if (isExtChanged && vfs->exists(origExt))
return origExt;
// fall back to a resource in the top level directory if it exists
std::string fallback{ topLevelDirectories.front() };
fallback += '\\';
fallback += Misc::getFileName(correctedPath);
if (vfs->exists(fallback))
return fallback;
if (isExtChanged)
{
fallback = topLevelDirectories.front();
fallback += '\\';
fallback += Misc::getFileName(origExt);
if (vfs->exists(fallback))
return fallback;
}
return correctedPath;
}
// Note: 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. So we pass ext=".dds" to all helpers
// looking for textures.
std::string Misc::ResourceHelpers::correctTexturePath(std::string_view resPath, const VFS::Manager* vfs)
{
return correctResourcePath({ { "textures", "bookart" } }, resPath, vfs, ".dds");
}
std::string Misc::ResourceHelpers::correctIconPath(std::string_view resPath, const VFS::Manager* vfs)
{
return correctResourcePath({ { "icons" } }, resPath, vfs, ".dds");
}
std::string Misc::ResourceHelpers::correctBookartPath(std::string_view resPath, const VFS::Manager* vfs)
{
return correctResourcePath({ { "bookart", "textures" } }, resPath, vfs, ".dds");
}
std::string Misc::ResourceHelpers::correctBookartPath(
std::string_view resPath, int width, int height, const VFS::Manager* vfs)
{
std::string image = correctBookartPath(resPath, vfs);
// Apparently a bug with some morrowind versions, they reference the image without the size suffix.
// So if the image isn't found, try appending the size.
if (!vfs->exists(image))
{
std::stringstream str;
str << image.substr(0, image.rfind('.')) << "_" << width << "_" << height << image.substr(image.rfind('.'));
image = Misc::ResourceHelpers::correctBookartPath(str.str(), vfs);
}
return image;
}
VFS::Path::Normalized Misc::ResourceHelpers::correctActorModelPath(
VFS::Path::NormalizedView resPath, const VFS::Manager* vfs)
{
std::string mdlname(resPath.value());
std::string::size_type p = mdlname.find_last_of('/');
if (p != std::string::npos)
mdlname.insert(mdlname.begin() + static_cast<std::string::difference_type>(p) + 1, 'x');
else
mdlname.insert(mdlname.begin(), 'x');
VFS::Path::Normalized kfname(mdlname);
if (Misc::getFileExtension(mdlname) == "nif")
kfname.changeExtension("kf");
if (!vfs->exists(kfname))
return VFS::Path::Normalized(resPath);
return mdlname;
}
std::string Misc::ResourceHelpers::correctMaterialPath(std::string_view resPath, const VFS::Manager* vfs)
{
return correctResourcePath({ { "materials" } }, resPath, vfs);
}
VFS::Path::Normalized Misc::ResourceHelpers::correctMeshPath(VFS::Path::NormalizedView resPath)
{
static constexpr VFS::Path::NormalizedView prefix("meshes");
return prefix / resPath;
}
VFS::Path::Normalized Misc::ResourceHelpers::correctSoundPath(VFS::Path::NormalizedView resPath)
{
static constexpr VFS::Path::NormalizedView prefix("sound");
return prefix / resPath;
}
VFS::Path::Normalized Misc::ResourceHelpers::correctMusicPath(VFS::Path::NormalizedView resPath)
{
static constexpr VFS::Path::NormalizedView prefix("music");
return prefix / resPath;
}
std::string_view Misc::ResourceHelpers::meshPathForESM3(std::string_view resPath)
{
constexpr std::string_view prefix = "meshes";
if (resPath.length() < prefix.size() + 1 || !Misc::StringUtils::ciStartsWith(resPath, prefix)
|| (resPath[prefix.size()] != '/' && resPath[prefix.size()] != '\\'))
{
throw std::runtime_error("Path should start with 'meshes\\'");
}
return resPath.substr(prefix.size() + 1);
}
VFS::Path::Normalized Misc::ResourceHelpers::correctSoundPath(
VFS::Path::NormalizedView resPath, const VFS::Manager& vfs)
{
// Note: likely should be replaced with
// return correctResourcePath({ { "sound" } }, resPath, vfs, ".mp3");
// but there is a slight difference in behaviour:
// - `correctResourcePath(..., ".mp3")` first checks `.mp3`, then tries the original extension
// - the implementation below first tries the original extension, then falls back to `.mp3`.
// Workaround: Bethesda at some point converted some of the files to mp3, but the references were kept as .wav.
if (!vfs.exists(resPath))
{
VFS::Path::Normalized sound(resPath);
sound.changeExtension("mp3");
return sound;
}
return VFS::Path::Normalized(resPath);
}
bool Misc::ResourceHelpers::isHiddenMarker(const ESM::RefId& id)
{
return id == "prisonmarker" || id == "divinemarker" || id == "templemarker" || id == "northmarker";
}
namespace
{
VFS::Path::Normalized getLODMeshNameImpl(VFS::Path::NormalizedView resPath, std::string_view pattern)
{
const std::string_view::size_type position = Misc::findExtension(resPath.value());
if (position == std::string::npos)
return VFS::Path::Normalized(resPath);
std::string withPattern(resPath.value());
withPattern.insert(position, pattern);
return VFS::Path::Normalized(std::move(withPattern));
}
VFS::Path::Normalized getBestLODMeshName(
VFS::Path::NormalizedView resPath, const VFS::Manager& vfs, std::string_view pattern)
{
if (VFS::Path::Normalized result = getLODMeshNameImpl(resPath, pattern); vfs.exists(result))
return result;
return VFS::Path::Normalized(resPath);
}
std::string_view getDistantMeshPattern(int esmVersion)
{
static constexpr std::string_view dist = "_dist";
static constexpr std::string_view far = "_far";
static constexpr std::string_view lod = "_lod";
switch (esmVersion)
{
case ESM::VER_120:
case ESM::VER_130:
return dist;
case ESM::VER_080:
case ESM::VER_100:
return far;
case ESM::VER_094:
case ESM::VER_170:
return lod;
default:
return std::string_view();
}
}
}
VFS::Path::Normalized Misc::ResourceHelpers::getLODMeshName(
int esmVersion, VFS::Path::NormalizedView resPath, const VFS::Manager& vfs, unsigned char lod)
{
const std::string_view distantMeshPattern = getDistantMeshPattern(esmVersion);
for (int l = lod; l >= 0; --l)
{
std::stringstream patern;
patern << distantMeshPattern << "_" << l;
const VFS::Path::Normalized meshName = getBestLODMeshName(resPath, vfs, patern.view());
if (meshName != resPath)
return meshName;
}
return getBestLODMeshName(resPath, vfs, distantMeshPattern);
}
|