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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include "vmtcheck_util.h"
#include "tier0/dbg.h"
#include "utldict.h"
#include "filesystem.h"
#include "FileSystem_Tools.h"
#include "KeyValues.h"
#include "cmdlib.h"
bool uselogfile = false;
struct AnalysisData
{
CUtlSymbolTable symbols;
};
static AnalysisData g_Analysis;
static bool spewed = false;
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:
//-----------------------------------------------------------------------------
void printusage( void )
{
vprint( 0, "usage: vmtcheck <materials/.vmt root directory>\n\
\t-v = verbose output\n\
\t-l = log to file log.txt\n\
\ne.g.: vmtcheck -l u:/hl2/hl2/materials\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 .vmt files\n", files.Count() );
}
}
}
}
} while ( FindNextFile( ff, &wfd ) );
}
void BuildFileList( CUtlVector< CUtlSymbol >& files, char const *rootdir, char const *extension )
{
files.RemoveAll();
BuildFileList_R( files, rootdir, extension );
}
bool ValidateVMTFile( char const *vmtname, int offset )
{
bool valid = true;
KeyValues *kv = new KeyValues( "Test" );
if ( kv->LoadFromFile( g_pFileSystem, &vmtname[offset] ) )
{
// Do any custom checking here...
}
else
{
valid = false;
}
kv->deleteThis();
return valid;
}
void ProcessMaterialsDirectory( char const *basedir )
{
vprint( 0, "building .vmt list\n" );
CUtlVector< CUtlSymbol > vmts;
BuildFileList( vmts, basedir, ".vmt" );
vprint( 0, "found %i .vmt files\n\n", vmts.Count() );
int offset = strlen( basedir ) + 1;
offset -= strlen( "materials/" );
if ( offset < 0 )
{
Error( "Bogus offset\n" );
}
// Now iterate vmts and load into memory, etc.
int c = vmts.Count();
int valid = 0;
for ( int i = 0; i < c; i++ )
{
CUtlSymbol& sym = vmts[ i ];
char const *vmtfile = g_Analysis.symbols.String( sym );
if ( verbose )
{
vprint( 0, "checking %i .vmt %s\n", i, vmtfile );
}
spewed = false;
if ( ValidateVMTFile( vmtfile, offset ) )
{
if ( !spewed )
{
valid++;
}
}
if ( i > 0 && !( i % 1000 ) )
{
vprint( 0, "Analyzed %i .vmt files (%.2f %%%%)\n", i, 100.0f * (float)i/(float)c );
}
}
int ecount = c - valid;
vprint( 0, "\nSummary: found %i/%i (%.2f percent) .vmt errors\n", ecount, c, 100.0 * ecount / max( c, 1 ) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CheckLogFile( void )
{
if ( uselogfile )
{
_unlink( "log.txt" );
vprint( 0, " Outputting to log.txt\n" );
}
}
void PrintHeader()
{
vprint( 0, "Valve Software - vmtcheck.exe (%s)\n", __DATE__ );
vprint( 0, "--- VMT File Consistency Checker ---\n" );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : argc -
// argv[] -
// Output : int
//-----------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
SpewOutputFunc( SpewFunc );
SpewActivate( "vmtcheck", 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;
default:
printusage();
break;
}
}
}
if ( argc < 2 || ( i != argc ) )
{
PrintHeader();
printusage();
}
CheckLogFile();
PrintHeader();
vprint( 0, " Looking for messed up .vmt files...\n" );
char vmtdir[ 256 ];
strcpy( vmtdir, argv[ i - 1 ] );
if ( !strstr( vmtdir, "materials" ) )
{
vprint( 0, "Materials dir %s looks invalid (format: u:/tf2/hl2/materials)\n", vmtdir );
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 );
vprint( 0, "game dir %s\nmaterials dir %s\n\n",
gamedir,
vmtdir );
Q_StripTrailingSlash( vmtdir );
ProcessMaterialsDirectory( vmtdir );
FileSystem_Term();
return 0;
}
|