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

196 lines
3.8 KiB
C++

#include "missionhotkey.h"
#include "enums.h"
#include "mission/missionhotkey.h"
#include "playerman/player.h"
namespace scripting {
namespace api {
hotkey_h::hotkey_h() : line(-1) {}
hotkey_h::hotkey_h(int l_line) : line(l_line) {}
hotkey_line* hotkey_h::getLine() const
{
if (!isValid())
return nullptr;
return &Hotkey_lines[line];
}
int hotkey_h::getIndex() const
{
return line;
}
bool hotkey_h::isValid() const
{
return line >= 0 && line < MAX_LINES;
}
//**********HANDLE: help section
ADE_OBJ(l_Hotkey, hotkey_h, "hotkey_ship", "Hotkey handle");
ADE_VIRTVAR(Text, l_Hotkey, nullptr, "The text of this hotkey line", "string", "The text")
{
hotkey_h current;
if (!ade_get_args(L, "o", l_Hotkey.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.getLine()->label);
}
ADE_VIRTVAR(Type,
l_Hotkey,
nullptr,
"The type of this hotkey line.",
"enumeration",
"A HOTKEY_* enumeration")
{
hotkey_h current;
lua_enum eh_idx = ENUM_INVALID;
if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "o", l_Enum.Set(enum_h(eh_idx)));
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
switch (current.getLine()->type) {
case HotkeyLineType::HEADING:
eh_idx = LE_HOTKEY_LINE_HEADING;
break;
case HotkeyLineType::WING:
eh_idx = LE_HOTKEY_LINE_WING;
break;
case HotkeyLineType::SHIP:
eh_idx = LE_HOTKEY_LINE_SHIP;
break;
case HotkeyLineType::SUBSHIP:
eh_idx = LE_HOTKEY_LINE_SUBSHIP;
break;
case HotkeyLineType::NONE:
default:
eh_idx = LE_HOTKEY_LINE_NONE;
break;
}
return ade_set_args(L, "o", l_Enum.Set(enum_h(eh_idx)));
}
ADE_VIRTVAR(Keys,
l_Hotkey,
nullptr,
"Gets a table of hotkeys set to the ship in the order from F5 - F12",
"{ number => boolean ... }",
"The hotkeys table")
{
hotkey_h current;
if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
return ADE_RETURN_NIL;
}
auto table = luacpp::LuaTable::create(L);
if (!current.isValid()) {
return ade_set_error(L, "t", &table);
}
int hotkeys;
if (current.getLine()->type == HotkeyLineType::WING)
hotkeys = get_wing_hotkeys(current.getIndex()); // for wings
else
hotkeys = get_ship_hotkeys(current.getIndex()); // for everything else (there's mastercard)
for (int i = 0; i < MAX_KEYED_TARGETS; i++) {
bool key_active = hotkeys & (1 << i);
table.addValue(i + 1, key_active); // translate to Lua index
}
return ade_set_args(L, "t", &table);
}
ADE_FUNC(addHotkey,
l_Hotkey,
"number Key",
"Adds a hotkey to the to the ship in the list. 1-8 correspond to F5-F12. Returns nothing.",
nullptr,
nullptr)
{
SCP_UNUSED(L);
hotkey_h current;
int key;
if (!ade_get_args(L, "oi", l_Hotkey.Get(&current), &key)) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ADE_RETURN_NIL;
}
key--;
add_hotkey(key, current.getIndex());
return ADE_RETURN_NIL;
}
ADE_FUNC(removeHotkey,
l_Hotkey,
"number Key",
"Removes a hotkey from the ship in the list. 1-8 correspond to F5-F12. Returns nothing.",
nullptr,
nullptr)
{
SCP_UNUSED(L);
hotkey_h current;
int key;
if (!ade_get_args(L, "oi", l_Hotkey.Get(&current), &key)) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ADE_RETURN_NIL;
}
key--;
remove_hotkey(key, current.getIndex());
return ADE_RETURN_NIL;
}
ADE_FUNC(clearHotkeys,
l_Hotkey,
nullptr,
"Clears all hotkeys from the ship in the list. Returns nothing.",
nullptr,
nullptr)
{
SCP_UNUSED(L);
hotkey_h current;
if (!ade_get_args(L, "o", l_Hotkey.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ADE_RETURN_NIL;
}
clear_hotkeys(current.getIndex());
return ADE_RETURN_NIL;
}
} // namespace api
} // namespace scripting