Subversion Repositories spk

Rev

Rev 213 | 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 );
213 cycrow 103
unsigned char SPKEXPORT *UnPCKFile (const Utils::WString &file, size_t *len, bool nocrypt );
1 cycrow 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
	}
308 cycrow 132
	virtual void UpdateDisplay(const Utils::WString& display) {};
1 cycrow 133
	void UpdateFile(C_File *f) { DoingFile(f); }
134
	void UpdatePackage(SMultiSpkFile *f) { DoingPackage(f); }
135
	unsigned long *GetDonePointer() { return &m_iDone; }
136
	unsigned long GetDone() { return m_iDone; }
137
	void SetDone(int i) { m_iDone = i; }
138
 
139
protected:
140
	virtual void ProgressUpdated ( const long cur, const long max ) = 0;
141
	virtual void ProgressUpdated2 ( const long cur, const long max ) { };
142
	virtual void StatusUpdated(int i) { }
143
	virtual void DoingFile ( C_File *file ) = 0;
144
	virtual void DoingPackage ( SMultiSpkFile *file ) { }
145
 
146
	bool m_bDoIn;
147
	long m_lMaxSize;
148
	bool m_bDoHalf;
149
	bool m_bSecondHalf;
150
	bool m_bDoSecond;
151
	int	 m_iStatus;
152
 
153
	unsigned long m_iDone;
154
};
155
 
156
 
157
class SPKEXPORT CProgressInfoDone : public CProgressInfo
158
{
159
public:
160
	CProgressInfoDone () : CProgressInfo() { m_pOnFile = NULL; m_bDontDoMax = false; m_pOnPackage = NULL; }
161
 
162
	unsigned long GetMax() { return m_lMaxSize; }
163
	C_File *GetFile() { return m_pOnFile; }
164
	SMultiSpkFile *GetPackage() { return m_pOnPackage; }
165
 
166
	void DoMax() { m_bDontDoMax = false; }
167
	void DontDoMax() { m_bDontDoMax = true; }
168
 
169
protected:
170
	virtual void ProgressUpdated ( const long cur, const long max ) { m_iDone = cur; m_lMaxSize = max; }
171
	virtual void DoingFile ( C_File *file );
172
	virtual void DoingPackage ( SMultiSpkFile *package );
173
	virtual void StatusUpdated(int i) { if ( i == STATUS_WRITE ) this->DontDoMax(); }
174
 
175
	SMultiSpkFile	*m_pOnPackage;
176
	C_File			*m_pOnFile;
177
	bool			 m_bDontDoMax;
178
};
179
 
180
#include "ansi7zip/7Decoder.h"
181
 
182
#define GZ_FLAG_TEXT     1     // 0
183
#define GZ_FLAG_HCRC     2     // 1
184
#define GZ_FLAG_EXTRA    4     // 2
185
#define GZ_FLAG_FILENAME 8     // 3
186
#define GZ_FLAG_COMMENT  16    // 4
187
#define GZ_FLAG_RES1     32    // 5
188
#define GZ_FLAG_RES2     64    // 6
189
#define GZ_FLAG_RES3     128   // 7
190
 
191
class CProgressInfo2
192
{
193
public:
194
	virtual void ProgressPercent ( float percent ) = 0;
195
};
196
 
197
 
197 cycrow 198
Utils::WString SPKEXPORT GetFileTypeString ( int type );
199
FileType SPKEXPORT GetFileTypeFromString(const Utils::WString &type);
200 cycrow 200
Utils::WString SPKEXPORT FormatErrorString (int error, const Utils::WString &rest);
1 cycrow 201
float	 SPKEXPORT GetLibraryVersion ();
202
float	 SPKEXPORT GetFileFormatVersion ();
203
 
204
class CBaseFile;
205
class SPKEXPORT C_File
206
{
207
public:
197 cycrow 208
	static Utils::WString GetDirectory(FileType eType, const Utils::WString &filename, CBaseFile *file);
127 cycrow 209
	static bool DoesTypeHaveExtraDir(int i);
210
 
211
public:
212
 
160 cycrow 213
	void clearUsed () { _iUsed = 0; }
214
	void incUsed () { ++_iUsed; }
1 cycrow 215
	void DeleteData ();
216
	// varible setting functions
127 cycrow 217
	void setFileType(FileType t) { m_iFileType = t; }
197 cycrow 218
	void setFilename(const Utils::WString &filename);
219
	void setName(const Utils::WString &name) { _sName = name; }
220
	void setDir(const Utils::WString &dir) { _sDir = dir; }
1 cycrow 221
	void SetCreationTime ( time_t time ) { m_tTime = time; }
222
	void SetFileSize ( long size ) { m_lSize = size; }
223
	void SetDataSize ( long size ) { m_lDataSize = size; }
224
	void SetUncompressedDataSize ( long size ) { m_lUncomprDataSize = size; }
225
	void SetDataCompression ( int c ) { m_iDataCompression = c; }
226
	void SetShared ( bool b ) { m_bShared = b; }
227
	void SetSigned ( bool b ) { m_bSigned = b; }
197 cycrow 228
	void setFullDir(const Utils::WString &dir) { _sFullDir = dir; }
229
	void setInMod (const Utils::WString &s) { _sInMod = s; }
1 cycrow 230
	void SetCompressedToFile ( bool b ) { m_bCompressedToFile = b; }
231
	void SetData(const unsigned char *data, size_t size) { m_sData = (unsigned char *)data, m_lDataSize = (long)size; }
43 cycrow 232
	void copyData(const unsigned char *data, size_t size);
1 cycrow 233
 
234
	// get functions
160 cycrow 235
	int getUsed () { return _iUsed; }
1 cycrow 236
	int GetLastError () { return m_iLastError; }
127 cycrow 237
	int GetFileType() { return m_iFileType; }
238
	FileType fileType() { return m_iFileType; }
197 cycrow 239
	const Utils::WString &filename() const { return _sName; }
240
	Utils::WString fullFilename() { return _sFullDir + "/" + _sName; }
1 cycrow 241
	long GetSize () { return m_lSize; }
242
	time_t GetCreationTime () { return m_tTime; }
197 cycrow 243
	const Utils::WString &name() { return _sName; }
244
	const Utils::WString &dir() { return _sDir; }
245
	Utils::WString dataSizeString() const;
246
	Utils::WString uncompressedSizeString() const;
247
	Utils::WString getDirectory(CBaseFile *spkfile) const;
1 cycrow 248
	int GetVersion () { return m_iVersion; }
249
	time_t GetLastModified() { return m_tTime; }
197 cycrow 250
	Utils::WString fileTypeString() const { return ::GetFileTypeString(m_iFileType); }
251
	Utils::WString creationTimeString() const;
252
	const Utils::WString &getTempFile () const { return _sTmpFile; }
253
	const Utils::WString fullDir() const { return _sFullDir; }
254
	Utils::WString baseName() const;
255
	const Utils::WString &originalName() const { return _sOriginalName; }
256
	const Utils::WString &getInMod() const { return _sInMod; }
257
	const Utils::WString &getSignature() { if ( !m_bUpdatedSignature ) updateSignature(); return _sSignature; }
160 cycrow 258
	void updateSignature();
197 cycrow 259
	void changeBaseName(const Utils::WString &b) { _sName = b + "." + this->fileExt(); }
260
	bool renameScript(const Utils::WString &baseName);
160 cycrow 261
	bool isInMod() { return !_sInMod.empty(); }
1 cycrow 262
 
130 cycrow 263
	void FixOriginalName() { _sOriginalName = _sName; }
197 cycrow 264
	void SetOriginalName(const Utils::WString &name) { _sOriginalName = name; }
1 cycrow 265
 
126 cycrow 266
	bool isFileInAddon() const;
121 cycrow 267
	bool IsFakePatch () const;
130 cycrow 268
	bool isAutoTextFile();
197 cycrow 269
	int  textFileID(const Utils::WString &name = Utils::WString::Null()) const;
1 cycrow 270
	bool IsCompressedToFile () { return m_bCompressedToFile; }
271
	bool CompareNew ( C_File *file );
197 cycrow 272
	Utils::WString fileExt() const;
273
	bool checkFileExt(const Utils::WString& ext);
274
	const Utils::WString &changeFileExt(const Utils::WString &ext);
1 cycrow 275
	bool CheckPackedExtension();
125 cycrow 276
	bool shouldCheckBaseName() const;
1 cycrow 277
 
278
	int GetCompressionType () { return m_iDataCompression; }
279
	long GetDataSize () { return m_lDataSize; }
280
	// returns the Uncompressed Data size, based on how the file is loaded, ie view data, view pointer, etc
178 cycrow 281
	long uncompressedDataSize() const;
197 cycrow 282
	Utils::WString getNameDirectory(CBaseFile *spkfile) const;
1 cycrow 283
 
127 cycrow 284
	unsigned int game() const;
129 cycrow 285
	bool isForGame(int game) const;
286
	int getForSingleGame() const;
127 cycrow 287
	void setGame(unsigned int i);
288
	//TODO
289
	unsigned int GetGame() const { return _iGame;  }
1 cycrow 290
 
291
	unsigned char *GetData () { return m_sData; }
292
	bool IsShared () { return m_bShared; }
293
	bool IsSigned () { return m_bSigned; }
294
	bool UpdateSigned();
295
	bool ReadSignedFile();
296
 
297
	// file reading functions
298
	long ReadFileSize ();
299
	time_t ReadLastModified ();
197 cycrow 300
	bool readFromFile(const Utils::WString filename);
1 cycrow 301
	bool ReadFromFile ();
58 cycrow 302
	bool readFromFile(CFileIO &File);
303
	bool readFromFile(CFileIO &File, int lSize, bool bDoSize = true);
1 cycrow 304
	bool ReadFromData ( char *data, long size );
305
	int ReadScriptVersion ();
197 cycrow 306
	Utils::WString filePointer() const;
127 cycrow 307
	bool isExternalFile() const;
1 cycrow 308
 
309
	// file writing functions
197 cycrow 310
	bool writeToDir(const Utils::WString &dir, CBaseFile *, bool = true, const Utils::WString &appendDir = Utils::WString::Null(), unsigned char * = NULL, long = 0);
311
	bool writeToFile(const Utils::WString &filename, unsigned char * = NULL, long = 0);
129 cycrow 312
	bool writeFilePointer(unsigned char *cData = NULL, long len = 0);
1 cycrow 313
 
314
	// file compression functions
308 cycrow 315
	unsigned char *CompressToData(int compressionType, size_t *outSize, CProgressInfo *progress = NULL, int level = DEFAULT_COMPRESSION_LEVEL);
197 cycrow 316
	bool uncompressToFile(const Utils::WString &toFile, CBaseFile *spkFile, bool includeDir = true, CProgressInfo *progress = NULL);
1 cycrow 317
	bool ChangeCompression(int compresstyp, CProgressInfo *progress);
318
	bool CompressData ( int compressionType, CProgressInfo * = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
319
	bool CompressFile ( CProgressInfo * = NULL );
320
	bool UncompressData ( CProgressInfo * = NULL );
321
	unsigned char *UncompressData ( long *size, CProgressInfo * = NULL );
322
 
323
	void CopyData(C_File *, bool includeData = true);
324
 
325
	bool IsDisabled () { return m_bDisabled; }
326
	void SetDisabled ( bool b ) { m_bDisabled = b; }
327
 
328
	void MarkSkip () { m_bSkip = true; }
329
	bool Skip () { return m_bSkip; }
330
	bool CheckPCK ();
331
	bool CheckValidFilePointer ();
332
	unsigned char *UnPCKFile ( size_t *len );
333
	bool MatchFile ( C_File *file );
334
	bool UnPCKFile ();
335
	bool PCKFile();
336
 
337
	void SetPos(int i)	{ m_iPos = i; }
338
	int  GetPos()		{ return m_iPos; }
339
 
115 cycrow 340
	unsigned char *BobDecompile(size_t *size);
1 cycrow 341
	bool BobDecompile();
342
	bool BodCompile();
343
 
344
	// contructor/decontructor
345
	C_File();
197 cycrow 346
	C_File(const Utils::WString &filename);
1 cycrow 347
	virtual ~C_File();
348
 
349
protected:
350
	static int m_iTempNum; // Used when creating temp files, allows new tmp files without overrighing used ones
351
 
352
	// private functions
353
	void Reset();
197 cycrow 354
	Utils::WString _getFullFileToDir(const Utils::WString &dir, bool includedir, CBaseFile *file) const;
1 cycrow 355
 
197 cycrow 356
	Utils::WString  _sName; // just the filename
357
	Utils::WString  _sDir;  // the extra dir (only for extras)
358
	Utils::WString  _sInMod; // in a mod files
359
	Utils::WString  _sSignature;
1 cycrow 360
 
361
	// file pointer varibles
197 cycrow 362
	Utils::WString  _sFullDir; // the full dir path of the file (This is used for file pointers when not loaded data in file)
1 cycrow 363
	long    m_lSize;  // size of current file pointer
364
 
365
	// Main file varibles
366
	int		m_iVersion;  // used when reading script versions
367
	bool	m_bSigned;   // signed status of file, installer only, read from file
368
	bool    m_bShared;   // is file a marked shared file (Not used much anymore)
160 cycrow 369
	int		_iUsed;     // used by installer, number of plugins that uses the file
1 cycrow 370
	time_t	m_tTime;     // Creation time of the file
371
 
372
	// File data varibles
373
	long	m_lDataSize;  // size of the data stream in what ever compression is set
374
	long	m_lUncomprDataSize; // size of stream if it was uncompressed
375
	unsigned char   *m_sData;  // stores the file data if loaded in memory
376
	int		m_iDataCompression; // the current compression of m_sData
377
 
127 cycrow 378
	FileType m_iFileType; // type if file, ie Script, Text, etc
1 cycrow 379
 
380
	bool    m_bUsedMalloc; // malloc type of m_sData, so it can use with free() or delete
197 cycrow 381
	Utils::WString	_sTmpFile;
1 cycrow 382
 
383
	bool	m_bCompressedToFile;
384
 
385
	bool    m_bSkip;
386
	bool	m_bLoaded;
387
	bool	m_bDisabled;	// if the file has be disabled
388
	bool	m_bUpdatedSignature;
389
 
390
	int		m_iPos;
127 cycrow 391
	unsigned int _iGame;
1 cycrow 392
 
197 cycrow 393
	Utils::WString _sOriginalName;
1 cycrow 394
 
395
	int m_iLastError;
396
	bool	m_bDontDeleteData; // fix for bad data deleteion
397
};
398
 
399
#endif // !defined(AFX_FILE_H__A0C15B81_4FD1_40D7_8EE8_2ECF5824BB8B__INCLUDED_)