-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathimportsfmv3.cpp
More file actions
228 lines (196 loc) · 6.95 KB
/
importsfmv3.cpp
File metadata and controls
228 lines (196 loc) · 6.95 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//====== Copyright © 1996-2006, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "dmserializers.h"
#include "dmebaseimporter.h"
#include "datamodel/idatamodel.h"
#include "datamodel/dmelement.h"
#include "datamodel/dmattributevar.h"
#include "tier1/KeyValues.h"
#include "tier1/utlbuffer.h"
#include "tier1/utlmap.h"
#include <limits.h>
//-----------------------------------------------------------------------------
// Format converter
//-----------------------------------------------------------------------------
class CImportSFMV3 : public CSFMBaseImporter
{
typedef CSFMBaseImporter BaseClass;
public:
CImportSFMV3( char const *formatName, char const *nextFormatName );
private:
virtual bool DoFixup( CDmElement *pSourceRoot );
void FixupElement( CDmElement *pElement );
// Fixes up all elements
void BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list );
};
//-----------------------------------------------------------------------------
// Singleton instance
//-----------------------------------------------------------------------------
static CImportSFMV3 s_ImportSFMV3( "sfm_v3", "sfm_v4" );
void InstallSFMV3Importer( IDataModel *pFactory )
{
pFactory->AddLegacyUpdater( &s_ImportSFMV3 );
}
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CImportSFMV3::CImportSFMV3( char const *formatName, char const *nextFormatName ) :
BaseClass( formatName, nextFormatName )
{
}
struct LogToCurveInfoTypeMap_t
{
const char *pLogType;
const char *pLogLayerType;
const char *pCurveInfoType;
};
LogToCurveInfoTypeMap_t g_typeMap[] =
{
{ "DmeIntLog", "DmeIntLogLayer", "DmeIntCurveInfo" },
{ "DmeFloatLog", "DmeFloatLogLayer", "DmeFloatCurveInfo" },
{ "DmeBoolLog", "DmeBoolLogLayer", "DmeBoolCurveInfo" },
// string,
// void,
// objectid,
{ "DmeColorLog", "DmeColorLogLayer", "DmeColorCurveInfo" },
{ "DmeVector2Log", "DmeVector2LogLayer", "DmeVector2CurveInfo" },
{ "DmeVector3Log", "DmeVector3LogLayer", "DmeVector3CurveInfo" },
{ "DmeVector4Log", "DmeVector4LogLayer", "DmeVector4CurveInfo" },
{ "DmeQAngleLog", "DmeQAngleLogLayer", "DmeQAngleCurveInfo" },
{ "DmeQuaternionLog", "DmeQuaternionLogLayer","DmeQuaternionCurveInfo" },
{ "DmeVMatrixLog", "DmeVMatrixLogLayer", "DmeVMatrixCurveInfo" },
};
const char *GetCurveInfoTypeFromLogType( const char *pLogType )
{
int c = ARRAYSIZE( g_typeMap );
for ( int i = 0; i < c; ++i )
{
if ( !Q_stricmp( pLogType, g_typeMap[ i ].pLogType ) )
return g_typeMap[ i ].pCurveInfoType;
}
return NULL;
}
bool IsLogLayerType( const char *pLogLayerType )
{
int c = ARRAYSIZE( g_typeMap );
for ( int i = 0; i < c; ++i )
{
if ( !Q_stricmp( pLogLayerType, g_typeMap[ i ].pLogLayerType ) )
return true;
}
return false;
}
void MoveAttribute( CDmElement *pFromElement, const char *pFromAttrName, CDmElement *pToElement = NULL, const char *pToAttrName = NULL, DmAttributeType_t toType = AT_UNKNOWN )
{
if ( !pToAttrName )
{
pToAttrName = pFromAttrName;
}
if ( pToElement )
{
CDmAttribute *pFromAttr = pFromElement->GetAttribute( pFromAttrName );
const void *pValue = pFromAttr->GetValueUntyped();
DmAttributeType_t fromType = pFromAttr->GetType();
if ( toType == AT_UNKNOWN )
{
toType = fromType;
}
CDmAttribute *pToAttr = pToElement->AddAttribute( pToAttrName, toType );
if ( !pToAttr )
{
Warning( "*** Problem in converter encountered!\n" );
Warning( "*** Unable to find or add attribute \"%s\" to element \"%s\"!\n", pToAttrName, pToElement->GetName() );
}
else if ( fromType != toType )
{
Warning( "*** Problem in file encountered!\n" );
Warning( "*** Element \"%s\" has attribute \"%s\" with an unexpected type!\n", pFromElement->GetName(), pFromAttrName );
}
else
{
pToAttr->SetValue( toType, pValue );
}
}
pFromElement->RemoveAttribute( pFromAttrName );
}
// Fixes up all elements
//-----------------------------------------------------------------------------
void CImportSFMV3::FixupElement( CDmElement *pElement )
{
if ( !pElement )
return;
const char *pType = pElement->GetTypeString();
// log layer
if ( IsLogLayerType( pType ) )
{
pElement->RemoveAttribute( "ownerlog" );
return;
}
// log
const char *pCurveInfoType = GetCurveInfoTypeFromLogType( pType );
if ( !pCurveInfoType )
return;
CDmElement *pCurveInfo = NULL;
CDmAttribute *pUseCurveTypeAttr = pElement->GetAttribute( "usecurvetypes" );
if ( pUseCurveTypeAttr && pUseCurveTypeAttr->GetValue<bool>() )
{
DmElementHandle_t hElement = g_pDataModel->CreateElement( "curve info", pCurveInfoType, pElement->GetFileId() );
pCurveInfo = g_pDataModel->GetElement( hElement );
}
pElement->RemoveAttribute( "usecurvetypes" );
MoveAttribute( pElement, "defaultcurvetype", pCurveInfo, "defaultCurveType", AT_INT );
MoveAttribute( pElement, "defaultedgezerovalue",pCurveInfo, "defaultEdgeZeroValue" );
MoveAttribute( pElement, "useedgeinfo", pCurveInfo, "useEdgeInfo", AT_BOOL );
MoveAttribute( pElement, "rightedgetime", pCurveInfo, "rightEdgeTime", AT_INT );
MoveAttribute( pElement, "left_edge_active", pCurveInfo, "leftEdgeActive", AT_BOOL );
MoveAttribute( pElement, "right_edge_active", pCurveInfo, "rightEdgeActive", AT_BOOL );
MoveAttribute( pElement, "left_edge_curvetype", pCurveInfo, "leftEdgeCurveType", AT_INT );
MoveAttribute( pElement, "right_edge_curvetype",pCurveInfo, "rightEdgeCurveType", AT_INT );
MoveAttribute( pElement, "left_edge_value", pCurveInfo, "leftEdgeValue" );
MoveAttribute( pElement, "right_edge_value", pCurveInfo, "rightEdgeValue" );
}
// Fixes up all elements
//-----------------------------------------------------------------------------
void CImportSFMV3::BuildList( CDmElement *pElement, CUtlRBTree< CDmElement *, int >& list )
{
if ( !pElement )
return;
if ( list.Find( pElement ) != list.InvalidIndex() )
return;
list.Insert( pElement );
// Descend to bottom of tree, then do fixup coming back up the tree
for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
{
if ( pAttribute->GetType() == AT_ELEMENT )
{
CDmElement *pElement = pAttribute->GetValueElement<CDmElement>( );
BuildList( pElement, list );
continue;
}
if ( pAttribute->GetType() == AT_ELEMENT_ARRAY )
{
CDmrElementArray<> array( pAttribute );
int nCount = array.Count();
for ( int i = 0; i < nCount; ++i )
{
CDmElement *pChild = array[ i ];
BuildList( pChild, list );
}
continue;
}
}
}
bool CImportSFMV3::DoFixup( CDmElement *pSourceRoot )
{
CUtlRBTree< CDmElement *, int > fixlist( 0, 0, DefLessFunc( CDmElement * ) );
BuildList( pSourceRoot, fixlist );
for ( int i = fixlist.FirstInorder(); i != fixlist.InvalidIndex() ; i = fixlist.NextInorder( i ) )
{
// Search and replace in the entire tree!
FixupElement( fixlist[ i ] );
}
return true;
}