summaryrefslogtreecommitdiff
path: root/utils/xbox/xbox_loader/xbox_loader.cpp
blob: 7cbeb777030d3394973d5d10ae40e3a990ecca9e (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
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//	APPLOADER.CPP
//
//	Stub executeable
//=====================================================================================//
#include "xbox_loader.h"

struct installData_t
{
	char			**m_ppSrcFiles;
	char			**m_ppDstFiles;
	DWORD			*m_pDstFileSizes;
	int				m_numFiles;
	DWORD			m_totalSize;
	xCompressHeader	**m_ppxcHeaders;
};

DWORD			g_installStartTime;
DWORD			g_installElapsedTime;
installData_t	g_installData;
CopyStats_t		g_copyStats;
int				g_activeDevice;
__int64			g_loaderStartTime;


//-----------------------------------------------------------------------------
// GetLocalizedLoadingString
//-----------------------------------------------------------------------------
const wchar_t *GetLocalizedLoadingString()
{
	switch( XGetLanguage() )
	{
	case XC_LANGUAGE_FRENCH:
		return L"CHARGEMENT...";
	case XC_LANGUAGE_ITALIAN:
		return L"CARICAMENTO...";
	case XC_LANGUAGE_GERMAN:
		return L"L�DT...";
	case XC_LANGUAGE_SPANISH:
		return L"CARGANDO...";
	}
	return L"LOADING...";
}

//-----------------------------------------------------------------------------
// GetNextLangauge
// Start at -1
//-----------------------------------------------------------------------------
int GetNextLanguage( int languageID )
{
	if ( languageID < 0 )
		return XC_LANGUAGE_ENGLISH;

	// cycle to end
	switch ( languageID )
	{
	case XC_LANGUAGE_ENGLISH:
		return XC_LANGUAGE_FRENCH;
	case XC_LANGUAGE_FRENCH:
		return XC_LANGUAGE_ITALIAN;
	case XC_LANGUAGE_ITALIAN:
		return XC_LANGUAGE_GERMAN;
	case XC_LANGUAGE_GERMAN:
		return XC_LANGUAGE_SPANISH;
	case XC_LANGUAGE_SPANISH:
		return -1;
	}

	return -1;
}

//-----------------------------------------------------------------------------
// GetLanguageString
//-----------------------------------------------------------------------------
const char *GetLanguageString( int languageID )
{
	switch( languageID )
	{
	case XC_LANGUAGE_FRENCH:
		return "french";
	case XC_LANGUAGE_ITALIAN:
		return "italian";
	case XC_LANGUAGE_GERMAN:
		return "german";
	case XC_LANGUAGE_SPANISH:
		return "spanish";
	}
	return "english";
}

//-----------------------------------------------------------------------------
// FixupNamespaceFilename
//-----------------------------------------------------------------------------
bool FixupNamespaceFilename( const char *pFilename, char *pOutFilename, int languageID )
{
	char newFilename[MAX_PATH];

	bool bFixup = false;
	int dstLen = 0;
	int srcLen = strlen( pFilename );
	for ( int i=0; i<srcLen+1; i++ )
	{
		// replace every occurrence of % with language
		if ( pFilename[i] == '%' )
		{
			int len = strlen( GetLanguageString( languageID ) );
			memcpy( newFilename + dstLen, GetLanguageString( languageID ), len );
			dstLen += len;
			bFixup = true;
		}
		else
		{
			newFilename[dstLen] = pFilename[i];
			dstLen++;
		}
	}

	strcpy( pOutFilename, newFilename );
	return bFixup;
}

//-----------------------------------------------------------------------------
// DeleteOtherLocalizedFiles
//-----------------------------------------------------------------------------
void DeleteOtherLocalizedFiles( const char *pFilename, int languageIDToKeep )
{
	char	newFilename[MAX_PATH];
	char	mrkFilename[MAX_PATH];
	bool	bFixup;

	int languageID = -1;
	while ( 1 )
	{
		languageID = GetNextLanguage( languageID );
		if ( languageID == -1 )
		{
			// cycled through
			break;
		}
		
		if ( languageID == languageIDToKeep )
		{
			// skip
			continue;
		}
		
		bFixup = FixupNamespaceFilename( pFilename, newFilename, languageID );
		if ( !bFixup )
		{
			// nothing to do
			continue;
		}

		SetFileAttributes( newFilename, FILE_ATTRIBUTE_NORMAL );
		DeleteFile( newFilename );

		// delete marker
		strcpy( mrkFilename, newFilename );
		strcat( mrkFilename, ".mrk" );
		SetFileAttributes( mrkFilename, FILE_ATTRIBUTE_NORMAL );
		DeleteFile( mrkFilename );
	}
}

//-----------------------------------------------------------------------------
// LerpColor
//-----------------------------------------------------------------------------
unsigned int LerpColor( unsigned int c0, unsigned int c1, float t )
{
	int 			i;
	float			a;
	float			b;
	unsigned char*	c;
	unsigned int	newcolor;

	if ( t <= 0.0f )
		return c0;
	else if ( t >= 1.0f )
		return c1;

	// lerp each component
	c = (unsigned char*)&newcolor;
	for ( i=0; i<4; i++ )
	{
		a    = (float)(c0 & 0xFF);
		b    = (float)(c1 & 0xFF);
		*c++ = (unsigned char)(a + t*(b-a));

		// next color component
		c0 >>= 8;
		c1 >>= 8;
	}		

	return newcolor;
}

//-----------------------------------------------------------------------------
// ConvertToWideString
//-----------------------------------------------------------------------------
void ConvertToWideString( wchar_t *pDst, const char *pSrc )
{
	int len = strlen( pSrc )+1;
	for (int i=0; i<len; i++)
	{
		pDst[i] = pSrc[i];
	}
}

//-----------------------------------------------------------------------------
// CopyString
//-----------------------------------------------------------------------------
char *CopyString( const char *pString )
{
	char *pNewString = (char *)malloc( strlen( pString ) + 1 );
	strcpy( pNewString, pString );

	return pNewString;
}

//-----------------------------------------------------------------------------
// FixFilename
//-----------------------------------------------------------------------------
void FixFilename( char *pPath )
{
	int len = strlen( pPath );
	for (int i=0; i<len; ++i)
	{
		if ( pPath[i] == '/')
			pPath[i] = '\\';
	}
}

//-----------------------------------------------------------------------------
// StripQuotes
//-----------------------------------------------------------------------------
void StripQuotes( char *pToken )
{
	int len = strlen( pToken );
	if ( pToken[0] == '"' && pToken[len-1] == '"' )
	{
		memcpy( pToken, pToken+1, len-2 );
		pToken[len-2] = '\0';
	}
}

//-----------------------------------------------------------------------------
// DoesFileExist
//-----------------------------------------------------------------------------
bool DoesFileExist( const char *pFilename, DWORD *pSize )
{
	HANDLE hFile = CreateFile( pFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
	if ( hFile != INVALID_HANDLE_VALUE )
	{
		if ( pSize )
		{
			*pSize = GetFileSize( hFile, NULL );
			if ( *pSize == (DWORD)-1 )
			{
				*pSize = 0;
			}
		}
	
		// exists
		CloseHandle( hFile );
		return true;
	}

	// not present
	return false;
}

//-----------------------------------------------------------------------------
//	NormalizePath
//
//-----------------------------------------------------------------------------
void NormalizePath( char* path, bool forceToLower )
{
	int	i;
	int	srclen;

	srclen = strlen( path );
	for ( i=0; i<srclen; i++ )
	{
		if ( path[i] == '/' )
			path[i] = '\\';
		else if ( forceToLower && ( path[i] >= 'A' && path[i] <= 'Z' ) )
			path[i] = path[i] - 'A' + 'a';
	}
}

//-----------------------------------------------------------------------------
// DeleteAllFiles
//
//-----------------------------------------------------------------------------
void DeleteAllFiles( const char* pDirectory, int level, bool bRecurse )
{
	HANDLE			hFind;
	WIN32_FIND_DATA	findData;
	char			basepath[MAX_PATH];
	char			searchpath[MAX_PATH];
	char			filename[MAX_PATH];

	TL_AddSeperatorToPath( (char*)pDirectory, basepath );
	strcpy( searchpath, basepath );
	strcat( searchpath, "*.*" );

	hFind = FindFirstFile( searchpath, &findData );
	if ( hFind != INVALID_HANDLE_VALUE )
	{
		do
		{
			if ( !stricmp( findData.cFileName, "." ) || !stricmp( findData.cFileName, ".." ) )
			{
				continue;
			}

			strcpy( filename, basepath );
			strcat( filename, findData.cFileName );
			NormalizePath( filename, false );

			if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
			{
				if ( bRecurse )
				{
					DeleteAllFiles( filename, level+1, true );
					RemoveDirectory( filename );
					continue;
				}
			}

			SetFileAttributes( filename, FILE_ATTRIBUTE_NORMAL );
			DeleteFile( filename );
		}
		while ( FindNextFile( hFind, &findData ) );
		FindClose( hFind );
	}
}

//-----------------------------------------------------------------------------
// GetXCompressedHeader
//-----------------------------------------------------------------------------
bool GetXCompressedHeader( const char *pFilename, xCompressHeader *pHeader )
{
	HANDLE hFile = CreateFile( pFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
	if ( hFile != INVALID_HANDLE_VALUE )
	{
		DWORD dwBytesRead = 0;
		ReadFile( hFile, pHeader, sizeof( xCompressHeader ), &dwBytesRead, NULL );
		CloseHandle( hFile );
		if ( pHeader->nMagic == xCompressHeader::MAGIC && pHeader->nVersion == xCompressHeader::VERSION )
		{
			// valid
			return true;
		}
	}

	// invalid
	return false;
}

//-----------------------------------------------------------------------------
// IsTargetFileValid
//
// Optional non-zero expected source file size must matcg
//-----------------------------------------------------------------------------
bool IsTargetFileValid( const char *pFilename, DWORD dwSrcFileSize )
{
	char	mrkFilename[MAX_PATH];
	DWORD	dwTargetFileSize = 0;
	DWORD	dwSize = 0;
	DWORD	dwAttributes;

	// all valid target files are non-zero
	if ( !DoesFileExist( pFilename, &dwTargetFileSize ) || !dwTargetFileSize )
	{
		return false;
	}

	dwAttributes = GetFileAttributes( pFilename );
	if ( dwAttributes != (DWORD)-1 && ( dwAttributes & FILE_ATTRIBUTE_READONLY ) )
	{
		// target files marked read only don't get overwritten
		return true;
	}

	// all valid target files must have marker file
	// presence ensures file was successfully copied
	strcpy( mrkFilename, pFilename );
	strcat( mrkFilename, ".mrk" );
	if ( !DoesFileExist( mrkFilename, NULL ) )
	{
		// cannot rely on contents, regardless of size match
		DeleteFile( pFilename );
		return false;
	}

	if ( dwSrcFileSize && dwSrcFileSize != dwTargetFileSize )
	{
		DeleteFile( pFilename );
		return false;
	}

	// assume valid
	return true;
}

//-----------------------------------------------------------------------------
// Constructor for CXBoxLoader class
//-----------------------------------------------------------------------------
CXBoxLoader::CXBoxLoader() : CXBApplication()
{
	// need a persistent time base, use the RTC
	// all other tick counters reset across relaunch
	FILETIME fileTime;
	GetSystemTimeAsFileTime( &fileTime );
	g_loaderStartTime = ((ULARGE_INTEGER*)&fileTime)->QuadPart;

	m_contextCode            = 0;
	m_pLastMovieFrame        = NULL;
	m_pVB                    = NULL;
	m_bAllowAttractAbort     = false;
	m_numFiles               = 0;
	m_bLaunch                = false;
	m_dwLoading              = 0;
	m_bDrawLegal             = false;
	m_LegalTime              = 0;
	m_installThread          = NULL;
	m_State                  = 0;
	m_bDrawLoading           = false;
	m_bDrawProgress          = false;
	m_bInstallComplete       = false;
	m_FrameCounter           = 0;
	m_MovieCount             = 0;
	m_bMovieErrorIsFatal     = false;
	m_bDrawDebug             = false;
	m_LoadingBarStartTime    = 0;
	m_LoadingBarEndTime      = 0;
	m_LegalStartTime         = 0;
	m_bCaptureLastMovieFrame = 0;
	m_bDrawSlideShow         = false;
	m_SlideShowStartTime     = 0;
	m_pLogData               = NULL;
	m_pDefaultTrueTypeFont   = NULL;
}

//-----------------------------------------------------------------------------
// FatalMediaError
//-----------------------------------------------------------------------------
void CXBoxLoader::FatalMediaError()
{
    m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0L );

	LPDIRECT3DSURFACE8 pBackBuffer;
    m_pd3dDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );

	LPCWSTR	pLine1;
	LPCWSTR	pLine2;
	switch( XGetLanguage() )
	{
	case XC_LANGUAGE_FRENCH:
		pLine1 = L"Le disque utilis� pr�sente une anomalie."; 
		pLine2 = L"Il est peut-�tre sale ou endommag�.";
		break;
	case XC_LANGUAGE_ITALIAN:
		pLine1 = L"Il disco in uso ha qualche problema.";
		pLine2 = L"Potrebbe essere sporco o danneggiato.";
		break;
	case XC_LANGUAGE_GERMAN:
		pLine1 = L"Bei der benutzten CD ist ein Problem aufgetreten.";
		pLine2 = L"M�glicherweise ist sie verschmutzt oder besch�digt.";
		break;
	case XC_LANGUAGE_SPANISH:
		pLine1 = L"Hay un problema con el disco que est� usando.";
		pLine2 = L"Puede estar sucio o da�ado.";
		break;
	default:
		pLine1 = L"There is a problem with the disc you are using.";
		pLine2 = L"It may be dirty or damaged.";
		break;
	}

	if ( m_pDefaultTrueTypeFont )
	{
		m_pDefaultTrueTypeFont->SetTextAlignment( XFONT_CENTER|XFONT_TOP );
		m_pDefaultTrueTypeFont->TextOut( pBackBuffer, pLine1, (unsigned)-1, 320, 240-15 );
		m_pDefaultTrueTypeFont->TextOut( pBackBuffer, pLine2, (unsigned)-1, 320, 240+15 );
	}

    // Present the scene
    m_pd3dDevice->Present( NULL, NULL, NULL, NULL );

	pBackBuffer->Release();

	// forever
	while (1);
}

//-----------------------------------------------------------------------------
// LoadTexture
//-----------------------------------------------------------------------------
D3DTexture *CXBoxLoader::LoadTexture( int resourceID )
{
    // Get access to the texture
    return ( m_xprResource.GetTexture( resourceID ) );
}

//-----------------------------------------------------------------------------
// LoadFont
//-----------------------------------------------------------------------------
HRESULT CXBoxLoader::LoadFont( CXBFont *pFont, int resourceID )
{
	return pFont->Create( m_xprResource.GetTexture( resourceID ), 
                   m_xprResource.GetData( resourceID + sizeof(D3DTexture) ) );
}

//-----------------------------------------------------------------------------
// DrawRect
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawRect( int x, int y, int w, int h, DWORD color )
{
    // Set states
    D3DDevice::SetTexture( 0, NULL );
    D3DDevice::SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_DISABLE );
    D3DDevice::SetRenderState( D3DRS_ZENABLE, FALSE ); 
    D3DDevice::SetRenderState( D3DRS_FOGENABLE, FALSE );
    D3DDevice::SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
	D3DDevice::SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); 
    D3DDevice::SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
    D3DDevice::SetRenderState( D3DRS_ALPHATESTENABLE, FALSE ); 
    D3DDevice::SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
    D3DDevice::SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
    D3DDevice::SetVertexShader( D3DFVF_XYZRHW|D3DFVF_DIFFUSE );

    FLOAT fX1 = x;
    FLOAT fY1 = y;
    FLOAT fX2 = x + w - 1;
    FLOAT fY2 = y + h - 1;

    D3DDevice::Begin( D3DPT_QUADLIST );
    D3DDevice::SetVertexDataColor( D3DVSDE_DIFFUSE, color );
    D3DDevice::SetVertexData4f( D3DVSDE_VERTEX, fX1, fY1, 1.0f, 1.0f );
    D3DDevice::SetVertexData4f( D3DVSDE_VERTEX, fX2, fY1, 1.0f, 1.0f );
    D3DDevice::SetVertexData4f( D3DVSDE_VERTEX, fX2, fY2, 1.0f, 1.0f );
    D3DDevice::SetVertexData4f( D3DVSDE_VERTEX, fX1, fY2, 1.0f, 1.0f );
    D3DDevice::End();
}


//-----------------------------------------------------------------------------
// DrawTexture
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawTexture( D3DTexture *pD3DTexture, int x, int y, int w, int h, int color )
{
	struct VERTEX { D3DXVECTOR4 p; D3DCOLOR c; FLOAT tu, tv; };
	if ( !m_pVB )
	{
		// Create a vertex buffer for rendering the help screen
		D3DDevice::CreateVertexBuffer( 4*sizeof(VERTEX), D3DUSAGE_WRITEONLY, 0L, D3DPOOL_DEFAULT, &m_pVB );
	}

	VERTEX* v;
    m_pVB->Lock( 0, 0, (BYTE**)&v, 0L );

    // Calculate vertex positions
    FLOAT fLeft     = x + 0.5f;
    FLOAT fTop      = y + 0.5f;
    FLOAT fRight    = x+w - 0.5f;
    FLOAT fBottom   = y+h - 0.5f;

	// position
    v[0].p  = D3DXVECTOR4( fLeft, fTop, 0, 0 ); 
    v[1].p  = D3DXVECTOR4( fRight, fTop, 0, 0 );  
    v[2].p  = D3DXVECTOR4( fRight, fBottom, 0, 0 );  
    v[3].p  = D3DXVECTOR4( fLeft, fBottom, 0, 0 ); 

	// color
	v[0].c  = color; 
	v[1].c  = color; 
	v[2].c  = color; 
	v[3].c  = color;

	D3DSURFACE_DESC	desc;
	pD3DTexture->GetLevelDesc( 0, &desc );

	// linear texcoords
	v[0].tu = 0; 
	v[0].tv = 0;
	v[1].tu = desc.Width; 
	v[1].tv = 0;
	v[2].tu = desc.Width;
	v[2].tv = desc.Height;
	v[3].tu = 0;
	v[3].tv = desc.Height;

    m_pVB->Unlock();

    // Set state to render the image
    D3DDevice::SetTexture( 0, pD3DTexture );
    D3DDevice::SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
    D3DDevice::SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    D3DDevice::SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    D3DDevice::SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
    D3DDevice::SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
    D3DDevice::SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
    D3DDevice::SetTextureStageState( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP );
    D3DDevice::SetTextureStageState( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP );
	D3DDevice::SetTextureStageState( 0, D3DTSS_ADDRESSW,  D3DTADDRESS_CLAMP );
    D3DDevice::SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
	D3DDevice::SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    D3DDevice::SetRenderState( D3DRS_ZENABLE, FALSE );
    D3DDevice::SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
    D3DDevice::SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_ALWAYS );
	D3DDevice::SetRenderState( D3DRS_FOGENABLE, FALSE );
    D3DDevice::SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
    D3DDevice::SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
    D3DDevice::SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
    D3DDevice::SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
    D3DDevice::SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
    D3DDevice::SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
	D3DDevice::SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
    D3DDevice::SetVertexShader( D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1 );

    // Render the image
    D3DDevice::SetStreamSource( 0, m_pVB, sizeof(VERTEX) );
    D3DDevice::DrawPrimitive( D3DPT_QUADLIST, 0, 1 );
}

//-----------------------------------------------------------------------------
// StartVideo
//
// May take a few ms.
//-----------------------------------------------------------------------------
HRESULT CXBoxLoader::StartVideo( const CHAR* strFilename, bool bFromMemory, bool bFatalOnError )
{   
	HRESULT hr;
	if ( bFromMemory )
	{
		// play from memory, so as no to interfere with disc access
		hr = m_player.OpenMovieFromMemory( strFilename, D3DFMT_LIN_A8R8G8B8, m_pd3dDevice, TRUE );
	}
	else
	{
		// play from disc
		hr = m_player.OpenFile( strFilename, D3DFMT_LIN_A8R8G8B8, m_pd3dDevice, TRUE );
	}

	// can fail anytime
	m_bMovieErrorIsFatal = bFatalOnError;

	m_MovieCount++;

	if ( FAILED( hr ) )
	{
		OUTPUT_DEBUG_STRING( "Video playback failed!\n" );

		if ( bFatalOnError )
		{
			FatalMediaError();
		}
	}

    return hr;
}

//-----------------------------------------------------------------------------
// StopVideo
//
// May take a few ms.
//-----------------------------------------------------------------------------
void CXBoxLoader::StopVideo()
{
	m_player.TerminatePlayback();
}

//-----------------------------------------------------------------------------
// LoadInstallScript
//
// Parse filenames to be copied
//-----------------------------------------------------------------------------
bool CXBoxLoader::LoadInstallScript()
{
	void			*pFileData = NULL;
	DWORD			fileSize = 0;
	DWORD			dwSrcSize;
	HRESULT			hr;
	char			srcFile[MAX_PATH];
	char			dstFile[MAX_PATH];
	char			localizedFile[MAX_PATH];
	HANDLE			hFind;
	char			sourceFilename[MAX_PATH];
	char			sourcePath[MAX_PATH];
	char			targetFilename[MAX_PATH];
	char			filename[MAX_PATH];
	WIN32_FIND_DATA	findData;
	bool			bCompressed;
	xCompressHeader	xcHeader;
	char			*pVersion;
	bool			bTargetIsLocalized;
	int				languageID;
	
	memset( &g_installData, 0, sizeof( installData_t ) );

	hr = XBUtil_LoadFile( "D:\\LoaderMedia\\install.txt", &pFileData, &fileSize );
	if ( hr != S_OK || !fileSize )
	{
		return false;
	}

	languageID = XGetLanguage();

	// full re-install
	bool bForce = true;

	// scan
	TL_SetScriptData( (char*)pFileData, fileSize );
	while ( 1 )
	{
		char *pToken = TL_GetToken( true );
		if ( !pToken || !pToken[0] )
			break;
		StripQuotes( pToken );
		strcpy( srcFile, pToken);

		pToken = TL_GetToken( true );
		if ( !pToken || !pToken[0] )
			break;
		StripQuotes( pToken );
		strcpy( dstFile, pToken);

		// replace with language token
		FixupNamespaceFilename( srcFile, srcFile, languageID );
		bTargetIsLocalized = FixupNamespaceFilename( dstFile, localizedFile, languageID );

		if ( bTargetIsLocalized )
		{
			// localized files are allowed to change without requiring a full re-install
			bool bDeleteMapCache = false;
			if ( !IsTargetFileValid( localizedFile, 0 ) )
			{
				// must delete map cache to ensure localized files have enough room
				bDeleteMapCache = true;
			}

			// only allowing one localized file of this type, delete all others
			DeleteOtherLocalizedFiles( dstFile, languageID );
			strcpy( dstFile, localizedFile );

			if ( bDeleteMapCache )
			{
				char mapPath[MAX_PATH];
				strcpy( mapPath, localizedFile );
				TL_StripFilename( mapPath );
				TL_AddSeperatorToPath( mapPath, mapPath );
				strcat( mapPath, "maps\\" );
				DeleteAllFiles( mapPath, 0, false );
			}
		}

		pVersion = strstr( dstFile, "version_" );
		if ( pVersion )
		{
			if ( m_numFiles )
			{
				// version statement out of sequence
				return false;
			}
		
			m_Version = atoi( pVersion + strlen( "version_" ) );

			if ( IsTargetFileValid( dstFile, 0 ) )
			{
				// version file exists, files should be same
				bForce = false;
			}

			if ( bForce )
			{
				// delete all files at the specified directory
				strcpy( targetFilename, dstFile );
				TL_StripFilename( targetFilename );
				DeleteAllFiles( targetFilename, 0, true );
			}
		}

		// source file could be wildcard, get path only
		strcpy( sourcePath, srcFile );
		TL_StripFilename( sourcePath );

		hFind = FindFirstFile( srcFile, &findData );
		if ( hFind != INVALID_HANDLE_VALUE )
		{
			do
			{
				if ( !stricmp( findData.cFileName, "." ) || !stricmp( findData.cFileName, ".." ) )
				{
					continue;
				}
				if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
				{
					continue;
				}

				TL_AddSeperatorToPath( sourcePath, sourceFilename );
				strcat( sourceFilename, findData.cFileName );
				NormalizePath( sourceFilename, false );

				// target filename may be path or absolute file
				strcpy( targetFilename, dstFile );

				TL_StripPath( dstFile, filename );
				if ( !filename[0] )
				{
					// target filename is path only
					TL_AddSeperatorToPath( dstFile, targetFilename );
					strcat( targetFilename, findData.cFileName );
					NormalizePath( targetFilename, false );
				}

				if ( !DoesFileExist( sourceFilename, &dwSrcSize ) )
				{
					// can't validate source
					return false;
				}
			
				if ( strstr( sourceFilename, ".xz_" ) && strstr( targetFilename, ".xzp" ) )
				{
					bCompressed = true;
					if ( GetXCompressedHeader( sourceFilename, &xcHeader ) )
					{
						g_installData.m_totalSize += xcHeader.nUncompressedFileSize;

						if ( !bForce && IsTargetFileValid( targetFilename, xcHeader.nUncompressedFileSize ) )
						{
							// target already exists, no need to recopy
							g_copyStats.m_bytesCopied += xcHeader.nUncompressedFileSize;
							continue;
						}
					}
					else
					{
						// invalid
						return false;
					}
				}
				else
				{
					g_installData.m_totalSize += dwSrcSize;

					bCompressed = false;
					if ( !bForce && IsTargetFileValid( targetFilename, dwSrcSize ) )
					{
						// target already exists, no need to recopy
						g_copyStats.m_bytesCopied += dwSrcSize;
						continue;
					}
				}

				if ( m_numFiles < MAX_FILES )
				{
					m_fileSrc[m_numFiles]  = CopyString( sourceFilename );
					m_fileDest[m_numFiles] = CopyString( targetFilename );

					if ( bCompressed )
					{
						xCompressHeader *pxcHeader = new xCompressHeader;
						memcpy( pxcHeader, &xcHeader, sizeof( xCompressHeader ) );
						m_fileCompressionHeaders[m_numFiles] = pxcHeader;
						m_fileDestSizes[m_numFiles] = pxcHeader->nUncompressedFileSize;
					}
					else
					{
						m_fileCompressionHeaders[m_numFiles] = NULL;
						m_fileDestSizes[m_numFiles] = dwSrcSize;
					}

					m_numFiles++;
				}
			}
			while ( FindNextFile( hFind, &findData ) );
			FindClose( hFind );
		}
		else
		{
			// source file not found, invalid
			return false;
		}
	}

	// finsihed with install script
	free( pFileData );

	g_installData.m_ppSrcFiles     = m_fileSrc;
	g_installData.m_ppDstFiles     = m_fileDest;
	g_installData.m_ppxcHeaders    = m_fileCompressionHeaders;
	g_installData.m_pDstFileSizes  = m_fileDestSizes;
	g_installData.m_numFiles       = m_numFiles;

	return true;
}

//-----------------------------------------------------------------------------
// Copies all install files to the hard drive
//-----------------------------------------------------------------------------
DWORD WINAPI InstallThreadFunc( LPVOID lpParam ) 
{
	char	mrkFilename[MAX_PATH];
	bool	bSuccess;
	HANDLE	hFile;

	g_installStartTime = GetTickCount();

	// started loading
	*(DWORD*)lpParam = 1;

	for ( int i = 0; i < g_installData.m_numFiles; ++i )
	{
		DWORD bytesCopied = g_copyStats.m_bytesCopied;

		// install has already validated, if it's in the list, copy it
		bSuccess = CopyFileOverlapped( g_installData.m_ppSrcFiles[i], g_installData.m_ppDstFiles[i], g_installData.m_ppxcHeaders[i], &g_copyStats );

		strcpy( mrkFilename, g_installData.m_ppDstFiles[i] );
		strcat( mrkFilename, ".mrk" );

		SetFileAttributes( mrkFilename, FILE_ATTRIBUTE_NORMAL );
		if ( bSuccess )
		{
			// add marker
			hFile = CreateFile( mrkFilename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
			if ( hFile != INVALID_HANDLE_VALUE )
			{
				CloseHandle( hFile );
			}
		}
		else
		{
			// remove marker
			DeleteFile( mrkFilename );
			DeleteFile( g_installData.m_ppDstFiles[i] );

			// errors can't stop install
			// snap progress to expected completion
			g_copyStats.m_bytesCopied = bytesCopied + g_installData.m_pDstFileSizes[i];
		}
	}

	g_installElapsedTime = GetTickCount() - g_installStartTime;

	// finished loading
	*(DWORD*)lpParam = 0;

	return 0;
}

//-----------------------------------------------------------------------------
// Verify disk space
//-----------------------------------------------------------------------------
bool CXBoxLoader::VerifyInstall( void )
{
	memset( &g_copyStats, 0, sizeof( CopyStats_t ) );

	LoadLogFile();

	if ( !LoadInstallScript() )
	{
		return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Look for possible forensic log file
//-----------------------------------------------------------------------------
void CXBoxLoader::LoadLogFile( void )
{
#if defined( XBOX_FORENSIC_LOG )
	HRESULT		hr;
	char		*pFileData = NULL;
	DWORD		fileSize = 0;

	hr = XBUtil_LoadFile( "Z:\\hl2fatal.log", (void**)&pFileData, &fileSize );
	if ( hr != S_OK || !fileSize )
	{
		return;
	}

	// copy and null terminate
	m_pLogData = (char *)malloc( fileSize+1 );

	int j = 0;
	for (int i=0; i<(int)fileSize; i++)
	{
		if ( pFileData[i] == 0x0D )
			continue;
		m_pLogData[j++] = pFileData[i];
	}
	m_pLogData[j] = '\0';

	free( pFileData );
#endif
}

//-----------------------------------------------------------------------------
// Starts installation to disk
//-----------------------------------------------------------------------------
bool CXBoxLoader::StartInstall( void )
{
	// Start the install thread
	m_installThread = CreateThread( NULL, 0, &InstallThreadFunc, &m_dwLoading, 0, 0 );
	if ( !m_installThread )
	{
		// failed
		return false;
	}

	// success
	return true;
}

//-----------------------------------------------------------------------------
// Shows the legal text
//-----------------------------------------------------------------------------
void CXBoxLoader::StartLegalScreen( int legal )
{
	m_bDrawLegal     = true;
	m_LegalTime      = GetTickCount();
	m_LegalStartTime = 0;

	switch ( legal )
	{
	case LEGAL_MAIN:
		m_pLegalTexture = m_pMainLegalTexture;
		break;
	case LEGAL_SOURCE:
		m_pLegalTexture = m_pSourceLegalTexture;
		break;
	}
}

//-----------------------------------------------------------------------------
// DrawLegals
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawLegals()
{
	unsigned int	color;
	float			t;

	if ( !m_bDrawLegal )
		return;

	if ( !m_LegalStartTime )
	{
		m_LegalStartTime = GetTickCount();
	}

	// fade legals
	t = (float)(GetTickCount() - m_LegalStartTime)/LEGAL_DISPLAY_TIME;
	if ( t < 0.1f ) 
	{
		// fade up
		color = LerpColor( 0xFF000000, 0xFFFFFFFF, t*10.0f );
	}
	else if ( t < 0.9f )
	{
		// hold
		color = 0xFFFFFFFF;
	}
	else
	{
		// fade out
		color = LerpColor( 0xFFFFFFFF, 0xFF000000, (t-0.9f)*10.0f );
	}

	DrawTexture( m_pLegalTexture, 0, 0, 640, 480, color ); 
}

//-----------------------------------------------------------------------------
// DrawSlideshow
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawSlideshow()
{
	float			t;

	if ( !m_bDrawSlideShow )
		return;

	if ( !m_SlideShowStartTime )
	{
		m_SlideShowStartTime = GetTickCount();
		m_SlideShowCount = -1;
		m_bFinalSlide = false;
	}

	if ( !m_bInstallComplete && ( GetTickCount() - m_SlideShowStartTime > SLIDESHOW_SLIDETIME ) )
	{
		// next slide
		m_SlideShowCount++;
		m_SlideShowStartTime = GetTickCount();
	}

	t = ( GetTickCount() - m_SlideShowStartTime )/(float)SLIDESHOW_FLIPTIME;
	if ( t >= 1.0f )
		t = 1.0f;

	if ( m_bInstallComplete && !m_bFinalSlide && t >= 1.0f )
	{
		// wait for current slide to complete
		// final slide must transition back to transition screen
		m_SlideShowStartTime = GetTickCount();
		m_bFinalSlide = true;
		t = 0;
	}

	if ( !m_bFinalSlide )
	{
		// fade next slide in
		unsigned int fadeInColor  = LerpColor( 0x00FFFFFF, 0xFFFFFFFF, t );
		if ( fadeInColor != 0xFFFFFFFF && m_SlideShowCount != -1 )
			DrawTexture( m_pSlideShowTextures[m_SlideShowCount % MAX_SLIDESHOW_TEXTURES], 0, 0, 640, 480, 0xFFFFFFFF ); 
		DrawTexture( m_pSlideShowTextures[(m_SlideShowCount + 1) % MAX_SLIDESHOW_TEXTURES], 0, 0, 640, 480, fadeInColor );
	}
	else
	{
		// fade last slide out
		unsigned int fadeInColor  = LerpColor( 0xFFFFFFFF, 0x00FFFFFF, t );
		DrawTexture( m_pSlideShowTextures[(m_SlideShowCount + 1) % MAX_SLIDESHOW_TEXTURES], 0, 0, 640, 480, fadeInColor );
	}

	if ( m_bInstallComplete && m_bFinalSlide && t >= 1.0f )
	{
		// end of slideshow
		m_bDrawSlideShow = false;
	}
}

//-----------------------------------------------------------------------------
// DrawDebug
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawDebug()
{
#ifndef _RETAIL
	if ( !m_bDrawDebug )
		return;

	DrawRect( 0, 0, 640, 480, 0xC0000000 );

	m_Font.Begin();
	m_Font.SetScaleFactors( 0.8f, 0.8f );

	int xPos = SCREEN_WIDTH/2;
	int yPos = SCREEN_HEIGHT/4;
	float rate;

	wchar_t	textBuffer[256];
	swprintf( textBuffer, L"Version: %d", m_Version );
	m_Font.DrawText( 40, 40, 0xffffffff, textBuffer, 0 );

	wchar_t srcFilename[MAX_PATH];
	wchar_t dstFilename[MAX_PATH];
	ConvertToWideString( srcFilename, g_copyStats.m_srcFilename );
	ConvertToWideString( dstFilename, g_copyStats.m_dstFilename );
	swprintf( textBuffer, L"From: %s (%.2f MB)", srcFilename, (float)g_copyStats.m_readSize/(1024.0f*1024.0f) );
	m_Font.DrawText( xPos, yPos + 20, 0xffffffff, textBuffer, XBFONT_CENTER_X );
	swprintf( textBuffer, L"To: %s (%.2f MB)", dstFilename, (float)g_copyStats.m_writeSize/(1024.0f*1024.0f)  );
	m_Font.DrawText( xPos, yPos + 40, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	if ( g_copyStats.m_bufferReadTime && m_dwLoading )
		rate = ( g_copyStats.m_bufferReadSize/(1024.0f*1024.0f) ) / ( g_copyStats.m_bufferReadTime * 0.001f );
	else
		rate = 0;
	swprintf( textBuffer, L"Buffer Read: %.2f MB (%.2f MB/s) (%d)", g_copyStats.m_bufferReadSize/(1024.0f*1024.0f), rate, g_copyStats.m_numReadBuffers );
	m_Font.DrawText( xPos, yPos + 80, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	rate = g_copyStats.m_inflateTime && m_dwLoading ? (float)g_copyStats.m_inflateSize/(g_copyStats.m_inflateTime * 0.001f) : 0;
	swprintf( textBuffer, L"Inflate: %.2f MB (%.2f MB/s)", g_copyStats.m_inflateSize/(1024.0f*1024.0f), rate/(1024.0f*1024.0f) );
	m_Font.DrawText( xPos, yPos + 100, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	if ( g_copyStats.m_bufferWriteTime && m_dwLoading )
		rate = ( g_copyStats.m_bufferWriteSize/(1024.0f*1024.0f) ) / ( g_copyStats.m_bufferWriteTime * 0.001f );
	else
		rate = 0;
	swprintf( textBuffer, L"Buffer Write: %.2f MB (%.2f MB/s) (%d)", g_copyStats.m_bufferWriteSize/(1024.0f*1024.0f), rate, g_copyStats.m_numWriteBuffers );
	m_Font.DrawText( xPos, yPos + 120, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	rate = g_copyStats.m_totalReadTime && m_dwLoading ? (float)g_copyStats.m_totalReadSize/(g_copyStats.m_totalReadTime * 0.001f) : 0;
	swprintf( textBuffer, L"Total Read: %d MB (%.2f MB/s)", g_copyStats.m_totalReadSize/(1024*1024), rate/(1024.0f*1024.0f) );
	m_Font.DrawText( xPos, yPos + 160, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	rate = g_copyStats.m_totalWriteTime && m_dwLoading ? (float)g_copyStats.m_totalWriteSize/(g_copyStats.m_totalWriteTime * 0.001f) : 0;
	swprintf( textBuffer, L"Total Write: %d MB (%.2f MB/s)", g_copyStats.m_totalWriteSize/(1024*1024), rate/(1024.0f*1024.0f) );
	m_Font.DrawText( xPos, yPos + 180, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	float elapsed = (float)(GetTickCount() - g_installStartTime) * 0.001f;
	if ( m_dwLoading )
	{
		if ( elapsed )
			rate = g_copyStats.m_totalWriteSize/elapsed;
		else
			rate = 0;
	}
	else
	{
		if ( g_installElapsedTime )
			rate = g_copyStats.m_totalWriteSize/(g_installElapsedTime * 0.001f);
		else
			rate = 0;
	}
	swprintf( textBuffer, L"Progress: %d/%d MB Elapsed: %d secs (%.2f MB/s)", g_copyStats.m_bytesCopied/(1024*1024), g_installData.m_totalSize/(1024*1024), (int)elapsed, rate/(1024.0f*1024.0f) );
	m_Font.DrawText( xPos, yPos + 220, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	swprintf( textBuffer, L"Errors: %d", g_copyStats.m_copyErrors );
	m_Font.DrawText( xPos, yPos + 240, 0xffffffff, textBuffer, XBFONT_CENTER_X );

	m_Font.End();
#endif
}

void CXBoxLoader::DrawLog()
{
#if defined( XBOX_FORENSIC_LOG )
	wchar_t	textBuffer[1024];
	int		numChars;

	if ( !m_pLogData )
		return;

	DrawRect( 0, 0, 640, 480, 0xC0000000 );

	m_Font.Begin();
	m_Font.SetScaleFactors( 0.8f, 0.8f );

	char *pStart = m_pLogData;
	char *pEnd = pStart;
	int yPos = 40;
	for (int i=0; i<20; i++)
	{
		pEnd = strstr( pStart, "\n" );
		if ( !pEnd )
			numChars = strlen( pStart );
		else
			numChars = pEnd-pStart;
	
		if ( numChars )
		{
			for (int j=0; j<numChars; j++)
			{
				textBuffer[j] = pStart[j];
			}
			textBuffer[j] = 0;
			m_Font.DrawText( 40, yPos, 0xffffffff, textBuffer, 0 );
		}

		if ( !pEnd )
			break;

		// next line
		pStart = pEnd+1;
		yPos += 10;
	}

	m_Font.End();
#endif
}

//-----------------------------------------------------------------------------
// DrawLoadingMarquee
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawLoadingMarquee()
{
	if ( !m_bDrawLoading )
		return;

	int y = 0.80f*480;
	DrawTexture( m_pLoadingIconTexture, (640-64)/2, y-64, 64, 64, 0xFFFFFFFF ); 

	// draw loading text
	m_Font.Begin();
	m_Font.SetScaleFactors( 0.8f, 0.8f );
	m_Font.DrawText( 320, y, PROGRESS_TEXT_COLOR, GetLocalizedLoadingString(), XBFONT_CENTER_X );
	m_Font.End();
}

//-----------------------------------------------------------------------------
// DrawProgressBar
//-----------------------------------------------------------------------------
void CXBoxLoader::DrawProgressBar()
{
	if ( !m_bDrawProgress )
		return;
	
	if ( !m_LoadingBarStartTime )
	{
		m_LoadingBarStartTime = GetTickCount();
	}

	// slide the loading bar up
	float tUp = (float)(GetTickCount() - m_LoadingBarStartTime)/LOADINGBAR_UPTIME;
	if ( tUp > 1.0f)
		tUp = 1.0f;
	float y = 480.0f + tUp*((float)PROGRESS_Y - 480.0f);

	float t0 = 0;
	float t1 = 0;
	int numSegments = 0;
	if ( tUp == 1.0f )
	{
		// loading bar is up
		// don't snap, animate progress to current level of completion
		t0 = (float)g_copyStats.m_bytesCopied/(float)g_installData.m_totalSize;
		if ( t0 > 1.0f )
			t0 = 1.0f;
		t1 = (float)(GetTickCount() - m_LoadingBarStartTime - LOADINGBAR_WAITTIME)/LOADINGBAR_SLIDETIME;
		if ( t1 < 0.0f )
			t1 = 0.0f;
		else if ( t1 > 1.0f)
			t1 = 1.0f;
		numSegments = t0 * t1 * (float)SEGMENT_COUNT;
	}

#if 0
	float tDown = 0;
	if ( t0 == 1.0f && t1 == 1.0f && !m_dwLoading )
	{
		// loading anim and copying of data are finished
		// slide the loading bar down
		if ( !m_LoadingBarEndTime )
		{
			m_LoadingBarEndTime = GetTickCount();
		}
		tDown = (float)(GetTickCount() - m_LoadingBarEndTime - LOADINGBAR_WAITTIME)/LOADINGBAR_UPTIME;
		if ( tDown < 0.0f )
			tDown = 0.0f;
		else if ( tDown > 1.0f)
			tDown = 1.0f;
		y = PROGRESS_Y + tDown*(480.0f - (float)PROGRESS_Y);
	}

	if ( tDown == 1.0f )
	{
		// loading bar is offscreen
		m_bInstallComplete = true;
	}
#else
	if ( t0 == 1.0f && t1 == 1.0f && !m_dwLoading )
	{
		m_bInstallComplete = true;
	}
#endif

	int x = (640-FOOTER_W)/2;
	DrawTexture( m_pFooterTexture, x, y, FOOTER_W, 480 - PROGRESS_Y, PROGRESS_FOOTER_COLOR ); 
	x += FOOTER_W - 35;

	// draw left justified loading text
	m_Font.Begin();
	m_Font.SetScaleFactors( 0.8f, 0.8f );
	int textWidth = m_Font.GetTextWidth( GetLocalizedLoadingString() );
	x -= SEGMENT_W + textWidth;
	m_Font.DrawText( x, y+20, PROGRESS_TEXT_COLOR, GetLocalizedLoadingString(), XBFONT_LEFT );
	m_Font.End();

	// draw progess bar
	x -= SEGMENT_W + PROGRESS_W;
	DrawRect( x, y+25, PROGRESS_W, PROGRESS_H, PROGRESS_INSET_COLOR );
	for ( int i =0; i<numSegments; i++ )
	{
		DrawRect( x, y+25+2, SEGMENT_W, PROGRESS_H-4, PROGRESS_SEGMENT_COLOR );
		x += SEGMENT_W+SEGMENT_GAP;
	}

}

//-----------------------------------------------------------------------------
// Name: PlayVideoFrame()
// Desc: Plays one frame of video if a movie is currently open and if there is
// a frame available. This function is safe to call at any time.
//-----------------------------------------------------------------------------
BOOL CXBoxLoader::PlayVideoFrame()
{
    if ( !m_player.IsPlaying() )
        return FALSE;

    const FLOAT fMovieWidth = FLOAT( m_player.GetWidth() );
    const FLOAT fMovieHeight = FLOAT( m_player.GetHeight() );

    // Move to the next frame.
    LPDIRECT3DTEXTURE8 pTexture = 0;
	pTexture = m_player.AdvanceFrameForTexturing( m_pd3dDevice );

    // See if the movie is over now.
    if ( !m_player.IsPlaying() )
    {
		if ( m_bCaptureLastMovieFrame )
		{
			m_bCaptureLastMovieFrame = false;

			// Copy Texture
			if ( m_pLastMovieFrame )
			{
				m_pLastMovieFrame->Release();
				m_pLastMovieFrame = NULL;
			}

			if ( pTexture )
			{
				// copy the last frame
				D3DSURFACE_DESC	d3dSurfaceDesc;
				D3DLOCKED_RECT	srcRect;
				D3DLOCKED_RECT	dstRect;
				pTexture->GetLevelDesc( 0, &d3dSurfaceDesc );
				m_pd3dDevice->CreateTexture( d3dSurfaceDesc.Width, d3dSurfaceDesc.Height, 0, 0, d3dSurfaceDesc.Format, 0, &m_pLastMovieFrame );
				pTexture->LockRect( 0, &srcRect, NULL, D3DLOCK_READONLY | D3DLOCK_NOOVERWRITE );
				m_pLastMovieFrame->LockRect( 0, &dstRect, NULL, D3DLOCK_READONLY | D3DLOCK_NOOVERWRITE );
				memcpy( dstRect.pBits, srcRect.pBits, srcRect.Pitch*d3dSurfaceDesc.Height );
			}
		}

        // Clean up the movie, then return.
        m_player.Destroy();
        return FALSE;
    }

    // If no texture is ready, return TRUE to indicate that a movie is playing,
    // but don't render anything yet.
    if ( !pTexture )
        return TRUE;

    const FLOAT fSizeY   = 480.0f;
    const FLOAT fOriginX = 320.0f - ( fSizeY * .5f * fMovieWidth / fMovieHeight );
    const FLOAT fOriginY = 240.0f - fSizeY * .5f;

    // Draw the texture.
    m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
    m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );

    // Draw the texture as a quad.
    m_pd3dDevice->SetTexture( 0, pTexture );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );

    // Wrapping isn't allowed on linear textures.
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );

    FLOAT fLeft   = fOriginX + 0.5f;
    FLOAT fRight  = fOriginX + ( fSizeY * fMovieWidth) / fMovieHeight - 0.5f;
    FLOAT fTop    = fOriginY + 0.5f;
    FLOAT fBottom = fOriginY + fSizeY - 0.5f;

    // On linear textures the texture coordinate range is from 0,0 to width,height, instead
    // of 0,0 to 1,1.
    m_pd3dDevice->SetVertexShader( D3DFVF_XYZRHW|D3DFVF_TEX1 );
    m_pd3dDevice->Begin( D3DPT_QUADLIST );
    m_pd3dDevice->SetVertexData2f( D3DVSDE_TEXCOORD0, 0, fMovieHeight );
    m_pd3dDevice->SetVertexData4f( D3DVSDE_VERTEX, fLeft,  fBottom, 0.0f, 1.0f );
    m_pd3dDevice->SetVertexData2f( D3DVSDE_TEXCOORD0, 0, 0 );
    m_pd3dDevice->SetVertexData4f( D3DVSDE_VERTEX, fLeft,  fTop,    0.0f, 1.0f );
    m_pd3dDevice->SetVertexData2f( D3DVSDE_TEXCOORD0, fMovieWidth, 0 );
    m_pd3dDevice->SetVertexData4f( D3DVSDE_VERTEX, fRight, fTop,    0.0f, 1.0f );
    m_pd3dDevice->SetVertexData2f( D3DVSDE_TEXCOORD0, fMovieWidth, fMovieHeight );
    m_pd3dDevice->SetVertexData4f( D3DVSDE_VERTEX, fRight, fBottom, 0.0f, 1.0f );
    m_pd3dDevice->End();
 
	return TRUE;
}

//-----------------------------------------------------------------------------
// LaunchHL2
//-----------------------------------------------------------------------------
void CXBoxLoader::LaunchHL2( unsigned int contextCode )
{
	LAUNCH_DATA			launchData;
	RelaunchHeader_t	*pRelaunch;
	const char			*pHL2Name;

	memset( &launchData, 0, sizeof( LAUNCH_DATA ) );

	// build the relaunch structure that HL2 uses
	pRelaunch = GetRelaunchHeader( launchData.Data );

	pRelaunch->magicNumber = RELAUNCH_MAGIC_NUMBER;
	pRelaunch->nBytesRelaunchData = 0;

	if ( ( contextCode & CONTEXTCODE_MAGICMASK ) == CONTEXTCODE_HL2MAGIC )
	{
		// ok to re-establish persistent data
		pRelaunch->bRetail     = (contextCode & CONTEXTCODE_RETAIL_MODE) > 0;
		pRelaunch->bInDebugger = (contextCode & CONTEXTCODE_INDEBUGGER) > 0;
	}
	else
	{
		// ensure we launch under retail conditions
		contextCode            = CONTEXTCODE_NO_XBDM;
		g_activeDevice         = -1;
		pRelaunch->bRetail     = true;
		pRelaunch->bInDebugger = false;
	}

	pRelaunch->contextCode  = contextCode;
	pRelaunch->activeDevice = g_activeDevice;
	pRelaunch->startTime    = g_loaderStartTime;

	// launch the xbe that is expected
	if ( contextCode & CONTEXTCODE_DEBUG_XBE )
	{
		// debug xbe
		pHL2Name = "D:\\hl2d_xbox.xbe";
	}
	else if ( contextCode & CONTEXTCODE_RELEASE_XBE )
	{
		// release xbe
		pHL2Name = "D:\\hl2r_xbox.xbe";
	}
	else
	{
		// default launch to retail xbe
		pHL2Name = "D:\\hl2_xbox.xbe";
	}

	XLaunchNewImage( pHL2Name, &launchData );

	// failed
	FatalMediaError();
}

//-----------------------------------------------------------------------------
// Performs initialization
//-----------------------------------------------------------------------------
HRESULT CXBoxLoader::Initialize()
{
	DWORD			launchType;
	LAUNCH_DATA		launchData;

	// no active device until set
	g_activeDevice = -1;

	// get launch info and command line params needed for early setting of systems
	LPSTR	pCmdLine = "";
	DWORD	retVal   = XGetLaunchInfo( &launchType, &launchData );
	if ( retVal == ERROR_SUCCESS )
	{
		if ( launchType == LDT_FROM_DASHBOARD )
		{
			// launched from dashboard
			LD_FROM_DASHBOARD *pLaunchFromDashboard = (LD_FROM_DASHBOARD *)(&launchData);
			m_contextCode = pLaunchFromDashboard->dwContext;
		}
		else if ( launchType == LDT_TITLE )
		{
			// launched directly from HL2 to do something
			LAUNCH_DATA* pLaunchData = (LAUNCH_DATA *)(&launchData);
			pCmdLine = (char *)pLaunchData->Data;

			RelaunchHeader_t *pHeader = GetRelaunchHeader( pLaunchData->Data );
			if ( pHeader->magicNumber == RELAUNCH_MAGIC_NUMBER )
			{
				m_contextCode  = pHeader->contextCode;
				g_activeDevice = pHeader->activeDevice;
			}
		}
		else if ( launchType == LDT_FROM_DEBUGGER_CMDLINE )
		{
			// launched from the debugger
			LAUNCH_DATA* pLaunchData = (LAUNCH_DATA *)(&launchData);
			pCmdLine = (char *)pLaunchData->Data;
			strlwr( pCmdLine );

			// assume retail mode
			m_contextCode |= CONTEXTCODE_HL2MAGIC;
			m_contextCode |= CONTEXTCODE_RETAIL_MODE;

			if ( strstr( pCmdLine, "-indebugger" ) )
			{
				m_contextCode |= CONTEXTCODE_INDEBUGGER;
			}
#ifndef _RETAIL
			if ( DmIsDebuggerPresent() )
			{
				m_contextCode |= CONTEXTCODE_INDEBUGGER;
			}
#endif
			if ( strstr( pCmdLine, "-debug" ) )
			{
				// launch to debug xbe
				m_contextCode |= CONTEXTCODE_DEBUG_XBE;
			}
			else if ( strstr( pCmdLine, "-release" ) )
			{
				// launch to release xbe
				m_contextCode |= CONTEXTCODE_RELEASE_XBE;
			}
			else
			{
				// default launch to retail xbe
				m_contextCode |= CONTEXTCODE_RETAIL_XBE|CONTEXTCODE_NO_XBDM;
				if ( strstr( pCmdLine, "-dev" ) )
				{
					// force dev link
					m_contextCode &= ~CONTEXTCODE_NO_XBDM;
				}
			}

			if ( strstr( pCmdLine, "-attract" ) )
			{
				// running the attract sequence
				m_contextCode |= CONTEXTCODE_ATTRACT;
			}
		}
	}

	if ( ( m_contextCode & CONTEXTCODE_MAGICMASK ) != CONTEXTCODE_HL2MAGIC )
	{
		// unknown, run the install normally
		// 0 is a special indicator, due to lack of valid magic
		m_contextCode = 0;
	}
	else
	{
		if ( m_contextCode & CONTEXTCODE_DASHBOARD )
		{
			// coming from dashboard, back to HL2 - immediately!
			LaunchHL2( m_contextCode );
			return S_OK;
		}
	}

   if ( FAILED( XFONT_OpenDefaultFont( &m_pDefaultTrueTypeFont ) ) )
   {
        return XBAPPERR_MEDIANOTFOUND;
   }

	// load install resources for context
	// Load resource file
	if ( FAILED( m_xprResource.Create( "D:\\LoaderMedia\\loader.xpr" ) ) )
	{
		return XBAPPERR_MEDIANOTFOUND;
	}

	if ( FAILED( LoadFont( &m_Font, loader_Font_OFFSET ) ) )
	{
		return XBAPPERR_MEDIANOTFOUND;
	}

	if ( !( m_contextCode & CONTEXTCODE_ATTRACT ) )
	{
		m_pFooterTexture = LoadTexture( loader_Footer_OFFSET );
		if ( !m_pFooterTexture )
		{
			return XBAPPERR_MEDIANOTFOUND;
		}

		switch( XGetLanguage() )
		{
		case XC_LANGUAGE_FRENCH:
			m_pMainLegalTexture = LoadTexture( loader_MainLegal_french_OFFSET );
			break;
		case XC_LANGUAGE_ITALIAN:
			m_pMainLegalTexture = LoadTexture( loader_MainLegal_italian_OFFSET );
			break;
		case XC_LANGUAGE_GERMAN:
			m_pMainLegalTexture = LoadTexture( loader_MainLegal_german_OFFSET );
			break;
		case XC_LANGUAGE_SPANISH:
			m_pMainLegalTexture = LoadTexture( loader_MainLegal_spanish_OFFSET );
			break;
		default:
			m_pMainLegalTexture = LoadTexture( loader_MainLegal_english_OFFSET );
			break;
		}
		if ( !m_pMainLegalTexture )
		{
			return XBAPPERR_MEDIANOTFOUND;
		}

		m_pSourceLegalTexture = LoadTexture( loader_SourceLegal_OFFSET );
		if ( !m_pSourceLegalTexture )
		{
			return XBAPPERR_MEDIANOTFOUND;
		}

		switch( XGetLanguage() )
		{
		case XC_LANGUAGE_FRENCH:
			m_pSlideShowTextures[0] = LoadTexture( loader_SlideShow1_french_OFFSET );
			break;
		case XC_LANGUAGE_ITALIAN:
			m_pSlideShowTextures[0] = LoadTexture( loader_SlideShow1_italian_OFFSET );
			break;
		case XC_LANGUAGE_GERMAN:
			m_pSlideShowTextures[0] = LoadTexture( loader_SlideShow1_german_OFFSET );
			break;
		case XC_LANGUAGE_SPANISH:
			m_pSlideShowTextures[0] = LoadTexture( loader_SlideShow1_spanish_OFFSET );
			break;
		default:
			m_pSlideShowTextures[0] = LoadTexture( loader_SlideShow1_english_OFFSET );
			break;
		}
		m_pSlideShowTextures[1] = LoadTexture( loader_SlideShow2_OFFSET );
		m_pSlideShowTextures[2] = LoadTexture( loader_SlideShow3_OFFSET );
		m_pSlideShowTextures[3] = LoadTexture( loader_SlideShow4_OFFSET );
		m_pSlideShowTextures[4] = LoadTexture( loader_SlideShow5_OFFSET );
		m_pSlideShowTextures[5] = LoadTexture( loader_SlideShow6_OFFSET );
		m_pSlideShowTextures[6] = LoadTexture( loader_SlideShow7_OFFSET );
		m_pSlideShowTextures[7] = LoadTexture( loader_SlideShow8_OFFSET );
		m_pSlideShowTextures[8] = LoadTexture( loader_SlideShow9_OFFSET );
		for ( int i=0; i<MAX_SLIDESHOW_TEXTURES; i++ )
		{
			if ( !m_pSlideShowTextures )
				return XBAPPERR_MEDIANOTFOUND;
		}

		if ( !VerifyInstall() )
		{
			OUTPUT_DEBUG_STRING( "Install failed!\n" );
			return -1;
		}
	}

	m_pLoadingIconTexture = LoadTexture( loader_LoadingIcon_OFFSET );
	if ( !m_pLoadingIconTexture )
	{
		return XBAPPERR_MEDIANOTFOUND;
	}

    return S_OK;
}

//-----------------------------------------------------------------------------
// Performs per-frame video checks
//-----------------------------------------------------------------------------
void CXBoxLoader::TickVideo()
{
	if ( m_bMovieErrorIsFatal && m_player.IsFailed() )
	{
		FatalMediaError();
	}
}

//-----------------------------------------------------------------------------
// Performs per-frame updates
//-----------------------------------------------------------------------------
HRESULT CXBoxLoader::FrameMove()
{
	bool bFailed = false;

	TickVideo();

	if ( m_State >= 10 && g_copyStats.m_copyErrors )
	{
		FatalMediaError();
	}

	if ( m_State >= 10 && ( m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START ) )
	{
		m_bDrawDebug ^= 1;
	}

	if ( m_bAllowAttractAbort && ( m_DefaultGamepad.bPressedAnalogButtons[XINPUT_GAMEPAD_A] || ( m_DefaultGamepad.wPressedButtons & XINPUT_GAMEPAD_START ) ) )
	{
		StopVideo();

		// allow only once, until reset
		m_bAllowAttractAbort = false;
	}

	switch ( m_State )
	{
		case 0:
			if ( m_contextCode & CONTEXTCODE_ATTRACT )
			{
				// play attract mode
				m_State = 1;
			}
			else
			{
				// normal installation
				m_State = 9;
			}
			break;

		case 1:
			// Play the attract video
			if ( FAILED( StartVideo( "D:\\LoaderMedia\\Demo_Attract.xmv", false, false ) ) )
			{
				// jump to finish
				m_State = 4;
			}
			else
			{
				m_State = 2;
			}
			break;
			
		case 2:
			if ( m_player.IsPlaying() || m_player.IsFailed() )
			{
				// attract is playing, wait for finish
				m_State = 3;
				m_bAllowAttractAbort = true;
			}
			break;

		case 3:
			if ( !m_player.IsPlaying() || m_player.IsFailed() )
			{
				// attract is over or aborted
				m_State = 4;
			}
			break;

		case 4:
			// place loading
			m_bDrawLoading = true;
			m_FrameCounter = 0;
			m_State = 5;
			break;

		case 5:
			// wait for two frames to pass to ensure loading is rendered
			if ( m_FrameCounter > 2 )
			{
				m_bLaunch = true;
				m_State = 6;
			}
			break;

		case 6:
			// idle
			m_State = 6;
			break;
		
		case 9:
			// Play the opening Valve video
			StartVideo( "D:\\LoaderMedia\\Valve_Leader.xmv", true, true );

			// Start the async installation process
			if ( !StartInstall() )
			{
				OUTPUT_DEBUG_STRING( "Install failed!\n" );
				bFailed = true;
				break;
			}
			m_State = 10;
			break;

		case 10:
			if ( m_player.IsPlaying() )
			{
				// intro is playing, wait for finish
				m_State = 15;
			}
			break;

		case 15:
			if ( !m_player.IsPlaying() )
			{
				// intro is over
				m_State = 20;
			}
			break;

		case 20:
			// start legals
			StartLegalScreen( LEGAL_SOURCE );
			m_State = 25;
			break;

		case 25:
			if ( m_bDrawLegal && GetTickCount() - m_LegalTime > LEGAL_DISPLAY_TIME )
			{
				// advance to next legal
				StartLegalScreen( LEGAL_MAIN );
				m_State = 30;
			}
			break;

		case 30:
			if ( m_bDrawLegal && GetTickCount() - m_LegalTime > LEGAL_DISPLAY_TIME )
			{
				// end of all legals
				if ( IsTargetFileValid( "Z:\\LoaderMedia\\Title_Load.xmv", 0 ) )
				{
					m_bDrawLegal = false;
					m_State = 40;
				}
			}
			break;

		case 40:
			// play the gordon/alyx hl2 game movie
			m_bCaptureLastMovieFrame = true;
			StartVideo( "Z:\\LoaderMedia\\Title_Load.xmv", true, true );
			m_State = 50;
			break;

		case 50:
			if ( m_player.IsPlaying() )
			{
				// title movie is playing, wait for finish
				m_State = 60;
			}
			break;

		case 60:
			if ( m_player.GetElapsedTime() >= 8000 && !m_bDrawProgress )
			{
				// wait for known audio click, then put up progress bar
				// start the loading bar animation
				m_bDrawProgress = true;
			}
			
			if ( m_bInstallComplete && m_bDrawProgress )
			{
				// install has completed
				if ( m_player.IsPlaying() )
				{
					// force the movie to end 
					m_player.TerminatePlayback();
				}
				m_State = 70;
			}
			else if ( !m_bInstallComplete && !m_player.IsPlaying() )
			{
				// intro movie has finished, but install is still running
				// start up slideshow cycler
				m_bDrawSlideShow = true;
				m_State = 70;
			}
			break;

		case 70:
			// wait for movie or slideshow to stop
			if ( !m_player.IsPlaying() && !m_bDrawSlideShow )
			{
				m_bLaunch = true;
				m_State = 80;
			}
			break;

		case 80:
			// idle
			break;
	}


	if ( bFailed )
	{
		FatalMediaError();
	}

	return S_OK;
}

//-----------------------------------------------------------------------------
// Renders the scene
//-----------------------------------------------------------------------------
HRESULT CXBoxLoader::Render()
{
    m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0L );

    // Play a frame from a video.
    BOOL bPlayedFrame = PlayVideoFrame();

	// hold the last frame of the title movie
	if ( m_State >= 60 && !bPlayedFrame )
	{
		DrawTexture( m_pLastMovieFrame, 0, 0, 640, 480, 0xFFFFFFFF );
	}

	DrawSlideshow();
	DrawLoadingMarquee();
	DrawProgressBar();
	DrawLegals();
	DrawDebug();
	DrawLog();

	if ( m_bLaunch )
	{
		// The installation has finished
		// Persist the image before launching hl2
		m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
		m_pd3dDevice->PersistDisplay();

		// Make sure the installation thread has completely exited
		if ( m_installThread )
		{
			WaitForSingleObject( m_installThread, INFINITE );
			CloseHandle( m_installThread );
		}

		LaunchHL2( m_contextCode );
		return S_OK;
	}

    // Present the scene
    m_pd3dDevice->Present( NULL, NULL, NULL, NULL );

	m_FrameCounter++;

    return S_OK;
}

//-----------------------------------------------------------------------------
// Entry point to the program.
//-----------------------------------------------------------------------------
VOID __cdecl main()
{
    CXBoxLoader xbApp;
    if ( FAILED( xbApp.Create() ) )
	{
		xbApp.FatalMediaError();
	}
	xbApp.Run();
}