aboutsummaryrefslogtreecommitdiff
path: root/src/path_functions.cpp
diff options
context:
space:
mode:
authoroldmud0 <oldmud0@users.noreply.github.com>2021-06-05 20:28:55 -0500
committeroldmud0 <oldmud0@users.noreply.github.com>2021-06-05 20:28:55 -0500
commit037d96a5d96bae6e341d8f7ca4f485d519cde02b (patch)
tree6b25ebc80bd2ce74372241793ec2e4724acb71f1 /src/path_functions.cpp
parentd27501313cae78b838c1e738ebfaeae4740a23b4 (diff)
Use intuitive behavior for loading assets with ambiguous extensions
Diffstat (limited to 'src/path_functions.cpp')
-rw-r--r--src/path_functions.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/path_functions.cpp b/src/path_functions.cpp
index b1a5e48e..f79f7bf8 100644
--- a/src/path_functions.cpp
+++ b/src/path_functions.cpp
@@ -246,6 +246,39 @@ QString AOApplication::get_real_path(const VPath &vpath) {
return QString();
}
+// Special case of get_real_path where multiple suffixes need to be tried
+// on each mount path.
+QString AOApplication::get_real_suffixed_path(const VPath &vpath,
+ const QStringList &suffixes) {
+ // Try cache first
+ QString phys_path = asset_lookup_cache.value(vpath);
+ if (!phys_path.isEmpty() && exists(phys_path)) {
+ return phys_path;
+ }
+
+ // Cache miss; try each suffix on all known mount paths
+ QStringList bases = get_mount_paths();
+ bases.push_front(get_base_path());
+
+ for (const QString &base : bases) {
+ for (const QString &suffix : suffixes) {
+ QDir baseDir(base);
+ const QString path = baseDir.absoluteFilePath(vpath.toQString() + suffix);
+ if (!path.startsWith(baseDir.absolutePath())) {
+ qWarning() << "invalid path" << path << "(path is outside vfs)";
+ break;
+ }
+ if (exists(get_case_sensitive_path(path))) {
+ asset_lookup_cache.insert(vpath, path);
+ return path;
+ }
+ }
+ }
+
+ // File or directory not found
+ return QString();
+}
+
void AOApplication::invalidate_lookup_cache() {
asset_lookup_cache.clear();
}