Refactor cpp code and update docs, adjust lua

This commit is contained in:
Nauta Turbidus 2024-04-22 00:38:36 +02:00
parent 6d59c9dc8e
commit 625601bc0c
7 changed files with 62 additions and 76 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 = <comma-separated aliases>`
e.g. `gameid_alias = aa,bb` (game has been named "aa" and then "bb" before)
* `aliases = <comma-separated 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`:

View File

@ -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<std::string>(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<GameFindPath> 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<fs::DirListNode> 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<fs::DirListNode> 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<std::string> 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<std::string> 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<std::string> aliases;
if (conf.exists("aliases"))
{
std::vector<std::string> aliases_raw = str_split(conf.get("aliases"), ',');
for (const std::string &alias : aliases_raw)
aliases.insert(static_cast<std::string>(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");

View File

@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
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<std::string, std::string> addon_mods_paths;
std::string menuicon_path;
std::unordered_set<std::string> aliases;
// For logging purposes
std::vector<const char *> 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<std::string> &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)
{
}

View File

@ -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");