From 8fc7bf2af40905715550922baff98035b6af4ba6 Mon Sep 17 00:00:00 2001 From: JosiahWI <41302989+JosiahWI@users.noreply.github.com> Date: Wed, 26 Mar 2025 13:03:53 -0500 Subject: [PATCH] Fix `core.get_content_info` docs and harden implementation (#15925) --- doc/menu_lua_api.md | 2 +- src/script/lua_api/l_mainmenu.cpp | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/doc/menu_lua_api.md b/doc/menu_lua_api.md index d1b126351..0fccf37a3 100644 --- a/doc/menu_lua_api.md +++ b/doc/menu_lua_api.md @@ -331,7 +331,7 @@ Package - content which is downloadable from the content db, may or may not be i ```lua { name = "technical_id", - type = "mod" or "modpack" or "game" or "txp", + type = "mod" or "modpack" or "game" or "txp" or "unknown", title = "Human readable title", description = "description", author = "author", diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 4f41f4075..f6734c788 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -30,8 +30,12 @@ #include "common/c_converter.h" #include "gui/guiOpenURL.h" #include "gettext.h" +#include "log.h" #include "util/string.h" +#include +#include + /******************************************************************************/ std::string ModApiMainMenu::getTextData(lua_State *L, const std::string &name) { @@ -355,6 +359,14 @@ int ModApiMainMenu::l_get_content_info(lua_State *L) spec.path = path; parseContentInfo(spec); + if (spec.type == "unknown") { + // In <=5.11.0 the API call was erroneously not documented as + // being able to return type "unknown". + // TODO inspect call sites and make sure this is handled, then we can + // likely remove the warning. + warningstream << "Requested content info has type \"unknown\"" << std::endl; + } + lua_newtable(L); lua_pushstring(L, spec.name.c_str()); @@ -369,11 +381,6 @@ int ModApiMainMenu::l_get_content_info(lua_State *L) lua_pushstring(L, spec.author.c_str()); lua_setfield(L, -2, "author"); - if (!spec.title.empty()) { - lua_pushstring(L, spec.title.c_str()); - lua_setfield(L, -2, "title"); - } - lua_pushinteger(L, spec.release); lua_setfield(L, -2, "release"); @@ -389,8 +396,12 @@ int ModApiMainMenu::l_get_content_info(lua_State *L) if (spec.type == "mod") { ModSpec spec; spec.path = path; - // TODO return nothing on failure (needs callers to handle it) - static_cast(parseModContents(spec)); + // Since the content was already determined to be a mod, + // the parsing is guaranteed to succeed unless the init.lua + // file happens to be deleted between the content parse and + // the mod parse. + [[maybe_unused]] bool success = parseModContents(spec); + assert(success); // Dependencies lua_newtable(L);