Subversion Repositories spk

Rev

Rev 166 | Rev 171 | 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
 
170 cycrow 60
enum BaseFileType {TYPE_BASE, TYPE_SPK, TYPE_XSP, TYPE_ARCHIVE};
6 cycrow 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 {
170 cycrow 72
	SSPKHeader2 () : iFileCompression(0), iDataCompression(0), iNumFiles(0), lSize(0), lFullSize(0) { }
6 cycrow 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
 
170 cycrow 90
	// Virtual Functions
91
	virtual Utils::String getFullPackageName(int language, const Utils::String &byString) const;
92
	virtual Utils::String getFullPackageName(int language, bool includeVersion = true, const Utils::String &byString = "by") const;
93
	virtual Utils::String getFullPackageName(const Utils::String &format, int lang) const;
94
 
95
	virtual Utils::String CreateValuesLine() const;
96
 
97
	virtual bool LoadPackageData(const Utils::String& sFirst, const Utils::String& sRest, const Utils::String& sMainGame, Utils::CStringList& otherGames, Utils::CStringList& gameAddons, CProgressInfo* progress);
98
	virtual bool GeneratePackagerScript(bool wildcard, Utils::CStringList* list, int game, const Utils::CStringList& gameAddons, bool datafile = false);
99
	virtual bool GeneratePackagerScriptFile(bool wildcard, Utils::CStringList* list, int game, const Utils::CStringList& gameAddons);
100
 
101
	// Getters
102
	Utils::String getNameValidFile() const;
103
	const CLinkList<C_File> &fileList() const;
104
	CLinkList<C_File>& fileList(FileType type, CLinkList<C_File> &list) const;
105
	CLinkList<C_File>& fileList(FileType type);
6 cycrow 106
	CLinkList<C_File> *GetFileList() { return &m_lFiles; }
170 cycrow 107
	C_File *icon() const { return m_pIconFile; }
158 cycrow 108
	const Utils::String &iconExt() const { return _sIconExt; }
170 cycrow 109
	int dataCompression () const { return m_SHeader2.iDataCompression; }
110
	float fileVersion () const { return m_SHeader.fVersion; }
111
	size_t fileSize() const;
112
	const Utils::String& getLanguageName(int lang) const;
113
	Utils::String getAutosaveName() const;
114
	bool IsMod();
115
	bool IsFakePatch() const;
116
 
117
	// Setters
118
	void SetAutosaveName() { this->setFilename(getAutosaveName()); }
119
	void addWebMirror(const Utils::String& str);
120
	void removeWebMirror(const Utils::String& str);
121
	void SetDataCompression(int c) { m_SHeader2.iDataCompression = c; }
122
	void SetFileCompression(int c) { m_SHeader2.iFileCompression = c; }
123
	void SetValueCompression(int c) { m_SHeader.iValueCompression = c; }
124
	void setIcon(C_File* file, const Utils::String& ext) { if (m_pIconFile) delete m_pIconFile; _sIconExt = ext.c_str(); m_pIconFile = file; _changed(); }
125
	void setFtpAddr(const Utils::String& str) { _sFtpAddr = str; }
126
 
127
	// Game Compatability
128
	SGameCompat* GetGameCompatability(int game);
6 cycrow 129
	bool RemoveGameCompatability(int game);
46 cycrow 130
	void AddGameCompatability(int game, const Utils::String &version);
6 cycrow 131
	bool CheckGameCompatability(int game);
170 cycrow 132
	bool checkGameVersionCompatability(int game, const Utils::String &sVersion, int iVersion) const;
6 cycrow 133
	bool AnyGameCompatability() { return !m_lGames.empty(); }
134
 
170 cycrow 135
	// Web Mirrors
162 cycrow 136
	bool anyWebMirrors() { return !_lMirrors.empty(); }
170 cycrow 137
	const Utils::CStringList &webMirrors() const { return _lMirrors; }
162 cycrow 138
	int getMaxWebMirrors() { return _lMirrors.size(); }
139
	Utils::String getWebMirror(size_t i) { if ( i >= 0 && i < _lMirrors.size() ) return _lMirrors.get(i)->str; return Utils::String::Null(); }
140
	void clearWebMirrors() { _lMirrors.clear(); }
6 cycrow 141
 
170 cycrow 142
	// Files
13 cycrow 143
	C_File *GetFirstFile(int type) const;
144
	C_File *GetNextFile(C_File *prev) const;
145
	C_File *GetPrevFile(C_File *next) const;
170 cycrow 146
	C_File* findMatchingMod(C_File* f) const;
6 cycrow 147
 
170 cycrow 148
	void convertNormalMod(C_File *f, const Utils::String &to) const;
149
	void convertFakePatch(C_File *f) const;
150
	void convertAutoText(C_File *f) const;
151
	void renameFile(C_File *f, const Utils::String &baseName) const;
6 cycrow 152
 
170 cycrow 153
	size_t countFiles(FileType filetype) const;
154
	C_File* findFileAt(FileType filetype, size_t pos) const;
155
	C_File* findFile(const Utils::String& file, FileType type, const Utils::String& dir = Utils::String::Null(), int game = 0) const;
6 cycrow 156
 
155 cycrow 157
	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);
170 cycrow 158
	void AddFile(C_File* file);
159
	C_File* addFile(const Utils::String& file, const Utils::String& dir, FileType type, int game = 0, bool packed = false);
160
	C_File* appendFile(const Utils::String& file, int type, int game, bool packed, const Utils::String& dir = Utils::String::Null(), CProgressInfo* progress = NULL);
161
	bool addFileNow(const Utils::String&, const Utils::String&, FileType type, CProgressInfo* progress = NULL);
162
	virtual bool RemoveFile(int pos);
163
	virtual bool RemoveFile(C_File* files);
164
	virtual bool removeFile(const Utils::String &file, FileType type, const Utils::String &dir = Utils::String::Null(), int game = 0);
165
	virtual bool RemoveFile(CyString file, int type, CyString dir = NullString, int game = 0) { return removeFile(file.ToString(), static_cast<FileType>(type), dir.ToString(), game); }
166
	void removeAllFiles(FileType type, int game);
6 cycrow 167
 
170 cycrow 168
	Utils::String createFilesLine(bool updateheader, CProgressInfo* = NULL);
6 cycrow 169
 
170
	// error handling
134 cycrow 171
	void ClearError () { _sLastError = ""; _iLastError = SPKERR_NONE; }
172
	int lastError() const { return _iLastError; }
173
	const Utils::String lastErrorString() const { return _sLastError; }
6 cycrow 174
 
52 cycrow 175
	virtual bool WriteHeader(CFileIO &file, int iHeader, int iLength);
176
	virtual bool WriteData(CFileIO &file, CProgressInfo * = NULL );
6 cycrow 177
	virtual bool WriteFile ( CyString filename, CProgressInfo * = NULL );
130 cycrow 178
	virtual bool ReadFile(CyString filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
179
	virtual bool readFile(const Utils::String &filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
58 cycrow 180
	bool readFile(CFileIO &File, int readtype, CProgressInfo *progress);
6 cycrow 181
 
131 cycrow 182
	virtual bool ExtractFile(C_File *file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
183
	virtual bool ExtractFile(int file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
184
	virtual bool extractFile(int file, const Utils::String &dir, bool includedir = true, CProgressInfo *progress = NULL);
185
	virtual bool extractFile(C_File *file, const Utils::String &dir, bool includedir = true, CProgressInfo *progress = NULL);
186
	virtual bool extractFile(int filenum, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
187
	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 188
	virtual bool extractAll(const Utils::String &dir, int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
6 cycrow 189
 
126 cycrow 190
	virtual bool SaveToArchive(CyString filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
131 cycrow 191
	virtual bool saveToArchive(const Utils::String &filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
109 cycrow 192
	virtual void addGeneratedFiles(HZIP &hz) {};
193
 
6 cycrow 194
	void ClearFileData();
195
 
196
	// reading files
197
	void ReadAllFilesToMemory ();
198
	void ReadIconFileToMemory ();
199
	bool ReadFileToMemory(C_File *f);
200
 
201
	// compression
202
	void RecompressAllFiles ( int compresstype, CProgressInfo *progress );
47 cycrow 203
	void CompressAllFiles ( int compresstype, CProgressInfo *progress = NULL, CProgressInfo *overallProgress = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
6 cycrow 204
	bool UncompressAllFiles ( CProgressInfo * = NULL );
205
 
206
	// static functions
207
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
208
	static int CheckFile ( CyString filename, float *version = NULL );
209
 
210
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
211
 
212
	// installing
213
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
214
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
215
	virtual bool IsPatch () { return false; }
216
 
217
	// installer functions
218
	bool IsProfileEnabled () { return m_bProfile; }
219
	bool IsEnabled () { return m_bEnable; }
220
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
221
	bool IsGlobalEnabled () { return m_bGlobal; }
222
 
223
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
224
	void SetEnabled ( bool en ) { m_bEnable = en; }
225
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
226
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
227
 
228
	int  GetLoadError() { return m_iLoadError; }
229
	void SetLoadError(int i) { m_iLoadError = i; }
134 cycrow 230
	Utils::String createUpdateFile(const Utils::String &dir) const;
6 cycrow 231
 
14 cycrow 232
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 233
 
234
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
235
 
170 cycrow 236
	Utils::String fileSizeString() const;
6 cycrow 237
 
238
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 239
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
240
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
241
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
242
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 243
	void ClearNeededPackages();
244
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
245
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
160 cycrow 246
	void removeFakePatchOrder(bool after, const Utils::String &scriptName, const Utils::String &author);
247
	void removeFakePatchOrder(const Utils::String &scriptName, const Utils::String &author);
248
	void addFakePatchOrder(bool after, const Utils::String &scriptName, const Utils::String &author);
249
	bool anyFakePatchOrder() const { if ( !_lFakePatchBefore.empty() || !_lFakePatchAfter.empty() ) return true; return false; }
250
	const Utils::CStringList &getFakePatchBeforeOrder() const { return _lFakePatchBefore; }
251
	const Utils::CStringList &getFakePatchAfterOrder() const { return _lFakePatchAfter; }
88 cycrow 252
	void updateTextDB() { this->_resetTextDB(); }
6 cycrow 253
 
88 cycrow 254
	virtual bool readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares);
89 cycrow 255
	virtual bool readCommands(int iLang, CLinkList<SCommandSlot> &list);
256
	virtual bool readWingCommands(int iLang, CLinkList<SCommandSlot> &list);
88 cycrow 257
 
6 cycrow 258
	int  FindFirstGameInPackage();
259
	bool IsAnyGameInPackage();
260
	bool IsMultipleGamesInPackage();
261
	bool IsGameInPackage(int game);
262
 
170 cycrow 263
	virtual BaseFileType type () const { return BaseFileType::TYPE_BASE; }
264
	virtual int GetType() { return type(); }
6 cycrow 265
	bool AnyFileType ( int type );
266
	CBaseFile *GetParent () { return m_pParent; }
267
	void SetParent ( CBaseFile *file ) { m_pParent = file; }
48 cycrow 268
	int ParseLanguage(const Utils::String &lang) const;
6 cycrow 269
 
270
	virtual bool UpdateSigned (bool updateFiles);
271
	int GetNum() { return m_iNum; }
272
	void SetNum(int i) { m_iNum = i; }
273
 
274
	bool	IsFullyLoaded() { return m_bFullyLoaded; }
275
	virtual bool   IsSigned ()		{ return m_bSigned;}
276
	void SetOverrideFiles(bool b) { m_bOverrideFiles = b; }
277
	bool IsUpdateChecked () { return m_bUpdate; }
278
	void SetUpdateChecked ( bool en ) { m_bUpdate = en; }
279
 
280
	unsigned char *CreateData(size_t *size, CProgressInfo *progress = NULL);
281
 
282
protected:
283
	virtual void Delete ();
284
	virtual void SetDefaults ();
285
 
286
	// reading of files
14 cycrow 287
	virtual bool CheckHeader(const Utils::String header) const;
6 cycrow 288
	virtual bool ParseHeader ( CyString header );
289
	virtual bool ParseFileHeader ( CyString header );
290
	virtual bool ParseFilesLine ( CyString line );
291
	virtual void ReadValues ( CyString values );
292
	virtual void ReadFiles ( CyString values );
293
 
14 cycrow 294
	void _install_adjustFakePatches(CPackages *pPackages);
43 cycrow 295
	void _install_renameText(CPackages *pPackages);
50 cycrow 296
	bool _install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile);
297
	bool _install_setEnabled(bool bEnable, C_File *fit);
298
	bool _install_checkVersion(C_File *pFile, const Utils::String &sDestination);
299
	Utils::String _install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination);
300
	C_File *_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList);
301
	bool _install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr);
51 cycrow 302
	bool _install_createDirectory(CDirIO &Dir, const Utils::String &sTo, C_File *pFile, CyStringList *errorStr);
303
	void _install_writeFile(C_File *pFile, const Utils::String &sDestination, CyStringList *errorStr);
14 cycrow 304
 
109 cycrow 305
	int _read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress);
306
	int _read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress);
58 cycrow 307
	CFileIO *_startRead();
51 cycrow 308
 
88 cycrow 309
	void _addFile(C_File *file, bool dontChange = false);
310
	void _updateTextDB(C_File *file);
311
	void _resetTextDB();
312
	void _addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType);
89 cycrow 313
	bool _readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list);
131 cycrow 314
	Utils::String _replaceFilename(const Utils::String &fname);
88 cycrow 315
 
316
protected:
6 cycrow 317
	SSPKHeader m_SHeader;
318
	SSPKHeader2 m_SHeader2;
319
 
320
	C_File *m_pIconFile;
158 cycrow 321
	Utils::String _sIconExt;
134 cycrow 322
	Utils::String _sLastError;
323
	int _iLastError;
6 cycrow 324
 
325
	CLinkList<C_File>  m_lFiles;
170 cycrow 326
	CLinkList<C_File>  m_lTempFiles;
162 cycrow 327
	Utils::CStringList _lMirrors;			//TODO: move to CorePackage
160 cycrow 328
	Utils::CStringList _lFakePatchBefore;
329
	Utils::CStringList _lFakePatchAfter;
6 cycrow 330
 
88 cycrow 331
	CTextDB	*_pTextDB;
332
 
6 cycrow 333
	CLinkList<SGameCompat> m_lGames;
334
 
335
	bool m_bSigned;
336
	bool m_bFullyLoaded;
337
 
338
	//installer varibles
339
	bool m_bEnable;
340
	bool m_bModifiedEnabled;
341
	bool m_bGlobal;
342
	bool m_bProfile;
343
	int m_iLoadError;
344
 
345
	CBaseFile *m_pParent;
346
 
347
	int m_iNum;
348
 
349
	CLinkList<SNeededLibrary> m_lNeededLibrarys;
350
 
130 cycrow 351
	bool	_bCombineFiles;
6 cycrow 352
	bool	m_bOverrideFiles;
170 cycrow 353
	Utils::String	_sFtpAddr;
354
	bool			m_bAutoGenerateUpdateFile;
355
	bool			m_bUpdate;
6 cycrow 356
};
357
 
358
 
359
class SPKEXPORT CArchiveFile : public CBaseFile
360
{
361
public:
362
	CArchiveFile();
363
	virtual ~CArchiveFile();
170 cycrow 364
	virtual Utils::String getFullPackageName(const Utils::String& format, int lang) const override { return "Archive(" + name() + ")"; }
365
	virtual Utils::String getFullPackageName(int language, const Utils::String& byString) const override { return "Archive(" + name() + ")"; }
366
	virtual Utils::String getFullPackageName(int language, bool includeVersion = true, const Utils::String& byString = "by") const override { return "Archive(" + name() + ")"; }
367
 
368
	virtual BaseFileType type() const override { return BaseFileType::TYPE_ARCHIVE; }
6 cycrow 369
};
370
 
371
#endif //__BASEFILE_H__
372