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
	CyString file = filename.FindReplace ( "\\", "/" ).FindReplace ( "//", "/" );
-
 
429
	int tok = file.NumToken ( '/' );
428
	setFilename(filename.ToString());
430
 
429
}
431
	m_sFullDir = file.GetToken ( "/", 1, -1 );
430
void C_File::setFilename(const Utils::String &filename)
-
 
431
{
432
	m_sName = file.GetToken ( "/", -1 );
432
	Utils::String file = filename.findReplace ( "\\", "/" ).findReplace ( "//", "/" );
433
	if ( m_sFullDir.Right(2) == "/." )
-
 
434
		m_sFullDir = m_sFullDir.Left(-2);
-
 
435
 
433
 
-
 
434
	_sFullDir = file.tokens("/", 1, -2);
-
 
435
	m_sName = file.token("/", -1);
-
 
436
	if ( _sFullDir.right(2) == "/." )
-
 
437
		_sFullDir = _sFullDir.left(-2);
-
 
438
 
436
	ReadFileSize ();
439
	ReadFileSize ();
437
 
440
 
438
	ReadLastModified ();
441
	ReadLastModified ();
439
}
442
}
440
 
443
 
441
 
444
 
442
/*
445
/*
443
	Func:	ReadFromFile
446
	Func:	ReadFromFile
444
	Return:	Boolean - Returns true if read was successfull
447
	Return:	Boolean - Returns true if read was successfull
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;
459
	}
466
	}
460
 
467
 
461
	m_iDataCompression = SPKCOMPRESS_NONE;
468
	m_iDataCompression = SPKCOMPRESS_NONE;
462
	m_lDataSize = m_lUncomprDataSize = m_lSize;
469
	m_lDataSize = m_lUncomprDataSize = m_lSize;
463
 
470
 
464
	DeleteData ();
471
	DeleteData ();
465
 
472
 
Line 529... Line 536...
529
	catch(std::exception &e) {
536
	catch(std::exception &e) {
530
		CLog::logf(CLog::Log_IO, 2, "C_File::readFromFile() unable to read from file, %d (%s)", m_lDataSize, e.what());
537
		CLog::logf(CLog::Log_IO, 2, "C_File::readFromFile() unable to read from file, %d (%s)", m_lDataSize, e.what());
531
		DeleteData ();
538
		DeleteData ();
532
		m_lDataSize = 0;
539
		m_lDataSize = 0;
533
		return false;
540
		return false;
534
	}
541
	}
535
	return true;
542
	return true;
536
}
543
}
537
 
544
 
538
void C_File::copyData(const unsigned char *data, size_t size)
545
void C_File::copyData(const unsigned char *data, size_t size)
539
{
546
{
540
	m_lDataSize = (long)size;
547
	m_lDataSize = (long)size;
541
	delete m_sData;
548
	delete m_sData;
542
	m_sData = new unsigned char[size];
549
	m_sData = new unsigned char[size];
543
	memcpy(m_sData, data, size);
550
	memcpy(m_sData, data, size);
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();
555
 
562
 
556
	return fullfile;
563
	return fullfile;
557
}
564
}
558
 
565
 
559
bool C_File::isExternalFile() const
566
bool C_File::isExternalFile() const
560
{
567
{
561
	if (!m_sData && !m_lDataSize)
568
	if (!m_sData && !m_lDataSize)
562
	{
569
	{
563
		Utils::String file = this->filePointer();
570
		Utils::String file = this->filePointer();
564
		if (!file.empty() && CFileIO::Exists(file))
571
		if (!file.empty() && CFileIO::Exists(file))
565
			return true;
572
			return true;
Line 580... Line 587...
580
 
587
 
581
 
588
 
582
void C_File::UpdateSignature()
589
void C_File::UpdateSignature()
583
{
590
{
584
	m_sSignature = "";
591
	m_sSignature = "";
585
 
592
 
586
	bool deleteData = false;
593
	bool deleteData = false;
587
	if ( !m_sData )
594
	if ( !m_sData )
588
	{
595
	{
589
		if ( !ReadFromFile() )
596
		if ( !ReadFromFile() )
590
			return;
597
			return;
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;
639
}
646
}
640
 
647
 
641
/*
648
/*
642
	Func:	ReadLastModifed()
649
	Func:	ReadLastModifed()
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 1033... Line 1040...
1033
		if ( res == SZ_OK )
1040
		if ( res == SZ_OK )
1034
		{
1041
		{
1035
			DeleteData ();
1042
			DeleteData ();
1036
			m_iDataCompression = SPKCOMPRESS_NONE;
1043
			m_iDataCompression = SPKCOMPRESS_NONE;
1037
			m_lDataSize = (long)uncomprLen;
1044
			m_lDataSize = (long)uncomprLen;
1038
			m_sData = new unsigned char[m_lDataSize];
1045
			m_sData = new unsigned char[m_lDataSize];
1039
			memcpy(m_sData, uncompr, m_lDataSize);
1046
			memcpy(m_sData, uncompr, m_lDataSize);
1040
			delete uncompr;
1047
			delete uncompr;
1041
			return true;
1048
			return true;
1042
		}
1049
		}
1043
 
1050
 
1044
		if ( uncompr )
1051
		if ( uncompr )
1045
			delete [] uncompr;
1052
			delete [] uncompr;
1046
	}
1053
	}
1047
	else if ( m_iDataCompression == SPKCOMPRESS_7ZIP )
1054
	else if ( m_iDataCompression == SPKCOMPRESS_7ZIP )
1048
	{
1055
	{
1049
		long len = m_lUncomprDataSize;
1056
		long len = m_lUncomprDataSize;
1050
		unsigned char *compr = LZMADecode_C ( (unsigned char *)m_sData, m_lDataSize, (size_t*)&len, NULL );
1057
		unsigned char *compr = LZMADecode_C ( (unsigned char *)m_sData, m_lDataSize, (size_t*)&len, NULL );
1051
		if ( compr )
1058
		if ( compr )
Line 1053... Line 1060...
1053
			DeleteData ();
1060
			DeleteData ();
1054
			m_sData = compr;
1061
			m_sData = compr;
1055
			m_lDataSize = len;
1062
			m_lDataSize = len;
1056
			m_iDataCompression = SPKCOMPRESS_NONE;
1063
			m_iDataCompression = SPKCOMPRESS_NONE;
1057
			return true;
1064
			return true;
1058
		}
1065
		}
1059
	}
1066
	}
1060
 
1067
 
1061
	return false;
1068
	return false;
1062
}
1069
}
1063
 
1070
 
1064
unsigned char *C_File::UncompressData ( long *size, CProgressInfo *progress )
1071
unsigned char *C_File::UncompressData ( long *size, CProgressInfo *progress )
1065
{
1072
{
1066
	// no data to try to uncompress
1073
	// no data to try to uncompress
1067
	if ( (!m_sData) || (!m_lDataSize) )
1074
	if ( (!m_sData) || (!m_lDataSize) )
1068
		return NULL;
1075
		return NULL;
1069
 
1076
 
1070
	//if ( m_bCompressedToFile )
1077
	//if ( m_bCompressedToFile )
1071
	//	return NULL;
1078
	//	return NULL;
1072
 
1079
 
1073
	// if comopression is set to none, dont bother
1080
	// if comopression is set to none, dont bother
1074
	if ( m_iDataCompression == SPKCOMPRESS_NONE )
1081
	if ( m_iDataCompression == SPKCOMPRESS_NONE )
1075
	{
1082
	{
1076
		*size = m_lDataSize;
1083
		*size = m_lDataSize;
1077
		return m_sData;
1084
		return m_sData;
1078
	}
1085
	}
1079
 
1086
 
1080
	if ( m_iDataCompression == SPKCOMPRESS_ZLIB )
1087
	if ( m_iDataCompression == SPKCOMPRESS_ZLIB )
1081
	{
1088
	{
1082
		unsigned long uncomprLen = m_lUncomprDataSize;
1089
		unsigned long uncomprLen = m_lUncomprDataSize;
1083
		unsigned char *uncompr = new unsigned char[m_lUncomprDataSize];
1090
		unsigned char *uncompr = new unsigned char[m_lUncomprDataSize];
1084
		int err = uncompress ( uncompr, &uncomprLen, (const unsigned char *)m_sData, m_lDataSize );
1091
		int err = uncompress ( uncompr, &uncomprLen, (const unsigned char *)m_sData, m_lDataSize );
1085
		if ( err == Z_OK )
1092
		if ( err == Z_OK )
1086
		{
1093
		{
1087
			*size = uncomprLen;
1094
			*size = uncomprLen;
1088
			return uncompr;
1095
			return uncompr;
1089
		}
1096
		}
1090
		if ( uncompr )
1097
		if ( uncompr )
1091
			delete [] uncompr;
1098
			delete [] uncompr;
1092
	}
1099
	}
1093
	if ( m_iDataCompression == SPKCOMPRESS_7ZIP )
1100
	if ( m_iDataCompression == SPKCOMPRESS_7ZIP )
1094
	{
1101
	{
1095
		long len = m_lUncomprDataSize;
1102
		long len = m_lUncomprDataSize;
1096
		unsigned char *compr = LZMADecode_C ( m_sData, m_lDataSize, (size_t *)&len, NULL );
1103
		unsigned char *compr = LZMADecode_C ( m_sData, m_lDataSize, (size_t *)&len, NULL );
1097
		if ( compr )
1104
		if ( compr )
1098
		{
1105
		{
1099
			*size = len;
1106
			*size = len;
1100
			return compr;
1107
			return compr;
1101
		}
1108
		}
1102
		if ( compr )
1109
		if ( compr )
1103
			delete [] compr;
1110
			delete [] compr;
1104
	}
1111
	}
1105
	else if ( m_iDataCompression == SPKCOMPRESS_LZMA )
1112
	else if ( m_iDataCompression == SPKCOMPRESS_LZMA )
1106
	{
1113
	{
1107
		size_t uncomprLen = m_lUncomprDataSize;
1114
		size_t uncomprLen = m_lUncomprDataSize;
1108
		unsigned char *uncompr = new unsigned char[m_lUncomprDataSize];
1115
		unsigned char *uncompr = new unsigned char[m_lUncomprDataSize];
1109
		SRes res = Lzma86_Decode(uncompr, &uncomprLen, m_sData, (size_t*)&m_lDataSize);
1116
		SRes res = Lzma86_Decode(uncompr, &uncomprLen, m_sData, (size_t*)&m_lDataSize);
1110
		if ( res == SZ_OK )
1117
		if ( res == SZ_OK )
1111
		{
1118
		{
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;
1175
	}
1181
	}
1176
 
1182
 
1177
	if ( (!len) || (!data) ) {
1183
	if ( (!len) || (!data) ) {
1178
		if ( m_lSize ) return false;
1184
		if ( m_lSize ) return false;
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);
1198
		}
1204
		}
1199
	}
1205
	}
1200
 
1206
 
1201
	return ret;
1207
	return ret;
1202
}
1208
}
1203
 
-
 
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
 
1209
 
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 );
1252
}
1234
}
1253
CyString C_File::GetUncompressedSizeString ()
1235
CyString C_File::GetUncompressedSizeString ()
1254
{
1236
{
1255
	return SPK::GetSizeString ( GetUncompressedDataSize() );
1237
	return SPK::GetSizeString ( GetUncompressedDataSize() );
1256
}
1238
}
1257
 
1239
 
1258
CyString C_File::GetCreationTimeString ()
1240
CyString C_File::GetCreationTimeString ()
1259
{
1241
{
1260
	if ( !m_tTime )
1242
	if ( !m_tTime )
1261
		return NullString;
1243
		return NullString;
1262
 
1244
 
1263
	struct tm   *currDate;
1245
	struct tm   *currDate;
1264
	char    dateString[100];
1246
	char    dateString[100];
1265
 
1247
 
1266
	time_t n = m_tTime;
1248
	time_t n = m_tTime;
1267
 
1249
 
1268
	currDate = localtime(&n);
1250
	currDate = localtime(&n);
1269
 
1251
 
1270
	strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
1252
	strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
1271
 
1253
 
1272
 
1254
 
1273
	return CyString(dateString);
1255
	return CyString(dateString);
1274
}
1256
}
1275
 
1257
 
1276
bool C_File::CompareNew ( C_File *file )
1258
bool C_File::CompareNew ( C_File *file )
1277
{
1259
{
1278
	if ( !m_iVersion )
1260
	if ( !m_iVersion )
1279
		ReadScriptVersion ();
1261
		ReadScriptVersion ();
1280
 
1262
 
1281
	// if version, check if its later version
1263
	// if version, check if its later version
1282
	if ( (m_iVersion) && (file->GetVersion()) )
1264
	if ( (m_iVersion) && (file->GetVersion()) )
1283
	{
1265
	{
1284
		if ( m_iVersion > file->GetVersion() )
1266
		if ( m_iVersion > file->GetVersion() )
1285
			return false;
1267
			return false;
1286
	}
1268
	}
1287
 
1269
 
1288
	// now check for last modified time
1270
	// now check for last modified time
1289
	if ( (m_tTime) && (file->GetLastModified()) )
1271
	if ( (m_tTime) && (file->GetLastModified()) )
1290
	{
1272
	{
1291
		if ( m_tTime > file->GetLastModified() )
1273
		if ( m_tTime > file->GetLastModified() )
Line 1331... Line 1313...
1331
		case FILETYPE_SHIPSCENE:
1313
		case FILETYPE_SHIPSCENE:
1332
			return "ShipScene";
1314
			return "ShipScene";
1333
		case FILETYPE_COCKPITSCENE:
1315
		case FILETYPE_COCKPITSCENE:
1334
			return "CockpitScene";
1316
			return "CockpitScene";
1335
	}
1317
	}
1336
 
1318
 
1337
	return Utils::String::Null();
1319
	return Utils::String::Null();
1338
}
1320
}
1339
 
1321
 
1340
FileType GetFileTypeFromString ( CyString type )
1322
FileType GetFileTypeFromString ( CyString type )
1341
{
1323
{
Line 1430... Line 1412...
1430
		case SPKINSTALL_UNINSTALL_MOVE_FAIL:
1412
		case SPKINSTALL_UNINSTALL_MOVE_FAIL:
1431
			errorStr = "Unable to Move uninstall file: %1";
1413
			errorStr = "Unable to Move uninstall file: %1";
1432
			break;
1414
			break;
1433
		case SPKINSTALL_UNINSTALL_COPY:
1415
		case SPKINSTALL_UNINSTALL_COPY:
1434
			errorStr = "Coping uninstall file: %1";
1416
			errorStr = "Coping uninstall file: %1";
1435
			break;
1417
			break;
1436
		case SPKINSTALL_UNINSTALL_COPY_FAIL:
1418
		case SPKINSTALL_UNINSTALL_COPY_FAIL:
1437
			errorStr = "Unable to Copy uninstall file: %1";
1419
			errorStr = "Unable to Copy uninstall file: %1";
1438
			break;
1420
			break;
1439
		case SPKINSTALL_UNINSTALL_REMOVE:
1421
		case SPKINSTALL_UNINSTALL_REMOVE:
1440
			errorStr = "Removing Uninstall file: %1";
1422
			errorStr = "Removing Uninstall file: %1";
1441
			break;
1423
			break;
1442
		case SPKINSTALL_UNINSTALL_REMOVE_FAIL:
1424
		case SPKINSTALL_UNINSTALL_REMOVE_FAIL:
1443
			errorStr = "Unable to remove Uninstall file: %1";
1425
			errorStr = "Unable to remove Uninstall file: %1";
1444
			break;
1426
			break;
1445
		case SPKINSTALL_SHARED:
1427
		case SPKINSTALL_SHARED:
1446
			errorStr = "Removing Unused Shared file: %1";
1428
			errorStr = "Removing Unused Shared file: %1";
1447
			break;
1429
			break;
1448
		case SPKINSTALL_SHARED_FAIL:
1430
		case SPKINSTALL_SHARED_FAIL:
1449
			errorStr = "Unable to remove Unused Shared file: %1";
1431
			errorStr = "Unable to remove Unused Shared file: %1";
1450
			break;
1432
			break;
1451
		case SPKINSTALL_ORIGINAL_BACKUP:
1433
		case SPKINSTALL_ORIGINAL_BACKUP:
1452
			errorStr = "Backing up original file: %1";
1434
			errorStr = "Backing up original file: %1";
1453
			break;
1435
			break;
1454
		case SPKINSTALL_ORIGINAL_BACKUP_FAIL:
1436
		case SPKINSTALL_ORIGINAL_BACKUP_FAIL:
1455
			errorStr = "Unable to back up original file: %1";
1437
			errorStr = "Unable to back up original file: %1";
1456
			break;
1438
			break;
1457
		case SPKINSTALL_ORIGINAL_RESTORE:
1439
		case SPKINSTALL_ORIGINAL_RESTORE:
1458
			errorStr = "Restoring original file: %1";
1440
			errorStr = "Restoring original file: %1";
1459
			break;
1441
			break;
1460
		case SPKINSTALL_ORIGINAL_RESTORE_FAIL:
1442
		case SPKINSTALL_ORIGINAL_RESTORE_FAIL:
1461
			errorStr = "Unable to restore original file: %1";
1443
			errorStr = "Unable to restore original file: %1";
1462
			break;
1444
			break;
1463
		case SPKINSTALL_FAKEPATCH:
1445
		case SPKINSTALL_FAKEPATCH:
1464
			errorStr = "Shifted fake patch: %1 to %2";
1446
			errorStr = "Shifted fake patch: %1 to %2";
Line 1479... Line 1461...
1479
			errorStr = "Orphaned File removed: %1";
1461
			errorStr = "Orphaned File removed: %1";
1480
			break;
1462
			break;
1481
		case SPKINSTALL_ORPHANED_FAIL:
1463
		case SPKINSTALL_ORPHANED_FAIL:
1482
			errorStr = "Unable to remove Orphaned File: %1";
1464
			errorStr = "Unable to remove Orphaned File: %1";
1483
			break;
1465
			break;
1484
	}
1466
	}
1485
 
1467
 
1486
	CyString ret = errorStr.Args(args, max);
1468
	CyString ret = errorStr.Args(args, max);
1487
	CLEANSPLIT(args, max)
1469
	CLEANSPLIT(args, max)
1488
	return ret;
1470
	return ret;
1489
}
1471
}
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 1529... Line 1511...
1529
			m_bUsedMalloc = false;
1511
			m_bUsedMalloc = false;
1530
			m_lDataSize = (long)size;
1512
			m_lDataSize = (long)size;
1531
			m_sData = new unsigned char[size];
1513
			m_sData = new unsigned char[size];
1532
			memcpy(m_sData, data, size);
1514
			memcpy(m_sData, data, size);
1533
			delete [] data;
1515
			delete [] data;
1534
		}
1516
		}
1535
 
1517
 
1536
		return true;
1518
		return true;
1537
	}
1519
	}
1538
 
1520
 
1539
	return false;
1521
	return false;
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 1604... Line 1586...
1604
 
1586
 
1605
unsigned char *UnPCKFile ( const char *file, size_t *len, bool nocrypt )
1587
unsigned char *UnPCKFile ( const char *file, size_t *len, bool nocrypt )
1606
{
1588
{
1607
	CFileIO File(file);
1589
	CFileIO File(file);
1608
	if ( !File.startRead() ) return NULL;
1590
	if ( !File.startRead() ) return NULL;
1609
 
1591
 
1610
	size_t size;
1592
	size_t size;
1611
	unsigned char *data = File.readAll(&size);
1593
	unsigned char *data = File.readAll(&size);
1612
 
1594
 
1613
	if ( data ) {
1595
	if ( data ) {
1614
		unsigned char *unData = UnPCKData ( data, size, len, nocrypt );
1596
		unsigned char *unData = UnPCKData ( data, size, len, nocrypt );
Line 1654... Line 1636...
1654
		if ( tempData ) delete []tempData;
1636
		if ( tempData ) delete []tempData;
1655
		return NULL;
1637
		return NULL;
1656
	}
1638
	}
1657
	memset ( uncompr, 0, sizeof(uncompr) );
1639
	memset ( uncompr, 0, sizeof(uncompr) );
1658
 
1640
 
1659
 
1641
 
1660
	// find header size
1642
	// find header size
1661
	unsigned char *buf = newData + PCKHEADERSIZE;
1643
	unsigned char *buf = newData + PCKHEADERSIZE;
1662
 
1644
 
1663
//	buf = data + (6 + sizeof(time_t));
1645
//	buf = data + (6 + sizeof(time_t));
1664
	char flag = newData[3];
1646
	char flag = newData[3];
Line 1753... Line 1735...
1753
}
1735
}
1754
 
1736
 
1755
int ReadScriptVersionFromData ( unsigned char *data, long size )
1737
int ReadScriptVersionFromData ( unsigned char *data, long size )
1756
{
1738
{
1757
	int iVersion = 0;
1739
	int iVersion = 0;
1758
 
1740
 
1759
	if ( IsDataPCK ( data, size ) )
1741
	if ( IsDataPCK ( data, size ) )
1760
	{
1742
	{
1761
		size_t unpckSize = 0;
1743
		size_t unpckSize = 0;
1762
		unsigned char *unpckData = UnPCKData ( data, size, &unpckSize );
1744
		unsigned char *unpckData = UnPCKData ( data, size, &unpckSize );
1763
		if ( (unpckData) && (unpckSize) ) {
1745
		if ( (unpckData) && (unpckSize) ) {
Line 1802... Line 1784...
1802
		tag[pos - iStartPos] = '\0';
1784
		tag[pos - iStartPos] = '\0';
1803
		return atoi(tag);
1785
		return atoi(tag);
1804
	}
1786
	}
1805
 
1787
 
1806
	return iVersion;
1788
	return iVersion;
1807
}
1789
}
1808
 
1790
 
1809
CyString C_File::GetBaseName ()
1791
CyString C_File::GetBaseName ()
1810
{
1792
{
1811
	// remove any directory
1793
	// remove any directory
1812
	CyString file = m_sName.GetToken ( "/", m_sName.NumToken ( '/' ) );
1794
	CyString file = m_sName.GetToken ( "/", m_sName.NumToken ( '/' ) );
Line 1851... Line 1833...
1851
	m_sName = CFileIO(m_sName).ChangeFileExtension(ext);
1833
	m_sName = CFileIO(m_sName).ChangeFileExtension(ext);
1852
	return m_sName;
1834
	return m_sName;
1853
}
1835
}
1854
 
1836
 
1855
bool C_File::CheckPackedExtension()
1837
bool C_File::CheckPackedExtension()
1856
{
1838
{
1857
	CyString ext = this->GetFileExt();
1839
	CyString ext = this->GetFileExt();
1858
	if ( ext == "pck" )
1840
	if ( ext == "pck" )
1859
		return true;
1841
		return true;
1860
	else if ( ext == "pbb" )
1842
	else if ( ext == "pbb" )
1861
		return true;
1843
		return true;
1862
	else if ( ext == "pbd" )
1844
	else if ( ext == "pbd" )
1863
		return true;
1845
		return true;
1864
 
1846
 
1865
	return false;
1847
	return false;
1866
}
1848
}
1867
 
1849
 
1868
bool C_File::shouldCheckBaseName() const
1850
bool C_File::shouldCheckBaseName() const
1869
{
1851
{
1870
	Utils::String ext = CFileIO(m_sName).extension();
1852
	Utils::String ext = CFileIO(m_sName).extension();
1871
	if (ext == "xml" || ext == "txt" || ext == "pck")
1853
	if (ext == "xml" || ext == "txt" || ext == "pck")
1872
		return true;
1854
		return true;
1873
	if (ext == "bod" || ext == "bob" || ext == "pbb" || ext == "pbd")
1855
	if (ext == "bod" || ext == "bob" || ext == "pbb" || ext == "pbd")
1874
		return true;
1856
		return true;
1875
	return false;
1857
	return false;
1876
}
1858
}
1877
 
1859
 
1878
unsigned int C_File::game() const 
1860
unsigned int C_File::game() const 
1879
{ 
1861
{ 
1880
	return _iGame; 
1862
	return _iGame; 
1881
}
1863
}
-
 
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
}
1882
 
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;
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 2027... Line 2033...
2027
	// uncompress the file
2033
	// uncompress the file
2028
	if ( !this->UncompressData() )
2034
	if ( !this->UncompressData() )
2029
		return false;
2035
		return false;
2030
 
2036
 
2031
	// un pck the file
2037
	// un pck the file
2032
	if ( this->CheckFileExt("pck") )
2038
	if ( this->CheckFileExt("pck") )
2033
	{
2039
	{
2034
		if ( !this->UnPCKFile() )
2040
		if ( !this->UnPCKFile() )
2035
			return false;
2041
			return false;
2036
	}
2042
	}
2037
 
2043
 
2038
	// now we should have the raw data
2044
	// now we should have the raw data
2039
	CyString data((const char *)m_sData);
2045
	CyString data((const char *)m_sData);
2040
	data.Truncate(m_lDataSize);
2046
	data.Truncate(m_lDataSize);
Line 2042... Line 2048...
2042
 
2048
 
2043
	this->DeleteData();
2049
	this->DeleteData();
2044
	m_sData = new unsigned char[data.Length()];
2050
	m_sData = new unsigned char[data.Length()];
2045
	memcpy(m_sData, data.c_str(), data.Length());
2051
	memcpy(m_sData, data.c_str(), data.Length());
2046
	m_lDataSize = (long)data.Length();
2052
	m_lDataSize = (long)data.Length();
2047
 
2053
 
2048
	// repck the file
2054
	// repck the file
2049
	if ( this->CheckFileExt("pck") )
2055
	if ( this->CheckFileExt("pck") )
2050
	{
2056
	{
2051
		if ( !this->PCKFile() )
2057
		if ( !this->PCKFile() )
2052
			return false;
2058
			return false;
2053
	}
2059
	}
2054
 
2060
 
2055
 
2061
 
2056
	return true;
2062
	return true;
-
 
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;
2057
}
2087
}