mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-08 17:48:46 -10:00
* 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]>
133 lines
3.0 KiB
C++
133 lines
3.0 KiB
C++
//
|
|
//
|
|
|
|
#include "LuaEnum.h"
|
|
|
|
namespace scripting {
|
|
namespace api {
|
|
|
|
lua_enum_h::lua_enum_h() : lua_enum(-1) {}
|
|
|
|
lua_enum_h::lua_enum_h(int idx) : lua_enum(idx) {}
|
|
|
|
dynamic_sexp_enum_list* lua_enum_h::getEnum() const
|
|
{
|
|
return &Dynamic_enums[lua_enum];
|
|
}
|
|
|
|
bool lua_enum_h::isValid() const
|
|
{
|
|
return SCP_vector_inbounds(Dynamic_enums, lua_enum);
|
|
}
|
|
|
|
ADE_OBJ(l_LuaEnum, lua_enum_h, "LuaEnum", "Lua Enum handle");
|
|
|
|
ADE_VIRTVAR(Name, l_LuaEnum, "string", "The enum name", "string", "The enum name or nil if handle is invalid")
|
|
{
|
|
lua_enum_h lua_enum;
|
|
const char* enum_name;
|
|
if (!ade_get_args(L, "o|s", l_LuaEnum.Get(&lua_enum), &enum_name))
|
|
return ade_set_error(L, "o", l_LuaEnum.Set(lua_enum_h()));
|
|
|
|
if (!lua_enum.isValid()) {
|
|
return ADE_RETURN_NIL;
|
|
}
|
|
|
|
if (ADE_SETTING_VAR) {
|
|
LuaError(L, "This property is read only.");
|
|
}
|
|
|
|
return ade_set_args(L, "s", lua_enum.getEnum()->name.c_str());
|
|
}
|
|
|
|
ADE_INDEXER(l_LuaEnum, "number Index", "Array of enum items", "string", "enum item string, or nil if index or enum handle is invalid")
|
|
{
|
|
lua_enum_h lua_enum;
|
|
int idx = -1;
|
|
if (!ade_get_args(L, "o|i", l_LuaEnum.Get(&lua_enum), &idx))
|
|
return ADE_RETURN_NIL;
|
|
|
|
if (!lua_enum.isValid()) {
|
|
return ADE_RETURN_NIL;
|
|
}
|
|
|
|
idx--; //Convert from lua 1 based index
|
|
if ((idx < 0) || (idx >= static_cast<int>(lua_enum.getEnum()->list.size())))
|
|
return ADE_RETURN_NIL;
|
|
|
|
return ade_set_args(L, "s", lua_enum.getEnum()->list[idx].c_str());
|
|
}
|
|
|
|
ADE_FUNC(__len, l_LuaEnum, nullptr, "The number of Lua enum items", "number", "The number of Lua enums item or nil if handle is invalid")
|
|
{
|
|
lua_enum_h lua_enum;
|
|
if (!ade_get_args(L, "o", l_LuaEnum.Get(&lua_enum)))
|
|
return ade_set_error(L, "i", 0);
|
|
|
|
if (!lua_enum.isValid()) {
|
|
return ADE_RETURN_NIL;
|
|
}
|
|
|
|
return ade_set_args(L, "i", static_cast<int>(lua_enum.getEnum()->list.size()));
|
|
}
|
|
|
|
ADE_FUNC(addEnumItem,
|
|
l_LuaEnum,
|
|
"string itemname",
|
|
"Adds an enum item with the given string.",
|
|
"boolean",
|
|
"Returns true if successful, false otherwise")
|
|
{
|
|
lua_enum_h lua_enum;
|
|
const char* item_name;
|
|
if (!ade_get_args(L, "os", l_LuaEnum.Get(&lua_enum) , &item_name))
|
|
return ADE_RETURN_FALSE;
|
|
|
|
if (!lua_enum.isValid()) {
|
|
return ADE_RETURN_FALSE;
|
|
}
|
|
|
|
for (size_t i = 0; i < lua_enum.getEnum()->list.size(); i++) {
|
|
if (!stricmp(item_name, lua_enum.getEnum()->list[i].c_str())) {
|
|
return ADE_RETURN_FALSE;
|
|
}
|
|
}
|
|
|
|
lua_enum.getEnum()->list.push_back(item_name);
|
|
|
|
return ADE_RETURN_TRUE;
|
|
}
|
|
|
|
ADE_FUNC(removeEnumItem,
|
|
l_LuaEnum,
|
|
"string itemname",
|
|
"Removes an enum item with the given string.",
|
|
"boolean",
|
|
"Returns true if successful, false otherwise")
|
|
{
|
|
lua_enum_h lua_enum;
|
|
const char* item_name;
|
|
if (!ade_get_args(L, "os", l_LuaEnum.Get(&lua_enum), &item_name))
|
|
return ADE_RETURN_FALSE;
|
|
|
|
if (!lua_enum.isValid()) {
|
|
return ADE_RETURN_FALSE;
|
|
}
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < lua_enum.getEnum()->list.size(); i++) {
|
|
if (!stricmp(item_name, lua_enum.getEnum()->list[i].c_str())) {
|
|
lua_enum.getEnum()->list.erase(lua_enum.getEnum()->list.begin() + i);
|
|
return ADE_RETURN_TRUE;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return ADE_RETURN_FALSE;
|
|
}
|
|
|
|
} // namespace api
|
|
} // namespace scripting
|