Subversion Repositories spk

Rev

Rev 50 | Rev 52 | 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"
6 cycrow 16
 
46 cycrow 17
#include "Package/CorePackage.h"
18
 
6 cycrow 19
#define GAME_ALL			0
20
#define GAME_X2				1
21
#define GAME_X3				2
22
#define GAME_X3TC			3
23
#define GAME_X3AP			4
24
#define GAME_XREBIRTH		5
25
#define GAME_MAX			(GAME_XREBIRTH)
26
 
27
#define FILEVERSION 4.20f
20 cycrow 28
 
6 cycrow 29
#define WRITECHUNK		50000
30
 
31
class CPackages;
32
 
33
enum {
34
	READFLAG_NONE = 0,
35
	READFLAG_NOUNCOMPRESS = 1
36
};
37
 
38
enum {SPKFILE_INVALID, SPKFILE_SINGLE, SPKFILE_MULTI, SPKFILE_SINGLESHIP, SPKFILE_OLD, SPKFILE_BASE};
39
enum {SPKREAD_ALL, SPKREAD_NODATA, SPKREAD_VALUES, SPKREAD_HEADER};
40
enum {DELETESTATE_NONE, DELETESTATE_WAITING, DELETESTATE_DONE};
41
enum {SPKINSTALL_CREATEDIRECTORY, SPKINSTALL_CREATEDIRECTORY_FAIL, SPKINSTALL_WRITEFILE, SPKINSTALL_WRITEFILE_FAIL, SPKINSTALL_DELETEFILE, SPKINSTALL_DELETEFILE_FAIL,
42
		SPKINSTALL_SKIPFILE, SPKINSTALL_REMOVEDIR, SPKINSTALL_ENABLEFILE, SPKINSTALL_DISABLEFILE, SPKINSTALL_ENABLEFILE_FAIL, SPKINSTALL_DISABLEFILE_FAIL,
43
		SPKINSTALL_UNINSTALL_MOVE, SPKINSTALL_UNINSTALL_COPY, SPKINSTALL_UNINSTALL_MOVE_FAIL, SPKINSTALL_UNINSTALL_COPY_FAIL, SPKINSTALL_UNINSTALL_REMOVE, SPKINSTALL_UNINSTALL_REMOVE_FAIL,
44
		SPKINSTALL_ORIGINAL_BACKUP, SPKINSTALL_ORIGINAL_RESTORE, SPKINSTALL_ORIGINAL_BACKUP_FAIL, SPKINSTALL_ORIGINAL_RESTORE_FAIL, SPKINSTALL_FAKEPATCH, SPKINSTALL_FAKEPATCH_FAIL, 
45
		SPKINSTALL_MISSINGFILE, SPKINSTALL_SHARED, SPKINSTALL_SHARED_FAIL, SPKINSTALL_ORPHANED, SPKINSTALL_ORPHANED_FAIL, SPKINSTALL_UNCOMPRESS_FAIL, SPKINSTALL_AUTOTEXT, SPKINSTALL_AUTOTEXT_FAIL
46
};
47
 
48
enum {PACKAGETYPE_NORMAL, PACKAGETYPE_LIBRARY, PACKAGETYPE_CUSTOMSTART, PACKAGETYPE_PATCH, PACKAGETYPE_UPDATE, PACKAGETYPE_MOD, PACKAGETYPE_SHIP, PACKAGETYPE_FAKEPATCH};
49
 
50
enum {TYPE_BASE, TYPE_SPK, TYPE_XSP, TYPE_ARCHIVE};
51
 
52
// text pages
53
#define TEXTPAGE_RACE		1266
54
#define TEXTPAGE_OBJECTS	17
55
#define TEXTPAGE_CLASS		2006
56
#define TEXTPAGE_CARGOCLASS	1999
57
 
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
tstruct SNeededLibrary {
46 cycrow 68
	Utils::String	sName;
69
	Utils::String	sAuthor;
70
	Utils::String	sMinVersion;
6 cycrow 71
} SNeededLibrary;
72
 
73
tstruct SGameCompat {
74
	int			iGame;
46 cycrow 75
	Utils::String	sVersion;
6 cycrow 76
	int			iVersion;
77
} SGameCompat;
78
 
79
tstruct SSPKHeader2 {
80
	SSPKHeader2 () { iNumFiles = 0; lSize = 0; lFullSize = 0; }
81
	int iNumFiles;
82
	long lSize;
83
	long lFullSize;
84
	int iFileCompression;
85
	int iDataCompression;
86
} SSPKHeader2;
87
 
88
typedef struct SNames {
89
	int iLanguage;
46 cycrow 90
	Utils::String sName;
6 cycrow 91
} SNames;
92
 
93
 
46 cycrow 94
class SPKEXPORT CBaseFile : public SPK::Package::CCorePackage
6 cycrow 95
{
96
public:
13 cycrow 97
	static Utils::String ConvertGameToString(int game);
98
	static int GetGameFromString(const Utils::String &game);
6 cycrow 99
	static CyString ErrorString(int error, CyString errorStr = NullString);
100
 
101
	CBaseFile();
102
	virtual ~CBaseFile();
103
 
104
	// Get basic Settings
105
	virtual CyString GetFullPackageName(int language, CyString byString) { return GetFullPackageName(language, true, byString); }
106
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") 
107
	{ 
108
		CyString p;
109
		if ( language == -1 )
50 cycrow 110
			p = this->name();
6 cycrow 111
		else
112
			p = GetLanguageName(language);
113
		if ( includeVersion )
114
		{
115
			p += " V";
50 cycrow 116
			p += this->version();
6 cycrow 117
		}
118
		p += " ";
50 cycrow 119
		p += byString + " " + this->author();
6 cycrow 120
		return p;
121
	}
122
	CLinkList<C_File> *GetFileList() { return &m_lFiles; }
123
	C_File *GetIcon () { return m_pIconFile; }
124
	CyString GetIconExt () { return m_sIconExt; }
125
	CyString GetNameValidFile ();
126
	int GetDataCompression () { return m_SHeader2.iDataCompression; }
127
	float GetFileVersion () { return m_SHeader.fVersion; }
128
	long GetFullFileSize ();
129
	CyString GetLanguageName ( int lang );
130
	virtual CyString GetFullPackageName(CyString format, int lang);
50 cycrow 131
	Utils::String GetAutosaveName();
132
	void SetAutosaveName() { this->setFilename(GetAutosaveName()); }
6 cycrow 133
	SGameCompat *GetGameCompatability(int game);
134
	bool RemoveGameCompatability(int game);
46 cycrow 135
	void AddGameCompatability(int game, const Utils::String &version);
6 cycrow 136
	bool CheckGameCompatability(int game);
137
	bool CheckGameVersionCompatability(int game, CyString sVersion, int iVersion);
138
	bool AnyGameCompatability() { return !m_lGames.empty(); }
139
 
140
	bool AnyWebMirrors() { return !m_lMirrors.Empty(); }
141
	CyStringList *GetWebMirrors() { return &m_lMirrors; }
142
	int GetMaxWebMirrors() { return m_lMirrors.Count(); }
143
	CyString GetWebMirror(int i) { if ( i >= 0 && i < m_lMirrors.Count() ) return m_lMirrors.StringAt(i); return NullString; }
144
	void ClearWebMirrors() { m_lMirrors.Clear(); }
145
 
146
	CLinkList<C_File> *GetFileList(int type);
13 cycrow 147
	C_File *GetFirstFile(int type) const;
148
	C_File *GetNextFile(C_File *prev) const;
149
	C_File *GetPrevFile(C_File *next) const;
6 cycrow 150
 
14 cycrow 151
	virtual Utils::String CreateValuesLine () const;
6 cycrow 152
 
14 cycrow 153
	virtual bool LoadPackageData(const Utils::String &sFfirst, const Utils::String &sRest);
6 cycrow 154
	virtual bool GeneratePackagerScript(bool wildcard, CyStringList *list, bool datafile = false);
155
	virtual bool GeneratePackagerScriptFile(bool wildcard, CyStringList *list);
156
 
157
	void ConvertNormalMod(C_File *f, CyString to);
158
	void ConvertFakePatch(C_File *f);
159
	void ConvertAutoText(C_File *f);
160
	C_File *FindMatchingMod(C_File *f);
161
	void RenameFile(C_File *f, CyString baseName);
162
 
163
	// set basic settings
50 cycrow 164
	void AddWebMirror		( CyString str ) { m_lMirrors.PushBack(str, "", true); _changed(); }
165
	void RemoveWebMirror	( CyString str ) { m_lMirrors.Remove(str, true); _changed(); }
6 cycrow 166
	void SetDataCompression ( int c )       { m_SHeader2.iDataCompression = c; }
167
	void SetFileCompression ( int c )       { m_SHeader2.iFileCompression = c; }
168
	void SetValueCompression( int c )       { m_SHeader.iValueCompression = c; }
50 cycrow 169
	void SetIcon ( C_File *file, CyString ext ) { if ( m_pIconFile ) delete m_pIconFile; m_sIconExt = ext; m_pIconFile = file; _changed(); }
6 cycrow 170
	void SetFtpAddr			( CyString str ) { m_sFtpAddr = str; }
171
 
172
	void AddFileScript(int filetype, bool shared, CyString rest);
173
 
174
	bool IsMod();
13 cycrow 175
	bool IsFakePatch() const;
6 cycrow 176
 
177
	// error handling
178
	void ClearError () { m_sLastError = ""; m_iLastError = SPKERR_NONE; }
179
	int GetLastError () { return m_iLastError; }
180
	CyString GetLastErrorString() { return m_sLastError; }
181
 
182
	// file handling
183
	void AddFile ( C_File *file );
184
	C_File *AddFile ( CyString, CyString, int type, int game = 0 );
185
	C_File *AppendFile ( CyString file, int type, int game, CyString dir = NullString, CProgressInfo *progress = NULL );
186
	C_File *FindFile ( CyString, int, CyString = NullString, int game = 0 );
187
	bool AddFileNow ( CyString, CyString, int type, CProgressInfo *progress = NULL );
188
	int CountFiles ( int filetype );
189
	C_File *FindFileAt ( int filetype, int pos );
190
	virtual bool RemoveFile ( int pos );
191
	virtual bool RemoveFile ( C_File *files );
192
	virtual bool RemoveFile ( CyString file, int type, CyString dir = NullString, int game = 0 );
193
	void RemoveAllFiles ( int type, int game );
194
	CyString CreateFilesLine ( bool updateheader, CProgressInfo * = NULL );
195
 
196
	virtual bool WriteHeader ( FILE *id, int, int );
197
	virtual bool WriteData ( FILE *id, CProgressInfo * = NULL );
198
	virtual bool WriteFile ( CyString filename, CProgressInfo * = NULL );
199
	virtual bool ReadFile ( CyString filename, int readType = SPKREAD_ALL, CProgressInfo *progress = NULL );
51 cycrow 200
	bool readFile(std::fstream &stream, int readtype, CProgressInfo *progress);
6 cycrow 201
 
202
	virtual bool ExtractFile ( C_File *file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL );
203
	virtual bool ExtractFile ( int file, CyString dir, bool includedir = true, CProgressInfo *progress = NULL );
204
	virtual bool ExtractAll ( CyString dir, int game, bool includedir = true, CProgressInfo *progress = NULL );
205
 
206
	virtual bool SaveToArchive(CyString filename, int game, CProgressInfo *progress = NULL);
207
	void ClearFileData();
208
 
209
	CLinkList<SNames> *GetNamesList() { return &m_lNames; }
210
 
211
	// reading files
212
	void ReadAllFilesToMemory ();
213
	void ReadIconFileToMemory ();
214
	bool ReadFileToMemory(C_File *f);
215
 
216
	// compression
217
	void RecompressAllFiles ( int compresstype, CProgressInfo *progress );
47 cycrow 218
	void CompressAllFiles ( int compresstype, CProgressInfo *progress = NULL, CProgressInfo *overallProgress = NULL, int level = DEFAULT_COMPRESSION_LEVEL );
6 cycrow 219
	bool UncompressAllFiles ( CProgressInfo * = NULL );
220
 
221
	// static functions
51 cycrow 222
	static Utils::String readEndOfLine(std::fstream &stream, int *line = NULL, bool upper = true);
6 cycrow 223
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
224
	static int CheckFile ( CyString filename, float *version = NULL );
225
 
226
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
227
 
228
	// installing
229
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
230
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
231
	virtual bool IsPatch () { return false; }
232
 
233
	// installer functions
234
	bool IsProfileEnabled () { return m_bProfile; }
235
	bool IsEnabled () { return m_bEnable; }
236
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
237
	bool IsGlobalEnabled () { return m_bGlobal; }
238
 
239
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
240
	void SetEnabled ( bool en ) { m_bEnable = en; }
241
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
242
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
243
 
244
	int  GetLoadError() { return m_iLoadError; }
245
	void SetLoadError(int i) { m_iLoadError = i; }
246
	CyString CreateUpdateFile(CyString dir);
247
 
248
	// language functions
249
	void RemoveLanguageName ( int lang );
46 cycrow 250
	void AddLanguageName ( int lang, const Utils::String &name );
6 cycrow 251
	void ClearNames ();
252
 
14 cycrow 253
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 254
 
255
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
256
 
257
	CyString GetFullFileSizeString();
258
 
259
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 260
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
261
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
262
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
263
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 264
	void ClearNeededPackages();
265
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
266
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
267
	void RemoveFakePatchOrder(bool after, CyString scriptName, CyString author);
268
	void RemoveFakePatchOrder(CyString scriptName, CyString author);
269
	void AddFakePatchOrder(bool after, CyString scriptName, CyString author);
270
	bool AnyFakePatchOrder() { if ( !m_lFakePatchBefore.Empty() || !m_lFakePatchAfter.Empty() ) return true; return false; }
271
	CyStringList &GetFakePatchBeforeOrder() { return m_lFakePatchBefore; }
272
	CyStringList &GetFakePatchAfterOrder() { return m_lFakePatchAfter; }
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
 
51 cycrow 323
	int _read_FileHeader(std::fstream &stream, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress);
324
	int _read_Header(std::fstream &stream, int iReadType, int iMaxProgress, CProgressInfo *pProgress);
325
 
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
 
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