From 625601bc0ce674effc59610a45a99b370d301675 Mon Sep 17 00:00:00 2001 From: Nauta Turbidus <88062389+nauta-turbidus@users.noreply.github.com> Date: Mon, 22 Apr 2024 00:38:36 +0200 Subject: [PATCH] Refactor cpp code and update docs, adjust lua --- builtin/mainmenu/content/contentdb.lua | 7 +-- builtin/mainmenu/init.lua | 20 +----- builtin/mainmenu/tab_local.lua | 4 +- doc/lua_api.md | 4 +- src/content/subgames.cpp | 85 +++++++++++++------------- src/content/subgames.h | 8 ++- src/script/lua_api/l_mainmenu.cpp | 10 ++- 7 files changed, 62 insertions(+), 76 deletions(-) diff --git a/builtin/mainmenu/content/contentdb.lua b/builtin/mainmenu/content/contentdb.lua index d9aa700c1..87e504389 100644 --- a/builtin/mainmenu/content/contentdb.lua +++ b/builtin/mainmenu/content/contentdb.lua @@ -94,7 +94,6 @@ local function start_install(package, reason) gamedata.errormessage = result.msg else local delete_old_dir - print(dump(package)) if package.path then local name = pkgmgr.normalize_game_id(package.path:match("[^/\\]+[/\\]?$")) if name ~= package.name then @@ -149,7 +148,7 @@ local function start_install(package, reason) gameid_aliases[#gameid_aliases + 1] = alias_cut end end - conf:set("gameid_alias", table.concat(gameid_aliases, ",")) + conf:set("aliases", table.concat(gameid_aliases, ",")) end conf:write() end @@ -407,10 +406,6 @@ local function fetch_pkgs(params) package.url_part = core.urlencode(package.author) .. "/" .. core.urlencode(package.name) - if package.name == "void" then - package.aliases = {"Linuxdirk/old_void"} - end - if package.aliases then local suffix = "/" .. package.name local suf_len = #suffix diff --git a/builtin/mainmenu/init.lua b/builtin/mainmenu/init.lua index f8737791c..3a67cbf7c 100644 --- a/builtin/mainmenu/init.lua +++ b/builtin/mainmenu/init.lua @@ -81,25 +81,9 @@ local function init_globals() if element.gameid == gameid then return true end - local gameconfig = Settings(pkgmgr.find_by_gameid(gameid).path .. "/game.conf") - local aliases = (gameconfig:get("gameid_alias") or ""):split() - for _, alias in pairs(aliases) do - alias = alias:trim() - if element.gameid == alias then + for _, alias in ipairs(pkgmgr.find_by_gameid(gameid).aliases) do + if pkgmgr.normalize_game_id(element.gameid) == pkgmgr.normalize_game_id(alias) then return true - else - local el_id_len = #element.gameid - if el_id_len > 5 and element.gameid:sub(el_id_len - 4) == "_game" then - if element.gameid:sub(1, el_id_len - 5) == alias then - return true - end - end - local alias_len = #alias - if alias_len > 5 and alias:sub(alias_len - 4) == "_game" then - if element.gameid == alias:sub(1, alias_len - 5) then - return true - end - end end end return false diff --git a/builtin/mainmenu/tab_local.lua b/builtin/mainmenu/tab_local.lua index 2a9e1316c..782a19a49 100644 --- a/builtin/mainmenu/tab_local.lua +++ b/builtin/mainmenu/tab_local.lua @@ -360,9 +360,7 @@ local function main_button_handler(this, fields, name, tabdata) if not game_obj then local function try_to_find_alias(world) for _, game in ipairs(pkgmgr.games) do - local aliases = game.aliases:split() - for _, alias in pairs(aliases) do - alias = alias:trim() + for _, alias in pairs(game.aliases) do if pkgmgr.normalize_game_id(alias) == pkgmgr.normalize_game_id(world.gameid) then world.gameid = game.id return game diff --git a/doc/lua_api.md b/doc/lua_api.md index 3580c3d22..1fddef95e 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -93,8 +93,8 @@ The game directory can contain the following files: an internal ID used to track versions. * `textdomain`: Textdomain used to translate description. Defaults to game id. See [Translating content meta](#translating-content-meta). - * `gameid_alias = ` - e.g. `gameid_alias = aa,bb` (game has been named "aa" and then "bb" before) + * `aliases = ` + e.g. `aliases = aa,bb` (game has been named "aa" and then "bb" before) This allows automatic loading of worlds using a gameid from this list. This is intended to allow a full rename of a game, including its id. * `minetest.conf`: diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp index 28b3f098d..77cbf9b79 100644 --- a/src/content/subgames.cpp +++ b/src/content/subgames.cpp @@ -101,6 +101,15 @@ SubgameSpec findSubgame(const std::string &id) std::string share = porting::path_share; std::string user = porting::path_user; + auto normalize_game_id = [&](std::string_view name){ + if (str_ends_with(name, "_game")) + return name.substr(0, name.size() - 5); + + return name; + }; + + std::string idv = static_cast(normalize_game_id(trim(id))); + // Get games install locations Strfnd search_paths(getSubgamePathEnv()); @@ -134,8 +143,6 @@ SubgameSpec findSubgame(const std::string &id) } } - std::string idv = id; - // Failed to find the game, try to find aliased game if (game_path.empty()) { std::vector gamespaths; @@ -147,49 +154,37 @@ SubgameSpec findSubgame(const std::string &id) while (!search_paths.at_end()) gamespaths.emplace_back(search_paths.next(PATH_DELIM), false); - for (const GameFindPath &gamespath : gamespaths) { - const std::string &path = gamespath.path; - std::vector dirlist = fs::GetDirListing(path); - for (const fs::DirListNode &dln : dirlist) { - if (!dln.dir) - continue; + auto look_for_alias = [&](){ + for (const GameFindPath &gamespath : gamespaths) { + const std::string &path = gamespath.path; + std::vector dirlist = fs::GetDirListing(path); + for (const fs::DirListNode &dln : dirlist) { + if (!dln.dir) + continue; - // If configuration file is not found or broken, ignore game - Settings conf; - std::string conf_path = path + DIR_DELIM + dln.name + - DIR_DELIM + "game.conf"; - if (!conf.readConfigFile(conf_path.c_str())) - continue; + // If configuration file is not found or broken, ignore game + Settings conf; + std::string conf_path = path + DIR_DELIM + dln.name + + DIR_DELIM + "game.conf"; + if (!conf.readConfigFile(conf_path.c_str())) + continue; - if (conf.exists("gameid_alias")) { - std::vector aliases = str_split(conf.get("gameid_alias"), ','); - for (const std::string &alias_raw : aliases) { - std::string_view alias = trim(alias_raw); - auto found_alias = [&](){ - idv = dln.name; - game_path = path + DIR_DELIM + dln.name; - user_game = gamespath.user_specific; - }; - // Try direct matching with the alias - if (alias == id) { - found_alias(); - break; - // Make sure a "_game" suffix is ignored - } else if (str_ends_with(alias, "_game") - && alias.substr(0, alias.size() - 5) == id) { - found_alias(); - break; - } else if (str_ends_with(id, "_game") - && id.substr(0, id.size() - 5) == alias) { - found_alias(); - break; + if (conf.exists("aliases")) { + std::vector aliases = str_split(conf.get("aliases"), ','); + for (const std::string &alias_raw : aliases) { + std::string_view alias = normalize_game_id(trim(alias_raw)); + if (alias == idv) { + idv = dln.name; + game_path = path + DIR_DELIM + dln.name; + user_game = gamespath.user_specific; + return; + } } } } } - if (!game_path.empty()) - break; // Aliased game has been found, escape the loop early - } + }; + look_for_alias(); if (game_path.empty()) // Failed to find the game taking aliases into account return SubgameSpec(); @@ -229,9 +224,13 @@ SubgameSpec findSubgame(const std::string &id) if (conf.exists("release")) game_release = conf.getS32("release"); - std::string gameid_alias; - if (conf.exists("gameid_alias")) - gameid_alias = conf.get("gameid_alias"); + std::unordered_set aliases; + if (conf.exists("aliases")) + { + std::vector aliases_raw = str_split(conf.get("aliases"), ','); + for (const std::string &alias : aliases_raw) + aliases.insert(static_cast(normalize_game_id(trim(alias)))); + } std::string menuicon_path; #ifndef SERVER @@ -240,7 +239,7 @@ SubgameSpec findSubgame(const std::string &id) #endif SubgameSpec spec(id, game_path, gamemod_path, mods_paths, game_title, - menuicon_path, game_author, game_release, gameid_alias); + menuicon_path, game_author, game_release, aliases); if (conf.exists("name") && !conf.exists("title")) spec.deprecation_msgs.push_back("\"name\" setting in game.conf is deprecated, please use \"title\" instead"); diff --git a/src/content/subgames.h b/src/content/subgames.h index 1395426ef..3b67e17bc 100644 --- a/src/content/subgames.h +++ b/src/content/subgames.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include class Settings; @@ -34,7 +35,6 @@ struct SubgameSpec int release; std::string path; std::string gamemods_path; - std::string gameid_alias; /** * Map from virtual path to mods path @@ -42,6 +42,8 @@ struct SubgameSpec std::unordered_map addon_mods_paths; std::string menuicon_path; + std::unordered_set aliases; + // For logging purposes std::vector deprecation_msgs; @@ -51,11 +53,11 @@ struct SubgameSpec const std::string &title = "", const std::string &menuicon_path = "", const std::string &author = "", int release = 0, - const std::string &gameid_alias = "") : + const std::unordered_set &aliases = {}) : id(id), title(title), author(author), release(release), path(path), gamemods_path(gamemods_path), addon_mods_paths(addon_mods_paths), - menuicon_path(menuicon_path), gameid_alias(gameid_alias) + menuicon_path(menuicon_path), aliases(aliases) { } diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 4906adb76..89623ecec 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -333,7 +333,15 @@ int ModApiMainMenu::l_get_games(lua_State *L) lua_settable(L, top_lvl2); lua_pushstring(L, "aliases"); - lua_pushstring(L, game.gameid_alias.c_str()); + lua_newtable(L); + int table_aliases = lua_gettop(L); + int alias_index = 1; + for (const auto &alias : game.aliases) { + lua_pushnumber(L, alias_index); + lua_pushstring(L, alias.c_str()); + lua_settable(L, table_aliases); + alias_index++; + } lua_settable(L, top_lvl2); lua_pushstring(L, "addon_mods_paths");