summaryrefslogtreecommitdiff
path: root/hammer/manifest.cpp
blob: 7a1a81d643a3c83a8bf7448e62fc4a7cab0b7fa1 (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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=====================================================================================//

#include "stdafx.h"
#include "Manifest.h"
#include "CustomMessages.h"
#include "GlobalFunctions.h"
#include "MainFrm.h"
#include "MapDoc.h"
#include "MapSolid.h"
#include "MapWorld.h"
#include "MapInstance.h"
#include "ToolManager.h"
#include "ChunkFile.h"
#include "ManifestDialog.h"
#include "History.h"
#include "HelperFactory.h"
#include "SaveInfo.h"
#include "tier2/tier2.h"
#include "p4lib/ip4.h"

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>

IMPLEMENT_DYNCREATE(CManifest, CMapDoc)


BEGIN_MESSAGE_MAP(CManifest, CMapDoc)
	//{{AFX_MSG_MAP(CManifest)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


IMPLEMENT_MAPCLASS( CManifestInstance )


//-----------------------------------------------------------------------------
// Purpose: default constructor
//-----------------------------------------------------------------------------
CManifestMap::CManifestMap( void )
{
	m_Map = NULL;
	m_RelativeMapFileName = "";
	m_AbsoluteMapFileName = "";
	m_FriendlyName = "unnamed";
	m_bTopLevelMap = false;
	m_bPrimaryMap = false;
	m_bProtected = false;
	m_bReadOnly = false;
	m_bIsVersionControlled = false;
	m_bCheckedOut = false;
	m_bDefaultCheckin = false;
	m_bVisible = true;
	m_Entity = NULL;
	m_InternalID = 0;
}


//-----------------------------------------------------------------------------
// Purpose: returns true if the manifest map is editable
//-----------------------------------------------------------------------------
bool CManifestMap::IsEditable( void )
{
	return ( m_bProtected == false && m_bReadOnly == false && m_bPrimaryMap );
}


//-----------------------------------------------------------------------------
// Purpose: default constructor
//-----------------------------------------------------------------------------
CManifestInstance::CManifestInstance( void ) :
	CMapEntity()
{
	m_pManifestMap = NULL;
}


//-----------------------------------------------------------------------------
// Purpose: default constructor
//-----------------------------------------------------------------------------
CManifestInstance::CManifestInstance( CManifestMap *pManifestMap ) :
	CMapEntity()
{
	m_pManifestMap = pManifestMap;
}


//-----------------------------------------------------------------------------
// Purpose: returns true if the manifest map this instance owns is editable
//-----------------------------------------------------------------------------
bool CManifestInstance::IsEditable( void )
{
	return m_pManifestMap->IsEditable();
}


//-----------------------------------------------------------------------------
// Purpose: default constructor
//-----------------------------------------------------------------------------
CManifest::CManifest( void ) :
	CMapDoc()
{
	m_bIsValid = false;
	m_bRelocateSave = false;
	m_ManifestDir[ 0 ] = 0;
	m_pPrimaryMap = NULL;
	m_ManifestWorld = NULL;
	m_NextInternalID = 1;
	m_bManifestChanged = false;
	m_bManifestUserPrefsChanged = false;
	m_pSaveUndo = m_pUndo;
	m_pSaveRedo = m_pRedo;
	m_bReadOnly = true;
	m_bIsVersionControlled = false;
	m_bCheckedOut = false;
	m_bDefaultCheckin = false;
}


//-----------------------------------------------------------------------------
// Purpose: default destructor
//-----------------------------------------------------------------------------
CManifest::~CManifest( void )
{
	m_Maps.PurgeAndDeleteElements();
}


//-----------------------------------------------------------------------------
// Purpose: this function will parse through the known keys for the manifest map entry
// Input  : szKey - the key name
//			szValue - the value
//			pManifestMap - the manifest map this belongs to
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadKeyInfoCallback( const char *szKey, const char *szValue, CManifest *pDoc )
{
	if ( !stricmp( szKey, "NextInternalID" ) )
	{
		pDoc->m_NextInternalID = atoi( szValue );
	}

	return ChunkFile_Ok;
}


//-----------------------------------------------------------------------------
// Purpose: this function is responsible for setting up the manifest map about to be read in
// Input  : pFile - the chunk file being read
//			pDoc - the owning manifest document
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadManifestInfoCallback( CChunkFile *pFile, CManifest *pDoc )
{
	ChunkFileResult_t eResult = pFile->ReadChunk( ( KeyHandler_t )LoadKeyInfoCallback, pDoc );

	return( eResult );
}


//-----------------------------------------------------------------------------
// Purpose: this function will parse through the known keys for the manifest map entry
// Input  : szKey - the key name
//			szValue - the value
//			pManifestMap - the manifest map this belongs to
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadKeyCallback( const char *szKey, const char *szValue, CManifestMap *pManifestMap )
{
	if ( !stricmp( szKey, "InternalID" ) )
	{
		pManifestMap->m_InternalID = atoi( szValue );
	}
	else if ( !stricmp( szKey, "Name" ) )
	{
		pManifestMap->m_FriendlyName = szValue;
	}
	else if ( !stricmp( szKey, "File" ) )
	{
		pManifestMap->m_RelativeMapFileName = szValue;
		pManifestMap->m_AbsoluteMapFileName += szValue;
		if ( !pManifestMap->m_Map->LoadVMF( pManifestMap->m_AbsoluteMapFileName, VMF_LOAD_ACTIVATE | VMF_LOAD_IS_SUBMAP ) )
		{
			delete pManifestMap->m_Map;
			pManifestMap->m_Map = NULL;
		}
		pManifestMap->m_bReadOnly = true;
	}
	else if ( !stricmp( szKey, "TopLevel" ) )
	{
		pManifestMap->m_bTopLevelMap = ( atoi( szValue ) == 1 );
	}

	return ChunkFile_Ok;
}


//-----------------------------------------------------------------------------
// Purpose: this function is responsible for setting up the manifest map about to be read in
// Input  : pFile - the chunk file being read
//			pDoc - the owning manifest document
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadManifestVMFCallback( CChunkFile *pFile, CManifest *pDoc )
{
	char FileName[ MAX_PATH ];

	strcpy( FileName, pDoc->m_ManifestDir );

	CManifestMap	*pManifestMap = pDoc->CreateNewMap( FileName, "", false );
	SetActiveMapDoc( pManifestMap->m_Map );

	ChunkFileResult_t eResult = pFile->ReadChunk( ( KeyHandler_t )LoadKeyCallback, pManifestMap );

	if ( pManifestMap->m_Map )
	{
		pManifestMap->m_Map->SetEditable( false );
	}
	SetActiveMapDoc( pDoc );

	return( eResult );
}


//-----------------------------------------------------------------------------
// Purpose: this function will load the VMF chunk
// Input  : pFile - the chunk file being read
//			pDoc - the owning manifest document
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadManifestMapsCallback( CChunkFile *pFile, CManifest *pDoc )
{
	CChunkHandlerMap Handlers;
	Handlers.AddHandler( "VMF", ( ChunkHandler_t )LoadManifestVMFCallback, pDoc );
	pFile->PushHandlers(&Handlers);

	ChunkFileResult_t eResult = ChunkFile_Ok;

	eResult = pFile->ReadChunk();

	pFile->PopHandlers();

	return( eResult );
}


typedef struct SManifestLoadPrefs
{
	CManifest		*pDoc;
	CManifestMap	*pManifestMap;
} TManifestLoadPrefs;


//-----------------------------------------------------------------------------
// Purpose: this function will parse through the known keys for the manifest map entry
// Input  : szKey - the key name
//			szValue - the value
//			pManifestMap - the manifest map this belongs to
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadKeyPrefsCallback( const char *szKey, const char *szValue, TManifestLoadPrefs *pManifestLoadPrefs )
{
	if ( !stricmp( szKey, "InternalID" ) )
	{
		pManifestLoadPrefs->pManifestMap = pManifestLoadPrefs->pDoc->FindMapByID( atoi( szValue ) );
	}
	else if ( !stricmp( szKey, "IsPrimary" ) )
	{
		if ( pManifestLoadPrefs->pManifestMap )
		{
			pManifestLoadPrefs->pManifestMap->m_bPrimaryMap = ( atoi( szValue ) == 1 );
		}
	}
	else if ( !stricmp( szKey, "IsProtected" ) )
	{
		if ( pManifestLoadPrefs->pManifestMap )
		{
			pManifestLoadPrefs->pManifestMap->m_bProtected = ( atoi( szValue ) == 1 );
		}
	}
	else if ( !stricmp( szKey, "IsVisible" ) )
	{
		if ( pManifestLoadPrefs->pManifestMap )
		{
			pManifestLoadPrefs->pManifestMap->m_bVisible = ( atoi( szValue ) == 1 );
		}
	}

	return ChunkFile_Ok;
}


//-----------------------------------------------------------------------------
// Purpose: this function is responsible for setting up the manifest map about to be read in
// Input  : pFile - the chunk file being read
//			pDoc - the owning manifest document
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadManifestVMFPrefsCallback( CChunkFile *pFile, CManifest *pDoc )
{
	TManifestLoadPrefs	ManifestLoadPrefs;
	
	ManifestLoadPrefs.pDoc = pDoc;
	ManifestLoadPrefs.pManifestMap = NULL;

	ChunkFileResult_t eResult = pFile->ReadChunk( ( KeyHandler_t )LoadKeyPrefsCallback, &ManifestLoadPrefs );

	return( eResult );
}


//-----------------------------------------------------------------------------
// Purpose: this function will load the VMF chunk
// Input  : pFile - the chunk file being read
//			pDoc - the owning manifest document
// Output : ChunkFileResult_t - result of the parsing
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadManifestMapsPrefsCallback( CChunkFile *pFile, CManifest *pDoc )
{
	CChunkHandlerMap Handlers;
	Handlers.AddHandler( "VMF", ( ChunkHandler_t )LoadManifestVMFPrefsCallback, pDoc );
	pFile->PushHandlers(&Handlers);

	ChunkFileResult_t eResult = ChunkFile_Ok;

	eResult = pFile->ReadChunk();

	pFile->PopHandlers();

	return( eResult );
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : 
// Output : 
//-----------------------------------------------------------------------------
ChunkFileResult_t CManifest::LoadManifestCordoningPrefsCallback( CChunkFile *pFile, CManifest *pDoc )
{
	CChunkHandlerMap Handlers;
	Handlers.AddHandler( "cordons", ( ChunkHandler_t )CMapDoc::LoadCordonCallback, pDoc );
	pFile->PushHandlers(&Handlers);

	ChunkFileResult_t eResult = ChunkFile_Ok;

	eResult = pFile->ReadChunk();

	pFile->PopHandlers();

	return( eResult );
}


//-----------------------------------------------------------------------------
// Purpose: This function will load in a vmf manifest
// Input  : pszFileName - the file name of the manifest to load
// Output : returns true if the load was successful
//-----------------------------------------------------------------------------
bool CManifest::LoadVMFManifest( const char *pszFileName )
{
	FILE *fp = fopen( pszFileName, "rb" );
	if ( !fp )
	{
		return false;
	}

	V_StripExtension( pszFileName, m_ManifestDir, sizeof( m_ManifestDir ) );
	strcat( m_ManifestDir, "\\" );

	CChunkFile File;
	ChunkFileResult_t eResult = File.Open( pszFileName, ChunkFile_Read );

	m_bLoading = true;

	if (eResult == ChunkFile_Ok)
	{
		//
		// Set up handlers for the subchunks that we are interested in.
		//
		CChunkHandlerMap Handlers;
		Handlers.AddHandler( "Info", ( ChunkHandler_t )CManifest::LoadManifestInfoCallback, this );
		Handlers.AddHandler( "Maps", ( ChunkHandler_t )CManifest::LoadManifestMapsCallback, this );

		Handlers.SetErrorHandler( ( ChunkErrorHandler_t )CMapDoc::HandleLoadError, this);

		File.PushHandlers(&Handlers);

		while (eResult == ChunkFile_Ok)
		{
			eResult = File.ReadChunk();
		}

		if (eResult == ChunkFile_EOF)
		{
			eResult = ChunkFile_Ok;
		}

		File.PopHandlers();
	}

	if (eResult == ChunkFile_Ok)
	{
	}
	else
	{
		GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Error loading manifest!", MB_OK | MB_ICONEXCLAMATION );
	}

	if ( GetNumMaps() == 0 )
	{
		GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Manifest file does not contain any maps!", MB_OK | MB_ICONEXCLAMATION );
		return false;
	}

	SetActiveMapDoc( this );
	Postload( pszFileName );
	m_ManifestWorld->PostloadWorld();

	bool bSetIDs = false;

	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		if ( pManifestMap->m_InternalID == 0 )
		{
			pManifestMap->m_InternalID = m_NextInternalID;
			m_NextInternalID++;
			bSetIDs = true;
		}

		if ( pManifestMap->m_Map == NULL || pManifestMap->m_Map->GetMapWorld() == NULL )
		{
			pManifestMap->m_bPrimaryMap = false;
			RemoveSubMap( pManifestMap );
			i = -1;
		}
	}

	LoadVMFManifestUserPrefs( pszFileName );

	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		if ( pManifestMap->m_bPrimaryMap )
		{
			SetPrimaryMap( pManifestMap );
		}
	}

	if ( !m_pPrimaryMap )
	{
		SetPrimaryMap( GetMap( 0 ) );
	}

	m_bLoading = false;
	m_bIsValid = true;

	m_bManifestChanged = bSetIDs;

	GetMainWnd()->m_ManifestFilterControl.UpdateManifestList();

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: this function will load the user prefs for the manifest file.
// Input  : pszFileName - the manifest file name.
// Output : true if prefs were loaded.
//-----------------------------------------------------------------------------
bool CManifest::LoadVMFManifestUserPrefs( const char *pszFileName )
{
	char		UserName[ MAX_PATH ], FileName[ MAX_PATH ], UserPrefsFileName[ MAX_PATH ];
	DWORD		UserNameSize;

	m_bManifestUserPrefsChanged = false;

	UserNameSize = sizeof( UserName );
	if ( GetUserName( UserName, &UserNameSize ) == 0 )
	{
		strcpy( UserPrefsFileName, "default" );
	}

	strcpy( FileName, m_ManifestDir );
	sprintf( UserPrefsFileName, "%s.vmm_prefs", UserName );
	strcat( FileName, UserPrefsFileName );

	FILE *fp = fopen( FileName, "rb" );
	if ( !fp )
	{
		return false;
	}

	CChunkFile File;
	ChunkFileResult_t eResult = File.Open( FileName, ChunkFile_Read );

	m_bLoading = true;

	if ( eResult == ChunkFile_Ok )
	{
		//
		// Set up handlers for the subchunks that we are interested in.
		//
		CChunkHandlerMap Handlers;
		Handlers.AddHandler( "Maps", ( ChunkHandler_t )CManifest::LoadManifestMapsPrefsCallback, this );
		Handlers.AddHandler( "cordoning", ( ChunkHandler_t )CManifest::LoadManifestCordoningPrefsCallback, this );

		Handlers.SetErrorHandler( ( ChunkErrorHandler_t )CMapDoc::HandleLoadError, this);

		File.PushHandlers(&Handlers);

		while( eResult == ChunkFile_Ok )
		{
			eResult = File.ReadChunk();
		}

		if ( eResult == ChunkFile_EOF )
		{
			eResult = ChunkFile_Ok;
		}

		File.PopHandlers();
	}

	if ( eResult == ChunkFile_Ok )
	{
	}
	else
	{
		// no pref message for now
//		GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Error loading manifest!", MB_OK | MB_ICONEXCLAMATION );
	}

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: this function will load in a manifest file ( and its user prefs )
// Input  : pszFileName - the name of the manifest file.
// Output : returns true if the manifest was loaded.
//-----------------------------------------------------------------------------
bool CManifest::Load( const char *pszFileName )
{
	if ( !LoadVMFManifest( pszFileName ) )
	{
		return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: This function will save the manifest, the associated maps, and user prefs. 
// Input  : pszFileName - the name of the manifest
//			bForce - if true, we need to save all files, as we are relocating.
// Output : returns true if all files saved successfully.
//-----------------------------------------------------------------------------
bool CManifest::Save( const char *pszFileName, bool bForce )
{
	bool	bSuccess = true;

	if ( bForce || m_bManifestChanged )
	{
		if ( !SaveVMFManifest( pszFileName ) )
		{
			bSuccess = false;
		}
	}

	if ( !SaveVMFManifestMaps( pszFileName ) )
	{
		bSuccess = false;
	}

//	if ( bForce || m_bManifestUserPrefsChanged )
	{
		if ( !SaveVMFManifestUserPrefs( pszFileName ) )
		{
			bSuccess = false;
		}
	}

	return bSuccess;
}


//-----------------------------------------------------------------------------
// Purpose: this function will save the manifest file and all modified maps.  If we are
//			relocating the manifest to a new place, all maps will be saved relative to 
//			the new place
// Input  : the file name the manifest should be saved as
// Output : returns true if the save was completely successful.  A partial save will
//			return false.
//-----------------------------------------------------------------------------
bool CManifest::SaveVMFManifest( const char *pszFileName )
{
	bool		bSaved = true;
	CChunkFile	File;	

	ChunkFileResult_t eResult = File.Open( pszFileName, ChunkFile_Write );
	if (eResult != ChunkFile_Ok)
	{
		GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Error saving Manifest!" , MB_OK | MB_ICONEXCLAMATION );
		bSaved = false;
	}
	else
	{
		eResult = File.BeginChunk( "Info" );
		eResult = File.WriteKeyValueInt( "NextInternalID", m_NextInternalID );
		eResult = File.EndChunk();

		eResult = File.BeginChunk( "Maps" );
		if (eResult == ChunkFile_Ok)
		{
			for( int i = 0; i < GetNumMaps(); i++ )
			{
				CManifestMap	*pManifestMap = GetMap( i );

				eResult = File.BeginChunk("VMF");
				if (eResult == ChunkFile_Ok)
				{
					eResult = File.WriteKeyValue( "Name", pManifestMap->m_FriendlyName );
					eResult = File.WriteKeyValue( "File", pManifestMap->m_RelativeMapFileName );
					eResult = File.WriteKeyValueInt( "InternalID", pManifestMap->m_InternalID );
					if ( pManifestMap->m_bTopLevelMap == true )
					{
						eResult = File.WriteKeyValue( "TopLevel", "1" );
					}

					eResult = File.EndChunk();
				}
			}
		}

		if (eResult == ChunkFile_Ok)
		{
			eResult = File.EndChunk();
		}
		else
		{
			GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Error saving Manifest!", MB_OK | MB_ICONEXCLAMATION );
			bSaved = false;
		}

		File.Close();
	}

	V_StripExtension( pszFileName, m_ManifestDir, sizeof( m_ManifestDir ) );
	CreateDirectory( m_ManifestDir, NULL );
	strcat( m_ManifestDir, "\\" );

	if ( bSaved )
	{
		m_bManifestChanged = false;
	}

	return bSaved;
}


//-----------------------------------------------------------------------------
// Purpose: This function will save all maps associated with a manifest.  Only modified
//			maps are saved unless we are relocating the manifest.
// Input  : pszFileName - the name of the manifest file
// Output : returns true if the maps were saved successfully.
//-----------------------------------------------------------------------------
bool CManifest::SaveVMFManifestMaps( const char *pszFileName )
{
	bool	bSaved = true;

	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		if ( m_bRelocateSave )
		{
			char FileName[ MAX_PATH ];

			strcpy( FileName, m_ManifestDir );
			strcat( FileName, pManifestMap->m_RelativeMapFileName );
			pManifestMap->m_AbsoluteMapFileName = FileName;
		}

		if ( ( pManifestMap->m_Map->IsModified() || m_bRelocateSave ) )
		{
			if ( pManifestMap->m_Map->SaveVMF( pManifestMap->m_AbsoluteMapFileName, 0 ) == false )
			{
				bSaved = false;
			}
		}
	}

	if ( !bSaved )
	{
		GetMainWnd()->MessageBox( "Not all pieces of the manifest were saved!", "Error saving Manifest!", MB_OK | MB_ICONEXCLAMATION );
	}

	return bSaved;
}


//-----------------------------------------------------------------------------
// Purpose: this function will save the user prefs of the manifest.
// Input  : pszFileName - the name of the manifest file.
// Output : returns true if the prefs were saved.
//-----------------------------------------------------------------------------
bool CManifest::SaveVMFManifestUserPrefs( const char *pszFileName )
{
	bool		bSaved = true;
	CChunkFile	File;	
	char		UserName[ MAX_PATH ], FileName[ MAX_PATH ], UserPrefsFileName[ MAX_PATH ];
	DWORD		UserNameSize;

	UserNameSize = sizeof( UserName );
	if ( GetUserName( UserName, &UserNameSize ) == 0 )
	{
		strcpy( UserPrefsFileName, "default" );
	}

	strcpy( FileName, m_ManifestDir );
	sprintf( UserPrefsFileName, "%s.vmm_prefs", UserName );
	strcat( FileName, UserPrefsFileName );

	ChunkFileResult_t eResult = File.Open( FileName, ChunkFile_Write );
	if (eResult != ChunkFile_Ok)
	{
		GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Error saving Manifest User Prefs!" , MB_OK | MB_ICONEXCLAMATION );
		bSaved = false;
	}
	else
	{
		eResult = File.BeginChunk( "Maps" );
		if (eResult == ChunkFile_Ok)
		{
			for( int i = 0; i < GetNumMaps(); i++ )
			{
				CManifestMap	*pManifestMap = GetMap( i );

				eResult = File.BeginChunk("VMF");
				if (eResult == ChunkFile_Ok)
				{
					eResult = File.WriteKeyValueInt( "InternalID", pManifestMap->m_InternalID );
					if ( pManifestMap->m_bPrimaryMap )
					{
						eResult = File.WriteKeyValue( "IsPrimary", "1" );
					}
					if ( pManifestMap->m_bProtected == true )
					{
						eResult = File.WriteKeyValue( "IsProtected", "1" );
					}
					if ( pManifestMap->m_bVisible == false )
					{
						eResult = File.WriteKeyValue( "IsVisible", "0" );
					}

					eResult = File.EndChunk();
				}
			}
		}

		if (eResult == ChunkFile_Ok)
		{
			eResult = File.EndChunk();
		}
		else
		{
			GetMainWnd()->MessageBox( File.GetErrorText( eResult ), "Error saving Manifest User Prefs!", MB_OK | MB_ICONEXCLAMATION );
			bSaved = false;
		}

		eResult = File.BeginChunk( "cordoning" );
		eResult = CordonSaveVMF( &File, NULL );

		if ( m_bIsCordoning )
		{
			CSaveInfo	SaveInfo;

			SaveInfo.SetVisiblesOnly( false );
			CMapWorld *pCordonWorld = CordonCreateWorld();
			eResult = pCordonWorld->SaveSolids( &File, &SaveInfo, 0 );
		}

		eResult = File.EndChunk();

		File.Close();
	}

	if ( bSaved )
	{
		m_bManifestUserPrefsChanged = false;
	}

	return bSaved;
}


//-----------------------------------------------------------------------------
// Purpose: this function will initialize the manifest
//-----------------------------------------------------------------------------
void CManifest::Initialize( void )
{
	__super::Initialize();

	m_ManifestWorld = new CMapWorld( this );
	m_ManifestWorld->CullTree_Build();
}


//-----------------------------------------------------------------------------
// Purpose: this function will update the manifest and all of its sub maps
//-----------------------------------------------------------------------------
void CManifest::Update( void )
{
	__super::Update();

	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		pManifestMap->m_Map->Update();
	}
}


//-----------------------------------------------------------------------------
// Purpose: this function allows you to indicate if the user prefs have been modified.
// Input  : bModified - the new status of the user prefs
//-----------------------------------------------------------------------------
void CManifest::SetManifestPrefsModifiedFlag( bool bModified )
{
	m_bManifestUserPrefsChanged = bModified;
}


//-----------------------------------------------------------------------------
// Purpose: this function handles the routing of the manifest's modified flag down to the primary map
// Input  : bModified - the new modified status
//-----------------------------------------------------------------------------
void CManifest::SetModifiedFlag( BOOL bModified )
{
	if ( m_pPrimaryMap )
	{
		m_pPrimaryMap->m_Map->SetModifiedFlag( bModified );
	}
	
	if ( bModified == false )
	{
		for( int i = 0; i < GetNumMaps(); i++ )
		{
			CManifestMap	*pManifestMap = GetMap( i );

			if ( pManifestMap->m_Map->IsModified() )
			{
				bModified = true;
				break;
			}
		}
	}

	if ( bModified != IsModified() )
	{
		GetMainWnd()->m_ManifestFilterControl.Invalidate();
	}

	__super::SetModifiedFlag( bModified );
}


//-----------------------------------------------------------------------------
// Purpose: this function will return the full path to a sub map.
// Input  : pManifestMapFileName - the relative name of the sub map
// Output : pOutputPath - the full path to the sub map
//-----------------------------------------------------------------------------
void CManifest::GetFullMapPath( const char *pManifestMapFileName, char *pOutputPath )
{
	strcpy( pOutputPath, m_ManifestDir );
	strcat( pOutputPath, pManifestMapFileName );
}


//-----------------------------------------------------------------------------
// Purpose: this function will attempt to find the manifest map that owns the map doc
// Input  : pMap - the map doc to look up
// Output : returns a pointer to the owning manifest map, otherwise NULL
//-----------------------------------------------------------------------------
CManifestMap *CManifest::FindMap( CMapDoc *pMap )
{
	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		if ( pManifestMap->m_Map == pMap )
		{
			return pManifestMap;
		}
	}

	return NULL;
}


//-----------------------------------------------------------------------------
// Purpose: this function will attempt to look up a map by its internal id.
// Input  : InternalID - the internal ID.
// Output : returns the manifest map if one is found.
//-----------------------------------------------------------------------------
CManifestMap *CManifest::FindMapByID( int InternalID )
{
	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		if ( pManifestMap->m_InternalID == InternalID )
		{
			return pManifestMap;
		}
	}

	return NULL;
}


//-----------------------------------------------------------------------------
// Purpose: this function will set the manifest map as the primary map.  All entity / brush 
//			operations happen exclusively on the primary map.
// Input  : pManifestMap - the manifest map to make primary
//-----------------------------------------------------------------------------
void CManifest::SetPrimaryMap( CManifestMap	*pManifestMap )
{
	if ( m_pPrimaryMap )
	{
		m_pPrimaryMap->m_bPrimaryMap = false;
		m_pPrimaryMap->m_Map->m_nNextMapObjectID = m_nNextMapObjectID;
		m_pPrimaryMap->m_Map->m_nNextMapObjectID = m_nNextNodeID;
		m_pPrimaryMap->m_Map->SetEditable( false );
	}

	ClearSelection();
	CheckFileStatus();

	m_pPrimaryMap = pManifestMap;
	if ( m_pPrimaryMap )
	{
		m_pPrimaryMap->m_bPrimaryMap = true;
		m_pWorld = m_pPrimaryMap->m_Map->GetMapWorld();
		m_VisGroups = m_pPrimaryMap->m_Map->m_VisGroups;
		m_RootVisGroups = m_pPrimaryMap->m_Map->m_RootVisGroups;
		m_nNextMapObjectID = m_pPrimaryMap->m_Map->m_nNextMapObjectID;
		m_nNextNodeID = m_pPrimaryMap->m_Map->m_nNextMapObjectID;
		m_pPrimaryMap->m_Map->SetEditable( !m_pPrimaryMap->m_bReadOnly );

		m_pUndo = m_pPrimaryMap->m_Map->m_pUndo;
		m_pRedo = m_pPrimaryMap->m_Map->m_pRedo;
		CHistory::SetHistory( m_pPrimaryMap->m_Map->m_pUndo );

//		m_pSelection = m_pPrimaryMap->m_Map->m_pSelection;
	}

	m_bManifestUserPrefsChanged = true;

	GetMainWnd()->GlobalNotify( WM_MAPDOC_CHANGED );
	GetMainWnd()->m_ManifestFilterControl.Invalidate();
	UpdateAllViews( MAPVIEW_UPDATE_SELECTION | MAPVIEW_UPDATE_TOOL | MAPVIEW_RENDER_NOW );
}


//-----------------------------------------------------------------------------
// Purpose: sets the visibility flag of a sub map.
// Input  : pManifestMap - the map to set the flag
//			bIsVisible - the visiblity status
//-----------------------------------------------------------------------------
void CManifest::SetVisibility( CManifestMap	*pManifestMap, bool bIsVisible )
{
	pManifestMap->m_bVisible = bIsVisible;

	GetMainWnd()->m_ManifestFilterControl.Invalidate();
	UpdateAllViews( MAPVIEW_UPDATE_SELECTION | MAPVIEW_UPDATE_TOOL | MAPVIEW_RENDER_NOW );
}


//-----------------------------------------------------------------------------
// Purpose: this function will create and default a new manifest map, add it to the world
// Input  : AbsoluteFileName - the full path of the vmf file
//			RelativeFileName - the relative path of the vmf file
// Output : returns a pointer to the newly created manifest map.
//-----------------------------------------------------------------------------
CManifestMap *CManifest::CreateNewMap( const char *AbsoluteFileName, const char *RelativeFileName, bool bSetID )
{
	CManifestMap	*pManifestMap = new CManifestMap();

	pManifestMap->m_AbsoluteMapFileName = AbsoluteFileName;
	pManifestMap->m_RelativeMapFileName = RelativeFileName;
	pManifestMap->m_Map = new CMapDoc();
	SetActiveMapDoc( pManifestMap->m_Map );
	pManifestMap->m_Map->SetManifest( this );
	pManifestMap->m_Map->SetEditable( false );

	pManifestMap->m_Entity = new CManifestInstance( pManifestMap );

	pManifestMap->m_Entity->SetPlaceholder( true );
	pManifestMap->m_Entity->SetOrigin( Vector( 0.0f, 0.0f, 0.0f ) );
	pManifestMap->m_Entity->SetClass( "func_instance" );
	pManifestMap->m_Entity->SetKeyValue( "classname", "func_instance" );

	// ensure we are a pure instance of only the instance helper!
	pManifestMap->m_Entity->RemoveAllChildren();
	CHelperInfo	HI;

	HI.SetName( "instance" );

	CMapClass *pHelper = CHelperFactory::CreateHelper( &HI, pManifestMap->m_Entity );
	if ( pHelper != NULL )
	{
		pManifestMap->m_Entity->AddHelper( pHelper, false );
	}

	if ( bSetID )
	{
		pManifestMap->m_InternalID = m_NextInternalID;
		m_NextInternalID++;
	}

	CMapInstance	*pMapInstance = pManifestMap->m_Entity->GetChildOfType( ( CMapInstance * )NULL );
	if ( pMapInstance )
	{
		pMapInstance->SetManifest( pManifestMap );
	}
	AddManifestObjectToWorld( pManifestMap->m_Entity );
	m_Maps.AddToTail( pManifestMap );

	m_bManifestChanged = true;

	return pManifestMap;
}


//-----------------------------------------------------------------------------
// Purpose: This function will move the selection of the active map to a new sub map.
// Input  : pManifestMap - the sub map the selection shoud be moved to
//			CenterContents - if the contents should be centered
//-----------------------------------------------------------------------------
void CManifest::MoveSelectionToSubmap( CManifestMap *pManifestMap, bool CenterContents )
{
#if 0
	if ( s_Clipboard.Objects.Count() != 0 )
	{
		return false;
	}
#endif

	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	if ( !pDoc )
	{
		return;
	}

	pDoc->Copy();
	if ( pDoc->GetClipboardCount() == 0 )
	{
		return;
	}
	pDoc->Delete();

	pManifestMap->m_Map->ManifestPaste( pManifestMap->m_Map->GetMapWorld(), Vector( 0.0f, 0.0f, 0.0f ), QAngle( 0.0f, 0.0f, 0.0f ), NULL, false, NULL );
	pManifestMap->m_Entity->CalcBounds( TRUE );
	
	UpdateAllViews( MAPVIEW_UPDATE_SELECTION | MAPVIEW_UPDATE_TOOL | MAPVIEW_RENDER_NOW );
}


//-----------------------------------------------------------------------------
// Purpose: this function will cut the selection and move it into a newly created 
//			map doc and manifest map.
// Input  : FriendlyName - this is the text friendly name that the user can refer 
//				to the map as
//			FileName - The relative file name for this new map to be saved as
//			CenterContents - whether or not we should center the contents in the new map
// Output : returns a pointer to the newly created manifest map.
//-----------------------------------------------------------------------------
CManifestMap *CManifest::MoveSelectionToNewSubmap( CString &FriendlyName, CString &FileName, bool CenterContents )
{
#if 0
	if ( s_Clipboard.Objects.Count() != 0 )
	{
		return false;
	}
#endif

	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();

	if ( !pDoc )
	{
		return NULL;
	}

	pDoc->Copy();
	if ( pDoc->GetClipboardCount() == 0 )
	{
		return NULL;
	}
	
	char AbsoluteFileName[ MAX_PATH ];

	strcpy( AbsoluteFileName, m_ManifestDir );
	strcat( AbsoluteFileName, FileName );

	CManifestMap	*pManifestMap = CreateNewMap( AbsoluteFileName, FileName, true );
	pManifestMap->m_FriendlyName = FriendlyName;

	pManifestMap->m_Map->Initialize();
	if ( pManifestMap->m_Map->SaveVMF( pManifestMap->m_AbsoluteMapFileName, 0 ) == false )
	{
		m_bLoading = false;
		SetActiveMapDoc( this );
		delete pManifestMap;
		return NULL;
	}
	pDoc->Delete();
	pManifestMap->m_Map->ManifestPaste( pManifestMap->m_Map->GetMapWorld(), Vector( 0.0f, 0.0f, 0.0f ), QAngle( 0.0f, 0.0f, 0.0f ), NULL, false, NULL );
	pManifestMap->m_Entity->CalcBounds( TRUE );
	SetPrimaryMap( pManifestMap );

	SetActiveMapDoc( this );
	__super::SetModifiedFlag( true );
	pDoc->SetModifiedFlag( true );
	pManifestMap->m_Map->SetModifiedFlag( true );

	UpdateAllViews( MAPVIEW_UPDATE_SELECTION | MAPVIEW_UPDATE_TOOL | MAPVIEW_RENDER_NOW );
	GetMainWnd()->m_ManifestFilterControl.UpdateManifestList();

	return pManifestMap;
}


//-----------------------------------------------------------------------------
// Purpose: this function will add a new sub map to the manifest.  
// Input  : FriendlyName - the friendly name string
//			FileName - the file name of the sub map
// Output : returns a pointer to the new manifest map.
//-----------------------------------------------------------------------------
CManifestMap *CManifest::AddNewSubmap( CString &FriendlyName, CString &FileName )
{
	char AbsoluteFileName[ MAX_PATH ];

	strcpy( AbsoluteFileName, m_ManifestDir );
	strcat( AbsoluteFileName, FileName );

	CManifestMap	*pManifestMap = CreateNewMap( AbsoluteFileName, FileName, true );

	pManifestMap->m_FriendlyName = FriendlyName;

	pManifestMap->m_Map->Initialize();
	pManifestMap->m_Entity->CalcBounds( TRUE );

	if ( pManifestMap->m_Map->SaveVMF( pManifestMap->m_AbsoluteMapFileName, 0 ) == false )
	{
		m_bLoading = false;
		SetActiveMapDoc( this );
		delete pManifestMap;
		return NULL;
	}

	SetPrimaryMap( pManifestMap );
	SetActiveMapDoc( this );
	__super::SetModifiedFlag( true );

	UpdateAllViews( MAPVIEW_UPDATE_SELECTION | MAPVIEW_UPDATE_TOOL | MAPVIEW_RENDER_NOW );
	GetMainWnd()->m_ManifestFilterControl.UpdateManifestList();

	return pManifestMap;
}


//-----------------------------------------------------------------------------
// Purpose: This function add an external vmf file to the manifest
// Input  : pszFileName - the absolute file name of the vmf file
//			bFromInstance - if the map is coming from a func_instance somewhere
// Output : returns true if the map could be loaded and the manifest was created
//-----------------------------------------------------------------------------
bool CManifest::AddExistingMap( const char *pszFileName, bool bFromInstance )
{
	char AbsoluteFileName[ MAX_PATH ], RelativeFileName[ MAX_PATH ];

	char FileExt[ MAX_PATH ];

	_splitpath_s( pszFileName, NULL, 0, NULL, 0, RelativeFileName, sizeof( RelativeFileName ), FileExt, sizeof( FileExt ) );
	strcat( RelativeFileName, FileExt );

	strcpy( AbsoluteFileName, m_ManifestDir );
	strcat( AbsoluteFileName, RelativeFileName );
	CManifestMap	*pManifestMap = CreateNewMap( AbsoluteFileName, RelativeFileName, true );

	m_bLoading = true;
	if ( !pManifestMap->m_Map->LoadVMF( pszFileName, VMF_LOAD_ACTIVATE | VMF_LOAD_IS_SUBMAP ) )
	{
		m_bLoading = false;
		SetActiveMapDoc( this );
		delete pManifestMap;
		return false;
	}

	if ( pManifestMap->m_Map->SaveVMF( pManifestMap->m_AbsoluteMapFileName, 0 ) == false )
	{
		m_bLoading = false;
		SetActiveMapDoc( this );
		delete pManifestMap;
		return false;
	}

	pManifestMap->m_Map->GetMapWorld()->CullTree_Build();
	pManifestMap->m_Entity->PostUpdate( Notify_Changed );
	if ( m_Maps.Count() == 1 )
	{
		pManifestMap->m_bTopLevelMap = true;
	}

	SetPrimaryMap( pManifestMap );

	m_bLoading = false;

	SetActiveMapDoc( this );
	__super::SetModifiedFlag( true );

	GetMainWnd()->m_ManifestFilterControl.UpdateManifestList();

	if ( GetPathName().GetLength() == 0 )
	{
		char	ManifestFile[ MAX_PATH ];

		strcpy( ManifestFile, pszFileName );
		V_SetExtension( ManifestFile, ".vmm", sizeof( ManifestFile ) );

		m_bRelocateSave = true;
		OnSaveDocument( ManifestFile );
		m_bRelocateSave = false;

		SetPathName( ManifestFile, false );
	}

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: This function will allow the user to browse to an exist map to add to the manifest.
//-----------------------------------------------------------------------------
bool CManifest::AddExistingMap( void )
{
	char szInitialDir[ MAX_PATH ];
	
	V_strcpy_safe( szInitialDir, GetPathName() );
	if ( szInitialDir[ 0 ] == '\0' )
	{
		strcpy( szInitialDir, g_pGameConfig->szMapDir );
	}

	CFileDialog dlg( TRUE, NULL, NULL, OFN_LONGNAMES | OFN_HIDEREADONLY | OFN_NOCHANGEDIR, "Valve Map Files (*.vmf)|*.vmf||" );
	dlg.m_ofn.lpstrInitialDir = szInitialDir;
	int iRvl = dlg.DoModal();

	if ( iRvl == IDCANCEL )
	{
		return false;
	}

	//
	// Get the directory they browsed to for next time.
	//
	CString str = dlg.GetPathName();
	int nSlash = str.ReverseFind( '\\' );
	if ( nSlash != -1 )
	{
		strcpy( szInitialDir, str.Left( nSlash ) );
	}

	if ( str.Find('.') == -1 )
	{
		switch ( dlg.m_ofn.nFilterIndex )
		{
			case 1:
				str += ".vmf";
				break;
		}
	}

	return AddExistingMap( str, false );
}


//-----------------------------------------------------------------------------
// Purpose: This function will remove the sub map from the manifest
// Input  : pManifestMap - the sub map to be removed
// Output : returns true if it was successful
//-----------------------------------------------------------------------------
bool CManifest::RemoveSubMap( CManifestMap *pManifestMap )
{
	if ( m_Maps.Count() > 1 )
	{
		m_Maps.FindAndRemove( pManifestMap );

		const CMapObjectList *pChildren = m_ManifestWorld->GetChildren();
		FOR_EACH_OBJ( *pChildren, pos )
		{
			CMapClass	*pChild = pChildren->Element( pos );
			CMapEntity	*pEntity = dynamic_cast< CMapEntity * >( pChild );

			if ( pEntity && stricmp( pEntity->GetClassName(), "func_instance" ) == 0 )
			{
				CMapInstance	*pMapInstance = pEntity->GetChildOfType( ( CMapInstance * )NULL );
				if ( pMapInstance )
				{
					if ( pMapInstance->GetManifestMap() == pManifestMap )
					{
						m_ManifestWorld->RemoveObjectFromWorld( pChild, true );
						break;
					}
				}
			}
		}

		delete pManifestMap;

		return true;
	}

	return false;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : 
// Output : 
//-----------------------------------------------------------------------------
bool CManifest::CheckOut( )
{
	if ( !p4 )
	{
		return false;
	}

	if ( !p4->OpenFileForEdit( GetPathName() ) )
	{
		return false;
	}

	CheckFileStatus();

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : 
// Output : 
//-----------------------------------------------------------------------------
bool CManifest::AddToVersionControl( )
{
	if ( !p4 )
	{
		return false;
	}

	if ( !p4->OpenFileForAdd( GetPathName() ) )
	{
		return false;
	}

	CheckFileStatus();

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : 
// Output : 
//-----------------------------------------------------------------------------
void CManifest::CheckFileStatus( void )
{
	P4File_t	FileInfo;

	m_bReadOnly = !g_pFullFileSystem->IsFileWritable( GetPathName() );
	m_bCheckedOut = false;
	m_bIsVersionControlled = false;
	if ( p4 != NULL && p4->GetFileInfo( GetPathName(), &FileInfo ) == true )
	{
		m_bIsVersionControlled = true;
		if ( FileInfo.m_eOpenState == P4FILE_OPENED_FOR_ADD || FileInfo.m_eOpenState == P4FILE_OPENED_FOR_EDIT )
		{
			m_bCheckedOut = true;
		}
	}

	for( int i = 0; i < GetNumMaps(); i++ )
	{
		CManifestMap	*pManifestMap = GetMap( i );

		pManifestMap->m_bReadOnly = !g_pFullFileSystem->IsFileWritable( pManifestMap->m_AbsoluteMapFileName );
		pManifestMap->m_bCheckedOut = false;
		pManifestMap->m_bIsVersionControlled = false;

		if ( p4 != NULL && p4->GetFileInfo( pManifestMap->m_AbsoluteMapFileName, &FileInfo ) == true )
		{
			pManifestMap->m_bIsVersionControlled = true;
			if ( FileInfo.m_eOpenState == P4FILE_OPENED_FOR_ADD || FileInfo.m_eOpenState == P4FILE_OPENED_FOR_EDIT )
			{
				pManifestMap->m_bCheckedOut = true;
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: This function will clear the selection
//-----------------------------------------------------------------------------
void CManifest::ClearSelection( void )
{
	SelectFace( NULL, 0, scClear | scSaveChanges );
	SelectObject( NULL, scClear | scSaveChanges );
}


//-----------------------------------------------------------------------------
// Purpose: This function will add the object to the primary map of the manifest
// Input  : pObject - a pointer to the object to be added
//			pParent - a pointer to the parent of this object
//-----------------------------------------------------------------------------
void CManifest::AddObjectToWorld(CMapClass *pObject, CMapClass *pParent)
{
	m_pPrimaryMap->m_Map->AddObjectToWorld( pObject, pParent );

	m_pPrimaryMap->m_Entity->PostUpdate( Notify_Changed );
}


//-----------------------------------------------------------------------------
// Purpose: This function will pass on the notification that a map has been updated
// Input  : pInstanceMapDoc - the map that was updated
//-----------------------------------------------------------------------------
void CManifest::UpdateInstanceMap( CMapDoc *pInstanceMapDoc )
{
	const CMapObjectList *pChildren = m_ManifestWorld->GetChildren();
	FOR_EACH_OBJ( *pChildren, pos )
	{
		CMapClass	*pChild = pChildren->Element( pos );
		CMapEntity	*pEntity = dynamic_cast< CMapEntity * >( pChild );

		if ( pEntity && stricmp( pEntity->GetClassName(), "func_instance" ) == 0 )
		{
			CMapInstance	*pMapInstance = pEntity->GetChildOfType( ( CMapInstance * )NULL );
			if ( pMapInstance )
			{
				if ( pMapInstance->GetInstancedMap() == pInstanceMapDoc )
				{
					pMapInstance->UpdateInstanceMap();
					m_ManifestWorld->UpdateChild( pMapInstance );
				}
			}
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: this function will add the object to the local manifest world.  the
//			only objects that should be added are ManifestInstance.
// Input  : pObject - a pointer to the object to be added
//			pParent - a pointer to the parent of this object
//-----------------------------------------------------------------------------
void CManifest::AddManifestObjectToWorld( CMapClass *pObject, CMapClass *pParent )
{
	m_ManifestWorld->AddObjectToWorld( pObject, pParent );
}


//-----------------------------------------------------------------------------
// Purpose: Removes an object from the manifest world.
// Input  : pObject - object to remove from the world.
//			bChildren - whether we're removing the object's children as well.
//-----------------------------------------------------------------------------
void CManifest::RemoveManifestObjectFromWorld( CMapClass *pObject, bool bRemoveChildren )
{
	m_ManifestWorld->RemoveObjectFromWorld( pObject, bRemoveChildren );
}

//-----------------------------------------------------------------------------
// Purpose: this function will start the loading process for a manifest
// Input  : lpszPathName - the absoltue path of the manifest file
// Output : Returns TRUE on success, FALSE on failure.
//-----------------------------------------------------------------------------
BOOL CManifest::OnOpenDocument(LPCTSTR lpszPathName) 
{
	Initialize();

	if (!SelectDocType())
	{
		return FALSE;
	}

	//
	// Call any per-class PreloadWorld functions here.
	//
	CMapSolid::PreloadWorld();

	if ( !Load( lpszPathName ) )
	{
		return FALSE;
	}

	SetModifiedFlag( FALSE );
	Msg( mwStatus, "Opened %s", lpszPathName );
	SetActiveMapDoc( this );

	//
	// We set the active doc before loading for displacements (and maybe other
	// things), but visgroups aren't available until after map load. We have to refresh
	// the visgroups here or they won't be correct.
	//
	GetMainWnd()->GlobalNotify( WM_MAPDOC_CHANGED );

	m_pToolManager->SetTool( TOOL_POINTER );

	return(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: This function will save out the manifest
// Input  : lpszPathName - the absolute filename of the manifest
// Output : Returns TRUE on success, FALSE on failure.
//-----------------------------------------------------------------------------
BOOL CManifest::OnSaveDocument(LPCTSTR lpszPathName) 
{
	if ( !Save( lpszPathName, m_bRelocateSave ) )
	{
		return FALSE;
	}

	SetModifiedFlag( FALSE );

	return TRUE;
}


//-----------------------------------------------------------------------------
// Purpose: this function will be called when the use select Save As.  This will
//			allow all of the submaps to be relocated relative to a new save location
//			of the manifest itself.  Directories should be created automatically.
//-----------------------------------------------------------------------------
void CManifest::OnFileSaveAs(void)
{
	m_bRelocateSave = true;

	__super::OnFileSaveAs();

	m_bRelocateSave = false;
}


//-----------------------------------------------------------------------------
// Purpose: this function will delete the manifest world and rest of the contents.
//-----------------------------------------------------------------------------
void CManifest::DeleteContents( void )
{
	m_pSelection->RemoveAll();

	if ( m_ManifestWorld )
	{
		delete m_ManifestWorld;
		m_ManifestWorld = NULL;
	}

	m_pWorld = NULL;
	m_VisGroups = NULL;
	m_RootVisGroups = NULL;
	m_pUndo = m_pSaveUndo;
	m_pRedo = m_pSaveRedo;

	__super::DeleteContents();
}

#include <tier0/memdbgoff.h>