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

75 lines
2.3 KiB
C++

//
//
#include "enums.h"
#include "parse_object.h"
#include "ship_registry_entry.h"
#include "ship.h"
#include "ship/ship.h"
namespace scripting {
namespace api {
//**********HANDLE: ShipRegistryEntry
ADE_OBJ(l_ShipRegistryEntry, int, "ship_registry_entry", "Ship entry handle");
ADE_OBJ_VALIDATOR(l_ShipRegistryEntry, idx, Ship_registry.in_bounds(idx));
ADE_VIRTVAR(Name, l_ShipRegistryEntry, nullptr, "Name of ship", "string", "Ship name, or empty string if handle is invalid")
{
int idx;
if (!ade_get_args(L, "o", l_ShipRegistryEntry.Get(&idx)))
return ade_set_args(L, "s", "");
if (idx < 0 || (size_t)idx >= Ship_registry.size())
return ade_set_args(L, "s", "");
if (ADE_SETTING_VAR)
LuaError(L, "This property is read only.");
return ade_set_args(L, "s", Ship_registry[idx].name);
}
ADE_VIRTVAR(Status, l_ShipRegistryEntry, nullptr, "Status of ship", "enumeration", "A SHIP_STATUS_* enumeration, or nil if handle is invalid")
{
int idx;
if (!ade_get_args(L, "o", l_ShipRegistryEntry.Get(&idx)))
return ADE_RETURN_NIL;
if (idx < 0 || (size_t)idx >= Ship_registry.size())
return ADE_RETURN_NIL;
if (ADE_SETTING_VAR)
LuaError(L, "This property is read only.");
return ade_set_args(L, "o", l_Enum.Set(enum_h(static_cast<lua_enum>(LE_INVALID + (int)Ship_registry[idx].status))));
}
ADE_FUNC(getParsedShip, l_ShipRegistryEntry, nullptr, "Return the parsed ship associated with this ship registry entry", "parse_object", "The parsed ship, or nil if handle is invalid. If this ship entry is for a ship-create'd ship, the returned handle may be invalid.")
{
int idx;
if (!ade_get_args(L, "o", l_ShipRegistryEntry.Get(&idx)))
return ADE_RETURN_NIL;
if (idx < 0 || (size_t)idx >= Ship_registry.size())
return ADE_RETURN_NIL;
return ade_set_args(L, "o", l_ParseObject.Set(parse_object_h(Ship_registry[idx].p_objp_or_null())));
}
ADE_FUNC(getShip, l_ShipRegistryEntry, nullptr, "Return the ship associated with this ship registry entry", "ship", "The ship, or nil if handle is invalid. The returned handle will be invalid if the ship has not yet arrived in-mission.")
{
int idx;
if (!ade_get_args(L, "o", l_ShipRegistryEntry.Get(&idx)))
return ADE_RETURN_NIL;
if (idx < 0 || (size_t)idx >= Ship_registry.size())
return ADE_RETURN_NIL;
return ade_set_args(L, "o", l_Ship.Set(object_h(Ship_registry[idx].objnum)));
}
}
}