Subversion Repositories spk

Rev

Rev 171 | Rev 175 | 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;
171 cycrow 112
	//const Utils::String& getLanguageName(int lang) const { return name(lang); }
170 cycrow 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);
172 cycrow 162
	bool removeFile(size_t pos);
163
	bool removeFile(C_File* files);
164
	bool removeFile(const Utils::String &file, FileType type, const Utils::String &dir = Utils::String::Null(), int game = 0);
170 cycrow 165
	void removeAllFiles(FileType type, int game);
6 cycrow 166
 
170 cycrow 167
	Utils::String createFilesLine(bool updateheader, CProgressInfo* = NULL);
6 cycrow 168
 
169
	// error handling
134 cycrow 170
	void ClearError () { _sLastError = ""; _iLastError = SPKERR_NONE; }
171
	int lastError() const { return _iLastError; }
172
	const Utils::String lastErrorString() const { return _sLastError; }
6 cycrow 173
 
52 cycrow 174
	virtual bool WriteHeader(CFileIO &file, int iHeader, int iLength);
175
	virtual bool WriteData(CFileIO &file, CProgressInfo * = NULL );
6 cycrow 176
	virtual bool WriteFile ( CyString filename, CProgressInfo * = NULL );
130 cycrow 177
	virtual bool ReadFile(CyString filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
178
	virtual bool readFile(const Utils::String &filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
58 cycrow 179
	bool readFile(CFileIO &File, int readtype, CProgressInfo *progress);
6 cycrow 180
 
131 cycrow 181
	virtual bool ExtractFile(C_File *file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
182
	virtual bool ExtractFile(int file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
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);
185
	virtual bool extractFile(int filenum, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
186
	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 187
	virtual bool extractAll(const Utils::String &dir, int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
6 cycrow 188
 
126 cycrow 189
	virtual bool SaveToArchive(CyString filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
131 cycrow 190
	virtual bool saveToArchive(const Utils::String &filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
109 cycrow 191
	virtual void addGeneratedFiles(HZIP &hz) {};
192
 
6 cycrow 193
	void ClearFileData();
194
 
195
	// reading files
196
	void ReadAllFilesToMemory ();
197
	void ReadIconFileToMemory ();
198
	bool ReadFileToMemory(C_File *f);
199
 
200
	// compression
201
	void RecompressAllFiles ( int compresstype, CProgressInfo *progress );
47 cycrow 202
	void CompressAllFiles ( int compresstype, CProgressInfo *progress = NULL, CProgressInfo *overallProgress = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
6 cycrow 203
	bool UncompressAllFiles ( CProgressInfo * = NULL );
204
 
205
	// static functions
206
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
207
	static int CheckFile ( CyString filename, float *version = NULL );
208
 
209
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
210
 
211
	// installing
212
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
213
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
214
	virtual bool IsPatch () { return false; }
215
 
216
	// installer functions
217
	bool IsProfileEnabled () { return m_bProfile; }
218
	bool IsEnabled () { return m_bEnable; }
219
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
220
	bool IsGlobalEnabled () { return m_bGlobal; }
221
 
222
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
223
	void SetEnabled ( bool en ) { m_bEnable = en; }
224
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
225
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
226
 
227
	int  GetLoadError() { return m_iLoadError; }
228
	void SetLoadError(int i) { m_iLoadError = i; }
134 cycrow 229
	Utils::String createUpdateFile(const Utils::String &dir) const;
6 cycrow 230
 
14 cycrow 231
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 232
 
233
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
234
 
170 cycrow 235
	Utils::String fileSizeString() const;
6 cycrow 236
 
237
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 238
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
239
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
240
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
241
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 242
	void ClearNeededPackages();
243
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
244
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
160 cycrow 245
	void removeFakePatchOrder(bool after, const Utils::String &scriptName, const Utils::String &author);
246
	void removeFakePatchOrder(const Utils::String &scriptName, const Utils::String &author);
247
	void addFakePatchOrder(bool after, const Utils::String &scriptName, const Utils::String &author);
248
	bool anyFakePatchOrder() const { if ( !_lFakePatchBefore.empty() || !_lFakePatchAfter.empty() ) return true; return false; }
249
	const Utils::CStringList &getFakePatchBeforeOrder() const { return _lFakePatchBefore; }
250
	const Utils::CStringList &getFakePatchAfterOrder() const { return _lFakePatchAfter; }
88 cycrow 251
	void updateTextDB() { this->_resetTextDB(); }
6 cycrow 252
 
88 cycrow 253
	virtual bool readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares);
89 cycrow 254
	virtual bool readCommands(int iLang, CLinkList<SCommandSlot> &list);
255
	virtual bool readWingCommands(int iLang, CLinkList<SCommandSlot> &list);
88 cycrow 256
 
6 cycrow 257
	int  FindFirstGameInPackage();
258
	bool IsAnyGameInPackage();
259
	bool IsMultipleGamesInPackage();
260
	bool IsGameInPackage(int game);
261
 
170 cycrow 262
	virtual BaseFileType type () const { return BaseFileType::TYPE_BASE; }
263
	virtual int GetType() { return type(); }
6 cycrow 264
	bool AnyFileType ( int type );
265
	CBaseFile *GetParent () { return m_pParent; }
266
	void SetParent ( CBaseFile *file ) { m_pParent = file; }
48 cycrow 267
	int ParseLanguage(const Utils::String &lang) const;
6 cycrow 268
 
269
	virtual bool UpdateSigned (bool updateFiles);
270
	int GetNum() { return m_iNum; }
271
	void SetNum(int i) { m_iNum = i; }
272
 
273
	bool	IsFullyLoaded() { return m_bFullyLoaded; }
274
	virtual bool   IsSigned ()		{ return m_bSigned;}
275
	void SetOverrideFiles(bool b) { m_bOverrideFiles = b; }
276
	bool IsUpdateChecked () { return m_bUpdate; }
277
	void SetUpdateChecked ( bool en ) { m_bUpdate = en; }
278
 
279
	unsigned char *CreateData(size_t *size, CProgressInfo *progress = NULL);
280
 
281
protected:
282
	virtual void Delete ();
283
	virtual void SetDefaults ();
284
 
285
	// reading of files
14 cycrow 286
	virtual bool CheckHeader(const Utils::String header) const;
6 cycrow 287
	virtual bool ParseHeader ( CyString header );
288
	virtual bool ParseFileHeader ( CyString header );
289
	virtual bool ParseFilesLine ( CyString line );
290
	virtual void ReadValues ( CyString values );
291
	virtual void ReadFiles ( CyString values );
292
 
14 cycrow 293
	void _install_adjustFakePatches(CPackages *pPackages);
43 cycrow 294
	void _install_renameText(CPackages *pPackages);
50 cycrow 295
	bool _install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile);
296
	bool _install_setEnabled(bool bEnable, C_File *fit);
297
	bool _install_checkVersion(C_File *pFile, const Utils::String &sDestination);
298
	Utils::String _install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination);
299
	C_File *_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList);
300
	bool _install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr);
51 cycrow 301
	bool _install_createDirectory(CDirIO &Dir, const Utils::String &sTo, C_File *pFile, CyStringList *errorStr);
302
	void _install_writeFile(C_File *pFile, const Utils::String &sDestination, CyStringList *errorStr);
14 cycrow 303
 
109 cycrow 304
	int _read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress);
305
	int _read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress);
58 cycrow 306
	CFileIO *_startRead();
51 cycrow 307
 
88 cycrow 308
	void _addFile(C_File *file, bool dontChange = false);
309
	void _updateTextDB(C_File *file);
310
	void _resetTextDB();
311
	void _addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType);
89 cycrow 312
	bool _readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list);
131 cycrow 313
	Utils::String _replaceFilename(const Utils::String &fname);
88 cycrow 314
 
315
protected:
6 cycrow 316
	SSPKHeader m_SHeader;
317
	SSPKHeader2 m_SHeader2;
318
 
319
	C_File *m_pIconFile;
158 cycrow 320
	Utils::String _sIconExt;
134 cycrow 321
	Utils::String _sLastError;
322
	int _iLastError;
6 cycrow 323
 
324
	CLinkList<C_File>  m_lFiles;
170 cycrow 325
	CLinkList<C_File>  m_lTempFiles;
162 cycrow 326
	Utils::CStringList _lMirrors;			//TODO: move to CorePackage
160 cycrow 327
	Utils::CStringList _lFakePatchBefore;
328
	Utils::CStringList _lFakePatchAfter;
6 cycrow 329
 
88 cycrow 330
	CTextDB	*_pTextDB;
331
 
6 cycrow 332
	CLinkList<SGameCompat> m_lGames;
333
 
334
	bool m_bSigned;
335
	bool m_bFullyLoaded;
336
 
337
	//installer varibles
338
	bool m_bEnable;
339
	bool m_bModifiedEnabled;
340
	bool m_bGlobal;
341
	bool m_bProfile;
342
	int m_iLoadError;
343
 
344
	CBaseFile *m_pParent;
345
 
346
	int m_iNum;
347
 
348
	CLinkList<SNeededLibrary> m_lNeededLibrarys;
349
 
130 cycrow 350
	bool	_bCombineFiles;
6 cycrow 351
	bool	m_bOverrideFiles;
170 cycrow 352
	Utils::String	_sFtpAddr;
353
	bool			m_bAutoGenerateUpdateFile;
354
	bool			m_bUpdate;
6 cycrow 355
};
356
 
357
 
358
class SPKEXPORT CArchiveFile : public CBaseFile
359
{
360
public:
361
	CArchiveFile();
362
	virtual ~CArchiveFile();
170 cycrow 363
	virtual Utils::String getFullPackageName(const Utils::String& format, int lang) const override { return "Archive(" + name() + ")"; }
364
	virtual Utils::String getFullPackageName(int language, const Utils::String& byString) const override { return "Archive(" + name() + ")"; }
365
	virtual Utils::String getFullPackageName(int language, bool includeVersion = true, const Utils::String& byString = "by") const override { return "Archive(" + name() + ")"; }
366
 
367
	virtual BaseFileType type() const override { return BaseFileType::TYPE_ARCHIVE; }
6 cycrow 368
};
369
 
370
#endif //__BASEFILE_H__
371