Made VScript's enum registration use direct API functions to create constant tables instead of declaring them in string literal code

This commit is contained in:
Blixibon 2021-09-19 21:09:25 -05:00
parent d629fac2b5
commit 0368abaf29

View File

@ -2669,44 +2669,28 @@ void SquirrelVM::RegisterEnum(ScriptEnumDesc_t* pEnumDesc)
if (!pEnumDesc) if (!pEnumDesc)
return; return;
sq_newtableex(vm_, pEnumDesc->m_ConstantBindings.Count());
sq_pushconsttable(vm_); sq_pushconsttable(vm_);
sq_pushstring(vm_, pEnumDesc->m_pszScriptName, -1); sq_pushstring(vm_, pEnumDesc->m_pszScriptName, -1);
sq_push(vm_, -3);
// Check if class name is already taken sq_rawset(vm_, -3);
if (sq_get(vm_, -2) == SQ_OK)
{
HSQOBJECT obj;
sq_resetobject(&obj);
sq_getstackobj(vm_, -1, &obj);
if (!sq_isnull(obj))
{
sq_pop(vm_, 2);
return;
}
}
sq_pop(vm_, 1);
// HACKHACK: I have no idea how to declare enums with the current API.
// For now, we'll just cram everything into a script buffer and compile it. (Blixibon)
char szScript[2048];
V_snprintf( szScript, sizeof(szScript), "enum %s {\n", pEnumDesc->m_pszScriptName );
for (int i = 0; i < pEnumDesc->m_ConstantBindings.Count(); ++i) for (int i = 0; i < pEnumDesc->m_ConstantBindings.Count(); ++i)
{ {
auto& scriptConstant = pEnumDesc->m_ConstantBindings[i]; auto& scriptConstant = pEnumDesc->m_ConstantBindings[i];
sq_pushstring(vm_, scriptConstant.m_pszScriptName, -1);
PushVariant(vm_, scriptConstant.m_data);
sq_rawset(vm_, -4);
char szValue[64]; char szValue[64];
GetVariantScriptString( scriptConstant.m_data, szValue, sizeof(szValue) ); GetVariantScriptString( scriptConstant.m_data, szValue, sizeof(szValue) );
V_snprintf( szScript, sizeof(szScript), "%s%s = %s\n", szScript, scriptConstant.m_pszScriptName, szValue );
RegisterConstantDocumentation(vm_, &scriptConstant, szValue, pEnumDesc); RegisterConstantDocumentation(vm_, &scriptConstant, szValue, pEnumDesc);
} }
V_strcat_safe( szScript, "}" ); sq_pop(vm_, 2);
Run( szScript );
RegisterEnumDocumentation(vm_, pEnumDesc); RegisterEnumDocumentation(vm_, pEnumDesc);
} }