Script: Add GetProperties function to get the keys of a proplist

Günther Brammer 2012-05-26 01:32:43 +02:00
parent 62bc721a65
commit 4e71af5489
11 changed files with 109 additions and 1 deletions

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>GetProperties</title>
<category>Objects</category>
<subcat>Properties</subcat>
<version>5.3 OC</version>
<syntax>
<rtype>array</rtype>
<params>
<param>
<type>proplist</type>
<name>object</name>
<desc>Object to request property from, <code>nil</code> in local calls.</desc>
<optional />
</param>
</params>
</syntax>
<desc>Returns the names of all properties of <code>object</code>.</desc>
<related><funclink>SetProperty</funclink></related>
<related><funclink>GetProperty</funclink></related>
<examples>
<example>
<code>GetProperties({foo = 1, bar = 2}) == ["bar", "foo"]</code>
</example>
</examples>
</func>
<author>Günther</author><date>2012</date>
</funcs>

View File

@ -27,6 +27,7 @@
</syntax>
<desc>Returns the property <code>key</code> of <code>object</code>.</desc>
<related><funclink>SetProperty</funclink></related>
<related><funclink>GetProperties</funclink></related>
</func>
<author>Günther</author><date>2009-05</date>
</funcs>

View File

@ -556,6 +556,20 @@ bool C4Effect::GetPropertyByS(C4String *k, C4Value *pResult) const
return C4PropListNumbered::GetPropertyByS(k, pResult);
}
C4ValueArray * C4Effect::GetProperties() const
{
C4ValueArray * a = C4PropList::GetProperties();
int i;
i = a->GetSize();
a->SetSize(i + 5);
(*a)[i++] = C4VString(&::Strings.P[P_Name]);
(*a)[i++] = C4VString(&::Strings.P[P_Priority]);
(*a)[i++] = C4VString(&::Strings.P[P_Interval]);
(*a)[i++] = C4VString(&::Strings.P[P_CommandTarget]);
(*a)[i++] = C4VString(&::Strings.P[P_Time]);
return a;
}
// Some other, internal effects -------------------------------------------------------------
static int32_t GetSmokeLevel()

View File

@ -133,6 +133,7 @@ public:
virtual void SetPropertyByS(C4String * k, const C4Value & to);
virtual void ResetProperty(C4String * k);
virtual bool GetPropertyByS(C4String *k, C4Value *pResult) const;
virtual C4ValueArray * GetProperties() const;
protected:
void TempRemoveUpperEffects(C4Object *pObj, bool fTempRemoveThis, C4Effect **ppLastRemovedEffect); // temp remove all effects with higher priority

View File

@ -4957,3 +4957,13 @@ bool C4Object::GetPropertyByS(C4String *k, C4Value *pResult) const
}
return C4PropListNumbered::GetPropertyByS(k, pResult);
}
C4ValueArray * C4Object::GetProperties() const
{
C4ValueArray * a = C4PropList::GetProperties();
int i;
i = a->GetSize();
a->SetSize(i + 1);
(*a)[i++] = C4VString(&::Strings.P[P_Plane]);
return a;
}

View File

@ -409,6 +409,7 @@ public:
virtual void SetPropertyByS(C4String * k, const C4Value & to);
virtual void ResetProperty(C4String * k);
virtual bool GetPropertyByS(C4String *k, C4Value *pResult) const;
virtual C4ValueArray * GetProperties() const;
};
#endif

View File

@ -533,6 +533,30 @@ int32_t C4PropList::GetPropertyInt(C4PropertyName n) const
return 0;
}
C4ValueArray * C4PropList::GetProperties() const
{
C4ValueArray * a;
int i;
if (prototype)
{
a = prototype->GetProperties();
i = a->GetSize();
a->SetSize(i + Properties.GetSize());
}
else
{
a = new C4ValueArray(Properties.GetSize());
i = 0;
}
const C4Property * p = Properties.First();
while (p)
{
(*a)[i++] = C4VString(p->Key);
p = Properties.Next(p);
}
return a;
}
void C4PropList::SetPropertyByS(C4String * k, const C4Value & to)
{
assert(!constant);

View File

@ -70,9 +70,10 @@ public:
// some proplists have references that are not reference-counted
virtual bool Delete() { return false; }
// These three operate on properties as seen by script, which can be dynamic
// These four operate on properties as seen by script, which can be dynamic
// or reflect C++ variables
virtual bool GetPropertyByS(C4String *k, C4Value *pResult) const;
virtual C4ValueArray * GetProperties() const;
// not allowed on frozen proplists
virtual void SetPropertyByS(C4String * k, const C4Value & to);
virtual void ResetProperty(C4String * k);

View File

@ -205,6 +205,15 @@ static C4Value FnResetProperty_C4V(C4PropList * _this, C4Value * key_C4V, C4Valu
return C4VTrue;
}
static C4ValueArray * FnGetProperties(C4PropList * _this, C4PropList * p)
{
if (!p) p = _this;
if (!p) throw new NeedNonGlobalContext("GetProperties");
C4ValueArray * r = p->GetProperties();
r->SortStrings();
return r;
}
static C4Value FnLog_C4V(C4PropList * _this, C4Value *szMessage, C4Value * iPar0, C4Value * iPar1, C4Value * iPar2, C4Value * iPar3, C4Value * iPar4, C4Value * iPar5, C4Value * iPar6, C4Value * iPar7, C4Value * iPar8)
{
Log(FnStringFormat(_this, FnStringPar(szMessage->getStr()),iPar0,iPar1,iPar2,iPar3,iPar4,iPar5,iPar6,iPar7,iPar8).getData());
@ -622,6 +631,7 @@ void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
AddFunc(pEngine, "CreateArray", FnCreateArray);
AddFunc(pEngine, "CreatePropList", FnCreatePropList);
AddFunc(pEngine, "GetProperties", FnGetProperties);
AddFunc(pEngine, "C4Id", FnC4Id, false);
AddFunc(pEngine, "Distance", FnDistance);
AddFunc(pEngine, "Angle", FnAngle);

View File

@ -104,6 +104,19 @@ void C4ValueArray::Sort(class C4SortObject &rSort)
std::stable_sort(pData, pData+iSize, C4SortObjectSTL(rSort));
}
struct C4ValueArraySortStringscomp
{
bool operator ()(const C4Value &v1, const C4Value &v2)
{
return v1.getStr() && v2.getStr() && v1._getStr()->GetData() < v2._getStr()->GetData();
}
};
void C4ValueArray::SortStrings()
{
std::stable_sort(pData, pData+iSize, C4ValueArraySortStringscomp());
}
C4Value &C4ValueArray::operator[](int32_t iElem)
{
assert(iElem < MaxSize);

View File

@ -76,6 +76,7 @@ public:
void SetSlice(int32_t startIndex, int32_t endIndex, const C4Value &Val);
void Sort(class C4SortObject &rSort);
void SortStrings();
private:
// Reference counter