Subversion Repositories spk

Rev

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