From 5791ee68a7dd6946d30b3ab55b63987942a7b289 Mon Sep 17 00:00:00 2001 From: AzaezelX Date: Sun, 24 Dec 2023 03:15:56 -0600 Subject: [PATCH] adds a getFieldCountNS and getFieldNS method to allow field lookup by namespace --- Engine/source/console/simObject.cpp | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/Engine/source/console/simObject.cpp b/Engine/source/console/simObject.cpp index 92080dff1..c3a45c3be 100644 --- a/Engine/source/console/simObject.cpp +++ b/Engine/source/console/simObject.cpp @@ -3326,6 +3326,33 @@ DefineEngineMethod( SimObject, getFieldCount, S32, (),, return list.size() - numDummyEntries; } +DefineEngineFunction(getFieldCountNS, S32, (StringTableEntry className), , + "Get the number of static fields on the name space.\n" + "@return The number of static fields defined on the object.") +{ + Namespace* ns = Con::lookupNamespace(className); + if (!ns) + return 0; + AbstractClassRep* rep = ns->mClassRep; + if (!rep) + return 0; + + const AbstractClassRep::FieldList& list = rep->mFieldList; + const AbstractClassRep::Field* f; + U32 numDummyEntries = 0; + + for (S32 i = 0; i < list.size(); i++) + { + f = &list[i]; + + // The special field types do not need to be counted. + if (f->type >= AbstractClassRep::ARCFirstCustomField || f->flag.test(AbstractClassRep::FieldFlags::FIELD_ComponentInspectors)) + numDummyEntries++; + } + + return list.size() - numDummyEntries; +} + //----------------------------------------------------------------------------- DefineEngineMethod( SimObject, getField, const char*, ( S32 index ),, @@ -3357,6 +3384,42 @@ DefineEngineMethod( SimObject, getField, const char*, ( S32 index ),, return ""; } +DefineEngineFunction(getFieldNS, const char*, (StringTableEntry className,S32 index), , + "Retrieve the value of a static field by index.\n" + "@param index The index of the static field.\n" + "@return The value of the static field with the given index or \"\".") +{ + Namespace* ns = Con::lookupNamespace(className); + if (!ns) + return 0; + AbstractClassRep* rep = ns->mClassRep; + if (!rep) + return 0; + + const AbstractClassRep::FieldList& list = rep->mFieldList; + if ((index < 0) || (index >= list.size())) + return ""; + + const AbstractClassRep::Field* f; + S32 currentField = 0; + for (U32 i = 0; i < list.size() && currentField <= index; i++) + { + f = &list[i]; + + // The special field types can be skipped. + if (f->type >= AbstractClassRep::ARCFirstCustomField || f->flag.test(AbstractClassRep::FieldFlags::FIELD_ComponentInspectors)) + continue; + + if (currentField == index) + return f->pFieldname; + + currentField++; + } + + // if we found nada, return nada. + return ""; +} + DefineEngineFunction(getClassHierarchy, const char*, (const char* name), , "Returns the inheritance hierarchy for a given class.") {