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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// $Header: $
// $NoKeywords: $
//
// Converts from any one DMX file format to another
// Can also output SMD or a QCI header from DMX input
//
//=============================================================================
#include "tier0/dbg.h"
#include "tier0/icommandline.h"
#include "datamodel/idatamodel.h"
#include "filesystem.h"
#include "appframework/appframework.h"
#include "tier1/utlbuffer.h"
#include "dmserializers/idmserializers.h"
#include "tier1/utlstring.h"
#include "datamodel/dmattribute.h"
#include "datamodel/dmelement.h"
#include "tier2/tier2.h"
class CDmElement;
//-----------------------------------------------------------------------------
// Standard spew functions
//-----------------------------------------------------------------------------
static SpewRetval_t DMXConvertOutputFunc( SpewType_t spewType, char const *pMsg )
{
printf( pMsg );
fflush( stdout );
if (spewType == SPEW_ERROR)
return SPEW_ABORT;
return (spewType == SPEW_ASSERT) ? SPEW_DEBUGGER : SPEW_CONTINUE;
}
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
class CDmxConvertApp : public CDefaultAppSystemGroup<CSteamAppSystemGroup>
{
public:
// Methods of IApplication
virtual bool Create();
virtual bool PreInit( );
virtual int Main();
virtual void PostShutdown();
};
DEFINE_CONSOLE_STEAM_APPLICATION_OBJECT( CDmxConvertApp );
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
bool CDmxConvertApp::Create()
{
SpewOutputFunc( DMXConvertOutputFunc );
AddSystem( g_pDataModel, VDATAMODEL_INTERFACE_VERSION );
AddSystem( g_pDmSerializers, DMSERIALIZERS_INTERFACE_VERSION );
return true;
}
bool CDmxConvertApp::PreInit( )
{
CreateInterfaceFn factory = GetFactory();
ConnectTier1Libraries( &factory, 1 );
ConnectTier2Libraries( &factory, 1 );
if ( !g_pFullFileSystem || !g_pDataModel )
{
Warning( "DMXConvert is missing a required interface!\n" );
return false;
}
return true;
}
void CDmxConvertApp::PostShutdown()
{
DisconnectTier2Libraries();
DisconnectTier1Libraries();
}
//-----------------------------------------------------------------------------
// The application object
//-----------------------------------------------------------------------------
int CDmxConvertApp::Main()
{
g_pDataModel->OnlyCreateUntypedElements( true );
g_pDataModel->SetDefaultElementFactory( NULL );
// This bit of hackery allows us to access files on the harddrive
g_pFullFileSystem->AddSearchPath( "", "LOCAL", PATH_ADD_TO_HEAD );
const char *pInFileName = CommandLine()->ParmValue("-i" );
const char *pOutFileName = CommandLine()->ParmValue("-o" );
const char *pInFormat = CommandLine()->ParmValue("-if" );
const char *pOutFormat = CommandLine()->ParmValue("-of" );
const char *pOutEncoding = CommandLine()->ParmValue("-oe" );
if ( !pInFileName )
{
Msg( "Usage: dmxconvert -i <in file> [-if <in format_hint>] [-o <out file>] [-oe <out encoding>] [-of <out format>]\n" );
Msg( "If no output file is specified, dmx to dmx conversion will overwrite the input\n" );
Msg( "Supported DMX file encodings:\n" );
for ( int i = 0; i < g_pDataModel->GetEncodingCount(); ++i )
{
Msg( " %s\n", g_pDataModel->GetEncodingName( i ) );
}
Msg( "Supported DMX file formats:\n" );
for ( int i = 0; i < g_pDataModel->GetFormatCount(); ++i )
{
Msg( " %s\n", g_pDataModel->GetFormatName( i ) );
}
return -1;
}
// When reading, keep the CRLF; this will make ReadFile read it in binary format
// and also append a couple 0s to the end of the buffer.
DmxHeader_t header;
CDmElement *pRoot;
if ( g_pDataModel->RestoreFromFile( pInFileName, NULL, pInFormat, &pRoot, CR_DELETE_NEW, &header ) == DMFILEID_INVALID )
{
Error( "Encountered an error reading file \"%s\"!\n", pInFileName );
return -1;
}
if ( !pOutFormat )
{
pOutFormat = header.formatName;
if ( !g_pDataModel->FindFormatUpdater( pOutFormat ) )
{
pOutFormat = "dmx"; // default to generic dmx format for legacy files
}
}
if ( !pOutEncoding )
{
pOutEncoding = g_pDataModel->GetDefaultEncoding( pOutFormat );
if ( !pOutEncoding )
{
// no default encoding was found, try to convert the file to the generic dmx format
pOutFormat = GENERIC_DMX_FORMAT;
pOutEncoding = g_pDataModel->GetDefaultEncoding( pOutFormat );
}
}
if ( !pOutFileName )
{
pOutFileName = pInFileName;
}
// TODO - in theory, at some point, we may have converters from pInFormat to pOutFormat
// until then, treat it as a noop, and hope for the best
if ( !g_pDataModel->SaveToFile( pOutFileName, NULL, pOutEncoding, pOutFormat, pRoot ) )
{
Error( "Encountered an error writing file \"%s\"!\n", pOutFileName );
return -1;
}
g_pDataModel->RemoveFileId( pRoot->GetFileId() );
return 0;
}
|