aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCrystalwarrior <Varsash@Gmail.com>2022-07-26 00:54:11 +0300
committerGitHub <noreply@github.com>2022-07-26 00:54:11 +0300
commitf8c2b1a2f06d2c2f99fa9eaa7b2334524823aac5 (patch)
tree2c1895467a4c9eb681c99ac82d187b15c67f8567
parent0dac3cc2c017e8c785cb00c340ea37e050eb6366 (diff)
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>
-rw-r--r--include/aoapplication.h3
-rw-r--r--src/courtroom.cpp17
-rw-r--r--src/path_functions.cpp58
-rw-r--r--src/text_file_functions.cpp26
4 files changed, 38 insertions, 66 deletions
diff --git a/include/aoapplication.h b/include/aoapplication.h
index c3b7550f..d9b9e89f 100644
--- a/include/aoapplication.h
+++ b/include/aoapplication.h
@@ -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 //////
diff --git a/src/courtroom.cpp b/src/courtroom.cpp
index 1884c7c8..fdb16132 100644
--- a/src/courtroom.cpp
+++ b/src/courtroom.cpp
@@ -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));
}
diff --git a/src/path_functions.cpp b/src/path_functions.cpp
index 4e031d8f..45920d50 100644
--- a/src/path_functions.cpp
+++ b/src/path_functions.cpp
@@ -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
@@ -355,50 +359,6 @@ QString AOApplication::get_real_path(const VPath &vpath) {
// 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);
QString path = baseDir.absoluteFilePath(vpath.toQString() + suffix);
@@ -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();
}
diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp
index dc95dc34..ee6db6b9 100644
--- a/src/text_file_functions.cpp
+++ b/src/text_file_functions.cpp
@@ -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