Subversion Repositories spk

Rev

Rev 49 | Rev 51 | 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 );
200
	virtual bool ReadFile ( FILE *id, int readtype, CProgressInfo *progress );
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
222
	static CyString GetEndOfLine ( FILE *id, int *line = NULL, bool upper = true );
223
	static int CheckFile ( CyString filename, float *version = NULL );
224
 
225
	bool IsFileAdded(C_File *f) { return m_lFiles.FindData(f); }
226
 
227
	// installing
228
	void SwitchFilePointer(C_File *oldFile, C_File *newFile);
229
	bool InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *spklist, CyStringList *errorStr, bool enabled = true, CPackages *packages = NULL );
230
	virtual bool IsPatch () { return false; }
231
 
232
	// installer functions
233
	bool IsProfileEnabled () { return m_bProfile; }
234
	bool IsEnabled () { return m_bEnable; }
235
	bool IsModifiedEnabled () { return m_bModifiedEnabled; }
236
	bool IsGlobalEnabled () { return m_bGlobal; }
237
 
238
	void SetProfileEnabled ( bool en ) { m_bProfile = en; }
239
	void SetEnabled ( bool en ) { m_bEnable = en; }
240
	void SetModifiedEnabled ( bool en ) { m_bModifiedEnabled = en; }
241
	void SetGlobalEnabled ( bool en ) { m_bGlobal = en; }
242
 
243
	int  GetLoadError() { return m_iLoadError; }
244
	void SetLoadError(int i) { m_iLoadError = i; }
245
	CyString CreateUpdateFile(CyString dir);
246
 
247
	// language functions
248
	void RemoveLanguageName ( int lang );
46 cycrow 249
	void AddLanguageName ( int lang, const Utils::String &name );
6 cycrow 250
	void ClearNames ();
251
 
14 cycrow 252
	virtual bool ParseValueLine(const Utils::String &line);
6 cycrow 253
 
254
	CLinkList<SGameCompat>  *GetGameCompatabilityList() { return &m_lGames; }
255
 
256
	CyString GetFullFileSizeString();
257
 
258
	CLinkList<SNeededLibrary> *GetNeededLibraries() { return &m_lNeededLibrarys; }
46 cycrow 259
	void AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion);
260
	bool IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
261
	SNeededLibrary *FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author);
262
	void RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author);
6 cycrow 263
	void ClearNeededPackages();
264
	bool AnyDependacies() { return (m_lNeededLibrarys.size()) ? true: false; }
265
	bool AutoGenerateUpdateFile() { return m_bAutoGenerateUpdateFile; }
266
	void RemoveFakePatchOrder(bool after, CyString scriptName, CyString author);
267
	void RemoveFakePatchOrder(CyString scriptName, CyString author);
268
	void AddFakePatchOrder(bool after, CyString scriptName, CyString author);
269
	bool AnyFakePatchOrder() { if ( !m_lFakePatchBefore.Empty() || !m_lFakePatchAfter.Empty() ) return true; return false; }
270
	CyStringList &GetFakePatchBeforeOrder() { return m_lFakePatchBefore; }
271
	CyStringList &GetFakePatchAfterOrder() { return m_lFakePatchAfter; }
272
 
50 cycrow 273
//	bool IsChanged() { return m_bChanged; }
274
//	void SetChanged(bool b) { m_bChanged = b; }
6 cycrow 275
 
276
	int  FindFirstGameInPackage();
277
	bool IsAnyGameInPackage();
278
	bool IsMultipleGamesInPackage();
279
	bool IsGameInPackage(int game);
280
 
281
	virtual int GetType () { return TYPE_BASE; }
282
	bool AnyFileType ( int type );
283
	CBaseFile *GetParent () { return m_pParent; }
284
	void SetParent ( CBaseFile *file ) { m_pParent = file; }
48 cycrow 285
	int ParseLanguage(const Utils::String &lang) const;
6 cycrow 286
 
287
	virtual bool UpdateSigned (bool updateFiles);
288
	int GetNum() { return m_iNum; }
289
	void SetNum(int i) { m_iNum = i; }
290
 
291
	bool	IsFullyLoaded() { return m_bFullyLoaded; }
292
	virtual bool   IsSigned ()		{ return m_bSigned;}
293
	void SetOverrideFiles(bool b) { m_bOverrideFiles = b; }
294
	bool IsUpdateChecked () { return m_bUpdate; }
295
	void SetUpdateChecked ( bool en ) { m_bUpdate = en; }
296
 
297
	unsigned char *CreateData(size_t *size, CProgressInfo *progress = NULL);
298
 
299
protected:
300
	virtual void Delete ();
301
	virtual void SetDefaults ();
302
 
303
	// reading of files
14 cycrow 304
	virtual bool CheckHeader(const Utils::String header) const;
6 cycrow 305
	virtual bool ParseHeader ( CyString header );
306
	virtual bool ParseFileHeader ( CyString header );
307
	virtual bool ParseFilesLine ( CyString line );
308
	virtual void ReadValues ( CyString values );
309
	virtual void ReadFiles ( CyString values );
310
 
14 cycrow 311
	void _install_adjustFakePatches(CPackages *pPackages);
43 cycrow 312
	void _install_renameText(CPackages *pPackages);
50 cycrow 313
	bool _install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile);
314
	bool _install_setEnabled(bool bEnable, C_File *fit);
315
	bool _install_checkVersion(C_File *pFile, const Utils::String &sDestination);
316
	Utils::String _install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination);
317
	C_File *_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList);
318
	bool _install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr);
14 cycrow 319
 
6 cycrow 320
	SSPKHeader m_SHeader;
321
	SSPKHeader2 m_SHeader2;
322
 
323
	C_File *m_pIconFile;
324
	CyString m_sIconExt;
325
	CyString m_sLastError;
326
	int m_iLastError;
327
 
328
	CLinkList<C_File>  m_lFiles;
48 cycrow 329
	CLinkList<SNames> m_lNames;			//TODO: move to CorePackage
330
	CyStringList m_lMirrors;			//TODO: move to CorePackage
6 cycrow 331
	CyStringList m_lFakePatchBefore;
332
	CyStringList m_lFakePatchAfter;
333
 
334
	CLinkList<SGameCompat> m_lGames;
335
 
336
	bool m_bSigned;
337
	bool m_bFullyLoaded;
338
 
339
	//installer varibles
340
	bool m_bEnable;
341
	bool m_bModifiedEnabled;
342
	bool m_bGlobal;
343
	bool m_bProfile;
344
	int m_iLoadError;
345
 
346
	CBaseFile *m_pParent;
347
 
348
	int m_iNum;
349
 
350
	CLinkList<SNeededLibrary> m_lNeededLibrarys;
351
 
352
	bool	m_bOverrideFiles;
353
	CyString	m_sFtpAddr;
354
	bool	m_bAutoGenerateUpdateFile;
355
	bool m_bUpdate;
356
};
357
 
358
 
359
class SPKEXPORT CArchiveFile : public CBaseFile
360
{
361
public:
362
	CArchiveFile();
363
	virtual ~CArchiveFile();
50 cycrow 364
	virtual CyString GetFullPackageName(CyString format, int lang) { return CyString("Archive(") + this->name() + ")"; }
365
	virtual CyString GetFullPackageName(int language, CyString byString) { return CyString("Archive(") + this->name() + ")"; }
366
	virtual CyString GetFullPackageName(int language, bool includeVersion = true, CyString byString = "by") { return CyString("Archive(") + this->name() + ")"; }
6 cycrow 367
	virtual int GetType () { return TYPE_ARCHIVE; }
368
};
369
 
370
#endif //__BASEFILE_H__
371