Subversion Repositories spk

Rev

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