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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: vcd_sound_check.cpp : Defines the entry point for the console application.
//
//=============================================================================//
#include "cbase.h"
#include <stdio.h>
#include <windows.h>
#include "tier0/dbg.h"
#include "utldict.h"
#include "tier1/UtlLinkedList.h"
#include "filesystem.h"
#include "FileSystem_Tools.h"
#include "KeyValues.h"
#include "cmdlib.h"
#include "scriplib.h"
#include "vstdlib/random.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include "choreoscene.h"
#include "choreoevent.h"
#include "choreoactor.h"
#include "choreochannel.h"
#include "iscenetokenprocessor.h"
bool uselogfile = false;
struct AnalysisData
{
CUtlSymbolTable symbols;
};
static AnalysisData g_Analysis;
IFileSystem *filesystem = NULL;
static CUniformRandomStream g_Random;
IUniformRandomStream *random = &g_Random;
ISoundEmitterSystemBase *soundemitter = NULL;
static bool spewed = false;
static bool spewmoveto = false;
static bool vcdonly= false;
//-----------------------------------------------------------------------------
// Purpose: Helper for parsing scene data file
//-----------------------------------------------------------------------------
class CSceneTokenProcessor : public ISceneTokenProcessor
{
public:
const char *CurrentToken( void );
bool GetToken( bool crossline );
bool TokenAvailable( void );
void Error( const char *fmt, ... );
};
//-----------------------------------------------------------------------------
// Purpose:
// Output : const char
//-----------------------------------------------------------------------------
const char *CSceneTokenProcessor::CurrentToken( void )
{
return token;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : crossline -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CSceneTokenProcessor::GetToken( bool crossline )
{
return ::GetToken( crossline ) ? true : false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CSceneTokenProcessor::TokenAvailable( void )
{
return ::TokenAvailable() ? true : false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *fmt -
// ... -
//-----------------------------------------------------------------------------
void CSceneTokenProcessor::Error( const char *fmt, ... )
{
char string[ 2048 ];
va_list argptr;
va_start( argptr, fmt );
Q_vsnprintf( string, sizeof(string), fmt, argptr );
va_end( argptr );
Warning( "%s", string );
Assert(0);
}
static CSceneTokenProcessor g_TokenProcessor;
SpewRetval_t SpewFunc( SpewType_t type, char const *pMsg )
{
spewed = true;
printf( "%s", pMsg );
OutputDebugString( pMsg );
if ( type == SPEW_ERROR )
{
printf( "\n" );
OutputDebugString( "\n" );
}
return SPEW_CONTINUE;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : depth -
// *fmt -
// ... -
//-----------------------------------------------------------------------------
void vprint( int depth, const char *fmt, ... )
{
char string[ 8192 ];
va_list va;
va_start( va, fmt );
vsprintf( string, fmt, va );
va_end( va );
FILE *fp = NULL;
if ( uselogfile )
{
fp = fopen( "log.txt", "ab" );
}
while ( depth-- > 0 )
{
printf( " " );
OutputDebugString( " " );
if ( fp )
{
fprintf( fp, " " );
}
}
::printf( "%s", string );
OutputDebugString( string );
if ( fp )
{
char *p = string;
while ( *p )
{
if ( *p == '\n' )
{
fputc( '\r', fp );
}
fputc( *p, fp );
p++;
}
fclose( fp );
}
}
void logprint( char const *logfile, const char *fmt, ... )
{
char string[ 8192 ];
va_list va;
va_start( va, fmt );
vsprintf( string, fmt, va );
va_end( va );
FILE *fp = NULL;
static bool first = true;
if ( first )
{
first = false;
fp = fopen( logfile, "wb" );
}
else
{
fp = fopen( logfile, "ab" );
}
if ( fp )
{
char *p = string;
while ( *p )
{
if ( *p == '\n' )
{
fputc( '\r', fp );
}
fputc( *p, fp );
p++;
}
fclose( fp );
}
}
void Con_Printf( const char *fmt, ... )
{
va_list args;
static char output[1024];
va_start( args, fmt );
vprintf( fmt, args );
vsprintf( output, fmt, args );
vprint( 0, output );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void printusage( void )
{
vprint( 0, "usage: vcd_sound_check <.wav root directory> <scenes root directory>\n\
\t-v = verbose output\n\
\t-m = spew moveto info\n\
\t-o = spew vcd overlap info only\n\
\t-l = log to file log.txt\n\
\ne.g.: vcd_sound_check -l u:/hl2/hl2/sound/vo u:/hl2/hl2/scenes\n" );
// Exit app
exit( 1 );
}
void BuildFileList_R( CUtlVector< CUtlSymbol >& files, char const *dir, char const *extension )
{
WIN32_FIND_DATA wfd;
char directory[ 256 ];
char filename[ 256 ];
HANDLE ff;
sprintf( directory, "%s\\*.*", dir );
if ( ( ff = FindFirstFile( directory, &wfd ) ) == INVALID_HANDLE_VALUE )
return;
int extlen = strlen( extension );
do
{
if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
if ( wfd.cFileName[ 0 ] == '.' )
continue;
// Recurse down directory
sprintf( filename, "%s\\%s", dir, wfd.cFileName );
BuildFileList_R( files, filename, extension );
}
else
{
int len = strlen( wfd.cFileName );
if ( len > extlen )
{
if ( !stricmp( &wfd.cFileName[ len - extlen ], extension ) )
{
char filename[ MAX_PATH ];
Q_snprintf( filename, sizeof( filename ), "%s\\%s", dir, wfd.cFileName );
_strlwr( filename );
Q_FixSlashes( filename );
CUtlSymbol sym = g_Analysis.symbols.AddString( filename );
files.AddToTail( sym );
if ( !( files.Count() % 3000 ) )
{
vprint( 0, "...found %i .%s files\n", files.Count(), extension );
}
}
}
}
} while ( FindNextFile( ff, &wfd ) );
}
void BuildFileList( CUtlVector< CUtlSymbol >& files, char const *rootdir, char const *extension )
{
files.RemoveAll();
BuildFileList_R( files, rootdir, extension );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CheckLogFile( void )
{
if ( uselogfile )
{
_unlink( "log.txt" );
vprint( 0, " Outputting to log.txt\n" );
}
}
void PrintHeader()
{
vprint( 0, "Valve Software - vcd_sound_check.exe (%s)\n", __DATE__ );
vprint( 0, "--- Voice Wav File .vcd Checker ---\n" );
}
//-----------------------------------------------------------------------------
// Purpose: For each .wav file in the list, see if any vcd file in the list references it
// First build an index of .wav to .vcd mappings, then search wav list and print results
// Input : vcdfiles -
// wavfiles -
//-----------------------------------------------------------------------------
struct VCDList
{
VCDList()
{
}
VCDList( const VCDList& src )
{
int c = src.vcds.Count();
for ( int i = 0 ; i < c; i++ )
{
vcds.AddToTail( src.vcds[ i ] );
}
}
VCDList& operator =( const VCDList& src )
{
if ( this == &src )
return *this;
int c = src.vcds.Count();
for ( int i = 0 ; i < c; i++ )
{
vcds.AddToTail( src.vcds[ i ] );
}
return *this;
}
CUtlVector< CUtlSymbol > vcds;
};
static int ecounter = 0;
void SpewMoveto( bool first, char const *vcdname, CChoreoEvent *e )
{
if ( !spewmoveto )
return;
//
if ( first )
{
ecounter = 0;
}
logprint( "moveto.txt", "\"%s\",%i,\"%s\",%.3f,\"%s\",%s\n",
vcdname,
++ecounter,
e->GetName(),
e->GetStartTime(),
e->GetParameters(),
e->IsResumeCondition() ? "YES" : "no" );
}
static bool ChoreEventStartTimeLessFunc( CChoreoEvent * const &p1, CChoreoEvent * const &p2 )
{
CChoreoEvent *e1;
CChoreoEvent *e2;
e1 = const_cast< CChoreoEvent * >( p1 );
e2 = const_cast< CChoreoEvent * >( p2 );
return e1->GetStartTime() < e2->GetStartTime();
}
static bool IsFlexTrackBeingUsed( CChoreoEvent *event, char const *trackName )
{
int tc = event->GetNumFlexAnimationTracks();
for ( int track = 0; track < tc; ++track )
{
CFlexAnimationTrack *t = event->GetFlexAnimationTrack( track );
if ( !t->IsTrackActive() )
{
continue;
}
int sampleCountNormal = t->GetNumSamples( 0 );
int sampleCountBalance = 0;
if ( t->IsComboType() )
{
sampleCountBalance = t->GetNumSamples( 1 );
}
if ( !sampleCountNormal && !sampleCountBalance )
continue;
// Otherwise, see if the test track has this as an active track
if ( !Q_stricmp( t->GetFlexControllerName(), trackName ) )
{
return true;
}
}
return false;
}
static bool EventCollidesWithRows( CUtlLinkedList< CChoreoEvent*, int >& list, CChoreoEvent *event, char *trackName, size_t trackNameLength )
{
float st = event->GetStartTime();
float ed = event->GetEndTime();
for ( int i = list.Head(); i != list.InvalidIndex(); i = list.Next( i ) )
{
CChoreoEvent *test = list[ i ];
float teststart = test->GetStartTime();
float testend = test->GetEndTime();
// See if spans overlap
if ( teststart >= ed )
continue;
if ( testend <= st )
continue;
// Now see if they deal with the same flex controller
int tc = event->GetNumFlexAnimationTracks();
for ( int track = 0; track < tc; ++track )
{
CFlexAnimationTrack *t = event->GetFlexAnimationTrack( track );
if ( !t->IsTrackActive() )
{
continue;
}
int sampleCountNormal = t->GetNumSamples( 0 );
int sampleCountBalance = 0;
if ( t->IsComboType() )
{
sampleCountBalance = t->GetNumSamples( 1 );
}
if ( !sampleCountNormal && !sampleCountBalance )
continue;
// Otherwise, see if the test track has this as an active track
if ( IsFlexTrackBeingUsed( test, t->GetFlexControllerName() ) )
{
Q_strncpy( trackName, t->GetFlexControllerName(), trackNameLength );
return true;
}
}
return false;
}
return false;
}
void CheckForOverlappingFlexTracks( CChoreoScene *scene )
{
for ( int a = 0; a < scene->GetNumActors(); ++a )
{
CChoreoActor *actor = scene->GetActor( a );
CUtlRBTree< CChoreoEvent * > actorFlexEvents( 0, 0, ChoreEventStartTimeLessFunc );
for ( int c = 0; c < actor->GetNumChannels(); ++c )
{
CChoreoChannel *channel = actor->GetChannel( c );
for ( int e = 0 ; e < channel->GetNumEvents(); ++e )
{
CChoreoEvent *event = channel->GetEvent( e );
if ( event->GetType() != CChoreoEvent::FLEXANIMATION )
continue;
actorFlexEvents.Insert( event );
}
}
CUtlVector< CUtlLinkedList< CChoreoEvent*, int > > rows;
bool done = false;
int i;
// Now check for overlaps
for ( i = actorFlexEvents.FirstInorder(); i != actorFlexEvents.InvalidIndex() && !done; i = actorFlexEvents.NextInorder( i ) )
{
CChoreoEvent *e = actorFlexEvents[ i ];
if ( !rows.Count() )
{
rows.AddToTail();
CUtlLinkedList< CChoreoEvent*, int >& list = rows[ 0 ];
list.AddToHead( e );
continue;
}
// Does it come totally after what's in rows[0]?
int rowCount = rows.Count();
bool addrow = true;
for ( int j = 0; j < rowCount; j++ )
{
CUtlLinkedList< CChoreoEvent*, int >& list = rows[ j ];
char offender[ 256 ];
if ( !EventCollidesWithRows( list, e, offender, sizeof( offender ) ) )
{
// Update row event list
list.AddToHead( e );
addrow = false;
break;
}
else
{
Msg( "[%s] has overlapping events for actor [%s] [%s] [flex: %s]\n",
scene->GetFilename(), actor->GetName(), e->GetName(), offender );
done = true;
}
}
if ( addrow )
{
// Add a new row
int idx = rows.AddToTail();
CUtlLinkedList< CChoreoEvent *, int >& list = rows[ idx ];
list.AddToHead( e );
}
}
// Assert( rows.Count() <= 1 );
}
}
void ProcessVCD( CUtlDict< VCDList, int >& database, CUtlSymbol& vcdname )
{
// vprint( 0, "Processing '%s'\n", g_Analysis.symbols.String( vcdname ) );
// Load the .vcd
char fullname[ 512 ];
Q_snprintf( fullname, sizeof( fullname ), "%s", g_Analysis.symbols.String( vcdname ) );
LoadScriptFile( fullname );
CChoreoScene *scene = ChoreoLoadScene( fullname, NULL, &g_TokenProcessor, Con_Printf );
if ( scene )
{
bool first = true;
// Now iterate the events looking for speak events
int c = scene->GetNumEvents();
for ( int i = 0; i < c; i++ )
{
CChoreoEvent *e = scene->GetEvent( i );
if ( e->GetType() == CChoreoEvent::MOVETO )
{
SpewMoveto( first, fullname, e );
first = false;
}
if ( e->GetType() != CChoreoEvent::SPEAK )
continue;
// Look up sound in sound emitter system
char const *wavename = soundemitter->GetWavFileForSound( e->GetParameters(), NULL );
if ( !wavename || !wavename[ 0 ] )
{
continue;
}
char fullwavename[ 512 ];
Q_snprintf( fullwavename, sizeof( fullwavename ), "%ssound\\%s",
gamedir, wavename );
Q_FixSlashes( fullwavename );
// Now add to proper slot
VCDList *entry = NULL;
// Add vcd to database
int slot = database.Find( fullwavename );
if ( slot == database.InvalidIndex() )
{
VCDList nullEntry;
slot = database.Insert( fullwavename, nullEntry );
}
entry = &database[ slot ];
if ( entry->vcds.Find( vcdname ) == entry->vcds.InvalidIndex() )
{
entry->vcds.AddToTail( vcdname );
}
}
if ( vcdonly )
{
CheckForOverlappingFlexTracks( scene );
}
}
delete scene;
}
void CorrelateWavsAndVCDs( CUtlVector< CUtlSymbol >& vcdfiles, CUtlVector< CUtlSymbol >& wavfiles )
{
CUtlDict< VCDList, int > database;
int i;
int c = vcdfiles.Count();
for ( i = 0; i < c; i++ )
{
CUtlSymbol& vcdname = vcdfiles[ i ];
// Load the .vcd and update the database
ProcessVCD( database, vcdname );
}
if ( vcdonly )
return;
vprint( 0, "Found %i wav files in %i vcds\n",
database.Count(), vcdfiles.Count() );
// Now look for any wavfiles that weren't in the database
int ecount = 0;
c = wavfiles.Count();
for ( i = 0; i < c; i++ )
{
CUtlSymbol& wavename = wavfiles[ i ];
int idx = database.Find( g_Analysis.symbols.String( wavename ) );
if ( idx != database.InvalidIndex() )
{
VCDList *listentry = &database[ idx ];
int vcdcount = listentry->vcds.Count();
if ( vcdcount >= 2 && verbose )
{
vprint( 0, " wave '%s' used by multiple .vcds:\n", g_Analysis.symbols.String( wavename ) );
int j;
for ( j = 0; j < vcdcount; j++ )
{
vprint( 1, "%i -- '%s'\n", j+1, g_Analysis.symbols.String( listentry->vcds[ j ] ) );
}
}
continue;
}
vprint( 0, "%i -- '%s' not referenced by .vcd\n",
++ecount, g_Analysis.symbols.String( wavename ) );
}
vprint( 0, "\nSummary: found %i/%i (%.2f percent) .wav errors\n", ecount, c, 100.0 * ecount / max( c, 1 ) );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : argc -
// argv[] -
// Output : int
//-----------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
SpewOutputFunc( SpewFunc );
SpewActivate( "vcd_sound_check", 2 );
int i=1;
for ( i ; i<argc ; i++)
{
if ( argv[ i ][ 0 ] == '-' )
{
switch( argv[ i ][ 1 ] )
{
case 'l':
uselogfile = true;
break;
case 'v':
verbose = true;
break;
case 'm':
spewmoveto = true;
break;
case 'o':
vcdonly = true;
break;
default:
printusage();
break;
}
}
}
if ( argc < 3 || ( i != argc ) )
{
PrintHeader();
printusage();
}
CheckLogFile();
PrintHeader();
vprint( 0, " Looking for .wav files not referenced in .vcd files...\n" );
char sounddir[ 256 ];
char vcddir[ 256 ];
strcpy( sounddir, argv[ i - 2 ] );
strcpy( vcddir, argv[ i - 1 ] );
if ( !strstr( sounddir, "sound" ) )
{
vprint( 0, "Sound dir %s looks invalid (format: u:/tf2/hl2/sound/vo)\n", sounddir );
return 0;
}
if ( !strstr( vcddir, "scenes" ) )
{
vprint( 0, ".vcd dir %s looks invalid (format: u:/tf2/hl2/scenes)\n", vcddir );
return 0;
}
char workingdir[ 256 ];
workingdir[0] = 0;
Q_getwd( workingdir, sizeof( workingdir ) );
// If they didn't specify -game on the command line, use VPROJECT.
CmdLib_InitFileSystem( workingdir );
CSysModule *pSoundEmitterModule = g_pFullFileSystem->LoadModule( "soundemittersystem.dll" );
if ( !pSoundEmitterModule )
{
vprint( 0, "Sys_LoadModule( soundemittersystem.dll ) failed!\n" );
return 0;
}
CreateInterfaceFn hSoundEmitterFactory = Sys_GetFactory( pSoundEmitterModule );
if ( !hSoundEmitterFactory )
{
vprint( 0, "Sys_GetFactory on soundemittersystem.dll failed!\n" );
return 0;
}
soundemitter = ( ISoundEmitterSystemBase * )hSoundEmitterFactory( SOUNDEMITTERSYSTEM_INTERFACE_VERSION, NULL );
if ( !soundemitter )
{
vprint( 0, "Couldn't get interface %s from soundemittersystem.dll!\n", SOUNDEMITTERSYSTEM_INTERFACE_VERSION );
return 0;
}
filesystem = (IFileSystem *)(CmdLib_GetFileSystemFactory()( FILESYSTEM_INTERFACE_VERSION, NULL ));
if ( !filesystem )
{
AssertMsg( 0, "Failed to create/get IFileSystem" );
return 1;
}
Q_FixSlashes( gamedir );
Q_strlower( gamedir );
vprint( 0, "game dir %s\nsounds dir %s\nvcd dir %s\n\n",
gamedir,
sounddir,
vcddir );
Q_StripTrailingSlash( sounddir );
Q_StripTrailingSlash( vcddir );
filesystem->RemoveFile( "moveto.txt", "GAME" );
//
//ProcessMaterialsDirectory( vmtdir );
vprint( 0, "Initializing sound emitter system\n" );
soundemitter->Connect( FileSystem_GetFactory() );
soundemitter->Init();
vprint( 0, "Loaded %i sounds\n", soundemitter->GetSoundCount() );
vprint( 0, "Building list of .vcd files\n" );
CUtlVector< CUtlSymbol > vcdfiles;
BuildFileList( vcdfiles, vcddir, ".vcd" );
vprint( 0, "found %i .vcd files\n\n", vcdfiles.Count() );
vprint( 0, "Building list of known .wav files\n" );
CUtlVector< CUtlSymbol > wavfiles;
BuildFileList( wavfiles, sounddir, ".wav" );
vprint( 0, "found %i .wav files\n\n", wavfiles.Count() );
CorrelateWavsAndVCDs( vcdfiles, wavfiles );
soundemitter->Shutdown();
soundemitter = 0;
g_pFullFileSystem->UnloadModule( pSoundEmitterModule );
FileSystem_Term();
return 0;
}
|