Get the add-on ID from the file path if possible. (#7981)

Co-authored-by: Gunter Labes <soliton@wesnoth.org>
This commit is contained in:
Pentarctagon 2023-10-27 10:22:52 -05:00 committed by GitHub
parent 81bbcabe13
commit 6b9a7627b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 2 deletions

View file

@ -29,7 +29,6 @@ lg::log_domain log_editor("editor");
namespace editor {
// TODO: remember refreshing through F5
std::string initialize_addon()
{
std::string addon_id = "";

View file

@ -915,7 +915,12 @@ void context_manager::load_map(const std::string& filename, bool new_context)
if(filesystem::ends_with(filename, ".cfg")) {
if(editor_controller::current_addon_id_ == "") {
editor_controller::current_addon_id_ = editor::initialize_addon();
// if no addon id has been set and the file being loaded is from an addon
// then use the file path to determine the addon rather than showing a dialog
editor_controller::current_addon_id_ = filesystem::get_addon_id_from_path(filename);
if(editor_controller::current_addon_id_ == "") {
editor_controller::current_addon_id_ = editor::initialize_addon();
}
set_addon_id(editor_controller::current_addon_id_);
}

View file

@ -1776,4 +1776,19 @@ std::string get_localized_path(const std::string& file, const std::string& suff)
return "";
}
std::string get_addon_id_from_path(const std::string& location)
{
std::string full_path = normalize_path(location, true);
std::string addons_path = normalize_path(get_addons_dir(), true);
if(full_path.find(addons_path) == 0) {
bfs::path path(full_path.substr(addons_path.size()+1));
if(path.size() > 0) {
return path.begin()->string();
}
}
return "";
}
} // namespace filesystem

View file

@ -466,4 +466,10 @@ std::string get_program_invocation(const std::string &program_name);
*/
std::string get_localized_path(const std::string& file, const std::string& suff = "");
/**
* Returns the add-on ID from a path.
* aka the directory directly following the "add-ons" folder, or an empty string if none is found.
*/
std::string get_addon_id_from_path(const std::string& location);
}