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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: .360 Creation for all studiomdl generated files (mdl, vvd, vtx, ani, phy)
//
//=====================================================================================//
#include "MakeGameData.h"
#include "studiobyteswap.h"
#include "studio.h"
#include "vphysics_interface.h"
#include "materialsystem/IMaterial.h"
#include "materialsystem/hardwareverts.h"
#include "optimize.h"
//-----------------------------------------------------------------------------
// Models are already converted in a pre-pass.
//-----------------------------------------------------------------------------
bool CreateTargetFile_Model( const char *pSourceName, const char *pTargetName, bool bWriteToZip )
{
// model component should be present
CUtlBuffer targetBuffer;
if ( !scriptlib->ReadFileToBuffer( pTargetName, targetBuffer ) )
{
return false;
}
// no conversion to write, but possibly zipped
bool bSuccess = WriteBufferToFile( pTargetName, targetBuffer, bWriteToZip, WRITE_TO_DISK_NEVER );
return bSuccess;
}
//-----------------------------------------------------------------------------
// Load necessary dlls
//-----------------------------------------------------------------------------
bool InitStudioByteSwap( void )
{
StudioByteSwap::SetVerbose( false );
StudioByteSwap::ActivateByteSwapping( true );
StudioByteSwap::SetCollisionInterface( g_pPhysicsCollision );
return true;
}
//----------------------------------------------------------------------
// Get list of files that a model requires.
//----------------------------------------------------------------------
bool GetDependants_MDL( const char *pModelName, CUtlVector< CUtlString > *pList )
{
if ( !g_bModPathIsValid )
{
Msg( "Indeterminate mod path, Cannot perform BSP conversion\n" );
return false;
}
CUtlBuffer sourceBuf;
if ( !g_pFullFileSystem->ReadFile( pModelName, "GAME", sourceBuf ) )
{
Msg( "Error! Couldn't open file '%s'!\n", pModelName );
return false;
}
studiohdr_t *pStudioHdr = (studiohdr_t *)sourceBuf.Base();
Studio_ConvertStudioHdrToNewVersion( pStudioHdr );
if ( pStudioHdr->version != STUDIO_VERSION )
{
Msg( "Error! Bad Model '%s', Expecting Version (%d), got (%d)\n", pModelName, STUDIO_VERSION, pStudioHdr->version );
return false;
}
char szOutName[MAX_PATH];
if ( pStudioHdr->flags & STUDIOHDR_FLAGS_OBSOLETE )
{
V_strncpy( szOutName, "materials/sprites/obsolete.vmt", sizeof( szOutName ) );
V_FixSlashes( szOutName );
pList->AddToTail( szOutName );
}
else if ( pStudioHdr->textureindex != 0 )
{
// iterate each texture
int i;
int j;
for ( i = 0; i < pStudioHdr->numtextures; i++ )
{
// iterate through all directories until a valid material is found
bool bFound = false;
for ( j = 0; j < pStudioHdr->numcdtextures; j++ )
{
char szPath[MAX_PATH];
V_ComposeFileName( "materials", pStudioHdr->pCdtexture( j ), szPath, sizeof( szPath ) );
// should have been fixed in studiomdl
// some mdls are ending up with double slashes, borking loads
int len = strlen( szPath );
if ( len > 2 && szPath[len-2] == '\\' && szPath[len-1] == '\\' )
{
szPath[len-1] = '\0';
}
V_ComposeFileName( szPath, pStudioHdr->pTexture( i )->pszName(), szOutName, sizeof( szOutName ) );
V_SetExtension( szOutName, ".vmt", sizeof( szOutName ) );
if ( g_pFullFileSystem->FileExists( szOutName, "GAME" ) )
{
bFound = true;
break;
}
}
if ( bFound )
{
pList->AddToTail( szOutName );
}
}
}
return true;
}
//-----------------------------------------------------------------------------
// Get the preload data for a vhv file
//-----------------------------------------------------------------------------
bool GetPreloadData_VHV( const char *pFilename, CUtlBuffer &fileBufferIn, CUtlBuffer &preloadBufferOut )
{
HardwareVerts::FileHeader_t *pHeader = (HardwareVerts::FileHeader_t *)fileBufferIn.Base();
unsigned int version = BigLong( pHeader->m_nVersion );
// ensure caller's buffer is clean
// caller determines preload size, via TellMaxPut()
preloadBufferOut.Purge();
if ( version != VHV_VERSION )
{
// bad version
Msg( "Can't preload: '%s', expecting version %d got version %d\n", pFilename, VHV_VERSION, version );
return false;
}
unsigned int nPreloadSize = sizeof( HardwareVerts::FileHeader_t );
preloadBufferOut.Put( fileBufferIn.Base(), nPreloadSize );
return true;
}
//-----------------------------------------------------------------------------
// Get the preload data for a vtx file
//-----------------------------------------------------------------------------
bool GetPreloadData_VTX( const char *pFilename, CUtlBuffer &fileBufferIn, CUtlBuffer &preloadBufferOut )
{
OptimizedModel::FileHeader_t *pHeader = (OptimizedModel::FileHeader_t *)fileBufferIn.Base();
unsigned int version = BigLong( pHeader->version );
// ensure caller's buffer is clean
// caller determines preload size, via TellMaxPut()
preloadBufferOut.Purge();
if ( version != OPTIMIZED_MODEL_FILE_VERSION )
{
// bad version
Msg( "Can't preload: '%s', expecting version %d got version %d\n", pFilename, OPTIMIZED_MODEL_FILE_VERSION, version );
return false;
}
unsigned int nPreloadSize = sizeof( OptimizedModel::FileHeader_t );
preloadBufferOut.Put( fileBufferIn.Base(), nPreloadSize );
return true;
}
//-----------------------------------------------------------------------------
// Get the preload data for a vvd file
//-----------------------------------------------------------------------------
bool GetPreloadData_VVD( const char *pFilename, CUtlBuffer &fileBufferIn, CUtlBuffer &preloadBufferOut )
{
vertexFileHeader_t *pHeader = (vertexFileHeader_t *)fileBufferIn.Base();
unsigned int id = BigLong( pHeader->id );
unsigned int version = BigLong( pHeader->version );
// ensure caller's buffer is clean
// caller determines preload size, via TellMaxPut()
preloadBufferOut.Purge();
if ( id != MODEL_VERTEX_FILE_ID )
{
// bad version
Msg( "Can't preload: '%s', expecting id %d got id %d\n", pFilename, MODEL_VERTEX_FILE_ID, id );
return false;
}
if ( version != MODEL_VERTEX_FILE_VERSION )
{
// bad version
Msg( "Can't preload: '%s', expecting version %d got version %d\n", pFilename, MODEL_VERTEX_FILE_VERSION, version );
return false;
}
unsigned int nPreloadSize = sizeof( vertexFileHeader_t );
preloadBufferOut.Put( fileBufferIn.Base(), nPreloadSize );
return true;
}
bool CompressFunc( const void *pInput, int inputSize, void **pOutput, int *pOutputSize )
{
*pOutput = NULL;
*pOutputSize = 0;
if ( !inputSize )
{
// nothing to do
return false;
}
unsigned int compressedSize;
unsigned char *pCompressedOutput = LZMA_OpportunisticCompress( (unsigned char *)pInput, inputSize, &compressedSize );
if ( pCompressedOutput )
{
*pOutput = pCompressedOutput;
*pOutputSize = compressedSize;
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Rebuilds all of a MDL's components.
//-----------------------------------------------------------------------------
static bool GenerateModelFiles( const char *pMdlFilename )
{
CUtlBuffer tempBuffer;
int fileSize;
int paddedSize;
int swappedSize;
// .mdl
CUtlBuffer mdlBuffer;
if ( !scriptlib->ReadFileToBuffer( pMdlFilename, mdlBuffer ) )
{
return false;
}
if ( !Studio_ConvertStudioHdrToNewVersion( (studiohdr_t *)mdlBuffer.Base() ))
{
Msg("%s needs to be recompiled\n", pMdlFilename );
}
// .vtx
char szVtxFilename[MAX_PATH];
V_StripExtension( pMdlFilename, szVtxFilename, sizeof( szVtxFilename ) );
V_strncat( szVtxFilename, ".dx90.vtx", sizeof( szVtxFilename ) );
CUtlBuffer vtxBuffer;
bool bHasVtx = ReadFileToBuffer( szVtxFilename, vtxBuffer, false, true );
// .vvd
char szVvdFilename[MAX_PATH];
V_StripExtension( pMdlFilename, szVvdFilename, sizeof( szVvdFilename ) );
V_strncat( szVvdFilename, ".vvd", sizeof( szVvdFilename ) );
CUtlBuffer vvdBuffer;
bool bHasVvd = ReadFileToBuffer( szVvdFilename, vvdBuffer, false, true );
if ( bHasVtx != bHasVvd )
{
// paired resources, either mandates the other
return false;
}
// a .mdl file that has .vtx/.vvd gets re-processed to cull lod data
if ( bHasVtx && bHasVvd )
{
// cull lod if needed
IMdlStripInfo *pStripInfo = NULL;
bool bResult = mdllib->StripModelBuffers( mdlBuffer, vvdBuffer, vtxBuffer, &pStripInfo );
if ( !bResult )
{
return false;
}
if ( pStripInfo )
{
// .vsi
CUtlBuffer vsiBuffer;
pStripInfo->Serialize( vsiBuffer );
pStripInfo->DeleteThis();
// save strip info for later processing
char szVsiFilename[MAX_PATH];
V_StripExtension( pMdlFilename, szVsiFilename, sizeof( szVsiFilename ) );
V_strncat( szVsiFilename, ".vsi", sizeof( szVsiFilename ) );
WriteBufferToFile( szVsiFilename, vsiBuffer, false, WRITE_TO_DISK_ALWAYS );
}
}
// .ani processing may further update .mdl buffer
char szAniFilename[MAX_PATH];
V_StripExtension( pMdlFilename, szAniFilename, sizeof( szAniFilename ) );
V_strncat( szAniFilename, ".ani", sizeof( szAniFilename ) );
CUtlBuffer aniBuffer;
bool bHasAni = ReadFileToBuffer( szAniFilename, aniBuffer, false, true );
if ( bHasAni )
{
// Some vestigal .ani files exist in the tree, only process valid .ani
if ( ((studiohdr_t*)mdlBuffer.Base())->numanimblocks != 0 )
{
// .ani processing modifies .mdl buffer
fileSize = aniBuffer.TellPut();
paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING;
aniBuffer.EnsureCapacity( paddedSize );
tempBuffer.EnsureCapacity( paddedSize );
V_StripExtension( pMdlFilename, szAniFilename, sizeof( szAniFilename ) );
V_strncat( szAniFilename, ".360.ani", sizeof( szAniFilename ) );
swappedSize = StudioByteSwap::ByteswapStudioFile( szAniFilename, tempBuffer.Base(), aniBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc );
if ( swappedSize > 0 )
{
// .ani buffer is replaced with swapped data
aniBuffer.Purge();
aniBuffer.Put( tempBuffer.Base(), swappedSize );
WriteBufferToFile( szAniFilename, aniBuffer, false, WRITE_TO_DISK_ALWAYS );
}
else
{
return false;
}
}
}
// .phy
char szPhyFilename[MAX_PATH];
V_StripExtension( pMdlFilename, szPhyFilename, sizeof( szPhyFilename ) );
V_strncat( szPhyFilename, ".phy", sizeof( szPhyFilename ) );
CUtlBuffer phyBuffer;
bool bHasPhy = ReadFileToBuffer( szPhyFilename, phyBuffer, false, true );
if ( bHasPhy )
{
fileSize = phyBuffer.TellPut();
paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING;
phyBuffer.EnsureCapacity( paddedSize );
tempBuffer.EnsureCapacity( paddedSize );
V_StripExtension( pMdlFilename, szPhyFilename, sizeof( szPhyFilename ) );
V_strncat( szPhyFilename, ".360.phy", sizeof( szPhyFilename ) );
swappedSize = StudioByteSwap::ByteswapStudioFile( szPhyFilename, tempBuffer.Base(), phyBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc );
if ( swappedSize > 0 )
{
// .phy buffer is replaced with swapped data
phyBuffer.Purge();
phyBuffer.Put( tempBuffer.Base(), swappedSize );
WriteBufferToFile( szPhyFilename, phyBuffer, false, WRITE_TO_DISK_ALWAYS );
}
else
{
return false;
}
}
if ( bHasVtx )
{
fileSize = vtxBuffer.TellPut();
paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING;
vtxBuffer.EnsureCapacity( paddedSize );
tempBuffer.EnsureCapacity( paddedSize );
V_StripExtension( pMdlFilename, szVtxFilename, sizeof( szVtxFilename ) );
V_strncat( szVtxFilename, ".dx90.360.vtx", sizeof( szVtxFilename ) );
swappedSize = StudioByteSwap::ByteswapStudioFile( szVtxFilename, tempBuffer.Base(), vtxBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc );
if ( swappedSize > 0 )
{
// .vtx buffer is replaced with swapped data
vtxBuffer.Purge();
vtxBuffer.Put( tempBuffer.Base(), swappedSize );
WriteBufferToFile( szVtxFilename, vtxBuffer, false, WRITE_TO_DISK_ALWAYS );
}
else
{
return false;
}
}
if ( bHasVvd )
{
fileSize = vvdBuffer.TellPut();
paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING;
vvdBuffer.EnsureCapacity( paddedSize );
tempBuffer.EnsureCapacity( paddedSize );
V_StripExtension( pMdlFilename, szVvdFilename, sizeof( szVvdFilename ) );
V_strncat( szVvdFilename, ".360.vvd", sizeof( szVvdFilename ) );
swappedSize = StudioByteSwap::ByteswapStudioFile( szVvdFilename, tempBuffer.Base(), vvdBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc );
if ( swappedSize > 0 )
{
// .vvd buffer is replaced with swapped data
vvdBuffer.Purge();
vvdBuffer.Put( tempBuffer.Base(), swappedSize );
WriteBufferToFile( szVvdFilename, vvdBuffer, false, WRITE_TO_DISK_ALWAYS );
}
else
{
return false;
}
}
// swap and write final .mdl
fileSize = mdlBuffer.TellPut();
paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING;
mdlBuffer.EnsureCapacity( paddedSize );
tempBuffer.EnsureCapacity( paddedSize );
char szMdlFilename[MAX_PATH];
V_StripExtension( pMdlFilename, szMdlFilename, sizeof( szMdlFilename ) );
V_strncat( szMdlFilename, ".360.mdl", sizeof( szMdlFilename ) );
swappedSize = StudioByteSwap::ByteswapStudioFile( szMdlFilename, tempBuffer.Base(), mdlBuffer.PeekGet(), fileSize, NULL, CompressFunc );
if ( swappedSize > 0 )
{
// .mdl buffer is replaced with swapped data
mdlBuffer.Purge();
mdlBuffer.Put( tempBuffer.Base(), swappedSize );
WriteBufferToFile( szMdlFilename, mdlBuffer, false, WRITE_TO_DISK_ALWAYS );
}
else
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Returns true if specified model path has a dirty sub-component, and requires
// update.
//-----------------------------------------------------------------------------
static bool ModelNeedsUpdate( const char *pMdlSourcePath )
{
struct ModelExtensions_t
{
const char *pSourceExtension;
const char *pTargetExtension;
bool bSourceMustExist; // if source exists, so must target
};
ModelExtensions_t pExtensions[] =
{
{ ".mdl", ".360.mdl", true },
{ ".dx90.vtx", ".dx90.360.vtx", false },
{ ".vvd", ".360.vvd", false },
{ ".phy", ".360.phy", false },
{ ".ani", ".360.ani", false },
// vtx/vvd generate a vsi, vsi must be fresher to be valid
{ ".dx90.vtx", ".vsi", false },
{ ".vvd", ".vsi", false },
};
if ( g_bForce )
{
return true;
}
for ( int i = 0; i < ARRAYSIZE( pExtensions ); i++ )
{
char szSourcePath[MAX_PATH];
struct _stat sourceStatBuf;
V_strncpy( szSourcePath, pMdlSourcePath, sizeof( szSourcePath ) );
V_SetExtension( szSourcePath, pExtensions[i].pSourceExtension, sizeof( szSourcePath ) );
int retVal = _stat( szSourcePath, &sourceStatBuf );
if ( retVal != 0 )
{
// couldn't get source
if ( pExtensions[i].bSourceMustExist )
{
return true;
}
else
{
// source is optional
continue;
}
}
char szTargetPath[MAX_PATH];
struct _stat targetStatBuf;
V_strncpy( szTargetPath, pMdlSourcePath, sizeof( szTargetPath ) );
V_SetExtension( szTargetPath, pExtensions[i].pTargetExtension, sizeof( szTargetPath ) );
if ( _stat( szTargetPath, &targetStatBuf ) != 0 )
{
// target doesn't exist
return true;
}
if ( difftime( sourceStatBuf.st_mtime, targetStatBuf.st_mtime ) > 0 )
{
// source is older (thus newer), update required
return true;
}
}
return false;
}
static bool ModelNamesLessFunc( CUtlString const &pLHS, CUtlString const &pRHS )
{
return CaselessStringLessThan( pLHS.Get(), pRHS.Get() );
}
//-----------------------------------------------------------------------------
// Models require specialized group handling to generate intermediate lod culled
// versions that are then used as the the source for target conversion.
//-----------------------------------------------------------------------------
bool PreprocessModelFiles( CUtlVector<fileList_t> &fileList )
{
if ( !InitStudioByteSwap() )
{
return false;
}
CUtlVector< CUtlString > updateList;
CUtlRBTree< CUtlString, int > visitedModels( 0, 0, ModelNamesLessFunc );
char szSourcePath[MAX_PATH];
strcpy( szSourcePath, g_szSourcePath );
V_StripFilename( szSourcePath );
if ( !szSourcePath[0] )
strcpy( szSourcePath, "." );
V_AppendSlash( szSourcePath, sizeof( szSourcePath ) );
char szModelName[MAX_PATH];
for ( int i=0; i<fileList.Count(); i++ )
{
V_strncpy( szModelName, fileList[i].fileName.String(), sizeof( szModelName ) );
if ( V_stristr( szModelName, ".360." ) )
{
// must ignore any target files
continue;
}
// want only model related files
char *pExtension = V_stristr( szModelName, ".mdl" );
if ( !pExtension )
{
pExtension = V_stristr( szModelName, ".dx90.vtx" );
if ( !pExtension )
{
pExtension = V_stristr( szModelName, ".vvd" );
if ( !pExtension )
{
pExtension = V_stristr( szModelName, ".ani" );
if ( !pExtension )
{
pExtension = V_stristr( szModelName, ".phy" );
if ( !pExtension )
{
pExtension = V_stristr( szModelName, ".vsi" );
if ( !pExtension )
{
continue;
}
}
}
}
}
}
*pExtension = '\0';
V_strncat( szModelName, ".mdl", sizeof( szModelName ) );
if ( visitedModels.Find( szModelName ) != visitedModels.InvalidIndex() )
{
// already processed
continue;
}
visitedModels.Insert( szModelName );
// resolve to full source path
const char *ptr = szModelName;
if ( !strnicmp( ptr, ".\\", 2 ) )
ptr += 2;
else if ( !strnicmp( ptr, szSourcePath, strlen( szSourcePath ) ) )
ptr += strlen( szSourcePath );
char szCleanName[MAX_PATH];
strcpy( szCleanName, szSourcePath );
strcat( szCleanName, ptr );
char szFullSourcePath[MAX_PATH];
_fullpath( szFullSourcePath, szCleanName, sizeof( szFullSourcePath ) );
// any one dirty component generates the set of all expected files
if ( ModelNeedsUpdate( szFullSourcePath ) )
{
int index = updateList.AddToTail();
updateList[index].Set( szFullSourcePath );
}
}
Msg( "\n" );
Msg( "Model Pre Pass: Updating %d Models.\n", updateList.Count() );
for ( int i = 0; i < updateList.Count(); i++ )
{
if ( !GenerateModelFiles( updateList[i].String() ) )
{
int error = g_errorList.AddToTail();
g_errorList[error].result = false;
g_errorList[error].fileName.Set( updateList[i].String() );
}
}
// iterate error list
if ( g_errorList.Count() )
{
Msg( "\n" );
for ( int i = 0; i < g_errorList.Count(); i++ )
{
Msg( "ERROR: could not pre-process model: %s\n", g_errorList[i].fileName.String() );
}
}
return true;
}
|