Use intuitive behavior for loading assets with ambiguous extensions

This commit is contained in:
oldmud0 2021-06-05 20:28:55 -05:00
parent d27501313c
commit 037d96a5d9
5 changed files with 40 additions and 24 deletions

View File

@ -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);

View File

@ -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());

View File

@ -1,4 +1,5 @@
#include <QCoreApplication>
#include <QElapsedTimer>
#include <QMessageBox>
#include <QTimer>
#include <functional>

View File

@ -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();
}

View File

@ -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