From 037d96a5d96bae6e341d8f7ca4f485d519cde02b Mon Sep 17 00:00:00 2001 From: oldmud0 Date: Sat, 5 Jun 2021 20:28:55 -0500 Subject: [PATCH] Use intuitive behavior for loading assets with ambiguous extensions --- include/aoapplication.h | 4 +--- src/aobutton.cpp | 9 ++------- src/debug_functions.cpp | 1 + src/path_functions.cpp | 33 +++++++++++++++++++++++++++++++++ src/text_file_functions.cpp | 17 +++-------------- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/include/aoapplication.h b/include/aoapplication.h index 0c61d7b..24a09ec 100644 --- a/include/aoapplication.h +++ b/include/aoapplication.h @@ -167,6 +167,7 @@ public: QString get_sfx(QString p_sfx, QString p_misc="", QString p_character=""); QString get_case_sensitive_path(QString p_file); QString get_real_path(const VPath &vpath); + QString get_real_suffixed_path(const VPath &vpath, const QStringList &suffixes); void invalidate_lookup_cache(); ////// Functions for reading and writing files ////// @@ -355,9 +356,6 @@ public: // Returns the sfx with p_identifier from courtroom_sounds.ini in the current theme path QString get_court_sfx(QString p_identifier, QString p_misc=""); - // Find the correct suffix for a given file - QString get_suffix(VPath file_to_check, QStringList suffixes); - // Figure out if we can opus this or if we should fall back to wav QString get_sfx_suffix(VPath sound_to_check); diff --git a/src/aobutton.cpp b/src/aobutton.cpp index fb9da7d..242fe79 100644 --- a/src/aobutton.cpp +++ b/src/aobutton.cpp @@ -20,13 +20,8 @@ void AOButton::set_image(QString p_path, QString p_misc) { movie->stop(); QString p_image; - // Check if the user wants animated themes - if (ao_app->get_animated_theme()) - // We want an animated image - p_image = ao_app->get_image(p_path, ao_app->current_theme, ao_app->get_subtheme(), ao_app->default_theme, p_misc); - else - // Grab a static variant of the image - p_image = ao_app->get_image_path(ao_app->get_asset_paths(p_path, ao_app->current_theme, ao_app->get_subtheme(), ao_app->default_theme, p_misc), true); + p_image = ao_app->get_image(p_path, ao_app->current_theme, ao_app->get_subtheme(), + ao_app->default_theme, p_misc, "", "", !ao_app->get_animated_theme()); if (p_image.isEmpty()) { this->setIcon(QIcon()); this->setIconSize(this->size()); diff --git a/src/debug_functions.cpp b/src/debug_functions.cpp index 1613a7d..456aee8 100644 --- a/src/debug_functions.cpp +++ b/src/debug_functions.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/src/path_functions.cpp b/src/path_functions.cpp index b1a5e48..f79f7bf 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(); } diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index c92287f..9c46341 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -495,32 +495,21 @@ QString AOApplication::get_court_sfx(QString p_identifier, QString p_misc) return ""; } -QString AOApplication::get_suffix(VPath path_to_check, QStringList suffixes) { - for (const QString &suffix : suffixes) { - QString path = get_real_path(VPath(path_to_check.toQString() + suffix)); - if (!path.isEmpty()) - return path; - } - - return QString(); -} - QString AOApplication::get_sfx_suffix(VPath sound_to_check) { - return get_suffix(sound_to_check, { "", ".opus", ".ogg", ".mp3", ".wav" }); + return get_real_suffixed_path(sound_to_check, + { "", ".opus", ".ogg", ".mp3", ".wav" }); } QString AOApplication::get_image_suffix(VPath path_to_check, bool static_image) { QStringList suffixes { "" }; - // A better method would to actually use AOImageReader and see if these images have more than 1 frame. - // However, that might not be performant. if (!static_image) { suffixes.append({ ".webp", ".apng", ".gif" }); } suffixes.append(".png"); - return get_suffix(path_to_check, suffixes); + return get_real_suffixed_path(path_to_check, suffixes); } // returns whatever is to the right of "search_line =" within target_tag and