Subversion Repositories spk

Rev

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

Rev 196 Rev 197
Line 38... Line 38...
38
 
38
 
39
//////////////////////////////////////////////////////////////////////
39
//////////////////////////////////////////////////////////////////////
40
// STATIC FUNCTIONS
40
// STATIC FUNCTIONS
41
//////////////////////////////////////////////////////////////////////
41
//////////////////////////////////////////////////////////////////////
42
 
42
 
43
Utils::String C_File::GetDirectory(FileType eType, const Utils::String &filename, CBaseFile *file)
43
Utils::WString C_File::GetDirectory(FileType eType, const Utils::WString &filename, CBaseFile *file)
44
{
44
{
45
	switch (eType)
45
	switch (eType)
46
	{
46
	{
47
	case FILETYPE_SCRIPT:
47
	case FILETYPE_SCRIPT:
48
		return "Scripts";
48
		return L"Scripts";
49
	case FILETYPE_TEXT:
49
	case FILETYPE_TEXT:
50
		return "T";
50
		return L"T";
51
	case FILETYPE_README:
51
	case FILETYPE_README:
52
	{
52
	{
53
		if (file)
53
		if (file)
54
			return Utils::String("PluginManager/Readme/") + file->getNameValidFile();
54
			return Utils::WString(L"PluginManager/Readme/") + file->getNameValidFile();
55
		return "PluginManager/Readme";
55
		return L"PluginManager/Readme";
56
	}
56
	}
57
	case FILETYPE_MAP:
57
	case FILETYPE_MAP:
58
		return "Maps";
58
		return L"Maps";
59
	case FILETYPE_MOD:
59
	case FILETYPE_MOD:
60
		return "Mods";
60
		return L"Mods";
61
	case FILETYPE_UNINSTALL:
61
	case FILETYPE_UNINSTALL:
62
		return "PluginManager/Uninstall";
62
		return L"PluginManager/Uninstall";
63
	case FILETYPE_SOUND:
63
	case FILETYPE_SOUND:
64
		if (!filename.empty() && !CFileIO(filename).isFileExtension(L"wav"))
64
		if (!filename.empty() && !CFileIO(filename).isFileExtension(L"wav"))
65
			return "Soundtrack";
65
			return L"Soundtrack";
66
		return "s";
66
		return L"s";
67
	case FILETYPE_SOUNDTRACK:
67
	case FILETYPE_SOUNDTRACK:
68
		return "Soundtrack";
68
		return L"Soundtrack";
69
	case FILETYPE_EXTRA:
69
	case FILETYPE_EXTRA:
70
		return "PluginManager/Extras";
70
		return L"PluginManager/Extras";
71
	case FILETYPE_SCREEN:
71
	case FILETYPE_SCREEN:
72
		return "loadscr";
72
		return L"loadscr";
73
	case FILETYPE_ADVERT:
73
	case FILETYPE_ADVERT:
74
		return "PluginManager/Graphics";
74
		return L"PluginManager/Graphics";
75
	case FILETYPE_MISSION:
75
	case FILETYPE_MISSION:
76
		return "Director";
76
		return L"Director";
77
	}
77
	}
78
	return Utils::String::Null();
78
	return Utils::WString::Null();
79
}
79
}
80
 
80
 
81
bool C_File::DoesTypeHaveExtraDir(int i)
81
bool C_File::DoesTypeHaveExtraDir(int i)
82
{
82
{
83
	switch (i)
83
	switch (i)
Line 129... Line 129...
129
	m_bUsedMalloc = false;
129
	m_bUsedMalloc = false;
130
	m_sData = NULL;
130
	m_sData = NULL;
131
	Reset();
131
	Reset();
132
}
132
}
133
 
133
 
134
C_File::C_File(const Utils::String &filename)
134
C_File::C_File(const Utils::WString &filename)
135
{
135
{
136
	m_bDontDeleteData = false;
136
	m_bDontDeleteData = false;
137
	m_bUsedMalloc = false;
137
	m_bUsedMalloc = false;
138
	m_sData = NULL;
138
	m_sData = NULL;
139
	Reset ();
139
	Reset ();
Line 142... Line 142...
142
 
142
 
143
C_File::~C_File()
143
C_File::~C_File()
144
{
144
{
145
	DeleteData ();
145
	DeleteData ();
146
	if (!_sTmpFile.empty())
146
	if (!_sTmpFile.empty())
147
		remove(_sTmpFile.c_str());
147
		CFileIO::Remove(_sTmpFile);
148
}
148
}
149
 
149
 
150
 
150
 
151
long C_File::uncompressedDataSize() const
151
long C_File::uncompressedDataSize() const
152
{
152
{
Line 160... Line 160...
160
/*
160
/*
161
	Func:	getDirectory()
161
	Func:	getDirectory()
162
	Return: Directory String
162
	Return: Directory String
163
	Desc:	Returns the directory the file goes into, based on m_sDir and Filetype
163
	Desc:	Returns the directory the file goes into, based on m_sDir and Filetype
164
*/
164
*/
165
Utils::String C_File::getDirectory(CBaseFile *file) const
165
Utils::WString C_File::getDirectory(CBaseFile *file) const
166
{
166
{
167
	if (IsFakePatch())
167
	if (IsFakePatch())
168
		return "";
168
		return L"";
169
 
169
 
170
	Utils::String sDir = _sDir;
170
	Utils::WString sDir = _sDir;
171
 
171
 
172
	if ((m_iFileType == FILETYPE_MOD) && (sDir == "Patch"))
172
	if ((m_iFileType == FILETYPE_MOD) && (sDir == L"Patch"))
173
		return "PluginManager/Patch";
173
		return L"PluginManager/Patch";
174
 
174
 
175
	if ((!sDir.empty()) && (m_iFileType != FILETYPE_README) && sDir != ".")
175
	if ((!sDir.empty()) && (m_iFileType != FILETYPE_README) && sDir != L".")
176
	{
176
	{
177
		Utils::String dir = sDir.findReplace("\\", "/");
177
		Utils::WString dir = sDir.findReplace(L"\\", L"/");
178
		if (file)
178
		if (file)
179
		{
179
		{
180
			dir = dir.findReplace("$scriptname", file->getNameValidFile());
180
			dir = dir.findReplace(L"$scriptname", file->getNameValidFile());
181
			dir = dir.findReplace("$scriptauthor", file->author());
181
			dir = dir.findReplace(L"$scriptauthor", file->author());
182
		}
182
		}
183
		return dir;
183
		return dir;
184
	}
184
	}
185
 
185
 
186
	return C_File::GetDirectory(m_iFileType, _sName, file);
186
	return C_File::GetDirectory(m_iFileType, _sName, file);
187
}
187
}
188
 
188
 
189
Utils::String C_File::getNameDirectory(CBaseFile *file) const
189
Utils::WString C_File::getNameDirectory(CBaseFile *file) const
190
{
190
{
191
	Utils::String dir = getDirectory(file);
191
	Utils::WString dir = getDirectory(file);
192
	if (!dir.empty())
192
	if (!dir.empty())
193
		dir += "/";
193
		dir += L"/";
194
	return (dir + _sName).findReplace("\\", "/").findReplace("//", "/");
194
	return (dir + _sName).findReplace(L"\\", L"/").findReplace(L"//", L"/");
195
}
195
}
196
 
196
 
197
/*
197
/*
198
	Func:	Reset()
198
	Func:	Reset()
199
	Desc:	Resets the file data, clears all data inside
199
	Desc:	Resets the file data, clears all data inside
Line 245... Line 245...
245
			m_bSigned = false;
245
			m_bSigned = false;
246
			break;
246
			break;
247
 
247
 
248
		// extra files are a special case
248
		// extra files are a special case
249
		case FILETYPE_EXTRA:
249
		case FILETYPE_EXTRA:
250
			if ( this->dir().left(6).Compare("extras") )
250
			if ( this->dir().left(6).Compare(L"extras") )
251
				m_bSigned = true;
251
				m_bSigned = true;
252
			else
252
			else
253
				m_bSigned = false;
253
				m_bSigned = false;
254
			break;
254
			break;
255
 
255
 
Line 287... Line 287...
287
	}
287
	}
288
	m_bUsedMalloc = false;
288
	m_bUsedMalloc = false;
289
	m_iDataCompression = SPKCOMPRESS_NONE;
289
	m_iDataCompression = SPKCOMPRESS_NONE;
290
}
290
}
291
 
291
 
292
int C_File::textFileID(const Utils::String &sFilename) const
292
int C_File::textFileID(const Utils::WString &sFilename) const
293
{
293
{
294
	if ( m_iFileType != FILETYPE_TEXT )
294
	if ( m_iFileType != FILETYPE_TEXT )
295
		return -1;
295
		return -1;
296
 
296
 
297
	Utils::String filename = sFilename;
297
	Utils::WString filename = sFilename;
298
 
298
 
299
	if ( filename.empty() )
299
	if ( filename.empty() )
300
		filename = _sName;
300
		filename = _sName;
301
 
301
 
302
	Utils::String textid;
302
	Utils::WString textid;
303
	if ( filename.contains(";-L") || filename.contains("-l") )
303
	if ( filename.contains(L";-L") || filename.contains(L"-l") )
304
		textid = filename.token("-", 1);
304
		textid = filename.token(L"-", 1);
305
	else
305
	else
306
		textid = filename.tokens(".", -1).right(4);
306
		textid = filename.tokens(L".", -1).right(4);
307
 
307
 
308
	if (textid.isNumber()) return textid.toInt();
308
	if (textid.isNumber()) return textid.toInt();
309
	return -1;
309
	return -1;
310
}
310
}
311
bool C_File::isAutoTextFile ()
311
bool C_File::isAutoTextFile ()
Line 345... Line 345...
345
bool C_File::IsFakePatch() const
345
bool C_File::IsFakePatch() const
346
{
346
{
347
	if (m_iFileType != FILETYPE_MOD)
347
	if (m_iFileType != FILETYPE_MOD)
348
		return false;
348
		return false;
349
 
349
 
350
	Utils::String name = _sName;
350
	Utils::WString name = _sName;
351
 
351
 
352
	if (name.token(".", 1).toLong())
352
	if (name.token(L".", 1).toLong())
353
		return true;
353
		return true;
354
 
354
 
355
	if (name.left(10) == "FakePatch_")
355
	if (name.left(10) == L"FakePatch_")
356
		return true;
356
		return true;
357
 
357
 
358
	return false;
358
	return false;
359
}
359
}
360
 
360
 
Line 370... Line 370...
370
	Desc:	Sets the file pointer
370
	Desc:	Sets the file pointer
371
			Reads the file size and last modifed time to store in the class
371
			Reads the file size and last modifed time to store in the class
372
			Splits up the filename and dir path
372
			Splits up the filename and dir path
373
*/
373
*/
374
 
374
 
375
void C_File::setFilename(const Utils::String &filename)
375
void C_File::setFilename(const Utils::WString &filename)
376
{
376
{
377
	Utils::String file = filename.findReplace ( "\\", "/" ).findReplace ( "//", "/" );
377
	Utils::WString file = filename.findReplace ( L"\\", L"/" ).findReplace ( L"//", L"/" );
378
 
378
 
379
	_sFullDir = file.tokens("/", 1, -2);
379
	_sFullDir = file.tokens(L"/", 1, -2);
380
	_sName = file.token("/", -1);
380
	_sName = file.token(L"/", -1);
381
	if ( _sFullDir.right(2) == "/." )
381
	if ( _sFullDir.right(2) == L"/." )
382
		_sFullDir = _sFullDir.left(-2);
382
		_sFullDir = _sFullDir.left(-2);
383
 
383
 
384
	ReadFileSize ();
384
	ReadFileSize ();
385
 
385
 
386
	ReadLastModified ();
386
	ReadLastModified ();
Line 396... Line 396...
396
bool C_File::ReadFromFile ()
396
bool C_File::ReadFromFile ()
397
{
397
{
398
	return this->readFromFile(filePointer());
398
	return this->readFromFile(filePointer());
399
}
399
}
400
 
400
 
401
bool C_File::readFromFile(const Utils::String filename)
401
bool C_File::readFromFile(const Utils::WString filename)
402
{
402
{
403
	CFileIO File(filename);
403
	CFileIO File(filename);
404
	if ( !File.startRead() ) {
404
	if ( !File.startRead() ) {
405
		m_iLastError = SPKERR_FILEOPEN;
405
		m_iLastError = SPKERR_FILEOPEN;
406
		return false;
406
		return false;
Line 496... Line 496...
496
	Func:	filePointer
496
	Func:	filePointer
497
	Desc:	Returns the file pointer name
497
	Desc:	Returns the file pointer name
498
			Joins dir and name together
498
			Joins dir and name together
499
			Works for relative paths as well
499
			Works for relative paths as well
500
*/
500
*/
501
Utils::String C_File::filePointer() const
501
Utils::WString C_File::filePointer() const
502
{
502
{
503
	Utils::String fullfile = _sFullDir;
503
	Utils::WString fullfile = _sFullDir;
504
	if ( !fullfile.empty() )
504
	if ( !fullfile.empty() )
505
		fullfile += "/";
505
		fullfile += L"/";
506
 
506
 
507
	if ( !_sName.empty() )
507
	if ( !_sName.empty() )
508
		fullfile += _sName;
508
		fullfile += _sName;
509
 
509
 
510
	return fullfile;
510
	return fullfile;
Line 512... Line 512...
512
 
512
 
513
bool C_File::isExternalFile() const
513
bool C_File::isExternalFile() const
514
{
514
{
515
	if (!m_sData && !m_lDataSize)
515
	if (!m_sData && !m_lDataSize)
516
	{
516
	{
517
		Utils::String file = this->filePointer();
517
		Utils::WString file = this->filePointer();
518
		if (!file.empty() && CFileIO::Exists(file))
518
		if (!file.empty() && CFileIO::Exists(file))
519
			return true;
519
			return true;
520
	}
520
	}
521
	return false;
521
	return false;
522
}
522
}
523
 
523
 
524
 
524
 
525
 
525
 
526
void C_File::updateSignature()
526
void C_File::updateSignature()
527
{
527
{
528
	_sSignature = "";
528
	_sSignature = L"";
529
 
529
 
530
	bool deleteData = false;
530
	bool deleteData = false;
531
	if ( !m_sData )
531
	if ( !m_sData )
532
	{
532
	{
533
		if ( !ReadFromFile() )
533
		if ( !ReadFromFile() )
Line 546... Line 546...
546
	size_t fPos = m_lDataSize;
546
	size_t fPos = m_lDataSize;
547
	if ( fPos > 700 )
547
	if ( fPos > 700 )
548
		fPos = 700;
548
		fPos = 700;
549
	unsigned char *data = m_sData + (m_lDataSize - fPos);
549
	unsigned char *data = m_sData + (m_lDataSize - fPos);
550
	data[fPos - 1] = '\0';
550
	data[fPos - 1] = '\0';
551
	Utils::String sData ((char *)data);
551
	Utils::WString sData ((char *)data);
552
	int pos = sData.findPos("</codearray>", 0);
552
	int pos = sData.findPos(L"</codearray>", 0);
553
	if ( pos != -1 )
553
	if ( pos != -1 )
554
	{
554
	{
555
		sData = sData.right(sData.length() - pos);
555
		sData = sData.right(sData.length() - pos);
556
		pos = sData.findPos("<signature>", 0);
556
		pos = sData.findPos(L"<signature>", 0);
557
		int endpos = sData.findPos("</signature>", 0);
557
		int endpos = sData.findPos(L"</signature>", 0);
558
		if ( pos != -1 && endpos != -1 )
558
		if ( pos != -1 && endpos != -1 )
559
		{
559
		{
560
			_sSignature = sData.mid(pos + 12, endpos - (pos + 12) + 1);
560
			_sSignature = sData.mid(pos + 12, endpos - (pos + 12) + 1);
561
			_sSignature = _sSignature.remove('\n').remove('\r');
561
			_sSignature = _sSignature.remove('\n').remove('\r');
562
		}
562
		}
Line 587... Line 587...
587
	Desc:	Reads the last modified time of the file and returns
587
	Desc:	Reads the last modified time of the file and returns
588
			Uses seperate rountines for Windows and Linux
588
			Uses seperate rountines for Windows and Linux
589
*/
589
*/
590
time_t C_File::ReadLastModified ()
590
time_t C_File::ReadLastModified ()
591
{
591
{
592
	Utils::String file = filePointer();
592
	Utils::WString file = filePointer();
593
	if ( file.empty() )
593
	if ( file.empty() )
594
		return m_tTime;
594
		return m_tTime;
595
 
595
 
596
	#ifndef _WIN32
596
	#ifndef _WIN32
597
	struct stat attrib;			// create a file attribute structure
597
	struct stat attrib;			// create a file attribute structure
Line 612... Line 612...
612
{
612
{
613
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
613
	if ( (m_iFileType != FILETYPE_SCRIPT) && (m_iFileType != FILETYPE_UNINSTALL) )
614
		return false;
614
		return false;
615
 
615
 
616
	// check file pointer
616
	// check file pointer
617
	Utils::String file = filePointer();
617
	Utils::WString file = filePointer();
618
	Utils::WString ext = CFileIO(file).extension();
618
	Utils::WString ext = CFileIO(file).extension();
619
	if ( !ext.Compare(L"xml") && !ext.Compare(L"pck") )
619
	if ( !ext.Compare(L"xml") && !ext.Compare(L"pck") )
620
		return false;
620
		return false;
621
 
621
 
622
	if ( m_iDataCompression != SPKCOMPRESS_NONE )
622
	if ( m_iDataCompression != SPKCOMPRESS_NONE )
Line 703... Line 703...
703
########################################################################################################################
703
########################################################################################################################
704
*/
704
*/
705
 
705
 
706
bool C_File::CompressFile ( CProgressInfo *progress )
706
bool C_File::CompressFile ( CProgressInfo *progress )
707
{
707
{
708
	Utils::String file = this->filePointer();
708
	Utils::WString file = this->filePointer();
709
	if ( !CFileIO(this->filePointer()).exists() )
709
	if ( !CFileIO(file).exists() )
710
	{
710
	{
711
		if ( !this->writeToFile("tempuncompr.dat", m_sData, m_lDataSize) )
711
		if ( !this->writeToFile("tempuncompr.dat", m_sData, m_lDataSize) )
712
			return false;
712
			return false;
713
		file = "tempuncompr.dat";
713
		file = "tempuncompr.dat";
714
	}
714
	}
715
 
715
 
716
	bool ret = false;
716
	bool ret = false;
717
 
717
 
718
	FILE *fIn = fopen(file.c_str(), "rb");
718
	FILE *fIn = fopen(std::string(file.begin(), file.end()).c_str(), "rb");
719
	if ( fIn )
719
	if ( fIn )
720
	{
720
	{
721
		FILE *fOut = fopen("tempcompr.dat", "wb");
721
		FILE *fOut = fopen("tempcompr.dat", "wb");
722
		if ( fOut )
722
		if ( fOut )
723
		{
723
		{
Line 730... Line 730...
730
			fclose(fOut);
730
			fclose(fOut);
731
			if ( err == Z_OK )
731
			if ( err == Z_OK )
732
			{
732
			{
733
				DeleteData ();
733
				DeleteData ();
734
 
734
 
735
				CFileIO File("tempcompr.dat");
735
				CFileIO File(L"tempcompr.dat");
736
				File.setAutoDelete(true);
736
				File.setAutoDelete(true);
737
				if ( File.startRead() ) {
737
				if ( File.startRead() ) {
738
					m_sData = File.readAll((size_t *)&m_lDataSize);
738
					m_sData = File.readAll((size_t *)&m_lDataSize);
739
					if ( !m_sData ) {
739
					if ( !m_sData ) {
740
						m_iLastError = SPKERR_FILEREAD;
740
						m_iLastError = SPKERR_FILEREAD;
Line 750... Line 750...
750
			}
750
			}
751
		}
751
		}
752
		fclose(fIn);
752
		fclose(fIn);
753
	}
753
	}
754
 
754
 
755
	CFileIO::Remove("tempuncompr.dat");
755
	CFileIO::Remove(L"tempuncompr.dat");
756
 
756
 
757
	return ret;
757
	return ret;
758
}
758
}
759
 
759
 
760
bool C_File::ChangeCompression ( int compressionType, CProgressInfo *progress )
760
bool C_File::ChangeCompression ( int compressionType, CProgressInfo *progress )
Line 853... Line 853...
853
 
853
 
854
	if ( compressionType == SPKCOMPRESS_7ZIP )
854
	if ( compressionType == SPKCOMPRESS_7ZIP )
855
		compressionType = SPKCOMPRESS_LZMA;
855
		compressionType = SPKCOMPRESS_LZMA;
856
 
856
 
857
	// if its zlib, and we are trying to compress pcked files (ie already zlib compression) then switch to lzma instead
857
	// if its zlib, and we are trying to compress pcked files (ie already zlib compression) then switch to lzma instead
858
	if ( compressionType == SPKCOMPRESS_ZLIB && (this->checkFileExt("pck") || this->checkFileExt("cat") || this->checkFileExt("dat") || this->checkFileExt("pbb") || this->checkFileExt("pbd")) )
858
	if ( compressionType == SPKCOMPRESS_ZLIB && (this->checkFileExt(L"pck") || this->checkFileExt(L"cat") || this->checkFileExt(L"dat") || this->checkFileExt(L"pbb") || this->checkFileExt(L"pbd")) )
859
		compressionType = SPKCOMPRESS_LZMA;
859
		compressionType = SPKCOMPRESS_LZMA;
860
 
860
 
861
	// if its already compressed, no need to compress again
861
	// if its already compressed, no need to compress again
862
	if ( compressionType == m_iDataCompression )
862
	if ( compressionType == m_iDataCompression )
863
		return true;
863
		return true;
Line 1063... Line 1063...
1063
	}
1063
	}
1064
 
1064
 
1065
	return NULL;
1065
	return NULL;
1066
}
1066
}
1067
 
1067
 
1068
bool C_File::uncompressToFile(const Utils::String &toFile, CBaseFile *spkfile, bool includedir, CProgressInfo *progress)
1068
bool C_File::uncompressToFile(const Utils::WString &toFile, CBaseFile *spkfile, bool includedir, CProgressInfo *progress)
1069
{
1069
{
1070
#ifdef _INCLUDE7ZIP
1070
#ifdef _INCLUDE7ZIP
1071
	if ( (!m_sData) || (!m_lDataSize) )
1071
	if ( (!m_sData) || (!m_lDataSize) )
1072
		return false;
1072
		return false;
1073
	// if theres a tmp file, open it and check it still exists
1073
	// if theres a tmp file, open it and check it still exists
Line 1109... Line 1109...
1109
 
1109
 
1110
bool C_File::writeFilePointer(unsigned char *cData, long len)
1110
bool C_File::writeFilePointer(unsigned char *cData, long len)
1111
{
1111
{
1112
	return writeToFile(filePointer(), cData, len);
1112
	return writeToFile(filePointer(), cData, len);
1113
}
1113
}
1114
bool C_File::writeToFile(const Utils::String &filename, unsigned char *cData, long len)
1114
bool C_File::writeToFile(const Utils::WString &filename, unsigned char *cData, long len)
1115
{
1115
{
1116
	unsigned char *data = cData;
1116
	unsigned char *data = cData;
1117
	if ( (!len) || (!data) ) {
1117
	if ( (!len) || (!data) ) {
1118
		len = m_lDataSize;
1118
		len = m_lDataSize;
1119
		data = m_sData;
1119
		data = m_sData;
Line 1124... Line 1124...
1124
	}
1124
	}
1125
 
1125
 
1126
	bool ret = false;
1126
	bool ret = false;
1127
 
1127
 
1128
	// check for cat file
1128
	// check for cat file
1129
	if ( filename.contains("::")) {
1129
	if ( filename.contains(L"::")) {
1130
		Utils::String catfile = filename.token("::", 1);
1130
		Utils::WString catfile = filename.token(L"::", 1);
1131
		Utils::String file = filename.token( "::", 2);
1131
		Utils::WString file = filename.token(L"::", 2);
1132
 
1132
 
1133
		CCatFile newcat;
1133
		CCatFile newcat;
1134
		return newcat.addData(catfile, data, len, file, true, true);
1134
		return newcat.addData(catfile.toString(), data, len, file.toString(), true, true);
1135
	}
1135
	}
1136
	else {
1136
	else {
1137
		Utils::String filen = filename.findReplace ( ";/";, "\\" );
1137
		Utils::WString filen = filename.findReplace(L";/";, L"\\");
1138
		filen = filen.findReplace ( ";\\\\";, "\\" );
1138
		filen = filen.findReplace(L";\\\\";, L"\\");
1139
 
1139
 
1140
		if ( len && data ) {
1140
		if ( len && data ) {
1141
			CFileIO File(filen);
1141
			CFileIO File(filen);
1142
			if ( File.startWrite() ) ret = File.write(data, len);
1142
			if ( File.startWrite() ) ret = File.write(data, len);
1143
		}
1143
		}
1144
	}
1144
	}
1145
 
1145
 
1146
	return ret;
1146
	return ret;
1147
}
1147
}
1148
 
1148
 
1149
bool C_File::writeToDir(const Utils::String &dir, CBaseFile *spkfile, bool includedir, const Utils::String &appendDir, unsigned char *data, long len)
1149
bool C_File::writeToDir(const Utils::WString &dir, CBaseFile *spkfile, bool includedir, const Utils::WString &appendDir, unsigned char *data, long len)
1150
{
1150
{
1151
	Utils::String fullfile = _getFullFileToDir(dir, includedir, spkfile);
1151
	Utils::WString fullfile = _getFullFileToDir(dir, includedir, spkfile);
1152
 
1152
 
1153
	if (!appendDir.empty())
1153
	if (!appendDir.empty())
1154
	{
1154
	{
1155
		if (!fullfile.empty())
1155
		if (!fullfile.empty())
1156
			fullfile += "/";
1156
			fullfile += L"/";
1157
		fullfile += appendDir;
1157
		fullfile += appendDir;
1158
	}
1158
	}
1159
 
1159
 
1160
	Utils::String fulldir = fullfile.tokens("/", 1, -2);
1160
	Utils::WString fulldir = fullfile.tokens(L"/", 1, -2);
1161
	if (!fulldir.empty())
1161
	if (!fulldir.empty())
1162
	{
1162
	{
1163
		if (!CDirIO(fulldir).create())
1163
		if (!CDirIO(fulldir).create())
1164
			return false;
1164
			return false;
1165
	}
1165
	}
1166
 
1166
 
1167
	return writeToFile(fullfile, data, len);
1167
	return writeToFile(fullfile, data, len);
1168
}
1168
}
1169
 
1169
 
1170
Utils::String C_File::dataSizeString() const
1170
Utils::WString C_File::dataSizeString() const
1171
{
1171
{
1172
	return SPK::GetSizeString(m_lDataSize);
1172
	return SPK::GetSizeString(m_lDataSize);
1173
}
1173
}
1174
Utils::String C_File::uncompressedSizeString() const
1174
Utils::WString C_File::uncompressedSizeString() const
1175
{
1175
{
1176
	return SPK::GetSizeString(uncompressedDataSize());
1176
	return SPK::GetSizeString(uncompressedDataSize());
1177
}
1177
}
1178
 
1178
 
1179
Utils::String C_File::creationTimeString() const
1179
Utils::WString C_File::creationTimeString() const
1180
{
1180
{
1181
	if ( !m_tTime )
1181
	if ( !m_tTime )
1182
		return Utils::String::Null();
1182
		return Utils::String::Null();
1183
 
1183
 
1184
	struct tm   *currDate;
1184
	struct tm   *currDate;
Line 1188... Line 1188...
1188
 
1188
 
1189
	currDate = localtime(&n);
1189
	currDate = localtime(&n);
1190
 
1190
 
1191
	strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
1191
	strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
1192
 
1192
 
1193
	return Utils::String(dateString);
1193
	return Utils::WString(dateString);
1194
}
1194
}
1195
 
1195
 
1196
bool C_File::CompareNew ( C_File *file )
1196
bool C_File::CompareNew ( C_File *file )
1197
{
1197
{
1198
	if ( !m_iVersion )
1198
	if ( !m_iVersion )
Line 1214... Line 1214...
1214
 
1214
 
1215
	// assume same or newer
1215
	// assume same or newer
1216
	return true;
1216
	return true;
1217
}
1217
}
1218
 
1218
 
1219
Utils::String GetFileTypeString(int type)
1219
Utils::WString GetFileTypeString(int type)
1220
{
1220
{
1221
	switch ( type )
1221
	switch ( type )
1222
	{
1222
	{
1223
		case FILETYPE_SCRIPT:
1223
		case FILETYPE_SCRIPT:
1224
			return "Script";
1224
			return L"Script";
1225
		case FILETYPE_TEXT:
1225
		case FILETYPE_TEXT:
1226
			return "Text";
1226
			return L"Text";
1227
		case FILETYPE_README:
1227
		case FILETYPE_README:
1228
			return "Readme";
1228
			return L"Readme";
1229
		case FILETYPE_MAP:
1229
		case FILETYPE_MAP:
1230
			return "Map";
1230
			return L"Map";
1231
		case FILETYPE_MOD:
1231
		case FILETYPE_MOD:
1232
			return "Mod";
1232
			return L"Mod";
1233
		case FILETYPE_UNINSTALL:
1233
		case FILETYPE_UNINSTALL:
1234
			return "Uninstall";
1234
			return L"Uninstall";
1235
		case FILETYPE_SOUND:
1235
		case FILETYPE_SOUND:
1236
			return "Sound";
1236
			return L"Sound";
1237
		case FILETYPE_EXTRA:
1237
		case FILETYPE_EXTRA:
1238
			return "Extra";
1238
			return L"Extra";
1239
		case FILETYPE_SCREEN:
1239
		case FILETYPE_SCREEN:
1240
			return "Screen";
1240
			return L"Screen";
1241
		case FILETYPE_ADVERT:
1241
		case FILETYPE_ADVERT:
1242
			return "Advert";
1242
			return L"Advert";
1243
		case FILETYPE_MISSION:
1243
		case FILETYPE_MISSION:
1244
			return "Mission";
1244
			return L"Mission";
1245
		case FILETYPE_BACKUP:
1245
		case FILETYPE_BACKUP:
1246
			return "Backup";
1246
			return L"Backup";
1247
		case FILETYPE_SHIPOTHER:
1247
		case FILETYPE_SHIPOTHER:
1248
			return "ShipOther";
1248
			return L"ShipOther";
1249
		case FILETYPE_SHIPMODEL:
1249
		case FILETYPE_SHIPMODEL:
1250
			return "ShipModel";
1250
			return L"ShipModel";
1251
		case FILETYPE_SHIPSCENE:
1251
		case FILETYPE_SHIPSCENE:
1252
			return "ShipScene";
1252
			return L"ShipScene";
1253
		case FILETYPE_COCKPITSCENE:
1253
		case FILETYPE_COCKPITSCENE:
1254
			return "CockpitScene";
1254
			return L"CockpitScene";
1255
	}
1255
	}
1256
 
1256
 
1257
	return Utils::String::Null();
1257
	return Utils::WString::Null();
1258
}
1258
}
1259
 
1259
 
1260
FileType GetFileTypeFromString(const Utils::String &type)
1260
FileType GetFileTypeFromString(const Utils::WString &type)
1261
{
1261
{
1262
	Utils::String ltype = type.lower();
1262
	Utils::WString ltype = type.lower();
1263
	if ( ltype == "script" || ltype == "scriptpck")
1263
	if ( ltype == L"script" || ltype == L"scriptpck")
1264
		return FILETYPE_SCRIPT;
1264
		return FILETYPE_SCRIPT;
1265
	else if ( ltype == "text" )
1265
	else if ( ltype == L"text" )
1266
		return FILETYPE_TEXT;
1266
		return FILETYPE_TEXT;
1267
	else if ( ltype == "readme" )
1267
	else if ( ltype == L"readme" )
1268
		return FILETYPE_README;
1268
		return FILETYPE_README;
1269
	else if ( ltype == "map" )
1269
	else if ( ltype == L"map" )
1270
		return FILETYPE_MAP;
1270
		return FILETYPE_MAP;
1271
	else if ( ltype == "mod" )
1271
	else if ( ltype == L"mod" )
1272
		return FILETYPE_MOD;
1272
		return FILETYPE_MOD;
1273
	else if ( ltype == "uninstall" )
1273
	else if ( ltype == L"uninstall" )
1274
		return FILETYPE_UNINSTALL;
1274
		return FILETYPE_UNINSTALL;
1275
	else if ( ltype == "sound" )
1275
	else if ( ltype == L"sound" )
1276
		return FILETYPE_SOUND;
1276
		return FILETYPE_SOUND;
1277
	else if ( ltype == "extra" )
1277
	else if ( ltype == L"extra" )
1278
		return FILETYPE_EXTRA;
1278
		return FILETYPE_EXTRA;
1279
	else if ( ltype == "screen" )
1279
	else if ( ltype == L"screen" )
1280
		return FILETYPE_SCREEN;
1280
		return FILETYPE_SCREEN;
1281
	else if ( ltype == "advert" )
1281
	else if ( ltype == L"advert" )
1282
		return FILETYPE_ADVERT;
1282
		return FILETYPE_ADVERT;
1283
	else if ( ltype == "mission" )
1283
	else if ( ltype == L"mission" )
1284
		return FILETYPE_MISSION;
1284
		return FILETYPE_MISSION;
1285
	else if ( ltype == "backup" )
1285
	else if ( ltype == L"backup" )
1286
		return FILETYPE_BACKUP;
1286
		return FILETYPE_BACKUP;
1287
	else if ( ltype == "shipother" )
1287
	else if ( ltype == L"shipother" )
1288
		return FILETYPE_SHIPOTHER;
1288
		return FILETYPE_SHIPOTHER;
1289
	else if ( ltype == "shipmodel" )
1289
	else if ( ltype == L"shipmodel" )
1290
		return FILETYPE_SHIPMODEL;
1290
		return FILETYPE_SHIPMODEL;
1291
	else if ( ltype == "shipscene" )
1291
	else if ( ltype == L"shipscene" )
1292
		return FILETYPE_SHIPSCENE;
1292
		return FILETYPE_SHIPSCENE;
1293
	else if ( ltype == "cockpitscene" )
1293
	else if ( ltype == L"cockpitscene" )
1294
		return FILETYPE_COCKPITSCENE;
1294
		return FILETYPE_COCKPITSCENE;
1295
 
1295
 
1296
	return FILETYPE_UNKNOWN;
1296
	return FILETYPE_UNKNOWN;
1297
}
1297
}
1298
 
1298
 
Line 1412... Line 1412...
1412
bool C_File::CheckPCK ()
1412
bool C_File::CheckPCK ()
1413
{
1413
{
1414
	if ( (m_sData) && (m_lDataSize) && (m_iDataCompression == SPKCOMPRESS_NONE) )
1414
	if ( (m_sData) && (m_lDataSize) && (m_iDataCompression == SPKCOMPRESS_NONE) )
1415
		return IsDataPCK ( m_sData, m_lDataSize );
1415
		return IsDataPCK ( m_sData, m_lDataSize );
1416
 
1416
 
1417
	Utils::String filename = filePointer();
1417
	Utils::WString filename = filePointer();
1418
	if ( !filename.empty() ) {
1418
	if ( !filename.empty() ) {
1419
		CFileIO File(filename);
1419
		CFileIO File(filename);
1420
		if ( File.startRead() ) {
1420
		if ( File.startRead() ) {
1421
			unsigned char data[4];
1421
			unsigned char data[4];
1422
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
1422
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
1423
		}
1423
		}
1424
	}
1424
	}
1425
 
1425
 
1426
	if (checkFileExt("pck") || checkFileExt("pbb") || checkFileExt("pbd")) return true;
1426
	if (checkFileExt(L"pck") || checkFileExt(L"pbb") || checkFileExt(L"pbd")) return true;
1427
	return false;
1427
	return false;
1428
}
1428
}
1429
 
1429
 
1430
bool C_File::PCKFile()
1430
bool C_File::PCKFile()
1431
{
1431
{
Line 1724... Line 1724...
1724
	}
1724
	}
1725
 
1725
 
1726
	return iVersion;
1726
	return iVersion;
1727
}
1727
}
1728
 
1728
 
1729
Utils::String C_File::baseName() const
1729
Utils::WString C_File::baseName() const
1730
{
1730
{
1731
	// remove any directory
1731
	// remove any directory
1732
	Utils::String file = _sName.token("/", -1);
1732
	Utils::WString file = _sName.token(L"/", -1);
1733
 
1733
 
1734
	// remove file extension
1734
	// remove file extension
1735
	return file.remToken(".", -1);
1735
	return file.remToken(L".", -1);
1736
}
1736
}
1737
 
1737
 
1738
void C_File::CopyData(C_File *oldFile, bool includeData)
1738
void C_File::CopyData(C_File *oldFile, bool includeData)
1739
{
1739
{
1740
	setFilename(oldFile->fullFilename());
1740
	setFilename(oldFile->fullFilename());
Line 1762... Line 1762...
1762
}
1762
}
1763
 
1763
 
1764
float GetLibraryVersion() { return (float)LIBRARYVERSION; }
1764
float GetLibraryVersion() { return (float)LIBRARYVERSION; }
1765
float GetFileFormatVersion() { return (float)FILEVERSION; }
1765
float GetFileFormatVersion() { return (float)FILEVERSION; }
1766
 
1766
 
1767
Utils::String C_File::fileExt() const 
1767
Utils::WString C_File::fileExt() const 
1768
{ 
1768
{ 
1769
	return _sName.token(".", -1); 
1769
	return _sName.token(L".", -1); 
1770
}
1770
}
1771
 
1771
 
1772
bool C_File::checkFileExt(const Utils::String& ext) 
1772
bool C_File::checkFileExt(const Utils::WString& ext) 
1773
{
1773
{
1774
	if (fileExt().Compare(ext)) return true; return false;
1774
	if (fileExt().Compare(ext)) return true; return false;
1775
}
1775
}
1776
 
1776
 
1777
const Utils::String &C_File::changeFileExt(const Utils::String &ext)
1777
const Utils::WString &C_File::changeFileExt(const Utils::WString &ext)
1778
{
1778
{
1779
	_sName = CFileIO(_sName).changeFileExtension(ext).toString();
1779
	_sName = CFileIO(_sName).changeFileExtension(ext);
1780
	return _sName;
1780
	return _sName;
1781
}
1781
}
1782
 
1782
 
1783
bool C_File::CheckPackedExtension()
1783
bool C_File::CheckPackedExtension()
1784
{
1784
{
1785
	Utils::String ext = this->fileExt();
1785
	Utils::WString ext = this->fileExt();
1786
	if ( ext == "pck" )
1786
	if ( ext == L"pck" )
1787
		return true;
1787
		return true;
1788
	else if ( ext == "pbb" )
1788
	else if ( ext == L"pbb" )
1789
		return true;
1789
		return true;
1790
	else if ( ext == "pbd" )
1790
	else if ( ext == L"pbd" )
1791
		return true;
1791
		return true;
1792
 
1792
 
1793
	return false;
1793
	return false;
1794
}
1794
}
1795
 
1795
 
Line 1843... Line 1843...
1843
 
1843
 
1844
unsigned char *C_File::BobDecompile(size_t *size)
1844
unsigned char *C_File::BobDecompile(size_t *size)
1845
{
1845
{
1846
	(*size) = 0;
1846
	(*size) = 0;
1847
 
1847
 
1848
	Utils::String fromFile = this->filePointer();
1848
	Utils::WString fromFile = this->filePointer();
1849
	if ( !CFileIO::Exists(fromFile) ) {
1849
	if ( !CFileIO::Exists(fromFile) ) {
1850
		if ( this->writeToFile(CPackages::tempDirectory() + "bob.tmp") ) {
1850
		if ( this->writeToFile(CPackages::tempDirectory().toString() + "bob.tmp")) {
1851
			fromFile = CPackages::tempDirectory() + "bob.tmp";
1851
			fromFile = CPackages::tempDirectory() + L"bob.tmp";
1852
		}
1852
		}
1853
	}
1853
	}
1854
 
1854
 
1855
	fromFile = fromFile.findReplace("/", "\\");
1855
	fromFile = fromFile.findReplace(L"/", L"\\");
1856
	Utils::String toFile = CPackages::tempDirectory() + "bod.tmp";
1856
	Utils::WString toFile = CPackages::tempDirectory() + L"bod.tmp";
1857
	toFile = toFile.findReplace("/", "\\");
1857
	toFile = toFile.findReplace("/", "\\");
1858
 
1858
 
1859
	if ( CFileIO::Exists(fromFile) ) {
1859
	if ( CFileIO::Exists(fromFile) ) {
1860
		if ( X2BC_BOB2BOD(fromFile.c_str(), toFile.c_str()) ) {
1860
		if ( X2BC_BOB2BOD(fromFile.toString().c_str(), toFile.toString().c_str())) {
1861
			CFileIO F(toFile);
1861
			CFileIO F(toFile);
1862
			if ( F.exists() && F.startRead() ) {
1862
			if ( F.exists() && F.startRead() ) {
1863
				unsigned char *data = F.readAll(size);
1863
				unsigned char *data = F.readAll(size);
1864
				F.close();
1864
				F.close();
1865
				F.remove();
1865
				F.remove();
Line 1867... Line 1867...
1867
				return data;
1867
				return data;
1868
			}
1868
			}
1869
		}
1869
		}
1870
	}
1870
	}
1871
 
1871
 
1872
	if ( CFileIO::Exists(CPackages::tempDirectory() + "bob.tmp") )
1872
	if ( CFileIO::Exists(CPackages::tempDirectory() + L"bob.tmp") )
1873
		CFileIO(CPackages::tempDirectory() + ";bob.tmp").remove();
1873
		CFileIO::Remove(CPackages::tempDirectory() + L";bob.tmp");
1874
	/*
1874
	/*
1875
	Settings settings;
1875
	Settings settings;
1876
	bob_dom_document doc(&settings);
1876
	bob_dom_document doc(&settings);
1877
	
1877
	
1878
	ibinaryrealfile is;
1878
	ibinaryrealfile is;
Line 1966... Line 1966...
1966
bool C_File::BodCompile()
1966
bool C_File::BodCompile()
1967
{
1967
{
1968
	return true;
1968
	return true;
1969
}
1969
}
1970
 
1970
 
1971
bool C_File::renameScript(const Utils::String &baseName)
1971
bool C_File::renameScript(const Utils::WString &baseName)
1972
{
1972
{
1973
	if ( !m_sData || !m_lDataSize )
1973
	if ( !m_sData || !m_lDataSize )
1974
	{
1974
	{
1975
		if ( !this->ReadFromFile() )
1975
		if ( !this->ReadFromFile() )
1976
			return false;
1976
			return false;
Line 1979... Line 1979...
1979
	// uncompress the file
1979
	// uncompress the file
1980
	if ( !this->UncompressData() )
1980
	if ( !this->UncompressData() )
1981
		return false;
1981
		return false;
1982
 
1982
 
1983
	// un pck the file
1983
	// un pck the file
1984
	if (this->checkFileExt("pck"))
1984
	if (this->checkFileExt(L"pck"))
1985
	{
1985
	{
1986
		if ( !this->UnPCKFile() )
1986
		if ( !this->UnPCKFile() )
1987
			return false;
1987
			return false;
1988
	}
1988
	}
1989
 
1989
 
1990
	// now we should have the raw data
1990
	// now we should have the raw data
1991
	Utils::String data((const char *)m_sData);
1991
	Utils::WString data((const char *)m_sData);
1992
	data.truncate(m_lDataSize);
1992
	data.truncate(m_lDataSize);
1993
	data = data.findReplace(this->baseName(), baseName);
1993
	data = data.findReplace(this->baseName(), baseName);
1994
 
1994
 
1995
	this->DeleteData();
1995
	this->DeleteData();
1996
	m_sData = new unsigned char[data.length()];
1996
	m_sData = new unsigned char[data.length()];
1997
	memcpy(m_sData, data.c_str(), data.length());
1997
	memcpy(m_sData, data.c_str(), data.length());
1998
	m_lDataSize = (long)data.length();
1998
	m_lDataSize = (long)data.length();
1999
 
1999
 
2000
	// repck the file
2000
	// repck the file
2001
	if (this->checkFileExt("pck"))
2001
	if (this->checkFileExt(L"pck"))
2002
	{
2002
	{
2003
		if ( !this->PCKFile() )
2003
		if ( !this->PCKFile() )
2004
			return false;
2004
			return false;
2005
	}
2005
	}
2006
 
2006
 
2007
 
2007
 
2008
	return true;
2008
	return true;
2009
}
2009
}
2010
 
2010
 
2011
Utils::String C_File::_getFullFileToDir(const Utils::String &dir, bool includedir, CBaseFile *file) const
2011
Utils::WString C_File::_getFullFileToDir(const Utils::WString &dir, bool includedir, CBaseFile *file) const
2012
{
2012
{
2013
	Utils::String fullfile = dir;
2013
	Utils::WString fullfile = dir;
2014
	if (includedir)
2014
	if (includedir)
2015
	{
2015
	{
2016
		Utils::String d = getDirectory(file);
2016
		Utils::WString d = getDirectory(file);
2017
		if (!d.empty())
2017
		if (!d.empty())
2018
		{
2018
		{
2019
			if (!fullfile.empty())
2019
			if (!fullfile.empty())
2020
				fullfile += "/";
2020
				fullfile += L"/";
2021
			fullfile += d;
2021
			fullfile += d;
2022
		}
2022
		}
2023
	}
2023
	}
2024
	if (!_sName.empty())
2024
	if (!_sName.empty())
2025
	{
2025
	{
2026
		if (!fullfile.empty())
2026
		if (!fullfile.empty())
2027
			fullfile += "/";
2027
			fullfile += L"/";
2028
		fullfile += _sName;
2028
		fullfile += _sName;
2029
	}
2029
	}
2030
 
2030
 
2031
	fullfile = fullfile.findReplace("\\", "/");
2031
	fullfile = fullfile.findReplace(L"\\", L"/");
2032
	return fullfile;
2032
	return fullfile;
2033
}
2033
}