Subversion Repositories spk

Rev

Rev 147 | Rev 155 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 cycrow 1
// SpkFile.cpp: implementation of the CSpkFile class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
 
5
#include "BaseFile.h"
6
 
7
//////////////////////////////////////////////////////////////////////
8
// Construction/Destruction
9
//////////////////////////////////////////////////////////////////////
10
 
11
#include "spk.h"
12
 
13
#include "DirIO.h"
14
#include "File_IO.h"
15
#include "CatFile.h"
16
#include "Packages.h"
88 cycrow 17
#include "TextDB.h"
6 cycrow 18
 
46 cycrow 19
#include <Package/InstallText.h>
6 cycrow 20
 
46 cycrow 21
// remove these eventually
22
using namespace SPK;
23
using namespace Package;
6 cycrow 24
 
25
CArchiveFile::~CArchiveFile()
26
{
27
	Delete ();
28
}
29
 
30
CArchiveFile::CArchiveFile() : CBaseFile ()
31
{
32
	SetDefaults ();
33
}
34
 
35
void CBaseFile::SetDefaults ()
36
{
46 cycrow 37
	_setDefaults();
38
 
6 cycrow 39
	m_pIconFile = NULL;
40
 
41
	m_bAutoGenerateUpdateFile = false;
42
	m_SHeader.iValueCompression = SPKCOMPRESS_ZLIB;
43
	m_SHeader2.iFileCompression = SPKCOMPRESS_ZLIB;
44
	m_SHeader2.iDataCompression = SPKCOMPRESS_LZMA;
45
	m_pParent = NULL;
50 cycrow 46
	_changed();
6 cycrow 47
	m_bUpdate = false;
130 cycrow 48
	_bCombineFiles = false;
6 cycrow 49
 
50
	ClearError();
51
 
52
	m_iLoadError = 0;
53
 
54
	m_bFullyLoaded = false;
55
	m_bSigned = false;
56
	m_bEnable = m_bGlobal = m_bProfile = m_bModifiedEnabled = true;
57
	m_bOverrideFiles = false;
58
}
59
 
88 cycrow 60
CBaseFile::CBaseFile() : _pTextDB(NULL)
6 cycrow 61
{
62
	SetDefaults ();
63
}
64
CBaseFile::~CBaseFile()
65
{
66
	Delete();
67
}
68
 
69
void CBaseFile::Delete ()
70
{
71
	m_lFiles.clear(true);
72
 
73
	if ( m_pIconFile )
74
	{
75
		delete m_pIconFile;
76
		m_pIconFile = NULL;
77
	}
78
 
88 cycrow 79
	if ( _pTextDB ) {
80
		delete _pTextDB;
81
		_pTextDB = NULL;
82
	}
83
 
6 cycrow 84
	m_lNames.clear(true);
85
}
86
 
121 cycrow 87
CyString CBaseFile::GetLanguageName ( int lang ) const
6 cycrow 88
{
89
	for ( CListNode<SNames> *node = m_lNames.Front(); node; node = node->next() )
90
	{
91
		SNames *n = node->Data();
92
		if ( n->iLanguage == lang )
93
			return n->sName;
94
	}
50 cycrow 95
	return this->name();
6 cycrow 96
}
97
 
98
 
99
/*
100
##########################################################################################
101
##################                Base Class Functions                  ##################
102
##########################################################################################
103
*/
104
 
131 cycrow 105
const CLinkList<C_File> *CBaseFile::fileList() const
106
{
107
	return &m_lFiles;
108
}
53 cycrow 109
CLinkList<C_File> *CBaseFile::fileList(int type) const
6 cycrow 110
{
111
	CLinkList<C_File> *list = new CLinkList<C_File>;
112
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
113
	{
114
		C_File *f = node->Data();
115
		if ( f->GetFileType() == type )
116
			list->push_back(f);
117
	}
118
 
119
	return list;
120
}
121
 
13 cycrow 122
C_File *CBaseFile::GetNextFile(C_File *prev) const
6 cycrow 123
{
124
	int type = -1;
125
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
126
	{
127
		C_File *f = node->Data();
128
		if ( type == -1 )
129
		{
130
			if ( f == prev )
131
				type = f->GetFileType();
132
		}
133
		else
134
		{
135
			if ( f->GetFileType() == type )
136
				return f;
137
		}
138
	}
139
 
140
	return NULL;
141
}
142
 
13 cycrow 143
C_File *CBaseFile::GetPrevFile(C_File *next) const
6 cycrow 144
{
145
	if ( !next )
146
		return NULL;
147
 
148
	int type = -1;
149
	for ( CListNode<C_File> *node = m_lFiles.Back(); node; node = node->prev() )
150
	{
151
		C_File *f = node->Data();
152
		if ( type == -1 )
153
		{
154
			if ( f == next )
155
				type = f->GetFileType();
156
		}
157
		else
158
		{
159
			if ( f->GetFileType() == type )
160
				return f;
161
		}
162
	}
163
 
164
	return NULL;
165
}
166
 
13 cycrow 167
C_File *CBaseFile::GetFirstFile(int type) const
6 cycrow 168
{
169
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
170
	{
171
		C_File *f = node->Data();
172
		if ( f->GetFileType() == type )
173
			return f;
174
	}
175
 
176
	return NULL;
177
}
178
 
179
int CBaseFile::CheckFile ( CyString filename, float *version )
180
{
91 cycrow 181
	CFileIO File(filename);
182
	if ( !File.startRead() ) return 0;
183
	Utils::String line = File.readEndOfLine();
51 cycrow 184
	Utils::String type = line.token(";", 1);
185
	File.close();
6 cycrow 186
 
187
	// check for old version
51 cycrow 188
	if ( line.left(3) == "HiP" ) return SPKFILE_OLD;
6 cycrow 189
 
190
	// check for format
51 cycrow 191
	if ( version ) *version = line.token(";", 2);
6 cycrow 192
 
51 cycrow 193
	if ( type == "BaseCycrow" )	return SPKFILE_BASE;
194
	if ( type == "SPKCycrow" )	return SPKFILE_SINGLE;
195
	if ( type == "XSPCycrow" )	return SPKFILE_SINGLESHIP;
196
	if ( type == "MSPKCycrow" )	return SPKFILE_MULTI;
6 cycrow 197
	return SPKFILE_INVALID;
198
}
199
 
200
void CBaseFile::ClearFileData()
201
{
88 cycrow 202
	for ( CListNode<C_File> *f = m_lFiles.Front(); f; f = f->next() )
6 cycrow 203
	{
88 cycrow 204
		f->Data()->DeleteData();
6 cycrow 205
	}
206
}
207
 
130 cycrow 208
Utils::String CBaseFile::getNameValidFile() const
6 cycrow 209
{
130 cycrow 210
	Utils::String name = this->name();
211
	name.removeChar(':');
212
	name.removeChar('/');
213
	name.removeChar('\\');
214
	name.removeChar('*');
215
	name.removeChar('?');
216
	name.removeChar('"');
217
	name.removeChar('<');
218
	name.removeChar('>');
219
	name.removeChar('|');
220
	return name;
221
}
6 cycrow 222
 
223
void CBaseFile::SwitchFilePointer(C_File *oldFile, C_File *newFile)
224
{
225
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
226
	{
227
		C_File *f = node->Data();
228
		if ( f == oldFile )
229
		{
230
			node->ChangeData(newFile);
231
			break;
232
		}
233
	}
234
}
235
 
236
bool CBaseFile::AnyFileType ( int type )
237
{
238
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
239
	{
240
		C_File *f = node->Data();
241
		if ( f->GetFileType() == type )
242
			return true;
243
	}
244
 
245
	return false;
246
}
247
 
248
void CBaseFile::AddFile ( C_File *file )
249
{
250
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
251
	{
252
		C_File *f = node->Data();
130 cycrow 253
		if ( f->fileType() != file->fileType() )
6 cycrow 254
			continue;
130 cycrow 255
		if ( f->name() != file->name () )
6 cycrow 256
			continue;
257
		if ( f->GetDir() != file->GetDir() )
258
			continue;
127 cycrow 259
		if ( f->game() != file->game() )
6 cycrow 260
			continue;
261
 
262
		m_lFiles.remove(node, true);
263
		break;
264
	}
265
 
88 cycrow 266
	_addFile(file);
6 cycrow 267
}
268
 
127 cycrow 269
C_File *CBaseFile::AddFile(CyString file, CyString dir, int type, int game)
6 cycrow 270
{
127 cycrow 271
	return addFile(file.ToString(), dir.ToString(), (FileType)type, game);
272
}
273
C_File *CBaseFile::addFile(const Utils::String &file, const Utils::String &dir, FileType type, int game)
274
{
275
	C_File *newfile = new C_File(file);
276
	newfile->SetDir(dir);
130 cycrow 277
	newfile->setFileType(type);
127 cycrow 278
	newfile->setGame(game);
6 cycrow 279
 
280
	// first check if the file already exists
281
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
282
	{
283
		C_File *f = node->Data();
130 cycrow 284
		if ( f->fileType() != newfile->fileType() )
6 cycrow 285
			continue;
130 cycrow 286
		if ( f->name() != newfile->name () )
6 cycrow 287
			continue;
134 cycrow 288
		if ( f->dir() != newfile->dir() )
6 cycrow 289
			continue;
130 cycrow 290
		if (f->game() != newfile->game())
291
		{
292
			//same file, for different game, check if they are the same and combine them
293
			if (_bCombineFiles)
294
			{
295
				char *data = NULL;
296
				char *newData = NULL;
297
				size_t size = 0;
298
				size_t newSize = 0;
299
 
300
				if (f->GetData() && f->GetDataSize())
143 cycrow 301
					data = (char *)f->UncompressData((long *)&size, NULL);
130 cycrow 302
				else if(f->isExternalFile())
303
					data = CFileIO(f->filePointer()).ReadToData(&size);
304
				if (newfile->GetData() && newfile->GetDataSize())
143 cycrow 305
					newData = (char *)f->UncompressData((long *)&newSize, NULL);
130 cycrow 306
				else
307
					newData = CFileIO(newfile->filePointer()).ReadToData(&newSize);
308
 
309
				// compare bytes
310
				bool matched = false;
311
				if (size == newSize)
312
				{
313
					matched = true;
314
					for (unsigned long i = 0; i < size; ++i)
315
					{
316
						if (data[i] != newData[i])
317
						{
318
							matched = false;
319
							break;
320
						}
321
					}
322
				}
323
 
143 cycrow 324
				if (data)
130 cycrow 325
					delete data;
143 cycrow 326
				if (newData)
130 cycrow 327
					delete newData;
328
 
329
				if (matched)
330
				{
331
					if(!f->game() || f->game() == GAME_ALLNEW)
332
						newfile->setGame(0);
333
					else
334
						newfile->setGame(newfile->game() | f->game());
335
					m_lFiles.remove(node, true);
336
					break;
337
				}
338
			}
6 cycrow 339
			continue;
130 cycrow 340
		}
6 cycrow 341
 
342
		// must already exist, delete this one
343
		m_lFiles.remove(node, true);
344
		break;
345
	}
346
 
88 cycrow 347
	_addFile(newfile);
6 cycrow 348
 
349
	return newfile;
350
}
351
 
352
bool CBaseFile::AddFileNow ( CyString file, CyString dir, int type, CProgressInfo *progress )
353
{
354
	C_File *f = AddFile ( file, dir, type );
355
	if ( !f->ReadFromFile () )
356
		return false;
357
 
358
	// compress the file
359
	return f->CompressData ( m_SHeader2.iDataCompression, progress );
360
}
361
 
362
C_File *CBaseFile::AppendFile ( CyString file, int type, int game, CyString dir, CProgressInfo *progress )
363
{
364
	C_File *newfile = AddFile ( file, dir, type, game );
365
	if ( !newfile )
366
		return NULL;
367
 
368
 
369
	// read the file into memory
370
	if ( newfile->ReadFromFile () )
371
	{
372
		// now compress the file
373
		if ( newfile->CompressData ( m_SHeader2.iDataCompression, progress ) )
374
			return newfile;
375
	}
376
	else if ( newfile->GetLastError() == SPKERR_MALLOC )
377
	{
378
		if ( newfile->CompressFile ( progress ) )
379
			return newfile;
380
	}
381
 
382
	m_lFiles.pop_back ();
383
	delete newfile;
384
 
385
	return NULL;
386
}
387
 
388
C_File *CBaseFile::FindFileAt ( int filetype, int pos )
389
{
390
	int count = 0;
391
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
392
	{
393
		C_File *file = node->Data();
394
		if ( file->GetFileType() != filetype )
395
			continue;
396
 
397
		if ( count == pos )
398
			return file;
399
 
400
		++count;
401
	}
402
 
403
	return NULL;
404
}
405
 
406
C_File *CBaseFile::FindFile ( CyString filename, int type, CyString dir, int game )
407
{
408
	CyString lfile = filename.ToLower();
409
	lfile = lfile.FindReplace ( "\\", "/" );
410
 
411
	lfile = lfile.GetToken ( lfile.NumToken('/'), '/' );
412
 
413
	CListNode<C_File> *node = m_lFiles.Front();
414
	while ( node )
415
	{
416
		C_File *f = node->Data();
417
		node = node->next();
418
 
419
		if ( type != f->GetFileType() )
420
			continue;
421
		if ( dir != f->GetDir() )
422
			continue;
127 cycrow 423
		if ( game != f->game() )
6 cycrow 424
			continue;
425
		if ( f->GetName().ToLower() == lfile )
426
			return f;
427
	}
428
	return NULL;
429
}
430
 
431
bool CBaseFile::RemoveFile ( CyString file, int type, CyString dir, int game )
432
{
433
	C_File *f = FindFile (file, type, dir, game);
434
	if ( !f )
435
		return false;
436
	return RemoveFile ( f );
437
}
438
 
439
bool CBaseFile::RemoveFile ( C_File *file )
440
{
441
	int count = 0;
442
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
443
	{
444
		C_File *f = node->Data();
445
		if ( f == file )
446
			return RemoveFile ( count );
447
		++count;
448
	}
449
	return false;
450
}
451
 
452
bool CBaseFile::RemoveFile ( int pos )
453
{
454
	if ( (pos < 0) || (pos >= m_lFiles.size()) )
455
		return false;
456
 
457
	C_File *file = m_lFiles.Get ( pos );
458
	m_lFiles.erase ( pos + 1 );
459
 
460
	if ( file )
461
		delete file;
462
 
50 cycrow 463
	_changed();
6 cycrow 464
 
465
	return true;
466
}
467
 
468
 
129 cycrow 469
void CBaseFile::removeAllFiles(FileType type, int game)
6 cycrow 470
{
471
	if ( m_lFiles.empty() )
472
		return;
473
 
474
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
475
	{
129 cycrow 476
		if (game == 0 && node->Data()->game() && node->Data()->game() != GAME_ALLNEW)
477
			continue;
478
		if ( game > -1 )
479
		{
480
			unsigned int fileGame = node->Data()->game() & ~GAME_ALLNEW;
481
			if (fileGame != (1 << game))
482
			{
483
				// just remove the game from file
484
				if (fileGame & (1 << game))
485
					node->Data()->setGame(node->Data()->game() & ~(1 << game));
6 cycrow 486
				continue;
129 cycrow 487
			}
6 cycrow 488
		}
129 cycrow 489
		if ( type == FILETYPE_UNKNOWN || node->Data()->GetFileType() == type )
6 cycrow 490
			node->DeleteData();
491
	}
492
 
493
	m_lFiles.RemoveEmpty();
494
 
50 cycrow 495
	_changed();
6 cycrow 496
}
497
 
498
void CBaseFile::RecompressAllFiles ( int type, CProgressInfo *progress )
499
{
500
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
501
	{
502
		C_File *file = node->Data();
503
		if ( progress )
504
			progress->UpdateFile(file);
505
 
506
		if ( file->GetCompressionType() == type )
507
			continue;
508
 
509
		if ( !file->GetData() )
510
			file->ReadFromFile();
511
 
512
		file->ChangeCompression ( type, progress );
513
	}
514
}
515
 
47 cycrow 516
void CBaseFile::CompressAllFiles ( int type, CProgressInfo *progress, CProgressInfo *overallProgress, int level )
6 cycrow 517
{
47 cycrow 518
	if ( overallProgress ) overallProgress->SetMax(m_lFiles.size());
519
 
520
	int iCount = 0;
6 cycrow 521
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
522
	{
523
		C_File *file = node->Data();
524
		if ( progress )
525
			progress->UpdateFile(file);
526
 
527
		if ( !file->GetData() )
528
			file->ReadFromFile();
529
 
530
		file->CompressData ( type, progress, level );
47 cycrow 531
 
532
		if ( overallProgress ) overallProgress->SetDone(++iCount);
6 cycrow 533
	}
534
}
535
 
536
bool CBaseFile::UncompressAllFiles ( CProgressInfo *progress )
537
{
538
	int countFile = 0;
539
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
540
	{
541
		C_File *fit = node->Data();
542
		if ( progress )
543
		{
544
			progress->UpdateFile ( fit );
545
			progress->UpdateProgress(countFile++, m_lFiles.size());
546
		}
547
 
548
		bool uncomprToFile = false;
549
 
550
		if ( progress )
551
			progress->SwitchSecond();
552
 
127 cycrow 553
		if(!fit->isExternalFile())
6 cycrow 554
		{
127 cycrow 555
			if (!fit->UncompressData(progress))
6 cycrow 556
			{
127 cycrow 557
				if (fit->GetCompressionType() == SPKCOMPRESS_7ZIP)
6 cycrow 558
				{
127 cycrow 559
					if (!fit->UncompressToFile("temp", this, false, progress))
560
						return false;
561
					else
562
					{
563
						uncomprToFile = true;
564
						fit->SetFullDir("temp");
565
					}
6 cycrow 566
				}
127 cycrow 567
 
568
				if (!uncomprToFile)
569
					return false;
6 cycrow 570
			}
571
		}
572
 
573
		if ( progress )
574
			progress->SwitchSecond();
575
	}
576
	return true;
577
}
578
 
579
long CBaseFile::GetFullFileSize()
580
{
581
	long fullsize = 1000;
582
 
583
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
584
		fullsize += node->Data()->GetUncompressedDataSize();
585
 
586
	if ( m_pIconFile )
587
		fullsize += m_pIconFile->GetUncompressedDataSize();
588
 
589
	return fullsize;
590
}
591
 
592
/*
593
	Func:   GetEndOfLine
594
	Input:  id - The file id for the current file to read from
595
	        line - Pointed to hold the line number thats read
596
			upper - true if it converts to uppercase
597
	Return: String - the string it has read
598
	Desc:   Reads a string from a file, simlar to readLine() classes, reads to the end of the line in a file
599
*/
600
CyString CBaseFile::GetEndOfLine ( FILE *id, int *line, bool upper )
601
{
602
	CyString word;
603
 
604
	char c = fgetc ( id );
605
	if ( c == -1 )
606
		return "";
607
 
608
	while ( (c != 13) && (!feof(id)) && (c != '\n') )
609
	{
610
		word += c;
611
		c = fgetc ( id );
612
	}
613
 
614
	if ( line )
615
		++(*line);
616
 
617
	if ( upper )
618
		return word.ToUpper();
619
 
620
	return word;
621
}
622
 
623
int CBaseFile::CountFiles ( int filetype )
624
{
625
	int i = 0;
626
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
627
	{
628
		C_File *file = node->Data();
629
		if ( file->GetFileType() != filetype )
630
			continue;
631
		++i;
632
	}
633
 
634
	return i;
635
}
636
 
637
void CBaseFile::ClearNames ()
638
{
639
	m_lNames.clear(true);
640
}
641
void CBaseFile::RemoveLanguageName ( int lang )
642
{
643
	SNames *n;
644
	for ( n = m_lNames.First(); n; n = m_lNames.Next() )
645
	{
646
		if ( n->iLanguage == lang )
647
		{
648
			m_lNames.RemoveCurrent();
649
			delete n;
50 cycrow 650
			_changed();
6 cycrow 651
		}
652
	}
653
}
654
 
46 cycrow 655
void CBaseFile::AddLanguageName ( int lang, const Utils::String &name )
6 cycrow 656
{
657
	// first check for an existing language
658
	SNames *n;
659
	for ( n = m_lNames.First(); n; n = m_lNames.Next() )
660
	{
661
		if ( n->iLanguage == lang )
662
		{
663
			n->sName = name;
664
			return;
665
		}
666
	}
667
 
668
	// not found, add a new entry
669
	n = new SNames;
670
	n->iLanguage = lang;
671
	n->sName = name;
672
	m_lNames.push_back ( n );
673
 
50 cycrow 674
	_changed();
6 cycrow 675
}
676
 
677
CyString CBaseFile::GetFullPackageName(CyString format, int lang)
678
{
679
	if ( format.Empty() )
680
		return GetFullPackageName(lang);
681
 
50 cycrow 682
	CyString args[3] = { this->GetLanguageName(lang), this->version(), this->author() };
6 cycrow 683
	return format.Args(args, 3);
684
}
685
 
686
/*
687
	Func:   CreateFilesLine
688
	Return: String - returns the full string for files list
689
	Desc:   Creates a signle line list of all the files
690
*/
691
CyString CBaseFile::CreateFilesLine ( bool updateheader, CProgressInfo *progress )
692
{
693
	CyString line;
694
 
695
	if ( progress )
696
	{
697
		progress->SetDone(0);
698
		progress->UpdateStatus(STATUS_COMPRESS);
699
	}
700
 
701
	if ( updateheader )
702
	{
703
		m_SHeader2.iNumFiles = 0;
704
		m_SHeader2.lFullSize = 0;
705
	}
706
 
707
	if ( m_pIconFile )
708
	{
709
		// no data, read it from file
710
		if ( !m_pIconFile->GetData() )
711
			m_pIconFile->ReadFromFile ();
712
 
713
		// compress the file
714
		if ( !m_pIconFile->CompressData ( m_SHeader2.iDataCompression, progress ) )
715
			m_pIconFile->SetDataCompression(SPKCOMPRESS_NONE);
716
 
717
		line += CyString("Icon:") + (m_pIconFile->GetDataSize() + (long)4) + ":" + m_pIconFile->GetUncompressedDataSize() + ":" + (long)m_pIconFile->GetCompressionType() + ":" + m_sIconExt + "\n";
718
		if ( updateheader )
719
		{
720
			++m_SHeader2.iNumFiles;
721
			m_SHeader2.lFullSize += (m_pIconFile->GetDataSize() + 4);
722
		}
723
	}
724
 
725
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
726
	{
727
		C_File *file = node->Data();
728
		if ( progress )
729
			progress->UpdateFile ( file );
730
 
731
		// no data, read it from file
732
		if ( !file->GetData() )
733
		{
734
			if ( !file->ReadFromFile () )
735
			{
736
				if ( file->GetLastError() == SPKERR_MALLOC )
737
				{
738
					if ( !file->CompressFile ( progress ) )
739
						continue;
740
				}
741
			}
742
		}
743
 
744
		if ( !file->GetData() )
745
			continue;
746
 
747
		// compress the file
748
		if ( !file->CompressData ( m_SHeader2.iDataCompression, progress ) )
749
		{
750
			file->SetDataCompression(SPKCOMPRESS_NONE);
751
			file->SetUncompressedDataSize(file->GetDataSize());
752
		}
753
 
754
		CyString command = GetFileTypeString ( file->GetFileType() );
755
		if ( command.Empty() )
756
			continue;
757
 
758
		if ( file->IsShared() )
759
			command = CyString("$") + command;
760
 
761
		if ( file->GetDir().Empty() )
128 cycrow 762
			line += (command + ":" + (file->GetDataSize() + (long)4) + ":" + file->GetUncompressedDataSize() + ":" + (long)file->GetCompressionType() + ":" + (long)file->GetCreationTime() + ":" + ((file->IsCompressedToFile()) ? "1" : "0") + ":" + file->GetFilename() + ":NULL:" + (long)file->game() + "\n");
6 cycrow 763
		else
128 cycrow 764
			line += (command + ":" + (file->GetDataSize() + (long)4) + ":" + file->GetUncompressedDataSize() + ":" + (long)file->GetCompressionType() + ":" + (long)file->GetCreationTime() + ":" + ((file->IsCompressedToFile()) ? "1" : "0") + ":" + file->GetFilename() + ":" + file->GetDir() + ":" + (long)file->game() + "\n");
6 cycrow 765
 
766
		if ( updateheader )
767
		{
768
			++m_SHeader2.iNumFiles;
769
			m_SHeader2.lFullSize += (file->GetDataSize() + 4);
770
		}
771
	}
772
 
773
	return line;
774
}
775
 
776
 
777
/*
778
######################################################################################
779
##########							Reading Functions			    		##########
780
######################################################################################
781
*/
782
 
783
void CBaseFile::ReadAllFilesToMemory ()
784
{
785
	// no file to read from
50 cycrow 786
	if ( this->filename().empty() ) return;
6 cycrow 787
 
788
	// now open the file
58 cycrow 789
	CFileIO File(this->filename());
790
	if ( !File.startRead() ) return;
6 cycrow 791
 
792
	// read the header
58 cycrow 793
	File.readEndOfLine();
6 cycrow 794
	// skip past values
58 cycrow 795
	File.seek(4 + m_SHeader.lValueCompressSize);
6 cycrow 796
 
797
	// read the next header
58 cycrow 798
	File.readEndOfLine();
6 cycrow 799
	// skip past files
58 cycrow 800
	File.seek(4 + m_SHeader2.lSize);
6 cycrow 801
 
802
	if ( m_pIconFile )
803
	{
804
		if ( (!m_pIconFile->GetData()) && (!m_pIconFile->Skip()) )
51 cycrow 805
			m_pIconFile->readFromFile(File, m_pIconFile->GetDataSize());
6 cycrow 806
		else
58 cycrow 807
			File.seek(4 + m_pIconFile->GetDataSize());
6 cycrow 808
	}
809
 
810
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
811
	{
812
		C_File *fit = node->Data();
813
		if ( (!fit->GetData()) && (!fit->Skip()) )
51 cycrow 814
			fit->readFromFile(File, fit->GetDataSize());
6 cycrow 815
		else
58 cycrow 816
			File.seek(4 + fit->GetDataSize());
6 cycrow 817
	}
818
 
51 cycrow 819
	File.close();
6 cycrow 820
}
821
 
822
bool CBaseFile::ReadFileToMemory(C_File *f)
823
{
62 cycrow 824
	if ( this->filename().empty() || !f ) return false;		// no filename to load from
51 cycrow 825
	if ( f->GetData() && f->GetDataSize() ) return true;	// already loaded the data
826
	if ( !m_lFiles.FindData(f) ) return false;				// unable to find file entry
6 cycrow 827
 
828
	// now open the file
58 cycrow 829
	CFileIO *File = _startRead();
830
	if ( !File ) return false;
6 cycrow 831
 
58 cycrow 832
	if ( m_pIconFile ) File->seek(4 + m_pIconFile->GetDataSize());
51 cycrow 833
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
6 cycrow 834
		C_File *fit = node->Data();
51 cycrow 835
		if (fit == f ) {
58 cycrow 836
			fit->readFromFile(*File, fit->GetDataSize());
6 cycrow 837
			break;
838
		}
58 cycrow 839
		else File->seek(4 + fit->GetDataSize());
6 cycrow 840
	}
841
 
58 cycrow 842
	delete File;
6 cycrow 843
	return true;
844
}
845
 
58 cycrow 846
CFileIO *CBaseFile::_startRead()
6 cycrow 847
{
848
	// no file to read from
58 cycrow 849
	if ( this->filename().empty() ) return NULL;
6 cycrow 850
 
851
	// now open the file
58 cycrow 852
	CFileIO *File = new CFileIO(this->filename());
853
	if ( !File->startRead() ) return NULL;
6 cycrow 854
 
855
	// read the header
58 cycrow 856
	File->readEndOfLine();
6 cycrow 857
	// skip past values
58 cycrow 858
	File->seek(4 + m_SHeader.lValueCompressSize);
6 cycrow 859
 
860
	// read the next header
58 cycrow 861
	File->readEndOfLine();
6 cycrow 862
	// skip past files
58 cycrow 863
	File->seek(4 + m_SHeader2.lSize);
6 cycrow 864
 
58 cycrow 865
	return File;
866
}
6 cycrow 867
 
88 cycrow 868
void CBaseFile::_addFile(C_File *file, bool dontChange)
869
{
870
	if ( !dontChange ) {
871
		file->UpdateSigned();
872
		_changed();
873
	}
874
	m_lFiles.push_back(file);
875
 
876
	_updateTextDB(file);
877
}
878
 
879
void CBaseFile::_updateTextDB(C_File *file)
880
{
881
	if ( !_pTextDB ) _pTextDB = new CTextDB();
882
 
883
	if ( file->GetFileType() == FILETYPE_TEXT ) {
884
		Utils::String baseFile = CFileIO(file->filePointer()).baseName();
885
		int lang = (baseFile.isin("-L")) ? baseFile.right(3) : baseFile.truncate(-4);
886
 
887
		// read in the text file to the database
888
		_pTextDB->parseTextFile(0, 0, file->filePointer(), lang);
889
	}
890
}
891
 
892
void CBaseFile::_resetTextDB()
893
{
894
	if ( _pTextDB ) delete _pTextDB;
895
	_pTextDB = new CTextDB();
896
 
897
	for(CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next()) {
898
		_updateTextDB(node->Data());
899
	}
900
}
901
 
58 cycrow 902
void CBaseFile::ReadIconFileToMemory ()
903
{
904
	if ( !m_pIconFile )	return;
905
	CFileIO *File = _startRead();
906
	if ( !File ) return;
907
 
908
	if ( (!m_pIconFile->GetData()) && (!m_pIconFile->Skip()) )
909
		m_pIconFile->readFromFile(*File, m_pIconFile->GetDataSize());
910
	else
911
		File->seek(4 + m_pIconFile->GetDataSize());
912
 
913
	delete File;
6 cycrow 914
}
915
 
14 cycrow 916
void CBaseFile::_install_adjustFakePatches(CPackages *pPackages)
6 cycrow 917
{
918
	CyStringList lPatches;
14 cycrow 919
	int startfake = pPackages->FindNextFakePatch();
6 cycrow 920
 
14 cycrow 921
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
922
	{
923
		C_File *fit = node->Data();
924
		// only do fake patchs
925
		if ( !fit->IsFakePatch() )
926
			continue;
927
 
928
		// we should only have cat and dat files, but lets check just incase they have been added incorrectly
929
		if ( !fit->CheckFileExt ("cat") && !fit->CheckFileExt("dat") )
930
			continue;
931
 
932
		// search for the name on the list
933
		SStringList *opposite = lPatches.FindString(fit->GetBaseName());
934
		CyString newname;
935
		if ( opposite )
936
			newname = opposite->data;
937
		else
938
		{
939
			newname = CyString::Number((long)startfake).PadNumber(2);
940
			lPatches.PushBack(fit->GetBaseName(), newname);
941
		}
942
 
943
		// rename the file
944
		fit->FixOriginalName();
50 cycrow 945
		CLog::logf(CLog::Log_Install, 2, "Adjusting fake patch number, %s => %s", fit->GetNameDirectory(this).c_str(), (newname + "." + fit->GetFileExt()).c_str());
14 cycrow 946
		fit->SetName ( newname + "." + fit->GetFileExt() );
947
 
948
		// find the next gap
949
		if ( !opposite ) {
950
			startfake = pPackages->FindNextFakePatch(startfake + 1);
951
		}
952
	}
953
 
954
}
955
 
43 cycrow 956
void CBaseFile::_install_renameText(CPackages *pPackages)
14 cycrow 957
{
130 cycrow 958
	int starttext = pPackages->findNextTextFile();
43 cycrow 959
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
960
		C_File *fit = node->Data();
130 cycrow 961
		if ( !fit->isAutoTextFile() )
43 cycrow 962
			continue;
6 cycrow 963
 
130 cycrow 964
		Utils::String newname = SPK::FormatTextName(starttext, pPackages->GetLanguage(), (pPackages->GetCurrentGameFlags() & EXEFLAG_TCTEXT));
43 cycrow 965
		fit->FixOriginalName();
130 cycrow 966
		CLog::logf(CLog::Log_Install, 2, "Adjusting text file, %s => %s", fit->getNameDirectory(this).c_str(), (newname + "." + fit->fileExt()).c_str());
967
		fit->setName(newname + "." + fit->fileExt());
6 cycrow 968
 
43 cycrow 969
		++starttext;
970
	}
971
}
6 cycrow 972
 
50 cycrow 973
bool CBaseFile::_install_setEnabled(bool bEnable, C_File *fit)
974
{
975
	if ( !bEnable )
976
	{
977
		if ( (fit->GetFileType() == FILETYPE_UNINSTALL) || (fit->GetFileType() == FILETYPE_README) || (fit->GetFileType() == FILETYPE_ADVERT) )
978
			bEnable = true;
979
		else if ( (fit->GetFileType() == FILETYPE_EXTRA) && (fit->GetDir().Left(7).ToLower() == "Extras/") )
980
			bEnable = true;
981
		else if ( (IsPatch()) && (fit->GetFileType() == FILETYPE_MOD) && (!fit->IsFakePatch()) )
982
			bEnable = true;
983
 
984
		if ( bEnable ) CLog::logf(CLog::Log_Install, 3, "Filetype(%d) is always enabled, setting enabled flag", fit->GetFileType());
985
	}
986
 
987
	return bEnable;
988
}
989
 
990
bool CBaseFile::_install_uncompress(C_File *fit, CProgressInfo *progress, CyStringList *errorStr, bool *uncomprToFile)
991
{
992
	*uncomprToFile = false;
134 cycrow 993
	_sLastError = fit->getNameDirectory(this);
994
	_iLastError = SPKERR_UNCOMPRESS;
96 cycrow 995
 
50 cycrow 996
	if ( !fit->UncompressData ( progress ) )
997
	{
998
		CLog::log(CLog::Log_Install, 2, "Failed to uncompress data, attempting file decompression");
999
		if ( fit->GetCompressionType() == SPKCOMPRESS_7ZIP )
1000
		{
1001
			if ( fit->UncompressToFile ( NullString, this, false, progress ) )
1002
				*uncomprToFile = true;
1003
		}
1004
 
1005
		if ( !uncomprToFile )
1006
		{
1007
			if ( errorStr )
134 cycrow 1008
				errorStr->PushBack(CyString(_sLastError), ERRORLOG_OLD(SPKINSTALL_UNCOMPRESS_FAIL));
50 cycrow 1009
			CLog::log(CLog::Log_Install, 1, "Unable to decompress file, skipping");
1010
			return false;
1011
		}
1012
	}
1013
	ClearError ();
1014
 
1015
	return true;
1016
}
1017
 
1018
bool CBaseFile::_install_checkVersion(C_File *pFile, const Utils::String &sDestination)
1019
{
1020
	// new check if we should install the file
1021
	// first get the version
1022
	if ( !m_bOverrideFiles && pFile->ReadScriptVersion() )
1023
	{
1024
		CLog::log(CLog::Log_Install, 2, "Checking for existing file version");
1025
		C_File checkfile;
1026
		CyString checkfilename = sDestination;
1027
		if ( !checkfilename.Empty() ) checkfilename += "/";
1028
		checkfilename += pFile->GetNameDirectory(this);
1029
		checkfile.SetFilename ( checkfilename );
127 cycrow 1030
		checkfile.setFileType(pFile->fileType());
50 cycrow 1031
		if ( checkfile.CheckValidFilePointer() ) {
1032
			if ( checkfile.ReadScriptVersion() > pFile->GetVersion() ) {
1033
				CLog::log(CLog::Log_Install, 1, "Newer version of the file found in directory, skipping");
1034
				return false;
1035
			}
1036
		}
1037
	}
1038
 
1039
	return true;
1040
}
1041
 
1042
Utils::String CBaseFile::_install_adjustFilepointer(C_File *pFile, bool bEnabled, const Utils::String &sDestination)
1043
{
130 cycrow 1044
	Utils::String filename = sDestination;
1045
	if ( !filename.empty() ) filename += "/";
50 cycrow 1046
 
130 cycrow 1047
	if ( (IsPatch()) && (pFile->fileType() == FILETYPE_MOD) )
50 cycrow 1048
		pFile->SetDir ( CyString("Patch") );
1049
 
1050
	if ( pFile->IsInMod() )
1051
	{
1052
		if ( bEnabled )
130 cycrow 1053
			pFile->setFilename(filename + pFile->GetInMod().ToString() + "::" + pFile->getNameDirectory(this));
50 cycrow 1054
		else
130 cycrow 1055
			pFile->setFilename(filename + "PluginManager/DisabledFiles.cat::" + pFile->getNameDirectory(this));
50 cycrow 1056
	}
1057
	else
130 cycrow 1058
		pFile->setFilename ( filename + pFile->getNameDirectory(this) );
50 cycrow 1059
 
1060
	if ( !bEnabled )
1061
	{
1062
		if ( !pFile->IsInMod() )
1063
		{
1064
			if ( pFile->IsFakePatch() )
130 cycrow 1065
				pFile->setFilename ( filename + "PluginManager/Disabled/FakePatches/FakePatch_" + this->getNameValidFile() + "_" + this->author() + "_" + pFile->name() );
1066
			else if ( pFile->isAutoTextFile() )
1067
				pFile->setFilename ( filename + "PluginManager/Disabled/TextFiles/Text_" + this->getNameValidFile() + "_" + this->author() + "_" + pFile->name() );
50 cycrow 1068
			else
130 cycrow 1069
				pFile->SetFullDir ( filename + "PluginManager/Disabled/" + pFile->getDirectory(this) );
50 cycrow 1070
		}
1071
		pFile->SetDisabled(true);
1072
	}
1073
 
129 cycrow 1074
	CLog::logf(CLog::Log_Install, 2, "Adjusting the file pointer to correct install destintation, %s", pFile->filePointer().c_str());
50 cycrow 1075
 
130 cycrow 1076
	return filename;
50 cycrow 1077
}
1078
 
1079
C_File *CBaseFile::_install_checkFile(C_File *pFile, CyStringList *errorStr, bool *bDoFile, CLinkList<C_File> *pFileList)
1080
{
1081
	if ( !pFile->IsFakePatch() && pFile->GetFileType() != FILETYPE_README )
1082
	{
1083
		C_File *cFile;
1084
		for ( cFile = pFileList->First(); cFile; cFile = pFileList->Next() )
1085
		{
1086
			if ( !cFile->MatchFile(pFile) ) continue;
1087
			if ( !m_bOverrideFiles && !cFile->CompareNew(pFile) ) {
134 cycrow 1088
				if ( errorStr ) errorStr->PushBack(pFile->GetNameDirectory(this), ERRORLOG_OLD(SPKINSTALL_SKIPFILE));
50 cycrow 1089
				CLog::log(CLog::Log_Install, 1, "Newer version of the file already installed, skipping");
1090
				*bDoFile = false;
1091
			}
1092
			break;
1093
		}
1094
 
1095
		return cFile;
1096
	}
1097
 
1098
	return NULL;
1099
}
1100
 
1101
bool CBaseFile::_install_checkFileEnable(C_File *pCheckFile, C_File *fit, const Utils::String &sDestination, bool bEnabled, CyStringList *errorStr)
1102
{
1103
	// found a file, check if its in the disabled directory
125 cycrow 1104
	Utils::String dir = CFileIO(pCheckFile->filePointer()).dir();
1105
	Utils::String lastDir = CDirIO(dir).topDir().lower();
50 cycrow 1106
 
1107
	// if its disabled, rename it so its enabled
125 cycrow 1108
	if ( ((pCheckFile->IsDisabled()) || (lastDir == "disabled") || (dir.lower().isin("/disabled/"))) && (bEnabled) )
50 cycrow 1109
	{
125 cycrow 1110
		CLog::logf(CLog::Log_Install, 2, "Existing file, %s, is disabled, re-enabling it", pCheckFile->filePointer().c_str());
50 cycrow 1111
		// first check if the directory exists
1112
		if ( pCheckFile->IsInMod() ) {
125 cycrow 1113
			CyString tofile = pCheckFile->filePointer().token("::", 2);
50 cycrow 1114
 
1115
			CCatFile tocat;
125 cycrow 1116
			int err = tocat.open(fit->filePointer().token("::", 1), "", CATREAD_CATDECRYPT, true);
50 cycrow 1117
			if ( (err == CATERR_NONE) || (err == CATERR_CREATED) ) {
129 cycrow 1118
				tocat.AppendFile(pCheckFile->filePointer(), tofile.ToString());
125 cycrow 1119
				CLog::logf(CLog::Log_Install, 2, "Adding existing file into new mod File, %s => %s", fit->filePointer().token("::", 1).c_str(), tofile.c_str());
50 cycrow 1120
			}
1121
 
1122
			CCatFile fromcat;
125 cycrow 1123
			err = fromcat.open(pCheckFile->filePointer().token("::", 1), "", CATREAD_CATDECRYPT, false);
50 cycrow 1124
			if ( err == CATERR_NONE ) {
124 cycrow 1125
				fromcat.removeFile(tofile.ToString());
125 cycrow 1126
				CLog::logf(CLog::Log_Install, 2, "Removing file from existing mod, %s::%s", pCheckFile->filePointer().token("::", 1).c_str(), tofile.c_str());
50 cycrow 1127
			}
1128
 
125 cycrow 1129
			CLog::logf(CLog::Log_Install, 1, "Adjusting existing file name %s => %s", pCheckFile->filePointer().c_str(), fit->filePointer().c_str());
50 cycrow 1130
			pCheckFile->SetFilename ( fit->GetFilePointer() );
1131
			CLog::logf(CLog::Log_Install, 2, "Adjusting In Mod setting, %s => %s", pCheckFile->GetInMod().c_str(), fit->GetInMod().c_str());
1132
			pCheckFile->SetInMod(fit->GetInMod());
1133
		}
1134
		else {
1135
			CyString to = pCheckFile->GetDirectory(this);
1136
			CDirIO Dir(sDestination);
121 cycrow 1137
			if ( !Dir.exists(to.ToString()) ) {
50 cycrow 1138
				if ( !Dir.Create ( to ) ) {
134 cycrow 1139
					if ( errorStr )	errorStr->PushBack(to, ERRORLOG_OLD(SPKINSTALL_CREATEDIRECTORY_FAIL));
50 cycrow 1140
					return false;
1141
				}
134 cycrow 1142
				if ( errorStr )	errorStr->PushBack(to, ERRORLOG_OLD(SPKINSTALL_CREATEDIRECTORY));
50 cycrow 1143
			}
1144
 
1145
			CyString destfile = CyString(sDestination) + "/" + pCheckFile->GetNameDirectory(this);
52 cycrow 1146
			if ( CFileIO(destfile).ExistsOld() ) CFileIO::Remove(destfile.ToString());
50 cycrow 1147
			CLog::logf(CLog::Log_Install, 1, "Adjusting existing filename, %s => %s", pCheckFile->GetFilePointer().c_str(), destfile.c_str());
1148
			rename ( pCheckFile->GetFilePointer().c_str(), destfile.c_str() );
1149
			pCheckFile->SetFilename ( CyString(sDestination) + "/" + pCheckFile->GetNameDirectory(this) );
1150
		}
1151
		pCheckFile->SetDisabled(false);
1152
 
134 cycrow 1153
		if ( errorStr ) errorStr->PushBack(pCheckFile->GetNameDirectory(this), ERRORLOG_OLD(SPKINSTALL_ENABLEFILE));
50 cycrow 1154
	}
1155
 
1156
	return true;
1157
}
51 cycrow 1158
 
1159
bool CBaseFile::_install_createDirectory(CDirIO &Dir, const Utils::String &sTo, C_File *pFile, CyStringList *errorStr)
1160
{
134 cycrow 1161
	_sLastError = sTo;
1162
	if ( !sTo.contains( "::" ) )
51 cycrow 1163
	{
121 cycrow 1164
		if ( !Dir.exists(sTo) )
51 cycrow 1165
		{
1166
			CLog::logf(CLog::Log_Install, 2, "Creating directory to install file into, %s", sTo.c_str());
1167
			if ( !Dir.Create(sTo) )
1168
			{
1169
				if ( errorStr )
134 cycrow 1170
					errorStr->PushBack(CyString(sTo), ERRORLOG_OLD(SPKINSTALL_CREATEDIRECTORY_FAIL));
51 cycrow 1171
				return false;
1172
			}
1173
			if ( errorStr )
134 cycrow 1174
				errorStr->PushBack(CyString(sTo), ERRORLOG_OLD(SPKINSTALL_CREATEDIRECTORY));
51 cycrow 1175
		}
1176
	}
1177
	else {
129 cycrow 1178
		CLog::logf(CLog::Log_Install, 2, "Adjusting file extension for file in mod, %s => %s", pFile->filePointer().c_str(), CCatFile::PckChangeExtension(pFile->filePointer()).c_str());
134 cycrow 1179
		pFile->setFilename(CCatFile::PckChangeExtension(pFile->filePointer()));
51 cycrow 1180
	}
1181
 
1182
	return true;
1183
}
1184
 
1185
void CBaseFile::_install_writeFile(C_File *pFile, const Utils::String &sDestination, CyStringList *errorStr)
1186
{
134 cycrow 1187
	_iLastError = SPKERR_WRITEFILE;
1188
	_sLastError = pFile->filePointer();
1189
	Utils::String sInstalledFile = pFile->getNameDirectory(this);
51 cycrow 1190
	if ( pFile->IsDisabled() )
1191
	{
134 cycrow 1192
		sInstalledFile = pFile->filePointer().findRemove(sDestination);
51 cycrow 1193
		if ( sInstalledFile[0] == '/' || sInstalledFile[0] == '\\' )
134 cycrow 1194
			sInstalledFile.erase(0, 1);
51 cycrow 1195
	}
1196
 
129 cycrow 1197
	if ( !pFile->writeFilePointer() )
51 cycrow 1198
	{
1199
		CLog::log(CLog::Log_Install, 1, "Failed to write the file");
1200
		if ( errorStr )
134 cycrow 1201
			errorStr->PushBack(CyString(sInstalledFile), ERRORLOG_OLD(SPKINSTALL_WRITEFILE_FAIL));
51 cycrow 1202
	}
1203
	else
1204
	{
1205
		CLog::log(CLog::Log_Install, 1, "File written successfully");
1206
		CLog::log(CLog::Log_Install, 2, "Checking signed status of the file");
1207
		pFile->UpdateSigned();
1208
		if ( errorStr )
134 cycrow 1209
			errorStr->PushBack(CyString(sInstalledFile), ERRORLOG_OLD(SPKINSTALL_WRITEFILE));
51 cycrow 1210
 
1211
		switch(pFile->GetFileType())
1212
		{
1213
			case FILETYPE_SCRIPT:
1214
			case FILETYPE_UNINSTALL:
1215
				CLog::log(CLog::Log_Install, 2, "Updating file signature");
1216
				pFile->UpdateSignature();
1217
				break;
1218
		}
1219
	}
1220
}
1221
 
43 cycrow 1222
bool CBaseFile::InstallFiles ( CyString destdir, CProgressInfo *progress, CLinkList<C_File> *filelist, CyStringList *errorStr, bool enabled, CPackages *packages )
1223
{
50 cycrow 1224
	//TODO: add errorStr and progress as member variables
43 cycrow 1225
	if ( enabled ) {
14 cycrow 1226
		this->_install_adjustFakePatches(packages);
43 cycrow 1227
		if ( packages ) this->_install_renameText(packages);
6 cycrow 1228
	}
1229
 
50 cycrow 1230
	bool bFailed = false;
1231
 
6 cycrow 1232
	CDirIO Dir(destdir);
1233
	int fileCount = 0;
1234
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
1235
	{
1236
		C_File *fit = node->Data();
1237
 
50 cycrow 1238
		// start the install process, check if we need to the file enabled or disabled
1239
		CLog::logf(CLog::Log_Install, 1, "Preparing to install file: %s", fit->GetNameDirectory(this).c_str());
1240
		bool fileEnabled = _install_setEnabled(enabled, fit);
1241
 
1242
		// check if the file is for the correct game version
147 cycrow 1243
		if (!fit->isForGame(packages->GetGame())) 
1244
		{
1245
			CLog::logf(CLog::Log_Install, 1, "File didn't match game version, skipping, %d != %d", fit->game(), packages->GetGame());
1246
			continue;
6 cycrow 1247
		}
1248
 
50 cycrow 1249
		// update the progress display to show we are processing this file
1250
		if ( progress ) {
1251
			if ( progress->IsSecond() ) progress->SwitchSecond();
6 cycrow 1252
			progress->UpdateFile ( fit );
1253
			progress->UpdateProgress(fileCount++, m_lFiles.size());
1254
			progress->SwitchSecond();
1255
		}
1256
 
1257
		// first uncompress the file
50 cycrow 1258
		//TODO: add this flag to C_File
1259
		bool uncomprToFile;
1260
		if ( !this->_install_uncompress(fit, progress, errorStr, &uncomprToFile) ) {
1261
			bFailed = true;
1262
			continue;
6 cycrow 1263
		}
1264
 
50 cycrow 1265
		bool dofile = _install_checkVersion(fit, destdir.ToString());
6 cycrow 1266
 
1267
		// change file pointer
125 cycrow 1268
		Utils::String sInstallDir = _install_adjustFilepointer(fit, fileEnabled, destdir.ToString());
6 cycrow 1269
 
1270
		C_File *adjustPointer = NULL;
1271
 
50 cycrow 1272
		if ( filelist ) {
98 cycrow 1273
			C_File *cFile = _install_checkFile(fit, errorStr, &dofile, filelist);
6 cycrow 1274
 
1275
			// no matching file found, adding to main list
50 cycrow 1276
			if ( !cFile ) filelist->push_back ( fit );
6 cycrow 1277
			else
1278
			{
1279
				// if the file is not enabled, we need to check for any that might be enabled
51 cycrow 1280
				if ( !fileEnabled ) //_install_checkDisabled(cFile, destdir, errorStr);
6 cycrow 1281
				{
50 cycrow 1282
					//TODO: check what this is actually doing
6 cycrow 1283
					if ( !cFile->GetUsed() )
1284
					{
1285
						CFileIO rFile(cFile->GetFilePointer());
52 cycrow 1286
						if ( rFile.exists() )
6 cycrow 1287
						{
1288
							if ( errorStr )
1289
							{
52 cycrow 1290
								if ( rFile.remove() )
134 cycrow 1291
									errorStr->PushBack(cFile->GetFilePointer().Remove(destdir), ERRORLOG_OLD(SPKINSTALL_DELETEFILE));
6 cycrow 1292
								else
134 cycrow 1293
									errorStr->PushBack(cFile->GetFilePointer().Remove(destdir), ERRORLOG_OLD(SPKINSTALL_DELETEFILE_FAIL));
6 cycrow 1294
							}
1295
						}
1296
						cFile->SetFilename(fit->GetFilePointer());
1297
						cFile->SetDisabled(true);
1298
					}
1299
					else
1300
					{
125 cycrow 1301
						fit->SetFullDir ( CyString(sInstallDir) + fit->GetDirectory(this) );
51 cycrow 1302
						fit->SetDisabled(false);
6 cycrow 1303
					}
1304
				}
1305
				// move it to enabled
50 cycrow 1306
				else {
1307
					if ( !this->_install_checkFileEnable(cFile, fit, destdir.ToString(), fileEnabled, errorStr) ) {
1308
						bFailed = true;
1309
						continue;
6 cycrow 1310
					}
1311
				}
1312
 
1313
				adjustPointer = cFile;
50 cycrow 1314
				if ( dofile ) adjustPointer->SetCreationTime(fit->GetCreationTime());
6 cycrow 1315
			}
1316
		}
1317
 
1318
		if ( dofile )
1319
		{
1320
			// uncompressed to file, rename and move
1321
			if ( uncomprToFile )
1322
			{
134 cycrow 1323
				_iLastError = SPKERR_WRITEFILE;
1324
				Utils::String to = fit->getDirectory(this);
1325
				if ( !fileEnabled )	to = "PluginManager/Disabled/" + to;
6 cycrow 1326
 
134 cycrow 1327
				if ( !_install_createDirectory(Dir, to, fit, errorStr) ) {
51 cycrow 1328
					bFailed = true;
1329
					continue;
6 cycrow 1330
				}
1331
 
1332
				int err = 1;
134 cycrow 1333
				_sLastError = to;
51 cycrow 1334
				if ( !fit->GetTempFile ().Empty() )	err = rename ( fit->GetTempFile().c_str(), to.c_str() );
1335
				if ( err ) {
1336
					bFailed = true;
1337
					continue;
1338
				}
6 cycrow 1339
			}
1340
			//otherwise, just extract the file
1341
			else
1342
			{
1343
				// old file is found in list, switch to using new one
50 cycrow 1344
				if ( (filelist) && (adjustPointer) ) {
6 cycrow 1345
					adjustPointer->CopyData(fit, false);
50 cycrow 1346
					CLog::log(CLog::Log_Install, 2, "Copying data into existing file");
1347
				}
6 cycrow 1348
 
134 cycrow 1349
				Utils::String fpointer = fit->filePointer();
1350
				_iLastError = SPKERR_CREATEDIRECTORY;
1351
				Utils::String dir = CFileIO(fit->filePointer()).dir();
6 cycrow 1352
 
134 cycrow 1353
				dir = dir.findRemove(destdir.ToString());
149 cycrow 1354
				if (!dir.empty() && (dir[0] == '/' || dir[0] == '\\')) 
1355
					dir.erase(0, 1);
6 cycrow 1356
 
134 cycrow 1357
				if ( !_install_createDirectory(Dir, dir, fit, errorStr) ) {
51 cycrow 1358
					bFailed = true;
1359
					continue;
6 cycrow 1360
				}
1361
 
51 cycrow 1362
				_install_writeFile(fit, destdir.ToString(), errorStr);
6 cycrow 1363
			}
1364
			ClearError ();
1365
		}
1366
 
1367
		if ( adjustPointer )
1368
		{
50 cycrow 1369
			CLog::log(CLog::Log_Install, 2, "Adjusting pointers to existing file and deleting new file pointer");
6 cycrow 1370
			node->ChangeData(adjustPointer);
1371
			delete fit;
1372
		}
50 cycrow 1373
 
1374
		CLog::log(CLog::Log_Install, 1, "File installation completed");
6 cycrow 1375
	}
1376
 
1377
	// now clear or data memory
50 cycrow 1378
	CLog::log(CLog::Log_Install, 2, "Delting temporary file data from memory");
51 cycrow 1379
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) node->Data()->DeleteData();
6 cycrow 1380
 
50 cycrow 1381
	return !bFailed;
6 cycrow 1382
}
1383
 
1384
/*######################################################################################################*/
1385
 
1386
 
1387
/*
1388
	Func:   ParseHeader
1389
	Input:  Header String - string formated directly from the file
1390
	Return: Boolean - If string is a valid header
1391
	Desc:   Splits up the main header string to get all required settings
1392
*/
1393
bool CBaseFile::ParseHeader ( CyString header )
1394
{
14 cycrow 1395
	if ( !this->CheckHeader(header.GetToken ( 1, ';' ).ToString()) )
6 cycrow 1396
		return false;
1397
 
1398
	m_SHeader.fVersion = header.GetToken ( 2, ';' ).ToFloat();
1399
	if ( m_SHeader.fVersion > FILEVERSION )
1400
		return false;
1401
 
1402
	m_SHeader.iValueCompression = header.GetToken ( 3, ';' ).ToInt();
1403
	m_SHeader.lValueCompressSize = header.GetToken ( 4, ';' ).ToLong();
1404
 
1405
	return true;
1406
}
1407
 
14 cycrow 1408
bool CBaseFile::CheckHeader(const Utils::String header) const
6 cycrow 1409
{
1410
	if ( header.Compare("BaseCycrow") )
1411
		return true;
1412
	return false;
1413
}
1414
 
1415
/*
1416
	Func:   ParseFileHeader
1417
	Input:  Header String - string formated directly from the file
1418
	Return: Boolean - If string is a valid header
1419
	Desc:   Splits up the file header string to get all required settings
1420
*/
1421
bool CBaseFile::ParseFileHeader ( CyString header )
1422
{
1423
	if ( header.GetToken ( 1, ';' ) != "FileHeader" )
1424
		return false;
1425
 
1426
	m_SHeader2.iNumFiles = header.GetToken ( 2, ';' ).ToInt();
1427
	m_SHeader2.lSize = header.GetToken ( 3, ';' ).ToInt();
1428
	m_SHeader2.lFullSize = header.GetToken ( 4, ';' ).ToInt();
1429
	m_SHeader2.iFileCompression = header.GetToken ( 5, ';' ).ToInt();
1430
	m_SHeader2.iDataCompression = header.GetToken ( 6, ';' ).ToInt();
1431
 
1432
	return true;
1433
}
1434
 
1435
 
1436
/*
1437
	Func:   ParseValueLine
1438
	Input:  String - single line from a file to set
1439
	Return: Boolean - returns true if value exists
1440
	Desc:   Reads the line and assigns the parameters for the file
1441
*/
14 cycrow 1442
bool CBaseFile::ParseValueLine(const Utils::String &sLine)
6 cycrow 1443
{
14 cycrow 1444
	Utils::String first = sLine.token(" ", 1);
1445
	Utils::String rest  = sLine.tokens(" ", 2);
6 cycrow 1446
 
50 cycrow 1447
	if ( first.Compare("Name:") )					this->setName(rest);
1448
	else if ( first.Compare("Author:") )			this->setAuthor(rest);
1449
	else if ( first.Compare("Version:") )			this->setVersion(rest);
14 cycrow 1450
	else if ( first.Compare("fGameVersion:") ) {
6 cycrow 1451
		if ( m_lGames.Back() ) {
1452
			m_lGames.Back()->Data()->sVersion = rest;
1453
		}
1454
	}
14 cycrow 1455
	else if ( first.Compare("GameVersion:") ) {
6 cycrow 1456
		if ( m_lGames.Back() ) {
14 cycrow 1457
			m_lGames.Back()->Data()->iVersion = rest;
6 cycrow 1458
		}
1459
	}
14 cycrow 1460
	else if ( first.Compare("Game:") )
46 cycrow 1461
		this->AddGameCompatability(rest, "");
14 cycrow 1462
	else if ( first.Compare("GameCompat:") )
1463
		this->AddGameCompatability(rest.token(" ", 1), rest.tokens(" ", 2));
1464
	else if ( first.Compare("GameCompatExact:") )
1465
		this->AddGameCompatability(rest.token(" ", 1), rest.tokens(" ", 2));
50 cycrow 1466
	else if ( first.Compare("Date:") )				this->setCreationDate(rest);
49 cycrow 1467
	else if ( first.Compare("WebAddress:") )		this->setWebAddress(rest);
1468
	else if ( first.Compare("WebSite:") )			this->setWebSite(rest);
1469
	else if ( first.Compare("Email:") )				this->setEmail(rest);
14 cycrow 1470
	else if ( first.Compare("WebMirror1:") || first.Compare("Mirror1:") || first.Compare("WebMirror:") )
6 cycrow 1471
		this->AddWebMirror(rest);
14 cycrow 1472
	else if ( first.Compare("WebMirror2:") || first.Compare("Mirror2:") )
6 cycrow 1473
		this->AddWebMirror(rest);
48 cycrow 1474
	else if ( first.Compare("PluginType:") )		this->setPluginType(rest);
1475
	else if ( first.Compare("Desc:") )				this->setDescription(rest);
1476
	else if ( first.Compare("UninstallAfter:") )	this->addUninstallText(ParseLanguage(rest.token("|", 1)), false, rest.tokens("|", 2));
1477
	else if ( first.Compare("UninstallBefore:") )	this->addUninstallText(ParseLanguage(rest.token("|", 1)), true, rest.tokens("|", 2));
1478
	else if ( first.Compare("InstallAfter:") )		this->addInstallText(ParseLanguage(rest.token("|", 1)), false, rest.tokens("|", 2));
1479
	else if ( first.Compare("InstallBefore:") )		this->addInstallText(ParseLanguage(rest.token("|", 1)), true, rest.tokens("|", 2));
14 cycrow 1480
	else if ( first.Compare("ScriptName:") )
1481
		AddLanguageName(ParseLanguage(rest.token(":", 1)), rest.token(":", 2));
48 cycrow 1482
	else if ( first.Compare("GameChanging:") )		this->setGameChanging(rest);
1483
	else if ( first.Compare("EaseOfUse:") )			this->setEaseOfUse(rest);
1484
	else if ( first.Compare("Recommended:") )		this->setRecommended(rest);
14 cycrow 1485
	else if ( first.Compare("NeededLibrary:") )
1486
		this->AddNeededLibrary(rest.token("||", 1), rest.token("||", 2), rest.token("||", 3));
1487
	else if ( first.Compare("FakePatchBefore:") )
1488
		this->AddFakePatchOrder(false, rest.token("||", 1), rest.token("||", 2));
1489
	else if ( first.Compare("FakePatchAfter:") )
1490
		this->AddFakePatchOrder(true, rest.token("||", 1), rest.token("||", 2));
49 cycrow 1491
	else if ( first.Compare("ForumLink:") )			this->setForumLink(rest);
6 cycrow 1492
	else
1493
		return false;
1494
 
1495
	return true;
1496
}
1497
 
48 cycrow 1498
int CBaseFile::ParseLanguage(const Utils::String &lang) const
6 cycrow 1499
{
48 cycrow 1500
	int langID = lang;
1501
	if ( !langID ) {
1502
		if ( lang.Compare("english") )		return 44;
1503
		else if ( lang.Compare("default") )	return 0;
1504
		else if ( lang.Compare("german") )	return 49;
1505
		else if ( lang.Compare("russian") )	return 7;
1506
		else if ( lang.Compare("spanish") )	return 34;
1507
		else if ( lang.Compare("french") )	return 33;
6 cycrow 1508
	}
1509
 
1510
	return langID;
1511
}
1512
 
1513
/*
1514
	Func:   ReadValues
1515
	Input:  String - values in one long line
1516
	Desc:   splits the values data into each line to read the data
1517
*/
1518
void CBaseFile::ReadValues ( CyString values )
1519
{
1520
	int num = 0;
1521
	CyString *lines = values.SplitToken ( '\n', &num );
1522
 
1523
	for ( int i = 0; i < num; i++ )
14 cycrow 1524
		ParseValueLine ( lines[i].ToString() );
6 cycrow 1525
 
1526
	CLEANSPLIT(lines, num)
1527
}
1528
 
1529
/*
1530
	Func:   ParseFilesLine
1531
	Input:  String - single line from a file to set
1532
	Return: Boolean - returns true if value exists
1533
	Desc:   Reads the line and assigns the parameters for the file
1534
*/
127 cycrow 1535
bool CBaseFile::ParseFilesLine ( CyString sLine )
6 cycrow 1536
{
127 cycrow 1537
	Utils::String line = sLine.ToString();
1538
 
1539
	if ( !line.contains(":") )
6 cycrow 1540
		return false;
1541
 
127 cycrow 1542
	Utils::String command = line.token(":", 1);
6 cycrow 1543
 
127 cycrow 1544
	long size = line.token(":", 2).toInt();
1545
	long usize = line.token(":", 3).toInt ();
1546
	long compression = line.token(":", 4).toInt ();
6 cycrow 1547
 
1548
	if ( command == "Icon" )
1549
	{
127 cycrow 1550
		m_sIconExt = line.token (":", 5);
6 cycrow 1551
		m_pIconFile = new C_File ();
1552
		m_pIconFile->SetDataSize ( size - 4 );
1553
		m_pIconFile->SetDataCompression ( compression );
1554
		m_pIconFile->SetUncompressedDataSize ( usize );
1555
 
1556
		return true;
1557
	}
1558
 
127 cycrow 1559
	time_t time = line.token(":", 5).toLong();
1560
	bool compressToFile = (line.token(":", 6).toInt() == 1) ? true : false;
1561
	Utils::String name  = line.token(":", 7);
1562
	Utils::String dir = line.token(":", 8);
6 cycrow 1563
 
127 cycrow 1564
	if ( name.empty() )
6 cycrow 1565
		return true;
1566
 
1567
	bool shared = false;
127 cycrow 1568
	if ( command.left(1) == "$" )
6 cycrow 1569
	{
1570
		shared = true;
127 cycrow 1571
		command.erase(0, 1);
6 cycrow 1572
	}
1573
 
127 cycrow 1574
	FileType type = FILETYPE_UNKNOWN;
6 cycrow 1575
	if ( command == "Script" )
1576
		type = FILETYPE_SCRIPT;
1577
	else if ( command == "Text" )
1578
		type = FILETYPE_TEXT;
1579
	else if ( command == "Readme" )
1580
		type = FILETYPE_README;
1581
	else if ( command == "Map" )
1582
		type = FILETYPE_MAP;
1583
	else if ( command == "Mod" )
1584
		type = FILETYPE_MOD;
1585
	else if ( command == "Uninstall" )
1586
		type = FILETYPE_UNINSTALL;
1587
	else if ( command == "Sound" )
1588
		type = FILETYPE_SOUND;
1589
	else if ( command == "Mission" )
1590
		type = FILETYPE_MISSION;
1591
	else if ( command == "Extra" )
1592
		type = FILETYPE_EXTRA;
1593
	else if ( command == "Screen" )
1594
		type = FILETYPE_SCREEN;
1595
	else if ( command == "Backup" )
1596
		type = FILETYPE_BACKUP;
1597
	else if ( command == "Advert" )
1598
		type = FILETYPE_ADVERT;
1599
	else if ( command == "ShipScene" )
1600
		type = FILETYPE_SHIPSCENE;
1601
	else if ( command == "CockpitScene" )
1602
		type = FILETYPE_COCKPITSCENE;
1603
	else if ( command == "ShipOther" )
1604
		type = FILETYPE_SHIPOTHER;
1605
	else if ( command == "ShipModel" )
1606
		type = FILETYPE_SHIPMODEL;
1607
 
127 cycrow 1608
	if (type == FILETYPE_UNKNOWN)
6 cycrow 1609
		return false;
1610
 
127 cycrow 1611
	C_File *file = new C_File();
6 cycrow 1612
 
127 cycrow 1613
	if (dir.left(5).Compare("GAME_")) {
130 cycrow 1614
		unsigned int iGame = dir.token("_", 2).toInt();
1615
		if (!iGame)
1616
			file->setGame(0);
1617
		else
1618
			file->setGame(1 << 31 | 1 << iGame);
127 cycrow 1619
		dir = Utils::String::Null();
6 cycrow 1620
	} 
127 cycrow 1621
	else if ( line.countToken(":") >= 9 ) 
1622
	{
1623
		Utils::String game = line.token(":", 9);
1624
		if (game.contains("_"))
130 cycrow 1625
		{
1626
			unsigned int iGame = game.token("_", 2).toInt();
1627
			if (iGame)
1628
				file->setGame(1 << 31 | 1 << iGame);
1629
			else
1630
				file->setGame(0);
1631
		}
127 cycrow 1632
		else
1633
		{
1634
			int iGame = game.toInt();
1635
			if (iGame & (1 << 31))
1636
				file->setGame(iGame);
130 cycrow 1637
			else if (!iGame)
1638
				file->setGame(0);
127 cycrow 1639
			else
1640
				file->setGame(1 << 31 | 1 << iGame);
1641
		}
6 cycrow 1642
	}
1643
 
127 cycrow 1644
	if (dir.Compare("NULL")) {
1645
		dir = Utils::String::Null();
1646
	}
1647
 
1648
	file->setFileType(type);
6 cycrow 1649
	file->SetCreationTime ( time );
1650
	file->SetName ( name );
1651
	file->SetDir ( dir );
1652
	file->SetDataSize ( size - 4 );
1653
	file->SetDataCompression ( compression );
1654
	file->SetUncompressedDataSize ( usize );
1655
	file->SetShared ( shared );
1656
	file->SetCompressedToFile ( compressToFile );
1657
 
88 cycrow 1658
	_addFile(file, true);
6 cycrow 1659
 
1660
	return true;
1661
}
1662
 
1663
 
1664
/*
1665
	Func:   ParseFiles
1666
	Input:  String - values in one long line
1667
	Desc:   splits the files data into each line to read the data
1668
*/
1669
void CBaseFile::ReadFiles ( CyString values )
1670
{
1671
	int num = 0;
1672
	CyString *lines = values.SplitToken ( '\n', &num );
1673
 
1674
	for ( int i = 0; i < num; i++ )
1675
		ParseFilesLine ( lines[i] );
1676
 
1677
	CLEANSPLIT(lines, num)
1678
}
1679
 
1680
 
1681
 
1682
/*
1683
	Func:   ReadFile
1684
	Input:  filename - the name of the file to open and read
1685
			readdata - If falses, dont read the files to memory, just read the headers and values
1686
	Return: boolean - return ture if acceptable format
1687
	Desc:   Opens and reads the spk file and loads all data into class
1688
*/
130 cycrow 1689
bool CBaseFile::ReadFile(CyString filename, int readtype, CProgressInfo *progress)
6 cycrow 1690
{
130 cycrow 1691
	return readFile(filename.ToString(), readtype, progress);
1692
}
1693
bool CBaseFile::readFile(const Utils::String &filename, int readtype, CProgressInfo *progress)
1694
{
1695
	CFileIO File(filename);
58 cycrow 1696
	if ( !File.startRead() ) return false;
6 cycrow 1697
 
58 cycrow 1698
	bool ret = this->readFile(File, readtype, progress);
130 cycrow 1699
	if ( ret ) this->setFilename(filename);
6 cycrow 1700
 
58 cycrow 1701
	File.close();
6 cycrow 1702
	return ret;
1703
}
51 cycrow 1704
 
109 cycrow 1705
int CBaseFile::_read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress)
6 cycrow 1706
{
51 cycrow 1707
	int doneLen = 0;
6 cycrow 1708
 
51 cycrow 1709
	// read data to memory
1710
	unsigned char *readData;
1711
	try {
1712
		readData = new unsigned char[m_SHeader.lValueCompressSize];
1713
	}
1714
	catch (std::exception &e) {
1715
		CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_Header() unable to malloc [header], %d (%s)", m_SHeader.lValueCompressSize, e.what());
1716
		return -1;
1717
	}
6 cycrow 1718
 
51 cycrow 1719
	unsigned char size[4];
109 cycrow 1720
	File.read(size, 4);
1721
	File.read(readData, m_SHeader.lValueCompressSize);
6 cycrow 1722
 
51 cycrow 1723
	unsigned long uncomprLen = (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
6 cycrow 1724
 
51 cycrow 1725
	// check for zlib compression
1726
	if ( m_SHeader.iValueCompression == SPKCOMPRESS_ZLIB ) {
1727
		// uncomress the data
1728
		try {
6 cycrow 1729
			unsigned char *uncompr = new unsigned char[uncomprLen];
1730
			int err = uncompress ( uncompr, &uncomprLen, readData, m_SHeader.lValueCompressSize );
1731
			// update the progress for each section
51 cycrow 1732
			if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(2, iMaxProgress);
1733
			if ( err == Z_OK ) this->ReadValues ( CyString ((char *)uncompr) );
6 cycrow 1734
			doneLen = uncomprLen;
1735
			delete uncompr;
1736
		}
51 cycrow 1737
		catch (std::exception &e) {
1738
			CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_Header() unable to malloc [uncompr], %d (%s)", uncomprLen, e.what());
53 cycrow 1739
			delete []readData;
51 cycrow 1740
			return -1;
6 cycrow 1741
		}
51 cycrow 1742
	}
1743
	else if ( m_SHeader.iValueCompression == SPKCOMPRESS_7ZIP ) {
1744
		long len = uncomprLen;
1745
		unsigned char *compr = LZMADecode_C ( readData, m_SHeader.lValueCompressSize, (size_t*)&len, NULL );
1746
		// update the progress for each section
1747
		if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(2, iMaxProgress);
6 cycrow 1748
 
51 cycrow 1749
		if ( compr ) ReadValues ( CyString ((char *)compr) );
6 cycrow 1750
	}
51 cycrow 1751
	// no compression
1752
	else
1753
		ReadValues ( CyString ((char *)readData) );
6 cycrow 1754
 
53 cycrow 1755
	delete []readData;
6 cycrow 1756
 
51 cycrow 1757
	return doneLen;
1758
}
6 cycrow 1759
 
109 cycrow 1760
int CBaseFile::_read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress)
51 cycrow 1761
{
1762
	unsigned char *readData;
1763
	try {
1764
		readData = new unsigned char[m_SHeader2.lSize];
1765
	}
1766
	catch (std::exception &e) {
1767
		CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_FileHeader() unable to malloc [header], %d (%s)", m_SHeader2.lSize, e.what());
1768
		return -1;
1769
	}
6 cycrow 1770
 
51 cycrow 1771
	unsigned char size[4];
109 cycrow 1772
	File.read(size, 4);
1773
	File.read(readData, m_SHeader2.lSize);
6 cycrow 1774
 
51 cycrow 1775
	unsigned long uncomprLen = (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
6 cycrow 1776
 
51 cycrow 1777
	// check for zlib compression
1778
	if ( m_SHeader.iValueCompression == SPKCOMPRESS_ZLIB ) {
1779
		if ( uncomprLen < (unsigned long)iDoneLen ) uncomprLen = iDoneLen;
6 cycrow 1780
 
51 cycrow 1781
		try {
6 cycrow 1782
			unsigned char *uncompr = new unsigned char[uncomprLen];
1783
			int err = uncompress ( uncompr, &uncomprLen, readData, m_SHeader2.lSize );
1784
			// update the progress for each section
51 cycrow 1785
			if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(5, iMaxProgress);
1786
			if ( err == Z_OK ) ReadFiles ( CyString ((char *)uncompr) );
6 cycrow 1787
			delete uncompr;
1788
		}
51 cycrow 1789
		catch (std::exception &e) {
1790
			CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_FileHeader() unable to malloc [uncompr], %d (%s)", uncomprLen, e.what());
53 cycrow 1791
			delete []readData;
51 cycrow 1792
			return -1;
6 cycrow 1793
		}
1794
 
1795
	}
51 cycrow 1796
	else if ( m_SHeader.iValueCompression == SPKCOMPRESS_7ZIP )
1797
	{
1798
		long len = uncomprLen;
1799
		unsigned char *compr = LZMADecode_C ( readData, m_SHeader2.lSize, (size_t*)&len, NULL );
1800
		// update the progress for each section
1801
		if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(5, iMaxProgress);
1802
		if ( compr ) ReadFiles ( CyString ((char *)compr) );
1803
	}
1804
	else
1805
		ReadFiles ( CyString ((char *)readData) );
6 cycrow 1806
 
53 cycrow 1807
	delete []readData;
51 cycrow 1808
	return true;
1809
}
1810
 
58 cycrow 1811
bool CBaseFile::readFile(CFileIO &File, int readtype, CProgressInfo *progress)
51 cycrow 1812
{
1813
	ClearError ();
1814
 
1815
	// first read the header
58 cycrow 1816
	if ( !ParseHeader(File.readEndOfLine()) ) return false;
51 cycrow 1817
	if ( readtype == SPKREAD_HEADER ) return true;
1818
 
1819
	// update the progress for each section
1820
	int maxProgress = (readtype == SPKREAD_VALUES) ? 3 : 6;
1821
	if ( readtype != SPKREAD_ALL && progress ) progress->UpdateProgress(1, maxProgress);
1822
 
1823
	long doneLen = 0;
1824
	// next read the data values for the spk files
109 cycrow 1825
	if ( m_SHeader.lValueCompressSize ) doneLen = this->_read_Header(File, readtype, maxProgress, progress);
51 cycrow 1826
 
1827
	// update the progress for each section
1828
	if ( readtype != SPKREAD_ALL && progress ) progress->UpdateProgress(3, maxProgress);
1829
	if ( readtype == SPKREAD_VALUES ) return true;
1830
 
1831
	// next should be the next header
58 cycrow 1832
	if ( !ParseFileHeader(File.readEndOfLine()) ) return false;
51 cycrow 1833
 
1834
	// clear the current file list
1835
	m_lFiles.clear(true);
88 cycrow 1836
	if ( _pTextDB ) {
1837
		delete _pTextDB;
1838
		_pTextDB = NULL;
1839
	}
51 cycrow 1840
 
1841
	// update the progress for each section
1842
	if ( readtype != SPKREAD_ALL && progress ) progress->UpdateProgress(4, maxProgress);
1843
 
109 cycrow 1844
	if ( m_SHeader2.lSize ) this->_read_FileHeader(File, readtype, maxProgress, doneLen, progress);
51 cycrow 1845
 
6 cycrow 1846
	// file mismatch
1847
	long numfiles = m_lFiles.size();
51 cycrow 1848
	if ( m_pIconFile ) ++numfiles;
1849
	if ( m_SHeader2.iNumFiles != numfiles ) {
134 cycrow 1850
		_iLastError = SPKERR_FILEMISMATCH;
6 cycrow 1851
		return false;
1852
	}
1853
 
1854
	// update the progress for each section
51 cycrow 1855
	if ( readtype != SPKREAD_ALL && progress )	progress->UpdateProgress(6, maxProgress);
6 cycrow 1856
 
51 cycrow 1857
	if ( readtype == SPKREAD_ALL ) {
6 cycrow 1858
		int fileCount = 2;
58 cycrow 1859
		if ( m_pIconFile ) m_pIconFile->readFromFile(File, m_pIconFile->GetDataSize());
6 cycrow 1860
 
51 cycrow 1861
		// ok finally we need to read all the file
1862
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
58 cycrow 1863
			node->Data()->readFromFile(File, node->Data()->GetDataSize());
51 cycrow 1864
			if ( progress )	progress->UpdateProgress(fileCount++, m_lFiles.size() + 2);
6 cycrow 1865
		}
1866
 
1867
		m_bFullyLoaded = true;
1868
	}
1869
 
1870
	return true;
1871
}
1872
 
1873
bool CBaseFile::IsMod()
1874
{
1875
	// check for any mod files that are not fake patchs
1876
	for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
1877
	{
1878
		C_File *f = fNode->Data();
1879
		if ( f->GetFileType() != FILETYPE_MOD )
1880
			continue;
1881
 
1882
		if ( !f->IsFakePatch() )
1883
			return true;
1884
	}
1885
 
1886
	return false;
1887
}
1888
 
13 cycrow 1889
bool CBaseFile::IsFakePatch() const
6 cycrow 1890
{
1891
	// check for any mod files that are not fake patchs
1892
	for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
1893
	{
1894
		C_File *f = fNode->Data();
1895
		if ( f->GetFileType() != FILETYPE_MOD )
1896
			continue;
1897
 
1898
		if ( f->IsFakePatch() )
1899
			return true;
1900
	}
1901
 
1902
	return false;
1903
}
1904
 
14 cycrow 1905
Utils::String CBaseFile::CreateValuesLine () const
6 cycrow 1906
{
14 cycrow 1907
	Utils::String values("Name: ");
50 cycrow 1908
	values += this->name() + "\n";
1909
	values += "Author: " + this->author() + "\n";
1910
	values += "Version: " + this->version() + "\n";
1911
	if ( !this->creationDate().empty() )values += "Date: "			+ this->creationDate()	+ "\n";
49 cycrow 1912
	if ( !this->webAddress().empty() )	values += "WebAddress: "	+ this->webAddress()	+ "\n";
1913
	if ( !this->webSite().empty() )		values += "WebSite: "		+ this->webSite()		+ "\n";
1914
	if ( !this->email().empty() )		values += "Email: "			+ this->email()			+ "\n";
1915
	if ( !this->forumLink().empty() )	values += "ForumLink: "		+ this->forumLink()		+ "\n";
1916
 
6 cycrow 1917
	for ( SStringList *str = m_lMirrors.Head(); str; str = str->next )
14 cycrow 1918
		values += Utils::String("WebMirror: ") + str->str.ToString() + "\n";
48 cycrow 1919
	if ( !this->description().empty() ) {
1920
		Utils::String desc = this->description();
14 cycrow 1921
		desc = desc.findReplace("<newline>", "<br>");
1922
		desc = desc.findReplace("\n", "<br>");
1923
		desc.remove('\r');
1924
		values += "Desc: " + desc + "\n";
6 cycrow 1925
	}
1926
 
1927
	for ( CListNode<SGameCompat> *gc = m_lGames.Front(); gc; gc = gc->next() ) {
46 cycrow 1928
		if ( !gc->Data()->sVersion.empty() )
1929
			values += Utils::String("GameCompatExact: ") + (long)gc->Data()->iGame + " " + gc->Data()->sVersion + "\n";
6 cycrow 1930
		else
14 cycrow 1931
			values += Utils::String("GameCompat: ") + (long)gc->Data()->iGame + " " + (long)gc->Data()->iVersion + "\n";
6 cycrow 1932
	}
1933
 
1934
	if ( m_bSigned )
1935
		values += "Signed\n";
1936
 
46 cycrow 1937
	for ( int j = 0; j < 2; j++ ) {
1938
		const CInstallText *text;
1939
		Utils::String textStart;
1940
		switch(j) {
1941
			case 0:	textStart = "Uninstall"; text = this->uninstallText(); break;
1942
			case 1: textStart = "Install"; text = this->installText(); break;
1943
		}
1944
		for ( unsigned int i = 0; i < text->count(); i++ ) {
1945
			int iLang = text->language(i);
1946
			if ( !text->getBefore(iLang).empty() ) values += textStart + "Before: " + (long)iLang + "|" + text->getBefore(iLang) + "\n";
1947
			if ( !text->getAfter(iLang).empty() ) values += textStart + "After: " + (long)iLang + "|" + text->getAfter(iLang) + "\n";
1948
		}
6 cycrow 1949
	}
1950
 
46 cycrow 1951
	values += Utils::String("GameChanging: ") + (long)gameChanging() + "\n";
1952
	values += Utils::String("EaseOfUse: ") + (long)easeOfUse() + "\n";
1953
	values += Utils::String("Recommended: ") + (long)recommended() + "\n";
6 cycrow 1954
 
14 cycrow 1955
	for ( CListNode<SNames> *nNode = m_lNames.Front(); nNode; nNode = nNode->next() )
46 cycrow 1956
		values += Utils::String("ScriptName: ") + (long)nNode->Data()->iLanguage + ":" + nNode->Data()->sName + "\n";
6 cycrow 1957
 
14 cycrow 1958
	for ( CListNode<SNeededLibrary> *libNode = m_lNeededLibrarys.Front(); libNode; libNode = libNode->next() ) {
6 cycrow 1959
		SNeededLibrary *l = libNode->Data();
14 cycrow 1960
		values += (CyString("NeededLibrary: ") + l->sName + "||" + l->sAuthor + "||" + l->sMinVersion + "\n").ToString();
6 cycrow 1961
	}
1962
 
1963
	for ( SStringList *fpbNode = m_lFakePatchBefore.Head(); fpbNode; fpbNode = fpbNode->next )
14 cycrow 1964
		values += (CyString("FakePatchBefore: ") + fpbNode->str + "||" + fpbNode->data + "\n").ToString();
6 cycrow 1965
	for ( SStringList *fpaNode = m_lFakePatchAfter.Head(); fpaNode; fpaNode = fpaNode->next )
14 cycrow 1966
		values += (CyString("FakePatchAfter: ") + fpaNode->str + "||" + fpaNode->data + "\n").ToString();
6 cycrow 1967
 
48 cycrow 1968
	values += Utils::String("PluginType: ") + (long)this->pluginType() + "\n";
6 cycrow 1969
 
1970
	return values;
1971
}
1972
 
1973
 
1974
/*
1975
	Func:   WriteFile
1976
	Input:  filename - The filename of the spk file to write to
1977
	Desc:   Writes the data to an spk file
1978
*/
1979
bool CBaseFile::WriteFile ( CyString filename, CProgressInfo *progress )
1980
{
52 cycrow 1981
	CFileIO File(filename);
1982
	if ( File.startWrite() ) return WriteData(File, progress);
1983
	return false;
6 cycrow 1984
}
1985
 
52 cycrow 1986
bool CBaseFile::WriteHeader(CFileIO &file, int valueheader, int valueComprLen)
6 cycrow 1987
{
52 cycrow 1988
	return file.write("BaseCycrow;%.2f;%d;%d\n", FILEVERSION, valueheader, valueComprLen);
6 cycrow 1989
}
1990
 
52 cycrow 1991
bool CBaseFile::WriteData(CFileIO &file, CProgressInfo *progress )
6 cycrow 1992
{
1993
	int valueheader = m_SHeader.iValueCompression, fileheader = m_SHeader.iValueCompression;
1994
	if ( valueheader == SPKCOMPRESS_7ZIP )
1995
		valueheader = SPKCOMPRESS_ZLIB;
1996
	if ( fileheader == SPKCOMPRESS_7ZIP )
1997
		fileheader = SPKCOMPRESS_ZLIB;
1998
 
1999
	// get the script values
2000
	this->UpdateSigned(true);
2001
	CyString values = this->CreateValuesLine();
2002
 
2003
	// compress the values
2004
	int valueUncomprLen = (int)values.Length();
2005
	unsigned long valueComprLen = 0;
2006
	unsigned char *valueCompr = NULL;
2007
	bool compressed = false;
2008
	if ( valueheader == SPKCOMPRESS_ZLIB )
2009
	{
2010
		valueComprLen = valueUncomprLen;
2011
		if ( valueComprLen < 100 )
2012
			valueComprLen = 200;
2013
		else if ( valueComprLen < 1000 )
2014
			valueComprLen *= 2;
2015
 
2016
		valueCompr = (unsigned char *)calloc((unsigned int)valueComprLen, 1);
2017
		int err = compress ( (unsigned char *)valueCompr, &valueComprLen, (const unsigned char *)values.c_str(), (unsigned long)values.Length(), 0 );
2018
		if ( err == Z_OK )
2019
			compressed = true;
2020
	}
2021
 
2022
	if ( !compressed )
2023
	{
2024
		valueComprLen = valueUncomprLen;
2025
		valueCompr = (unsigned char *)calloc((unsigned int)valueComprLen, 1);
2026
		memcpy ( valueCompr, values.c_str(), valueComprLen );
2027
		valueheader = SPKCOMPRESS_NONE;
2028
	}
2029
 
2030
	// write the main header to the file
52 cycrow 2031
	if ( !this->WriteHeader(file, valueheader, valueComprLen) )	return false;
6 cycrow 2032
 
2033
	// write the compressed data to file
52 cycrow 2034
	file.put(static_cast<unsigned char>(valueUncomprLen >> 24));
2035
	file.put(static_cast<unsigned char>(valueUncomprLen >> 16));
2036
	file.put(static_cast<unsigned char>(valueUncomprLen >> 8));
2037
	file.put(static_cast<unsigned char>(valueUncomprLen));
2038
	file.write(valueCompr, valueComprLen);
6 cycrow 2039
	free ( valueCompr );
2040
 
2041
	// now compress the files header
2042
	// create the files values
2043
	CyString files = CreateFilesLine ( true, progress );
2044
 
2045
	// compress the files values
2046
	long fileUncomprLen = (long)files.Length(), fileComprLen = fileUncomprLen;
2047
	unsigned char *fileCompr = NULL;
2048
 
2049
	compressed = false;
2050
	if ( fileUncomprLen )
2051
	{
2052
		if ( fileheader == SPKCOMPRESS_ZLIB )
2053
		{
2054
			if ( fileComprLen < 100 )
2055
				fileComprLen = 200;
2056
			else if ( fileComprLen < 1000 )
2057
				fileComprLen *= 2;
2058
			fileCompr = (unsigned char *)calloc((unsigned int)fileComprLen, 1);
2059
			int err = compress ( (unsigned char *)fileCompr, (unsigned long *)&fileComprLen, (const unsigned char *)files.c_str(), (unsigned long)files.Length(), 0 );
2060
			if ( err == Z_OK )
2061
				compressed = true;
2062
		}
2063
	}
2064
 
2065
	// if unable to compress, store it as plain text
2066
	if ( !compressed )
2067
	{
2068
		fileComprLen = fileUncomprLen;
2069
		fileCompr = (unsigned char *)calloc((unsigned int)fileComprLen, 1);
2070
		memcpy ( fileCompr, files.c_str(), fileComprLen );
2071
		fileheader = SPKCOMPRESS_NONE;
2072
	}
2073
 
2074
	// now write the file header
2075
	m_SHeader2.lSize = fileComprLen;
52 cycrow 2076
	file.write("FileHeader;%d;%ld;%ld;%d;%d\n", m_SHeader2.iNumFiles, m_SHeader2.lSize, m_SHeader2.lFullSize, fileheader, m_SHeader2.iDataCompression);
6 cycrow 2077
 
52 cycrow 2078
	file.put(static_cast<unsigned char>(fileUncomprLen >> 24));
2079
	file.put(static_cast<unsigned char>(fileUncomprLen >> 16));
2080
	file.put(static_cast<unsigned char>(fileUncomprLen >> 8));
2081
	file.put(static_cast<unsigned char>(fileUncomprLen));
2082
	file.write(fileCompr, fileComprLen);
6 cycrow 2083
	free ( fileCompr );
2084
 
2085
	if ( progress )
2086
	{
2087
		progress->UpdateStatus(STATUS_WRITE);
2088
		progress->SetDone(0);
2089
		long max = 0;
88 cycrow 2090
		for ( CListNode<C_File> *file = m_lFiles.Front(); file; file = file->next() )
2091
			max += file->Data()->GetDataSize();
6 cycrow 2092
		if ( m_pIconFile )
2093
			max += m_pIconFile->GetDataSize();
2094
		progress->SetMax(max);
2095
	}
2096
 
2097
	// now finally, write all the file data
52 cycrow 2098
	if ( m_pIconFile ) {
2099
		if ( progress )	progress->UpdateFile(m_pIconFile);
2100
		file.writeSize(m_pIconFile->GetUncompressedDataSize());
2101
		file.write(m_pIconFile->GetData(), m_pIconFile->GetDataSize());
2102
		if ( progress )	progress->IncDone(m_pIconFile->GetDataSize());
6 cycrow 2103
	}
2104
 
2105
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2106
	{
52 cycrow 2107
		C_File *f = node->Data();
2108
		if ( progress )	progress->UpdateFile(f);
2109
		file.writeSize(f->GetUncompressedDataSize());
2110
		unsigned char *data = f->GetData();
2111
		size_t remaining = f->GetDataSize();
2112
		while ( remaining )	{
6 cycrow 2113
			size_t writeSize = WRITECHUNK;
52 cycrow 2114
			if ( writeSize > remaining ) writeSize = remaining;
6 cycrow 2115
 
52 cycrow 2116
			if ( file.write(data, writeSize) ) {
2117
				data += writeSize;
2118
				remaining -= writeSize;
2119
			}
6 cycrow 2120
 
52 cycrow 2121
			if ( progress ) progress->IncDone((int)writeSize);
6 cycrow 2122
		}
2123
	}
2124
 
50 cycrow 2125
	_changed();
6 cycrow 2126
 
2127
	return true;
2128
}
2129
 
2130
 
131 cycrow 2131
bool CBaseFile::ExtractFile(C_File *file, CyString dir, bool includedir, CProgressInfo *progress)
6 cycrow 2132
{
131 cycrow 2133
	return extractFile(file, dir.ToString(), includedir, progress);
2134
}
2135
bool CBaseFile::extractFile(C_File *file, const Utils::String &dir, bool includedir, CProgressInfo *progress)
2136
{
2137
	if (ReadFileToMemory(file))
6 cycrow 2138
	{
2139
		// now finally, uncompress the file
2140
		long len = 0;
131 cycrow 2141
		unsigned char *data = file->UncompressData(&len, progress);
2142
		if (!data)
6 cycrow 2143
		{
2144
			// attempt a file decompress
131 cycrow 2145
			if (file->GetCompressionType() == SPKCOMPRESS_7ZIP)
6 cycrow 2146
			{
131 cycrow 2147
				if (file->UncompressToFile(dir, this, includedir, progress))
6 cycrow 2148
					return true;
2149
			}
2150
			return false;
2151
		}
2152
 
131 cycrow 2153
		if (!file->writeToDir(dir, this, includedir, Utils::String::Null(), data, len))
6 cycrow 2154
			return false;
2155
 
2156
		return true;
2157
 
2158
	}
2159
	else
2160
		return false;
2161
}
131 cycrow 2162
bool CBaseFile::extractFile(C_File *file, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir, CProgressInfo *progress)
2163
{
2164
	if (ReadFileToMemory(file))
2165
	{
2166
		CDirIO Dir(dir);
2167
		Utils::String addonDir;
2168
		if (file->isFileInAddon())
2169
		{
2170
			int addonGame = file->getForSingleGame();
2171
			if (!addonGame) addonGame = game;
6 cycrow 2172
 
131 cycrow 2173
			if (addonGame > 0)
2174
				addonDir = gameAddons.findString(Utils::String::Number(addonGame));
2175
		}
2176
 
2177
		if (!addonDir.empty())
2178
			Dir.cd(addonDir);
2179
 
2180
		// create directory first
2181
		Dir.create(file->getDirectory(this));
2182
 
2183
		// now finally, uncompress the file
2184
		long len = 0;
2185
		unsigned char *data = file->UncompressData(&len, progress);
2186
		if (!data)
2187
		{
2188
			// attempt a file decompress
2189
			if (file->GetCompressionType() == SPKCOMPRESS_7ZIP)
2190
			{
2191
				if (file->UncompressToFile(Dir.dir(), this, includedir, progress))
2192
					return true;
2193
			}
2194
			return false;
2195
		}
2196
 
2197
		if (!file->writeToDir(Dir.dir(), this, includedir, Utils::String::Null(), data, len))
2198
			return false;
2199
 
2200
		return true;
2201
 
2202
	}
2203
	else
2204
		return false;
2205
}
2206
 
2207
bool CBaseFile::ExtractFile(int filenum, CyString dir, bool includedir, CProgressInfo *progress)
6 cycrow 2208
{
2209
	// invalid valus
131 cycrow 2210
	if (filenum < 0)
6 cycrow 2211
		return false;
2212
	// out of range
131 cycrow 2213
	if (filenum > m_lFiles.size())
6 cycrow 2214
		return false;
2215
 
2216
	// get the file pointer
131 cycrow 2217
	C_File *file = m_lFiles.Get(filenum);
2218
	return extractFile(file, dir.ToString(), includedir, progress);
6 cycrow 2219
}
2220
 
131 cycrow 2221
bool CBaseFile::extractFile(int filenum, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir, CProgressInfo *progress)
2222
{
2223
	// invalid valus
2224
	if (filenum < 0)
2225
		return false;
2226
	// out of range
2227
	if (filenum > m_lFiles.size())
2228
		return false;
2229
 
2230
	// get the file pointer
2231
	C_File *file = m_lFiles.Get(filenum);
2232
	return extractFile(file, dir, game, gameAddons, includedir, progress);
2233
}
2234
 
2235
bool CBaseFile::extractFile(int filenum, const Utils::String &dir, bool includedir, CProgressInfo *progress)
2236
{
2237
	// invalid valus
2238
	if (filenum < 0)
2239
		return false;
2240
	// out of range
2241
	if (filenum > m_lFiles.size())
2242
		return false;
2243
 
2244
	// get the file pointer
2245
	C_File *file = m_lFiles.Get(filenum);
2246
	return ExtractFile(file, dir, includedir, progress);
2247
}
2248
 
129 cycrow 2249
bool CBaseFile::extractAll(const Utils::String &dir, int game, const Utils::CStringList &gameAddons, bool includedir, CProgressInfo *progress)
6 cycrow 2250
{
2251
	// no file to read from
129 cycrow 2252
	if (this->filename().empty())
6 cycrow 2253
		return false;
2254
 
2255
	// now open the file
58 cycrow 2256
	CFileIO *File = _startRead();
129 cycrow 2257
	if (!File) return false;
6 cycrow 2258
 
2259
	// now were in the file section
2260
	// skip past each one
129 cycrow 2261
	if (m_pIconFile) File->seek(4 + m_pIconFile->GetDataSize());
6 cycrow 2262
 
134 cycrow 2263
	for (CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next()) 
2264
	{
6 cycrow 2265
		C_File *fit = node->Data();
2266
 
134 cycrow 2267
		if ((!fit->GetDataSize()) || (!fit->GetData())) 
2268
		{
2269
			if (!fit->readFromFile(*File, fit->GetDataSize())) 
2270
				return false;
6 cycrow 2271
		}
2272
		else
58 cycrow 2273
			File->seek(fit->GetDataSize());
6 cycrow 2274
 
129 cycrow 2275
		if (game && !fit->isForGame(game))
2276
			continue;
2277
 
134 cycrow 2278
		if (progress)	
2279
			progress->UpdateFile(fit);
2280
 
129 cycrow 2281
		CDirIO Dir(dir);
2282
		Utils::String addonDir;
2283
		if (fit->isFileInAddon())
2284
		{
2285
			int addonGame = fit->getForSingleGame();
2286
			if (!addonGame) addonGame = game;
2287
 
2288
			if (addonGame > 0)
2289
				addonDir = gameAddons.findString(Utils::String::Number(addonGame));
6 cycrow 2290
		}
2291
 
129 cycrow 2292
		if (!addonDir.empty())
2293
			Dir.cd(addonDir);
2294
 
6 cycrow 2295
		// create directory first
129 cycrow 2296
		Dir.create(fit->getDirectory(this));
6 cycrow 2297
 
2298
		long size = 0;
129 cycrow 2299
		unsigned char *data = fit->UncompressData(&size, progress);
2300
		if ((!data) && (fit->GetCompressionType() == SPKCOMPRESS_7ZIP)) {
2301
			if (!fit->UncompressToFile(Dir.dir(), this, includedir, progress)) return false;
6 cycrow 2302
		}
129 cycrow 2303
		else if ((!data) || (!fit->writeToDir(Dir.dir(), this, includedir, Utils::String::Null(), data, size))) return false;
6 cycrow 2304
	}
2305
 
58 cycrow 2306
	delete File;
6 cycrow 2307
	return true;
2308
}
2309
 
2310
 
2311
 
2312
bool CBaseFile::UpdateSigned (bool updateFiles)
2313
{
2314
	m_bSigned = true;
2315
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2316
	{
2317
		C_File *file = node->Data();
2318
		if ( updateFiles )
2319
			file->UpdateSigned();
2320
		// extra, text, soundtrack, readmes and screen files do not require a modified game directly
2321
		if ( (file->GetFileType() == FILETYPE_EXTRA) || (file->GetFileType() == FILETYPE_TEXT) || (file->GetFileType() == FILETYPE_SOUND) || (file->GetFileType() == FILETYPE_SCREEN) || (file->GetFileType() == FILETYPE_README) )
2322
			continue;
2323
		// mods and maps always need modified game
2324
		else if ( (file->GetFileType() == FILETYPE_MOD) || (file->GetFileType() == FILETYPE_MAP) )
2325
		{
2326
			m_bSigned = false;
2327
			break;
2328
		}
2329
 
2330
		// else should be a script file, script or uninstall type
2331
		// all scripts must be signed, if any are not, then no signed status
2332
		if ( !file->IsSigned () )
2333
		{
2334
			m_bSigned = false;
2335
			break;
2336
		}
2337
	}
2338
 
2339
	return m_bSigned;
2340
}
2341
 
2342
 
46 cycrow 2343
bool CBaseFile::IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2344
{
2345
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2346
	{
2347
		SNeededLibrary *l = node->Data();
2348
		if ( l->sName.Compare(scriptName) && l->sAuthor.Compare(author) )
2349
			return true;
2350
	}
2351
 
2352
	return false;
2353
}
2354
 
46 cycrow 2355
SNeededLibrary *CBaseFile::FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2356
{
2357
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2358
	{
2359
		SNeededLibrary *l = node->Data();
2360
		if ( l->sName.Compare(scriptName) && l->sAuthor.Compare(author) )
2361
			return l;
2362
	}
2363
 
2364
	return NULL;
2365
}
2366
 
2367
void CBaseFile::RemoveFakePatchOrder(CyString scriptName, CyString author)
2368
{
2369
	RemoveFakePatchOrder(true, scriptName, author);
2370
	RemoveFakePatchOrder(false, scriptName, author);
2371
}
2372
void CBaseFile::RemoveFakePatchOrder(bool after, CyString scriptName, CyString author)
2373
{
2374
	CyStringList *list;
2375
	if ( after )
2376
		list = &m_lFakePatchAfter;
2377
	else
2378
		list = &m_lFakePatchBefore;
2379
 
2380
	for ( SStringList *str = list->Head(); str; str = str->next )
2381
	{
2382
		// already added
2383
		if ( str->str.Compare(scriptName) && str->data.Compare(author) )
2384
			str->remove = true;
2385
	}
2386
 
2387
	list->RemoveMarked();
2388
}
2389
 
2390
void CBaseFile::AddFakePatchOrder(bool after, CyString scriptName, CyString author)
2391
{
2392
	CyStringList *list;
2393
	if ( after )
2394
		list = &m_lFakePatchAfter;
2395
	else
2396
		list = &m_lFakePatchBefore;
2397
 
2398
	// check if the package already exists
2399
	for ( SStringList *str = list->Head(); str; str = str->next )
2400
	{
2401
		// already added
2402
		if ( str->str.Compare(scriptName) && str->data.Compare(author) )
2403
			return;
2404
	}
2405
 
2406
	// cant have it on both list
2407
	RemoveFakePatchOrder(!after, scriptName, author);
2408
 
2409
	list->PushBack(scriptName, author);
2410
}
46 cycrow 2411
void CBaseFile::AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion)
6 cycrow 2412
{
2413
	SNeededLibrary *l = this->FindPackageNeeded(scriptName, author);
2414
	if ( !l )
2415
	{
2416
		l = new SNeededLibrary;
2417
		l->sName = scriptName;
2418
		l->sAuthor = author;
2419
		m_lNeededLibrarys.push_back(l);
2420
	}
2421
	l->sMinVersion = minVersion;
2422
}
2423
 
46 cycrow 2424
void CBaseFile::RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2425
{
2426
	SNeededLibrary *l = this->FindPackageNeeded(scriptName, author);
2427
	if ( l )
2428
	{
2429
		m_lNeededLibrarys.remove(l);
2430
		delete l;
2431
	}
2432
}
2433
 
2434
void CBaseFile::ClearNeededPackages()
2435
{
2436
	SNeededLibrary *ns = this->FindPackageNeeded("<package>", "<author>");
46 cycrow 2437
	Utils::String version;
2438
	if ( ns ) version = ns->sMinVersion;
2439
 
6 cycrow 2440
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2441
		node->DeleteData();
2442
	m_lNeededLibrarys.clear();
2443
 
46 cycrow 2444
	if ( !version.empty() )	this->AddNeededLibrary("<package>", "<author>", version);
6 cycrow 2445
}
2446
 
2447
 
127 cycrow 2448
bool CBaseFile::GeneratePackagerScript(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons, bool datafile)
6 cycrow 2449
{
126 cycrow 2450
	list->pushBack("#");
2451
	list->pushBack("# Packager Script");
2452
	list->pushBack("# -- Generated by SPK Libraries V" + Utils::String::FromFloat(GetLibraryVersion(), 2) + " --");
2453
	list->pushBack("#");
2454
	list->pushBack("");
6 cycrow 2455
	if ( !datafile )
2456
	{
126 cycrow 2457
		list->pushBack("# Variable for your game directory, where to get files from");
2458
		list->pushBack("# $PATH variable is used to get the current path");
2459
		list->pushBack("Variable: $GAMEDIR $PATH");
2460
		list->pushBack("");
6 cycrow 2461
	}
126 cycrow 2462
	list->pushBack("# The name of the script");
2463
	list->pushBack("Name: " + this->name());
2464
	list->pushBack("");
2465
	list->pushBack("# The author of the script, ie, you");
2466
	list->pushBack("Author: " + this->author());
2467
	list->pushBack("");
2468
	list->pushBack("# The creation data, when it was created");
6 cycrow 2469
	if ( datafile )
126 cycrow 2470
		list->pushBack("Date: " + this->creationDate());
50 cycrow 2471
	else {
126 cycrow 2472
		list->pushBack("# $DATE variable is used to get the current date");
2473
		list->pushBack("Date: $DATE");
6 cycrow 2474
	}
126 cycrow 2475
	list->pushBack("");
2476
	list->pushBack("# The version of script");
6 cycrow 2477
	if ( datafile )
126 cycrow 2478
		list->pushBack("Version: " + this->version());
6 cycrow 2479
	else
2480
	{
126 cycrow 2481
		list->pushBack("# $ASK variable is used to get an input when creating");
2482
		list->pushBack("Version: $ASK");
6 cycrow 2483
	}
126 cycrow 2484
	list->pushBack("");
6 cycrow 2485
 
2486
	if ( !m_lGames.empty() ) {
126 cycrow 2487
		list->pushBack("# The game version the script is for <game> <version> (can have multiple games)");
6 cycrow 2488
		for ( SGameCompat *g = m_lGames.First(); g; g = m_lGames.Next() ) {
126 cycrow 2489
			if (game > 0 && g->iGame != game)
2490
				continue;
2491
 
13 cycrow 2492
			Utils::String game = CBaseFile::ConvertGameToString(g->iGame);
6 cycrow 2493
 
46 cycrow 2494
			if ( !g->sVersion.empty() )
6 cycrow 2495
			{
2496
				game += " ";
46 cycrow 2497
				game += g->sVersion;
6 cycrow 2498
			}
2499
			else
2500
			{
2501
				game += " ";
2502
				game += (long)g->iVersion;
2503
			}
2504
 
126 cycrow 2505
			list->pushBack("Game: " + game);
6 cycrow 2506
		}
2507
 
126 cycrow 2508
		list->pushBack("");
6 cycrow 2509
	}
2510
 
48 cycrow 2511
	if ( !this->description().empty() ) {
126 cycrow 2512
		list->pushBack("# The description of the script, displays when installing");
2513
		list->pushBack("Description: " + this->description());
2514
		list->pushBack("");
6 cycrow 2515
	}
2516
 
49 cycrow 2517
	if ( !this->webSite().empty() ) {
126 cycrow 2518
		list->pushBack("# A link to the website for the script, ie for an online help page");
2519
		list->pushBack("WebSite: " + this->webSite());
2520
		list->pushBack("");
6 cycrow 2521
	}
2522
 
49 cycrow 2523
	if ( !this->forumLink().empty() ) {
126 cycrow 2524
		list->pushBack("# A direct link to the thread in the egosoft forum");
2525
		list->pushBack("ForumLink: " + this->forumLink());
2526
		list->pushBack("");
6 cycrow 2527
	}
2528
 
49 cycrow 2529
	if ( !this->webAddress().empty() ) {
126 cycrow 2530
		list->pushBack("# A link to the address for the update file");
2531
		list->pushBack("WebAddress: " + this->webAddress());
2532
		list->pushBack("");
6 cycrow 2533
	}
2534
 
49 cycrow 2535
	if ( !this->email().empty() ) {
126 cycrow 2536
		list->pushBack("# The email address of the author, to allow users to contract if needed");
2537
		list->pushBack("Email: " + this->email());
2538
		list->pushBack("");
6 cycrow 2539
	}
2540
 
2541
	if ( m_lMirrors.Count() )
2542
	{
126 cycrow 2543
		list->pushBack("# A link to the mirror address for the update file, can have many of these");
6 cycrow 2544
		for ( SStringList *node = m_lMirrors.Head(); node; node = node->next )
126 cycrow 2545
			list->pushBack(Utils::String("WebMirror: ") + node->str.ToString());
2546
		list->pushBack("");
6 cycrow 2547
	}
2548
 
2549
	if ( m_bAutoGenerateUpdateFile )
2550
	{
126 cycrow 2551
		list->pushBack("# Auto generate the package update file when created");
2552
		list->pushBack("GenerateUpdateFile");
6 cycrow 2553
	}
2554
 
2555
	if ( m_lNeededLibrarys.size() )
2556
	{
126 cycrow 2557
		list->pushBack("# Needed Library dependacies, require these to be installed");
6 cycrow 2558
		for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
126 cycrow 2559
			list->pushBack("Depend: " + node->Data()->sName + "|" + node->Data()->sMinVersion + "|" + node->Data()->sAuthor);
6 cycrow 2560
 
126 cycrow 2561
		list->pushBack("");
6 cycrow 2562
	}
2563
 
46 cycrow 2564
	if ( !_noRatings() )
6 cycrow 2565
	{
126 cycrow 2566
		list->pushBack("# Ratings Values, 0 to 5, <ease> <changing> <recommended>");
2567
		list->pushBack(Utils::String("Ratings: ") + (long)easeOfUse() + " " + (long)gameChanging() + " " + (long)recommended());
2568
		list->pushBack("");
6 cycrow 2569
	}
2570
 
2571
	if ( m_lNames.size() )
2572
	{
126 cycrow 2573
		list->pushBack("# Package names, uses different names for different languages");
6 cycrow 2574
		for ( CListNode<SNames> *node = m_lNames.Front(); node; node = node->next() )
126 cycrow 2575
			list->pushBack(Utils::String("ScriptName: ") + (long)node->Data()->iLanguage + " " + node->Data()->sName);
2576
		list->pushBack("");
6 cycrow 2577
	}
2578
 
46 cycrow 2579
	for ( int j = 0; j < 2; j++ ) {
2580
		Utils::String installText = (j == 0) ? "Install" : "Uninstall";
2581
		const CInstallText *pText = (j == 0) ? this->installText() : this->uninstallText();
2582
		if ( pText->any() )
6 cycrow 2583
		{
126 cycrow 2584
			list->pushBack("# " + installText + " Texts, display text before and/or after " + installText + "ing to inform the use of special conditions");
46 cycrow 2585
			for ( unsigned int i = 0; i < pText->count(); i++ ) {
2586
				long iLang = pText->language(i);
126 cycrow 2587
				if ( !pText->getBefore(iLang).empty() )	list->pushBack(installText + "Before: " + iLang + " " + pText->getBefore(iLang));
2588
				if ( !pText->getAfter(iLang).empty()  )	list->pushBack(installText + "After: " + iLang + " " + pText->getAfter(iLang));
46 cycrow 2589
			}
126 cycrow 2590
			list->pushBack("");
6 cycrow 2591
		}
2592
	}
2593
 
126 cycrow 2594
	list->pushBack("# Plugin Type, the type the plugin is, mainly used to show users the type, types include: Normal, Stable, Experimental, Cheat, Mod");
48 cycrow 2595
	switch ( this->pluginType() )
6 cycrow 2596
	{
2597
		case PLUGIN_NORMAL:
126 cycrow 2598
			list->pushBack("PluginType: Normal");
6 cycrow 2599
			break;
2600
		case PLUGIN_STABLE:
126 cycrow 2601
			list->pushBack("PluginType: Stable");
6 cycrow 2602
			break;
2603
		case PLUGIN_EXPERIMENTAL:
126 cycrow 2604
			list->pushBack("PluginType: Experimental");
6 cycrow 2605
			break;
2606
		case PLUGIN_CHEAT:
126 cycrow 2607
			list->pushBack("PluginType: Cheat");
6 cycrow 2608
			break;
2609
		case PLUGIN_MOD:
126 cycrow 2610
			list->pushBack("PluginType: Mod");
6 cycrow 2611
			break;
2612
	}
126 cycrow 2613
	list->pushBack("");
6 cycrow 2614
 
2615
	return true;
2616
}
2617
 
127 cycrow 2618
bool CBaseFile::GeneratePackagerScriptFile(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons)
6 cycrow 2619
{
2620
	// now do files and wildcards
126 cycrow 2621
	Utils::CStringList files;
6 cycrow 2622
	for ( CListNode<C_File> *f = m_lFiles.Front(); f; f = f->next() )
2623
	{
127 cycrow 2624
		if (game && !(f->Data()->game() & 1 << game))
126 cycrow 2625
			continue;
6 cycrow 2626
 
126 cycrow 2627
		Utils::String name = "$GAMEDIR/";
2628
 
127 cycrow 2629
		// addon directory?
2630
		unsigned int checkGame = f->Data()->game() & ~(1 << 31);
2631
		unsigned int foundGame = 0;
2632
		for (int i = 0; i < 31; ++i)
2633
		{
2634
			if (checkGame == 1 << i)
2635
			{
2636
				foundGame = i;
2637
				break;
2638
			}
2639
		}
2640
 
2641
		if (foundGame)
2642
		{
2643
			Utils::String str = gameAddons.findString(Utils::String::Number(foundGame));
2644
			if(!str.empty())
2645
				name += str + "/";
2646
		}
2647
 
6 cycrow 2648
		bool done = false;
2649
		if ( wildcard )
2650
		{
131 cycrow 2651
			Utils::String base = f->Data()->baseName();
2652
			if ( f->Data()->fileType() == FILETYPE_SCRIPT )
6 cycrow 2653
			{
126 cycrow 2654
				if ( base.token(".", 1).Compare("plugin") || base.token(".", 1).Compare("lib") )
6 cycrow 2655
				{
131 cycrow 2656
					name += f->Data()->getDirectory(this) + "/" + base.tokens(".", 1, 2) + ".*";
6 cycrow 2657
					done = true;
2658
				}
126 cycrow 2659
				else if ( base.token(".", 1).Compare("al") && !base.token(".", 2).Compare("plugin") )
6 cycrow 2660
				{
131 cycrow 2661
					name += f->Data()->getDirectory(this) + "/" + base.tokens(".", 1, 2) + ".*";
6 cycrow 2662
					done = true;
2663
				}
2664
			}
131 cycrow 2665
			else if ( f->Data()->fileType() == FILETYPE_TEXT )
6 cycrow 2666
			{
126 cycrow 2667
				if ( base.contains("-L") )
6 cycrow 2668
				{
131 cycrow 2669
					name += f->Data()->getDirectory(this) + "/" + base.token("-L", 1) + "-L*";
6 cycrow 2670
					done = true;
2671
				}
2672
				else
2673
				{
131 cycrow 2674
					name += f->Data()->getDirectory(this) + "/*" + base.right(4) + ".*";
6 cycrow 2675
					done = true;
2676
				}
2677
			}
2678
		}
2679
 
2680
		if ( !done )
131 cycrow 2681
			name += f->Data()->getNameDirectory(this);
6 cycrow 2682
 
131 cycrow 2683
		if ( !f->Data()->dir().empty() )
6 cycrow 2684
		{
2685
			name += "|";
131 cycrow 2686
			name += f->Data()->dir();
6 cycrow 2687
		}
127 cycrow 2688
		Utils::String s = "GAME ";
2689
		if (!f->Data()->game() || f->Data()->game() == GAME_ALLNEW)
2690
			s += CBaseFile::ConvertGameToString(f->Data()->game());
2691
		else
2692
		{
2693
			bool first = true;
2694
			for (int i = 0; i < 31; ++i) 
2695
			{
2696
				if (f->Data()->game() & 1 << i)
2697
				{
2698
					if (first)
2699
						first = false;
2700
					else
2701
						s += "|";
2702
					s += CBaseFile::ConvertGameToString(i);
2703
				}
2704
			}
2705
		}
2706
 
2707
		s += " " + name;
126 cycrow 2708
		if(!files.contains(s))
2709
			files.pushBack(s, f->Data()->GetFileTypeString().ToString());
6 cycrow 2710
	}
2711
 
2712
 
126 cycrow 2713
	if ( !files.empty() )
6 cycrow 2714
	{
126 cycrow 2715
		list->pushBack("# Files List, all the files to add, can include wild cards");
2716
		for(auto itr = files.begin(); itr != files.end(); itr++)
2717
			list->pushBack((*itr)->data + ": " + (*itr)->str);
2718
		list->pushBack("");
6 cycrow 2719
	}
2720
 
2721
	return true;
2722
}
2723
 
50 cycrow 2724
Utils::String CBaseFile::GetAutosaveName()
6 cycrow 2725
{
50 cycrow 2726
	return this->name() + "-V" + this->version() + "-" + this->creationDate().findReplace("/", ".");
6 cycrow 2727
}
2728
 
2729
bool CBaseFile::CheckGameCompatability(int game)
2730
{
2731
	if ( m_lGames.empty() )
2732
		return true; // no game compatability added, assume its ok for all
2733
 
2734
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2735
		if ( node->Data()->iGame == 0 || node->Data()->iGame == game )
2736
			return true;
2737
	}
2738
	return false;
2739
}
2740
 
2741
bool CBaseFile::CheckGameVersionCompatability(int game, CyString sVersion, int iVersion)
2742
{
2743
	if ( m_lGames.empty() )
2744
		return true; // no game compatability added, assume its ok for all
2745
 
2746
	bool foundAll = false;
2747
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2748
		if ( node->Data()->iGame == 0 )
2749
			foundAll = true;
2750
		else if ( node->Data()->iGame == game ) { // now check the version
46 cycrow 2751
			if ( node->Data()->sVersion.empty() ) {
2752
				if ( CyString(node->Data()->sVersion).CompareVersion(sVersion) == COMPARE_OLDER )
6 cycrow 2753
					return false;
2754
				return true;
2755
			}
2756
			else {
2757
				if ( node->Data()->iVersion > iVersion )
2758
					return false;
2759
				return true;
2760
			}
2761
		}
2762
	}
2763
	return foundAll;
2764
}
2765
 
2766
bool CBaseFile::RemoveGameCompatability(int game)
2767
{
2768
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2769
		if ( node->Data()->iGame == game ) {
2770
			m_lGames.remove(node);
50 cycrow 2771
			_changed();
6 cycrow 2772
			return true;
2773
		}
2774
	}
2775
 
2776
	return false;
2777
}
2778
 
2779
SGameCompat *CBaseFile::GetGameCompatability(int game)
2780
{
2781
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2782
		if ( node->Data()->iGame == game ) {
2783
			return node->Data();
2784
		}
2785
	}
2786
 
2787
	return NULL;
2788
}
2789
 
46 cycrow 2790
void CBaseFile::AddGameCompatability(int game, const Utils::String &version)
6 cycrow 2791
{
2792
	// first check if we already have it on the list
2793
	SGameCompat *Found = this->GetGameCompatability(game);
2794
	if ( !Found ) {
2795
		Found = new SGameCompat;
2796
		m_lGames.push_back(Found);
2797
	}
2798
 
2799
	Found->iGame = game;
2800
	Found->iVersion = -1;
46 cycrow 2801
	Found->sVersion = "";
6 cycrow 2802
 
46 cycrow 2803
	if ( version.isin(".") || !version.isNumber() )
6 cycrow 2804
		Found->sVersion = version;
2805
	else
46 cycrow 2806
		Found->iVersion = version;
50 cycrow 2807
	_changed();
6 cycrow 2808
}
2809
 
131 cycrow 2810
Utils::String CBaseFile::_replaceFilename(const Utils::String &fname)
2811
{
2812
	Utils::String filename = fname;
2813
	Utils::String cdate = this->creationDate().findReplace("/", ".").remove(' ');
2814
	if (filename.isin("$AUTOSAVE"))
2815
	{
2816
		if (this->GetType() == TYPE_XSP)
2817
			filename = filename.findReplace("$AUTOSAVE", "$NAME-V$VERSION-$CDATE.xsp");
2818
		else
2819
			filename = filename.findReplace("$AUTOSAVE", "$NAME-V$VERSION-$CDATE.spk");
2820
	}
2821
	filename = filename.findReplace("$NAME", this->name().remove(' '));
2822
	filename = filename.findReplace("$AUTHOR", this->author().remove(' '));
2823
	filename = filename.findReplace("$DATE", cdate);
2824
	filename = filename.findReplace("$CDATE", cdate);
2825
	filename = filename.findReplace("$VERSION", this->version());
2826
 
2827
	return filename;
2828
}
134 cycrow 2829
bool CBaseFile::LoadPackageData(const Utils::String &sFirst, const Utils::String &sRest, const Utils::String &sMainGame, Utils::CStringList &otherGames, Utils::CStringList &gameAddons, CProgressInfo *progress)
6 cycrow 2830
{
134 cycrow 2831
	if (sFirst.Compare("Name"))					this->setName(sRest);
2832
	else if (sFirst.Compare("Author"))			this->setAuthor(sRest);
2833
	else if (sFirst.Compare("ScriptName"))
14 cycrow 2834
		AddLanguageName(ParseLanguage(sRest.token(" ", 1)), sRest.tokens(" ", 2));
134 cycrow 2835
	else if (sFirst.Compare("UninstallBefore"))	this->addUninstallText(ParseLanguage(sRest.token(" ", 1)), true, sRest.tokens(" ", 2));
2836
	else if (sFirst.Compare("UninstallAfter"))	this->addUninstallText(ParseLanguage(sRest.token(" ", 1)), false, sRest.tokens(" ", 2));
2837
	else if (sFirst.Compare("InstallBefore"))		this->addInstallText(ParseLanguage(sRest.token(" ", 1)), true, sRest.tokens(" ", 2));
2838
	else if (sFirst.Compare("InstallAfter"))		this->addInstallText(ParseLanguage(sRest.token(" ", 1)), false, sRest.tokens(" ", 2));
2839
	else if (sFirst.Compare("Date"))				this->setCreationDate(sRest);
2840
	else if (sFirst.Compare("Version"))			this->setVersion(sRest);
2841
	else if (sFirst.Compare("GameVersion"))
14 cycrow 2842
		this->AddGameCompatability(-1, sRest);
134 cycrow 2843
	else if (sFirst.Compare("PluginType")) {
2844
		if (sRest.isNumber())						this->setPluginType(sRest);
2845
		else if (sRest.Compare("Normal"))			this->setPluginType(PLUGIN_NORMAL);
2846
		else if (sRest.Compare("Stable"))			this->setPluginType(PLUGIN_STABLE);
2847
		else if (sRest.Compare("Experimental"))	this->setPluginType(PLUGIN_EXPERIMENTAL);
2848
		else if (sRest.Compare("Cheat"))			this->setPluginType(PLUGIN_CHEAT);
2849
		else if (sRest.Compare("Mod"))			this->setPluginType(PLUGIN_MOD);
6 cycrow 2850
	}
2851
	// new version
134 cycrow 2852
	else if (sFirst.Compare("GenerateUpdateFile"))
6 cycrow 2853
		m_bAutoGenerateUpdateFile = true;
134 cycrow 2854
	else if (sFirst.Compare("Game"))
6 cycrow 2855
	{
14 cycrow 2856
		Utils::String sGame = sRest.token(" ", 1);
2857
		this->AddGameCompatability(CBaseFile::GetGameFromString(sGame), sRest.token(" ", 2));
6 cycrow 2858
	}
134 cycrow 2859
	else if (sFirst.Compare("Description"))		this->setDescription(sRest);
2860
	else if (sFirst.Compare("AutoSave") || sFirst.Compare("AutoExport") || sFirst.Compare("AutoRarExport") || sFirst.Compare("AutoZipExport"))
6 cycrow 2861
	{
131 cycrow 2862
		Utils::String filename = _replaceFilename(sRest);
6 cycrow 2863
 
134 cycrow 2864
		if (sFirst.Compare("AutoZipExport") || sFirst.Compare("AutoExport"))
131 cycrow 2865
			this->setExportFilename(CFileIO(filename).changeFileExtension("zip"));
134 cycrow 2866
		else if (sFirst.Compare("AutoRarExport"))
131 cycrow 2867
			this->setExportFilename(CFileIO(filename).changeFileExtension("rar"));
6 cycrow 2868
		else
50 cycrow 2869
			this->setFilename(filename);
6 cycrow 2870
	}
134 cycrow 2871
	else if (sFirst.Compare("WebSite"))		this->setWebSite(sRest);
2872
	else if (sFirst.Compare("ForumLink") || sFirst.Compare("Forum")) this->setForumLink(sRest);
2873
	else if (sFirst.Compare("Email"))			this->setEmail(sRest);
2874
	else if (sFirst.Compare("WebAddress"))	this->setWebAddress(sRest);
2875
	else if (sFirst.Compare("WebMirror"))
14 cycrow 2876
		this->AddWebMirror(sRest);
134 cycrow 2877
	else if (sFirst.Compare("WebMirror1"))
14 cycrow 2878
		this->AddWebMirror(sRest);
134 cycrow 2879
	else if (sFirst.Compare("WebMirror2"))
14 cycrow 2880
		this->AddWebMirror(sRest);
134 cycrow 2881
	else if (sFirst.Compare("Ftp"))
14 cycrow 2882
		m_sFtpAddr = sRest;
134 cycrow 2883
	else if (sFirst.Compare("Ratings"))		_setRatings(sRest.token(" ", 1), sRest.token(" ", 2), sRest.token(" ", 3));
2884
	else if (sFirst.Compare("EaseOfUse"))		setEaseOfUse(sRest);
2885
	else if (sFirst.Compare("GameChanging"))	setGameChanging(sRest);
2886
	else if (sFirst.Compare("Recommended"))	setRecommended(sRest);
2887
	else if (sFirst.Compare("Depend"))
6 cycrow 2888
	{
46 cycrow 2889
		Utils::String version = sRest.token("|", 2);
2890
		Utils::String name = sRest.token("|", 1);
2891
		Utils::String author = sRest.tokens("|", 3);
6 cycrow 2892
 
2893
		this->AddNeededLibrary(name, author, version);
2894
	}
134 cycrow 2895
	else if (sFirst.Compare("DependPackage"))
6 cycrow 2896
	{
2897
		CPackages p;
134 cycrow 2898
		CBaseFile *spk = p.OpenPackage(sRest, 0, 0, SPKREAD_VALUES);
2899
		if (spk)
6 cycrow 2900
		{
50 cycrow 2901
			this->AddNeededLibrary(spk->name(), spk->author(), spk->version());
6 cycrow 2902
			delete spk;
2903
		}
2904
	}
130 cycrow 2905
	else if (sFirst.Compare("Icon"))
6 cycrow 2906
	{
14 cycrow 2907
		C_File *icon = new C_File(sRest.c_str());
134 cycrow 2908
		if (icon->ReadFromFile())
14 cycrow 2909
			this->SetIcon(icon, CFileIO(sRest).GetFileExtension());
6 cycrow 2910
	}
130 cycrow 2911
	else if (sFirst.Compare("CombineGameFiles"))
2912
		_bCombineFiles = sRest.Compare("true") || sRest.Compare("yes") || sRest.toInt();
134 cycrow 2913
	else if (sFirst.Compare("UpdateFile"))
2914
		this->createUpdateFile(sRest);
131 cycrow 2915
	else if (sFirst.Compare("ExportZip"))
2916
	{
2917
		Utils::String ext = "zip";
2918
		Utils::String game = sRest.word(1);
2919
		Utils::String file = _replaceFilename(CFileIO(sRest.words(2)).fullFilename());
2920
		if (game.contains("|"))
2921
		{
2922
			int max;
2923
			Utils::String *games = game.tokenise("|", &max);
2924
			if (games)
2925
			{
2926
				for (int i = 0; i < max; ++i)
2927
				{
2928
					unsigned int g = CBaseFile::GetGameFromString(games[i]);
2929
					Utils::String filename = CFileIO(file).dir() + "/" + CFileIO(file).baseName() + "_" + CBaseFile::ConvertGameToString(g) + "." + ext;
2930
					this->addAutoExport(g, filename);
2931
				}
2932
				CLEANSPLIT(games, max);
2933
			}
2934
		}
2935
		else
2936
		{
2937
			unsigned int g = CBaseFile::GetGameFromString(game);
2938
			Utils::String filename = CFileIO(file).dir() + "/" + CFileIO(file).baseName() + "_" + CBaseFile::ConvertGameToString(g) + "." + ext;
2939
			this->addAutoExport(g, filename);
2940
		}
2941
	}
2942
	else if (sFirst.Compare("Extract"))
2943
	{
2944
		Utils::String game = sRest.word(1);
2945
		Utils::String dir = CDirIO(sRest.words(2)).dir();
2946
		if (game.contains("|"))
2947
		{
2948
			int max;
2949
			Utils::String *games = game.tokenise("|", &max);
2950
			if (games)
2951
			{
2952
				for(int i = 0; i < max; ++i)
2953
					this->addAutoExtract(CBaseFile::GetGameFromString(games[i]), dir);
2954
				CLEANSPLIT(games, max);
2955
			}
2956
		}
2957
		else
2958
			this->addAutoExtract(CBaseFile::GetGameFromString(game), dir);
2959
	}
6 cycrow 2960
	else
2961
	{
14 cycrow 2962
		Utils::String checkType = sFirst;
6 cycrow 2963
		bool shared = false;
14 cycrow 2964
		if ( checkType.left(6).Compare("Shared") )
6 cycrow 2965
		{
14 cycrow 2966
			checkType = sFirst.right(-6);
6 cycrow 2967
			shared = true;
2968
		}
2969
 
2970
		// now check type name
127 cycrow 2971
		FileType filetype = GetFileTypeFromString(checkType);
2972
		if (filetype != FILETYPE_UNKNOWN)
134 cycrow 2973
			this->AddFileScript(filetype, shared, sRest, sMainGame, otherGames, gameAddons, progress);
6 cycrow 2974
		else if ( !checkType.Compare("changelog") )
2975
			return false;
2976
	}
2977
 
2978
	return true;
2979
}
2980
 
134 cycrow 2981
void CBaseFile::AddFileScript(FileType filetype, bool shared, CyString sRest, const Utils::String &sMainGame, Utils::CStringList &otherGames, Utils::CStringList &gameAddons, CProgressInfo *progress)
6 cycrow 2982
{
127 cycrow 2983
	Utils::String dir;
2984
	Utils::String rest = sRest.ToString();
2985
 
2986
	unsigned int mainGame = CBaseFile::GetGameFromString(sMainGame);
2987
	unsigned int game = 0;
2988
	if ( rest.token(" ", 1).left(4).Compare("GAME") ) {
2989
		Utils::String gameStr = rest.token(" ", 2);
2990
		if (gameStr.contains("|"))
2991
		{
2992
			int max = 0;
2993
			Utils::String *games = gameStr.tokenise("|", &max);
2994
			for (int i = 0; i < max; ++i)
2995
			{
2996
				unsigned int g = CBaseFile::GetGameFromString(games[i]);
2997
				if (g)
2998
					game |= 1 << g;
2999
			}
3000
			CLEANSPLIT(games, max);
3001
		}
3002
		else
131 cycrow 3003
		{
3004
			unsigned int g = CBaseFile::GetGameFromString(gameStr);
3005
			if (g)
3006
				game = 1 << g;
3007
		}
127 cycrow 3008
		rest = rest.tokens(" ", 3);
6 cycrow 3009
	}
127 cycrow 3010
	if (game)
3011
		game |= 1 << 31;
6 cycrow 3012
 
127 cycrow 3013
	if (rest.contains("|"))
3014
	{
3015
		dir = rest.tokens("|", 2);
3016
		rest = rest.token("|", 1);
6 cycrow 3017
	}
3018
 
127 cycrow 3019
	rest = rest.findReplace("\\", "/");
6 cycrow 3020
 
3021
	// wild cards
127 cycrow 3022
	if ( rest.containsAny("*?") )
6 cycrow 3023
	{
102 cycrow 3024
		CDirIO Dir(CFileIO(rest).dir());
6 cycrow 3025
		CyStringList *dirList = Dir.DirList();
3026
		if ( dirList )
3027
		{
3028
			for ( SStringList *strNode = dirList->Head(); strNode; strNode = strNode->next )
3029
			{
127 cycrow 3030
				Utils::String file = Dir.file(strNode->str.ToString());
3031
				if (file.match(rest))
6 cycrow 3032
				{
98 cycrow 3033
					int addGame = game;
3034
					// check if the file exists in the subdirectory too, if it does, add for each game
3035
					if ( game == GAME_ALL && !sMainGame.empty() && !otherGames.empty() ) {
3036
						CFileIO F(file);
3037
						for(Utils::String g = otherGames.firstString(); !g.empty(); g = otherGames.nextString()) {
102 cycrow 3038
							Utils::String checkDir = F.dir() + "/" + g;
121 cycrow 3039
							if ( CDirIO(checkDir).exists(F.filename()) ) {
98 cycrow 3040
								addGame = mainGame;
3041
								C_File *newfile = this->AppendFile(CDirIO(checkDir).File(F.filename()), filetype, CBaseFile::GetGameFromString(g), dir);
134 cycrow 3042
								if (newfile && progress)
3043
									progress->UpdateFile(newfile);
98 cycrow 3044
								if ( newfile ) newfile->SetShared(shared);
3045
							}
3046
						}
3047
					}
3048
 
3049
					C_File *newfile = this->AppendFile(file, filetype, addGame, dir);
134 cycrow 3050
					if (newfile && progress)
3051
						progress->UpdateFile(newfile);
6 cycrow 3052
					if ( newfile )
3053
						newfile->SetShared(shared);
3054
				}
3055
			}
3056
			delete dirList;
3057
		}
3058
	}
3059
	else
3060
	{
127 cycrow 3061
		unsigned int addGame = game;
98 cycrow 3062
		// check if the file exists in the subdirectory too, if it does, add for each game
3063
		if ( game == GAME_ALL && !sMainGame.empty() && !otherGames.empty() ) {
3064
			CFileIO F(rest);
3065
			for(Utils::String g = otherGames.firstString(); !g.empty(); g = otherGames.nextString()) {
102 cycrow 3066
				Utils::String checkDir = F.dir() + "/" + g;
121 cycrow 3067
				if ( CDirIO(checkDir).exists(F.filename()) ) {
98 cycrow 3068
					addGame = mainGame;
121 cycrow 3069
					C_File *newfile = this->AppendFile(CDirIO(checkDir).file(F.filename()), filetype, CBaseFile::GetGameFromString(g), dir);
134 cycrow 3070
					if (newfile && progress)
3071
						progress->UpdateFile(newfile);
98 cycrow 3072
					if ( newfile ) newfile->SetShared(shared);
3073
				}
3074
			}
3075
		}
3076
 
127 cycrow 3077
		unsigned int checkGame = addGame & ~(1 << 31);
3078
		unsigned int foundGame = 0;
3079
		for (int i = 0; i < 31; ++i)
3080
		{
3081
			if (1 << i == checkGame)
3082
			{
3083
				foundGame = i;
3084
				break;
3085
			}
3086
		}
3087
 
3088
		C_File *file = NULL;
3089
		if (foundGame)
3090
		{
3091
			Utils::String addon = gameAddons.findString(Utils::String::Number(foundGame));
3092
			if (!addon.empty())
3093
			{
3094
				Utils::String dir = C_File::GetDirectory(filetype, rest, this);
3095
				Utils::String filename = rest;
130 cycrow 3096
				if (CCatFile::IsAddonDir(dir) && !filename.contains(addon))
3097
				{
127 cycrow 3098
					filename = filename.findReplace(dir + "/", addon + "/" + dir + "/");
130 cycrow 3099
					file = this->AppendFile(filename, filetype, addGame, dir);
3100
				}
127 cycrow 3101
			}
3102
		}
3103
 
3104
		if(!file)
3105
			file = this->AppendFile(rest, filetype, addGame, dir);
3106
 
134 cycrow 3107
		if (file && progress)
3108
			progress->UpdateFile(file);
127 cycrow 3109
		if (file)
6 cycrow 3110
			file->SetShared(shared);
3111
	}
3112
}
3113
 
3114
 
3115
CyString CBaseFile::GetFullFileSizeString() { return SPK::GetSizeString ( this->GetFullFileSize() ); }
3116
 
52 cycrow 3117
// used for a multiple spk file
6 cycrow 3118
unsigned char *CBaseFile::CreateData(size_t *size, CProgressInfo *progress)
3119
{
52 cycrow 3120
	if ( this->WriteFile("temp.dat", progress) ) {
3121
		CFileIO File("temp.dat");
3122
		File.setAutoDelete(true);
3123
		return File.readAll(size);
6 cycrow 3124
	}
3125
	return NULL;
3126
}
3127
 
3128
 
3129
void CBaseFile::ConvertNormalMod(C_File *f, CyString to)
3130
{
3131
	C_File *match = this->FindMatchingMod(f);
3132
	if ( match )
3133
	{
3134
		// file link
3135
		if ( !match->GetData() )
3136
			match->ReadFromFile();
3137
		match->ChangeBaseName(to);
3138
	}
3139
 
3140
	// file link
3141
	if ( !f->GetData() )
3142
		f->ReadFromFile();
3143
 
3144
	f->ChangeBaseName(to);
3145
}
3146
 
3147
void CBaseFile::ConvertAutoText(C_File *f)
3148
{
3149
	CyString to;
3150
	if ( f->GetBaseName().IsIn("-L") )
3151
		to = CyString("0000-L") + f->GetBaseName().GetToken("-L", 2, 2);
3152
	else if ( f->GetBaseName().IsIn("-l") )
3153
		to = CyString("0000-L") + f->GetBaseName().GetToken("-l", 2, 2);
3154
	else
3155
		to = f->GetBaseName().Left(-4) + "0000";
3156
 
3157
	// file link
3158
	if ( !f->GetData() )
3159
		f->ReadFromFile();
3160
 
3161
	f->ChangeBaseName(to);
3162
 
3163
}
3164
 
3165
void CBaseFile::ConvertFakePatch(C_File *f)
3166
{
3167
	// find next available fake patch
3168
	int num = 0;
3169
 
3170
	bool found = true;
3171
	while ( found )
3172
	{
3173
		++num;
3174
		found = false;
3175
		CyString find = CyString::Number(num).PadNumber(2);
3176
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3177
		{
3178
			if ( node->Data()->GetFileType() != FILETYPE_MOD )
3179
				continue;
3180
			if ( !node->Data()->IsFakePatch() )
3181
				continue;
3182
			if ( node->Data()->GetBaseName().Compare(find) )
3183
			{
3184
				found = true;
3185
				break;
3186
			}
3187
		}
3188
	}
3189
 
3190
	CyString to = CyString::Number(num).PadNumber(2);
3191
	C_File *match = this->FindMatchingMod(f);
3192
 
3193
	// file link
3194
	if ( !f->GetData() )
3195
		f->ReadFromFile();
3196
 
3197
	f->ChangeBaseName(to);
3198
	if ( match )
3199
	{
3200
		// file link
3201
		if ( !match->GetData() )
3202
			match->ReadFromFile();
3203
		match->ChangeBaseName(to);
3204
	}
3205
}
3206
 
3207
C_File *CBaseFile::FindMatchingMod(C_File *f)
3208
{
3209
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3210
	{
3211
		if ( node->Data()->GetFileType() != FILETYPE_MOD )
3212
			continue;
3213
 
3214
		if ( f->GetFileExt().Compare(node->Data()->GetFileExt()) )
3215
			continue;
3216
 
3217
		if ( f->GetBaseName().Compare(node->Data()->GetBaseName()) )
3218
			return node->Data();
3219
	}
3220
 
3221
	return NULL;
3222
}
3223
 
3224
void CBaseFile::RenameFile(C_File *f, CyString baseName)
3225
{
3226
	if ( f->GetFileType() == FILETYPE_MOD )
3227
	{
3228
		C_File *match = this->FindMatchingMod(f);
3229
		if ( match )
3230
			match->ChangeBaseName(baseName);
3231
	}
3232
 
3233
	// need to edit the file
134 cycrow 3234
	if (f->GetFileType() == FILETYPE_SCRIPT || f->GetFileType() == FILETYPE_UNINSTALL)
3235
		f->RenameScript(baseName);
6 cycrow 3236
 
134 cycrow 3237
	f->ChangeBaseName(baseName);
6 cycrow 3238
}
3239
 
134 cycrow 3240
Utils::String CBaseFile::createUpdateFile(const Utils::String &dir) const
6 cycrow 3241
{
134 cycrow 3242
	Utils::String file = this->getNameValidFile() + "_" + this->author() + ".dat";
3243
	file.removeChar(' ');
6 cycrow 3244
 
134 cycrow 3245
	Utils::CStringList write;
3246
	write.pushBack("Package: " + this->name());
3247
	write.pushBack("Author: " + this->author());
3248
	write.pushBack("Version: " + this->version());
3249
	write.pushBack("File: " + CFileIO(this->filename()).filename());
6 cycrow 3250
 
3251
	CFileIO File(dir + "/" + file);
134 cycrow 3252
	if (File.writeFile(&write))
102 cycrow 3253
		return File.fullFilename();
134 cycrow 3254
	return Utils::String::Null();
6 cycrow 3255
}
3256
 
134 cycrow 3257
Utils::String CBaseFile::ErrorString(int error, const Utils::String &errorStr)
6 cycrow 3258
{
134 cycrow 3259
	if (error == SPKERR_NONE) return Utils::String::Null();
6 cycrow 3260
 
134 cycrow 3261
	Utils::String err;
6 cycrow 3262
 
126 cycrow 3263
	switch (error)
6 cycrow 3264
	{
126 cycrow 3265
	case SPKERR_MALLOC:
3266
		err = "Memory Failed";
3267
		break;
3268
	case SPKERR_FILEOPEN:
3269
		err = "Failed to open file";
3270
		break;
3271
	case SPKERR_FILEREAD:
3272
		err = "Failed to read file";
3273
		break;
3274
	case SPKERR_UNCOMPRESS:
3275
		err = "Failed to Uncompress";
3276
		break;
3277
	case SPKERR_WRITEFILE:
3278
		err = "Failed to write file";
3279
		break;
3280
	case SPKERR_CREATEDIRECTORY:
3281
		err = "Failed to create directory";
3282
		break;
3283
	case SPKERR_FILEMISMATCH:
3284
		err = "File count mismatch";
3285
		break;
6 cycrow 3286
	}
3287
 
134 cycrow 3288
	if (!err.empty())
6 cycrow 3289
	{
134 cycrow 3290
		if (!errorStr.empty())
6 cycrow 3291
		{
3292
			err += " (";
3293
			err += errorStr + ")";
3294
		}
3295
		return err;
3296
	}
3297
 
134 cycrow 3298
	return Utils::String::Number((long)error);
6 cycrow 3299
}
3300
 
126 cycrow 3301
bool CBaseFile::SaveToArchive(CyString filename, int game, const CGameExe *exes, CProgressInfo *progress)
6 cycrow 3302
{
131 cycrow 3303
	return saveToArchive(filename.ToString(), game, exes, progress);
3304
}
3305
bool CBaseFile::saveToArchive(const Utils::String &filename, int game, const CGameExe *exes, CProgressInfo *progress)
3306
{
3307
	CDirIO Dir(CFileIO(filename).dir());
3308
	if (!Dir.exists())
3309
		Dir.create();
3310
 
6 cycrow 3311
	TCHAR buf[5000];
3312
	wsprintf(buf, L"%hs", filename.c_str());
3313
 
3314
	HZIP hz = CreateZip(buf, 0);
126 cycrow 3315
	if (!hz) return false;
6 cycrow 3316
 
3317
	// read files and compress
3318
	ReadAllFilesToMemory();
126 cycrow 3319
	if (!UncompressAllFiles(progress))
6 cycrow 3320
	{
3321
		CloseZip(hz);
3322
		return false;
3323
	}
3324
 
126 cycrow 3325
	for (CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next())
6 cycrow 3326
	{
126 cycrow 3327
		if (game != -1) {
127 cycrow 3328
			if (game && !(node->Data()->game() & (1 << game)))
6 cycrow 3329
				continue;
127 cycrow 3330
			// extracting for all games, so ignore files that have a game set
3331
			if (!game && node->Data()->game() && node->Data()->game() != GAME_ALLNEW)
6 cycrow 3332
				continue;
3333
		}
131 cycrow 3334
		Utils::String fname = node->Data()->getNameDirectory(this);
126 cycrow 3335
 
3336
		// use the addon directory
3337
		if (node->Data()->isFileInAddon() && exes)
3338
		{
127 cycrow 3339
			unsigned int whatGame = game;
126 cycrow 3340
			if (game == -1)
127 cycrow 3341
			{
3342
				unsigned int checkGame = node->Data()->game() & ~GAME_ALLNEW;
3343
				whatGame = 0;
3344
				for (int i = 0; i < 31; ++i)
3345
				{
3346
					if (1 << i == checkGame)
3347
					{
3348
						whatGame = i;
3349
						break;
3350
					}
3351
				}
3352
			}
126 cycrow 3353
 
127 cycrow 3354
			if (whatGame > 0)
126 cycrow 3355
			{
127 cycrow 3356
				SGameExe *e = exes->GetGame(whatGame - 1);
3357
				if (e)
3358
				{
3359
					if (e->iFlags & EXEFLAG_ADDON)
3360
						fname = e->sAddon + "/" + fname;
3361
				}
126 cycrow 3362
			}
3363
		}
3364
 
6 cycrow 3365
		// create the directory
3366
		wsprintf(buf, L"%hs", fname.c_str());
127 cycrow 3367
		if (node->Data()->isExternalFile())
3368
		{
3369
			CFileIO file(node->Data()->filePointer());
3370
			size_t size = 0;
3371
			unsigned char *data = file.readAll(&size);
3372
			ZipAdd(hz, buf, data, size);
3373
			delete data;
3374
		}
3375
		else
3376
			ZipAdd(hz, buf, node->Data()->GetData(), node->Data()->GetDataSize());
6 cycrow 3377
	}
3378
 
109 cycrow 3379
	// if its a ship, then add any generated files
3380
	this->addGeneratedFiles(hz);
3381
 
6 cycrow 3382
	// add the data file
126 cycrow 3383
	Utils::CStringList list;
127 cycrow 3384
	Utils::CStringList addons;
3385
	if ( this->GeneratePackagerScript(false, &list, game, addons, true) )
6 cycrow 3386
	{
126 cycrow 3387
		if ( CFileIO("test.tmp").writeFile(&list) )
6 cycrow 3388
		{
3389
			ZipAdd(hz, L"pluginmanager.txt", L"test.tmp");
52 cycrow 3390
			CFileIO::Remove("test.tmp");
6 cycrow 3391
		}
3392
	}
3393
 
3394
	CloseZip(hz);
3395
 
3396
	return true;
3397
}
3398
 
13 cycrow 3399
int CBaseFile::GetGameFromString(const Utils::String &sGame)
6 cycrow 3400
{
3401
	int iGame = GAME_ALL;
3402
	if ( sGame.Compare("ALL") )
3403
		iGame = GAME_ALL;
3404
	else if ( sGame.Compare("X3") )
3405
		iGame = GAME_X3;
3406
	else if ( sGame.Compare("X2") )
3407
		iGame = GAME_X2;
3408
	else if ( sGame.Compare("X3TC") )
3409
		iGame = GAME_X3TC;
126 cycrow 3410
	else if (sGame.Compare("X3AP"))
6 cycrow 3411
		iGame = GAME_X3AP;
126 cycrow 3412
	else if (sGame.Compare("X3FL"))
3413
		iGame = GAME_X3FL;
13 cycrow 3414
	else if ( sGame.isNumber() )
3415
		iGame = sGame;
6 cycrow 3416
 
3417
	return iGame;
3418
}
3419
 
13 cycrow 3420
Utils::String CBaseFile::ConvertGameToString(int iGame)
6 cycrow 3421
{
13 cycrow 3422
	Utils::String game = "ALL";
6 cycrow 3423
 
3424
	switch(iGame) {
3425
		case GAME_ALL:
127 cycrow 3426
		case GAME_ALLNEW:
6 cycrow 3427
			game = "ALL";
3428
			break;
3429
		case GAME_X2:
3430
			game = "X2";
3431
			break;
3432
		case GAME_X3:
3433
			game = "X3";
3434
			break;
3435
		case GAME_X3TC:
3436
			game = "X3TC";
3437
			break;
3438
		case GAME_X3AP:
3439
			game = "X3AP";
3440
			break;
126 cycrow 3441
		case GAME_X3FL:
3442
			game = "X3FL";
3443
			break;
6 cycrow 3444
		default:
3445
			game = (long)iGame;
3446
	}
3447
 
3448
	return game;
3449
}
3450
 
3451
bool CBaseFile::IsGameInPackage(int game)
3452
{
3453
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3454
		if ( node->Data()->GetGame() == game )
3455
			return true;
3456
	}
3457
 
3458
	return false;
3459
}
3460
 
3461
bool CBaseFile::IsAnyGameInPackage()
3462
{
3463
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3464
		if ( node->Data()->GetGame() )
3465
			return true;
3466
	}
3467
 
3468
	return false;
3469
}
3470
 
88 cycrow 3471
Utils::String builtInWares()
3472
{
3473
	Utils::String str;
3474
	str += "28;0;0;0;0;59;5753;0;35714;1;1;0;35714;-100000;0;0;SS_WARE_SW_NEW1;\n";
3475
	str += "28;0;0;0;0;60;5763;0;33232;1;1;0;33232;0;1043;0;SS_WARE_SW_NEW2;\n";
3476
	str += "28;0;0;0;0;61;5773;0;21428;1;1;0;21428;0;1043;0;SS_WARE_SW_NEW3;\n";
3477
	str += "28;0;0;0;0;62;5783;0;56;1;1;0;56;-100000;0;0;SS_WARE_SW_NEW4;\n";
3478
	str += "28;0;0;0;0;63;5793;0;88;1;1;0;88;-100000;0;0;SS_WARE_SW_NEW5;\n";
3479
	str += "28;0;0;0;0;64;5803;0;283;1;1;0;283;-100000;0;0;SS_WARE_SW_NEW6;\n";
3480
	str += "28;0;0;0;0;65;5813;0;383;1;1;0;383;-100000;0;0;SS_WARE_SW_NEW7;\n";
3481
	str += "28;0;0;0;0;66;5823;0;1389;1;1;0;1389;-100000;1043;0;SS_WARE_SW_NEW8;\n";
3482
	str += "28;0;0;0;0;67;5833;0;3396;1;1;0;3396;-100000;0;0;SS_WARE_SW_NEW9;\n";
3483
	str += "28;0;0;0;0;68;5843;0;4215;1;1;0;4215;-100000;0;0;SS_WARE_SW_NEW10;\n";
3484
	str += "28;0;0;0;0;69;5853;0;5635;1;1;0;5635;-100000;0;0;SS_WARE_SW_NEW11;\n";
3485
	str += "28;0;0;0;0;70;5863;0;65735;1;1;0;65735;-100000;0;0;SS_WARE_SW_NEW12;\n";
3486
	str += "28;0;0;0;0;71;5873;0;17857;1;1;0;17857;333;1043;0;SS_WARE_SW_NEW13;\n";
3487
	str += "28;0;0;0;0;72;5883;0;21428;1;1;0;21428;0;1043;0;SS_WARE_SW_NEW14;\n";
3488
	str += "28;0;0;0;0;73;5893;0;324515;1;1;0;324515;-100000;0;0;SS_WARE_SW_NEW15;\n";
3489
	str += "28;0;0;0;0;74;5903;0;638508;1;1;0;638508;-100000;0;0;SS_WARE_SW_NEW16;\n";
3490
	str += "28;0;0;0;0;75;5913;0;225755;1;1;0;225755;-100000;0;0;SS_WARE_SW_NEW17;\n";
3491
	str += "28;0;0;0;0;76;5923;0;1931535;1;1;0;1931535;1000;0;0;SS_WARE_SW_NEW18;\n";
3492
	str += "28;0;0;0;0;77;5933;0;2209150;1;1;0;2209150;-100000;0;0;SS_WARE_SW_NEW19;\n";
3493
	str += "28;0;0;0;0;78;5943;0;6727565;1;1;0;6727565;-100000;0;0;SS_WARE_SW_NEW20;\n";
3494
	str += "28;0;0;0;0;85;9999;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_1;\n";
3495
	str += "28;0;0;0;0;86;15053;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_2;\n";
3496
	str += "28;0;0;0;0;87;15063;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_3;\n";
3497
	str += "28;0;0;0;0;88;15073;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_4;\n";
3498
	str += "28;0;0;0;0;89;15083;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_5;\n";
3499
	str += "28;0;0;0;0;90;15093;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_6;\n";
3500
 
3501
	return str;
3502
}
3503
 
3504
void CBaseFile::_addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType)
3505
{
3506
	int totalWares = 0;
3507
	Utils::String *w = wares.tokenise("\n", &totalWares);
3508
 
3509
	for(int i = 0; i < totalWares; i++) {
3510
		int textId = w[i].token(";", 7).toLong();
3511
		int useLang = iLang;
3512
		if ( !_pTextDB->exists(useLang, 17, textId) )
3513
			useLang = 44;
3514
		if ( !_pTextDB->exists(useLang, 17, textId) )
3515
			useLang = 49;
3516
		if ( _pTextDB->exists(useLang, 17, textId) ) {	
3517
			SWareEntry *ware = new SWareEntry;
3518
			ware->name = _pTextDB->get(useLang, 17, textId);
3519
			ware->description = _pTextDB->get(useLang, 17, textId + 1);
3520
			ware->id = w[i].token(";", -2);
3521
			ware->relval = w[i].token(";", 9).toLong();
3522
			ware->notority = w[i].token(";", 14).toLong();
3523
			ware->type = eType;
3524
			ware->position = i;
89 cycrow 3525
			ware->package = this;
88 cycrow 3526
			list.push_back(ware);
3527
		}
3528
	}
3529
 
3530
	CLEANSPLIT(w, totalWares);
3531
}
3532
 
3533
bool CBaseFile::readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares)
3534
{
3535
	_pTextDB->setLanguage(iLang);
3536
 
3537
	// now go through all emp wares and get the ones we have text for
3538
	_addWaresToList(iLang, list, empWares, Ware_EMP);
3539
	_addWaresToList(iLang, list, builtInWares(), Ware_BuiltIn);
3540
 
89 cycrow 3541
	return true;
3542
}
88 cycrow 3543
 
89 cycrow 3544
bool CBaseFile::_readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list)
3545
{
3546
	_pTextDB->setLanguage(iLang);
3547
 
3548
	for(int i = 2; i <= 13; i++) {
3549
		for(int j = 0; j < 64; j++) {
3550
			int id = (i * 100) + j;
3551
			if ( _pTextDB->exists(iLang, iStartID + 2, id) ) {
3552
				SCommandSlot *slot = new SCommandSlot;
3553
				list.push_back(slot);
3554
 
3555
				slot->id = _pTextDB->get(iLang, iStartID, id);
3556
				slot->name = _pTextDB->get(iLang, iStartID + 2, id);
3557
				slot->shortName = _pTextDB->get(iLang, iStartID + 3, id);
3558
				slot->info = _pTextDB->get(iLang, iStartID + 14, id);
3559
				slot->slot = id;
3560
				slot->package = this;
3561
 			}
3562
		}
3563
	}
3564
 
3565
	for(int i = 1400; i <= 2000; i++) {
3566
		if ( _pTextDB->exists(iLang, iStartID + 2, i) ) {
3567
			SCommandSlot *slot = new SCommandSlot;
3568
			list.push_back(slot);
3569
 
3570
			slot->id = _pTextDB->get(iLang, iStartID, i);
3571
			slot->name = _pTextDB->get(iLang, iStartID + 2, i);
3572
			slot->shortName = _pTextDB->get(iLang, iStartID + 3, i);
3573
			slot->info = _pTextDB->get(iLang, iStartID + 14, i);
3574
			slot->slot = i;
3575
			slot->package = this;
3576
		}
3577
	}
3578
 
3579
	for(int i = 6000; i <= 9999; i++) {
3580
		if ( _pTextDB->exists(iLang, iStartID + 2, i) ) {
3581
			SCommandSlot *slot = new SCommandSlot;
3582
			list.push_back(slot);
3583
 
3584
			slot->id = _pTextDB->get(iLang, iStartID, i);
3585
			slot->name = _pTextDB->get(iLang, iStartID + 2, i);
3586
			slot->shortName = _pTextDB->get(iLang, iStartID + 3, i);
3587
			slot->info = _pTextDB->get(iLang, iStartID + 14, i);
3588
			slot->slot = i;
3589
			slot->package = this;
3590
		}
3591
	}
3592
 
88 cycrow 3593
	return true;
3594
}
3595
 
89 cycrow 3596
bool CBaseFile::readWingCommands(int iLang, CLinkList<SCommandSlot> &list)
3597
{
3598
	return _readCommands(iLang, 2028, list);
3599
}
3600
 
3601
bool CBaseFile::readCommands(int iLang, CLinkList<SCommandSlot> &list)
3602
{
3603
	return _readCommands(iLang, 2008, list);
3604
}
3605
 
6 cycrow 3606
int CBaseFile::FindFirstGameInPackage()
3607
{
3608
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3609
		if ( node->Data()->GetGame() )
3610
			return node->Data()->GetGame();
3611
	}
3612
 
3613
	return 0;
3614
}
3615
bool CBaseFile::IsMultipleGamesInPackage()
3616
{
3617
	int game = 0;
3618
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3619
		if ( node->Data()->GetGame() ) {
3620
			if ( game != node->Data()->GetGame() )
3621
				return true;
3622
			game = node->Data()->GetGame();
3623
		}
3624
	}
3625
 
3626
	return false;
3627
}