Subversion Repositories spk

Rev

Rev 51 | Rev 58 | 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
 
1 cycrow 82
#define tstruct typedef struct SPKEXPORT
83
#define tclass class SPKEXPORT 
84
 
85
#include "File_IO.h"
32 cycrow 86
#include "zlib/zlib.h"
1 cycrow 87
//#include "x2bc/x2bc_common/bob_dom.h"
88
//#include "x2bc/x2bc_common/bob_realfile_stream.h"
89
 
90
// compression type
91
enum { SPKCOMPRESS_NONE, SPKCOMPRESS_ZLIB, SPKCOMPRESS_7ZIP, SPKCOMPRESS_LZMA, SPKCOMPRESS_BEST };
92
// file type
32 cycrow 93
typedef enum {
94
	FILETYPE_SCRIPT,
95
	FILETYPE_TEXT,
96
	FILETYPE_README,
97
	FILETYPE_MAP,
98
	FILETYPE_MOD,
99
	FILETYPE_UNINSTALL,
100
	FILETYPE_SOUND,
101
	FILETYPE_EXTRA,
102
	FILETYPE_SCREEN,
103
	FILETYPE_MISSION,
104
	FILETYPE_ADVERT,
105
	FILETYPE_SHIPOTHER,
106
	FILETYPE_SHIPMODEL,
107
	FILETYPE_SHIPSCENE,
108
	FILETYPE_COCKPITSCENE,
109
	FILETYPE_MAX,
110
	FILETYPE_BACKUP 
111
} FileType;
112
 
18 cycrow 113
// special file types used internally
114
enum {
115
	FILETYPE_SCRIPT_UNINSTALL		= 1000,
116
};
1 cycrow 117
// error
118
enum {SPKERR_NONE, SPKERR_MALLOC, SPKERR_FILEOPEN, SPKERR_FILEREAD, SPKERR_UNCOMPRESS, SPKERR_WRITEFILE, SPKERR_CREATEDIRECTORY, SPKERR_FILEMISMATCH};
119
enum {STATUS_NONE, STATUS_COMPRESS, STATUS_WRITE};
120
 
121
 
122
bool IsDataPCK ( const unsigned char *data, size_t size );
123
unsigned char SPKEXPORT *UnPCKData ( unsigned char *data, size_t datasize, size_t *len, bool nocrypt );
124
unsigned char SPKEXPORT *UnPCKFile ( const char *file, size_t *len, bool nocrypt );
125
unsigned char SPKEXPORT *UnPCKData ( unsigned char *data, size_t datasize, size_t *len );
126
int ReadScriptVersionFromData ( unsigned char *data, long size );
127
bool ReadSignedFromData ( unsigned char *data, long size );
128
 
129
struct SMultiSpkFile;
130
 
131
class C_File;
132
class SPKEXPORT CProgressInfo
133
{
134
public:
135
	CProgressInfo () { m_bDoIn = false; m_lMaxSize = 0; m_bDoHalf = false; m_bSecondHalf = false; m_bDoSecond = false; m_iStatus = -1; m_iDone = 0;}
136
 
137
	void SetIn ( bool in ) { m_bDoIn = in; }
138
	void SetMax ( long max ) { m_lMaxSize = max; }
139
	void DoHalf() { m_bDoHalf = true; m_bSecondHalf = false; }
140
	void SecondHalf() { m_bSecondHalf = true; }
141
	void SwitchSecond() { m_bDoSecond = !m_bDoSecond; }
142
	bool IsSecond() { return m_bDoSecond; }
143
	void UpdateStatus(int i ) { m_iStatus = i; StatusUpdated(i); }
144
	int  GetStatus() { return m_iStatus; }
145
	void IncDone(int i) { m_iDone += i; }
146
	void UpdateProgress ( const long cur, const long max )
147
	{
148
		if ( !m_bDoSecond )
149
			ProgressUpdated(cur, max);
150
		else
151
			ProgressUpdated2(cur, max);
152
	}
153
	void UpdateFile(C_File *f) { DoingFile(f); }
154
	void UpdatePackage(SMultiSpkFile *f) { DoingPackage(f); }
155
	unsigned long *GetDonePointer() { return &m_iDone; }
156
	unsigned long GetDone() { return m_iDone; }
157
	void SetDone(int i) { m_iDone = i; }
158
 
159
protected:
160
	virtual void ProgressUpdated ( const long cur, const long max ) = 0;
161
	virtual void ProgressUpdated2 ( const long cur, const long max ) { };
162
	virtual void StatusUpdated(int i) { }
163
	virtual void DoingFile ( C_File *file ) = 0;
164
	virtual void DoingPackage ( SMultiSpkFile *file ) { }
165
 
166
	bool m_bDoIn;
167
	long m_lMaxSize;
168
	bool m_bDoHalf;
169
	bool m_bSecondHalf;
170
	bool m_bDoSecond;
171
	int	 m_iStatus;
172
 
173
	unsigned long m_iDone;
174
};
175
 
176
 
177
class SPKEXPORT CProgressInfoDone : public CProgressInfo
178
{
179
public:
180
	CProgressInfoDone () : CProgressInfo() { m_pOnFile = NULL; m_bDontDoMax = false; m_pOnPackage = NULL; }
181
 
182
	unsigned long GetMax() { return m_lMaxSize; }
183
	C_File *GetFile() { return m_pOnFile; }
184
	SMultiSpkFile *GetPackage() { return m_pOnPackage; }
185
 
186
	void DoMax() { m_bDontDoMax = false; }
187
	void DontDoMax() { m_bDontDoMax = true; }
188
 
189
protected:
190
	virtual void ProgressUpdated ( const long cur, const long max ) { m_iDone = cur; m_lMaxSize = max; }
191
	virtual void DoingFile ( C_File *file );
192
	virtual void DoingPackage ( SMultiSpkFile *package );
193
	virtual void StatusUpdated(int i) { if ( i == STATUS_WRITE ) this->DontDoMax(); }
194
 
195
	SMultiSpkFile	*m_pOnPackage;
196
	C_File			*m_pOnFile;
197
	bool			 m_bDontDoMax;
198
};
199
 
200
#include "ansi7zip/7Decoder.h"
201
 
202
#define GZ_FLAG_TEXT     1     // 0
203
#define GZ_FLAG_HCRC     2     // 1
204
#define GZ_FLAG_EXTRA    4     // 2
205
#define GZ_FLAG_FILENAME 8     // 3
206
#define GZ_FLAG_COMMENT  16    // 4
207
#define GZ_FLAG_RES1     32    // 5
208
#define GZ_FLAG_RES2     64    // 6
209
#define GZ_FLAG_RES3     128   // 7
210
 
211
class CProgressInfo2
212
{
213
public:
214
	virtual void ProgressPercent ( float percent ) = 0;
215
};
216
 
217
 
218
CyString SPKEXPORT GetFileTypeString ( int type );
219
int		 SPKEXPORT GetFileTypeFromString ( CyString type );
220
CyString SPKEXPORT FormatErrorString ( int error, CyString rest );
221
float	 SPKEXPORT GetLibraryVersion ();
222
float	 SPKEXPORT GetFileFormatVersion ();
223
 
224
class CBaseFile;
225
class SPKEXPORT C_File
226
{
227
public:
228
	static bool DoesTypeHaveExtraDir(int i)
229
	{
230
		switch (i)
231
		{
232
			case FILETYPE_EXTRA:
233
			case FILETYPE_SHIPSCENE:
234
			case FILETYPE_COCKPITSCENE:
235
			case FILETYPE_SHIPMODEL:
236
			case FILETYPE_SHIPOTHER:
237
				return true;
238
		}
239
		return false;
240
	}
241
	void ClearUsed () { m_iUsed = 0; }
242
	void IncUsed () { ++m_iUsed; }
243
	void DeleteData ();
244
	// varible setting functions
245
	void SetFileType ( int t ) { m_iFileType = t; }
246
	void SetFilename ( CyString filename );
247
	void SetName ( CyString name ) { m_sName = name; }
248
	void SetDir ( CyString dir ) { m_sDir = dir; }
249
	void SetCreationTime ( time_t time ) { m_tTime = time; }
250
	void SetFileSize ( long size ) { m_lSize = size; }
251
	void SetDataSize ( long size ) { m_lDataSize = size; }
252
	void SetUncompressedDataSize ( long size ) { m_lUncomprDataSize = size; }
253
	void SetDataCompression ( int c ) { m_iDataCompression = c; }
254
	void SetShared ( bool b ) { m_bShared = b; }
255
	void SetSigned ( bool b ) { m_bSigned = b; }
256
	void SetFullDir ( CyString dir ) { m_sFullDir = dir; }
257
	void SetInMod ( CyString s ) { m_sInMod = s; }
258
	void SetCompressedToFile ( bool b ) { m_bCompressedToFile = b; }
259
	void SetData(const unsigned char *data, size_t size) { m_sData = (unsigned char *)data, m_lDataSize = (long)size; }
43 cycrow 260
	void copyData(const unsigned char *data, size_t size);
1 cycrow 261
 
262
	// get functions
263
	int GetUsed () { return m_iUsed; }
264
	int GetLastError () { return m_iLastError; }
265
	int GetFileType () { return m_iFileType; }
266
	CyString GetFilename () { return m_sName; }
267
	CyString GetFullFilename () { return m_sFullDir + "/" + m_sName; }
268
	long GetSize () { return m_lSize; }
269
	time_t GetCreationTime () { return m_tTime; }
270
	CyString GetName () { return m_sName; }
271
	CyString GetDir() { return m_sDir; }
272
	CyString GetDataSizeString ();
273
	CyString GetUncompressedSizeString ();
274
	CyString GetDirectory ( CBaseFile *spkfile );
275
	int GetVersion () { return m_iVersion; }
276
	time_t GetLastModified() { return m_tTime; }
277
	CyString GetFileTypeString () { return ::GetFileTypeString ( m_iFileType ); }
278
	CyString GetCreationTimeString ();
279
	CyString GetTempFile () { return m_sTmpFile; }
280
	CyString GetFullDir () { return m_sFullDir; }
281
	CyString GetBaseName ();
282
	CyString GetOriginalName() { return m_sOriginalName; }
283
	CyString GetInMod() { return m_sInMod; }
284
	CyString GetSignature() { if ( !m_bUpdatedSignature ) UpdateSignature(); return m_sSignature; }
285
	void UpdateSignature();
286
	void ChangeBaseName(CyString b) { m_sName = b + "." + this->GetFileExt(); }
287
	bool RenameScript(CyString baseName);
288
	bool IsInMod() { return !m_sInMod.Empty(); }
289
 
290
	void FixOriginalName() { m_sOriginalName = m_sName; }
291
	void SetOriginalName(CyString name) { m_sOriginalName = name; }
292
 
293
	bool IsFakePatch ();
294
	bool IsAutoTextFile();
295
	int  GetTextFileID(CyString name = NullString);
296
	bool IsCompressedToFile () { return m_bCompressedToFile; }
297
	bool CompareNew ( C_File *file );
298
	CyString GetFileExt () { return m_sName.GetToken ( m_sName.NumToken('.'), '.' ); }
299
	bool CheckFileExt ( const CyString &ext ) { if ( GetFileExt().Compare(ext) ) return true; return false; }
300
	CyString ChangeFileExt(CyString ext);
301
	bool CheckPackedExtension();
302
 
303
	int GetCompressionType () { return m_iDataCompression; }
304
	long GetDataSize () { return m_lDataSize; }
305
	// returns the Uncompressed Data size, based on how the file is loaded, ie view data, view pointer, etc
306
	long GetUncompressedDataSize ()
307
	{
308
		if ( m_lUncomprDataSize )
309
			return m_lUncomprDataSize;
310
		if ( m_lSize )
311
			return m_lSize;
312
		return m_lDataSize;
313
	}
314
	CyString GetNameDirectory ( CBaseFile *spkfile );
315
 
316
	int  GetGame() { return m_iGame; }
317
	void SetGame(int i) { m_iGame = i; }
318
 
319
	unsigned char *GetData () { return m_sData; }
320
	bool IsShared () { return m_bShared; }
321
	bool IsSigned () { return m_bSigned; }
322
	bool UpdateSigned();
323
	bool ReadSignedFile();
324
 
325
	// file reading functions
326
	long ReadFileSize ();
327
	time_t ReadLastModified ();
328
	bool ReadFromFile (CyString filename);
329
	bool ReadFromFile ();
330
	bool ReadFromFile ( FILE *id, long size, bool = true );
52 cycrow 331
	bool readFromFile(CFileIO &File, long lSize, bool bDoSize = true );
332
	bool readFromFile(std::fstream &stream, long lSize, bool bDoSize = true );
1 cycrow 333
	bool ReadFromData ( char *data, long size );
334
	int ReadScriptVersion ();
335
	CyString GetFilePointer ();
336
 
337
	// file writing functions
338
	bool WriteToFile ( CyString filename, unsigned char * = NULL, long = 0 );
339
	bool WriteToDir ( CyString &dir, CBaseFile *, bool = true, CyString appendDir = NullString, unsigned char * = NULL, long = 0 );
340
	bool WriteFilePointer ( unsigned char *cData = NULL, long len = 0 );
341
 
342
	// file compression functions
343
	unsigned char *CompressToData(int compressionType, unsigned long *outSize, CProgressInfo *progress = NULL, int level = DEFAULT_COMPRESSION_LEVEL);
344
	bool UncompressToFile ( CyString, CBaseFile *, bool = true, CProgressInfo * = NULL );
345
	bool ChangeCompression(int compresstyp, CProgressInfo *progress);
346
	bool CompressData ( int compressionType, CProgressInfo * = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
347
	bool CompressFile ( CProgressInfo * = NULL );
348
	bool UncompressData ( CProgressInfo * = NULL );
349
	unsigned char *UncompressData ( long *size, CProgressInfo * = NULL );
350
 
351
	void CopyData(C_File *, bool includeData = true);
352
 
353
	bool IsDisabled () { return m_bDisabled; }
354
	void SetDisabled ( bool b ) { m_bDisabled = b; }
355
 
356
	void MarkSkip () { m_bSkip = true; }
357
	bool Skip () { return m_bSkip; }
358
	bool CheckPCK ();
359
	bool CheckValidFilePointer ();
360
	unsigned char *UnPCKFile ( size_t *len );
361
	bool MatchFile ( C_File *file );
362
	bool UnPCKFile ();
363
	bool PCKFile();
364
 
365
	void SetPos(int i)	{ m_iPos = i; }
366
	int  GetPos()		{ return m_iPos; }
367
 
368
	bool BobDecompile();
369
	bool BodCompile();
370
 
371
	// contructor/decontructor
372
	C_File();
373
	C_File ( CyString filename );
374
	C_File ( const char *filename );
375
	virtual ~C_File();
376
 
377
protected:
378
	static int m_iTempNum; // Used when creating temp files, allows new tmp files without overrighing used ones
379
 
380
	// private functions
381
	void Reset();
382
	CyString GetFullFileToDir ( CyString dir, bool includedir, CBaseFile *file );
383
 
384
	CyString  m_sName; // just the filename
385
	CyString  m_sDir;  // the extra dir (only for extras)
386
	CyString  m_sInMod; // in a mod files
387
	CyString  m_sSignature;
388
 
389
	// file pointer varibles
390
	CyString  m_sFullDir; // the full dir path of the file (This is used for file pointers when not loaded data in file)
391
	long    m_lSize;  // size of current file pointer
392
 
393
	// Main file varibles
394
	int		m_iVersion;  // used when reading script versions
395
	bool	m_bSigned;   // signed status of file, installer only, read from file
396
	bool    m_bShared;   // is file a marked shared file (Not used much anymore)
397
	int		m_iUsed;     // used by installer, number of plugins that uses the file
398
	time_t	m_tTime;     // Creation time of the file
399
 
400
	// File data varibles
401
	long	m_lDataSize;  // size of the data stream in what ever compression is set
402
	long	m_lUncomprDataSize; // size of stream if it was uncompressed
403
	unsigned char   *m_sData;  // stores the file data if loaded in memory
404
	int		m_iDataCompression; // the current compression of m_sData
405
 
406
	int     m_iFileType; // type if file, ie Script, Text, etc
407
 
408
	bool    m_bUsedMalloc; // malloc type of m_sData, so it can use with free() or delete
409
	CyString	m_sTmpFile;
410
 
411
	bool	m_bCompressedToFile;
412
 
413
	bool    m_bSkip;
414
	bool	m_bLoaded;
415
	bool	m_bDisabled;	// if the file has be disabled
416
	bool	m_bUpdatedSignature;
417
 
418
	int		m_iPos;
419
	int		m_iGame;
420
 
421
	CyString	m_sOriginalName;
422
 
423
	int m_iLastError;
424
	bool	m_bDontDeleteData; // fix for bad data deleteion
425
};
426
 
427
#endif // !defined(AFX_FILE_H__A0C15B81_4FD1_40D7_8EE8_2ECF5824BB8B__INCLUDED_)