From: Pasi Kallinen Date: Wed, 15 Jan 2020 18:17:02 +0000 (+0200) Subject: Fix warning on nonexistent quest.lua text X-Git-Tag: NetHack-3.7.0_WIP~40^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2ba3fa9240ee9b353b2dacb904c58c892602cfa2;p=nethack Fix warning on nonexistent quest.lua text Cavemen don't have goal_alt message - before lua, that one fell through into goal_next message, but now it tried to load the "common" message. Add ability to define message fallbacks, and make goal_next the fallback for goal_alt. Also prevent issuing quest.lua errors twice. --- diff --git a/dat/quest.lua b/dat/quest.lua index 891556987..7e484df1d 100644 --- a/dat/quest.lua +++ b/dat/quest.lua @@ -4,7 +4,7 @@ -- - export the quest string replacements to lua, instead of %H etc -- - allow checking if hero is carrying item (see comments for %Cp Arc 00042) -- - fold quest_portal, quest_portal_again, quest_portal_demand into one --- - some roles have no goal_alt, fold into goal_next? +-- - write tests to check questtext validity? -- - qt_pager hack(?): if (qt_msg->delivery == 'p' && strcmp(windowprocs.name, "X11")) @@ -25,6 +25,10 @@ questtext = { + -- If a role doesn't have a specific message, try a fallback + msg_fallbacks = { + goal_alt = "goal_next" + }, common = { TEST_PATTERN = { output = "text", diff --git a/src/questpgr.c b/src/questpgr.c index 85c272557..5df059fd1 100644 --- a/src/questpgr.c +++ b/src/questpgr.c @@ -432,9 +432,10 @@ boolean common UNUSED; } boolean -com_pager_core(section, msgid) +com_pager_core(section, msgid, showerror) const char *section; const char *msgid; +boolean showerror; { const char *const howtoput[] = { "pline", "window", "text", "menu", "default", NULL }; const int howtoput2i[] = { 1, 2, 2, 3, 0, 0 }; @@ -442,6 +443,7 @@ const char *msgid; lua_State *L; char *synopsis; char *text; + char *fallback_msgid = NULL; if (skip_pager(TRUE)) return FALSE; @@ -449,7 +451,8 @@ const char *msgid; L = nhl_init(); if (!nhl_loadlua(L, QTEXT_FILE)) { - impossible("com_pager: %s not found.", QTEXT_FILE); + if (showerror) + impossible("com_pager: %s not found.", QTEXT_FILE); lua_close(L); return FALSE; } @@ -457,21 +460,40 @@ const char *msgid; lua_settop(L, 0); lua_getglobal(L, "questtext"); if (!lua_istable(L, -1)) { - impossible("com_pager: questtext in %s is not a lua table", QTEXT_FILE); + if (showerror) + impossible("com_pager: questtext in %s is not a lua table", + QTEXT_FILE); lua_close(L); return FALSE; } lua_getfield(L, -1, section); if (!lua_istable(L, -1)) { - impossible("com_pager: questtext[%s] in %s is not a lua table", section, QTEXT_FILE); + if (showerror) + impossible("com_pager: questtext[%s] in %s is not a lua table", + section, QTEXT_FILE); lua_close(L); return FALSE; } - lua_getfield(L, -1, msgid); +tryagain: + lua_getfield(L, -1, fallback_msgid ? fallback_msgid : msgid); if (!lua_istable(L, -1)) { - impossible("com_pager: questtext[%s][%s] in %s is not a lua table", section, msgid, QTEXT_FILE); + if (!fallback_msgid) { + /* Do we have questtxt[msg_fallbacks][]? */ + lua_getfield(L, -3, "msg_fallbacks"); + if (lua_istable(L, -1)) { + fallback_msgid = get_table_str_opt(L, msgid, NULL); + lua_pop(L, 2); + if (fallback_msgid) + goto tryagain; + } + } + + if (showerror) + impossible("com_pager: questtext[%s][%s] in %s is not a lua table", + section, msgid, QTEXT_FILE); + free(fallback_msgid); lua_close(L); return FALSE; } @@ -487,9 +509,11 @@ const char *msgid; nelems = (int) lua_tointeger(L, -1); lua_pop(L, 1); if (nelems < 2) { - impossible( - "com_pager: questtext[%s][%s] in %s in not an array of strings", + if (showerror) + impossible( + "com_pager: questtext[%s][%s] in %s in not an array of strings", section, msgid, QTEXT_FILE); + free(fallback_msgid); lua_close(L); return FALSE; } @@ -518,6 +542,7 @@ const char *msgid; free(synopsis); } + free(fallback_msgid); free(text); lua_close(L); return TRUE; @@ -527,15 +552,15 @@ void com_pager(msgid) const char *msgid; { - com_pager_core("common", msgid); + com_pager_core("common", msgid, TRUE); } void qt_pager(msgid) const char *msgid; { - if (!com_pager_core(g.urole.filecode, msgid)) - com_pager_core("common", msgid); + if (!com_pager_core(g.urole.filecode, msgid, FALSE)) + com_pager_core("common", msgid, TRUE); } struct permonst *