Subversion Repositories spk

Rev

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

Rev 128 Rev 129
Line 421... Line 421...
421
	Accept:	filename - String for the filename of disk
421
	Accept:	filename - String for the filename of disk
422
	Desc:	Sets the file pointer
422
	Desc:	Sets the file pointer
423
			Reads the file size and last modifed time to store in the class
423
			Reads the file size and last modifed time to store in the class
424
			Splits up the filename and dir path
424
			Splits up the filename and dir path
425
*/
425
*/
426
void C_File::SetFilename ( CyString filename )
426
void C_File::SetFilename(CyString filename)
427
{
427
{
-
 
428
	setFilename(filename.ToString());
-
 
429
}
-
 
430
void C_File::setFilename(const Utils::String &filename)
-
 
431
{
428
	CyString file = filename.FindReplace ( "\\", "/" ).FindReplace ( "//", "/" );
432
	Utils::String file = filename.findReplace ( "\\", "/" ).findReplace ( "//", "/" );
429
	int tok = file.NumToken ( '/' );
-
 
430
 
433
 
431
	m_sFullDir = file.GetToken ( "/", 1, -1 );
434
	_sFullDir = file.tokens("/", 1, -2);
432
	m_sName = file.GetToken ( "/", -1 );
435
	m_sName = file.token("/", -1);
433
	if ( m_sFullDir.Right(2) == "/." )
436
	if ( _sFullDir.right(2) == "/." )
434
		m_sFullDir = m_sFullDir.Left(-2);
437
		_sFullDir = _sFullDir.left(-2);
435
 
438
 
436
	ReadFileSize ();
439
	ReadFileSize ();
437
 
440
 
438
	ReadLastModified ();
441
	ReadLastModified ();
439
}
442
}
Line 445... Line 448...
445
	Desc:	Reads data from file pointer into data stream
448
	Desc:	Reads data from file pointer into data stream
446
			As its read from a file, there will be no compression, so its set to None
449
			As its read from a file, there will be no compression, so its set to None
447
*/
450
*/
448
bool C_File::ReadFromFile ()
451
bool C_File::ReadFromFile ()
449
{
452
{
450
	return this->ReadFromFile(GetFilePointer().c_str());
453
	return this->readFromFile(filePointer());
451
}
454
}
452
 
455
 
453
bool C_File::ReadFromFile (CyString filename)
456
bool C_File::ReadFromFile(CyString filename)
-
 
457
{
-
 
458
	return readFromFile(filename.ToString());
-
 
459
}
-
 
460
bool C_File::readFromFile(const Utils::String filename)
454
{
461
{
455
	CFileIO File(filename);
462
	CFileIO File(filename);
456
	if ( !File.startRead() ) {
463
	if ( !File.startRead() ) {
457
		m_iLastError = SPKERR_FILEOPEN;
464
		m_iLastError = SPKERR_FILEOPEN;
458
		return false;
465
		return false;
Line 544... Line 551...
544
}
551
}
545
 
552
 
546
 
553
 
547
Utils::String C_File::filePointer() const
554
Utils::String C_File::filePointer() const
548
{
555
{
549
	Utils::String fullfile = m_sFullDir.ToString();
556
	Utils::String fullfile = _sFullDir;
550
	if ( !fullfile.empty() )
557
	if ( !fullfile.empty() )
551
		fullfile += "/";
558
		fullfile += "/";
552
 
559
 
553
	if ( !m_sName.Empty() )
560
	if ( !m_sName.Empty() )
554
		fullfile += m_sName.ToString();
561
		fullfile += m_sName.ToString();
Line 628... Line 635...
628
	Return:	Returns the file size read
635
	Return:	Returns the file size read
629
	Desc:	Opens the file and seeks to the end
636
	Desc:	Opens the file and seeks to the end
630
*/
637
*/
631
long C_File::ReadFileSize ()
638
long C_File::ReadFileSize ()
632
{
639
{
633
	CFileIO File(GetFilePointer());
640
	CFileIO File(filePointer());
634
	if ( File.exists() ) m_lSize = File.fileSize();
641
	if ( File.exists() ) m_lSize = File.fileSize();
635
 
642
 
636
	m_lUncomprDataSize = m_lSize;
643
	m_lUncomprDataSize = m_lSize;
637
 
644
 
638
	return m_lSize;
645
	return m_lSize;
Line 643... Line 650...
643
	Desc:	Reads the last modified time of the file and returns
650
	Desc:	Reads the last modified time of the file and returns
644
			Uses seperate rountines for Windows and Linux
651
			Uses seperate rountines for Windows and Linux
645
*/
652
*/
646
time_t C_File::ReadLastModified ()
653
time_t C_File::ReadLastModified ()
647
{
654
{
648
	CyString file = GetFilePointer();
655
	Utils::String file = filePointer();
649
	if ( file.Empty() )
656
	if ( file.empty() )
650
		return m_tTime;
657
		return m_tTime;
651
 
658
 
652
	#ifndef _WIN32
659
	#ifndef _WIN32
653
	struct stat attrib;			// create a file attribute structure
660
	struct stat attrib;			// create a file attribute structure
654
    stat ( file.c_str(), &attrib);
661
    stat ( file.c_str(), &attrib);
Line 659... Line 666...
659
	return m_tTime;
666
	return m_tTime;
660
}
667
}
661
 
668
 
662
bool C_File::CheckValidFilePointer ()
669
bool C_File::CheckValidFilePointer ()
663
{
670
{
664
	return CFileIO::Exists(GetFilePointer().ToString());
671
	return CFileIO::Exists(filePointer());
665
}
672
}
666
 
673
 
667
bool C_File::ReadSignedFile()
674
bool C_File::ReadSignedFile()
668
{
675
{
669
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
676
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
670
		return false;
677
		return false;
671
 
678
 
672
	// check file pointer
679
	// check file pointer
673
	CyString file = GetFilePointer();
680
	Utils::String file = filePointer();
674
	CyString ext = CFileIO(file).GetFileExtension();
681
	Utils::String ext = CFileIO(file).extension();
675
	if ( !ext.Compare("xml") && !ext.Compare("pck") )
682
	if ( !ext.Compare("xml") && !ext.Compare("pck") )
676
		return false;
683
		return false;
677
 
684
 
678
	if ( m_iDataCompression != SPKCOMPRESS_NONE )
685
	if ( m_iDataCompression != SPKCOMPRESS_NONE )
679
		return m_bSigned;
686
		return m_bSigned;
Line 713... Line 720...
713
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) ) return 0;
720
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) ) return 0;
714
 
721
 
715
	if ( (m_sData) && (m_iDataCompression == SPKCOMPRESS_NONE) )
722
	if ( (m_sData) && (m_iDataCompression == SPKCOMPRESS_NONE) )
716
		m_iVersion = ::ReadScriptVersionFromData ( m_sData, m_lDataSize );
723
		m_iVersion = ::ReadScriptVersionFromData ( m_sData, m_lDataSize );
717
	else {
724
	else {
718
		CFileIO File(GetFilePointer());
725
		CFileIO File(filePointer());
719
		if ( File.startRead() ) {
726
		if ( File.startRead() ) {
720
			int iRead = (File.fileSize() > 5000) ? 5000 : File.fileSize();
727
			int iRead = (File.fileSize() > 5000) ? 5000 : File.fileSize();
721
			unsigned char *data = File.read(iRead);
728
			unsigned char *data = File.read(iRead);
722
			if ( data ) {
729
			if ( data ) {
723
				m_iVersion = ::ReadScriptVersionFromData(data, iRead);
730
				m_iVersion = ::ReadScriptVersionFromData(data, iRead);
Line 759... Line 766...
759
########################################################################################################################
766
########################################################################################################################
760
*/
767
*/
761
 
768
 
762
bool C_File::CompressFile ( CProgressInfo *progress )
769
bool C_File::CompressFile ( CProgressInfo *progress )
763
{
770
{
764
	CyString file = this->GetFilePointer();
771
	Utils::String file = this->filePointer();
765
	if ( !CFileIO(this->GetFilePointer()).exists() )
772
	if ( !CFileIO(this->filePointer()).exists() )
766
	{
773
	{
767
		if ( !this->WriteToFile("tempuncompr.dat", m_sData, m_lDataSize) )
774
		if ( !this->writeToFile("tempuncompr.dat", m_sData, m_lDataSize) )
768
			return false;
775
			return false;
769
		file = "tempuncompr.dat";
776
		file = "tempuncompr.dat";
770
	}
777
	}
771
 
778
 
772
	bool ret = false;
779
	bool ret = false;
Line 1137... Line 1144...
1137
	{
1144
	{
1138
		m_iTempNum++;
1145
		m_iTempNum++;
1139
		file = CyString("uncompr") + (long)m_iTempNum + ".tmp";
1146
		file = CyString("uncompr") + (long)m_iTempNum + ".tmp";
1140
	}
1147
	}
1141
	else
1148
	else
1142
		file = GetFullFileToDir ( file, includedir, spkfile );
1149
		file = _getFullFileToDir(file.ToString(), includedir, spkfile);
1143
 
1150
 
1144
	CFileIO File("compr.tmp");
1151
	CFileIO File("compr.tmp");
1145
	if ( !File.startWrite() ) return false;
1152
	if ( !File.startWrite() ) return false;
1146
	if ( !File.write(m_sData, m_lDataSize) ) return false;
1153
	if ( !File.write(m_sData, m_lDataSize) ) return false;
1147
	File.close();
1154
	File.close();
Line 1159... Line 1166...
1159
	return false;
1166
	return false;
1160
#endif
1167
#endif
1161
}
1168
}
1162
 
1169
 
1163
 
1170
 
1164
bool C_File::WriteFilePointer ( unsigned char *cData, long len )
1171
bool C_File::writeFilePointer(unsigned char *cData, long len)
1165
{
1172
{
1166
	return WriteToFile ( GetFilePointer(), cData, len );
1173
	return writeToFile(filePointer(), cData, len);
1167
}
1174
}
1168
 
-
 
1169
bool C_File::WriteToFile ( CyString filename, unsigned char *cData, long len )
1175
bool C_File::writeToFile(const Utils::String &filename, unsigned char *cData, long len)
1170
{
1176
{
1171
	unsigned char *data = cData;
1177
	unsigned char *data = cData;
1172
	if ( (!len) || (!data) ) {
1178
	if ( (!len) || (!data) ) {
1173
		len = m_lDataSize;
1179
		len = m_lDataSize;
1174
		data = m_sData;
1180
		data = m_sData;
Line 1179... Line 1185...
1179
	}
1185
	}
1180
 
1186
 
1181
	bool ret = false;
1187
	bool ret = false;
1182
 
1188
 
1183
	// check for cat file
1189
	// check for cat file
1184
	if ( filename.IsIn ( "::" ) ) {
1190
	if ( filename.contains("::")) {
1185
		Utils::String catfile = filename.GetToken ( "::", 1, 1 ).ToString();
1191
		Utils::String catfile = filename.token("::", 1);
1186
		Utils::String file = filename.GetToken ( "::", 2, 2 ).ToString();
1192
		Utils::String file = filename.token( "::", 2);
1187
 
1193
 
1188
		CCatFile newcat;
1194
		CCatFile newcat;
1189
		return newcat.AddData ( catfile, data, len, file, true, true );
1195
		return newcat.AddData ( catfile, data, len, file, true, true );
1190
	}
1196
	}
1191
	else {
1197
	else {
1192
		Utils::String filen = filename.FindReplace ( "/", "\\" ).ToString();
1198
		Utils::String filen = filename.findReplace ( "/", "\\" );
1193
		filen = filen.findReplace ( "\\\\", "\\" );
1199
		filen = filen.findReplace ( "\\\\", "\\" );
1194
 
1200
 
1195
		if ( len && data ) {
1201
		if ( len && data ) {
1196
			CFileIO File(filen);
1202
			CFileIO File(filen);
1197
			if ( File.startWrite() ) ret = File.write(data, len);
1203
			if ( File.startWrite() ) ret = File.write(data, len);
Line 1199... Line 1205...
1199
	}
1205
	}
1200
 
1206
 
1201
	return ret;
1207
	return ret;
1202
}
1208
}
1203
 
1209
 
1204
CyString C_File::GetFullFileToDir ( CyString dir, bool includedir, CBaseFile *file )
-
 
1205
{
-
 
1206
	CyString fullfile = dir;
-
 
1207
	if ( includedir )
-
 
1208
	{
-
 
1209
		CyString d = GetDirectory ( file );
-
 
1210
		if ( !d.Empty() )
-
 
1211
		{
-
 
1212
			if ( !fullfile.Empty() )
-
 
1213
				fullfile += "/";
-
 
1214
			fullfile += d;
-
 
1215
		}
-
 
1216
	}
-
 
1217
	if ( !m_sName.Empty() )
-
 
1218
	{
-
 
1219
		if ( !fullfile.Empty() )
-
 
1220
			fullfile += "/";
-
 
1221
		fullfile += m_sName;
-
 
1222
	}
-
 
1223
 
-
 
1224
	fullfile = fullfile.FindReplace ( "\\", "/" );
-
 
1225
	return fullfile;
-
 
1226
}
-
 
1227
 
-
 
1228
bool C_File::WriteToDir ( CyString &dir, CBaseFile *spkfile, bool includedir, CyString appendDir, unsigned char *data, long len )
1210
bool C_File::writeToDir(const Utils::String &dir, CBaseFile *spkfile, bool includedir, const Utils::String &appendDir, unsigned char *data, long len)
1229
{
1211
{
1230
	CyString fullfile = GetFullFileToDir ( dir, includedir, spkfile );
1212
	Utils::String fullfile = _getFullFileToDir(dir, includedir, spkfile);
1231
 
1213
 
1232
	if ( !appendDir.Empty() )
1214
	if (!appendDir.empty())
1233
	{
1215
	{
1234
		if ( !fullfile.Empty() )
1216
		if (!fullfile.empty())
1235
			fullfile += "/";
1217
			fullfile += "/";
1236
		fullfile += appendDir;
1218
		fullfile += appendDir;
1237
	}
1219
	}
1238
 
1220
 
1239
	CyString fulldir = fullfile.GetToken ( 1, fullfile.NumToken('/') - 2, '/' );
1221
	Utils::String fulldir = fullfile.tokens("/", 1, -2);
1240
	if ( !fulldir.Empty() )
1222
	if (!fulldir.empty())
1241
	{
1223
	{
1242
		if ( !CDirIO(fulldir).Create() )
1224
		if (!CDirIO(fulldir).Create())
1243
			return false;
1225
			return false;
1244
	}
1226
	}
1245
 
1227
 
1246
	return WriteToFile ( fullfile, data, len );
1228
	return writeToFile(fullfile, data, len);
1247
}
1229
}
1248
 
1230
 
1249
CyString C_File::GetDataSizeString ()
1231
CyString C_File::GetDataSizeString ()
1250
{
1232
{
1251
	return SPK::GetSizeString ( m_lDataSize );
1233
	return SPK::GetSizeString ( m_lDataSize );
Line 1492... Line 1474...
1492
bool C_File::CheckPCK ()
1474
bool C_File::CheckPCK ()
1493
{
1475
{
1494
	if ( (m_sData) && (m_lDataSize) && (m_iDataCompression == SPKCOMPRESS_NONE) )
1476
	if ( (m_sData) && (m_lDataSize) && (m_iDataCompression == SPKCOMPRESS_NONE) )
1495
		return IsDataPCK ( m_sData, m_lDataSize );
1477
		return IsDataPCK ( m_sData, m_lDataSize );
1496
 
1478
 
1497
	Utils::String filename = GetFilePointer().ToString();
1479
	Utils::String filename = filePointer();
1498
	if ( !filename.empty() ) {
1480
	if ( !filename.empty() ) {
1499
		CFileIO File(filename);
1481
		CFileIO File(filename);
1500
		if ( File.startRead() ) {
1482
		if ( File.startRead() ) {
1501
			unsigned char data[4];
1483
			unsigned char data[4];
1502
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
1484
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
Line 1544... Line 1526...
1544
	unsigned char *data = NULL;
1526
	unsigned char *data = NULL;
1545
	size_t datasize = 0;
1527
	size_t datasize = 0;
1546
 
1528
 
1547
	if ( CheckValidFilePointer() )
1529
	if ( CheckValidFilePointer() )
1548
	{
1530
	{
1549
		CFileIO File(GetFilePointer().ToString());
1531
		CFileIO File(filePointer());
1550
		if ( File.startRead() ) data = File.readAll(&datasize);
1532
		if ( File.startRead() ) data = File.readAll(&datasize);
1551
	}
1533
	}
1552
 
1534
 
1553
	if ( !data )
1535
	if ( !data )
1554
	{
1536
	{
Line 1878... Line 1860...
1878
unsigned int C_File::game() const 
1860
unsigned int C_File::game() const 
1879
{ 
1861
{ 
1880
	return _iGame; 
1862
	return _iGame; 
1881
}
1863
}
1882
 
1864
 
-
 
1865
bool C_File::isForGame(int game) const
-
 
1866
{
-
 
1867
	if (game > 0)
-
 
1868
	{
-
 
1869
		if (!_iGame || _iGame == GAME_ALLNEW)
-
 
1870
			return true;
-
 
1871
		return (_iGame & 1 << game) != 0;
-
 
1872
	}
-
 
1873
 
-
 
1874
	return true;
-
 
1875
}
-
 
1876
 
-
 
1877
int C_File::getForSingleGame() const
-
 
1878
{
-
 
1879
	int checkGame = _iGame & ~GAME_ALLNEW;
-
 
1880
	for (int i = 0; i < 31; ++i)
-
 
1881
	{
-
 
1882
		if (checkGame == (1 << i))
-
 
1883
			return i;
-
 
1884
	}
-
 
1885
 
-
 
1886
	return 0;
-
 
1887
}
-
 
1888
 
1883
void C_File::setGame(unsigned int i) 
1889
void C_File::setGame(unsigned int i) 
1884
{ 
1890
{ 
1885
	if (i == 0 || i == 1 << 31)
1891
	if (i == 0 || i == 1 << 31)
1886
		_iGame = 0;
1892
		_iGame = 0;
1887
	else
1893
	else
Line 1891... Line 1897...
1891
 
1897
 
1892
unsigned char *C_File::BobDecompile(size_t *size)
1898
unsigned char *C_File::BobDecompile(size_t *size)
1893
{
1899
{
1894
	(*size) = 0;
1900
	(*size) = 0;
1895
 
1901
 
1896
	Utils::String fromFile = this->GetFilePointer().ToString();
1902
	Utils::String fromFile = this->filePointer();
1897
	if ( !CFileIO::Exists(fromFile) ) {
1903
	if ( !CFileIO::Exists(fromFile) ) {
1898
		if ( this->WriteToFile(CPackages::tempDirectory() + "bob.tmp") ) {
1904
		if ( this->writeToFile(CPackages::tempDirectory() + "bob.tmp") ) {
1899
			fromFile = CPackages::tempDirectory() + "bob.tmp";
1905
			fromFile = CPackages::tempDirectory() + "bob.tmp";
1900
		}
1906
		}
1901
	}
1907
	}
1902
 
1908
 
1903
	fromFile = fromFile.findReplace("/", "\\");
1909
	fromFile = fromFile.findReplace("/", "\\");
Line 1924... Line 1930...
1924
	bob_dom_document doc(&settings);
1930
	bob_dom_document doc(&settings);
1925
	
1931
	
1926
	ibinaryrealfile is;
1932
	ibinaryrealfile is;
1927
	otextrealfile os;
1933
	otextrealfile os;
1928
	os.open(CPackages::tempDirectory() + "tmp.tmp", filestream::create);
1934
	os.open(CPackages::tempDirectory() + "tmp.tmp", filestream::create);
1929
	is.open(this->GetFilePointer().c_str(), filestream::rdonly);
1935
	is.open(this->filePointer().c_str(), filestream::rdonly);
1930
	
1936
	
1931
	if(is.fail() || os.fail()) return NULL;
1937
	if(is.fail() || os.fail()) return NULL;
1932
	
1938
	
1933
	bool bRes=doc.convert(is, os);
1939
	bool bRes=doc.convert(is, os);
1934
 
1940
 
Line 2052... Line 2058...
2052
			return false;
2058
			return false;
2053
	}
2059
	}
2054
 
2060
 
2055
 
2061
 
2056
	return true;
2062
	return true;
2057
}
-
 
2058
2063
}
-
 
2064
 
-
 
2065
Utils::String C_File::_getFullFileToDir(const Utils::String &dir, bool includedir, CBaseFile *file) const
-
 
2066
{
-
 
2067
	Utils::String fullfile = dir;
-
 
2068
	if (includedir)
-
 
2069
	{
-
 
2070
		Utils::String d = getDirectory(file);
-
 
2071
		if (!d.empty())
-
 
2072
		{
-
 
2073
			if (!fullfile.empty())
-
 
2074
				fullfile += "/";
-
 
2075
			fullfile += d;
-
 
2076
		}
-
 
2077
	}
-
 
2078
	if (!m_sName.Empty())
-
 
2079
	{
-
 
2080
		if (!fullfile.empty())
-
 
2081
			fullfile += "/";
-
 
2082
		fullfile += m_sName.ToString();
-
 
2083
	}
-
 
2084
 
-
 
2085
	fullfile = fullfile.findReplace("\\", "/");
-
 
2086
	return fullfile;
-
 
2087
}
-
 
2088