1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-19 12:06:07 +08:00

Added original SDK code for Alien Swarm.

This commit is contained in:
Scott Ehlert
2010-07-22 01:46:14 -05:00
commit c0a96ff1e8
3740 changed files with 1243478 additions and 0 deletions

View File

@ -0,0 +1,252 @@
//====== Copyright <20> 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#ifndef DMXATTRIBUTE_H
#define DMXATTRIBUTE_H
#ifdef _WIN32
#pragma once
#endif
#include "datamodel/dmattributetypes.h"
#include "tier1/utlvector.h"
#include "tier1/utlrbtree.h"
#include "tier1/utlsymbol.h"
#include "tier1/mempool.h"
#include "dmxloader/dmxloader.h"
//-----------------------------------------------------------------------------
// Forward declarations:
//-----------------------------------------------------------------------------
class CDmxElement;
#define DECLARE_DMX_ATTRIBUTE_TYPE_INTERNAL( _className, _storageType, _attributeType, _attributeName, _defaultSetStatement ) \
template< > class CDmAttributeInfo< _className > \
{ \
private: \
enum { ATTRIBUTE_TYPE = _attributeType }; \
typedef _storageType StorageType_t; \
static DmAttributeType_t AttributeType() { return _attributeType; } \
static const char *AttributeTypeName() { return _attributeName; } \
static void SetDefaultValue( _className& value ) { _defaultSetStatement } \
friend class CDmxAttribute; \
friend class CDmxElement; \
}; \
#define DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE_INTERNAL( _className, _storageType, _attributeType, _attributeName ) \
template< > class CDmAttributeInfo< CUtlVector<_className> > \
{ \
private: \
enum { ATTRIBUTE_TYPE = _attributeType }; \
typedef _storageType StorageType_t; \
static DmAttributeType_t AttributeType() { return _attributeType; } \
static const char *AttributeTypeName() { return _attributeName; } \
static void SetDefaultValue( CUtlVector< _className >& value ) { value.RemoveAll(); } \
friend class CDmxAttribute; \
friend class CDmxElement; \
}; \
#define DECLARE_DMX_ATTRIBUTE_TYPE( _className, _attributeType, _attributeName, _defaultSetStatement ) \
DECLARE_DMX_ATTRIBUTE_TYPE_INTERNAL( _className, _className, _attributeType, _attributeName, _defaultSetStatement )
#define DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE( _className, _attributeType, _attributeName )\
DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE_INTERNAL( _className, CUtlVector< _className >, _attributeType, _attributeName )
//-----------------------------------------------------------------------------
// Attribute info, modified for use in mod code
//-----------------------------------------------------------------------------
DECLARE_DMX_ATTRIBUTE_TYPE( CDmxElement*, AT_ELEMENT, "element", value = 0; )
DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE( CDmxElement*, AT_ELEMENT_ARRAY, "element_array" )
DECLARE_DMX_ATTRIBUTE_TYPE( CUtlString, AT_STRING, "string", value.Set( NULL ); )
DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE( CUtlString, AT_STRING_ARRAY, "string_array" )
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CDmxAttribute
{
DECLARE_DMX_ALLOCATOR( );
public:
// Returns attribute name and type
DmAttributeType_t GetType() const;
const char *GetTypeString() const;
template< class T > bool IsA() const;
// Returns the name. NOTE: The utlsymbol
// can be turned into a string by using g_pDataModel->String();
const char *GetName() const;
CUtlSymbolLarge GetNameSymbol() const;
void SetName( const char *pName );
// Gets values
template< class T > const T& GetValue( ) const;
template< class T > const CUtlVector< T >& GetArray( ) const;
const char *GetValueString() const;
// Sets values (+ type)
template< class T > void SetValue( const T& value );
void SetValue( const char *pString );
void SetValue( char *pString );
void SetValue( const void *pBuffer, size_t nLen );
void SetValue( const CDmxAttribute *pAttribute );
// Method to set values in an array (just directly operate on the array)
// NOTE: This will create a new array of the appropriate type if
// the type doesn't match the current type
template< class T > CUtlVector< T >& GetArrayForEdit();
// Sets the attribute to its default value based on its type
void SetToDefaultValue();
// Convert to and from string
void SetValueFromString( const char *pValue );
const char *GetValueAsString( char *pBuffer, size_t nBufLen ) const;
// Gets the size of an array, returns 0 if it's not an array type
int GetArrayCount() const;
// Read from file
bool Unserialize( DmAttributeType_t type, CUtlBuffer &buf );
bool UnserializeElement( DmAttributeType_t type, CUtlBuffer &buf );
bool Serialize( CUtlBuffer &buf ) const;
bool SerializeElement( int nIndex, CUtlBuffer &buf ) const;
bool SerializesOnMultipleLines() const;
// Returns the size of the variables storing the various attribute types
static int AttributeDataSize( DmAttributeType_t type );
// Gets the basic type for a given array attribute type (e.g. AT_INT_ARRAY -> AT_INT)
static DmAttributeType_t ArrayAttributeBasicType( DmAttributeType_t type );
private:
CDmxAttribute( const char *pAttributeName );
CDmxAttribute( CUtlSymbolLarge attributeName );
~CDmxAttribute();
// Allocate, free memory for data
void AllocateDataMemory( DmAttributeType_t type );
void AllocateDataMemory_AndConstruct( DmAttributeType_t type );
void FreeDataMemory( );
// Untyped methods for getting/setting used by unpack
void SetValue( DmAttributeType_t type, const void *pSrc, int nLen );
// NOTE: [Get|Set]ArrayValue don't currently support AT_STRING_ARRAY, AT_STRING_VOID or AT_ELEMENT_ARRAY
void SetArrayValue( DmAttributeType_t type, const void *pSrc, int nDataTypeSize, int nArrayLength, int nSrcStride );
void GetArrayValue( DmAttributeType_t type, void *pDest, int nDataTypeSize, int nArrayLength, const char *pDefaultString = NULL ) const;
void SetArrayCount( int nArrayCount );
const void *GetArrayBase( void ) const;
// Helper templated methods called from untyped methods (VT is vector datatype, T is basic datatype, VT will be the same as T if the attribute is non-array)
template < class VT, class T > void ConstructDataMemory( void );
template < class VT, class T > void DestructDataMemory( void );
template < class VT, class T > void SetArrayCount( int nArrayCount );
template < class VT, class T > void GetArrayCount( int &nArrayCount ) const;
template < class VT, class T > void GetArrayBase( const void * &pBasePtr ) const;
template < class VT, class T > void SerializesOnMultipleLines( bool &bResult ) const;
template < class VT, class T > void SerializeType( bool &bSuccess, CUtlBuffer &buf ) const;
template < class VT, class T > void SerializeTypedElement( bool &bSuccess, int nIndex, CUtlBuffer &buf ) const;
template < class VT, class T > void UnserializeType( bool &bSuccess, CUtlBuffer &buf );
template < class VT, class T > void UnserializeTypedElement( bool &bSuccess, CUtlBuffer &buf );
template < class VT, class T > void SetDefaultValue( void );
DmAttributeType_t m_Type;
CUtlSymbolLarge m_Name;
void *m_pData;
static CUtlSymbolTableLargeMT s_AttributeNameSymbols;
friend class CDmxElement;
public:
static const char *s_pAttributeTypeName[AT_TYPE_COUNT];
};
//-----------------------------------------------------------------------------
// Inline methods
//-----------------------------------------------------------------------------
inline DmAttributeType_t CDmxAttribute::GetType() const
{
return m_Type;
}
template< class T > inline bool CDmxAttribute::IsA() const
{
return GetType() == CDmAttributeInfo< T >::ATTRIBUTE_TYPE;
}
inline CUtlSymbolLarge CDmxAttribute::GetNameSymbol() const
{
return m_Name;
}
//-----------------------------------------------------------------------------
// Sets a value in the attribute
//-----------------------------------------------------------------------------
template< class T > void CDmxAttribute::SetValue( const T& value )
{
AllocateDataMemory( CDmAttributeInfo<T>::AttributeType() );
CopyConstruct( (T*)m_pData, value );
}
//-----------------------------------------------------------------------------
// Returns data in the attribute
//-----------------------------------------------------------------------------
inline const char *CDmxAttribute::GetValueString() const
{
if ( m_Type == AT_STRING )
return *(CUtlString*)m_pData;
return "";
}
template< class T >
inline const T& CDmxAttribute::GetValue( ) const
{
if ( CDmAttributeInfo<T>::AttributeType() == m_Type )
return *(T*)m_pData;
static T defaultValue;
CDmAttributeInfo<T>::SetDefaultValue( defaultValue );
return defaultValue;
}
template< class T >
inline const CUtlVector< T >& CDmxAttribute::GetArray( ) const
{
if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
return *( CUtlVector< T > *)m_pData;
static CUtlVector<T> defaultArray;
return defaultArray;
}
template< class T >
inline CUtlVector< T >& CDmxAttribute::GetArrayForEdit( )
{
if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
return *( CUtlVector< T > *)m_pData;
AllocateDataMemory( CDmAttributeInfo< CUtlVector< T > >::AttributeType() );
Construct( (CUtlVector<T>*)m_pData );
return *(CUtlVector< T > *)m_pData;
}
#endif // DMXATTRIBUTE_H

View File

@ -0,0 +1,518 @@
//====== Copyright <20> 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#ifndef DMXELEMENT_H
#define DMXELEMENT_H
#ifdef _WIN32
#pragma once
#endif
#include "datamodel/dmattributetypes.h"
#include "tier1/utlvector.h"
#include "tier1/utlrbtree.h"
#include "tier1/utlsymbol.h"
#include "tier1/mempool.h"
#include "tier1/UtlSortVector.h"
#include "dmxloader/dmxattribute.h"
//-----------------------------------------------------------------------------
// Sort functor class for attributes
//-----------------------------------------------------------------------------
class CDmxAttributeLess
{
public:
bool Less( const CDmxAttribute * pAttribute1, const CDmxAttribute *pAttribute2, void *pContext )
{
return (pAttribute1?pAttribute1->GetNameSymbol():CUtlSymbolLarge(UTL_INVAL_SYMBOL_LARGE)) < (pAttribute2?pAttribute2->GetNameSymbol():CUtlSymbolLarge(UTL_INVAL_SYMBOL_LARGE));
}
};
enum BitfieldType_t
{
BITFIELD_TYPE_NONE,
BITFIELD_TYPE_BOOL,
BITFIELD_TYPE_CHAR,
BITFIELD_TYPE_UNSIGNED_CHAR,
BITFIELD_TYPE_BYTE = BITFIELD_TYPE_UNSIGNED_CHAR,
BITFIELD_TYPE_SHORT,
BITFIELD_TYPE_UNSIGNED_SHORT,
BITFIELD_TYPE_INT,
BITFIELD_TYPE_UNSIGNED_INT,
};
//-----------------------------------------------------------------------------
// Used to unpack elements into a structure. Does not recurse
// Also does not work with arrays.
//-----------------------------------------------------------------------------
struct DmxElementUnpackStructure_t
{
const char *m_pAttributeName;
const char *m_pDefaultString;
DmAttributeType_t m_AttributeType;
int m_nOffset;
int m_nSize; // If size is -1 the AT_STRING datatype is considered to be a UtlString, rather than a char array.
int m_nBitOffset; // Default value for this should be -1. A non-negative value indicates that the attribute is a bitfield and
// m_nSize should be interpreted as number of bits
BitfieldType_t m_BitfieldType; // the data type of your variable in the bitfield.
const void *m_pUserData; // If you want to associate some app-specific data with each field
// Embedded structure / Baseclass ptr
const char *m_pTypeName;
const DmxElementUnpackStructure_t *m_pSub;
int m_nArrayLength; // For fixed-size arrays, this is a positive value (default is 0). For arrays, m_nSize is the size of an array element and m_pDefaultString is the default element value.
};
#define NO_BIT_OFFSET -1
#define UTL_STRING_SIZE -1
#define NO_USER_DATA NULL
#define NO_EMBEDDED_TYPENAME NULL
#define NO_EMBEDDED_STRUCT_PTR NULL
#define NOT_A_BITFIELD BITFIELD_TYPE_NONE
#define NOT_AN_ARRAY 0
#define DECLARE_DMXELEMENT_UNPACK() \
template <typename T> friend DmxElementUnpackStructure_t *DmxElementUnpackInit(T *);
#define BEGIN_DMXELEMENT_UNPACK( _structName ) \
template <typename T> DmxElementUnpackStructure_t *DmxElementUnpackInit(T *); \
template <> DmxElementUnpackStructure_t *DmxElementUnpackInit<_structName>( _structName * ); \
namespace _structName##_UnpackInit \
{ \
static DmxElementUnpackStructure_t *s_pUnpack = DmxElementUnpackInit( (_structName *)NULL ); \
} \
\
template <> DmxElementUnpackStructure_t *DmxElementUnpackInit<_structName>( _structName * ) \
{ \
typedef _structName DestStructType_t; \
static DmxElementUnpackStructure_t unpack[] = \
{ \
#define DMXELEMENT_UNPACK_FLTX4( _attributeName, _defaultString, _varName ) \
{ _attributeName, _defaultString, CDmAttributeInfo<float>::AttributeType(), offsetof( DestStructType_t, _varName ), sizeof( fltx4 ), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_EMBEDDED( _typeName, _attributeName, _varName, _embeddedUnpackStructure ) \
{ _attributeName, "", AT_TYPE_COUNT, offsetof( DestStructType_t, _varName ), sizeof( ((DestStructType_t *)0)->_varName), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, _typeName, _embeddedUnpackStructure, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_BASECLASS( _structName, _baseClass ) \
{ "Baseclass unpack", "", AT_TYPE_COUNT, (int)static_cast< _baseClass * >( (_structName*)0), 0, NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, #_baseClass, DmxElementUnpackInit<_baseClass>( (_baseClass *)0 ), NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_BASECLASS_NAMESPACE( _namespace, _structName, _baseClass ) \
{ "Baseclass unpack", "", AT_TYPE_COUNT, (int)static_cast< _baseClass * >( (_structName*)0), 0, NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, #_baseClass, DmxElementUnpackInit##_namespace<_baseClass>( (_baseClass *)0 ), NOT_AN_ARRAY },
#define VGUI_UNPACK_BASEPANEL() \
DMXELEMENT_UNPACK_BASECLASS_NAMESPACE( vgui, DestStructType_t, DestStructType_t::BaseClass ) \
#define DMXELEMENT_UNPACK_FIELD( _attributeName, _defaultString, _type, _varName ) \
{ _attributeName, _defaultString, CDmAttributeInfo<_type>::AttributeType(), offsetof( DestStructType_t, _varName ), sizeof( ((DestStructType_t *)0)->_varName), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
// Use for preallocated char array
#define DMXELEMENT_UNPACK_FIELD_STRING( _attributeName, _defaultString, _varName ) \
{ _attributeName, _defaultString, AT_STRING, offsetof( DestStructType_t, _varName ), sizeof( ((DestStructType_t *)0)->_varName), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
// Use for UtlString datatype
#define DMXELEMENT_UNPACK_FIELD_UTLSTRING( _attributeName, _defaultString, _varName ) \
{ _attributeName, _defaultString, AT_STRING, offsetof( DestStructType_t, _varName ), UTL_STRING_SIZE, NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_SHORT( _attributeName, _defaultString, _varName ) \
{ _attributeName, _defaultString, AT_INT, offsetof( DestStructType_t, _varName ), sizeof( short ), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_CHAR( _attributeName, _defaultString, _varName ) \
{ _attributeName, _defaultString, AT_INT, offsetof( DestStructType_t, _varName ), sizeof( char ), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_BITFIELD( _attributeName, _defaultString, _bitfieldType, _varName ) \
{ _attributeName, _defaultString, CDmAttributeInfo<int>::AttributeType(), DestStructType_t::Get##_varName##ByteOffset(), DestStructType_t::Get##_varName##BitCount(), DestStructType_t::Get##_varName##BitOffset(), _bitfieldType, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
// NOTE: DMXELEMENT_UNPACK_FIELD_ARRAY is for fixed-size arrays, not pointers or CUtlVectors (TODO: doesn't work for strings or bitfields yet!)
#define DMXELEMENT_UNPACK_FIELD_ARRAY( _attributeName, _defaultString, _type, _varName ) \
{ _attributeName, _defaultString, CDmAttributeInfo<CUtlVector<_type>>::AttributeType(), offsetof( DestStructType_t, _varName ), sizeof( ((DestStructType_t *)0)->_varName[0]), NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, ARRAYSIZE( ((DestStructType_t *)0)->_varName ) },
#define DMXELEMENT_UNPACK_FIELD_USERDATA( _attributeName, _defaultString, _type, _varName, _userData ) \
{ _attributeName, _defaultString, CDmAttributeInfo<_type>::AttributeType(), offsetof( DestStructType_t, _varName ), sizeof( ((DestStructType_t *)0)->_varName), NO_BIT_OFFSET, NOT_A_BITFIELD, _userData, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_FIELD_STRING_USERDATA( _attributeName, _defaultString, _varName, _userData ) \
{ _attributeName, _defaultString, AT_STRING, offsetof( DestStructType_t, _varName ), sizeof( ((DestStructType_t *)0)->_varName), NO_BIT_OFFSET, NOT_A_BITFIELD, _userData, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define DMXELEMENT_UNPACK_FIELD_UTLSTRING_USERDATA( _attributeName, _defaultString, _varName, _userData ) \
{ _attributeName, _defaultString, AT_STRING, offsetof( DestStructType_t, _varName ), UTL_STRING_SIZE, NO_BIT_OFFSET, NOT_A_BITFIELD, _userData, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR, NOT_AN_ARRAY },
#define END_DMXELEMENT_UNPACK( _structName, _varName ) \
{ NULL, NULL, AT_UNKNOWN, 0, 0, NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR } \
}; \
return unpack; \
} \
DmxElementUnpackStructure_t *_varName = _structName##_UnpackInit::s_pUnpack;
#define END_DMXELEMENT_UNPACK_TEMPLATE( _structName, _varName ) \
{ NULL, NULL, AT_UNKNOWN, 0, 0, NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR } \
}; \
return unpack; \
} \
template<> DmxElementUnpackStructure_t *_varName = _structName##_UnpackInit::s_pUnpack;
// Macros for when your class is inside a namespace.
#define BEGIN_DMXELEMENT_NAMESPACE_UNPACK( _nameSpace, _structName ) \
template <typename T> DmxElementUnpackStructure_t *DmxElementUnpackInit(T *); \
template <> DmxElementUnpackStructure_t *_nameSpace::DmxElementUnpackInit<_nameSpace::_structName>( _nameSpace::_structName * ); \
namespace _structName##_UnpackInit \
{ \
static DmxElementUnpackStructure_t *s_pUnpack = _nameSpace::DmxElementUnpackInit( (_nameSpace::_structName *)NULL ); \
} \
\
template <> DmxElementUnpackStructure_t *_nameSpace::DmxElementUnpackInit<_nameSpace::_structName>( _nameSpace::_structName * ) \
{ \
typedef _nameSpace::_structName DestStructType_t; \
static DmxElementUnpackStructure_t unpack[] = \
{ \
//#define DECLARE_DMXELEMENT_UNPACK_NAMESPACE( _namespace ) \
// template <typename T> friend DmxElementUnpackStructure_t *DmxElementUnpackInit##_namespace(T *);
// Adds serialization unpack structure and unpack func to your class.
#define DECLARE_DMXELEMENT_UNPACK_NAMESPACE( _namespace ) \
template <typename T> friend DmxElementUnpackStructure_t *DmxElementUnpackInit##_namespace(T *); \
private: \
static DmxElementUnpackStructure_t *s_pUnpackParams; \
public: \
virtual const DmxElementUnpackStructure_t* GetUnpackStructure() const { return s_pUnpackParams; } \
// Use when your panel class is derived from another baseclass
#define BEGIN_DMXELEMENT_UNPACK_NAMESPACE_SIMPLE( _namespace, _structName ) \
BEGIN_DMXELEMENT_UNPACK_NAMESPACE( _namespace, _structName ) \
VGUI_UNPACK_BASEPANEL() \
// Use when your panel class has no base class
#define BEGIN_DMXELEMENT_UNPACK_NAMESPACE_SIMPLE_NO_BASE( _namespace, _structName ) \
BEGIN_DMXELEMENT_UNPACK_NAMESPACE( _namespace, _structName ) \
#define BEGIN_DMXELEMENT_UNPACK_NAMESPACE( _namespace, _structName ) \
template <typename T> DmxElementUnpackStructure_t *_namespace::DmxElementUnpackInit##_namespace(T *); \
template <> DmxElementUnpackStructure_t *_namespace::DmxElementUnpackInit##_namespace<_structName>( _namespace::_structName * ); \
namespace _namespace##_structName##_UnpackInit \
{ \
static DmxElementUnpackStructure_t *s_pUnpack = _namespace::DmxElementUnpackInit##_namespace( (_namespace::_structName *)NULL ); \
} \
\
template <> DmxElementUnpackStructure_t *_namespace::DmxElementUnpackInit##_namespace<_structName>( _namespace::_structName * ) \
{ \
typedef _structName DestStructType_t; \
static DmxElementUnpackStructure_t unpack[] = \
{ \
// Use to end BEGIN_DMXELEMENT_UNPACK_NAMESPACE* macros
#define END_DMXELEMENT_UNPACK_NAMESPACE( _namespace, _structName, _varName ) \
{ NULL, NULL, AT_UNKNOWN, 0, 0, NO_BIT_OFFSET, NOT_A_BITFIELD, NO_USER_DATA, NO_EMBEDDED_TYPENAME, NO_EMBEDDED_STRUCT_PTR } \
}; \
return unpack; \
} \
DmxElementUnpackStructure_t *_structName::_varName = _namespace##_structName##_UnpackInit::s_pUnpack; \
#ifdef PLATFORM_POSIX
#define PRAGMA_DISABLE_4310
#define PRAGMA_ENABLE_4310
#else
#define PRAGMA_DISABLE_4310 __pragma(warning(disable:4310))
#define PRAGMA_ENABLE_4310 __pragma(warning(default:4310))
#endif
#define DECLARE_DMXELEMENT_BITFIELD( _fieldName, _type, _structName ) \
class CBitFieldInfo_##_fieldName \
{ \
public: \
CBitFieldInfo_##_fieldName () \
{ \
int nSize = ( sizeof(_structName) + 3 ) & ~0x3; \
unsigned char *pBuf = ( unsigned char * )stackalloc(nSize); \
memset( pBuf, 0, nSize ); \
PRAGMA_DISABLE_4310; \
(( _structName * )pBuf)->_fieldName = (_type)(0xFFFFFFFF); \
PRAGMA_ENABLE_4310; \
_type *pTest = (_type *)pBuf; \
for ( int i = 0; i < sizeof(_structName); ++i ) \
{ \
if ( pTest[i] == 0 ) \
continue; \
\
for ( int j = 0; j < 8*sizeof(_type); ++j ) \
{ \
unsigned int temp = ((unsigned int)pTest[i]) & ( 1 << j ) ; \
if ( temp == 0 ) \
continue; \
\
m_nByteOffset = i*sizeof(_type) + j / 8; \
m_nBitOffset = j & 0x7; \
\
int k; \
for ( k = j+1; k < 8*sizeof(_type); ++k ) \
{ \
unsigned int temp = ((unsigned int)pTest[i]) & ( 1 << k ) ; \
if ( temp != 0 ) \
continue; \
break; \
} \
m_nBitCount = k - j; \
break; \
} \
break; \
} \
} \
\
int GetByteOffset() const \
{ \
return m_nByteOffset; \
} \
\
int GetBitCount() const \
{ \
return m_nBitCount; \
} \
\
int GetBitOffset() const \
{ \
return m_nBitOffset; \
} \
\
private: \
int m_nByteOffset; \
int m_nBitCount; \
int m_nBitOffset; \
}; \
\
static int Get##_fieldName##BitCount() \
{ \
CBitFieldInfo_##_fieldName info; \
return info.GetBitCount(); \
} \
\
static int Get##_fieldName##ByteOffset() \
{ \
CBitFieldInfo_##_fieldName info; \
return info.GetByteOffset(); \
} \
\
static int Get##_fieldName##BitOffset() \
{ \
CBitFieldInfo_##_fieldName info; \
return info.GetBitOffset(); \
} \
friend class CBitFieldInfo_##_fieldName; \
// A bit of a hack, but we don't have access to
extern CUtlSymbolTableLargeMT g_DmxAttributeStrings;
//-----------------------------------------------------------------------------
// Element used to read dmx files from mod code. Similar to keyvalues.
//-----------------------------------------------------------------------------
class CDmxElement
{
DECLARE_DMX_ALLOCATOR( );
public:
bool HasAttribute( const char *pAttributeName ) const;
CDmxAttribute *GetAttribute( const char *pAttributeName );
const CDmxAttribute *GetAttribute( const char *pAttributeName ) const;
int AttributeCount() const;
CDmxAttribute *GetAttribute( int nIndex );
const CDmxAttribute *GetAttribute( int nIndex ) const;
CUtlSymbolLarge GetType() const;
const char* GetTypeString() const;
const char* GetName() const;
const DmObjectId_t &GetId() const;
// Add+remove+rename can only occur during lock
// NOTE: AddAttribute will find or add; returning an existing attribute if
// one with the appropriate name exists
void LockForChanges( bool bLock );
CDmxAttribute *AddAttribute( const char *pAttributeName );
void RemoveAttribute( const char *pAttributeName );
void RemoveAttributeByPtr( CDmxAttribute *pAttribute );
void RemoveAllAttributes();
void RenameAttribute( const char *pAttributeName, const char *pNewName );
// Simple methods to read attributes
const char *GetValueString( const char *pAttributeName ) const;
template< class T > const T& GetValue( const char *pAttributeName ) const;
template< class T > const T& GetValue( const char *pAttributeName, const T& defaultValue ) const;
template< class T > const CUtlVector<T>& GetArray( const char *pAttributeName ) const;
template< class T > const CUtlVector<T>& GetArray( const char *pAttributeName, const CUtlVector<T>& defaultValue ) const;
// Set methods
void SetName( const char *pName );
CDmxAttribute* SetValue( const char *pAttributeName, const char *pString );
CDmxAttribute* SetValue( const char *pAttributeName, void *pBuffer, int nLen );
template< class T > CDmxAttribute* SetValue( const char *pAttributeName, const T& value );
// Method to unpack data into a structure
void UnpackIntoStructure( void *pData, const DmxElementUnpackStructure_t *pUnpack ) const;
// Creates attributes based on the unpack structure
void AddAttributesFromStructure( const void *pData, const DmxElementUnpackStructure_t *pUnpack );
private:
typedef CUtlSortVector< CDmxAttribute*, CDmxAttributeLess > AttributeList_t;
CDmxElement( const char *pType );
~CDmxElement();
// Removes all elements recursively
void RemoveAllElementsRecursive();
// Adds elements to delete to the deletion list
void AddElementsToDelete( CUtlVector< CDmxElement * >& elementsToDelete );
// Sorts the vector when a change has occurred
void Resort( ) const;
// Finds an attribute by name
int FindAttribute( const char *pAttributeName ) const;
int FindAttribute( CUtlSymbolLarge attributeName ) const;
// Sets the object id
void SetId( const DmObjectId_t &id );
// Are we locked?
bool IsLocked() const;
template <typename T> void UnpackBitfield( T *pDest2, const DmxElementUnpackStructure_t *pUnpack, const CDmxAttribute *pAttribute ) const;
AttributeList_t m_Attributes;
DmObjectId_t m_Id; // We need this strictly because we support serialization
CUtlSymbolLarge m_Type;
char m_nLockCount;
mutable bool m_bResortNeeded : 1;
bool m_bIsMarkedForDeletion : 1;
static CUtlSymbolTableLargeMT s_TypeSymbols;
friend class CDmxSerializer;
friend class CDmxSerializerKeyValues2;
friend void CleanupDMX( CDmxElement* pElement );
friend CDmxElement* CreateDmxElement( const char *pType );
};
//-----------------------------------------------------------------------------
// inline methods
//-----------------------------------------------------------------------------
// Are we locked?
inline bool CDmxElement::IsLocked() const
{
return m_nLockCount > 0;
}
inline const char *CDmxElement::GetValueString( const char *pAttributeName ) const
{
const CDmxAttribute* pAttribute = GetAttribute( pAttributeName );
if ( pAttribute )
return pAttribute->GetValueString();
return "";
}
template< class T >
inline const T& CDmxElement::GetValue( const char *pAttributeName ) const
{
const CDmxAttribute* pAttribute = GetAttribute( pAttributeName );
if ( pAttribute )
return pAttribute->GetValue<T>();
static T defaultValue;
CDmAttributeInfo<T>::SetDefaultValue( defaultValue );
return defaultValue;
}
template< class T >
inline const T& CDmxElement::GetValue( const char *pAttributeName, const T& defaultValue ) const
{
const CDmxAttribute* pAttribute = GetAttribute( pAttributeName );
if ( pAttribute )
return pAttribute->GetValue<T>();
return defaultValue;
}
template< class T >
inline const CUtlVector<T>& CDmxElement::GetArray( const char *pAttributeName ) const
{
const CDmxAttribute* pAttribute = GetAttribute( pAttributeName );
if ( pAttribute )
return pAttribute->GetArray<T>();
static CUtlVector<T> defaultValue;
return defaultValue;
}
template< class T >
inline const CUtlVector<T>& CDmxElement::GetArray( const char *pAttributeName, const CUtlVector<T>& defaultValue ) const
{
const CDmxAttribute* pAttribute = GetAttribute( pAttributeName );
if ( pAttribute )
return pAttribute->GetArray<T>();
return defaultValue;
}
//-----------------------------------------------------------------------------
// Creates a dmx element
//-----------------------------------------------------------------------------
CDmxElement* CreateDmxElement( const char *pType );
//-----------------------------------------------------------------------------
// Helper class to lock elements for changes
//-----------------------------------------------------------------------------
class CDmxElementModifyScope
{
public:
CDmxElementModifyScope( CDmxElement *pElement ) : m_pElement( pElement )
{
m_pElement->LockForChanges( true );
}
~CDmxElementModifyScope()
{
Release();
}
void Release()
{
if ( m_pElement )
{
m_pElement->LockForChanges( false );
m_pElement = NULL;
}
}
private:
CDmxElement *m_pElement;
};
//-----------------------------------------------------------------------------
// Set methods
//-----------------------------------------------------------------------------
inline CDmxAttribute* CDmxElement::SetValue( const char *pAttributeName, const char *pString )
{
CDmxElementModifyScope modify( this );
CDmxAttribute *pAttribute = AddAttribute( pAttributeName );
pAttribute->SetValue( pString );
return pAttribute;
}
inline CDmxAttribute* CDmxElement::SetValue( const char *pAttributeName, void *pBuffer, int nLen )
{
CDmxElementModifyScope modify( this );
CDmxAttribute *pAttribute = AddAttribute( pAttributeName );
pAttribute->SetValue( pBuffer, nLen );
return pAttribute;
}
template< class T >
inline CDmxAttribute* CDmxElement::SetValue( const char *pAttributeName, const T& value )
{
CDmxElementModifyScope modify( this );
CDmxAttribute *pAttribute = AddAttribute( pAttributeName );
pAttribute->SetValue( value );
return pAttribute;
}
#endif // DMXELEMENT_H

View File

@ -0,0 +1,72 @@
//====== Copyright <20> 1996-2004, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#ifndef DMXLOADER_H
#define DMXLOADER_H
#ifdef _WIN32
#pragma once
#endif
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CUtlBuffer;
class CDmxElement;
//-----------------------------------------------------------------------------
// Serialization/Unserialization
//-----------------------------------------------------------------------------
bool SerializeDMX( CUtlBuffer &buf, CDmxElement *pRoot, const char *pFileName = NULL );
bool SerializeDMX( const char *pFileName, const char *pPathID, bool bTextMode, CDmxElement *pRoot );
bool UnserializeDMX( CUtlBuffer &buf, CDmxElement **ppRoot, const char *pFileName = NULL );
bool UnserializeDMX( const char *pFileName, const char *pPathID, bool bTextMode, CDmxElement **ppRoot );
//-----------------------------------------------------------------------------
// DMX elements/attributes can only be accessed inside a dmx context
//-----------------------------------------------------------------------------
void BeginDMXContext( );
void EndDMXContext( bool bDecommitMemory );
void DecommitDMXMemory();
//-----------------------------------------------------------------------------
// Helper macro
//-----------------------------------------------------------------------------
class CDMXContextHelper
{
public:
CDMXContextHelper( bool bDecommitMemory ) { m_bDecommitMemory = bDecommitMemory; BeginDMXContext(); }
~CDMXContextHelper() { EndDMXContext( m_bDecommitMemory ); }
private:
bool m_bDecommitMemory;
};
#define DECLARE_DMX_CONTEXT( ) CDMXContextHelper __dmxContextHelper( true );
#define DECLARE_DMX_CONTEXT_NODECOMMIT( ) CDMXContextHelper __dmxContextHelper( false );
#define DECLARE_DMX_CONTEXT_DECOMMIT( _decommit ) CDMXContextHelper __dmxContextHelper( _decommit );
//-----------------------------------------------------------------------------
// Used for allocation. All will be freed when we leave the DMX context
//-----------------------------------------------------------------------------
void* DMXAlloc( size_t size );
//-----------------------------------------------------------------------------
// Helper macro
//-----------------------------------------------------------------------------
#define DECLARE_DMX_ALLOCATOR( ) \
public: \
inline void* operator new( size_t size ) { MEM_ALLOC_CREDIT_( "DMXAlloc" ); return DMXAlloc(size); } \
inline void* operator new( size_t size, int nBlockUse, const char *pFileName, int nLine ) { MEM_ALLOC_CREDIT_( "DMXAlloc" ); return DMXAlloc(size); } \
inline void operator delete( void* p ) { } \
inline void operator delete( void* p, int nBlockUse, const char *pFileName, int nLine ) { } \
#endif // DMXLOADER_H