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

114 lines
2.4 KiB
C++

#include "rank.h"
#include "stats/scoring.h"
namespace scripting {
namespace api {
rank_h::rank_h() : rank(-1) {}
rank_h::rank_h(int l_rank) : rank(l_rank) {}
rank_stuff* rank_h::getRank() const
{
if (!isValid())
return nullptr;
return &Ranks[rank];
}
bool rank_h::isValid() const
{
return rank >= 0 && rank < (int)Ranks.size();
}
//**********HANDLE: rank
ADE_OBJ(l_Rank, rank_h, "rank", "Rank handle");
ADE_VIRTVAR(Name, l_Rank, nullptr, "The name of the rank", "string", "The name")
{
rank_h current;
if (!ade_get_args(L, "o", l_Rank.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.getRank()->name);
}
ADE_VIRTVAR(AltName, l_Rank, nullptr, "The alt name of the rank", "string", "The alt name")
{
rank_h current;
if (!ade_get_args(L, "o", l_Rank.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.getRank()->alt_name.c_str());
}
ADE_VIRTVAR(Title, l_Rank, nullptr, "The title of the rank", "string", "The title")
{
rank_h current;
if (!ade_get_args(L, "o", l_Rank.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.getRank()->title.c_str());
}
ADE_VIRTVAR(Bitmap, l_Rank, nullptr, "The bitmap of the rank", "string", "The bitmap")
{
rank_h current;
if (!ade_get_args(L, "o", l_Rank.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.getRank()->bitmap);
}
ADE_VIRTVAR(Index, l_Rank, nullptr, "The index of the rank within the Ranks list", "number", "The rank index")
{
rank_h current;
if (!ade_get_args(L, "o", l_Rank.Get(&current))) {
return ADE_RETURN_NIL;
}
if (!current.isValid()) {
return ade_set_error(L, "i", 0);
}
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
return ade_set_args(L, "i", current.rank + 1); // Convert to Lua's 1 based index system
}
} // namespace api
} // namespace scripting