summaryrefslogtreecommitdiff
path: root/utils/scenemanager/scenemanager_tools.cpp
blob: 884ee49d43ea1766dadebadcf56563708c5bd881 (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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//===========================================================================//
#include "cbase.h"
#include "StatusWindow.h"
#include "cmdlib.h"
#include <sys/stat.h>
#include "workspace.h"
#include "workspacemanager.h"
#include "workspacebrowser.h"
#include "tier2/riff.h"
#include "sentence.h"
#include "utlbuffer.h"
#include "SoundEmitterSystem/isoundemittersystembase.h"
#include <KeyValues.h>
#include "MultipleRequest.h"

bool SceneManager_HasWindowStyle( mxWindow *w, int bits )
{
	HWND wnd = (HWND)w->getHandle();
	DWORD style = GetWindowLong( wnd, GWL_STYLE );
	return ( style & bits ) ? true : false;
}

bool SceneManager_HasWindowExStyle( mxWindow *w, int bits )
{
	HWND wnd = (HWND)w->getHandle();
	DWORD style = GetWindowLong( wnd, GWL_EXSTYLE );
	return ( style & bits ) ? true : false;
}

void SceneManager_AddWindowStyle( mxWindow *w, int addbits )
{
	HWND wnd = (HWND)w->getHandle();
	DWORD style = GetWindowLong( wnd, GWL_STYLE );
	style |= addbits;
	SetWindowLong( wnd, GWL_STYLE, style );
}

void SceneManager_AddWindowExStyle( mxWindow *w, int addbits )
{
	HWND wnd = (HWND)w->getHandle();
	DWORD style = GetWindowLong( wnd, GWL_EXSTYLE );
	style |= addbits;
	SetWindowLong( wnd, GWL_EXSTYLE, style );
}

void SceneManager_RemoveWindowStyle( mxWindow *w, int removebits )
{
	HWND wnd = (HWND)w->getHandle();
	DWORD style = GetWindowLong( wnd, GWL_STYLE );
	style &= ~removebits;
	SetWindowLong( wnd, GWL_STYLE, style );
}

void SceneManager_RemoveWindowExStyle( mxWindow *w, int removebits )
{
	HWND wnd = (HWND)w->getHandle();
	DWORD style = GetWindowLong( wnd, GWL_EXSTYLE );
	style &= ~removebits;
	SetWindowLong( wnd, GWL_EXSTYLE, style );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *w - 
//-----------------------------------------------------------------------------
void SceneManager_MakeToolWindow( mxWindow *w, bool smallcaption )
{
	SceneManager_AddWindowStyle( w, WS_VISIBLE | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS );

	if ( smallcaption )
	{
		SceneManager_AddWindowExStyle( w, WS_EX_OVERLAPPEDWINDOW );
		SceneManager_AddWindowExStyle( w, WS_EX_TOOLWINDOW );
	}
	else
	{
		SceneManager_RemoveWindowStyle( w, WS_SYSMENU );
	}
}

char *va( const char *fmt, ... )
{
	va_list args;
	static char output[4][1024];
	static int outbuffer = 0;

	outbuffer++;
	va_start( args, fmt );
	vprintf( fmt, args );
	vsprintf( output[ outbuffer & 3 ], fmt, args );
	return output[ outbuffer & 3 ];
}

void Con_Overprintf( const char *fmt, ... )
{
	va_list args;
	static char output[1024];

	va_start( args, fmt );
	vprintf( fmt, args );
	vsprintf( output, fmt, args );

	if ( !g_pStatusWindow )
	{
		return;
	}

	g_pStatusWindow->StatusPrint( CONSOLE_R, CONSOLE_G, CONSOLE_B, true, output );
}

void Con_Printf( const char *fmt, ... )
{
	va_list args;
	static char output[1024];

	va_start( args, fmt );
//	vprintf( fmt, args );
	vsprintf( output, fmt, args );
	va_end( args );

	if ( !g_pStatusWindow )
	{
		return;
	}

	g_pStatusWindow->StatusPrint( CONSOLE_R, CONSOLE_G, CONSOLE_B, false, output );
}

void Con_ColorPrintf( int r, int g, int b, const char *fmt, ... )
{
	va_list args;
	static char output[1024];

	va_start( args, fmt );
	vprintf( fmt, args );
	vsprintf( output, fmt, args );

	if ( !g_pStatusWindow )
	{
		return;
	}

	g_pStatusWindow->StatusPrint( r, g, b, false, output );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pname - 
// Output : char
//-----------------------------------------------------------------------------
char *SceneManager_MakeWindowsSlashes( char *pname )
{
	static char returnString[ 4096 ];
	strcpy( returnString, pname );
	pname = returnString;

	while ( *pname ) {
		if ( *pname == '/' )
			*pname = '\\';
		pname++;
	}

	return returnString;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : const char
//-----------------------------------------------------------------------------
const char *SceneManager_GetGameDirectory( void )
{
	// Todo: Make SceneManager only use the filesystem read/write paths.
	return gamedir;
}

//-----------------------------------------------------------------------------
// Purpose: Takes a full path and determines if the file exists on the disk
// Input  : *filename - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool SceneManager_FullpathFileExists( const char *filename )
{
	// Should be a full path
	Assert( strchr( filename, ':' ) );

	struct _stat buf;
	int result = _stat( filename, &buf );
	if ( result != -1 )
		return true;

	return false;
}

//-----------------------------------------------------------------------------
// Purpose: converts an english string to unicode
//-----------------------------------------------------------------------------
int ConvertANSIToUnicode(const char *ansi, wchar_t *unicode, int unicodeBufferSize)
{
	return ::MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode, unicodeBufferSize);
}

//-----------------------------------------------------------------------------
// Purpose: converts an unicode string to an english string
//-----------------------------------------------------------------------------
int ConvertUnicodeToANSI(const wchar_t *unicode, char *ansi, int ansiBufferSize)
{
	return ::WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, ansiBufferSize, NULL, NULL);
}

int Sys_Exec( const char *pProgName, const char *pCmdLine, bool verbose )
{
#if 0
	int count = 0;
	char cmdLine[1024];
	STARTUPINFO si;

	memset( &si, 0, sizeof(si) );
	si.cb = sizeof(si);
	//GetStartupInfo( &si );

	sprintf( cmdLine, "%s %s", pProgName, pCmdLine );

	PROCESS_INFORMATION pi;
	memset( &pi, 0, sizeof( pi ) );

	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESTDHANDLES;

	if ( CreateProcess( NULL, cmdLine, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi ) )
	{
		WaitForSingleObject( pi.hProcess, INFINITE );
		/*
		do 
		{
			WaitForInputIdle( pi.hProcess, 100 );
			count++;
		} while ( count < 100 );
		*/

		/*
		DWORD exitCode = STILL_ACTIVE;
		do 
		{
			BOOL ok = GetExitCodeProcess( pi.hProcess, &exitCode );
			if ( !ok )
				break;
			Sleep( 100 );
		} while ( exitCode == STILL_ACTIVE );
		*/
		
		DWORD exitCode;
		
		GetExitCodeProcess( pi.hProcess, &exitCode );

		Con_Printf( "Finished\n" );
		
		CloseHandle( pi.hProcess );
		return (int)exitCode;
	}
	else
	{
			char *lpMsgBuf;
			
			FormatMessage( 
				FORMAT_MESSAGE_ALLOCATE_BUFFER | 
				FORMAT_MESSAGE_FROM_SYSTEM | 
				FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL,
				GetLastError(),
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				(LPTSTR) &lpMsgBuf,
				0,
				NULL 
			);

			Con_Printf( "error %s\n", lpMsgBuf );

			LocalFree( (HLOCAL)lpMsgBuf );
	}

	return false;
#else
	char tmp[1024];
	sprintf( tmp, "%s %s\n", pProgName, pCmdLine );

	_strlwr( tmp );

	int iret = system( tmp );
	if ( iret != 0 && verbose )
	{
		Con_Printf( "Execution failed: %s\n", tmp );
	}
	
	return iret;
#endif
}

//-----------------------------------------------------------------------------
// Checks it out/ checks it in baby
//-----------------------------------------------------------------------------

static void SceneManager_VSSCheckout( char const *pUserName, char const *pProjectDir, 
	char const* pRelativeDir, char const* pDestPath, char const* pFileNameWithExtension )
{
	char buf[1024];

	// Check for the existence of the file in source safe...
	sprintf( buf, "filetype %s/%s%s -O- -y%s\n",
		pProjectDir, pRelativeDir, pFileNameWithExtension,
		pUserName );
	int retVal = Sys_Exec( "ss.exe", buf, false );
	if (retVal != 0 )
	{
		Con_Printf( "File %s missing from VSS\n", pFileNameWithExtension );
		return;
	}

	// It's there, try to check it out
	sprintf( buf, "checkout %s/%s%s -GL%s -GWA -O- -y%s\n",
		pProjectDir, pRelativeDir, pFileNameWithExtension,
		pDestPath, pUserName );
	Sys_Exec( "ss.exe", buf, true );
}

//-----------------------------------------------------------------------------
// Checks it out/ checks it in baby
//-----------------------------------------------------------------------------

static void SceneManager_VSSCheckin( char const *pUserName, char const *pProjectDir, 
	char const* pRelativeDir, char const* pDestPath, char const* pFileNameWithExtension )
{
	char buf[1024];

	// Check for the existence of the file on disk. If it's not there, don't bother
	sprintf( buf, "%s%s", pDestPath, pFileNameWithExtension );
	struct _stat statbuf;
	int result = _stat( buf, &statbuf );
	if (result != 0)
		return;

	// Check for the existence of the file in source safe...
	sprintf( buf, "filetype %s/%s%s -O- -y%s\n",
		pProjectDir, pRelativeDir, pFileNameWithExtension,
		pUserName );
	int retVal = Sys_Exec( "ss.exe", buf, false );
	if (retVal != 0)
	{
		sprintf( buf, "Cp %s -O- -y%s\n",
			pProjectDir ,
			pUserName );
		Sys_Exec( "ss.exe", buf, true );

		// Try to add the file to source safe...
		sprintf( buf, "add %s%s -GL%s -O- -I- -y%s\n",
			pRelativeDir, pFileNameWithExtension,
			pDestPath, pUserName );
		Sys_Exec( "ss.exe", buf, true );
	}
	else
	{
		// It's there, just check it in
		sprintf( buf, "checkin %s/%s%s -GL%s -O- -I- -y%s\n",
			pProjectDir, pRelativeDir, pFileNameWithExtension,
			pDestPath, pUserName );
		Sys_Exec( "ss.exe", buf, true );
	}
}

void SplitFileName( char const *in, char *path, int maxpath, char *filename, int maxfilename )
{
   char drive[_MAX_DRIVE];
   char dir[_MAX_DIR];
   char fname[_MAX_FNAME];
   char ext[_MAX_EXT];

   _splitpath( in, drive, dir, fname, ext );

   if ( dir[0] )
   {
		Q_snprintf( path, maxpath, "\\%s", dir );
   }
   else
   {
	   path[0] = 0;
   }
   Q_snprintf( filename, maxfilename, "%s%s", fname, ext );
}


void VSS_Checkout( char const *name, bool updatestaticons /*= true*/ )
{
	CWorkspace *ws = GetWorkspaceManager()->GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		return;
	}

	if ( filesystem->IsFileWritable( name ) )
	{
		return;
	}

	char path[ 256 ];
	char filename[ 256 ];

	SplitFileName( name, path, sizeof( path ), filename, sizeof( filename ) );

	Con_ColorPrintf( 200, 200, 100, "VSS Checkout:  '%s'\n", name );

	SceneManager_VSSCheckout( 
		ws->GetVSSUserName(),
		ws->GetVSSProject(),
		path,
		va( "%s%s", gamedir, path ),
		filename );

	if ( updatestaticons )
	{
		GetWorkspaceManager()->RefreshBrowsers();
	}
}

void VSS_Checkin( char const *name, bool updatestaticons /*= true*/ )
{
	CWorkspace *ws = GetWorkspaceManager()->GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		return;
	}

	if ( !filesystem->IsFileWritable( name ) )
	{
		return;
	}

	char path[ 256 ];
	char filename[ 256 ];

	SplitFileName( name, path, sizeof( path ), filename, sizeof( filename ) );

	Con_ColorPrintf( 200, 200, 100, "VSS Checkin:  '%s'\n", name );

	SceneManager_VSSCheckin( 
		ws->GetVSSUserName(),
		ws->GetVSSProject(),
		path,
		va( "%s%s", gamedir, path ),
		filename );

	if ( updatestaticons )
	{
		GetWorkspaceManager()->RefreshBrowsers();
	}
}


//-----------------------------------------------------------------------------
// Purpose: Implements the RIFF i/o interface on stdio
//-----------------------------------------------------------------------------
class StdIOReadBinary : public IFileReadBinary
{
public:
	int open( const char *pFileName )
	{
		return (int)filesystem->Open( pFileName, "rb" );
	}

	int read( void *pOutput, int size, int file )
	{
		if ( !file )
			return 0;

		return filesystem->Read( pOutput, size, (FileHandle_t)file );
	}

	void seek( int file, int pos )
	{
		if ( !file )
			return;

		filesystem->Seek( (FileHandle_t)file, pos, FILESYSTEM_SEEK_HEAD );
	}

	unsigned int tell( int file )
	{
		if ( !file )
			return 0;

		return filesystem->Tell( (FileHandle_t)file );
	}

	unsigned int size( int file )
	{
		if ( !file )
			return 0;

		return filesystem->Size( (FileHandle_t)file );
	}

	void close( int file )
	{
		if ( !file )
			return;

		filesystem->Close( (FileHandle_t)file );
	}
};

class StdIOWriteBinary : public IFileWriteBinary
{
public:
	int create( const char *pFileName )
	{
		return (int)filesystem->Open( pFileName, "wb" );
	}

	int write( void *pData, int size, int file )
	{
		return filesystem->Write( pData, size, (FileHandle_t)file );
	}

	void close( int file )
	{
		filesystem->Close( (FileHandle_t)file );
	}

	void seek( int file, int pos )
	{
		filesystem->Seek( (FileHandle_t)file, pos, FILESYSTEM_SEEK_HEAD );
	}

	unsigned int tell( int file )
	{
		return filesystem->Tell( (FileHandle_t)file );
	}
};

static StdIOReadBinary io_in;
static StdIOWriteBinary io_out;

#define RIFF_WAVE			MAKEID('W','A','V','E')
#define WAVE_FMT			MAKEID('f','m','t',' ')
#define WAVE_DATA			MAKEID('d','a','t','a')
#define WAVE_FACT			MAKEID('f','a','c','t')
#define WAVE_CUE			MAKEID('c','u','e',' ')

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &walk - 
//-----------------------------------------------------------------------------
static void SceneManager_ParseSentence( CSentence& sentence, IterateRIFF &walk )
{
	CUtlBuffer buf( 0, 0, CUtlBuffer::TEXT_BUFFER );

	buf.EnsureCapacity( walk.ChunkSize() );
	walk.ChunkRead( buf.Base() );
	buf.SeekPut( CUtlBuffer::SEEK_HEAD, walk.ChunkSize() );

	sentence.InitFromDataChunk( buf.Base(), buf.TellPut() );
}

bool SceneManager_LoadSentenceFromWavFileUsingIO( char const *wavfile, CSentence& sentence, IFileReadBinary& io )
{
	sentence.Reset();

	InFileRIFF riff( wavfile, io );

	// UNDONE: Don't use printf to handle errors
	if ( riff.RIFFName() != RIFF_WAVE )
	{
		return false;
	}

	// set up the iterator for the whole file (root RIFF is a chunk)
	IterateRIFF walk( riff, riff.RIFFSize() );

	// This chunk must be first as it contains the wave's format
	// break out when we've parsed it
	bool found = false;
	while ( walk.ChunkAvailable() && !found )
	{
		switch( walk.ChunkName() )
		{
		case WAVE_VALVEDATA:
			{
				found = true;
				SceneManager_ParseSentence( sentence, walk );
			}
			break;
		}
		walk.ChunkNext();
	}

	return true;
}

bool SceneManager_LoadSentenceFromWavFile( char const *wavfile, CSentence& sentence )
{
	return SceneManager_LoadSentenceFromWavFileUsingIO( wavfile, sentence, io_in );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : store - 
//-----------------------------------------------------------------------------
static void SceneManager_StoreValveDataChunk( CSentence& sentence, IterateOutputRIFF& store )
{
	// Buffer and dump data
	CUtlBuffer buf( 0, 0, CUtlBuffer::TEXT_BUFFER );

	sentence.SaveToBuffer( buf );

	// Copy into store
	store.ChunkWriteData( buf.Base(), buf.TellPut() );
}

bool SceneManager_SaveSentenceToWavFile( char const *wavfile, CSentence& sentence )
{
	char tempfile[ 512 ];

	Q_StripExtension( wavfile, tempfile, sizeof( tempfile ) );
	Q_DefaultExtension( tempfile, ".tmp", sizeof( tempfile ) );

	if ( filesystem->FileExists( tempfile, "GAME" ) )
	{
		filesystem->RemoveFile( tempfile, "GAME" );
	}

	if ( !filesystem->IsFileWritable( wavfile ) )
	{
		int retval = MultipleRequest( va( "Check out '%s'?", wavfile ) );
		if ( retval != 0 )
			return false;

		VSS_Checkout( wavfile );
	}

	if ( !filesystem->IsFileWritable( wavfile ) )
	{
		Con_Printf( "%s is not writable, can't save sentence data to file\n", wavfile );
		return false;
	}
	
	// Rename original wavfile to temp
	filesystem->RenameFile( wavfile, tempfile, "GAME" );

	// NOTE:  Put this in it's own scope so that the destructor for outfileRFF actually closes the file!!!!
	{
		// Read from Temp
		InFileRIFF riff( tempfile, io_in );
		Assert( riff.RIFFName() == RIFF_WAVE );

		// set up the iterator for the whole file (root RIFF is a chunk)
		IterateRIFF walk( riff, riff.RIFFSize() );

		// And put data back into original wavfile by name
		OutFileRIFF riffout( wavfile, io_out );

		IterateOutputRIFF store( riffout );

		bool wordtrackwritten = false;

		// Walk input chunks and copy to output
		while ( walk.ChunkAvailable() )
		{
			store.ChunkStart( walk.ChunkName() );

			switch ( walk.ChunkName() )
			{
			case WAVE_VALVEDATA:
				{
					// Overwrite data
					SceneManager_StoreValveDataChunk( sentence, store );
					wordtrackwritten = true;
				}
				break;
			default:
				store.CopyChunkData( walk );
				break;
			}

			store.ChunkFinish();

			walk.ChunkNext();
		}

		// If we didn't write it above, write it now
		if ( !wordtrackwritten )
		{
			store.ChunkStart( WAVE_VALVEDATA );
			SceneManager_StoreValveDataChunk( sentence, store );
			store.ChunkFinish();
		}
	}

	// Remove temp file
	filesystem->RemoveFile( tempfile, NULL );

	return true;
}


void SceneManager_LoadWindowPositions( KeyValues *kv, mxWindow *wnd )
{
	bool zoomed = kv->GetInt( "zoomed", 0 ) ? true : false;
	int x = kv->GetInt( "x", 0 );
	int y = kv->GetInt( "y", 0 );
	int w = kv->GetInt( "w", 400 );
	int h = kv->GetInt( "h", 300 );

	wnd->setBounds( x, y, w, h );
	if ( zoomed )
	{
		ShowWindow( (HWND)wnd->getHandle(), SW_SHOWMAXIMIZED );
	}
}

static void Indent( CUtlBuffer& buf, int numtabs )
{
	for ( int i = 0 ; i < numtabs; i++ )
	{
		buf.Printf( "\t" );
	}
}

void SceneManager_SaveWindowPositions( CUtlBuffer& buf, int indent, mxWindow *wnd )
{
	int x, y, w, h;

	x = wnd->x();
	y = wnd->y();
	w = wnd->w();
	h = wnd->h();

	// xpos and ypos are screen space
	POINT pt;
	pt.x = x;
	pt.y = y;

	// Convert from screen space to relative to client area of parent window so
	//  the setBounds == MoveWindow call will offset to the same location
	if ( wnd->getParent() )
	{
		ScreenToClient( (HWND)wnd->getParent()->getHandle(), &pt );
		x = (short)pt.x;
		y = (short)pt.y;
	}


	Indent( buf, indent );
	buf.Printf( "\"x\"\t\"%i\"\n", x );

	Indent( buf, indent );
	buf.Printf( "\"y\"\t\"%i\"\n", y );

	Indent( buf, indent );
	buf.Printf( "\"w\"\t\"%i\"\n", w );

	Indent( buf, indent );
	buf.Printf( "\"h\"\t\"%i\"\n", h );

	bool zoomed = IsZoomed( (HWND)wnd->getHandle() ) ? true : false;

	Indent( buf, indent );
	buf.Printf( "\"zoomed\"\t\"%i\"\n", zoomed ? 1 : 0 );

}
#if defined( _WIN32 ) || defined( WIN32 )
#define PATHSEPARATOR(c) ((c) == '\\' || (c) == '/')
#else	//_WIN32
#define PATHSEPARATOR(c) ((c) == '/')
#endif	//_WIN32

static bool charsmatch( char c1, char c2 )
{
	if ( tolower( c1 ) == tolower( c2 ) )
		return true;
	if ( PATHSEPARATOR( c1 ) && PATHSEPARATOR( c2 ) )
		return true;
	return false;
}


char *Q_stristr_slash( char const *pStr, char const *pSearch )
{
	AssertValidStringPtr(pStr);
	AssertValidStringPtr(pSearch);

	if (!pStr || !pSearch) 
		return 0;

	char const* pLetter = pStr;

	// Check the entire string
	while (*pLetter != 0)
	{
		// Skip over non-matches
		if ( charsmatch( *pLetter, *pSearch ) )
		{
			// Check for match
			char const* pMatch = pLetter + 1;
			char const* pTest = pSearch + 1;
			while (*pTest != 0)
			{
				// We've run off the end; don't bother.
				if (*pMatch == 0)
					return 0;

				if ( !charsmatch( *pMatch, *pTest ) )
					break;

				++pMatch;
				++pTest;
			}

			// Found a match!
			if (*pTest == 0)
				return (char *)pLetter;
		}

		++pLetter;
	}

	return 0;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *filename - 
//-----------------------------------------------------------------------------
void MakeFileWriteable( const char *filename )
{
	Assert( filesystem );
	char fullpath[ 512 ];
	if (filesystem->GetLocalPath( filename, fullpath, sizeof(fullpath) ))
	{
		Q_FixSlashes( fullpath );
		SetFileAttributes( fullpath, FILE_ATTRIBUTE_NORMAL );
	}
}