Currently, expression handling in XML is spotting and inconsistent.
Each actor type needs to evaluate attributes; only a few do. Strings are handled with the "@foo()" notation. Instead, after loading an XML file for loading as an actor, precompile all XML attributes as Lua expressions. The primary difference to code is: - previously, Attr="string" would act as a string if it was being read as one. Now, specify Attr='"string"'. Attr="@string" is a shorthand. (note that @strings will not be parsed as Lua strings, so can not use Lua escape characters) - previously, Attr="@func()" would evaluate func(), and the value of the attribute would be the return value of the function. This was only supported for a few attributes. Now, say Attr="func()", and this will work for all fields. - Attributes of other types are unchanged, except that all attributes may be Lua expressions, eg. Width="SCREEN_WIDTH". - Attributes names ending with "Command" are special, like metrics. Prefix "%" to disable this. This brings XML handling in line with metric handling. (This is an intermediary step; I have another idea that follows from this that should eliminate the annoying "'foo'"/"@foo".)
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <map>
|
||||
struct DateTime;
|
||||
class RageFileBasic;
|
||||
struct lua_State;
|
||||
|
||||
class XNodeValue
|
||||
{
|
||||
@@ -18,6 +19,7 @@ public:
|
||||
virtual void GetValue( float &out ) const = 0;
|
||||
virtual void GetValue( bool &out ) const = 0;
|
||||
virtual void GetValue( unsigned &out ) const = 0;
|
||||
virtual void PushValue( lua_State *L ) const = 0;
|
||||
|
||||
template<typename T>
|
||||
T GetValue() const { T val; GetValue(val); return val; }
|
||||
@@ -26,6 +28,7 @@ public:
|
||||
virtual void SetValue( int v ) = 0;
|
||||
virtual void SetValue( float v ) = 0;
|
||||
virtual void SetValue( unsigned v ) = 0;
|
||||
virtual void SetValueFromStack( lua_State *L ) = 0;
|
||||
};
|
||||
|
||||
class XNodeStringValue: public XNodeValue
|
||||
@@ -40,11 +43,13 @@ public:
|
||||
void GetValue( float &out ) const;
|
||||
void GetValue( bool &out ) const;
|
||||
void GetValue( unsigned &out ) const;
|
||||
void PushValue( lua_State *L ) const;
|
||||
|
||||
void SetValue( const RString &v );
|
||||
void SetValue( int v );
|
||||
void SetValue( float v );
|
||||
void SetValue( unsigned v );
|
||||
void SetValueFromStack( lua_State *L );
|
||||
};
|
||||
|
||||
typedef map<RString,XNodeValue*> XAttrs;
|
||||
@@ -98,12 +103,14 @@ public:
|
||||
XNodeValue *GetAttr( const RString &sAttrName );
|
||||
template <typename T>
|
||||
bool GetAttrValue( const RString &sName, T &out ) const { const XNodeValue *pAttr=GetAttr(sName); if(pAttr==NULL) return false; pAttr->GetValue(out); return true; }
|
||||
bool PushAttrValue( lua_State *L, const RString &sName ) const;
|
||||
|
||||
// in one level child nodes
|
||||
const XNode *GetChild( const RString &sName ) const;
|
||||
XNode *GetChild( const RString &sName );
|
||||
template <typename T>
|
||||
bool GetChildValue( const RString &sName, T &out ) const { const XNode *pChild=GetChild(sName); if(pChild==NULL) return false; pChild->GetValue(out); return true; }
|
||||
bool PushChildValue( lua_State *L, const RString &sName ) const;
|
||||
|
||||
// modify DOM
|
||||
template <typename T>
|
||||
|
||||
Reference in New Issue
Block a user