Files
fs2open.github.com/code/scripting/api/objs/intelentry.cpp
T
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

193 lines
5.0 KiB
C++

//
//
#include "intelentry.h"
#include "menuui/techmenu.h"
namespace scripting {
namespace api {
//**********HANDLE: Intelentry
ADE_OBJ(l_Intelentry, int, "intel_entry", "Intel entry handle");
ADE_OBJ_VALIDATOR_RANGE(l_Intelentry, intel_info_size());
ADE_FUNC(__tostring, l_Intelentry, nullptr, "Intel entry name", "string", "Intel entry name, or an empty string if handle is invalid")
{
int idx;
const char* s = nullptr;
if(!ade_get_args(L, "o|s", l_Intelentry.Get(&idx), &s))
return ade_set_error(L, "s", "");
if(idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", Intel_info[idx].name);
}
ADE_FUNC(__eq, l_Intelentry, "intel_entry, intel_entry", "Checks if the two entries are equal", "boolean", "true if equal, false otherwise")
{
int idx1,idx2;
if(!ade_get_args(L, "oo", l_Intelentry.Get(&idx1), l_Intelentry.Get(&idx2)))
return ade_set_error(L, "b", false);
if(idx1 < 0 || idx1 >= intel_info_size())
return ade_set_error(L, "b", false);
if(idx2 < 0 || idx2 >= intel_info_size())
return ade_set_error(L, "b", false);
return ade_set_args(L, "b", idx1 == idx2);
}
ADE_VIRTVAR(Name, l_Intelentry, "string", "Intel entry name", "string", "Intel entry name, or an empty string if handle is invalid")
{
int idx;
const char* s = nullptr;
if(!ade_get_args(L, "o|s", l_Intelentry.Get(&idx), &s))
return ade_set_error(L, "s", "");
if(idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "s", "");
if(ADE_SETTING_VAR && s != nullptr) {
auto len = sizeof(Intel_info[idx].name);
strncpy(Intel_info[idx].name, s, len);
Intel_info[idx].name[len - 1] = 0;
}
return ade_set_args(L, "s", Intel_info[idx].name);
}
ADE_VIRTVAR(Description, l_Intelentry, "string", "Intel entry description", "string", "Description, or empty string if handle is invalid")
{
int idx;
const char* s = nullptr;
if(!ade_get_args(L, "o|s", l_Intelentry.Get(&idx), &s))
return ade_set_error(L, "s", "");
if(idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "s", "");
if(ADE_SETTING_VAR) {
if(s != nullptr) {
Intel_info[idx].desc = s;
} else {
Intel_info[idx].desc = "";
}
}
return ade_set_args(L, "s", Intel_info[idx].desc);
}
ADE_VIRTVAR(AnimFilename, l_Intelentry, "string", "Intel entry animation filename", "string", "Filename, or empty string if handle is invalid")
{
int idx;
const char* s = nullptr;
if(!ade_get_args(L, "o|s", l_Intelentry.Get(&idx), &s))
return ade_set_error(L, "s", "");
if(idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "s", "");
if(ADE_SETTING_VAR) {
if(s != nullptr) {
auto len = sizeof(Intel_info[idx].anim_filename);
strncpy(Intel_info[idx].anim_filename, s, len);
Intel_info[idx].anim_filename[len - 1] = 0;
} else {
strcpy_s(Intel_info[idx].anim_filename, "");
}
}
return ade_set_args(L, "s", Intel_info[idx].anim_filename);
}
ADE_VIRTVAR(InTechDatabase, l_Intelentry, "boolean", "Gets or sets whether this intel entry is visible in the tech room", "boolean", "True or false")
{
int idx;
bool new_value;
if (!ade_get_args(L, "o|b", l_Intelentry.Get(&idx), &new_value))
return ade_set_error(L, "b", false);
if (idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "b", false);
if (ADE_SETTING_VAR) {
if (new_value) {
Intel_info[idx].flags |= IIF_IN_TECH_DATABASE;
} else {
Intel_info[idx].flags &= ~IIF_IN_TECH_DATABASE;
}
}
return ade_set_args(L, "b", (Intel_info[idx].flags & IIF_IN_TECH_DATABASE) != 0);
}
ADE_VIRTVAR(CustomData,
l_Intelentry,
nullptr,
"Gets the custom data table for this entry",
"table",
"The entry's custom data table")
{
int idx;
const char* s = nullptr;
if (!ade_get_args(L, "o|s", l_Intelentry.Get(&idx), &s))
return ade_set_error(L, "s", "");
if (idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "s", "");
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
auto table = luacpp::LuaTable::create(L);
for (const auto& pair : Intel_info[idx].custom_data) {
table.addValue(pair.first, pair.second);
}
return ade_set_args(L, "t", &table);
}
ADE_FUNC(hasCustomData,
l_Intelentry,
nullptr,
"Detects whether the entry has any custom data",
"boolean",
"true if the entry's custom_data is not empty, false otherwise")
{
int idx;
const char* s = nullptr;
if (!ade_get_args(L, "o|s", l_Intelentry.Get(&idx), &s))
return ade_set_error(L, "s", "");
if (idx < 0 || idx >= intel_info_size())
return ade_set_error(L, "s", "");
if (ADE_SETTING_VAR) {
LuaError(L, "This property is read only.");
}
bool result = !Intel_info[idx].custom_data.empty();
return ade_set_args(L, "b", result);
}
ADE_FUNC(getIntelEntryIndex, l_Intelentry, nullptr, "Gets the index value of the intel entry", "number", "index value of the intel entry")
{
int idx;
if(!ade_get_args(L, "o", l_Intelentry.Get(&idx)))
return ade_set_args(L, "i", -1);
if(idx < 0 || idx >= intel_info_size())
return ade_set_args(L, "i", -1);
return ade_set_args(L, "i", idx + 1); // Lua is 1-based
}
}
}