diff options
| author | oldmud0 <oldmud0@users.noreply.github.com> | 2021-06-05 14:58:40 -0500 |
|---|---|---|
| committer | oldmud0 <oldmud0@users.noreply.github.com> | 2021-06-05 14:58:40 -0500 |
| commit | d27501313cae78b838c1e738ebfaeae4740a23b4 (patch) | |
| tree | 207740d8a42b84a851303b8f2583c523f1624caa /src/text_file_functions.cpp | |
| parent | a023657348051cbc7c8ea29e3b37f3e2e3fd16d8 (diff) | |
Finish mounting feature
To pull this one off, a new class called VPath was created that denotes
"virtual" paths that can exist anywhere among the list of mount points.
It is functionally identical to QString, except that implicit conversion
between QString and VPath is not allowed. This makes it easy to spot
errors in path resolution at compile time, since get_real_path must be
called to resolve a VPath into an absolute path that can be passed into
a Qt function as a QString.
Other functions, such as the get_*_suffix functions, also return an
absolute path QString for convenience.
As for the rest of the functions that return a VPath, you will need to
call get_real_path yourself.
Finally, a path resolution cache was added to try to avoid blowing
up what is already a massive lookup cost for assets. The cache
is invalidated when the mount path list is changed.
Currently, this cache isn't bounded. Might need to write an LRU cache
if issues arise.
Diffstat (limited to 'src/text_file_functions.cpp')
| -rw-r--r-- | src/text_file_functions.cpp | 78 |
1 files changed, 46 insertions, 32 deletions
diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index f6a5d6ce..c92287fb 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -124,6 +124,11 @@ QStringList AOApplication::get_call_words() return get_list_file(get_base_path() + "callwords.ini"); } +QStringList AOApplication::get_list_file(VPath path) +{ + return get_list_file(get_real_path(path)); +} + QStringList AOApplication::get_list_file(QString p_file) { QStringList return_value; @@ -276,6 +281,12 @@ QVector<server_type> AOApplication::read_serverlist_txt() } QString AOApplication::read_design_ini(QString p_identifier, + VPath p_design_path) +{ + return read_design_ini(p_identifier, get_real_path(p_design_path)); +} + +QString AOApplication::read_design_ini(QString p_identifier, QString p_design_path) { QSettings settings(p_design_path, QSettings::IniFormat); @@ -440,12 +451,14 @@ QString AOApplication::get_chat_markup(QString p_identifier, QString p_chat) return value.toLatin1(); // Backwards ass compatibility - QStringList backwards_paths{get_theme_path("misc/" + p_chat + "/config.ini"), - get_base_path() + "misc/" + p_chat + - "/config.ini", - get_base_path() + "misc/default/config.ini", - get_theme_path("misc/default/config.ini")}; - for (const QString &p : backwards_paths) { + QVector<VPath> backwards_paths { + get_theme_path("misc/" + p_chat + "/config.ini"), + VPath("misc/" + p_chat + "/config.ini"), + VPath("misc/default/config.ini"), + get_theme_path("misc/default/config.ini") + }; + + for (const VPath &p : backwards_paths) { QString value = read_design_ini(p_identifier, p); if (!value.isEmpty()) { return value.toLatin1(); @@ -482,36 +495,32 @@ QString AOApplication::get_court_sfx(QString p_identifier, QString p_misc) return ""; } -QString AOApplication::get_sfx_suffix(QString sound_to_check) +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) { - if (file_exists(sound_to_check)) - return sound_to_check; - if (file_exists(sound_to_check + ".opus")) - return sound_to_check + ".opus"; - if (file_exists(sound_to_check + ".ogg")) - return sound_to_check + ".ogg"; - if (file_exists(sound_to_check + ".mp3")) - return sound_to_check + ".mp3"; - if (file_exists(sound_to_check + ".mp4")) - return sound_to_check + ".mp4"; - return sound_to_check + ".wav"; + return get_suffix(sound_to_check, { "", ".opus", ".ogg", ".mp3", ".wav" }); } -QString AOApplication::get_image_suffix(QString path_to_check, bool static_image) +QString AOApplication::get_image_suffix(VPath path_to_check, bool static_image) { - if (file_exists(path_to_check)) - return path_to_check; + 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) { - if (file_exists(path_to_check + ".webp")) - return path_to_check + ".webp"; - if (file_exists(path_to_check + ".apng")) - return path_to_check + ".apng"; - if (file_exists(path_to_check + ".gif")) - return path_to_check + ".gif"; + suffixes.append({ ".webp", ".apng", ".gif" }); } - return path_to_check + ".png"; + suffixes.append(".png"); + + return get_suffix(path_to_check, suffixes); } // returns whatever is to the right of "search_line =" within target_tag and @@ -520,7 +529,7 @@ QString AOApplication::get_image_suffix(QString path_to_check, bool static_image QString AOApplication::read_char_ini(QString p_char, QString p_search_line, QString target_tag) { - QSettings settings(get_character_path(p_char, "char.ini"), + QSettings settings(get_real_path(get_character_path(p_char, "char.ini")), QSettings::IniFormat); settings.beginGroup(target_tag); QString value = settings.value(p_search_line).value<QString>(); @@ -531,7 +540,7 @@ QString AOApplication::read_char_ini(QString p_char, QString p_search_line, void AOApplication::set_char_ini(QString p_char, QString value, QString p_search_line, QString target_tag) { - QSettings settings(get_character_path(p_char, "char.ini"), + QSettings settings(get_real_path(get_character_path(p_char, "char.ini")), QSettings::IniFormat); settings.beginGroup(target_tag); settings.setValue(p_search_line, value); @@ -539,10 +548,10 @@ void AOApplication::set_char_ini(QString p_char, QString value, } // returns all the values of target_tag -QStringList AOApplication::read_ini_tags(QString p_path, QString target_tag) +QStringList AOApplication::read_ini_tags(VPath p_path, QString target_tag) { QStringList r_values; - QSettings settings(p_path, QSettings::IniFormat); + QSettings settings(get_real_path(p_path), QSettings::IniFormat); if (!target_tag.isEmpty()) settings.beginGroup(target_tag); QStringList keys = settings.allKeys(); @@ -1084,3 +1093,8 @@ bool AOApplication::get_animated_theme() configini->value("animated_theme", "true").value<QString>(); return result.startsWith("true"); } + +QStringList AOApplication::get_mount_paths() +{ + return configini->value("mount_paths").value<QStringList>(); +} |
