-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathimportsfmv1.cpp
More file actions
219 lines (189 loc) · 7.4 KB
/
importsfmv1.cpp
File metadata and controls
219 lines (189 loc) · 7.4 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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "dmserializers.h"
#include "dmebaseimporter.h"
#include "datamodel/idatamodel.h"
#include "datamodel/dmattribute.h"
#include "datamodel/dmelement.h"
#include <math.h>
//-----------------------------------------------------------------------------
// Format converter
//-----------------------------------------------------------------------------
class CImportSFMV1 : public CSFMBaseImporter
{
typedef CSFMBaseImporter BaseClass;
public:
CImportSFMV1( char const *formatName, char const *nextFormatName );
private:
virtual bool DoFixup( CDmElement *pSourceRoot );
// Fixes up a single time attribute - converting from float seconds to int tenths-of-a-millisecond
void ConvertTimeAttribute( CDmElement *pElementInternal, const char *pOldName, const char *pNewName );
// Fixes up a single timeframe
void FixupTimeframe( CDmElement *pElementInternal );
// Fixes up a single log - converting from int milliseconds to int tenths-of-a-millisecond
void FixupLog( CDmElement *pElementInternal );
CUtlRBTree< CDmElement*, int > m_fixedElements;
};
//-----------------------------------------------------------------------------
// Singleton instance
//-----------------------------------------------------------------------------
static CImportSFMV1 s_ImportDmxV1( "sfm_v1", "sfm_v2" );
void InstallSFMV1Importer( IDataModel *pFactory )
{
pFactory->AddLegacyUpdater( &s_ImportDmxV1 );
}
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CImportSFMV1::CImportSFMV1( char const *formatName, char const *nextFormatName ) :
BaseClass( formatName, nextFormatName )
{
m_fixedElements.SetLessFunc( DefLessFunc( CDmElement * ) );
}
//-----------------------------------------------------------------------------
// Fixes up all elements
//-----------------------------------------------------------------------------
bool CImportSFMV1::DoFixup( CDmElement *pElementInternal )
{
if ( !pElementInternal )
return true;
if ( m_fixedElements.Find( pElementInternal ) != m_fixedElements.InvalidIndex() )
return true;
m_fixedElements.Insert( pElementInternal );
const char *pType = pElementInternal->GetTypeString();
if ( !Q_strcmp( pType, "DmeTimeFrame" ) )
{
FixupTimeframe( pElementInternal );
}
else if ( !Q_strcmp( pType, "DmeLog" ) ||
!Q_strcmp( pType, "DmeIntLog" ) ||
!Q_strcmp( pType, "DmeFloatLog" ) ||
!Q_strcmp( pType, "DmeBoolLog" ) ||
!Q_strcmp( pType, "DmeColorLog" ) ||
!Q_strcmp( pType, "DmeVector2Log" ) ||
!Q_strcmp( pType, "DmeVector3Log" ) ||
!Q_strcmp( pType, "DmeVector4Log" ) ||
!Q_strcmp( pType, "DmeQAngleLog" ) ||
!Q_strcmp( pType, "DmeQuaternionLog" ) ||
!Q_strcmp( pType, "DmeVMatrixLog" ) )
{
FixupLog( pElementInternal );
}
for ( CDmAttribute *pAttribute = pElementInternal->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
{
if ( pAttribute->GetType() == AT_ELEMENT )
{
CDmElement *pElement = pAttribute->GetValueElement<CDmElement>( );
DoFixup( pElement );
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 ];
DoFixup( pChild );
}
continue;
}
}
return true;
}
//-----------------------------------------------------------------------------
// Fixes up a single time attribute - converting from float seconds to int tenths-of-a-millisecond
//-----------------------------------------------------------------------------
void CImportSFMV1::ConvertTimeAttribute( CDmElement *pElementInternal, const char *pOldName, const char *pNewName )
{
float time = 0.0f;
CDmAttribute *pOldAttr = pElementInternal->GetAttribute( pOldName );
if ( !pOldAttr )
{
Warning( "*** Problem in file encountered!\n" );
Warning( "*** TimeFrame \"%s\" is missing attribute \"%s\"!\n", pElementInternal->GetName(), pOldName );
Warning( "*** Setting new attribute \"%s\" to 0\n", pNewName );
}
else if ( pOldAttr->GetType() != AT_FLOAT )
{
Warning( "*** Problem in file encountered!\n" );
Warning( "*** TimeFrame \"%s\" has attribute \"%s\" with an unexpected type (expected float)!\n", pElementInternal->GetName(), pOldName );
}
else
{
time = pOldAttr->GetValue< float >();
pElementInternal->RemoveAttribute( pOldName );
}
CDmAttribute *pNewAttr = NULL;
// this is disabled because even dmxconvert installs *some* movieobjects factories, when it probably shouldn't
// the method of installing movieobjects factories will change at some point in the future, and we can turn on this safety check then
#if 0
int i = g_pDataModel->GetFirstFactory();
if ( g_pDataModel->IsValidFactory( i ) )
{
// factories installed - most likely from within movieobjects.lib
// ie there may be different ways of allocating attributes, so it's not safe to add them here
pNewAttr = pElementInternal->GetAttribute( pNewName );
if ( !pNewAttr || pNewAttr->GetType() != AT_INT )
{
Assert( 0 );
Warning( "*** Converter error - expected element \"%s\" to contain int attribute \"%s\"!\n", pElementInternal->GetName(), pNewName );
Warning( "*** - if you get this error, the converter is out of sync with the element library!\n" );
return;
}
}
else
{
#endif
// no factories installed - most likely from within dmxconvert.exe
// ie we're just working with CDmElement subclasses, so it's safe to add attributes
pNewAttr = pElementInternal->AddAttribute( pNewName, AT_INT );
if ( !pNewAttr )
{
Assert( 0 );
Warning( "*** Converter error - element \"%s\" already has a non-int attribute \"%s\"!\n", pElementInternal->GetName(), pNewName );
return;
}
#if 0
}
#endif
pNewAttr->SetValue< int >( floor( time * 10000 + 0.5f ) );
}
//-----------------------------------------------------------------------------
// Fixes up a single timeframe
//-----------------------------------------------------------------------------
void CImportSFMV1::FixupTimeframe( CDmElement *pElementInternal )
{
ConvertTimeAttribute( pElementInternal, "start", "startTime" );
ConvertTimeAttribute( pElementInternal, "duration", "durationTime" );
ConvertTimeAttribute( pElementInternal, "offset", "offsetTime" );
}
//-----------------------------------------------------------------------------
// Fixes up a single log - converting from int milliseconds to int tenths-of-a-millisecond
//-----------------------------------------------------------------------------
void CImportSFMV1::FixupLog( CDmElement *pElementInternal )
{
CDmAttribute *pAttr = pElementInternal->GetAttribute( "times" );
if ( !pAttr )
{
Warning( "*** Problem in file encountered!\n" );
Warning( "*** Log \"%s\" is missing attribute \"%s\"!\n", pElementInternal->GetName(), "times" );
return;
}
if ( pAttr->GetType() != AT_INT_ARRAY )
{
Warning( "*** Problem in file encountered!\n" );
Warning( "*** Log \"%s\" has attribute \"%s\" with an unexpected type (expected int array)!\n", pElementInternal->GetName(), "times" );
return;
}
CDmrArray<int> array( pAttr );
int c = array.Count();
for ( int i = 0; i < c; ++i )
{
// convert all log times from int milliseconds to int tenths-of-a-millisecond
array.Set( i, 10 * array[i] );
}
}