Subversion Repositories spk

Rev

Rev 134 | Rev 158 | 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; }
110
	CyString GetIconExt () { return m_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 ();
121 cycrow 115
	CyString GetLanguageName ( int lang ) const;
6 cycrow 116
	virtual CyString GetFullPackageName(CyString format, int lang);
50 cycrow 117
	Utils::String GetAutosaveName();
118
	void SetAutosaveName() { this->setFilename(GetAutosaveName()); }
6 cycrow 119
	SGameCompat *GetGameCompatability(int game);
120
	bool RemoveGameCompatability(int game);
46 cycrow 121
	void AddGameCompatability(int game, const Utils::String &version);
6 cycrow 122
	bool CheckGameCompatability(int game);
123
	bool CheckGameVersionCompatability(int game, CyString sVersion, int iVersion);
124
	bool AnyGameCompatability() { return !m_lGames.empty(); }
125
 
126
	bool AnyWebMirrors() { return !m_lMirrors.Empty(); }
127
	CyStringList *GetWebMirrors() { return &m_lMirrors; }
128
	int GetMaxWebMirrors() { return m_lMirrors.Count(); }
129
	CyString GetWebMirror(int i) { if ( i >= 0 && i < m_lMirrors.Count() ) return m_lMirrors.StringAt(i); return NullString; }
130
	void ClearWebMirrors() { m_lMirrors.Clear(); }
131
 
131 cycrow 132
	const CLinkList<C_File> *fileList() const;
53 cycrow 133
	CLinkList<C_File> *fileList(int type) const;
13 cycrow 134
	C_File *GetFirstFile(int type) const;
135
	C_File *GetNextFile(C_File *prev) const;
136
	C_File *GetPrevFile(C_File *next) const;
6 cycrow 137
 
14 cycrow 138
	virtual Utils::String CreateValuesLine () const;
6 cycrow 139
 
134 cycrow 140
	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 141
	virtual bool GeneratePackagerScript(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons, bool datafile = false);
142
	virtual bool GeneratePackagerScriptFile(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons);
6 cycrow 143
 
144
	void ConvertNormalMod(C_File *f, CyString to);
145
	void ConvertFakePatch(C_File *f);
146
	void ConvertAutoText(C_File *f);
147
	C_File *FindMatchingMod(C_File *f);
148
	void RenameFile(C_File *f, CyString baseName);
149
 
150
	// set basic settings
50 cycrow 151
	void AddWebMirror		( CyString str ) { m_lMirrors.PushBack(str, "", true); _changed(); }
152
	void RemoveWebMirror	( CyString str ) { m_lMirrors.Remove(str, true); _changed(); }
6 cycrow 153
	void SetDataCompression ( int c )       { m_SHeader2.iDataCompression = c; }
154
	void SetFileCompression ( int c )       { m_SHeader2.iFileCompression = c; }
155
	void SetValueCompression( int c )       { m_SHeader.iValueCompression = c; }
50 cycrow 156
	void SetIcon ( C_File *file, CyString ext ) { if ( m_pIconFile ) delete m_pIconFile; m_sIconExt = ext; m_pIconFile = file; _changed(); }
6 cycrow 157
	void SetFtpAddr			( CyString str ) { m_sFtpAddr = str; }
158
 
155 cycrow 159
	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 160
 
161
	bool IsMod();
13 cycrow 162
	bool IsFakePatch() const;
6 cycrow 163
 
164
	// error handling
134 cycrow 165
	void ClearError () { _sLastError = ""; _iLastError = SPKERR_NONE; }
166
	int lastError() const { return _iLastError; }
167
	const Utils::String lastErrorString() const { return _sLastError; }
6 cycrow 168
 
169
	// file handling
170
	void AddFile ( C_File *file );
127 cycrow 171
	C_File *AddFile(CyString, CyString, int type, int game = 0);
155 cycrow 172
	C_File *addFile(const Utils::String &file, const Utils::String &dir, FileType type, int game = 0, bool packed = false);
173
	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 174
	C_File *FindFile ( CyString, int, CyString = NullString, int game = 0 );
175
	bool AddFileNow ( CyString, CyString, int type, CProgressInfo *progress = NULL );
176
	int CountFiles ( int filetype );
177
	C_File *FindFileAt ( int filetype, int pos );
178
	virtual bool RemoveFile ( int pos );
179
	virtual bool RemoveFile ( C_File *files );
180
	virtual bool RemoveFile ( CyString file, int type, CyString dir = NullString, int game = 0 );
129 cycrow 181
	void removeAllFiles(FileType type, int game);
6 cycrow 182
	CyString CreateFilesLine ( bool updateheader, CProgressInfo * = NULL );
183
 
52 cycrow 184
	virtual bool WriteHeader(CFileIO &file, int iHeader, int iLength);
185
	virtual bool WriteData(CFileIO &file, CProgressInfo * = NULL );
6 cycrow 186
	virtual bool WriteFile ( CyString filename, CProgressInfo * = NULL );
130 cycrow 187
	virtual bool ReadFile(CyString filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
188
	virtual bool readFile(const Utils::String &filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL);
58 cycrow 189
	bool readFile(CFileIO &File, int readtype, CProgressInfo *progress);
6 cycrow 190
 
131 cycrow 191
	virtual bool ExtractFile(C_File *file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
192
	virtual bool ExtractFile(int file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL);
193
	virtual bool extractFile(int file, const Utils::String &dir, bool includedir = true, CProgressInfo *progress = NULL);
194
	virtual bool extractFile(C_File *file, const Utils::String &dir, bool includedir = true, CProgressInfo *progress = NULL);
195
	virtual bool extractFile(int filenum, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
196
	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 197
	virtual bool extractAll(const Utils::String &dir, int game, const Utils::CStringList &gameAddons, bool includedir = true, CProgressInfo *progress = NULL);
6 cycrow 198
 
126 cycrow 199
	virtual bool SaveToArchive(CyString filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
131 cycrow 200
	virtual bool saveToArchive(const Utils::String &filename, int game, const CGameExe *exes, CProgressInfo *progress = NULL);
109 cycrow 201
	virtual void addGeneratedFiles(HZIP &hz) {};
202
 
6 cycrow 203
	void ClearFileData();
204
 
205
	CLinkList<SNames> *GetNamesList() { return &m_lNames; }
206
 
207
	// reading files
208
	void ReadAllFilesToMemory ();
209
	void ReadIconFileToMemory ();
210
	bool ReadFileToMemory(C_File *f);
211
 
212
	// compression
213
	void RecompressAllFiles ( int compresstype, CProgressInfo *progress );
47 cycrow 214
	void CompressAllFiles ( int compresstype, CProgressInfo *progress = NULL, CProgressInfo *overallProgress = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
6 cycrow 215
	bool UncompressAllFiles ( CProgressInfo * = NULL );
216
 
217
	// static functions
218
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
219
	static int CheckFile ( CyString filename, float *version = NULL );
220
 
221
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
222
 
223
	// installing
224
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
225
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
226
	virtual bool IsPatch () { return false; }
227
 
228
	// installer functions
229
	bool IsProfileEnabled () { return m_bProfile; }
230
	bool IsEnabled () { return m_bEnable; }
231
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
232
	bool IsGlobalEnabled () { return m_bGlobal; }
233
 
234
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
235
	void SetEnabled ( bool en ) { m_bEnable = en; }
236
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
237
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
238
 
239
	int  GetLoadError() { return m_iLoadError; }
240
	void SetLoadError(int i) { m_iLoadError = i; }
134 cycrow 241
	Utils::String createUpdateFile(const Utils::String &dir) const;
6 cycrow 242
 
243
	// language functions
244
	void RemoveLanguageName ( int lang );
46 cycrow 245
	void AddLanguageName ( int lang, const Utils::String &name );
6 cycrow 246
	void ClearNames ();
247
 
14 cycrow 248
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 249
 
250
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
251
 
252
	CyString GetFullFileSizeString();
253
 
254
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 255
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
256
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
257
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
258
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 259
	void ClearNeededPackages();
260
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
261
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
262
	void RemoveFakePatchOrder(bool after, CyString scriptName, CyString author);
263
	void RemoveFakePatchOrder(CyString scriptName, CyString author);
264
	void AddFakePatchOrder(bool after, CyString scriptName, CyString author);
265
	bool AnyFakePatchOrder() { if ( !m_lFakePatchBefore.Empty() || !m_lFakePatchAfter.Empty() ) return true; return false; }
266
	CyStringList &GetFakePatchBeforeOrder() { return m_lFakePatchBefore; }
267
	CyStringList &GetFakePatchAfterOrder() { return m_lFakePatchAfter; }
88 cycrow 268
	void updateTextDB() { this->_resetTextDB(); }
6 cycrow 269
 
88 cycrow 270
	virtual bool readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares);
89 cycrow 271
	virtual bool readCommands(int iLang, CLinkList<SCommandSlot> &list);
272
	virtual bool readWingCommands(int iLang, CLinkList<SCommandSlot> &list);
88 cycrow 273
 
50 cycrow 274
//	bool IsChanged() { return m_bChanged; }
275
//	void SetChanged(bool b) { m_bChanged = b; }
6 cycrow 276
 
277
	int  FindFirstGameInPackage();
278
	bool IsAnyGameInPackage();
279
	bool IsMultipleGamesInPackage();
280
	bool IsGameInPackage(int game);
281
 
282
	virtual int GetType () { return TYPE_BASE; }
283
	bool AnyFileType ( int type );
284
	CBaseFile *GetParent () { return m_pParent; }
285
	void SetParent ( CBaseFile *file ) { m_pParent = file; }
48 cycrow 286
	int ParseLanguage(const Utils::String &lang) const;
6 cycrow 287
 
288
	virtual bool UpdateSigned (bool updateFiles);
289
	int GetNum() { return m_iNum; }
290
	void SetNum(int i) { m_iNum = i; }
291
 
292
	bool	IsFullyLoaded() { return m_bFullyLoaded; }
293
	virtual bool   IsSigned ()		{ return m_bSigned;}
294
	void SetOverrideFiles(bool b) { m_bOverrideFiles = b; }
295
	bool IsUpdateChecked () { return m_bUpdate; }
296
	void SetUpdateChecked ( bool en ) { m_bUpdate = en; }
297
 
298
	unsigned char *CreateData(size_t *size, CProgressInfo *progress = NULL);
299
 
300
protected:
301
	virtual void Delete ();
302
	virtual void SetDefaults ();
303
 
304
	// reading of files
14 cycrow 305
	virtual bool CheckHeader(const Utils::String header) const;
6 cycrow 306
	virtual bool ParseHeader ( CyString header );
307
	virtual bool ParseFileHeader ( CyString header );
308
	virtual bool ParseFilesLine ( CyString line );
309
	virtual void ReadValues ( CyString values );
310
	virtual void ReadFiles ( CyString values );
311
 
14 cycrow 312
	void _install_adjustFakePatches(CPackages *pPackages);
43 cycrow 313
	void _install_renameText(CPackages *pPackages);
50 cycrow 314
	bool _install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile);
315
	bool _install_setEnabled(bool bEnable, C_File *fit);
316
	bool _install_checkVersion(C_File *pFile, const Utils::String &sDestination);
317
	Utils::String _install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination);
318
	C_File *_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList);
319
	bool _install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr);
51 cycrow 320
	bool _install_createDirectory(CDirIO &Dir, const Utils::String &sTo, C_File *pFile, CyStringList *errorStr);
321
	void _install_writeFile(C_File *pFile, const Utils::String &sDestination, CyStringList *errorStr);
14 cycrow 322
 
109 cycrow 323
	int _read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress);
324
	int _read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress);
58 cycrow 325
	CFileIO *_startRead();
51 cycrow 326
 
88 cycrow 327
	void _addFile(C_File *file, bool dontChange = false);
328
	void _updateTextDB(C_File *file);
329
	void _resetTextDB();
330
	void _addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType);
89 cycrow 331
	bool _readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list);
131 cycrow 332
	Utils::String _replaceFilename(const Utils::String &fname);
88 cycrow 333
 
334
protected:
6 cycrow 335
	SSPKHeader m_SHeader;
336
	SSPKHeader2 m_SHeader2;
337
 
338
	C_File *m_pIconFile;
339
	CyString m_sIconExt;
134 cycrow 340
	Utils::String _sLastError;
341
	int _iLastError;
6 cycrow 342
 
343
	CLinkList<C_File>  m_lFiles;
48 cycrow 344
	CLinkList<SNames> m_lNames;			//TODO: move to CorePackage
345
	CyStringList m_lMirrors;			//TODO: move to CorePackage
6 cycrow 346
	CyStringList m_lFakePatchBefore;
347
	CyStringList m_lFakePatchAfter;
348
 
88 cycrow 349
	CTextDB	*_pTextDB;
350
 
6 cycrow 351
	CLinkList<SGameCompat> m_lGames;
352
 
353
	bool m_bSigned;
354
	bool m_bFullyLoaded;
355
 
356
	//installer varibles
357
	bool m_bEnable;
358
	bool m_bModifiedEnabled;
359
	bool m_bGlobal;
360
	bool m_bProfile;
361
	int m_iLoadError;
362
 
363
	CBaseFile *m_pParent;
364
 
365
	int m_iNum;
366
 
367
	CLinkList<SNeededLibrary> m_lNeededLibrarys;
368
 
130 cycrow 369
	bool	_bCombineFiles;
6 cycrow 370
	bool	m_bOverrideFiles;
371
	CyString	m_sFtpAddr;
372
	bool	m_bAutoGenerateUpdateFile;
373
	bool m_bUpdate;
374
};
375
 
376
 
377
class SPKEXPORT CArchiveFile : public CBaseFile
378
{
379
public:
380
	CArchiveFile();
381
	virtual ~CArchiveFile();
50 cycrow 382
	virtual CyString GetFullPackageName(CyString format, int lang) { return CyString("Archive(") + this->name() + ")"; }
383
	virtual CyString GetFullPackageName(int language, CyString byString) { return CyString("Archive(") + this->name() + ")"; }
384
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") { return CyString("Archive(") + this->name() + ")"; }
6 cycrow 385
	virtual int GetType () { return TYPE_ARCHIVE; }
386
};
387
 
388
#endif //__BASEFILE_H__
389