Subversion Repositories spk

Rev

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