Subversion Repositories spk

Rev

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

Rev 160 Rev 178
Line 146... Line 146...
146
	if (!_sTmpFile.empty())
146
	if (!_sTmpFile.empty())
147
		remove(_sTmpFile.c_str());
147
		remove(_sTmpFile.c_str());
148
}
148
}
149
 
149
 
150
 
150
 
-
 
151
long C_File::uncompressedDataSize() const
-
 
152
{
-
 
153
	if (m_lUncomprDataSize)
-
 
154
		return m_lUncomprDataSize;
-
 
155
	if (m_lSize)
-
 
156
		return m_lSize;
-
 
157
	return m_lDataSize;
-
 
158
}
-
 
159
 
151
/*
160
/*
152
	Func:	GetDirectory()
161
	Func:	getDirectory()
153
	Return: Directory String
162
	Return: Directory String
154
	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
155
*/
164
*/
156
CyString C_File::GetDirectory ( CBaseFile *file )
-
 
157
{
-
 
158
	return getDirectory(file);
-
 
159
}
-
 
160
 
-
 
161
Utils::String C_File::getDirectory(CBaseFile *file) const
165
Utils::String C_File::getDirectory(CBaseFile *file) const
162
{
166
{
163
	if (IsFakePatch())
167
	if (IsFakePatch())
164
		return "";
168
		return "";
165
 
169
 
Line 180... Line 184...
180
	}
184
	}
181
 
185
 
182
	return C_File::GetDirectory(m_iFileType, _sName, file);
186
	return C_File::GetDirectory(m_iFileType, _sName, file);
183
}
187
}
184
 
188
 
185
CyString C_File::GetNameDirectory(CBaseFile *file)
-
 
186
{
-
 
187
	return getNameDirectory(file);
-
 
188
}
-
 
189
 
-
 
190
Utils::String C_File::getNameDirectory(CBaseFile *file) const
189
Utils::String C_File::getNameDirectory(CBaseFile *file) const
191
{
190
{
192
	Utils::String dir = getDirectory(file);
191
	Utils::String dir = getDirectory(file);
193
	if (!dir.empty())
192
	if (!dir.empty())
194
		dir += "/";
193
		dir += "/";
Line 246... Line 245...
246
			m_bSigned = false;
245
			m_bSigned = false;
247
			break;
246
			break;
248
 
247
 
249
		// extra files are a special case
248
		// extra files are a special case
250
		case FILETYPE_EXTRA:
249
		case FILETYPE_EXTRA:
251
			if ( this->GetDir().Left(6).Compare("extras") )
250
			if ( this->dir().left(6).Compare("extras") )
252
				m_bSigned = true;
251
				m_bSigned = true;
253
			else
252
			else
254
				m_bSigned = false;
253
				m_bSigned = false;
255
			break;
254
			break;
256
 
255
 
Line 288... Line 287...
288
	}
287
	}
289
	m_bUsedMalloc = false;
288
	m_bUsedMalloc = false;
290
	m_iDataCompression = SPKCOMPRESS_NONE;
289
	m_iDataCompression = SPKCOMPRESS_NONE;
291
}
290
}
292
 
291
 
293
int C_File::GetTextFileID(CyString filename)
292
int C_File::textFileID(const Utils::String &sFilename) const
294
{
293
{
295
	if ( m_iFileType != FILETYPE_TEXT )
294
	if ( m_iFileType != FILETYPE_TEXT )
296
		return -1;
295
		return -1;
297
 
296
 
-
 
297
	Utils::String filename = sFilename;
-
 
298
 
298
	if ( filename.Empty() )
299
	if ( filename.empty() )
299
		filename = _sName;
300
		filename = _sName;
300
 
301
 
301
	CyString textid;
302
	Utils::String textid;
302
	if ( filename.IsIn("-L") || filename.IsIn("-l") )
303
	if ( filename.contains("-L") || filename.contains("-l") )
303
		textid = filename.GetToken("-", 1, 1);
304
		textid = filename.token("-", 1);
304
	else
305
	else
305
		textid = filename.GetToken(".", -1).Right(4);
306
		textid = filename.tokens(".", -1).right(4);
306
 
307
 
307
	if ( textid.IsNumber() ) return textid.ToInt();
308
	if (textid.isNumber()) return textid.toInt();
308
	return -1;
309
	return -1;
309
}
310
}
310
bool C_File::isAutoTextFile ()
311
bool C_File::isAutoTextFile ()
311
{
312
{
312
	int textid = GetTextFileID();
313
	int textid = textFileID();
313
	if ( textid == -1 ) return false;
314
	if ( textid == -1 ) return false;
314
 
315
 
315
	if ( textid == 4 )
316
	if ( textid == 4 )
316
		return true;
317
		return true;
317
 
318
 
318
	// check for original name
319
	// check for original name
319
	if ( !_sOriginalName.empty() )
320
	if ( !_sOriginalName.empty() )
320
	{
321
	{
321
		textid = GetTextFileID(_sOriginalName);
322
		textid = textFileID(_sOriginalName);
322
		if ( textid >= 0 && textid <= 3 )
323
		if ( textid >= 0 && textid <= 3 )
323
			return true;
324
			return true;
324
	}
325
	}
325
 
326
 
326
	return false;
327
	return false;
Line 368... Line 369...
368
	Accept:	filename - String for the filename of disk
369
	Accept:	filename - String for the filename of disk
369
	Desc:	Sets the file pointer
370
	Desc:	Sets the file pointer
370
			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
371
			Splits up the filename and dir path
372
			Splits up the filename and dir path
372
*/
373
*/
373
void C_File::SetFilename(CyString filename)
-
 
374
{
-
 
375
	setFilename(filename.ToString());
-
 
376
}
374
 
377
void C_File::setFilename(const Utils::String &filename)
375
void C_File::setFilename(const Utils::String &filename)
378
{
376
{
379
	Utils::String file = filename.findReplace ( "\\", "/" ).findReplace ( "//", "/" );
377
	Utils::String file = filename.findReplace ( "\\", "/" ).findReplace ( "//", "/" );
380
 
378
 
381
	_sFullDir = file.tokens("/", 1, -2);
379
	_sFullDir = file.tokens("/", 1, -2);
Line 398... Line 396...
398
bool C_File::ReadFromFile ()
396
bool C_File::ReadFromFile ()
399
{
397
{
400
	return this->readFromFile(filePointer());
398
	return this->readFromFile(filePointer());
401
}
399
}
402
 
400
 
403
bool C_File::ReadFromFile(CyString filename)
-
 
404
{
-
 
405
	return readFromFile(filename.ToString());
-
 
406
}
-
 
407
bool C_File::readFromFile(const Utils::String filename)
401
bool C_File::readFromFile(const Utils::String filename)
408
{
402
{
409
	CFileIO File(filename);
403
	CFileIO File(filename);
410
	if ( !File.startRead() ) {
404
	if ( !File.startRead() ) {
411
		m_iLastError = SPKERR_FILEOPEN;
405
		m_iLastError = SPKERR_FILEOPEN;
Line 496... Line 490...
496
	m_sData = new unsigned char[size];
490
	m_sData = new unsigned char[size];
497
	memcpy(m_sData, data, size);
491
	memcpy(m_sData, data, size);
498
}
492
}
499
 
493
 
500
 
494
 
-
 
495
/*
-
 
496
	Func:	filePointer
-
 
497
	Desc:	Returns the file pointer name
-
 
498
			Joins dir and name together
-
 
499
			Works for relative paths as well
-
 
500
*/
501
Utils::String C_File::filePointer() const
501
Utils::String C_File::filePointer() const
502
{
502
{
503
	Utils::String fullfile = _sFullDir;
503
	Utils::String fullfile = _sFullDir;
504
	if ( !fullfile.empty() )
504
	if ( !fullfile.empty() )
505
		fullfile += "/";
505
		fullfile += "/";
Line 519... Line 519...
519
			return true;
519
			return true;
520
	}
520
	}
521
	return false;
521
	return false;
522
}
522
}
523
 
523
 
524
/*
-
 
525
	Func:	GetFilePointer
-
 
526
	Desc:	Returns the file pointer name
-
 
527
			Joins dir and name together
-
 
528
			Works for relative paths as well
-
 
529
*/
-
 
530
CyString C_File::GetFilePointer ()
-
 
531
{
-
 
532
	return filePointer();
-
 
533
}
-
 
534
 
524
 
535
 
525
 
536
void C_File::updateSignature()
526
void C_File::updateSignature()
537
{
527
{
538
	_sSignature = "";
528
	_sSignature = "";
Line 694... Line 684...
694
	if ( file->name() != _sName )
684
	if ( file->name() != _sName )
695
	{
685
	{
696
		// check if the base name matches, but only for certain file extensions
686
		// check if the base name matches, but only for certain file extensions
697
		if (file->shouldCheckBaseName())
687
		if (file->shouldCheckBaseName())
698
		{
688
		{
699
			Utils::String baseName = CFileIO(file->GetName()).dir() + "/" + CFileIO(file->GetName()).baseName();
689
			Utils::String baseName = CFileIO(file->name()).dir() + "/" + CFileIO(file->name()).baseName();
700
			Utils::String compareBaseName = CFileIO(_sName).dir() + "/" + CFileIO(_sName).baseName();
690
			Utils::String compareBaseName = CFileIO(_sName).dir() + "/" + CFileIO(_sName).baseName();
701
			if (baseName.Compare(compareBaseName))
691
			if (baseName.Compare(compareBaseName))
702
				return true;
692
				return true;
703
		}
693
		}
704
		return false;
694
		return false;
Line 863... Line 853...
863
 
853
 
864
	if ( compressionType == SPKCOMPRESS_7ZIP )
854
	if ( compressionType == SPKCOMPRESS_7ZIP )
865
		compressionType = SPKCOMPRESS_LZMA;
855
		compressionType = SPKCOMPRESS_LZMA;
866
 
856
 
867
	// 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
868
	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("pck") || this->checkFileExt("cat") || this->checkFileExt("dat") || this->checkFileExt("pbb") || this->checkFileExt("pbd")) )
869
		compressionType = SPKCOMPRESS_LZMA;
859
		compressionType = SPKCOMPRESS_LZMA;
870
 
860
 
871
	// if its already compressed, no need to compress again
861
	// if its already compressed, no need to compress again
872
	if ( compressionType == m_iDataCompression )
862
	if ( compressionType == m_iDataCompression )
873
		return true;
863
		return true;
Line 1073... Line 1063...
1073
	}
1063
	}
1074
 
1064
 
1075
	return NULL;
1065
	return NULL;
1076
}
1066
}
1077
 
1067
 
1078
bool C_File::UncompressToFile ( CyString toFile, CBaseFile *spkfile, bool includedir, CProgressInfo *progress )
1068
bool C_File::uncompressToFile(const Utils::String &toFile, CBaseFile *spkfile, bool includedir, CProgressInfo *progress)
1079
{
1069
{
1080
#ifdef _INCLUDE7ZIP
1070
#ifdef _INCLUDE7ZIP
1081
	if ( (!m_sData) || (!m_lDataSize) )
1071
	if ( (!m_sData) || (!m_lDataSize) )
1082
		return false;
1072
		return false;
1083
	// 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
1084
	if ( !m_sTmpFile.Empty() )
1074
	if ( !m_sTmpFile.empty() )
1085
	{
1075
	{
1086
		if ( CFileIO::Exists(m_sTmpFile) ) return true;
1076
		if ( CFileIO::Exists(m_sTmpFile) ) return true;
1087
		m_sTmpFile = "";
1077
		m_sTmpFile = "";
1088
	}
1078
	}
1089
 
1079
 
1090
	// now uncompress to the file
1080
	// now uncompress to the file
1091
	CyString file = toFile;
1081
	Utils::String file = toFile;
1092
	if ( file.Empty() )
1082
	if (file.empty())
1093
	{
1083
	{
1094
		m_iTempNum++;
1084
		m_iTempNum++;
1095
		file = CyString("uncompr") + (long)m_iTempNum + ".tmp";
1085
		file = "uncompr" + (long)m_iTempNum + ".tmp";
1096
	}
1086
	}
1097
	else
1087
	else
1098
		file = _getFullFileToDir(file.ToString(), includedir, spkfile);
1088
		file = _getFullFileToDir(file, includedir, spkfile);
1099
 
1089
 
1100
	CFileIO File("compr.tmp");
1090
	CFileIO File("compr.tmp");
1101
	if ( !File.startWrite() ) return false;
1091
	if ( !File.startWrite() ) return false;
1102
	if ( !File.write(m_sData, m_lDataSize) ) return false;
1092
	if ( !File.write(m_sData, m_lDataSize) ) return false;
1103
	File.close();
1093
	File.close();
1104
	if ( LZMADecodeFile ( "compr.tmp", file.c_str(), (CProgressInfo7Zip *)progress ) )
1094
	if ( LZMADecodeFile ( "compr.tmp", file.c_str(), (CProgressInfo7Zip *)progress ) )
1105
	{
1095
	{
1106
		ret = true;
1096
		ret = true;
1107
		if ( toFile.Empty() )
1097
		if ( toFile.empty() )
1108
			m_sTmpFile = file;
1098
			m_sTmpFile = file;
1109
	}
1099
	}
1110
 
1100
 
1111
	CFileIO::Remove("compr.tmp");
1101
	CFileIO::Remove("compr.tmp");
1112
 
1102
 
Line 1179... Line 1169...
1179
 
1169
 
1180
Utils::String C_File::dataSizeString() const
1170
Utils::String C_File::dataSizeString() const
1181
{
1171
{
1182
	return SPK::GetSizeString(m_lDataSize);
1172
	return SPK::GetSizeString(m_lDataSize);
1183
}
1173
}
1184
CyString C_File::GetUncompressedSizeString ()
1174
Utils::String C_File::uncompressedSizeString() const
1185
{
1175
{
1186
	return SPK::GetSizeString ( GetUncompressedDataSize() );
1176
	return SPK::GetSizeString(uncompressedDataSize());
1187
}
1177
}
1188
 
1178
 
1189
CyString C_File::GetCreationTimeString ()
1179
Utils::String C_File::creationTimeString() const
1190
{
1180
{
1191
	if ( !m_tTime )
1181
	if ( !m_tTime )
1192
		return NullString;
1182
		return Utils::String::Null();
1193
 
1183
 
1194
	struct tm   *currDate;
1184
	struct tm   *currDate;
1195
	char    dateString[100];
1185
	char    dateString[100];
1196
 
1186
 
1197
	time_t n = m_tTime;
1187
	time_t n = m_tTime;
1198
 
1188
 
1199
	currDate = localtime(&n);
1189
	currDate = localtime(&n);
1200
 
1190
 
1201
	strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
1191
	strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
1202
 
1192
 
1203
 
-
 
1204
	return CyString(dateString);
1193
	return Utils::String(dateString);
1205
}
1194
}
1206
 
1195
 
1207
bool C_File::CompareNew ( C_File *file )
1196
bool C_File::CompareNew ( C_File *file )
1208
{
1197
{
1209
	if ( !m_iVersion )
1198
	if ( !m_iVersion )
Line 1432... Line 1421...
1432
			unsigned char data[4];
1421
			unsigned char data[4];
1433
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
1422
			if ( File.read(data, 3) ) return IsDataPCK ( data, 3 );
1434
		}
1423
		}
1435
	}
1424
	}
1436
 
1425
 
1437
	if ( CheckFileExt("pck") || CheckFileExt("pbb") || CheckFileExt("pbd") ) return true;
1426
	if (checkFileExt("pck") || checkFileExt("pbb") || checkFileExt("pbd")) return true;
1438
	return false;
1427
	return false;
1439
}
1428
}
1440
 
1429
 
1441
bool C_File::PCKFile()
1430
bool C_File::PCKFile()
1442
{
1431
{
Line 1743... Line 1732...
1743
	Utils::String file = _sName.token("/", -1);
1732
	Utils::String file = _sName.token("/", -1);
1744
 
1733
 
1745
	// remove file extension
1734
	// remove file extension
1746
	return file.remToken(".", -1);
1735
	return file.remToken(".", -1);
1747
}
1736
}
1748
CyString C_File::GetBaseName()
-
 
1749
{
-
 
1750
	return baseName();
-
 
1751
}
-
 
1752
 
1737
 
1753
void C_File::CopyData(C_File *oldFile, bool includeData)
1738
void C_File::CopyData(C_File *oldFile, bool includeData)
1754
{
1739
{
1755
	SetFilename(oldFile->GetFullFilename());
1740
	setFilename(oldFile->fullFilename());
1756
	_sDir = oldFile->dir();
1741
	_sDir = oldFile->dir();
1757
	m_tTime = oldFile->GetCreationTime();
1742
	m_tTime = oldFile->GetCreationTime();
1758
	m_bShared = oldFile->IsShared();
1743
	m_bShared = oldFile->IsShared();
1759
	m_bSigned = oldFile->IsSigned();
1744
	m_bSigned = oldFile->IsSigned();
1760
	m_iFileType = oldFile->fileType();
1745
	m_iFileType = oldFile->fileType();
Line 1769... Line 1754...
1769
		{
1754
		{
1770
			m_sData = new unsigned char[m_lDataSize];
1755
			m_sData = new unsigned char[m_lDataSize];
1771
			m_bUsedMalloc = false;
1756
			m_bUsedMalloc = false;
1772
			memcpy((char *)m_sData, (char *)oldFile->GetData(), m_lDataSize);
1757
			memcpy((char *)m_sData, (char *)oldFile->GetData(), m_lDataSize);
1773
		}
1758
		}
1774
		m_lUncomprDataSize = oldFile->GetUncompressedDataSize();
1759
		m_lUncomprDataSize = oldFile->uncompressedDataSize();
1775
	}
1760
	}
1776
	m_iDataCompression = oldFile->GetCompressionType();
1761
	m_iDataCompression = oldFile->GetCompressionType();
1777
}
1762
}
1778
 
1763
 
1779
float GetLibraryVersion() { return (float)LIBRARYVERSION; }
1764
float GetLibraryVersion() { return (float)LIBRARYVERSION; }
1780
float GetFileFormatVersion() { return (float)FILEVERSION; }
1765
float GetFileFormatVersion() { return (float)FILEVERSION; }
1781
 
1766
 
1782
CyString C_File::ChangeFileExt(CyString ext)
-
 
1783
{
-
 
1784
	_sName = CFileIO(_sName).changeFileExtension(ext.ToString());
-
 
1785
	return _sName;
-
 
1786
}
-
 
1787
 
-
 
1788
Utils::String C_File::fileExt() const 
1767
Utils::String C_File::fileExt() const 
1789
{ 
1768
{ 
1790
	return _sName.token(".", -1); 
1769
	return _sName.token(".", -1); 
1791
}
1770
}
1792
 
1771
 
-
 
1772
bool C_File::checkFileExt(const Utils::String& ext) 
-
 
1773
{
-
 
1774
	if (fileExt().Compare(ext)) return true; return false;
-
 
1775
}
-
 
1776
 
1793
const Utils::String &C_File::changeFileExt(const Utils::String &ext)
1777
const Utils::String &C_File::changeFileExt(const Utils::String &ext)
1794
{
1778
{
1795
	_sName = CFileIO(_sName).changeFileExtension(ext);
1779
	_sName = CFileIO(_sName).changeFileExtension(ext);
1796
	return _sName;
1780
	return _sName;
1797
}
1781
}
1798
 
1782
 
1799
bool C_File::CheckPackedExtension()
1783
bool C_File::CheckPackedExtension()
1800
{
1784
{
1801
	CyString ext = this->GetFileExt();
1785
	Utils::String ext = this->fileExt();
1802
	if ( ext == "pck" )
1786
	if ( ext == "pck" )
1803
		return true;
1787
		return true;
1804
	else if ( ext == "pbb" )
1788
	else if ( ext == "pbb" )
1805
		return true;
1789
		return true;
1806
	else if ( ext == "pbd" )
1790
	else if ( ext == "pbd" )
Line 1921... Line 1905...
1921
bool C_File::BobDecompile()
1905
bool C_File::BobDecompile()
1922
{
1906
{
1923
	bool bRes=false;
1907
	bool bRes=false;
1924
 
1908
 
1925
 
1909
 
1926
	CyStringList outData;
1910
	Utils::CStringList outData;
1927
	outData.PushBack("// Converted by SPK Libraries\n");
1911
	outData.pushBack("// Converted by SPK Libraries\n");
1928
	/*
1912
	/*
1929
	bob_with_errors *e=0;
1913
	bob_with_errors *e=0;
1930
	char *pszTime;
1914
	char *pszTime;
1931
	char *name="";
1915
	char *name="";
1932
 
1916
 
Line 1982... Line 1966...
1982
bool C_File::BodCompile()
1966
bool C_File::BodCompile()
1983
{
1967
{
1984
	return true;
1968
	return true;
1985
}
1969
}
1986
 
1970
 
1987
bool C_File::RenameScript(CyString baseName)
1971
bool C_File::renameScript(const Utils::String &baseName)
1988
{
1972
{
1989
	if ( !m_sData || !m_lDataSize )
1973
	if ( !m_sData || !m_lDataSize )
1990
	{
1974
	{
1991
		if ( !this->ReadFromFile() )
1975
		if ( !this->ReadFromFile() )
1992
			return false;
1976
			return false;
Line 1995... Line 1979...
1995
	// uncompress the file
1979
	// uncompress the file
1996
	if ( !this->UncompressData() )
1980
	if ( !this->UncompressData() )
1997
		return false;
1981
		return false;
1998
 
1982
 
1999
	// un pck the file
1983
	// un pck the file
2000
	if ( this->CheckFileExt("pck") )
1984
	if (this->checkFileExt("pck"))
2001
	{
1985
	{
2002
		if ( !this->UnPCKFile() )
1986
		if ( !this->UnPCKFile() )
2003
			return false;
1987
			return false;
2004
	}
1988
	}
2005
 
1989
 
2006
	// now we should have the raw data
1990
	// now we should have the raw data
2007
	CyString data((const char *)m_sData);
1991
	Utils::String data((const char *)m_sData);
2008
	data.Truncate(m_lDataSize);
1992
	data.truncate(m_lDataSize);
2009
	data = data.FindReplace(this->GetBaseName(), baseName);
1993
	data = data.findReplace(this->baseName(), baseName);
2010
 
1994
 
2011
	this->DeleteData();
1995
	this->DeleteData();
2012
	m_sData = new unsigned char[data.Length()];
1996
	m_sData = new unsigned char[data.length()];
2013
	memcpy(m_sData, data.c_str(), data.Length());
1997
	memcpy(m_sData, data.c_str(), data.length());
2014
	m_lDataSize = (long)data.Length();
1998
	m_lDataSize = (long)data.length();
2015
 
1999
 
2016
	// repck the file
2000
	// repck the file
2017
	if ( this->CheckFileExt("pck") )
2001
	if (this->checkFileExt("pck"))
2018
	{
2002
	{
2019
		if ( !this->PCKFile() )
2003
		if ( !this->PCKFile() )
2020
			return false;
2004
			return false;
2021
	}
2005
	}
2022
 
2006