Subversion Repositories spk

Rev

Rev 128 | Rev 130 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
// File.h: interface for the C_File class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
 
5
/*
6
File class, Created by Matthew Gravestock (Cycrow)
7
 
8
	This class handles the store of data for each of the file in the package.
9
	It also includes all the compression functions to compress and uncompress the files from the package
10
 
11
	Enums:
12
		Compression Type - The type of compression for each section and file, theres currently 3 types
13
			7Zip - Compress via 7Zip compression, only works on windows version, but linux version can decompress them. Default for file data on Windows version
14
			ZLIB - Compress via the ZLIB libray, ie Zip copression. Default compression for headers on all versions, and file data on Linux Version
15
			None - No compression, just write in plain text (Not really used, prodcudes much larger files)
16
 
17
		FileType - This is what type of file they are, deterimes how and where the file is installed
18
			Script - A script file, goes into the X3/Scripts directory
19
			Text - A Text file, ie 447532.xml, contains all text data used by script/mod.  Goes in the X3/t directory
20
			Readme - A Readme .txt file, can be displayed in installer in Rich Text format
21
			Map - A Map script, these are xml files that create a new universe
22
			Mod - .cat/.dat file pair, can also be a fake patch if named as a number, ie 01.cat
23
			Uninstall - An uninstall script, only goes to X3/Scripts directory when plugin is uninstalled, allows plugins to clean themselves when removed
24
			Sound - Soundtrack files, goes in the X3/Soundtrack directory, used for the sector music
25
			Screen - Screen shot file, goes in the X3/loadscr directory, will be displayed as a loading screen
26
			Extra - Any other file that doesn't fit in the section, can be placed in any directory thats required
27
 
28
		Error - Stores the last error found (currently only used for a few errors)
29
			None - No error, function was successful
30
			Malloc - Error trying to malloc memory space, possible cause (Not enough memory)
31
			Fileopen - Unable to open the file, possible cause (File doesn't exist)
32
			Fileread - Error trying to read a file, happens after the file is open
33
 
34
	Class includes all files needed for both 7Zip and ZLIB Libraries, and acts as the path between them
35
 
36
	Functions (Windows Only):
37
		LZMAEncodeData - Encodes a data stream using 7Zip, returns the encoded data as char array
38
		LZMADecodeData - Decodes a compressed stream using 7Zip, returns the uncompressed data
39
		LZMAEncodeFile - Encodes a file stream using 7Zip, writes to another file
40
		LZMADecodeFile - Decodes a file stream using 7Zip, writes uncomressed data to file
41
 
42
	Classes
43
		CProgressInfo - Used to store progress info, basse class that needs to be dervived from to handle the progress update.
44
						When decoding multi files, calls DoingFile() for each functions, allows class to display the current progress of the file
45
						ProgressUpdated() is called for each progress, as the processed size and max size
46
		CProgressInfo7Zip (Windows Only) - 7Zip specific progress, dervive from this for using progress on 7Zip Compression. (Currently no progress for ZLIB)
47
 
48
	C_File:
49
		The files here are stored in 1 of 2 ways, either file data, or a file pointer
50
 
51
		File Data:
52
			Uses the m_sData and m_lDataSize varibles.  As well as the m_iDataCompression.
53
			This method is used when reading the file to memory, either from a file or direct from the SPK Package.
54
			This can be stored in compressed form so when writing the SPK File, it doesn't need to be compressed again.
55
			CompressData() will compress the current data stream to m_sData, changes m_lDataSize and m_iDataCompression to match
56
			UncomressData() will uncompress the data, it can either uncomress and return the uncomressed data stream, without effecting m_sData,
57
			or it can change m_sData to the uncompressed data.
58
 
59
		File Pointer:
60
			When adding a new file, it will be first added as a pointer, this means that there is no data loaded, it just points to a filename on disk.
61
			ReadFileSize() will read the size of the file to m_lSize
62
			ReadLastModifed() will read the modified data tag of the file and store it in the class
63
			ReadFromFile() will open and read the file to the data stream, m_sData, the compression will be set to None.
64
			ReadFromFile (FILE *id) will read from a currently open file stream into the data, can be used to read a file from an existing SPK Package
65
 
66
 
67
*/
68
 
69
#if !defined(AFX_FILE_H__A0C15B81_4FD1_40D7_8EE8_2ECF5824BB8B__INCLUDED_)
70
#define AFX_FILE_H__A0C15B81_4FD1_40D7_8EE8_2ECF5824BB8B__INCLUDED_
71
 
72
#if _MSC_VER > 1000
73
#pragma once
74
#endif // _MSC_VER > 1000
75
 
32 cycrow 76
#define LZMA_LEVEL	5
77
#define LZMA_DICT	(1 << 26)
78
 
79
#define PCKHEADERSIZE 10
80
#define DEFAULT_COMPRESSION_LEVEL	5
81
 
94 cycrow 82
#include "spkdefines.h"
1 cycrow 83
#include "File_IO.h"
32 cycrow 84
#include "zlib/zlib.h"
127 cycrow 85
#include "enums.h"
1 cycrow 86
//#include "x2bc/x2bc_common/bob_dom.h"
87
//#include "x2bc/x2bc_common/bob_realfile_stream.h"
88
 
89
// compression type
90
enum { SPKCOMPRESS_NONE, SPKCOMPRESS_ZLIB, SPKCOMPRESS_7ZIP, SPKCOMPRESS_LZMA, SPKCOMPRESS_BEST };
32 cycrow 91
 
18 cycrow 92
// special file types used internally
93
enum {
94
	FILETYPE_SCRIPT_UNINSTALL		= 1000,
95
};
1 cycrow 96
// error
97
enum {SPKERR_NONE, SPKERR_MALLOC, SPKERR_FILEOPEN, SPKERR_FILEREAD, SPKERR_UNCOMPRESS, SPKERR_WRITEFILE, SPKERR_CREATEDIRECTORY, SPKERR_FILEMISMATCH};
98
enum {STATUS_NONE, STATUS_COMPRESS, STATUS_WRITE};
99
 
100
 
101
bool IsDataPCK ( const unsigned char *data, size_t size );
102
unsigned char SPKEXPORT *UnPCKData ( unsigned char *data, size_t datasize, size_t *len, bool nocrypt );
103
unsigned char SPKEXPORT *UnPCKFile ( const char *file, size_t *len, bool nocrypt );
104
unsigned char SPKEXPORT *UnPCKData ( unsigned char *data, size_t datasize, size_t *len );
105
int ReadScriptVersionFromData ( unsigned char *data, long size );
106
bool ReadSignedFromData ( unsigned char *data, long size );
107
 
108
struct SMultiSpkFile;
109
 
110
class C_File;
111
class SPKEXPORT CProgressInfo
112
{
113
public:
114
	CProgressInfo () { m_bDoIn = false; m_lMaxSize = 0; m_bDoHalf = false; m_bSecondHalf = false; m_bDoSecond = false; m_iStatus = -1; m_iDone = 0;}
115
 
116
	void SetIn ( bool in ) { m_bDoIn = in; }
117
	void SetMax ( long max ) { m_lMaxSize = max; }
118
	void DoHalf() { m_bDoHalf = true; m_bSecondHalf = false; }
119
	void SecondHalf() { m_bSecondHalf = true; }
120
	void SwitchSecond() { m_bDoSecond = !m_bDoSecond; }
121
	bool IsSecond() { return m_bDoSecond; }
122
	void UpdateStatus(int i ) { m_iStatus = i; StatusUpdated(i); }
123
	int  GetStatus() { return m_iStatus; }
124
	void IncDone(int i) { m_iDone += i; }
125
	void UpdateProgress ( const long cur, const long max )
126
	{
127
		if ( !m_bDoSecond )
128
			ProgressUpdated(cur, max);
129
		else
130
			ProgressUpdated2(cur, max);
131
	}
132
	void UpdateFile(C_File *f) { DoingFile(f); }
133
	void UpdatePackage(SMultiSpkFile *f) { DoingPackage(f); }
134
	unsigned long *GetDonePointer() { return &m_iDone; }
135
	unsigned long GetDone() { return m_iDone; }
136
	void SetDone(int i) { m_iDone = i; }
137
 
138
protected:
139
	virtual void ProgressUpdated ( const long cur, const long max ) = 0;
140
	virtual void ProgressUpdated2 ( const long cur, const long max ) { };
141
	virtual void StatusUpdated(int i) { }
142
	virtual void DoingFile ( C_File *file ) = 0;
143
	virtual void DoingPackage ( SMultiSpkFile *file ) { }
144
 
145
	bool m_bDoIn;
146
	long m_lMaxSize;
147
	bool m_bDoHalf;
148
	bool m_bSecondHalf;
149
	bool m_bDoSecond;
150
	int	 m_iStatus;
151
 
152
	unsigned long m_iDone;
153
};
154
 
155
 
156
class SPKEXPORT CProgressInfoDone : public CProgressInfo
157
{
158
public:
159
	CProgressInfoDone () : CProgressInfo() { m_pOnFile = NULL; m_bDontDoMax = false; m_pOnPackage = NULL; }
160
 
161
	unsigned long GetMax() { return m_lMaxSize; }
162
	C_File *GetFile() { return m_pOnFile; }
163
	SMultiSpkFile *GetPackage() { return m_pOnPackage; }
164
 
165
	void DoMax() { m_bDontDoMax = false; }
166
	void DontDoMax() { m_bDontDoMax = true; }
167
 
168
protected:
169
	virtual void ProgressUpdated ( const long cur, const long max ) { m_iDone = cur; m_lMaxSize = max; }
170
	virtual void DoingFile ( C_File *file );
171
	virtual void DoingPackage ( SMultiSpkFile *package );
172
	virtual void StatusUpdated(int i) { if ( i == STATUS_WRITE ) this->DontDoMax(); }
173
 
174
	SMultiSpkFile	*m_pOnPackage;
175
	C_File			*m_pOnFile;
176
	bool			 m_bDontDoMax;
177
};
178
 
179
#include "ansi7zip/7Decoder.h"
180
 
181
#define GZ_FLAG_TEXT     1     // 0
182
#define GZ_FLAG_HCRC     2     // 1
183
#define GZ_FLAG_EXTRA    4     // 2
184
#define GZ_FLAG_FILENAME 8     // 3
185
#define GZ_FLAG_COMMENT  16    // 4
186
#define GZ_FLAG_RES1     32    // 5
187
#define GZ_FLAG_RES2     64    // 6
188
#define GZ_FLAG_RES3     128   // 7
189
 
190
class CProgressInfo2
191
{
192
public:
193
	virtual void ProgressPercent ( float percent ) = 0;
194
};
195
 
196
 
126 cycrow 197
Utils::String SPKEXPORT GetFileTypeString ( int type );
127 cycrow 198
FileType SPKEXPORT GetFileTypeFromString ( CyString type );
1 cycrow 199
CyString SPKEXPORT FormatErrorString ( int error, CyString rest );
200
float	 SPKEXPORT GetLibraryVersion ();
201
float	 SPKEXPORT GetFileFormatVersion ();
202
 
203
class CBaseFile;
204
class SPKEXPORT C_File
205
{
206
public:
127 cycrow 207
	static Utils::String GetDirectory(FileType eType, const Utils::String &filename, CBaseFile *file);
208
	static bool DoesTypeHaveExtraDir(int i);
209
 
210
public:
211
 
1 cycrow 212
	void ClearUsed () { m_iUsed = 0; }
213
	void IncUsed () { ++m_iUsed; }
214
	void DeleteData ();
215
	// varible setting functions
127 cycrow 216
	void SetFileType(FileType t) { m_iFileType = t; }
217
	void setFileType(FileType t) { m_iFileType = t; }
129 cycrow 218
	void SetFilename(CyString filename);
219
	void setFilename(const Utils::String &filename);
1 cycrow 220
	void SetName ( CyString name ) { m_sName = name; }
221
	void SetDir ( CyString dir ) { m_sDir = dir; }
222
	void SetCreationTime ( time_t time ) { m_tTime = time; }
223
	void SetFileSize ( long size ) { m_lSize = size; }
224
	void SetDataSize ( long size ) { m_lDataSize = size; }
225
	void SetUncompressedDataSize ( long size ) { m_lUncomprDataSize = size; }
226
	void SetDataCompression ( int c ) { m_iDataCompression = c; }
227
	void SetShared ( bool b ) { m_bShared = b; }
228
	void SetSigned ( bool b ) { m_bSigned = b; }
129 cycrow 229
	void SetFullDir(CyString dir) { _sFullDir = dir.ToString(); }
230
	void setFullDir(const Utils::String &dir) { _sFullDir = dir; }
1 cycrow 231
	void SetInMod ( CyString s ) { m_sInMod = s; }
232
	void SetCompressedToFile ( bool b ) { m_bCompressedToFile = b; }
233
	void SetData(const unsigned char *data, size_t size) { m_sData = (unsigned char *)data, m_lDataSize = (long)size; }
43 cycrow 234
	void copyData(const unsigned char *data, size_t size);
1 cycrow 235
 
236
	// get functions
237
	int GetUsed () { return m_iUsed; }
238
	int GetLastError () { return m_iLastError; }
127 cycrow 239
	int GetFileType() { return m_iFileType; }
240
	FileType fileType() { return m_iFileType; }
1 cycrow 241
	CyString GetFilename () { return m_sName; }
129 cycrow 242
	CyString GetFullFilename() { return CyString(_sFullDir) + "/" + m_sName; }
243
	Utils::String fullFilename() { return _sFullDir + "/" + m_sName.ToString(); }
1 cycrow 244
	long GetSize () { return m_lSize; }
245
	time_t GetCreationTime () { return m_tTime; }
246
	CyString GetName () { return m_sName; }
247
	CyString GetDir() { return m_sDir; }
248
	CyString GetDataSizeString ();
249
	CyString GetUncompressedSizeString ();
121 cycrow 250
	CyString GetDirectory(CBaseFile *spkfile);
251
	Utils::String getDirectory(CBaseFile *spkfile) const;
1 cycrow 252
	int GetVersion () { return m_iVersion; }
253
	time_t GetLastModified() { return m_tTime; }
254
	CyString GetFileTypeString () { return ::GetFileTypeString ( m_iFileType ); }
255
	CyString GetCreationTimeString ();
256
	CyString GetTempFile () { return m_sTmpFile; }
129 cycrow 257
	CyString GetFullDir() { return _sFullDir; }
258
	const Utils::String fullDir() const { return _sFullDir; }
1 cycrow 259
	CyString GetBaseName ();
127 cycrow 260
	CyString GetOriginalName() { return CyString(_sOriginalName); }
261
	const Utils::String &originalName() { return _sOriginalName; }
1 cycrow 262
	CyString GetInMod() { return m_sInMod; }
263
	CyString GetSignature() { if ( !m_bUpdatedSignature ) UpdateSignature(); return m_sSignature; }
264
	void UpdateSignature();
265
	void ChangeBaseName(CyString b) { m_sName = b + "." + this->GetFileExt(); }
266
	bool RenameScript(CyString baseName);
267
	bool IsInMod() { return !m_sInMod.Empty(); }
268
 
127 cycrow 269
	void FixOriginalName() { _sOriginalName = m_sName.ToString(); }
270
	void SetOriginalName(const Utils::String &name) { _sOriginalName = name; }
1 cycrow 271
 
126 cycrow 272
	bool isFileInAddon() const;
121 cycrow 273
	bool IsFakePatch () const;
1 cycrow 274
	bool IsAutoTextFile();
275
	int  GetTextFileID(CyString name = NullString);
276
	bool IsCompressedToFile () { return m_bCompressedToFile; }
277
	bool CompareNew ( C_File *file );
278
	CyString GetFileExt () { return m_sName.GetToken ( m_sName.NumToken('.'), '.' ); }
279
	bool CheckFileExt ( const CyString &ext ) { if ( GetFileExt().Compare(ext) ) return true; return false; }
280
	CyString ChangeFileExt(CyString ext);
281
	bool CheckPackedExtension();
125 cycrow 282
	bool shouldCheckBaseName() const;
1 cycrow 283
 
284
	int GetCompressionType () { return m_iDataCompression; }
285
	long GetDataSize () { return m_lDataSize; }
286
	// returns the Uncompressed Data size, based on how the file is loaded, ie view data, view pointer, etc
287
	long GetUncompressedDataSize ()
288
	{
289
		if ( m_lUncomprDataSize )
290
			return m_lUncomprDataSize;
291
		if ( m_lSize )
292
			return m_lSize;
293
		return m_lDataSize;
294
	}
128 cycrow 295
	CyString GetNameDirectory(CBaseFile *spkfile);
296
	Utils::String getNameDirectory(CBaseFile *spkfile) const;
1 cycrow 297
 
127 cycrow 298
	unsigned int game() const;
129 cycrow 299
	bool isForGame(int game) const;
300
	int getForSingleGame() const;
127 cycrow 301
	void setGame(unsigned int i);
302
	//TODO
303
	unsigned int GetGame() const { return _iGame;  }
1 cycrow 304
 
305
	unsigned char *GetData () { return m_sData; }
306
	bool IsShared () { return m_bShared; }
307
	bool IsSigned () { return m_bSigned; }
308
	bool UpdateSigned();
309
	bool ReadSignedFile();
310
 
311
	// file reading functions
312
	long ReadFileSize ();
313
	time_t ReadLastModified ();
129 cycrow 314
	bool readFromFile(const Utils::String filename);
1 cycrow 315
	bool ReadFromFile (CyString filename);
316
	bool ReadFromFile ();
58 cycrow 317
	bool readFromFile(CFileIO &File);
318
	bool readFromFile(CFileIO &File, int lSize, bool bDoSize = true);
319
	//bool readFromFile(std::fstream &stream, long lSize, bool bDoSize = true );
1 cycrow 320
	bool ReadFromData ( char *data, long size );
321
	int ReadScriptVersion ();
129 cycrow 322
	CyString GetFilePointer();
88 cycrow 323
	Utils::String filePointer() const;
127 cycrow 324
	bool isExternalFile() const;
1 cycrow 325
 
326
	// file writing functions
129 cycrow 327
	bool writeToDir(const Utils::String &dir, CBaseFile *, bool = true, const Utils::String &appendDir = Utils::String::Null(), unsigned char * = NULL, long = 0);
328
	bool writeToFile(const Utils::String &filename, unsigned char * = NULL, long = 0);
329
	bool writeFilePointer(unsigned char *cData = NULL, long len = 0);
1 cycrow 330
 
331
	// file compression functions
332
	unsigned char *CompressToData(int compressionType, unsigned long *outSize, CProgressInfo *progress = NULL, int level = DEFAULT_COMPRESSION_LEVEL);
333
	bool UncompressToFile ( CyString, CBaseFile *, bool = true, CProgressInfo * = NULL );
334
	bool ChangeCompression(int compresstyp, CProgressInfo *progress);
335
	bool CompressData ( int compressionType, CProgressInfo * = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
336
	bool CompressFile ( CProgressInfo * = NULL );
337
	bool UncompressData ( CProgressInfo * = NULL );
338
	unsigned char *UncompressData ( long *size, CProgressInfo * = NULL );
339
 
340
	void CopyData(C_File *, bool includeData = true);
341
 
342
	bool IsDisabled () { return m_bDisabled; }
343
	void SetDisabled ( bool b ) { m_bDisabled = b; }
344
 
345
	void MarkSkip () { m_bSkip = true; }
346
	bool Skip () { return m_bSkip; }
347
	bool CheckPCK ();
348
	bool CheckValidFilePointer ();
349
	unsigned char *UnPCKFile ( size_t *len );
350
	bool MatchFile ( C_File *file );
351
	bool UnPCKFile ();
352
	bool PCKFile();
353
 
354
	void SetPos(int i)	{ m_iPos = i; }
355
	int  GetPos()		{ return m_iPos; }
356
 
115 cycrow 357
	unsigned char *BobDecompile(size_t *size);
1 cycrow 358
	bool BobDecompile();
359
	bool BodCompile();
360
 
361
	// contructor/decontructor
362
	C_File();
127 cycrow 363
	C_File(const Utils::String &filename);
1 cycrow 364
	virtual ~C_File();
365
 
366
protected:
367
	static int m_iTempNum; // Used when creating temp files, allows new tmp files without overrighing used ones
368
 
369
	// private functions
370
	void Reset();
129 cycrow 371
	Utils::String _getFullFileToDir(const Utils::String &dir, bool includedir, CBaseFile *file) const;
1 cycrow 372
 
373
	CyString  m_sName; // just the filename
374
	CyString  m_sDir;  // the extra dir (only for extras)
375
	CyString  m_sInMod; // in a mod files
376
	CyString  m_sSignature;
377
 
378
	// file pointer varibles
129 cycrow 379
	Utils::String  _sFullDir; // the full dir path of the file (This is used for file pointers when not loaded data in file)
1 cycrow 380
	long    m_lSize;  // size of current file pointer
381
 
382
	// Main file varibles
383
	int		m_iVersion;  // used when reading script versions
384
	bool	m_bSigned;   // signed status of file, installer only, read from file
385
	bool    m_bShared;   // is file a marked shared file (Not used much anymore)
386
	int		m_iUsed;     // used by installer, number of plugins that uses the file
387
	time_t	m_tTime;     // Creation time of the file
388
 
389
	// File data varibles
390
	long	m_lDataSize;  // size of the data stream in what ever compression is set
391
	long	m_lUncomprDataSize; // size of stream if it was uncompressed
392
	unsigned char   *m_sData;  // stores the file data if loaded in memory
393
	int		m_iDataCompression; // the current compression of m_sData
394
 
127 cycrow 395
	FileType m_iFileType; // type if file, ie Script, Text, etc
1 cycrow 396
 
397
	bool    m_bUsedMalloc; // malloc type of m_sData, so it can use with free() or delete
398
	CyString	m_sTmpFile;
399
 
400
	bool	m_bCompressedToFile;
401
 
402
	bool    m_bSkip;
403
	bool	m_bLoaded;
404
	bool	m_bDisabled;	// if the file has be disabled
405
	bool	m_bUpdatedSignature;
406
 
407
	int		m_iPos;
127 cycrow 408
	unsigned int _iGame;
1 cycrow 409
 
127 cycrow 410
	Utils::String _sOriginalName;
1 cycrow 411
 
412
	int m_iLastError;
413
	bool	m_bDontDeleteData; // fix for bad data deleteion
414
};
415
 
416
#endif // !defined(AFX_FILE_H__A0C15B81_4FD1_40D7_8EE8_2ECF5824BB8B__INCLUDED_)