Subversion Repositories spk

Rev

Rev 53 | Rev 56 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 53 Rev 54
Line 620... Line 620...
620
	return false;
620
	return false;
621
}
621
}
622
 
622
 
623
int C_File::ReadScriptVersion ()
623
int C_File::ReadScriptVersion ()
624
{
624
{
625
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
625
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) ) return 0;
626
		return 0;
-
 
627
 
626
 
628
	// check file pointer
-
 
629
	CyString file = GetFilePointer();
-
 
630
	// check file extenstion
-
 
631
	if ( (CFileIO(file).CheckFileExtension("pck")) || (CheckPCK()) )
-
 
632
	{
-
 
633
		size_t size = 0;
-
 
634
		unsigned char *data = UnPCKFile ( &size );
-
 
635
		if ( (data) && (size) )
-
 
636
			m_iVersion = ::ReadScriptVersionFromData ( data, (long)size );
-
 
637
	}
-
 
638
	else if ( (m_sData) && (m_iDataCompression == SPKCOMPRESS_NONE) )
627
	if ( (m_sData) && (m_iDataCompression == SPKCOMPRESS_NONE) )
639
		m_iVersion = ::ReadScriptVersionFromData ( m_sData, m_lDataSize );
628
		m_iVersion = ::ReadScriptVersionFromData ( m_sData, m_lDataSize );
640
	else
629
	else {
641
	{
-
 
642
		FILE *id = fopen ( file.c_str(), "rb+" );
-
 
643
		if ( id )
-
 
644
		{
-
 
645
			fclose ( id );
-
 
646
			std::string line;
-
 
647
			std::ifstream myfile ( file.c_str() );
630
		CFileIO File(GetFilePointer());
648
			if ( myfile.is_open() )
631
		if ( File.startRead() ) {
649
			{
-
 
650
				bool inscript = false;
-
 
651
				while (! myfile.eof() )
-
 
652
				{
-
 
653
					std::getline ( myfile, line );
632
			int iRead = (File.fileSize() > 5000) ? 5000 : File.fileSize();
654
					while ( line[0] == ' ' )
-
 
655
						line.erase ( 0, 1 );
-
 
656
					CyString sLine = line;
633
			unsigned char *data = File.read(iRead);
657
					if ( !inscript )
634
			if ( data ) {
658
					{
-
 
659
						if ( sLine.GetToken ( 1, '>' ) == "<script" )
-
 
660
							inscript = true;
-
 
661
					}
-
 
662
					else
-
 
663
					{
-
 
664
						if ( sLine.GetToken ( 1, '>' ) == "<version" )
-
 
665
						{
-
 
666
							m_iVersion = sLine.GetToken ( 2, '>' ).GetToken ( 1, '<' ).ToInt();
635
				m_iVersion = ::ReadScriptVersionFromData(data, iRead);
667
							break;
636
				delete []data;
668
						}
-
 
669
					}
-
 
670
				}
-
 
671
				myfile.close();
-
 
672
			}
637
			}
673
		}
638
		}
674
	}
639
	}
675
 
640
 
676
	return m_iVersion;
641
	return m_iVersion;
Line 723... Line 688...
723
			fclose(fOut);
688
			fclose(fOut);
724
			if ( err == Z_OK )
689
			if ( err == Z_OK )
725
			{
690
			{
726
				DeleteData ();
691
				DeleteData ();
727
 
692
 
728
				FILE *id = fopen ( "tempcompr.dat", "rb" );
693
				CFileIO File("tempcompr.dat");
729
				if ( id )
-
 
730
				{
-
 
731
					fseek ( id, 0, SEEK_END );
694
				File.setAutoDelete(true);
732
					m_lDataSize = ftell ( id );
695
				if ( File.startRead() ) {
733
					rewind ( id );
-
 
734
					m_sData = new unsigned char[m_lDataSize];
696
					m_sData = File.readAll((size_t *)&m_lDataSize);
735
					if ( !m_sData ) { fclose ( id ); m_iLastError = SPKERR_MALLOC; }
-
 
736
					else
-
 
737
					{
-
 
738
						fread ( m_sData, sizeof(unsigned char), m_lDataSize, id );
-
 
739
						if ( ferror(id) )
697
					if ( !m_sData ) {
740
						{
-
 
741
							m_iLastError = SPKERR_FILEREAD;
698
						m_iLastError = SPKERR_FILEREAD;
742
							DeleteData ();
-
 
743
							m_lDataSize = 0;
699
						m_lDataSize = 0;
744
						}
700
					}
745
						else
701
					else {
746
						{
-
 
747
							ret = true;
-
 
748
							m_iLastError = SPKERR_NONE;
702
						m_iLastError = SPKERR_NONE;
749
							m_iDataCompression = SPKCOMPRESS_ZLIB;
703
						m_iDataCompression = SPKCOMPRESS_ZLIB;
750
						}
704
						ret = true;
751
					}
705
					}
752
 
-
 
753
					fclose(id);
-
 
754
				}
706
				}
-
 
707
				File.close();
755
			}
708
			}
756
		}
709
		}
757
		CFileIO::Remove("tempcompr.dat");
-
 
758
 
-
 
759
		fclose(fIn);
710
		fclose(fIn);
760
	}
711
	}
761
 
712
 
762
	CFileIO::Remove("tempuncompr.dat");
713
	CFileIO::Remove("tempuncompr.dat");
763
 
714
 
Line 1130... Line 1081...
1130
}
1081
}
1131
 
1082
 
1132
bool C_File::WriteToFile ( CyString filename, unsigned char *cData, long len )
1083
bool C_File::WriteToFile ( CyString filename, unsigned char *cData, long len )
1133
{
1084
{
1134
	unsigned char *data = cData;
1085
	unsigned char *data = cData;
1135
	if ( (!len) || (!data) )
1086
	if ( (!len) || (!data) ) {
1136
	{
-
 
1137
		len = m_lDataSize;
1087
		len = m_lDataSize;
1138
		data = m_sData;
1088
		data = m_sData;
1139
	}
1089
	}
1140
 
1090
 
1141
	if ( (!len) || (!data) ) {
1091
	if ( (!len) || (!data) ) {
1142
		if ( m_lSize ) return false;
1092
		if ( m_lSize ) return false;
1143
	}
1093
	}
1144
 
1094
 
-
 
1095
	bool ret = false;
-
 
1096
 
1145
	// check for cat file
1097
	// check for cat file
1146
	if ( filename.IsIn ( "::" ) )
1098
	if ( filename.IsIn ( "::" ) ) {
1147
	{
-
 
1148
		CyString catfile = filename.GetToken ( "::", 1, 1 );
1099
		Utils::String catfile = filename.GetToken ( "::", 1, 1 ).ToString();
1149
		CyString file = filename.GetToken ( "::", 2, 2 );
1100
		Utils::String file = filename.GetToken ( "::", 2, 2 ).ToString();
1150
 
1101
 
1151
		CCatFile newcat;
1102
		CCatFile newcat;
1152
		return newcat.AddData ( catfile, data, len, file, true, true );
1103
		return newcat.AddData ( catfile, data, len, file, true, true );
1153
	}
1104
	}
1154
	else
1105
	else {
1155
	{
-
 
1156
		CyString filen = filename.FindReplace ( "/", "\\" );
1106
		Utils::String filen = filename.FindReplace ( "/", "\\" ).ToString();
1157
		filen = filen.FindReplace ( "\\\\", "\\" );
1107
		filen = filen.findReplace ( "\\\\", "\\" );
1158
 
-
 
1159
		FILE *id = fopen ( filen.c_str(), "wb" );
-
 
1160
		if ( !id )
-
 
1161
			return false;
-
 
1162
 
1108
 
1163
		bool ret = true;
-
 
1164
		if ( len && data ) {
1109
		if ( len && data ) {
1165
			fwrite ( data, sizeof(unsigned char), len, id );
-
 
1166
 
-
 
1167
			if ( ferror(id) )
-
 
1168
				ret = false;
1110
			CFileIO File(filen);
1169
 
-
 
1170
			fclose ( id );
1111
			if ( File.startWrite() ) ret = File.write(data, len);
1171
		}
1112
		}
1172
		return ret;
-
 
1173
	}
1113
	}
1174
 
1114
 
1175
	return false;
1115
	return ret;
1176
}
1116
}
1177
 
1117
 
1178
CyString C_File::GetFullFileToDir ( CyString dir, bool includedir, CBaseFile *file )
1118
CyString C_File::GetFullFileToDir ( CyString dir, bool includedir, CBaseFile *file )
1179
{
1119
{
1180
	CyString fullfile = dir;
1120
	CyString fullfile = dir;
Line 1466... Line 1406...
1466
bool C_File::CheckPCK ()
1406
bool C_File::CheckPCK ()
1467
{
1407
{
1468
	if ( (m_sData) && (m_lDataSize) && (m_iDataCompression == SPKCOMPRESS_NONE) )
1408
	if ( (m_sData) && (m_lDataSize) && (m_iDataCompression == SPKCOMPRESS_NONE) )
1469
		return IsDataPCK ( m_sData, m_lDataSize );
1409
		return IsDataPCK ( m_sData, m_lDataSize );
1470
 
1410
 
1471
	CyString filename = GetFilePointer();
1411
	Utils::String filename = GetFilePointer().ToString();
1472
	if ( !filename.Empty() )
1412
	if ( !filename.empty() ) {
1473
	{
-
 
1474
		FILE *id = fopen ( filename.c_str(), "rb+" );
1413
		CFileIO File(filename);
1475
		if ( id )
1414
		if ( File.startRead() ) {
1476
		{
-
 
1477
			unsigned char data[3];
1415
			unsigned char data[4];
1478
			fread ( data, sizeof(unsigned char), 3, id );
-
 
1479
			fclose ( id );
-
 
1480
 
-
 
1481
			return IsDataPCK ( data, 3 );
1416
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
1482
		}
1417
		}
1483
	}
1418
	}
1484
 
1419
 
1485
	if ( CheckFileExt("pck") || CheckFileExt("pbb") || CheckFileExt("pbd") )
1420
	if ( CheckFileExt("pck") || CheckFileExt("pbb") || CheckFileExt("pbd") ) return true;
1486
		return true;
-
 
1487
	return false;
1421
	return false;
1488
}
1422
}
1489
 
1423
 
1490
bool C_File::PCKFile()
1424
bool C_File::PCKFile()
1491
{
1425
{
Line 1524... Line 1458...
1524
	unsigned char *data = NULL;
1458
	unsigned char *data = NULL;
1525
	size_t datasize = 0;
1459
	size_t datasize = 0;
1526
 
1460
 
1527
	if ( CheckValidFilePointer() )
1461
	if ( CheckValidFilePointer() )
1528
	{
1462
	{
1529
		FILE *id = fopen ( GetFilePointer().c_str(), "rb+" );
1463
		CFileIO File(GetFilePointer().ToString());
1530
		if ( id )
-
 
1531
		{
-
 
1532
			fseek ( id, 0, SEEK_END );
-
 
1533
			datasize = ftell ( id );
-
 
1534
			rewind ( id );
-
 
1535
 
-
 
1536
			data = new unsigned char[datasize];
-
 
1537
			fread ( data, sizeof(unsigned char), datasize, id );
1464
		if ( File.startRead() ) data = File.readAll(&datasize);
1538
			fclose ( id );
-
 
1539
		}
-
 
1540
	}
1465
	}
1541
 
1466
 
1542
	if ( !data )
1467
	if ( !data )
1543
	{
1468
	{
1544
		if ( !m_lDataSize )
1469
		if ( !m_lDataSize )
Line 1592... Line 1517...
1592
	return false;
1517
	return false;
1593
}
1518
}
1594
 
1519
 
1595
unsigned char *UnPCKFile ( const char *file, size_t *len, bool nocrypt )
1520
unsigned char *UnPCKFile ( const char *file, size_t *len, bool nocrypt )
1596
{
1521
{
1597
	FILE *id = fopen ( file, "rb" );
-
 
1598
	if ( !id )
-
 
1599
		return NULL;
-
 
1600
 
-
 
1601
	fseek ( id, 0, SEEK_END );
-
 
1602
	size_t size = ftell ( id );
1522
	CFileIO File(file);
1603
	fseek ( id, 0, SEEK_SET );
-
 
1604
 
-
 
1605
	unsigned char *data = new unsigned char[size];
1523
	if ( !File.startRead() ) return NULL;
1606
	fread ( data, sizeof(unsigned char), size, id );
-
 
1607
 
-
 
1608
	if ( ferror(id) )
-
 
1609
	{
-
 
1610
		delete data;
-
 
1611
		data = NULL;
-
 
1612
	}
-
 
1613
 
1524
 
1614
	fclose ( id );
1525
	size_t size;
-
 
1526
	unsigned char *data = File.readAll(&size);
1615
 
1527
 
1616
	if ( data )
1528
	if ( data ) {
1617
	{
-
 
1618
		unsigned char *unData = UnPCKData ( data, size, len, nocrypt );
1529
		unsigned char *unData = UnPCKData ( data, size, len, nocrypt );
1619
		delete data;
1530
		delete data;
1620
		return unData;
1531
		return unData;
1621
	}
1532
	}
1622
 
1533
 
Line 1744... Line 1655...
1744
	return false;
1655
	return false;
1745
}
1656
}
1746
 
1657
 
1747
int ReadScriptVersionFromData ( unsigned char *data, long size )
1658
int ReadScriptVersionFromData ( unsigned char *data, long size )
1748
{
1659
{
-
 
1660
	int iVersion = 0;
-
 
1661
 
1749
	if ( IsDataPCK ( data, size ) )
1662
	if ( IsDataPCK ( data, size ) )
1750
	{
1663
	{
1751
		size_t unpckSize = 0;
1664
		size_t unpckSize = 0;
1752
		unsigned char *unpckData = UnPCKData ( data, size, &unpckSize );
1665
		unsigned char *unpckData = UnPCKData ( data, size, &unpckSize );
1753
		if ( (unpckData) && (unpckSize) )
1666
		if ( (unpckData) && (unpckSize) ) {
1754
			return ReadScriptVersionFromData ( unpckData, (long)unpckSize );
1667
			iVersion = ReadScriptVersionFromData ( unpckData, (long)unpckSize );
-
 
1668
			delete unpckData;
-
 
1669
		}
1755
		return 0;
1670
		return iVersion;
1756
	}
1671
	}
1757
 
1672
 
-
 
1673
	// only check the beginning of the file
-
 
1674
	int iMax = (size > 5000) ? 5000 : size;
-
 
1675
 
1758
	int pos = 0;
1676
	int pos = 0;
1759
	bool found = false;
1677
	bool found = false;
1760
	int iVersion = 0;
-
 
1761
 
-
 
1762
	// skip past initial space
1678
	// skip past initial space
-
 
1679
	char tag[21];
1763
	while ( !found )
1680
	while ( pos < iMax )
1764
	{
1681
	{
-
 
1682
		// skip past whitespace
1765
		while ( (pos < size) && (data[pos] == ' ') )
1683
		while ( (pos < iMax) && (data[pos] == ' ') ) ++pos;
1766
			++pos;
1684
		// find the first '<'
-
 
1685
		while ( (pos < iMax) && (data[pos] != '<') ) ++pos;
-
 
1686
		if ( data[pos] != '<' ) break; //not found
1767
 
1687
 
1768
		if ( data[pos] == '<' )
1688
		// find end tag '>'
1769
		{
-
 
1770
			CyString checktag;
1689
		int iStartPos = pos;
1771
			while ( (pos < size) && (data[pos] != '>') )
1690
		while ( (pos < iMax) && (data[pos] != '>') ) ++pos;
1772
			{
-
 
1773
				checktag += (char)data[pos];
1691
		if ( data[pos] != '>' ) break; //not found
1774
				++pos;
1692
		if ( (pos - iStartPos) > 20 ) continue;
1775
			}
-
 
1776
			++pos;
-
 
1777
 
1693
 
1778
			if ( checktag == "<version" )
1694
		// check if the tag is what we're looking for
1779
			{
-
 
1780
				CyString version;
1695
		memcpy(tag, data + iStartPos, pos - iStartPos);
1781
				while ( (pos < size) && (data[pos] != '<') )
1696
		tag[pos - iStartPos] = '\0';
1782
				{
-
 
1783
					version += (char)data[pos];
1697
		Utils::String sTag(tag);
1784
					++pos;
-
 
1785
				}
-
 
1786
				iVersion = version.ToInt();
1698
		if ( !sTag.Compare("<version") ) continue;
1787
				found = true;
-
 
1788
				break;
-
 
1789
			}
-
 
1790
		}
1699
		
1791
 
-
 
1792
		if ( found )
1700
		//extract the tag data
1793
			break;
-
 
1794
 
-
 
1795
		// skip to next line
1701
		iStartPos = pos + 1;
1796
		while ( (pos < size) && (data[pos] != '\n') )
1702
		while ( (pos < iMax) && (data[pos] != '<') ) ++pos;
1797
			++pos;
1703
		if ( (pos - iStartPos) > 20 ) continue;
1798
		++pos;
1704
		memcpy(tag, data + iStartPos, pos - iStartPos);
1799
 
-
 
1800
		if ( pos >= size )
1705
		tag[pos - iStartPos] = '\0';
1801
			break;
1706
		return atoi(tag);
1802
	}
1707
	}
1803
 
1708
 
1804
	return iVersion;
1709
	return iVersion;
1805
}
1710
}
1806
 
1711