summaryrefslogtreecommitdiff
path: root/utils/vmf_tweak/vmf_tweak.cpp
blob: d133d9e42824ecb08eeab0f1d8500821c40cb20d (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
// vmf_tweak.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "chunkfile.h"
#include "utllinkedlist.h"
#include <stdlib.h>
#include "tier1/strtools.h"
#include "tier0/icommandline.h"
#include "tier1/utlrbtree.h"
#include "tier1/utlbuffer.h"
#include "tier1/KeyValues.h"
#include "filesystem.h"
#include "FileSystem_Tools.h"
#include "cmdlib.h"

#include <windows.h>

char const *g_pInFilename = NULL;

char *CopyString( char const *pStr );

unsigned long g_CurLoadOrder = 0;


//-----------------------------------------------------------------------------
// default spec function
//-----------------------------------------------------------------------------
SpewRetval_t VMFTweakSpewFunc( SpewType_t spewType, char const *pMsg )
{
	OutputDebugString( pMsg );
	printf( pMsg );
	switch( spewType )
	{
	case SPEW_MESSAGE:
	case SPEW_WARNING:
	case SPEW_LOG:
		return SPEW_CONTINUE;

	case SPEW_ASSERT:
	case SPEW_ERROR:
	default:
		return SPEW_DEBUGGER;
	}
}

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 = false;
	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 );
	}
}


class CChunkKeyBase
{
public:
	unsigned long m_LoadOrder;	// Which order it appeared in the file.
};


class CKeyValue : public CChunkKeyBase
{
public:
	void SetKey( const char *pStr )
	{
		delete [] m_pKey;
		m_pKey = CopyString( pStr );
	}

	void SetValue( const char *pStr )
	{
		delete [] m_pValue;
		m_pValue = CopyString( pStr );
	}
	

public:
	char	*m_pKey;
	char	*m_pValue;
};


class CChunk : public CChunkKeyBase
{
public:
	// Returns true if the chunk has the specified key and if its value
	// is equal to pValueStr (case-insensitive).
	bool			CompareKey( char const *pKeyName, char const *pValueStr );

	// Look for a key by name.
	CKeyValue*		FindKey( char const *pKeyName );
	CKeyValue*		FindKey( char const *pKeyName, const char *pValue );

	CChunk*			FindChunk( char const *pKeyName );

	// Find a key by name, and replace any occurences with the new name.
	void			RenameKey( const char *szOldName, const char *szNewName );

public:

	char										*m_pChunkName;
	CUtlLinkedList<CKeyValue*, unsigned short>	m_Keys;
	CUtlLinkedList<CChunk*, unsigned short>		m_Chunks;
};


CChunk* ParseChunk( char const *pChunkName, bool bOnlyOne );


CChunk		*g_pCurChunk = 0;
CChunkFile	*g_pChunkFile = 0;
int g_DotCounter = 0;



// --------------------------------------------------------------------------------- //
// CChunk implementation.
// --------------------------------------------------------------------------------- //

bool CChunk::CompareKey( char const *pKeyName, char const *pValueStr )
{
	CKeyValue *pKey = FindKey( pKeyName );

	if( pKey && stricmp( pKey->m_pValue, pValueStr ) == 0 )
		return true;
	else
		return false;
}	


CKeyValue* CChunk::FindKey( char const *pKeyName )
{
	for( unsigned short i=m_Keys.Head(); i != m_Keys.InvalidIndex(); i=m_Keys.Next(i) )
	{
		CKeyValue *pKey = m_Keys[i];

		if( stricmp( pKey->m_pKey, pKeyName ) == 0 )
			return pKey;
	}

	return NULL;
}


CChunk* CChunk::FindChunk( char const *pChunkName )
{
	for( unsigned short i=m_Chunks.Head(); i != m_Chunks.InvalidIndex(); i=m_Chunks.Next(i) )
	{
		CChunk *pChunk = m_Chunks[i];

		if( stricmp( pChunk->m_pChunkName, pChunkName ) == 0 )
			return pChunk;
	}

	return NULL;
}

CKeyValue* CChunk::FindKey( char const *pKeyName, const char *pValue )
{
	for( unsigned short i=m_Keys.Head(); i != m_Keys.InvalidIndex(); i=m_Keys.Next(i) )
	{
		CKeyValue *pKey = m_Keys[i];

		if( stricmp( pKey->m_pKey, pKeyName ) == 0 && stricmp( pKey->m_pValue, pValue ) == 0 )
			return pKey;
	}

	return NULL;
}


void CChunk::RenameKey( const char *szOldName, const char *szNewName )
{
	if ((!szOldName) || (!szNewName))
		return;

	CKeyValue *pKey = FindKey( szOldName );
	if ( pKey )
	{
		delete pKey->m_pKey;
		pKey->m_pKey = CopyString( szNewName );
	}
}


// --------------------------------------------------------------------------------- //
// Util functions.
// --------------------------------------------------------------------------------- //
char *CopyString( char const *pStr )
{
	char *pRet = new char[ strlen(pStr) + 1 ];
	strcpy( pRet, pStr );
	return pRet;
}

ChunkFileResult_t MyDefaultHandler( CChunkFile *pFile, void *pData, char const *pChunkName )
{
	CChunk *pChunk = ParseChunk( pChunkName, true );
	g_pCurChunk->m_Chunks.AddToTail( pChunk );
	return ChunkFile_Ok;
}


ChunkFileResult_t MyKeyHandler( const char *szKey, const char *szValue, void *pData )
{
	// Add the key to the current chunk.
	CKeyValue *pKey = new CKeyValue;
	pKey->m_LoadOrder = g_CurLoadOrder++;
	
	pKey->m_pKey   = CopyString( szKey );
	pKey->m_pValue = CopyString( szValue );

	g_pCurChunk->m_Keys.AddToTail( pKey );
	return ChunkFile_Ok;
}


CChunk* ParseChunk( char const *pChunkName, bool bOnlyOne )
{
	// Add the new chunk.
	CChunk *pChunk = new CChunk;
	pChunk->m_pChunkName = CopyString( pChunkName );
	pChunk->m_LoadOrder = g_CurLoadOrder++;

	// Parse it out..
	CChunk *pOldChunk = g_pCurChunk;
	g_pCurChunk = pChunk;

	//if( ++g_DotCounter % 16 == 0 )
	//	printf( "." );

	while( 1 )
	{
		if( g_pChunkFile->ReadChunk( MyKeyHandler ) != ChunkFile_Ok )
			break;
	
		if( bOnlyOne )
			break;
	}

	g_pCurChunk = pOldChunk;
	return pChunk;
}


CChunk* ReadChunkFile( char const *pInFilename )
{
	CChunkFile chunkFile;

	if( chunkFile.Open( pInFilename, ChunkFile_Read ) != ChunkFile_Ok )
	{
		printf( "Error opening chunk file %s for reading.\n", pInFilename );
		return NULL;
	}

	printf( "Reading.." );
	chunkFile.SetDefaultChunkHandler( MyDefaultHandler, 0 );
	g_pChunkFile = &chunkFile;
	
	CChunk *pRet = ParseChunk( "***ROOT***", false );
	printf( "\n\n" );

	return pRet;
}

class CChunkHolder
{
public:
	static bool SortChunkFn( const CChunkHolder &a, const CChunkHolder &b )
	{
		return a.m_LoadOrder < b.m_LoadOrder;
	}

	unsigned long m_LoadOrder;
	CKeyValue *m_pKey;
	CChunk *m_pChunk;
};


void WriteChunks_R( CChunkFile *pFile, CChunk *pChunk, bool bRoot )
{
	if( !bRoot )
	{
		pFile->BeginChunk( pChunk->m_pChunkName );
	}

	// Sort them..
	CUtlRBTree<CChunkHolder,int> sortedStuff( 0, 0, &CChunkHolder::SortChunkFn );
	
	// Write keys.
	for( unsigned short i=pChunk->m_Keys.Head(); i != pChunk->m_Keys.InvalidIndex(); i = pChunk->m_Keys.Next( i ) )
	{
		CChunkHolder holder;
		holder.m_pKey = pChunk->m_Keys[i];
		holder.m_LoadOrder = holder.m_pKey->m_LoadOrder;
		holder.m_pChunk = NULL;
		sortedStuff.Insert( holder );
	}

	// Write subchunks.
	for( int i=pChunk->m_Chunks.Head(); i != pChunk->m_Chunks.InvalidIndex(); i = pChunk->m_Chunks.Next( i ) )
	{
		CChunkHolder holder;
		holder.m_pChunk = pChunk->m_Chunks[i];
		holder.m_LoadOrder = holder.m_pChunk->m_LoadOrder;
		holder.m_pKey = NULL;
		sortedStuff.Insert( holder );
	}

	// Write stuff in sorted order.
	int i = sortedStuff.FirstInorder();
	if ( i != sortedStuff.InvalidIndex() )
	{
		while ( 1 )
		{
			CChunkHolder &h = sortedStuff[i];
			if ( h.m_pKey )
			{
				pFile->WriteKeyValue( h.m_pKey->m_pKey, h.m_pKey->m_pValue );
			}
			else
			{
				WriteChunks_R( pFile, h.m_pChunk, false );
			}
			if ( i == sortedStuff.LastInorder() )
				break;
			i = sortedStuff.NextInorder( i );
		}
	}

	if( !bRoot )
	{
		pFile->EndChunk();
	}
}


bool WriteChunkFile( char const *pOutFilename, CChunk *pRoot )
{
	CChunkFile chunkFile;

	if( chunkFile.Open( pOutFilename, ChunkFile_Write ) != ChunkFile_Ok )
	{
		printf( "Error opening chunk file %s for writing.\n", pOutFilename );
		return false;
	}

	printf( "Writing.." );
	WriteChunks_R( &chunkFile, pRoot, true );
	printf( "\n\n" );

	return true;
}


//
//
// EXAMPLE
//
//
void ScanRopeSlack( CChunk *pChunk )
{
	if( stricmp( pChunk->m_pChunkName, "entity" ) == 0 )
	{
		if( pChunk->CompareKey( "classname", "keyframe_rope" ) ||
			pChunk->CompareKey( "classname", "move_rope" ) )
		{
			CKeyValue *pKey = pChunk->FindKey( "slack" );
			if( pKey )
			{
				// Subtract 100 from all the Slack properties.
				float flCur = (float)atof( pKey->m_pValue );
				char str[256];
				sprintf( str, "%f", flCur + 100 );
				pKey->m_pValue = CopyString( str );
			}
		}
	}
}

int g_nLogicAutoReplacementsMade = 0;
void LogicAuto( CChunk *pChunk )
{
	if ( pChunk->CompareKey( "classname", "logic_auto" ) )
	{
		CChunk *pConnections = pChunk->FindChunk( "connections" );
		if ( !pConnections )
			return;
		
		bool bFound = false;
		for ( int i=0; i < pConnections->m_Keys.Count(); i++ )
		{
			CKeyValue *pTestKV = pConnections->m_Keys[i];
			if ( V_stristr( pTestKV->m_pValue, "tonemap" ) == pTestKV->m_pValue )
			{
				bFound = true;
				break;
			}
		}
		
		if ( !bFound )
			return;
			
		++g_nLogicAutoReplacementsMade;			 

		// Ok, this is a logic_auto with OnMapSpawn outputs in its connections.		
		CUtlLinkedList<CKeyValue*,int> newKeys;
		FOR_EACH_LL( pConnections->m_Keys, i )
		{
			CKeyValue *pTestKV = pConnections->m_Keys[i];
			if ( V_stricmp( pTestKV->m_pKey, "OnMapSpawn" ) == 0 )
			{
				if ( V_stristr( pTestKV->m_pValue, "tonemap" ) == pTestKV->m_pValue )
				{
					CKeyValue *pNewKV = new CKeyValue;
					pNewKV->SetKey( "OnMapTransition" );
					pNewKV->SetValue( pTestKV->m_pValue );
					newKeys.AddToTail( pNewKV );
				}
			}
		}
		
		FOR_EACH_LL( newKeys, i )
		{
			if ( !pConnections->FindKey( "OnMapTransition", newKeys[i]->m_pValue ) )
			{
				pConnections->m_Keys.AddToTail( newKeys[i] );
			}
		}
		
		// Fix spawnflags.
		CKeyValue *pFlags = pChunk->FindKey("spawnflags");
		if ( pFlags )
		{
			unsigned long curVal;
			sscanf( pFlags->m_pValue, "%lu", &curVal );
			if ( curVal & 1 )
			{
				--curVal;
				char str[512];
				sprintf( str, "%lu", curVal );
				pFlags->SetValue( str );
			}
		}
	}
}


void ScanChunks( CChunk *pChunk, void (*fn)(CChunk *) )
{
	fn( pChunk );

	// Recurse into the children.
	for( unsigned short i=pChunk->m_Chunks.Head(); i != pChunk->m_Chunks.InvalidIndex(); i = pChunk->m_Chunks.Next( i ) )
	{
		ScanChunks( pChunk->m_Chunks[i], fn );
	}
}

int main(int argc, char* argv[])
{
	CommandLine()->CreateCmdLine( argc, argv );
	SpewOutputFunc( VMFTweakSpewFunc );

	if( argc < 2 )
	{
		printf( "vmf_tweak <input file> [output file]\n" );
		return 1;
	}

	g_pInFilename = argv[1];
	char const *pOutFilename = argc >= 3 ? argv[2] : "";

	CChunk *pRoot = ReadChunkFile( g_pInFilename );
	if( !pRoot )
		return 2;

	//
	//
	//
	// This is where you can put code to modify the VMF.
	//
	//
	//

	// If they didn't specify -game on the command line, use VPROJECT.
	char workingdir[ 256 ];
	workingdir[0] = 0;
	Q_getwd( workingdir, sizeof(workingdir) );
	CmdLib_InitFileSystem( workingdir );
	ScanChunks( pRoot, LogicAuto );
	FileSystem_Term();

	Msg( "%s: %d logic_auto replacements made.\n", g_pInFilename, g_nLogicAutoReplacementsMade );

	if ( argc >= 3 )
	{
		if( !WriteChunkFile( pOutFilename, pRoot ) )
			return 3;
	}

	return 0;
}