Part 2 of #713: merge get_real_suffixed_path into get_real_path (#717)

* Fix get_real_suffixed_path existing, causing the previous PR to not function on suffixed... anything
Fix suffixes ignoring a case where a suffixed path is already provided, causing that pre-suffixed filepath to fail to find anything

* Fix image paths being used as sound effects and vice versa
Better check for sfx and image absolute paths which double-checks the absolute path we got is *actually a valid file format*

Co-authored-by: stonedDiscord <Tukz@gmx.de>
Co-authored-by: Salanto <62221668+Salanto@users.noreply.github.com>
This commit is contained in:
Crystalwarrior 2022-07-26 00:54:11 +03:00 committed by GitHub
parent 0dac3cc2c0
commit f8c2b1a2f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 66 deletions

View File

@ -166,8 +166,7 @@ public:
QString get_sfx(QString p_sfx, QString p_misc="", QString p_character="");
QString get_pos_path(const QString& pos, bool desk = false);
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);
QString get_real_path(const VPath &vpath, const QStringList &suffixes={""});
void invalidate_lookup_cache();
////// Functions for reading and writing files //////

View File

@ -4695,22 +4695,9 @@ void Courtroom::set_effects_dropdown()
ui_effects_dropdown->show();
ui_effects_dropdown->addItems(effectslist);
// ICON-MAKING HELL
QString p_effect = ao_app->read_char_ini(current_char, "effects", "Options");
VPath custom_path("misc/" + p_effect + "/icons/");
VPath theme_path = ao_app->get_theme_path("effects/icons/");
VPath default_path = ao_app->get_theme_path("effects/icons/", "default");
// Make the icons
for (int i = 0; i < ui_effects_dropdown->count(); ++i) {
VPath entry = VPath(ui_effects_dropdown->itemText(i));
QString iconpath = ao_app->get_image_suffix(custom_path + entry);
if (!file_exists(iconpath)) {
iconpath = ao_app->get_image_suffix(theme_path + entry);
if (!file_exists(iconpath)) {
iconpath = ao_app->get_image_suffix(default_path + entry);
if (!file_exists(iconpath))
continue;
}
}
QString iconpath = ao_app->get_effect("icons/" + ui_effects_dropdown->itemText(i), current_char, "");
ui_effects_dropdown->setItemIcon(i, QIcon(iconpath));
}

View File

@ -334,11 +334,15 @@ QString AOApplication::get_case_sensitive_path(QString p_file)
#endif
}
QString AOApplication::get_real_path(const VPath &vpath) {
QString AOApplication::get_real_path(const VPath &vpath,
const QStringList &suffixes) {
// Try cache first
QString phys_path = asset_lookup_cache.value(qHash(vpath));
if (!phys_path.isEmpty() && exists(phys_path)) {
return phys_path;
for (const QString &suffix : suffixes) { // make sure cached asset is the right type
if (phys_path.endsWith(suffix, Qt::CaseInsensitive))
return phys_path;
}
}
// Cache miss; try all known mount paths
@ -354,50 +358,6 @@ QString AOApplication::get_real_path(const VPath &vpath) {
// content 2
// content 1
// base
for (const QString &base : bases) {
QDir baseDir(base);
QString path = baseDir.absoluteFilePath(vpath.toQString());
if (!path.startsWith(baseDir.absolutePath())) {
qWarning() << "invalid path" << path << "(path is outside vfs)";
break;
}
path = get_case_sensitive_path(path);
if (exists(path)) {
asset_lookup_cache.insert(qHash(vpath), path);
unsigned int cache_size = asset_lookup_cache.size();
if (is_power_2(cache_size))
qDebug() << "lookup cache has reached" << cache_size << "entries";
return path;
}
}
// Not found in mount paths; check if the file is remote
QString remotePath = vpath.toQString();
if (remotePath.startsWith("http:") || remotePath.startsWith("https:")) {
return remotePath;
}
// File or directory not found
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(qHash(vpath));
if (!phys_path.isEmpty() && exists(phys_path)) {
for (const QString &suffix : suffixes) { // make sure cached asset is the right type
if (phys_path.endsWith(suffix, Qt::CaseInsensitive))
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);
@ -417,6 +377,12 @@ QString AOApplication::get_real_suffixed_path(const VPath &vpath,
}
}
// Not found in mount paths; check if the file is remote
QString remotePath = vpath.toQString();
if (remotePath.startsWith("http:") || remotePath.startsWith("https:")) {
return remotePath;
}
// File or directory not found
return QString();
}

View File

@ -544,8 +544,18 @@ QString AOApplication::get_court_sfx(QString p_identifier, QString p_misc)
QString AOApplication::get_sfx_suffix(VPath sound_to_check)
{
return get_real_suffixed_path(sound_to_check,
{".opus", ".ogg", ".mp3", ".wav", ".mid", ".midi", ".xm", ".it", ".s3m", ".mod", ".mtm", ".umx" });
QStringList suffixes = {".opus", ".ogg", ".mp3", ".wav", ".mid", ".midi", ".xm", ".it", ".s3m", ".mod", ".mtm", ".umx" };
// Check if we were provided a direct filepath with a suffix already
QString path = sound_to_check.toQString();
// Loop through our suffixes
for (const QString &suffix : suffixes) {
// If our VPath ends with a valid suffix
if (path.endsWith(suffix, Qt::CaseInsensitive))
// Return that as the path
return get_real_path(sound_to_check);
}
// Otherwise, ignore the provided suffix and check our own
return get_real_path(sound_to_check, suffixes);
}
QString AOApplication::get_image_suffix(VPath path_to_check, bool static_image)
@ -556,7 +566,17 @@ QString AOApplication::get_image_suffix(VPath path_to_check, bool static_image)
}
suffixes.append(".png");
return get_real_suffixed_path(path_to_check, suffixes);
// Check if we were provided a direct filepath with a suffix already
QString path = path_to_check.toQString();
// Loop through our suffixes
for (const QString &suffix : suffixes) {
// If our VPath ends with a valid suffix
if (path.endsWith(suffix, Qt::CaseInsensitive))
// Return that as the path
return get_real_path(path_to_check);
}
// Otherwise, ignore the provided suffix and check our own
return get_real_path(path_to_check, suffixes);
}
// returns whatever is to the right of "search_line =" within target_tag and