Files
fs2open.github.com/code/scripting/api/libs/hookvars.cpp
T
asarium 8f4d51b14a Convert ade_get_args to variadic template based solution
This maintains the type information which can be used for enforce
const-correctness and also for implementing more advanced ways of
handling the type information.
2018-09-26 19:15:42 +02:00

129 lines
2.7 KiB
C++

//
//
#include "hookvars.h"
namespace scripting {
namespace api {
//**********LIBRARY: Scripting Variables
ADE_LIB(l_HookVar, "HookVariables", "hv", "Hook variables repository");
//WMC: IMPORTANT
//Be very careful when modifying this library, as the Globals[] library does depend
//on the current number of items in the library. If you add _anything_, modify __len.
//Or run changes by me.
//*****LIBRARY: Scripting Variables
ADE_LIB_DERIV(l_HookVar_Globals, "Globals", nullptr, nullptr, l_HookVar);
ADE_INDEXER(l_HookVar_Globals, "number Index", "Array of current HookVariable names", "string", "Hookvariable name, or empty string if invalid index specified")
{
int idx;
if(!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "s", "");
//Get lib
lua_getglobal(L, l_HookVar.GetName());
int lib_ldx = lua_gettop(L);
if(!lua_isuserdata(L, lib_ldx))
{
lua_pop(L, 1);
return ade_set_error(L, "s", "");
}
//Get metatable
lua_getmetatable(L, lib_ldx);
int mtb_ldx = lua_gettop(L);
if(!lua_istable(L, mtb_ldx))
{
lua_pop(L, 2);
return ade_set_error(L, "s", "");
}
//Get ade members table
lua_pushstring(L, "__ademembers");
lua_rawget(L, mtb_ldx);
int amt_ldx = lua_gettop(L);
if(!lua_istable(L, amt_ldx))
{
lua_pop(L, 3);
return ade_set_error(L, "s", "");
}
//List 'em
int count = 1;
lua_pushnil(L);
while(lua_next(L, amt_ldx))
{
//Now on stack: Key, value
lua_pushvalue(L, -2);
auto keyname = lua_tostring(L, -1);
if(strcmp(keyname, "Globals") != 0)
{
if(count == idx)
{
//lib, mtb, amt, key, value, string go bye-bye
lua_pop(L, 5);
return ade_set_args(L, "s", keyname);
}
count++;
}
lua_pop(L, 2); //Value, string
}
lua_pop(L, 3); //lib, mtb, amt
return ade_set_error(L, "s", "");
}
ADE_FUNC(__len, l_HookVar_Globals, NULL, "Number of HookVariables", "number", "Number of HookVariables")
{
//Get metatable
lua_getglobal(L, l_HookVar.GetName());
int lib_ldx = lua_gettop(L);
if(!lua_isuserdata(L, lib_ldx))
{
lua_pop(L, 1);
return ade_set_error(L, "i", 0);
}
lua_getmetatable(L, lib_ldx);
int mtb_ldx = lua_gettop(L);
if(!lua_istable(L, mtb_ldx))
{
lua_pop(L, 2);
return ade_set_error(L, "i", 0);
}
//Get ade members table
lua_pushstring(L, "__ademembers");
lua_rawget(L, mtb_ldx);
int amt_ldx = lua_gettop(L);
if(!lua_istable(L, amt_ldx))
{
lua_pop(L, 3);
return ade_set_error(L, "i", 0);
}
int total_len = 0;
lua_pushnil(L);
while(lua_next(L, amt_ldx))
{
total_len++;
lua_pop(L, 1); //value
}
size_t num_sub = ade_manager::getInstance()->getEntry(l_HookVar.GetIdx()).Num_subentries;
lua_pop(L, 3);
//WMC - Return length, minus the 'Globals' library
return ade_set_args(L, "i", (int)(total_len - num_sub));
}
}
}