Subversion Repositories spk

Rev

Rev 98 | Rev 121 | 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
26
#define GAME_XREBIRTH		5
27
#define GAME_MAX			(GAME_XREBIRTH)
28
 
29
#define FILEVERSION 4.20f
20 cycrow 30
 
6 cycrow 31
#define WRITECHUNK		50000
32
 
33
class CPackages;
34
 
88 cycrow 35
namespace SPK {
36
	class CTextDB;
37
}
38
 
39
using namespace SPK;
40
 
6 cycrow 41
enum {
42
	READFLAG_NONE = 0,
43
	READFLAG_NOUNCOMPRESS = 1
44
};
45
 
46
enum {SPKFILE_INVALID, SPKFILE_SINGLE, SPKFILE_MULTI, SPKFILE_SINGLESHIP, SPKFILE_OLD, SPKFILE_BASE};
47
enum {SPKREAD_ALL, SPKREAD_NODATA, SPKREAD_VALUES, SPKREAD_HEADER};
48
enum {DELETESTATE_NONE, DELETESTATE_WAITING, DELETESTATE_DONE};
49
enum {SPKINSTALL_CREATEDIRECTORY, SPKINSTALL_CREATEDIRECTORY_FAIL, SPKINSTALL_WRITEFILE, SPKINSTALL_WRITEFILE_FAIL, SPKINSTALL_DELETEFILE, SPKINSTALL_DELETEFILE_FAIL,
50
		SPKINSTALL_SKIPFILE, SPKINSTALL_REMOVEDIR, SPKINSTALL_ENABLEFILE, SPKINSTALL_DISABLEFILE, SPKINSTALL_ENABLEFILE_FAIL, SPKINSTALL_DISABLEFILE_FAIL,
51
		SPKINSTALL_UNINSTALL_MOVE, SPKINSTALL_UNINSTALL_COPY, SPKINSTALL_UNINSTALL_MOVE_FAIL, SPKINSTALL_UNINSTALL_COPY_FAIL, SPKINSTALL_UNINSTALL_REMOVE, SPKINSTALL_UNINSTALL_REMOVE_FAIL,
52
		SPKINSTALL_ORIGINAL_BACKUP, SPKINSTALL_ORIGINAL_RESTORE, SPKINSTALL_ORIGINAL_BACKUP_FAIL, SPKINSTALL_ORIGINAL_RESTORE_FAIL, SPKINSTALL_FAKEPATCH, SPKINSTALL_FAKEPATCH_FAIL, 
53
		SPKINSTALL_MISSINGFILE, SPKINSTALL_SHARED, SPKINSTALL_SHARED_FAIL, SPKINSTALL_ORPHANED, SPKINSTALL_ORPHANED_FAIL, SPKINSTALL_UNCOMPRESS_FAIL, SPKINSTALL_AUTOTEXT, SPKINSTALL_AUTOTEXT_FAIL
54
};
55
 
56
enum {PACKAGETYPE_NORMAL, PACKAGETYPE_LIBRARY, PACKAGETYPE_CUSTOMSTART, PACKAGETYPE_PATCH, PACKAGETYPE_UPDATE, PACKAGETYPE_MOD, PACKAGETYPE_SHIP, PACKAGETYPE_FAKEPATCH};
57
 
58
enum {TYPE_BASE, TYPE_SPK, TYPE_XSP, TYPE_ARCHIVE};
59
 
60
// spk header struct
61
tstruct SSPKHeader {
62
	SSPKHeader () { fVersion = 0; iValueCompression = lValueCompressSize = 0; }
63
	float fVersion;
64
	int iValueCompression;
65
	unsigned long lValueCompressSize;
66
} SSPKHeader;
67
 
68
 
69
tstruct SSPKHeader2 {
70
	SSPKHeader2 () { iNumFiles = 0; lSize = 0; lFullSize = 0; }
71
	int iNumFiles;
72
	long lSize;
73
	long lFullSize;
74
	int iFileCompression;
75
	int iDataCompression;
76
} SSPKHeader2;
77
 
46 cycrow 78
class SPKEXPORT CBaseFile : public SPK::Package::CCorePackage
6 cycrow 79
{
80
public:
13 cycrow 81
	static Utils::String ConvertGameToString(int game);
82
	static int GetGameFromString(const Utils::String &game);
6 cycrow 83
	static CyString ErrorString(int error, CyString errorStr = NullString);
84
 
85
	CBaseFile();
86
	virtual ~CBaseFile();
87
 
88
	// Get basic Settings
89
	virtual CyString GetFullPackageName(int language, CyString byString) { return GetFullPackageName(language, true, byString); }
90
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") 
91
	{ 
92
		CyString p;
93
		if ( language == -1 )
50 cycrow 94
			p = this->name();
6 cycrow 95
		else
96
			p = GetLanguageName(language);
97
		if ( includeVersion )
98
		{
99
			p += " V";
50 cycrow 100
			p += this->version();
6 cycrow 101
		}
102
		p += " ";
50 cycrow 103
		p += byString + " " + this->author();
6 cycrow 104
		return p;
105
	}
106
	CLinkList<C_File> *GetFileList() { return &m_lFiles; }
107
	C_File *GetIcon () { return m_pIconFile; }
108
	CyString GetIconExt () { return m_sIconExt; }
109
	CyString GetNameValidFile ();
110
	int GetDataCompression () { return m_SHeader2.iDataCompression; }
111
	float GetFileVersion () { return m_SHeader.fVersion; }
112
	long GetFullFileSize ();
113
	CyString GetLanguageName ( int lang );
114
	virtual CyString GetFullPackageName(CyString format, int lang);
50 cycrow 115
	Utils::String GetAutosaveName();
116
	void SetAutosaveName() { this->setFilename(GetAutosaveName()); }
6 cycrow 117
	SGameCompat *GetGameCompatability(int game);
118
	bool RemoveGameCompatability(int game);
46 cycrow 119
	void AddGameCompatability(int game, const Utils::String &version);
6 cycrow 120
	bool CheckGameCompatability(int game);
121
	bool CheckGameVersionCompatability(int game, CyString sVersion, int iVersion);
122
	bool AnyGameCompatability() { return !m_lGames.empty(); }
123
 
124
	bool AnyWebMirrors() { return !m_lMirrors.Empty(); }
125
	CyStringList *GetWebMirrors() { return &m_lMirrors; }
126
	int GetMaxWebMirrors() { return m_lMirrors.Count(); }
127
	CyString GetWebMirror(int i) { if ( i >= 0 && i < m_lMirrors.Count() ) return m_lMirrors.StringAt(i); return NullString; }
128
	void ClearWebMirrors() { m_lMirrors.Clear(); }
129
 
53 cycrow 130
	CLinkList<C_File> *fileList(int type) const;
13 cycrow 131
	C_File *GetFirstFile(int type) const;
132
	C_File *GetNextFile(C_File *prev) const;
133
	C_File *GetPrevFile(C_File *next) const;
6 cycrow 134
 
14 cycrow 135
	virtual Utils::String CreateValuesLine () const;
6 cycrow 136
 
98 cycrow 137
	virtual bool LoadPackageData(const Utils::String &sFfirst, const Utils::String &sRest, const Utils::String &sMainGame, Utils::CStringList &otherGames);
6 cycrow 138
	virtual bool GeneratePackagerScript(bool wildcard, CyStringList *list, bool datafile = false);
139
	virtual bool GeneratePackagerScriptFile(bool wildcard, CyStringList *list);
140
 
141
	void ConvertNormalMod(C_File *f, CyString to);
142
	void ConvertFakePatch(C_File *f);
143
	void ConvertAutoText(C_File *f);
144
	C_File *FindMatchingMod(C_File *f);
145
	void RenameFile(C_File *f, CyString baseName);
146
 
147
	// set basic settings
50 cycrow 148
	void AddWebMirror		( CyString str ) { m_lMirrors.PushBack(str, "", true); _changed(); }
149
	void RemoveWebMirror	( CyString str ) { m_lMirrors.Remove(str, true); _changed(); }
6 cycrow 150
	void SetDataCompression ( int c )       { m_SHeader2.iDataCompression = c; }
151
	void SetFileCompression ( int c )       { m_SHeader2.iFileCompression = c; }
152
	void SetValueCompression( int c )       { m_SHeader.iValueCompression = c; }
50 cycrow 153
	void SetIcon ( C_File *file, CyString ext ) { if ( m_pIconFile ) delete m_pIconFile; m_sIconExt = ext; m_pIconFile = file; _changed(); }
6 cycrow 154
	void SetFtpAddr			( CyString str ) { m_sFtpAddr = str; }
155
 
98 cycrow 156
	void AddFileScript(int filetype, bool shared, CyString rest, const Utils::String &sMainGame, Utils::CStringList &otherGames);
6 cycrow 157
 
158
	bool IsMod();
13 cycrow 159
	bool IsFakePatch() const;
6 cycrow 160
 
161
	// error handling
162
	void ClearError () { m_sLastError = ""; m_iLastError = SPKERR_NONE; }
163
	int GetLastError () { return m_iLastError; }
164
	CyString GetLastErrorString() { return m_sLastError; }
165
 
166
	// file handling
167
	void AddFile ( C_File *file );
168
	C_File *AddFile ( CyString, CyString, int type, int game = 0 );
169
	C_File *AppendFile ( CyString file, int type, int game, CyString dir = NullString, CProgressInfo *progress = NULL );
170
	C_File *FindFile ( CyString, int, CyString = NullString, int game = 0 );
171
	bool AddFileNow ( CyString, CyString, int type, CProgressInfo *progress = NULL );
172
	int CountFiles ( int filetype );
173
	C_File *FindFileAt ( int filetype, int pos );
174
	virtual bool RemoveFile ( int pos );
175
	virtual bool RemoveFile ( C_File *files );
176
	virtual bool RemoveFile ( CyString file, int type, CyString dir = NullString, int game = 0 );
177
	void RemoveAllFiles ( int type, int game );
178
	CyString CreateFilesLine ( bool updateheader, CProgressInfo * = NULL );
179
 
52 cycrow 180
	virtual bool WriteHeader(CFileIO &file, int iHeader, int iLength);
181
	virtual bool WriteData(CFileIO &file, CProgressInfo * = NULL );
6 cycrow 182
	virtual bool WriteFile ( CyString filename, CProgressInfo * = NULL );
183
	virtual bool ReadFile ( CyString filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL );
58 cycrow 184
	bool readFile(CFileIO &File, int readtype, CProgressInfo *progress);
6 cycrow 185
 
186
	virtual bool ExtractFile ( C_File *file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL );
187
	virtual bool ExtractFile ( int file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL );
188
	virtual bool ExtractAll ( CyString dir, int game, bool includedir = true, CProgressInfo *progress = NULL );
189
 
190
	virtual bool SaveToArchive(CyString filename, int game, CProgressInfo *progress = NULL);
109 cycrow 191
	virtual void addGeneratedFiles(HZIP &hz) {};
192
 
6 cycrow 193
	void ClearFileData();
194
 
195
	CLinkList<SNames> *GetNamesList() { return &m_lNames; }
196
 
197
	// reading files
198
	void ReadAllFilesToMemory ();
199
	void ReadIconFileToMemory ();
200
	bool ReadFileToMemory(C_File *f);
201
 
202
	// compression
203
	void RecompressAllFiles ( int compresstype, CProgressInfo *progress );
47 cycrow 204
	void CompressAllFiles ( int compresstype, CProgressInfo *progress = NULL, CProgressInfo *overallProgress = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
6 cycrow 205
	bool UncompressAllFiles ( CProgressInfo * = NULL );
206
 
207
	// static functions
208
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
209
	static int CheckFile ( CyString filename, float *version = NULL );
210
 
211
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
212
 
213
	// installing
214
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
215
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
216
	virtual bool IsPatch () { return false; }
217
 
218
	// installer functions
219
	bool IsProfileEnabled () { return m_bProfile; }
220
	bool IsEnabled () { return m_bEnable; }
221
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
222
	bool IsGlobalEnabled () { return m_bGlobal; }
223
 
224
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
225
	void SetEnabled ( bool en ) { m_bEnable = en; }
226
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
227
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
228
 
229
	int  GetLoadError() { return m_iLoadError; }
230
	void SetLoadError(int i) { m_iLoadError = i; }
231
	CyString CreateUpdateFile(CyString dir);
232
 
233
	// language functions
234
	void RemoveLanguageName ( int lang );
46 cycrow 235
	void AddLanguageName ( int lang, const Utils::String &name );
6 cycrow 236
	void ClearNames ();
237
 
14 cycrow 238
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 239
 
240
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
241
 
242
	CyString GetFullFileSizeString();
243
 
244
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 245
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
246
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
247
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
248
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 249
	void ClearNeededPackages();
250
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
251
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
252
	void RemoveFakePatchOrder(bool after, CyString scriptName, CyString author);
253
	void RemoveFakePatchOrder(CyString scriptName, CyString author);
254
	void AddFakePatchOrder(bool after, CyString scriptName, CyString author);
255
	bool AnyFakePatchOrder() { if ( !m_lFakePatchBefore.Empty() || !m_lFakePatchAfter.Empty() ) return true; return false; }
256
	CyStringList &GetFakePatchBeforeOrder() { return m_lFakePatchBefore; }
257
	CyStringList &GetFakePatchAfterOrder() { return m_lFakePatchAfter; }
88 cycrow 258
	void updateTextDB() { this->_resetTextDB(); }
6 cycrow 259
 
88 cycrow 260
	virtual bool readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares);
89 cycrow 261
	virtual bool readCommands(int iLang, CLinkList<SCommandSlot> &list);
262
	virtual bool readWingCommands(int iLang, CLinkList<SCommandSlot> &list);
88 cycrow 263
 
50 cycrow 264
//	bool IsChanged() { return m_bChanged; }
265
//	void SetChanged(bool b) { m_bChanged = b; }
6 cycrow 266
 
267
	int  FindFirstGameInPackage();
268
	bool IsAnyGameInPackage();
269
	bool IsMultipleGamesInPackage();
270
	bool IsGameInPackage(int game);
271
 
272
	virtual int GetType () { return TYPE_BASE; }
273
	bool AnyFileType ( int type );
274
	CBaseFile *GetParent () { return m_pParent; }
275
	void SetParent ( CBaseFile *file ) { m_pParent = file; }
48 cycrow 276
	int ParseLanguage(const Utils::String &lang) const;
6 cycrow 277
 
278
	virtual bool UpdateSigned (bool updateFiles);
279
	int GetNum() { return m_iNum; }
280
	void SetNum(int i) { m_iNum = i; }
281
 
282
	bool	IsFullyLoaded() { return m_bFullyLoaded; }
283
	virtual bool   IsSigned ()		{ return m_bSigned;}
284
	void SetOverrideFiles(bool b) { m_bOverrideFiles = b; }
285
	bool IsUpdateChecked () { return m_bUpdate; }
286
	void SetUpdateChecked ( bool en ) { m_bUpdate = en; }
287
 
288
	unsigned char *CreateData(size_t *size, CProgressInfo *progress = NULL);
289
 
290
protected:
291
	virtual void Delete ();
292
	virtual void SetDefaults ();
293
 
294
	// reading of files
14 cycrow 295
	virtual bool CheckHeader(const Utils::String header) const;
6 cycrow 296
	virtual bool ParseHeader ( CyString header );
297
	virtual bool ParseFileHeader ( CyString header );
298
	virtual bool ParseFilesLine ( CyString line );
299
	virtual void ReadValues ( CyString values );
300
	virtual void ReadFiles ( CyString values );
301
 
14 cycrow 302
	void _install_adjustFakePatches(CPackages *pPackages);
43 cycrow 303
	void _install_renameText(CPackages *pPackages);
50 cycrow 304
	bool _install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile);
305
	bool _install_setEnabled(bool bEnable, C_File *fit);
306
	bool _install_checkVersion(C_File *pFile, const Utils::String &sDestination);
307
	Utils::String _install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination);
308
	C_File *_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList);
309
	bool _install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr);
51 cycrow 310
	bool _install_createDirectory(CDirIO &Dir, const Utils::String &sTo, C_File *pFile, CyStringList *errorStr);
311
	void _install_writeFile(C_File *pFile, const Utils::String &sDestination, CyStringList *errorStr);
14 cycrow 312
 
109 cycrow 313
	int _read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress);
314
	int _read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress);
58 cycrow 315
	CFileIO *_startRead();
51 cycrow 316
 
88 cycrow 317
	void _addFile(C_File *file, bool dontChange = false);
318
	void _updateTextDB(C_File *file);
319
	void _resetTextDB();
320
	void _addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType);
89 cycrow 321
	bool _readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list);
88 cycrow 322
 
323
protected:
6 cycrow 324
	SSPKHeader m_SHeader;
325
	SSPKHeader2 m_SHeader2;
326
 
327
	C_File *m_pIconFile;
328
	CyString m_sIconExt;
329
	CyString m_sLastError;
330
	int m_iLastError;
331
 
332
	CLinkList<C_File>  m_lFiles;
48 cycrow 333
	CLinkList<SNames> m_lNames;			//TODO: move to CorePackage
334
	CyStringList m_lMirrors;			//TODO: move to CorePackage
6 cycrow 335
	CyStringList m_lFakePatchBefore;
336
	CyStringList m_lFakePatchAfter;
337
 
88 cycrow 338
	CTextDB	*_pTextDB;
339
 
6 cycrow 340
	CLinkList<SGameCompat> m_lGames;
341
 
342
	bool m_bSigned;
343
	bool m_bFullyLoaded;
344
 
345
	//installer varibles
346
	bool m_bEnable;
347
	bool m_bModifiedEnabled;
348
	bool m_bGlobal;
349
	bool m_bProfile;
350
	int m_iLoadError;
351
 
352
	CBaseFile *m_pParent;
353
 
354
	int m_iNum;
355
 
356
	CLinkList<SNeededLibrary> m_lNeededLibrarys;
357
 
358
	bool	m_bOverrideFiles;
359
	CyString	m_sFtpAddr;
360
	bool	m_bAutoGenerateUpdateFile;
361
	bool m_bUpdate;
362
};
363
 
364
 
365
class SPKEXPORT CArchiveFile : public CBaseFile
366
{
367
public:
368
	CArchiveFile();
369
	virtual ~CArchiveFile();
50 cycrow 370
	virtual CyString GetFullPackageName(CyString format, int lang) { return CyString("Archive(") + this->name() + ")"; }
371
	virtual CyString GetFullPackageName(int language, CyString byString) { return CyString("Archive(") + this->name() + ")"; }
372
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") { return CyString("Archive(") + this->name() + ")"; }
6 cycrow 373
	virtual int GetType () { return TYPE_ARCHIVE; }
374
};
375
 
376
#endif //__BASEFILE_H__
377