Subversion Repositories spk

Rev

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

Rev Author Line No. Line
6 cycrow 1
// BaseFile.h: interface for the CBaseFile class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
 
5
#ifndef __BASEFILE_H__
6
#define __BASEFILE_H__
7
 
8
#if _MSC_VER > 1000
9
#pragma once
10
#endif // _MSC_VER > 1000
11
 
12
#include "lists.h"
13
#include "StringList.h"
14
#include "File.h"
8 cycrow 15
#include "Utils/String.h"
88 cycrow 16
#include "Utils/StringList.h"
109 cycrow 17
#include "archive/zip.h"
6 cycrow 18
 
46 cycrow 19
#include "Package/CorePackage.h"
20
 
6 cycrow 21
#define GAME_ALL			0
22
#define GAME_X2				1
23
#define GAME_X3				2
24
#define GAME_X3TC			3
25
#define GAME_X3AP			4
126 cycrow 26
#define GAME_X3FL			5
127 cycrow 27
#define GAME_MAX			(GAME_X3FL)
28
#define GAME_ALLNEW			(1 << 31)
6 cycrow 29
 
127 cycrow 30
#define FILEVERSION 4.30f
20 cycrow 31
 
6 cycrow 32
#define WRITECHUNK		50000
33
 
34
class CPackages;
126 cycrow 35
class CGameExe;
6 cycrow 36
 
88 cycrow 37
namespace SPK {
38
	class CTextDB;
39
}
40
 
41
using namespace SPK;
42
 
6 cycrow 43
enum {
44
	READFLAG_NONE = 0,
45
	READFLAG_NOUNCOMPRESS = 1
46
};
47
 
48
enum {SPKFILE_INVALID, SPKFILE_SINGLE, SPKFILE_MULTI, SPKFILE_SINGLESHIP, SPKFILE_OLD, SPKFILE_BASE};
49
enum {SPKREAD_ALL, SPKREAD_NODATA, SPKREAD_VALUES, SPKREAD_HEADER};
50
enum {DELETESTATE_NONE, DELETESTATE_WAITING, DELETESTATE_DONE};
51
enum {SPKINSTALL_CREATEDIRECTORY, SPKINSTALL_CREATEDIRECTORY_FAIL, SPKINSTALL_WRITEFILE, SPKINSTALL_WRITEFILE_FAIL, SPKINSTALL_DELETEFILE, SPKINSTALL_DELETEFILE_FAIL,
52
		SPKINSTALL_SKIPFILE, SPKINSTALL_REMOVEDIR, SPKINSTALL_ENABLEFILE, SPKINSTALL_DISABLEFILE, SPKINSTALL_ENABLEFILE_FAIL, SPKINSTALL_DISABLEFILE_FAIL,
53
		SPKINSTALL_UNINSTALL_MOVE, SPKINSTALL_UNINSTALL_COPY, SPKINSTALL_UNINSTALL_MOVE_FAIL, SPKINSTALL_UNINSTALL_COPY_FAIL, SPKINSTALL_UNINSTALL_REMOVE, SPKINSTALL_UNINSTALL_REMOVE_FAIL,
54
		SPKINSTALL_ORIGINAL_BACKUP, SPKINSTALL_ORIGINAL_RESTORE, SPKINSTALL_ORIGINAL_BACKUP_FAIL, SPKINSTALL_ORIGINAL_RESTORE_FAIL, SPKINSTALL_FAKEPATCH, SPKINSTALL_FAKEPATCH_FAIL, 
55
		SPKINSTALL_MISSINGFILE, SPKINSTALL_SHARED, SPKINSTALL_SHARED_FAIL, SPKINSTALL_ORPHANED, SPKINSTALL_ORPHANED_FAIL, SPKINSTALL_UNCOMPRESS_FAIL, SPKINSTALL_AUTOTEXT, SPKINSTALL_AUTOTEXT_FAIL
56
};
57
 
58
enum {PACKAGETYPE_NORMAL, PACKAGETYPE_LIBRARY, PACKAGETYPE_CUSTOMSTART, PACKAGETYPE_PATCH, PACKAGETYPE_UPDATE, PACKAGETYPE_MOD, PACKAGETYPE_SHIP, PACKAGETYPE_FAKEPATCH};
59
 
60
enum {TYPE_BASE, TYPE_SPK, TYPE_XSP, TYPE_ARCHIVE};
61
 
62
// spk header struct
63
tstruct SSPKHeader {
64
	SSPKHeader () { fVersion = 0; iValueCompression = lValueCompressSize = 0; }
65
	float fVersion;
66
	int iValueCompression;
67
	unsigned long lValueCompressSize;
68
} SSPKHeader;
69
 
70
 
71
tstruct SSPKHeader2 {
72
	SSPKHeader2 () { iNumFiles = 0; lSize = 0; lFullSize = 0; }
73
	int iNumFiles;
74
	long lSize;
75
	long lFullSize;
76
	int iFileCompression;
77
	int iDataCompression;
78
} SSPKHeader2;
79
 
46 cycrow 80
class SPKEXPORT CBaseFile : public SPK::Package::CCorePackage
6 cycrow 81
{
82
public:
13 cycrow 83
	static Utils::String ConvertGameToString(int game);
84
	static int GetGameFromString(const Utils::String &game);
134 cycrow 85
	static Utils::String ErrorString(int error, const Utils::String &errorStr = Utils::String::Null());
6 cycrow 86
 
87
	CBaseFile();
88
	virtual ~CBaseFile();
89
 
90
	// Get basic Settings
91
	virtual CyString GetFullPackageName(int language, CyString byString) { return GetFullPackageName(language, true, byString); }
92
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") 
93
	{ 
94
		CyString p;
95
		if ( language == -1 )
50 cycrow 96
			p = this->name();
6 cycrow 97
		else
98
			p = GetLanguageName(language);
99
		if ( includeVersion )
100
		{
101
			p += " V";
50 cycrow 102
			p += this->version();
6 cycrow 103
		}
104
		p += " ";
50 cycrow 105
		p += byString + " " + this->author();
6 cycrow 106
		return p;
107
	}
108
	CLinkList<C_File> *GetFileList() { return &m_lFiles; }
109
	C_File *GetIcon () { return m_pIconFile; }
158 cycrow 110
	const Utils::String &iconExt() const { return _sIconExt; }
130 cycrow 111
	Utils::String getNameValidFile() const;
6 cycrow 112
	int GetDataCompression () { return m_SHeader2.iDataCompression; }
113
	float GetFileVersion () { return m_SHeader.fVersion; }
114
	long GetFullFileSize ();
158 cycrow 115
	CyString GetLanguageName(int lang) const;
116
	const Utils::String &getLanguageName(int lang) const;
6 cycrow 117
	virtual CyString GetFullPackageName(CyString format, int lang);
50 cycrow 118
	Utils::String GetAutosaveName();
119
	void SetAutosaveName() { this->setFilename(GetAutosaveName()); }
6 cycrow 120
	SGameCompat *GetGameCompatability(int game);
121
	bool RemoveGameCompatability(int game);
46 cycrow 122
	void AddGameCompatability(int game, const Utils::String &version);
6 cycrow 123
	bool CheckGameCompatability(int game);
124
	bool CheckGameVersionCompatability(int game, CyString sVersion, int iVersion);
125
	bool AnyGameCompatability() { return !m_lGames.empty(); }
126
 
162 cycrow 127
	bool anyWebMirrors() { return !_lMirrors.empty(); }
128
	const Utils::CStringList *getWebMirrors() const { return &_lMirrors; }
129
	int getMaxWebMirrors() { return _lMirrors.size(); }
130
	Utils::String getWebMirror(size_t i) { if ( i >= 0 && i < _lMirrors.size() ) return _lMirrors.get(i)->str; return Utils::String::Null(); }
131
	void clearWebMirrors() { _lMirrors.clear(); }
6 cycrow 132
 
131 cycrow 133
	const CLinkList<C_File> *fileList() const;
53 cycrow 134
	CLinkList<C_File> *fileList(int type) const;
13 cycrow 135
	C_File *GetFirstFile(int type) const;
136
	C_File *GetNextFile(C_File *prev) const;
137
	C_File *GetPrevFile(C_File *next) const;
6 cycrow 138
 
14 cycrow 139
	virtual Utils::String CreateValuesLine () const;
6 cycrow 140
 
134 cycrow 141
	virtual bool LoadPackageData(const Utils::String &sFfirst, const Utils::String &sRest, const Utils::String &sMainGame, Utils::CStringList &otherGames, Utils::CStringList &gameAddons, CProgressInfo *progress);
127 cycrow 142
	virtual bool GeneratePackagerScript(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons, bool datafile = false);
143
	virtual bool GeneratePackagerScriptFile(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons);
6 cycrow 144
 
145
	void ConvertNormalMod(C_File *f, CyString to);
146
	void ConvertFakePatch(C_File *f);
147
	void ConvertAutoText(C_File *f);
148
	C_File *FindMatchingMod(C_File *f);
149
	void RenameFile(C_File *f, CyString baseName);
150
 
151
	// set basic settings
162 cycrow 152
	void addWebMirror		(const Utils::String &str);
153
	void removeWebMirror	(const Utils::String &str);
6 cycrow 154
	void SetDataCompression ( int c )       { m_SHeader2.iDataCompression = c; }
155
	void SetFileCompression ( int c )       { m_SHeader2.iFileCompression = c; }
156
	void SetValueCompression( int c )       { m_SHeader.iValueCompression = c; }
158 cycrow 157
	void SetIcon ( C_File *file, CyString ext ) { if ( m_pIconFile ) delete m_pIconFile; _sIconExt = ext.c_str(); m_pIconFile = file; _changed(); }
6 cycrow 158
	void SetFtpAddr			( CyString str ) { m_sFtpAddr = str; }
159
 
155 cycrow 160
	void AddFileScript(FileType filetype, bool shared, bool packed, const Utils::String &rest, const Utils::String &sMainGame, Utils::CStringList &otherGames, Utils::CStringList &gameAddons, CProgressInfo *progress = NULL);
6 cycrow 161
 
162
	bool IsMod();
13 cycrow 163
	bool IsFakePatch() const;
6 cycrow 164
 
165
	// error handling
134 cycrow 166
	void ClearError () { _sLastError = ""; _iLastError = SPKERR_NONE; }
167
	int lastError() const { return _iLastError; }
168
	const Utils::String lastErrorString() const { return _sLastError; }
6 cycrow 169
 
170
	// file handling
171
	void AddFile ( C_File *file );
127 cycrow 172
	C_File *AddFile(CyString, CyString, int type, int game = 0);
155 cycrow 173
	C_File *addFile(const Utils::String &file, const Utils::String &dir, FileType type, int game = 0, bool packed = false);
174
	C_File *appendFile(const Utils::String &file, int type, int game, bool packed, const Utils::String &dir = Utils::String::Null(), CProgressInfo *progress = NULL );
6 cycrow 175
	C_File *FindFile ( CyString, int, CyString = NullString, int game = 0 );
176
	bool AddFileNow ( CyString, CyString, int type, CProgressInfo *progress = NULL );
177
	int CountFiles ( int filetype );
178
	C_File *FindFileAt ( int filetype, int pos );
179
	virtual bool RemoveFile ( int pos );
180
	virtual bool RemoveFile ( C_File *files );
181
	virtual bool RemoveFile ( CyString file, int type, CyString dir = NullString, int game = 0 );
129 cycrow 182
	void removeAllFiles(FileType type, int game);
6 cycrow 183
	CyString CreateFilesLine ( bool updateheader, CProgressInfo * = NULL );
184
 
52 cycrow 185
	virtual bool WriteHeader(CFileIO &file, int iHeader, int iLength);
186
	virtual bool WriteData(CFileIO &file, CProgressInfo * = NULL );
6 cycrow 187
	virtual bool WriteFile ( CyString filename, CProgressInfo * = NULL );
130 cycrow 188
	virtual bool ReadFile(CyString filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
189
	virtual bool readFile(const Utils::String &filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
58 cycrow 190
	bool readFile(CFileIO &File, int readtype, CProgressInfo *progress);
6 cycrow 191
 
131 cycrow 192
	virtual bool ExtractFile(C_File *file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
193
	virtual bool ExtractFile(int file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
194
	virtual bool extractFile(int file, const Utils::String &dir, bool includedir = true, CProgressInfo *progress = NULL);
195
	virtual bool extractFile(C_File *file, const Utils::String &dir, bool includedir = true, CProgressInfo *progress = NULL);
196
	virtual bool extractFile(int filenum, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
197
	virtual bool extractFile(C_File *file, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
129 cycrow 198
	virtual bool extractAll(const Utils::String &dir, int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
6 cycrow 199
 
126 cycrow 200
	virtual bool SaveToArchive(CyString filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
131 cycrow 201
	virtual bool saveToArchive(const Utils::String &filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
109 cycrow 202
	virtual void addGeneratedFiles(HZIP &hz) {};
203
 
6 cycrow 204
	void ClearFileData();
205
 
206
	CLinkList<SNames> *GetNamesList() { return &m_lNames; }
207
 
208
	// reading files
209
	void ReadAllFilesToMemory ();
210
	void ReadIconFileToMemory ();
211
	bool ReadFileToMemory(C_File *f);
212
 
213
	// compression
214
	void RecompressAllFiles ( int compresstype, CProgressInfo *progress );
47 cycrow 215
	void CompressAllFiles ( int compresstype, CProgressInfo *progress = NULL, CProgressInfo *overallProgress = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
6 cycrow 216
	bool UncompressAllFiles ( CProgressInfo * = NULL );
217
 
218
	// static functions
219
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
220
	static int CheckFile ( CyString filename, float *version = NULL );
221
 
222
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
223
 
224
	// installing
225
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
226
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
227
	virtual bool IsPatch () { return false; }
228
 
229
	// installer functions
230
	bool IsProfileEnabled () { return m_bProfile; }
231
	bool IsEnabled () { return m_bEnable; }
232
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
233
	bool IsGlobalEnabled () { return m_bGlobal; }
234
 
235
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
236
	void SetEnabled ( bool en ) { m_bEnable = en; }
237
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
238
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
239
 
240
	int  GetLoadError() { return m_iLoadError; }
241
	void SetLoadError(int i) { m_iLoadError = i; }
134 cycrow 242
	Utils::String createUpdateFile(const Utils::String &dir) const;
6 cycrow 243
 
244
	// language functions
245
	void RemoveLanguageName ( int lang );
46 cycrow 246
	void AddLanguageName ( int lang, const Utils::String &name );
6 cycrow 247
	void ClearNames ();
248
 
14 cycrow 249
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 250
 
251
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
252
 
253
	CyString GetFullFileSizeString();
254
 
255
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 256
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
257
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
258
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
259
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 260
	void ClearNeededPackages();
261
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
262
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
160 cycrow 263
	void removeFakePatchOrder(bool after, const Utils::String &scriptName, const Utils::String &author);
264
	void removeFakePatchOrder(const Utils::String &scriptName, const Utils::String &author);
265
	void addFakePatchOrder(bool after, const Utils::String &scriptName, const Utils::String &author);
266
	bool anyFakePatchOrder() const { if ( !_lFakePatchBefore.empty() || !_lFakePatchAfter.empty() ) return true; return false; }
267
	const Utils::CStringList &getFakePatchBeforeOrder() const { return _lFakePatchBefore; }
268
	const Utils::CStringList &getFakePatchAfterOrder() const { return _lFakePatchAfter; }
88 cycrow 269
	void updateTextDB() { this->_resetTextDB(); }
6 cycrow 270
 
88 cycrow 271
	virtual bool readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares);
89 cycrow 272
	virtual bool readCommands(int iLang, CLinkList<SCommandSlot> &list);
273
	virtual bool readWingCommands(int iLang, CLinkList<SCommandSlot> &list);
88 cycrow 274
 
50 cycrow 275
//	bool IsChanged() { return m_bChanged; }
276
//	void SetChanged(bool b) { m_bChanged = b; }
6 cycrow 277
 
278
	int  FindFirstGameInPackage();
279
	bool IsAnyGameInPackage();
280
	bool IsMultipleGamesInPackage();
281
	bool IsGameInPackage(int game);
282
 
283
	virtual int GetType () { return TYPE_BASE; }
284
	bool AnyFileType ( int type );
285
	CBaseFile *GetParent () { return m_pParent; }
286
	void SetParent ( CBaseFile *file ) { m_pParent = file; }
48 cycrow 287
	int ParseLanguage(const Utils::String &lang) const;
6 cycrow 288
 
289
	virtual bool UpdateSigned (bool updateFiles);
290
	int GetNum() { return m_iNum; }
291
	void SetNum(int i) { m_iNum = i; }
292
 
293
	bool	IsFullyLoaded() { return m_bFullyLoaded; }
294
	virtual bool   IsSigned ()		{ return m_bSigned;}
295
	void SetOverrideFiles(bool b) { m_bOverrideFiles = b; }
296
	bool IsUpdateChecked () { return m_bUpdate; }
297
	void SetUpdateChecked ( bool en ) { m_bUpdate = en; }
298
 
299
	unsigned char *CreateData(size_t *size, CProgressInfo *progress = NULL);
300
 
301
protected:
302
	virtual void Delete ();
303
	virtual void SetDefaults ();
304
 
305
	// reading of files
14 cycrow 306
	virtual bool CheckHeader(const Utils::String header) const;
6 cycrow 307
	virtual bool ParseHeader ( CyString header );
308
	virtual bool ParseFileHeader ( CyString header );
309
	virtual bool ParseFilesLine ( CyString line );
310
	virtual void ReadValues ( CyString values );
311
	virtual void ReadFiles ( CyString values );
312
 
14 cycrow 313
	void _install_adjustFakePatches(CPackages *pPackages);
43 cycrow 314
	void _install_renameText(CPackages *pPackages);
50 cycrow 315
	bool _install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile);
316
	bool _install_setEnabled(bool bEnable, C_File *fit);
317
	bool _install_checkVersion(C_File *pFile, const Utils::String &sDestination);
318
	Utils::String _install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination);
319
	C_File *_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList);
320
	bool _install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr);
51 cycrow 321
	bool _install_createDirectory(CDirIO &Dir, const Utils::String &sTo, C_File *pFile, CyStringList *errorStr);
322
	void _install_writeFile(C_File *pFile, const Utils::String &sDestination, CyStringList *errorStr);
14 cycrow 323
 
109 cycrow 324
	int _read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress);
325
	int _read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress);
58 cycrow 326
	CFileIO *_startRead();
51 cycrow 327
 
88 cycrow 328
	void _addFile(C_File *file, bool dontChange = false);
329
	void _updateTextDB(C_File *file);
330
	void _resetTextDB();
331
	void _addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType);
89 cycrow 332
	bool _readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list);
131 cycrow 333
	Utils::String _replaceFilename(const Utils::String &fname);
88 cycrow 334
 
335
protected:
6 cycrow 336
	SSPKHeader m_SHeader;
337
	SSPKHeader2 m_SHeader2;
338
 
339
	C_File *m_pIconFile;
158 cycrow 340
	Utils::String _sIconExt;
134 cycrow 341
	Utils::String _sLastError;
342
	int _iLastError;
6 cycrow 343
 
344
	CLinkList<C_File>  m_lFiles;
48 cycrow 345
	CLinkList<SNames> m_lNames;			//TODO: move to CorePackage
162 cycrow 346
	Utils::CStringList _lMirrors;			//TODO: move to CorePackage
160 cycrow 347
	Utils::CStringList _lFakePatchBefore;
348
	Utils::CStringList _lFakePatchAfter;
6 cycrow 349
 
88 cycrow 350
	CTextDB	*_pTextDB;
351
 
6 cycrow 352
	CLinkList<SGameCompat> m_lGames;
353
 
354
	bool m_bSigned;
355
	bool m_bFullyLoaded;
356
 
357
	//installer varibles
358
	bool m_bEnable;
359
	bool m_bModifiedEnabled;
360
	bool m_bGlobal;
361
	bool m_bProfile;
362
	int m_iLoadError;
363
 
364
	CBaseFile *m_pParent;
365
 
366
	int m_iNum;
367
 
368
	CLinkList<SNeededLibrary> m_lNeededLibrarys;
369
 
130 cycrow 370
	bool	_bCombineFiles;
6 cycrow 371
	bool	m_bOverrideFiles;
372
	CyString	m_sFtpAddr;
373
	bool	m_bAutoGenerateUpdateFile;
374
	bool m_bUpdate;
375
};
376
 
377
 
378
class SPKEXPORT CArchiveFile : public CBaseFile
379
{
380
public:
381
	CArchiveFile();
382
	virtual ~CArchiveFile();
50 cycrow 383
	virtual CyString GetFullPackageName(CyString format, int lang) { return CyString("Archive(") + this->name() + ")"; }
384
	virtual CyString GetFullPackageName(int language, CyString byString) { return CyString("Archive(") + this->name() + ")"; }
385
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") { return CyString("Archive(") + this->name() + ")"; }
6 cycrow 386
	virtual int GetType () { return TYPE_ARCHIVE; }
387
};
388
 
389
#endif //__BASEFILE_H__
390