summaryrefslogtreecommitdiff
path: root/dmxloader/dmxserializationdictionary.cpp
blob: 4e30efaef3aff3b55712728aa8f415fcfdcce50c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "dmxserializationdictionary.h"
#include "dmxloader/dmxelement.h"
#include "dmxloader/dmxattribute.h"


//-----------------------------------------------------------------------------
//
// Element dictionary used in serialization
//
//-----------------------------------------------------------------------------
CDmxSerializationDictionary::CDmxSerializationDictionary( int nElementsHint /* = 0 */ ) :
	m_Dict( 0, nElementsHint, CDmxSerializationDictionary::LessFunc )
{
}


//-----------------------------------------------------------------------------
// Used to sort the list of elements
//-----------------------------------------------------------------------------
bool CDmxSerializationDictionary::LessFunc( const DmxElementInfo_t &lhs, const DmxElementInfo_t &rhs )
{
	return lhs.m_pElement < rhs.m_pElement;
}


//-----------------------------------------------------------------------------
// Finds the handle of the element
//-----------------------------------------------------------------------------
DmxSerializationHandle_t CDmxSerializationDictionary::Find( CDmxElement *pElement )
{
	DmxElementInfo_t find;
	find.m_pElement = pElement;
	return m_Dict.Find( find );
}

	
//-----------------------------------------------------------------------------
// Creates the list of all things to serialize
//-----------------------------------------------------------------------------
void CDmxSerializationDictionary::BuildElementList_R( CDmxElement *pElement, bool bFlatMode, bool bIsRoot )
{
	if ( !pElement )
		return;

	// FIXME: Right here we should ask the element if it's an external
	// file reference and exit immediately if so.

	// This means we've already encountered this guy.
	// Therefore, he can never be a root element
	DmxSerializationHandle_t h = Find( pElement );
	if ( h != m_Dict.InvalidIndex() )
	{
		m_Dict[h].m_bRoot = true;
		return;
	}

	DmxElementInfo_t info;
	info.m_bRoot = bFlatMode || bIsRoot;
	info.m_pElement = pElement;
	m_Dict.Insert( info );

	int nCount = pElement->AttributeCount();
	for ( int i = 0; i < nCount; ++i )
	{
		CDmxAttribute *pAttribute = pElement->GetAttribute(i);
		switch( pAttribute->GetType() )
		{
		case AT_ELEMENT:
			{
				CDmxElement *pChild = pAttribute->GetValue<CDmxElement*>();
				if ( !pChild )
					break;

				BuildElementList_R( pChild, bFlatMode, false );
			}
			break;

		case AT_ELEMENT_ARRAY:
			{
				const CUtlVector<CDmxElement*> &array = pAttribute->GetArray<CDmxElement*>( );
				int nCountArray = array.Count();
				for ( int j = 0; j < nCountArray; ++j )
				{
					CDmxElement *pChild = array[ j ];
					if ( !pChild )
						break;

					BuildElementList_R( pChild, bFlatMode, false );
				}
			}
			break;
		}
	}
}

void CDmxSerializationDictionary::BuildElementList( CDmxElement *pElement, bool bFlatMode )
{
	BuildElementList_R( pElement, bFlatMode, true );
}


//-----------------------------------------------------------------------------
// Should I inline the serialization of this element?
//-----------------------------------------------------------------------------
bool CDmxSerializationDictionary::ShouldInlineElement( CDmxElement *pElement )
{
	// This means we've already encountered this guy.
	// Therefore, he can never be a root element
	DmxSerializationHandle_t h = Find( pElement );
	if ( h != m_Dict.InvalidIndex() )
		return !m_Dict[h].m_bRoot;

	// If we didn't find the element, it means it's a reference to an external
	// element (or it's NULL), so don't inline ie.
	return false;
}


//-----------------------------------------------------------------------------
// Clears the dictionary
//-----------------------------------------------------------------------------
void CDmxSerializationDictionary::Clear()
{
	m_Dict.RemoveAll();
}


//-----------------------------------------------------------------------------
// How many root elements do we have?
//-----------------------------------------------------------------------------
int CDmxSerializationDictionary::RootElementCount() const
{
	int nCount = 0;
	DmxSerializationHandle_t h = m_Dict.FirstInorder();
	while( h != m_Dict.InvalidIndex() )
	{
		if ( m_Dict[h].m_bRoot )
		{
			++nCount;
		}
		h = m_Dict.NextInorder( h );
	}
	return nCount;
}

	
//-----------------------------------------------------------------------------
// Iterates over all root elements to serialize
//-----------------------------------------------------------------------------
DmxSerializationHandle_t CDmxSerializationDictionary::FirstRootElement() const
{
	// NOTE: I don't have to use First/NextInorder here because there
	// are guaranteed to be no removals from the dictionary.
	// Also, using inorder traversal won't get my actual root element to be first in the file
	int nCount = m_Dict.Count();
	for ( DmxSerializationHandle_t h = 0; h < nCount; ++h )
	{
		if ( m_Dict[h].m_bRoot )
			return h;
	}
	return DMX_SERIALIZATION_HANDLE_INVALID;
}

DmxSerializationHandle_t CDmxSerializationDictionary::NextRootElement( DmxSerializationHandle_t h ) const
{
	++h;
	int nCount = m_Dict.Count();
	for ( ; h < nCount; ++h )
	{
		if ( m_Dict[h].m_bRoot )
			return h;
	}
	return DMX_SERIALIZATION_HANDLE_INVALID;
}

CDmxElement *CDmxSerializationDictionary::GetRootElement( DmxSerializationHandle_t h )
{
	Assert( m_Dict[h].m_bRoot );
	return m_Dict[h].m_pElement;
}