Subversion Repositories spk

Rev

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