-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.cpp
457 lines (376 loc) · 15.6 KB
/
main.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <fbxsdk.h>
#include <windows.h>
#include <shlwapi.h>
#include <shlobj.h>
#include <shellapi.h>
#include <assert.h>
#include <functional>
#include <algorithm>
#if _MSC_VER
#pragma warning(push, 0)
#pragma warning(disable: 4702)
#endif
// Note: this has been modified for this application
#include "cmdParser.h"
#if _MSC_VER
#pragma warning(pop)
#endif
//-------------------------------------------------------------------------
enum class FileFormat
{
Unknown,
Binary,
Ascii
};
//-------------------------------------------------------------------------
namespace FileSystemHelpers
{
static std::string GetFullPathString( char const* pPath )
{
assert( pPath != nullptr && pPath[0] != 0 );
char fullpath[256] = { 0 };
DWORD length = GetFullPathNameA( pPath, 256, fullpath, nullptr );
assert( length != 0 && length != 255 );
// We always append the trailing slash to simplify further operations
DWORD const result = GetFileAttributesA( fullpath );
if ( result != INVALID_FILE_ATTRIBUTES && ( result & FILE_ATTRIBUTE_DIRECTORY ) && fullpath[length - 1] != '\\' )
{
fullpath[length] = '\\';
fullpath[length + 1] = 0;
}
return std::string( fullpath );
}
static std::string GetFullPathString( std::string const& path )
{
return GetFullPathString( path.c_str() );
}
static std::string GetParentDirectoryPath( std::string const& path )
{
std::string dirPath;
size_t const lastSlashIdx = path.rfind( '\\' );
if ( lastSlashIdx != std::string::npos )
{
dirPath = path.substr( 0, lastSlashIdx + 1 );
}
return dirPath;
}
static bool IsValidDirectoryPath( std::string const& directoryPath )
{
DWORD const result = GetFileAttributesA( directoryPath.c_str() );
if ( result != INVALID_FILE_ATTRIBUTES && ( result & FILE_ATTRIBUTE_DIRECTORY ) )
{
return true;
}
return false;
}
static FileFormat GetFileFormat( std::string const& filePath )
{
FileFormat fileFormat = FileFormat::Unknown;
FILE* fp = nullptr;
int errcode = fopen_s( &fp, filePath.c_str(), "r" );
if ( errcode != 0 )
{
return fileFormat;
}
//-------------------------------------------------------------------------
fseek( fp, 0, SEEK_END );
size_t filesize = (size_t) ftell( fp );
fseek( fp, 0, SEEK_SET );
void* pFileData = malloc( filesize );
size_t readLength = fread( pFileData, 1, filesize, fp );
fclose( fp );
//-------------------------------------------------------------------------
// Ascii files cannot contain the null character
if ( memchr( pFileData, '\0', readLength ) != NULL )
{
fileFormat = FileFormat::Binary;
}
else
{
fileFormat = FileFormat::Ascii;
}
return fileFormat;
}
static void GetDirectoryContents( std::string const& directoryPath, std::vector<std::string>& directoryContents )
{
if ( !IsValidDirectoryPath( directoryPath ) )
{
printf( "Error! %s is not a valid directory!", directoryPath.c_str() );
return;
}
//-------------------------------------------------------------------------
std::string const directorySearchPath = directoryPath + "*";
//-------------------------------------------------------------------------
WIN32_FIND_DATAA findData;
HANDLE foundFileHandle = FindFirstFileA( directorySearchPath.c_str(), &findData );
assert( foundFileHandle != INVALID_HANDLE_VALUE );
//-------------------------------------------------------------------------
char stringBuffer[1024] = { 0 };
do
{
if ( strcmp( findData.cFileName, "." ) == 0 || strcmp( findData.cFileName, ".." ) == 0 )
{
continue;
}
if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
sprintf_s( stringBuffer, 1024, "%s%s\\", directoryPath.c_str(), findData.cFileName );
GetDirectoryContents( stringBuffer, directoryContents );
}
else
{
sprintf_s( stringBuffer, 1024, "%s%s", directoryPath.c_str(), findData.cFileName );
directoryContents.emplace_back( GetFullPathString( stringBuffer ) );
}
} while ( FindNextFileA( foundFileHandle, &findData ) != 0 );
}
static bool MakeDir( char const* pDirectoryPath )
{
assert( pDirectoryPath != nullptr );
return SUCCEEDED( SHCreateDirectoryExA( nullptr, pDirectoryPath, nullptr ) );
}
}
//-------------------------------------------------------------------------
class FbxConverter
{
public:
FbxConverter()
: m_pManager( FbxManager::Create() )
{
assert( m_pManager != nullptr );
auto pIOPluginRegistry = m_pManager->GetIOPluginRegistry();
// Find the IDs for the ascii and binary writers
int const numWriters = pIOPluginRegistry->GetWriterFormatCount();
for ( int i = 0; i < numWriters; i++ )
{
if ( pIOPluginRegistry->WriterIsFBX( i ) )
{
char const* pDescription = pIOPluginRegistry->GetWriterFormatDescription( i );
if ( strcmp( pDescription, "FBX binary (*.fbx)" ) == 0 )
{
const_cast<int&>( m_binaryWriteID ) = i;
}
else if ( strcmp( pDescription, "FBX ascii (*.fbx)" ) == 0 )
{
const_cast<int&>( m_asciiWriterID ) = i;
}
}
}
//-------------------------------------------------------------------------
// This should never occur but I'm leaving it here in case someone updates the plugin with a new SDK and names change
assert( m_binaryWriteID != -1 && m_asciiWriterID != -1 );
}
~FbxConverter()
{
m_pManager->Destroy();
m_pManager = nullptr;
}
int ConvertFbxFile( std::string const& inputFilepath, std::string const& outputFilepath, FileFormat outputFormat )
{
// Import
//-------------------------------------------------------------------------
FbxImporter* pImporter = FbxImporter::Create( m_pManager, "FBX Importer" );
if ( !pImporter->Initialize( inputFilepath.c_str(), -1, m_pManager->GetIOSettings() ) )
{
printf( "Error! Failed to load specified FBX file ( %s ): %s\n\n", inputFilepath.c_str(), pImporter->GetStatus().GetErrorString() );
return 1;
}
auto pScene = FbxScene::Create( m_pManager, "ImportScene" );
if ( !pImporter->Import( pScene ) )
{
printf( "Error! Failed to import scene from file ( %s ): %s\n\n", inputFilepath.c_str(), pImporter->GetStatus().GetErrorString() );
pImporter->Destroy();
return 1;
}
pImporter->Destroy();
// Set output format
//-------------------------------------------------------------------------
int const fileFormatIDToUse = ( outputFormat == FileFormat::Binary ) ? m_binaryWriteID : m_asciiWriterID;
// Export
//-------------------------------------------------------------------------
std::string const parentDirPath = FileSystemHelpers::GetParentDirectoryPath( outputFilepath );
if ( !FileSystemHelpers::MakeDir( parentDirPath.c_str() ) )
{
printf( "Error! Failed to create output directory (%s)!\n\n", outputFilepath.c_str() );
}
//-------------------------------------------------------------------------
FbxExporter* pExporter = FbxExporter::Create( m_pManager, "FBX Exporter" );
if ( !pExporter->Initialize( outputFilepath.c_str(), fileFormatIDToUse, m_pManager->GetIOSettings() ) )
{
printf( "Error! Failed to initialize exporter: %s\n\n", pExporter->GetStatus().GetErrorString() );
return 1;
}
if ( pExporter->Export( pScene ) )
{
printf( "Success!\nIn: %s \nOut (%s): %s\n\n", inputFilepath.c_str(), outputFormat == FileFormat::Binary ? "binary" : "ascii", outputFilepath.c_str() );
}
else
{
printf( "Error! File export failed: - %s\n\n", pExporter->GetStatus().GetErrorString() );
}
pExporter->Destroy();
//-------------------------------------------------------------------------
return 0;
}
bool IsFbxFile( std::string const& inputFilepath )
{
assert( !inputFilepath.empty() );
int readerID = -1;
auto pIOPluginRegistry = m_pManager->GetIOPluginRegistry();
pIOPluginRegistry->DetectReaderFileFormat( inputFilepath.c_str(), readerID );
return pIOPluginRegistry->ReaderIsFBX( readerID );
}
private:
FbxConverter( FbxConverter const& ) = delete;
FbxConverter& operator=( FbxConverter const& ) = delete;
private:
FbxManager* m_pManager = nullptr;
int const m_binaryWriteID = -1;
int const m_asciiWriterID = -1;
};
//-------------------------------------------------------------------------
static void PrintErrorAndHelp( char const* pErrorMessage = nullptr )
{
printf( "================================================\n" );
printf( "FBX File Format Converter\n" );
printf( "================================================\n" );
printf( "2020 - Bobby Anguelov - MIT License\n\n" );
if ( pErrorMessage != nullptr )
{
printf( "Error! %s\n\n", pErrorMessage );
}
printf( "Convert: -c <path> [-o <output path>] {-binary|-ascii}\n" );
printf( "Query: -q <path>\n" );
}
static void PrintFileFormat( std::string const& filePath )
{
FileFormat const fileFormat = FileSystemHelpers::GetFileFormat( filePath.c_str() );
if ( fileFormat == FileFormat::Binary )
{
printf( "%s - binary\n", filePath.c_str() );
}
else if ( fileFormat == FileFormat::Ascii )
{
printf( "%s - ascii\n", filePath.c_str() );
}
else
{
printf( "%s doesnt exist or is not an FBX file!\n", filePath.c_str() );
}
}
//-------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
cli::Parser cmdParser( argc, argv );
cmdParser.disable_help();
cmdParser.set_optional<std::string>( "c", "convert", "" );
cmdParser.set_optional<std::string>( "o", "output", "" );
cmdParser.set_optional<std::string>( "q", "query", "" );
cmdParser.set_optional<bool>( "binary", "", false, "" );
cmdParser.set_optional<bool>( "ascii", "", false, "" );
if ( cmdParser.run() )
{
FbxConverter fbxConverter;
//-------------------------------------------------------------------------
auto inputConvertPath = cmdParser.get<std::string>( "c" );
if ( !inputConvertPath.empty() )
{
bool const outputAsBinary = cmdParser.get<bool>( "binary" );
bool const outputAsAscii = cmdParser.get<bool>( "ascii" );
if ( outputAsAscii && outputAsBinary )
{
PrintErrorAndHelp( "Having both -ascii and -binary arguments is not allowed." );
}
else if ( !outputAsAscii && !outputAsBinary )
{
PrintErrorAndHelp( "Either -ascii or -binary required!" );
}
else
{
FileFormat const outputFormat = outputAsBinary ? FileFormat::Binary : FileFormat::Ascii;
inputConvertPath = FileSystemHelpers::GetFullPathString( inputConvertPath );
if ( FileSystemHelpers::IsValidDirectoryPath( inputConvertPath ) )
{
std::vector<std::string> directoryContents;
FileSystemHelpers::GetDirectoryContents( inputConvertPath, directoryContents );
auto outputPath = cmdParser.get<std::string>( "o" );
if ( outputPath.empty() )
{
for ( auto& filePath : directoryContents )
{
if ( !fbxConverter.IsFbxFile( filePath.c_str() ) )
{
continue;
}
fbxConverter.ConvertFbxFile( filePath.c_str(), filePath.c_str(), outputFormat );
}
return 0;
}
else // Convert and copy
{
outputPath = FileSystemHelpers::GetFullPathString( outputPath );
for ( auto& filePath : directoryContents )
{
if ( !fbxConverter.IsFbxFile( filePath ) )
{
continue;
}
std::string newOutputPath = filePath;
newOutputPath.replace( 0, inputConvertPath.length() - 1, outputPath.c_str() );
fbxConverter.ConvertFbxFile( filePath, newOutputPath, outputFormat );
}
return 0;
}
}
else
{
auto outputPath = cmdParser.get<std::string>( "o" );
if ( outputPath.empty() )
{
return fbxConverter.ConvertFbxFile( inputConvertPath, inputConvertPath, outputFormat );
}
else
{
outputPath = FileSystemHelpers::GetFullPathString( outputPath );
return fbxConverter.ConvertFbxFile( inputConvertPath, outputPath, outputFormat );
}
}
}
}
else // check for query cmd line arg
{
auto inputQueryPath = cmdParser.get<std::string>( "q" );
if ( !inputQueryPath.empty() )
{
inputQueryPath = FileSystemHelpers::GetFullPathString( inputQueryPath );
if ( FileSystemHelpers::IsValidDirectoryPath( inputQueryPath ) )
{
std::vector<std::string> directoryContents;
FileSystemHelpers::GetDirectoryContents( inputQueryPath, directoryContents );
for ( auto& filePath : directoryContents )
{
if ( !fbxConverter.IsFbxFile( filePath ) )
{
continue;
}
PrintFileFormat( filePath );
}
}
else
{
PrintFileFormat( inputQueryPath );
}
}
else
{
PrintErrorAndHelp( "Invalid Arguments!" );
}
}
return 0;
}
else
{
PrintErrorAndHelp();
}
return 1;
}