Files
Goober5000andClaude Fable 5.1 37b0b6ea82 Standardize isValid across the Lua API by making the types auto-generate it (#7761)
* Automatically generate the isValid() Lua API function for handle types

If an ADE object's stored type has an isValid() member, ade_obj now registers an isValid function for it automatically.  An explicit ADE_FUNC(isValid, ...) declared after the ADE_OBJ replaces the generated one, so types with special semantics (e.g. sound) keep their own.  The 67 hand-written functions that merely delegated to the member are removed, and 26 types that previously exposed no isValid() gain one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* Add ADE_OBJ_VALIDATOR for index-based Lua handles

Int-index handles (shipclass, weaponclass, team, etc.) can now declare their validity predicate with a single ADE_OBJ_VALIDATOR or ADE_OBJ_VALIDATOR_RANGE line after the ADE_OBJ.  The registered validator drives the generated isValid() function and is also honored by ade_set_args when the mod table asks for nil instead of invalid objects, which previously never applied to these handles.  The 19 hand-written range-check functions are replaced, and loadout_ship, default_primary, default_secondary, and support_rearm_pool_team gain isValid().  Add a validator to HudGauge while we're at it.

Also fully qualify the scripting namespace in LuaConvert.h so its templates can be instantiated from the unit tests.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
2026-09-08 10:50:53 -04:00

100 lines
2.5 KiB
C++

#include "gamehelp.h"
#include "gamehelp/gameplayhelp.h"
namespace scripting {
namespace api {
help_section_h::help_section_h() : section(-1) {}
help_section_h::help_section_h(int l_section) : section(l_section) {}
gameplay_help_section* help_section_h::getSection() const
{
if (!isValid())
return nullptr;
return &Help_text[section];
}
bool help_section_h::isValid() const
{
return section >= 0 && section < (int)Help_text.size();
}
//**********HANDLE: help section
ADE_OBJ(l_Help_Section, help_section_h, "help_section", "Help Section handle");
ADE_VIRTVAR(Title, l_Help_Section, nullptr, "The title of the help section", "string", "The title")
{
help_section_h current;
if (!ade_get_args(L, "o", l_Help_Section.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "s", "");
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", current.getSection()->title);
}
ADE_VIRTVAR(Header, l_Help_Section, nullptr, "The header of the help section", "string", "The header")
{
help_section_h current;
if (!ade_get_args(L, "o", l_Help_Section.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "s", "");
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "s", current.getSection()->header);
}
ADE_VIRTVAR(Keys, l_Help_Section, nullptr, "Gets a table of keys in the help section", "{ number => string ... }", "The keys table")
{
help_section_h current;
if (!ade_get_args(L, "o", l_Help_Section.Get(&current))) {
return ADE_RETURN_NIL;
}
auto table = luacpp::LuaTable::create(L);
if (!current.isValid()) {
return ade_set_error(L, "t", &table);
}
for (size_t i = 0; i < current.getSection()->key.size(); i++) {
table.addValue(i + 1, current.getSection()->key[i]); //translate to Lua index
}
return ade_set_args(L, "t", &table);
}
ADE_VIRTVAR(Texts, l_Help_Section, nullptr, "Gets a table of texts in the help section", "{ number => string ... }", "The texts table")
{
help_section_h current;
if (!ade_get_args(L, "o", l_Help_Section.Get(&current))) {
return ADE_RETURN_NIL;
}
auto table = luacpp::LuaTable::create(L);
if (!current.isValid()) {
return ade_set_error(L, "t", &table);
}
for (size_t i = 0; i < current.getSection()->text.size(); i++) {
table.addValue(i + 1, current.getSection()->text[i]); //translate to Lua index
}
return ade_set_args(L, "t", &table);
}
} // namespace api
} // namespace scripting