Subversion Repositories spk

Rev

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