Subversion Repositories spk

Rev

Rev 134 | Rev 147 | 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
129 cycrow 1243
		if ( fit->game() && packages->GetGame() && fit->game() != GAME_ALLNEW) {
1244
			if(!(fit->game() & (1 << packages->GetGame())))
1245
			{
1246
				CLog::logf(CLog::Log_Install, 1, "File didn't match game version, skipping, %d != %d", fit->game(), packages->GetGame());
6 cycrow 1247
				continue;
50 cycrow 1248
			}
6 cycrow 1249
		}
1250
 
50 cycrow 1251
		// update the progress display to show we are processing this file
1252
		if ( progress ) {
1253
			if ( progress->IsSecond() ) progress->SwitchSecond();
6 cycrow 1254
			progress->UpdateFile ( fit );
1255
			progress->UpdateProgress(fileCount++, m_lFiles.size());
1256
			progress->SwitchSecond();
1257
		}
1258
 
1259
		// first uncompress the file
50 cycrow 1260
		//TODO: add this flag to C_File
1261
		bool uncomprToFile;
1262
		if ( !this->_install_uncompress(fit, progress, errorStr, &uncomprToFile) ) {
1263
			bFailed = true;
1264
			continue;
6 cycrow 1265
		}
1266
 
50 cycrow 1267
		bool dofile = _install_checkVersion(fit, destdir.ToString());
6 cycrow 1268
 
1269
		// change file pointer
125 cycrow 1270
		Utils::String sInstallDir = _install_adjustFilepointer(fit, fileEnabled, destdir.ToString());
6 cycrow 1271
 
1272
		C_File *adjustPointer = NULL;
1273
 
50 cycrow 1274
		if ( filelist ) {
98 cycrow 1275
			C_File *cFile = _install_checkFile(fit, errorStr, &dofile, filelist);
6 cycrow 1276
 
1277
			// no matching file found, adding to main list
50 cycrow 1278
			if ( !cFile ) filelist->push_back ( fit );
6 cycrow 1279
			else
1280
			{
1281
				// if the file is not enabled, we need to check for any that might be enabled
51 cycrow 1282
				if ( !fileEnabled ) //_install_checkDisabled(cFile, destdir, errorStr);
6 cycrow 1283
				{
50 cycrow 1284
					//TODO: check what this is actually doing
6 cycrow 1285
					if ( !cFile->GetUsed() )
1286
					{
1287
						CFileIO rFile(cFile->GetFilePointer());
52 cycrow 1288
						if ( rFile.exists() )
6 cycrow 1289
						{
1290
							if ( errorStr )
1291
							{
52 cycrow 1292
								if ( rFile.remove() )
134 cycrow 1293
									errorStr->PushBack(cFile->GetFilePointer().Remove(destdir), ERRORLOG_OLD(SPKINSTALL_DELETEFILE));
6 cycrow 1294
								else
134 cycrow 1295
									errorStr->PushBack(cFile->GetFilePointer().Remove(destdir), ERRORLOG_OLD(SPKINSTALL_DELETEFILE_FAIL));
6 cycrow 1296
							}
1297
						}
1298
						cFile->SetFilename(fit->GetFilePointer());
1299
						cFile->SetDisabled(true);
1300
					}
1301
					else
1302
					{
125 cycrow 1303
						fit->SetFullDir ( CyString(sInstallDir) + fit->GetDirectory(this) );
51 cycrow 1304
						fit->SetDisabled(false);
6 cycrow 1305
					}
1306
				}
1307
				// move it to enabled
50 cycrow 1308
				else {
1309
					if ( !this->_install_checkFileEnable(cFile, fit, destdir.ToString(), fileEnabled, errorStr) ) {
1310
						bFailed = true;
1311
						continue;
6 cycrow 1312
					}
1313
				}
1314
 
1315
				adjustPointer = cFile;
50 cycrow 1316
				if ( dofile ) adjustPointer->SetCreationTime(fit->GetCreationTime());
6 cycrow 1317
			}
1318
		}
1319
 
1320
		if ( dofile )
1321
		{
1322
			// uncompressed to file, rename and move
1323
			if ( uncomprToFile )
1324
			{
134 cycrow 1325
				_iLastError = SPKERR_WRITEFILE;
1326
				Utils::String to = fit->getDirectory(this);
1327
				if ( !fileEnabled )	to = "PluginManager/Disabled/" + to;
6 cycrow 1328
 
134 cycrow 1329
				if ( !_install_createDirectory(Dir, to, fit, errorStr) ) {
51 cycrow 1330
					bFailed = true;
1331
					continue;
6 cycrow 1332
				}
1333
 
1334
				int err = 1;
134 cycrow 1335
				_sLastError = to;
51 cycrow 1336
				if ( !fit->GetTempFile ().Empty() )	err = rename ( fit->GetTempFile().c_str(), to.c_str() );
1337
				if ( err ) {
1338
					bFailed = true;
1339
					continue;
1340
				}
6 cycrow 1341
			}
1342
			//otherwise, just extract the file
1343
			else
1344
			{
1345
				// old file is found in list, switch to using new one
50 cycrow 1346
				if ( (filelist) && (adjustPointer) ) {
6 cycrow 1347
					adjustPointer->CopyData(fit, false);
50 cycrow 1348
					CLog::log(CLog::Log_Install, 2, "Copying data into existing file");
1349
				}
6 cycrow 1350
 
134 cycrow 1351
				Utils::String fpointer = fit->filePointer();
1352
				_iLastError = SPKERR_CREATEDIRECTORY;
1353
				Utils::String dir = CFileIO(fit->filePointer()).dir();
6 cycrow 1354
 
134 cycrow 1355
				dir = dir.findRemove(destdir.ToString());
1356
				if ( dir[0] == '/' || dir[0] == '\\' ) dir.erase(0, 1);
6 cycrow 1357
 
134 cycrow 1358
				if ( !_install_createDirectory(Dir, dir, fit, errorStr) ) {
51 cycrow 1359
					bFailed = true;
1360
					continue;
6 cycrow 1361
				}
1362
 
51 cycrow 1363
				_install_writeFile(fit, destdir.ToString(), errorStr);
6 cycrow 1364
			}
1365
			ClearError ();
1366
		}
1367
 
1368
		if ( adjustPointer )
1369
		{
50 cycrow 1370
			CLog::log(CLog::Log_Install, 2, "Adjusting pointers to existing file and deleting new file pointer");
6 cycrow 1371
			node->ChangeData(adjustPointer);
1372
			delete fit;
1373
		}
50 cycrow 1374
 
1375
		CLog::log(CLog::Log_Install, 1, "File installation completed");
6 cycrow 1376
	}
1377
 
1378
	// now clear or data memory
50 cycrow 1379
	CLog::log(CLog::Log_Install, 2, "Delting temporary file data from memory");
51 cycrow 1380
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) node->Data()->DeleteData();
6 cycrow 1381
 
50 cycrow 1382
	return !bFailed;
6 cycrow 1383
}
1384
 
1385
/*######################################################################################################*/
1386
 
1387
 
1388
/*
1389
	Func:   ParseHeader
1390
	Input:  Header String - string formated directly from the file
1391
	Return: Boolean - If string is a valid header
1392
	Desc:   Splits up the main header string to get all required settings
1393
*/
1394
bool CBaseFile::ParseHeader ( CyString header )
1395
{
14 cycrow 1396
	if ( !this->CheckHeader(header.GetToken ( 1, ';' ).ToString()) )
6 cycrow 1397
		return false;
1398
 
1399
	m_SHeader.fVersion = header.GetToken ( 2, ';' ).ToFloat();
1400
	if ( m_SHeader.fVersion > FILEVERSION )
1401
		return false;
1402
 
1403
	m_SHeader.iValueCompression = header.GetToken ( 3, ';' ).ToInt();
1404
	m_SHeader.lValueCompressSize = header.GetToken ( 4, ';' ).ToLong();
1405
 
1406
	return true;
1407
}
1408
 
14 cycrow 1409
bool CBaseFile::CheckHeader(const Utils::String header) const
6 cycrow 1410
{
1411
	if ( header.Compare("BaseCycrow") )
1412
		return true;
1413
	return false;
1414
}
1415
 
1416
/*
1417
	Func:   ParseFileHeader
1418
	Input:  Header String - string formated directly from the file
1419
	Return: Boolean - If string is a valid header
1420
	Desc:   Splits up the file header string to get all required settings
1421
*/
1422
bool CBaseFile::ParseFileHeader ( CyString header )
1423
{
1424
	if ( header.GetToken ( 1, ';' ) != "FileHeader" )
1425
		return false;
1426
 
1427
	m_SHeader2.iNumFiles = header.GetToken ( 2, ';' ).ToInt();
1428
	m_SHeader2.lSize = header.GetToken ( 3, ';' ).ToInt();
1429
	m_SHeader2.lFullSize = header.GetToken ( 4, ';' ).ToInt();
1430
	m_SHeader2.iFileCompression = header.GetToken ( 5, ';' ).ToInt();
1431
	m_SHeader2.iDataCompression = header.GetToken ( 6, ';' ).ToInt();
1432
 
1433
	return true;
1434
}
1435
 
1436
 
1437
/*
1438
	Func:   ParseValueLine
1439
	Input:  String - single line from a file to set
1440
	Return: Boolean - returns true if value exists
1441
	Desc:   Reads the line and assigns the parameters for the file
1442
*/
14 cycrow 1443
bool CBaseFile::ParseValueLine(const Utils::String &sLine)
6 cycrow 1444
{
14 cycrow 1445
	Utils::String first = sLine.token(" ", 1);
1446
	Utils::String rest  = sLine.tokens(" ", 2);
6 cycrow 1447
 
50 cycrow 1448
	if ( first.Compare("Name:") )					this->setName(rest);
1449
	else if ( first.Compare("Author:") )			this->setAuthor(rest);
1450
	else if ( first.Compare("Version:") )			this->setVersion(rest);
14 cycrow 1451
	else if ( first.Compare("fGameVersion:") ) {
6 cycrow 1452
		if ( m_lGames.Back() ) {
1453
			m_lGames.Back()->Data()->sVersion = rest;
1454
		}
1455
	}
14 cycrow 1456
	else if ( first.Compare("GameVersion:") ) {
6 cycrow 1457
		if ( m_lGames.Back() ) {
14 cycrow 1458
			m_lGames.Back()->Data()->iVersion = rest;
6 cycrow 1459
		}
1460
	}
14 cycrow 1461
	else if ( first.Compare("Game:") )
46 cycrow 1462
		this->AddGameCompatability(rest, "");
14 cycrow 1463
	else if ( first.Compare("GameCompat:") )
1464
		this->AddGameCompatability(rest.token(" ", 1), rest.tokens(" ", 2));
1465
	else if ( first.Compare("GameCompatExact:") )
1466
		this->AddGameCompatability(rest.token(" ", 1), rest.tokens(" ", 2));
50 cycrow 1467
	else if ( first.Compare("Date:") )				this->setCreationDate(rest);
49 cycrow 1468
	else if ( first.Compare("WebAddress:") )		this->setWebAddress(rest);
1469
	else if ( first.Compare("WebSite:") )			this->setWebSite(rest);
1470
	else if ( first.Compare("Email:") )				this->setEmail(rest);
14 cycrow 1471
	else if ( first.Compare("WebMirror1:") || first.Compare("Mirror1:") || first.Compare("WebMirror:") )
6 cycrow 1472
		this->AddWebMirror(rest);
14 cycrow 1473
	else if ( first.Compare("WebMirror2:") || first.Compare("Mirror2:") )
6 cycrow 1474
		this->AddWebMirror(rest);
48 cycrow 1475
	else if ( first.Compare("PluginType:") )		this->setPluginType(rest);
1476
	else if ( first.Compare("Desc:") )				this->setDescription(rest);
1477
	else if ( first.Compare("UninstallAfter:") )	this->addUninstallText(ParseLanguage(rest.token("|", 1)), false, rest.tokens("|", 2));
1478
	else if ( first.Compare("UninstallBefore:") )	this->addUninstallText(ParseLanguage(rest.token("|", 1)), true, rest.tokens("|", 2));
1479
	else if ( first.Compare("InstallAfter:") )		this->addInstallText(ParseLanguage(rest.token("|", 1)), false, rest.tokens("|", 2));
1480
	else if ( first.Compare("InstallBefore:") )		this->addInstallText(ParseLanguage(rest.token("|", 1)), true, rest.tokens("|", 2));
14 cycrow 1481
	else if ( first.Compare("ScriptName:") )
1482
		AddLanguageName(ParseLanguage(rest.token(":", 1)), rest.token(":", 2));
48 cycrow 1483
	else if ( first.Compare("GameChanging:") )		this->setGameChanging(rest);
1484
	else if ( first.Compare("EaseOfUse:") )			this->setEaseOfUse(rest);
1485
	else if ( first.Compare("Recommended:") )		this->setRecommended(rest);
14 cycrow 1486
	else if ( first.Compare("NeededLibrary:") )
1487
		this->AddNeededLibrary(rest.token("||", 1), rest.token("||", 2), rest.token("||", 3));
1488
	else if ( first.Compare("FakePatchBefore:") )
1489
		this->AddFakePatchOrder(false, rest.token("||", 1), rest.token("||", 2));
1490
	else if ( first.Compare("FakePatchAfter:") )
1491
		this->AddFakePatchOrder(true, rest.token("||", 1), rest.token("||", 2));
49 cycrow 1492
	else if ( first.Compare("ForumLink:") )			this->setForumLink(rest);
6 cycrow 1493
	else
1494
		return false;
1495
 
1496
	return true;
1497
}
1498
 
48 cycrow 1499
int CBaseFile::ParseLanguage(const Utils::String &lang) const
6 cycrow 1500
{
48 cycrow 1501
	int langID = lang;
1502
	if ( !langID ) {
1503
		if ( lang.Compare("english") )		return 44;
1504
		else if ( lang.Compare("default") )	return 0;
1505
		else if ( lang.Compare("german") )	return 49;
1506
		else if ( lang.Compare("russian") )	return 7;
1507
		else if ( lang.Compare("spanish") )	return 34;
1508
		else if ( lang.Compare("french") )	return 33;
6 cycrow 1509
	}
1510
 
1511
	return langID;
1512
}
1513
 
1514
/*
1515
	Func:   ReadValues
1516
	Input:  String - values in one long line
1517
	Desc:   splits the values data into each line to read the data
1518
*/
1519
void CBaseFile::ReadValues ( CyString values )
1520
{
1521
	int num = 0;
1522
	CyString *lines = values.SplitToken ( '\n', &num );
1523
 
1524
	for ( int i = 0; i < num; i++ )
14 cycrow 1525
		ParseValueLine ( lines[i].ToString() );
6 cycrow 1526
 
1527
	CLEANSPLIT(lines, num)
1528
}
1529
 
1530
/*
1531
	Func:   ParseFilesLine
1532
	Input:  String - single line from a file to set
1533
	Return: Boolean - returns true if value exists
1534
	Desc:   Reads the line and assigns the parameters for the file
1535
*/
127 cycrow 1536
bool CBaseFile::ParseFilesLine ( CyString sLine )
6 cycrow 1537
{
127 cycrow 1538
	Utils::String line = sLine.ToString();
1539
 
1540
	if ( !line.contains(":") )
6 cycrow 1541
		return false;
1542
 
127 cycrow 1543
	Utils::String command = line.token(":", 1);
6 cycrow 1544
 
127 cycrow 1545
	long size = line.token(":", 2).toInt();
1546
	long usize = line.token(":", 3).toInt ();
1547
	long compression = line.token(":", 4).toInt ();
6 cycrow 1548
 
1549
	if ( command == "Icon" )
1550
	{
127 cycrow 1551
		m_sIconExt = line.token (":", 5);
6 cycrow 1552
		m_pIconFile = new C_File ();
1553
		m_pIconFile->SetDataSize ( size - 4 );
1554
		m_pIconFile->SetDataCompression ( compression );
1555
		m_pIconFile->SetUncompressedDataSize ( usize );
1556
 
1557
		return true;
1558
	}
1559
 
127 cycrow 1560
	time_t time = line.token(":", 5).toLong();
1561
	bool compressToFile = (line.token(":", 6).toInt() == 1) ? true : false;
1562
	Utils::String name  = line.token(":", 7);
1563
	Utils::String dir = line.token(":", 8);
6 cycrow 1564
 
127 cycrow 1565
	if ( name.empty() )
6 cycrow 1566
		return true;
1567
 
1568
	bool shared = false;
127 cycrow 1569
	if ( command.left(1) == "$" )
6 cycrow 1570
	{
1571
		shared = true;
127 cycrow 1572
		command.erase(0, 1);
6 cycrow 1573
	}
1574
 
127 cycrow 1575
	FileType type = FILETYPE_UNKNOWN;
6 cycrow 1576
	if ( command == "Script" )
1577
		type = FILETYPE_SCRIPT;
1578
	else if ( command == "Text" )
1579
		type = FILETYPE_TEXT;
1580
	else if ( command == "Readme" )
1581
		type = FILETYPE_README;
1582
	else if ( command == "Map" )
1583
		type = FILETYPE_MAP;
1584
	else if ( command == "Mod" )
1585
		type = FILETYPE_MOD;
1586
	else if ( command == "Uninstall" )
1587
		type = FILETYPE_UNINSTALL;
1588
	else if ( command == "Sound" )
1589
		type = FILETYPE_SOUND;
1590
	else if ( command == "Mission" )
1591
		type = FILETYPE_MISSION;
1592
	else if ( command == "Extra" )
1593
		type = FILETYPE_EXTRA;
1594
	else if ( command == "Screen" )
1595
		type = FILETYPE_SCREEN;
1596
	else if ( command == "Backup" )
1597
		type = FILETYPE_BACKUP;
1598
	else if ( command == "Advert" )
1599
		type = FILETYPE_ADVERT;
1600
	else if ( command == "ShipScene" )
1601
		type = FILETYPE_SHIPSCENE;
1602
	else if ( command == "CockpitScene" )
1603
		type = FILETYPE_COCKPITSCENE;
1604
	else if ( command == "ShipOther" )
1605
		type = FILETYPE_SHIPOTHER;
1606
	else if ( command == "ShipModel" )
1607
		type = FILETYPE_SHIPMODEL;
1608
 
127 cycrow 1609
	if (type == FILETYPE_UNKNOWN)
6 cycrow 1610
		return false;
1611
 
127 cycrow 1612
	C_File *file = new C_File();
6 cycrow 1613
 
127 cycrow 1614
	if (dir.left(5).Compare("GAME_")) {
130 cycrow 1615
		unsigned int iGame = dir.token("_", 2).toInt();
1616
		if (!iGame)
1617
			file->setGame(0);
1618
		else
1619
			file->setGame(1 << 31 | 1 << iGame);
127 cycrow 1620
		dir = Utils::String::Null();
6 cycrow 1621
	} 
127 cycrow 1622
	else if ( line.countToken(":") >= 9 ) 
1623
	{
1624
		Utils::String game = line.token(":", 9);
1625
		if (game.contains("_"))
130 cycrow 1626
		{
1627
			unsigned int iGame = game.token("_", 2).toInt();
1628
			if (iGame)
1629
				file->setGame(1 << 31 | 1 << iGame);
1630
			else
1631
				file->setGame(0);
1632
		}
127 cycrow 1633
		else
1634
		{
1635
			int iGame = game.toInt();
1636
			if (iGame & (1 << 31))
1637
				file->setGame(iGame);
130 cycrow 1638
			else if (!iGame)
1639
				file->setGame(0);
127 cycrow 1640
			else
1641
				file->setGame(1 << 31 | 1 << iGame);
1642
		}
6 cycrow 1643
	}
1644
 
127 cycrow 1645
	if (dir.Compare("NULL")) {
1646
		dir = Utils::String::Null();
1647
	}
1648
 
1649
	file->setFileType(type);
6 cycrow 1650
	file->SetCreationTime ( time );
1651
	file->SetName ( name );
1652
	file->SetDir ( dir );
1653
	file->SetDataSize ( size - 4 );
1654
	file->SetDataCompression ( compression );
1655
	file->SetUncompressedDataSize ( usize );
1656
	file->SetShared ( shared );
1657
	file->SetCompressedToFile ( compressToFile );
1658
 
88 cycrow 1659
	_addFile(file, true);
6 cycrow 1660
 
1661
	return true;
1662
}
1663
 
1664
 
1665
/*
1666
	Func:   ParseFiles
1667
	Input:  String - values in one long line
1668
	Desc:   splits the files data into each line to read the data
1669
*/
1670
void CBaseFile::ReadFiles ( CyString values )
1671
{
1672
	int num = 0;
1673
	CyString *lines = values.SplitToken ( '\n', &num );
1674
 
1675
	for ( int i = 0; i < num; i++ )
1676
		ParseFilesLine ( lines[i] );
1677
 
1678
	CLEANSPLIT(lines, num)
1679
}
1680
 
1681
 
1682
 
1683
/*
1684
	Func:   ReadFile
1685
	Input:  filename - the name of the file to open and read
1686
			readdata - If falses, dont read the files to memory, just read the headers and values
1687
	Return: boolean - return ture if acceptable format
1688
	Desc:   Opens and reads the spk file and loads all data into class
1689
*/
130 cycrow 1690
bool CBaseFile::ReadFile(CyString filename, int readtype, CProgressInfo *progress)
6 cycrow 1691
{
130 cycrow 1692
	return readFile(filename.ToString(), readtype, progress);
1693
}
1694
bool CBaseFile::readFile(const Utils::String &filename, int readtype, CProgressInfo *progress)
1695
{
1696
	CFileIO File(filename);
58 cycrow 1697
	if ( !File.startRead() ) return false;
6 cycrow 1698
 
58 cycrow 1699
	bool ret = this->readFile(File, readtype, progress);
130 cycrow 1700
	if ( ret ) this->setFilename(filename);
6 cycrow 1701
 
58 cycrow 1702
	File.close();
6 cycrow 1703
	return ret;
1704
}
51 cycrow 1705
 
109 cycrow 1706
int CBaseFile::_read_Header(CFileIO &File, int iReadType, int iMaxProgress, CProgressInfo *pProgress)
6 cycrow 1707
{
51 cycrow 1708
	int doneLen = 0;
6 cycrow 1709
 
51 cycrow 1710
	// read data to memory
1711
	unsigned char *readData;
1712
	try {
1713
		readData = new unsigned char[m_SHeader.lValueCompressSize];
1714
	}
1715
	catch (std::exception &e) {
1716
		CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_Header() unable to malloc [header], %d (%s)", m_SHeader.lValueCompressSize, e.what());
1717
		return -1;
1718
	}
6 cycrow 1719
 
51 cycrow 1720
	unsigned char size[4];
109 cycrow 1721
	File.read(size, 4);
1722
	File.read(readData, m_SHeader.lValueCompressSize);
6 cycrow 1723
 
51 cycrow 1724
	unsigned long uncomprLen = (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
6 cycrow 1725
 
51 cycrow 1726
	// check for zlib compression
1727
	if ( m_SHeader.iValueCompression == SPKCOMPRESS_ZLIB ) {
1728
		// uncomress the data
1729
		try {
6 cycrow 1730
			unsigned char *uncompr = new unsigned char[uncomprLen];
1731
			int err = uncompress ( uncompr, &uncomprLen, readData, m_SHeader.lValueCompressSize );
1732
			// update the progress for each section
51 cycrow 1733
			if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(2, iMaxProgress);
1734
			if ( err == Z_OK ) this->ReadValues ( CyString ((char *)uncompr) );
6 cycrow 1735
			doneLen = uncomprLen;
1736
			delete uncompr;
1737
		}
51 cycrow 1738
		catch (std::exception &e) {
1739
			CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_Header() unable to malloc [uncompr], %d (%s)", uncomprLen, e.what());
53 cycrow 1740
			delete []readData;
51 cycrow 1741
			return -1;
6 cycrow 1742
		}
51 cycrow 1743
	}
1744
	else if ( m_SHeader.iValueCompression == SPKCOMPRESS_7ZIP ) {
1745
		long len = uncomprLen;
1746
		unsigned char *compr = LZMADecode_C ( readData, m_SHeader.lValueCompressSize, (size_t*)&len, NULL );
1747
		// update the progress for each section
1748
		if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(2, iMaxProgress);
6 cycrow 1749
 
51 cycrow 1750
		if ( compr ) ReadValues ( CyString ((char *)compr) );
6 cycrow 1751
	}
51 cycrow 1752
	// no compression
1753
	else
1754
		ReadValues ( CyString ((char *)readData) );
6 cycrow 1755
 
53 cycrow 1756
	delete []readData;
6 cycrow 1757
 
51 cycrow 1758
	return doneLen;
1759
}
6 cycrow 1760
 
109 cycrow 1761
int CBaseFile::_read_FileHeader(CFileIO &File, int iReadType, int iMaxProgress, int iDoneLen, CProgressInfo *pProgress)
51 cycrow 1762
{
1763
	unsigned char *readData;
1764
	try {
1765
		readData = new unsigned char[m_SHeader2.lSize];
1766
	}
1767
	catch (std::exception &e) {
1768
		CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_FileHeader() unable to malloc [header], %d (%s)", m_SHeader2.lSize, e.what());
1769
		return -1;
1770
	}
6 cycrow 1771
 
51 cycrow 1772
	unsigned char size[4];
109 cycrow 1773
	File.read(size, 4);
1774
	File.read(readData, m_SHeader2.lSize);
6 cycrow 1775
 
51 cycrow 1776
	unsigned long uncomprLen = (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
6 cycrow 1777
 
51 cycrow 1778
	// check for zlib compression
1779
	if ( m_SHeader.iValueCompression == SPKCOMPRESS_ZLIB ) {
1780
		if ( uncomprLen < (unsigned long)iDoneLen ) uncomprLen = iDoneLen;
6 cycrow 1781
 
51 cycrow 1782
		try {
6 cycrow 1783
			unsigned char *uncompr = new unsigned char[uncomprLen];
1784
			int err = uncompress ( uncompr, &uncomprLen, readData, m_SHeader2.lSize );
1785
			// update the progress for each section
51 cycrow 1786
			if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(5, iMaxProgress);
1787
			if ( err == Z_OK ) ReadFiles ( CyString ((char *)uncompr) );
6 cycrow 1788
			delete uncompr;
1789
		}
51 cycrow 1790
		catch (std::exception &e) {
1791
			CLog::logf(CLog::Log_IO, 2, "CBaseFile::_read_FileHeader() unable to malloc [uncompr], %d (%s)", uncomprLen, e.what());
53 cycrow 1792
			delete []readData;
51 cycrow 1793
			return -1;
6 cycrow 1794
		}
1795
 
1796
	}
51 cycrow 1797
	else if ( m_SHeader.iValueCompression == SPKCOMPRESS_7ZIP )
1798
	{
1799
		long len = uncomprLen;
1800
		unsigned char *compr = LZMADecode_C ( readData, m_SHeader2.lSize, (size_t*)&len, NULL );
1801
		// update the progress for each section
1802
		if ( iReadType != SPKREAD_ALL && pProgress ) pProgress->UpdateProgress(5, iMaxProgress);
1803
		if ( compr ) ReadFiles ( CyString ((char *)compr) );
1804
	}
1805
	else
1806
		ReadFiles ( CyString ((char *)readData) );
6 cycrow 1807
 
53 cycrow 1808
	delete []readData;
51 cycrow 1809
	return true;
1810
}
1811
 
58 cycrow 1812
bool CBaseFile::readFile(CFileIO &File, int readtype, CProgressInfo *progress)
51 cycrow 1813
{
1814
	ClearError ();
1815
 
1816
	// first read the header
58 cycrow 1817
	if ( !ParseHeader(File.readEndOfLine()) ) return false;
51 cycrow 1818
	if ( readtype == SPKREAD_HEADER ) return true;
1819
 
1820
	// update the progress for each section
1821
	int maxProgress = (readtype == SPKREAD_VALUES) ? 3 : 6;
1822
	if ( readtype != SPKREAD_ALL && progress ) progress->UpdateProgress(1, maxProgress);
1823
 
1824
	long doneLen = 0;
1825
	// next read the data values for the spk files
109 cycrow 1826
	if ( m_SHeader.lValueCompressSize ) doneLen = this->_read_Header(File, readtype, maxProgress, progress);
51 cycrow 1827
 
1828
	// update the progress for each section
1829
	if ( readtype != SPKREAD_ALL && progress ) progress->UpdateProgress(3, maxProgress);
1830
	if ( readtype == SPKREAD_VALUES ) return true;
1831
 
1832
	// next should be the next header
58 cycrow 1833
	if ( !ParseFileHeader(File.readEndOfLine()) ) return false;
51 cycrow 1834
 
1835
	// clear the current file list
1836
	m_lFiles.clear(true);
88 cycrow 1837
	if ( _pTextDB ) {
1838
		delete _pTextDB;
1839
		_pTextDB = NULL;
1840
	}
51 cycrow 1841
 
1842
	// update the progress for each section
1843
	if ( readtype != SPKREAD_ALL && progress ) progress->UpdateProgress(4, maxProgress);
1844
 
109 cycrow 1845
	if ( m_SHeader2.lSize ) this->_read_FileHeader(File, readtype, maxProgress, doneLen, progress);
51 cycrow 1846
 
6 cycrow 1847
	// file mismatch
1848
	long numfiles = m_lFiles.size();
51 cycrow 1849
	if ( m_pIconFile ) ++numfiles;
1850
	if ( m_SHeader2.iNumFiles != numfiles ) {
134 cycrow 1851
		_iLastError = SPKERR_FILEMISMATCH;
6 cycrow 1852
		return false;
1853
	}
1854
 
1855
	// update the progress for each section
51 cycrow 1856
	if ( readtype != SPKREAD_ALL && progress )	progress->UpdateProgress(6, maxProgress);
6 cycrow 1857
 
51 cycrow 1858
	if ( readtype == SPKREAD_ALL ) {
6 cycrow 1859
		int fileCount = 2;
58 cycrow 1860
		if ( m_pIconFile ) m_pIconFile->readFromFile(File, m_pIconFile->GetDataSize());
6 cycrow 1861
 
51 cycrow 1862
		// ok finally we need to read all the file
1863
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
58 cycrow 1864
			node->Data()->readFromFile(File, node->Data()->GetDataSize());
51 cycrow 1865
			if ( progress )	progress->UpdateProgress(fileCount++, m_lFiles.size() + 2);
6 cycrow 1866
		}
1867
 
1868
		m_bFullyLoaded = true;
1869
	}
1870
 
1871
	return true;
1872
}
1873
 
1874
bool CBaseFile::IsMod()
1875
{
1876
	// check for any mod files that are not fake patchs
1877
	for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
1878
	{
1879
		C_File *f = fNode->Data();
1880
		if ( f->GetFileType() != FILETYPE_MOD )
1881
			continue;
1882
 
1883
		if ( !f->IsFakePatch() )
1884
			return true;
1885
	}
1886
 
1887
	return false;
1888
}
1889
 
13 cycrow 1890
bool CBaseFile::IsFakePatch() const
6 cycrow 1891
{
1892
	// check for any mod files that are not fake patchs
1893
	for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
1894
	{
1895
		C_File *f = fNode->Data();
1896
		if ( f->GetFileType() != FILETYPE_MOD )
1897
			continue;
1898
 
1899
		if ( f->IsFakePatch() )
1900
			return true;
1901
	}
1902
 
1903
	return false;
1904
}
1905
 
14 cycrow 1906
Utils::String CBaseFile::CreateValuesLine () const
6 cycrow 1907
{
14 cycrow 1908
	Utils::String values("Name: ");
50 cycrow 1909
	values += this->name() + "\n";
1910
	values += "Author: " + this->author() + "\n";
1911
	values += "Version: " + this->version() + "\n";
1912
	if ( !this->creationDate().empty() )values += "Date: "			+ this->creationDate()	+ "\n";
49 cycrow 1913
	if ( !this->webAddress().empty() )	values += "WebAddress: "	+ this->webAddress()	+ "\n";
1914
	if ( !this->webSite().empty() )		values += "WebSite: "		+ this->webSite()		+ "\n";
1915
	if ( !this->email().empty() )		values += "Email: "			+ this->email()			+ "\n";
1916
	if ( !this->forumLink().empty() )	values += "ForumLink: "		+ this->forumLink()		+ "\n";
1917
 
6 cycrow 1918
	for ( SStringList *str = m_lMirrors.Head(); str; str = str->next )
14 cycrow 1919
		values += Utils::String("WebMirror: ") + str->str.ToString() + "\n";
48 cycrow 1920
	if ( !this->description().empty() ) {
1921
		Utils::String desc = this->description();
14 cycrow 1922
		desc = desc.findReplace("<newline>", "<br>");
1923
		desc = desc.findReplace("\n", "<br>");
1924
		desc.remove('\r');
1925
		values += "Desc: " + desc + "\n";
6 cycrow 1926
	}
1927
 
1928
	for ( CListNode<SGameCompat> *gc = m_lGames.Front(); gc; gc = gc->next() ) {
46 cycrow 1929
		if ( !gc->Data()->sVersion.empty() )
1930
			values += Utils::String("GameCompatExact: ") + (long)gc->Data()->iGame + " " + gc->Data()->sVersion + "\n";
6 cycrow 1931
		else
14 cycrow 1932
			values += Utils::String("GameCompat: ") + (long)gc->Data()->iGame + " " + (long)gc->Data()->iVersion + "\n";
6 cycrow 1933
	}
1934
 
1935
	if ( m_bSigned )
1936
		values += "Signed\n";
1937
 
46 cycrow 1938
	for ( int j = 0; j < 2; j++ ) {
1939
		const CInstallText *text;
1940
		Utils::String textStart;
1941
		switch(j) {
1942
			case 0:	textStart = "Uninstall"; text = this->uninstallText(); break;
1943
			case 1: textStart = "Install"; text = this->installText(); break;
1944
		}
1945
		for ( unsigned int i = 0; i < text->count(); i++ ) {
1946
			int iLang = text->language(i);
1947
			if ( !text->getBefore(iLang).empty() ) values += textStart + "Before: " + (long)iLang + "|" + text->getBefore(iLang) + "\n";
1948
			if ( !text->getAfter(iLang).empty() ) values += textStart + "After: " + (long)iLang + "|" + text->getAfter(iLang) + "\n";
1949
		}
6 cycrow 1950
	}
1951
 
46 cycrow 1952
	values += Utils::String("GameChanging: ") + (long)gameChanging() + "\n";
1953
	values += Utils::String("EaseOfUse: ") + (long)easeOfUse() + "\n";
1954
	values += Utils::String("Recommended: ") + (long)recommended() + "\n";
6 cycrow 1955
 
14 cycrow 1956
	for ( CListNode<SNames> *nNode = m_lNames.Front(); nNode; nNode = nNode->next() )
46 cycrow 1957
		values += Utils::String("ScriptName: ") + (long)nNode->Data()->iLanguage + ":" + nNode->Data()->sName + "\n";
6 cycrow 1958
 
14 cycrow 1959
	for ( CListNode<SNeededLibrary> *libNode = m_lNeededLibrarys.Front(); libNode; libNode = libNode->next() ) {
6 cycrow 1960
		SNeededLibrary *l = libNode->Data();
14 cycrow 1961
		values += (CyString("NeededLibrary: ") + l->sName + "||" + l->sAuthor + "||" + l->sMinVersion + "\n").ToString();
6 cycrow 1962
	}
1963
 
1964
	for ( SStringList *fpbNode = m_lFakePatchBefore.Head(); fpbNode; fpbNode = fpbNode->next )
14 cycrow 1965
		values += (CyString("FakePatchBefore: ") + fpbNode->str + "||" + fpbNode->data + "\n").ToString();
6 cycrow 1966
	for ( SStringList *fpaNode = m_lFakePatchAfter.Head(); fpaNode; fpaNode = fpaNode->next )
14 cycrow 1967
		values += (CyString("FakePatchAfter: ") + fpaNode->str + "||" + fpaNode->data + "\n").ToString();
6 cycrow 1968
 
48 cycrow 1969
	values += Utils::String("PluginType: ") + (long)this->pluginType() + "\n";
6 cycrow 1970
 
1971
	return values;
1972
}
1973
 
1974
 
1975
/*
1976
	Func:   WriteFile
1977
	Input:  filename - The filename of the spk file to write to
1978
	Desc:   Writes the data to an spk file
1979
*/
1980
bool CBaseFile::WriteFile ( CyString filename, CProgressInfo *progress )
1981
{
52 cycrow 1982
	CFileIO File(filename);
1983
	if ( File.startWrite() ) return WriteData(File, progress);
1984
	return false;
6 cycrow 1985
}
1986
 
52 cycrow 1987
bool CBaseFile::WriteHeader(CFileIO &file, int valueheader, int valueComprLen)
6 cycrow 1988
{
52 cycrow 1989
	return file.write("BaseCycrow;%.2f;%d;%d\n", FILEVERSION, valueheader, valueComprLen);
6 cycrow 1990
}
1991
 
52 cycrow 1992
bool CBaseFile::WriteData(CFileIO &file, CProgressInfo *progress )
6 cycrow 1993
{
1994
	int valueheader = m_SHeader.iValueCompression, fileheader = m_SHeader.iValueCompression;
1995
	if ( valueheader == SPKCOMPRESS_7ZIP )
1996
		valueheader = SPKCOMPRESS_ZLIB;
1997
	if ( fileheader == SPKCOMPRESS_7ZIP )
1998
		fileheader = SPKCOMPRESS_ZLIB;
1999
 
2000
	// get the script values
2001
	this->UpdateSigned(true);
2002
	CyString values = this->CreateValuesLine();
2003
 
2004
	// compress the values
2005
	int valueUncomprLen = (int)values.Length();
2006
	unsigned long valueComprLen = 0;
2007
	unsigned char *valueCompr = NULL;
2008
	bool compressed = false;
2009
	if ( valueheader == SPKCOMPRESS_ZLIB )
2010
	{
2011
		valueComprLen = valueUncomprLen;
2012
		if ( valueComprLen < 100 )
2013
			valueComprLen = 200;
2014
		else if ( valueComprLen < 1000 )
2015
			valueComprLen *= 2;
2016
 
2017
		valueCompr = (unsigned char *)calloc((unsigned int)valueComprLen, 1);
2018
		int err = compress ( (unsigned char *)valueCompr, &valueComprLen, (const unsigned char *)values.c_str(), (unsigned long)values.Length(), 0 );
2019
		if ( err == Z_OK )
2020
			compressed = true;
2021
	}
2022
 
2023
	if ( !compressed )
2024
	{
2025
		valueComprLen = valueUncomprLen;
2026
		valueCompr = (unsigned char *)calloc((unsigned int)valueComprLen, 1);
2027
		memcpy ( valueCompr, values.c_str(), valueComprLen );
2028
		valueheader = SPKCOMPRESS_NONE;
2029
	}
2030
 
2031
	// write the main header to the file
52 cycrow 2032
	if ( !this->WriteHeader(file, valueheader, valueComprLen) )	return false;
6 cycrow 2033
 
2034
	// write the compressed data to file
52 cycrow 2035
	file.put(static_cast<unsigned char>(valueUncomprLen >> 24));
2036
	file.put(static_cast<unsigned char>(valueUncomprLen >> 16));
2037
	file.put(static_cast<unsigned char>(valueUncomprLen >> 8));
2038
	file.put(static_cast<unsigned char>(valueUncomprLen));
2039
	file.write(valueCompr, valueComprLen);
6 cycrow 2040
	free ( valueCompr );
2041
 
2042
	// now compress the files header
2043
	// create the files values
2044
	CyString files = CreateFilesLine ( true, progress );
2045
 
2046
	// compress the files values
2047
	long fileUncomprLen = (long)files.Length(), fileComprLen = fileUncomprLen;
2048
	unsigned char *fileCompr = NULL;
2049
 
2050
	compressed = false;
2051
	if ( fileUncomprLen )
2052
	{
2053
		if ( fileheader == SPKCOMPRESS_ZLIB )
2054
		{
2055
			if ( fileComprLen < 100 )
2056
				fileComprLen = 200;
2057
			else if ( fileComprLen < 1000 )
2058
				fileComprLen *= 2;
2059
			fileCompr = (unsigned char *)calloc((unsigned int)fileComprLen, 1);
2060
			int err = compress ( (unsigned char *)fileCompr, (unsigned long *)&fileComprLen, (const unsigned char *)files.c_str(), (unsigned long)files.Length(), 0 );
2061
			if ( err == Z_OK )
2062
				compressed = true;
2063
		}
2064
	}
2065
 
2066
	// if unable to compress, store it as plain text
2067
	if ( !compressed )
2068
	{
2069
		fileComprLen = fileUncomprLen;
2070
		fileCompr = (unsigned char *)calloc((unsigned int)fileComprLen, 1);
2071
		memcpy ( fileCompr, files.c_str(), fileComprLen );
2072
		fileheader = SPKCOMPRESS_NONE;
2073
	}
2074
 
2075
	// now write the file header
2076
	m_SHeader2.lSize = fileComprLen;
52 cycrow 2077
	file.write("FileHeader;%d;%ld;%ld;%d;%d\n", m_SHeader2.iNumFiles, m_SHeader2.lSize, m_SHeader2.lFullSize, fileheader, m_SHeader2.iDataCompression);
6 cycrow 2078
 
52 cycrow 2079
	file.put(static_cast<unsigned char>(fileUncomprLen >> 24));
2080
	file.put(static_cast<unsigned char>(fileUncomprLen >> 16));
2081
	file.put(static_cast<unsigned char>(fileUncomprLen >> 8));
2082
	file.put(static_cast<unsigned char>(fileUncomprLen));
2083
	file.write(fileCompr, fileComprLen);
6 cycrow 2084
	free ( fileCompr );
2085
 
2086
	if ( progress )
2087
	{
2088
		progress->UpdateStatus(STATUS_WRITE);
2089
		progress->SetDone(0);
2090
		long max = 0;
88 cycrow 2091
		for ( CListNode<C_File> *file = m_lFiles.Front(); file; file = file->next() )
2092
			max += file->Data()->GetDataSize();
6 cycrow 2093
		if ( m_pIconFile )
2094
			max += m_pIconFile->GetDataSize();
2095
		progress->SetMax(max);
2096
	}
2097
 
2098
	// now finally, write all the file data
52 cycrow 2099
	if ( m_pIconFile ) {
2100
		if ( progress )	progress->UpdateFile(m_pIconFile);
2101
		file.writeSize(m_pIconFile->GetUncompressedDataSize());
2102
		file.write(m_pIconFile->GetData(), m_pIconFile->GetDataSize());
2103
		if ( progress )	progress->IncDone(m_pIconFile->GetDataSize());
6 cycrow 2104
	}
2105
 
2106
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2107
	{
52 cycrow 2108
		C_File *f = node->Data();
2109
		if ( progress )	progress->UpdateFile(f);
2110
		file.writeSize(f->GetUncompressedDataSize());
2111
		unsigned char *data = f->GetData();
2112
		size_t remaining = f->GetDataSize();
2113
		while ( remaining )	{
6 cycrow 2114
			size_t writeSize = WRITECHUNK;
52 cycrow 2115
			if ( writeSize > remaining ) writeSize = remaining;
6 cycrow 2116
 
52 cycrow 2117
			if ( file.write(data, writeSize) ) {
2118
				data += writeSize;
2119
				remaining -= writeSize;
2120
			}
6 cycrow 2121
 
52 cycrow 2122
			if ( progress ) progress->IncDone((int)writeSize);
6 cycrow 2123
		}
2124
	}
2125
 
50 cycrow 2126
	_changed();
6 cycrow 2127
 
2128
	return true;
2129
}
2130
 
2131
 
131 cycrow 2132
bool CBaseFile::ExtractFile(C_File *file, CyString dir, bool includedir, CProgressInfo *progress)
6 cycrow 2133
{
131 cycrow 2134
	return extractFile(file, dir.ToString(), includedir, progress);
2135
}
2136
bool CBaseFile::extractFile(C_File *file, const Utils::String &dir, bool includedir, CProgressInfo *progress)
2137
{
2138
	if (ReadFileToMemory(file))
6 cycrow 2139
	{
2140
		// now finally, uncompress the file
2141
		long len = 0;
131 cycrow 2142
		unsigned char *data = file->UncompressData(&len, progress);
2143
		if (!data)
6 cycrow 2144
		{
2145
			// attempt a file decompress
131 cycrow 2146
			if (file->GetCompressionType() == SPKCOMPRESS_7ZIP)
6 cycrow 2147
			{
131 cycrow 2148
				if (file->UncompressToFile(dir, this, includedir, progress))
6 cycrow 2149
					return true;
2150
			}
2151
			return false;
2152
		}
2153
 
131 cycrow 2154
		if (!file->writeToDir(dir, this, includedir, Utils::String::Null(), data, len))
6 cycrow 2155
			return false;
2156
 
2157
		return true;
2158
 
2159
	}
2160
	else
2161
		return false;
2162
}
131 cycrow 2163
bool CBaseFile::extractFile(C_File *file, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir, CProgressInfo *progress)
2164
{
2165
	if (ReadFileToMemory(file))
2166
	{
2167
		CDirIO Dir(dir);
2168
		Utils::String addonDir;
2169
		if (file->isFileInAddon())
2170
		{
2171
			int addonGame = file->getForSingleGame();
2172
			if (!addonGame) addonGame = game;
6 cycrow 2173
 
131 cycrow 2174
			if (addonGame > 0)
2175
				addonDir = gameAddons.findString(Utils::String::Number(addonGame));
2176
		}
2177
 
2178
		if (!addonDir.empty())
2179
			Dir.cd(addonDir);
2180
 
2181
		// create directory first
2182
		Dir.create(file->getDirectory(this));
2183
 
2184
		// now finally, uncompress the file
2185
		long len = 0;
2186
		unsigned char *data = file->UncompressData(&len, progress);
2187
		if (!data)
2188
		{
2189
			// attempt a file decompress
2190
			if (file->GetCompressionType() == SPKCOMPRESS_7ZIP)
2191
			{
2192
				if (file->UncompressToFile(Dir.dir(), this, includedir, progress))
2193
					return true;
2194
			}
2195
			return false;
2196
		}
2197
 
2198
		if (!file->writeToDir(Dir.dir(), this, includedir, Utils::String::Null(), data, len))
2199
			return false;
2200
 
2201
		return true;
2202
 
2203
	}
2204
	else
2205
		return false;
2206
}
2207
 
2208
bool CBaseFile::ExtractFile(int filenum, CyString dir, bool includedir, CProgressInfo *progress)
6 cycrow 2209
{
2210
	// invalid valus
131 cycrow 2211
	if (filenum < 0)
6 cycrow 2212
		return false;
2213
	// out of range
131 cycrow 2214
	if (filenum > m_lFiles.size())
6 cycrow 2215
		return false;
2216
 
2217
	// get the file pointer
131 cycrow 2218
	C_File *file = m_lFiles.Get(filenum);
2219
	return extractFile(file, dir.ToString(), includedir, progress);
6 cycrow 2220
}
2221
 
131 cycrow 2222
bool CBaseFile::extractFile(int filenum, const Utils::String &dir, unsigned int game, const Utils::CStringList &gameAddons, bool includedir, CProgressInfo *progress)
2223
{
2224
	// invalid valus
2225
	if (filenum < 0)
2226
		return false;
2227
	// out of range
2228
	if (filenum > m_lFiles.size())
2229
		return false;
2230
 
2231
	// get the file pointer
2232
	C_File *file = m_lFiles.Get(filenum);
2233
	return extractFile(file, dir, game, gameAddons, includedir, progress);
2234
}
2235
 
2236
bool CBaseFile::extractFile(int filenum, const Utils::String &dir, bool includedir, CProgressInfo *progress)
2237
{
2238
	// invalid valus
2239
	if (filenum < 0)
2240
		return false;
2241
	// out of range
2242
	if (filenum > m_lFiles.size())
2243
		return false;
2244
 
2245
	// get the file pointer
2246
	C_File *file = m_lFiles.Get(filenum);
2247
	return ExtractFile(file, dir, includedir, progress);
2248
}
2249
 
129 cycrow 2250
bool CBaseFile::extractAll(const Utils::String &dir, int game, const Utils::CStringList &gameAddons, bool includedir, CProgressInfo *progress)
6 cycrow 2251
{
2252
	// no file to read from
129 cycrow 2253
	if (this->filename().empty())
6 cycrow 2254
		return false;
2255
 
2256
	// now open the file
58 cycrow 2257
	CFileIO *File = _startRead();
129 cycrow 2258
	if (!File) return false;
6 cycrow 2259
 
2260
	// now were in the file section
2261
	// skip past each one
129 cycrow 2262
	if (m_pIconFile) File->seek(4 + m_pIconFile->GetDataSize());
6 cycrow 2263
 
134 cycrow 2264
	for (CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next()) 
2265
	{
6 cycrow 2266
		C_File *fit = node->Data();
2267
 
134 cycrow 2268
		if ((!fit->GetDataSize()) || (!fit->GetData())) 
2269
		{
2270
			if (!fit->readFromFile(*File, fit->GetDataSize())) 
2271
				return false;
6 cycrow 2272
		}
2273
		else
58 cycrow 2274
			File->seek(fit->GetDataSize());
6 cycrow 2275
 
129 cycrow 2276
		if (game && !fit->isForGame(game))
2277
			continue;
2278
 
134 cycrow 2279
		if (progress)	
2280
			progress->UpdateFile(fit);
2281
 
129 cycrow 2282
		CDirIO Dir(dir);
2283
		Utils::String addonDir;
2284
		if (fit->isFileInAddon())
2285
		{
2286
			int addonGame = fit->getForSingleGame();
2287
			if (!addonGame) addonGame = game;
2288
 
2289
			if (addonGame > 0)
2290
				addonDir = gameAddons.findString(Utils::String::Number(addonGame));
6 cycrow 2291
		}
2292
 
129 cycrow 2293
		if (!addonDir.empty())
2294
			Dir.cd(addonDir);
2295
 
6 cycrow 2296
		// create directory first
129 cycrow 2297
		Dir.create(fit->getDirectory(this));
6 cycrow 2298
 
2299
		long size = 0;
129 cycrow 2300
		unsigned char *data = fit->UncompressData(&size, progress);
2301
		if ((!data) && (fit->GetCompressionType() == SPKCOMPRESS_7ZIP)) {
2302
			if (!fit->UncompressToFile(Dir.dir(), this, includedir, progress)) return false;
6 cycrow 2303
		}
129 cycrow 2304
		else if ((!data) || (!fit->writeToDir(Dir.dir(), this, includedir, Utils::String::Null(), data, size))) return false;
6 cycrow 2305
	}
2306
 
58 cycrow 2307
	delete File;
6 cycrow 2308
	return true;
2309
}
2310
 
2311
 
2312
 
2313
bool CBaseFile::UpdateSigned (bool updateFiles)
2314
{
2315
	m_bSigned = true;
2316
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2317
	{
2318
		C_File *file = node->Data();
2319
		if ( updateFiles )
2320
			file->UpdateSigned();
2321
		// extra, text, soundtrack, readmes and screen files do not require a modified game directly
2322
		if ( (file->GetFileType() == FILETYPE_EXTRA) || (file->GetFileType() == FILETYPE_TEXT) || (file->GetFileType() == FILETYPE_SOUND) || (file->GetFileType() == FILETYPE_SCREEN) || (file->GetFileType() == FILETYPE_README) )
2323
			continue;
2324
		// mods and maps always need modified game
2325
		else if ( (file->GetFileType() == FILETYPE_MOD) || (file->GetFileType() == FILETYPE_MAP) )
2326
		{
2327
			m_bSigned = false;
2328
			break;
2329
		}
2330
 
2331
		// else should be a script file, script or uninstall type
2332
		// all scripts must be signed, if any are not, then no signed status
2333
		if ( !file->IsSigned () )
2334
		{
2335
			m_bSigned = false;
2336
			break;
2337
		}
2338
	}
2339
 
2340
	return m_bSigned;
2341
}
2342
 
2343
 
46 cycrow 2344
bool CBaseFile::IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2345
{
2346
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2347
	{
2348
		SNeededLibrary *l = node->Data();
2349
		if ( l->sName.Compare(scriptName) && l->sAuthor.Compare(author) )
2350
			return true;
2351
	}
2352
 
2353
	return false;
2354
}
2355
 
46 cycrow 2356
SNeededLibrary *CBaseFile::FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2357
{
2358
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2359
	{
2360
		SNeededLibrary *l = node->Data();
2361
		if ( l->sName.Compare(scriptName) && l->sAuthor.Compare(author) )
2362
			return l;
2363
	}
2364
 
2365
	return NULL;
2366
}
2367
 
2368
void CBaseFile::RemoveFakePatchOrder(CyString scriptName, CyString author)
2369
{
2370
	RemoveFakePatchOrder(true, scriptName, author);
2371
	RemoveFakePatchOrder(false, scriptName, author);
2372
}
2373
void CBaseFile::RemoveFakePatchOrder(bool after, CyString scriptName, CyString author)
2374
{
2375
	CyStringList *list;
2376
	if ( after )
2377
		list = &m_lFakePatchAfter;
2378
	else
2379
		list = &m_lFakePatchBefore;
2380
 
2381
	for ( SStringList *str = list->Head(); str; str = str->next )
2382
	{
2383
		// already added
2384
		if ( str->str.Compare(scriptName) && str->data.Compare(author) )
2385
			str->remove = true;
2386
	}
2387
 
2388
	list->RemoveMarked();
2389
}
2390
 
2391
void CBaseFile::AddFakePatchOrder(bool after, CyString scriptName, CyString author)
2392
{
2393
	CyStringList *list;
2394
	if ( after )
2395
		list = &m_lFakePatchAfter;
2396
	else
2397
		list = &m_lFakePatchBefore;
2398
 
2399
	// check if the package already exists
2400
	for ( SStringList *str = list->Head(); str; str = str->next )
2401
	{
2402
		// already added
2403
		if ( str->str.Compare(scriptName) && str->data.Compare(author) )
2404
			return;
2405
	}
2406
 
2407
	// cant have it on both list
2408
	RemoveFakePatchOrder(!after, scriptName, author);
2409
 
2410
	list->PushBack(scriptName, author);
2411
}
46 cycrow 2412
void CBaseFile::AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion)
6 cycrow 2413
{
2414
	SNeededLibrary *l = this->FindPackageNeeded(scriptName, author);
2415
	if ( !l )
2416
	{
2417
		l = new SNeededLibrary;
2418
		l->sName = scriptName;
2419
		l->sAuthor = author;
2420
		m_lNeededLibrarys.push_back(l);
2421
	}
2422
	l->sMinVersion = minVersion;
2423
}
2424
 
46 cycrow 2425
void CBaseFile::RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2426
{
2427
	SNeededLibrary *l = this->FindPackageNeeded(scriptName, author);
2428
	if ( l )
2429
	{
2430
		m_lNeededLibrarys.remove(l);
2431
		delete l;
2432
	}
2433
}
2434
 
2435
void CBaseFile::ClearNeededPackages()
2436
{
2437
	SNeededLibrary *ns = this->FindPackageNeeded("<package>", "<author>");
46 cycrow 2438
	Utils::String version;
2439
	if ( ns ) version = ns->sMinVersion;
2440
 
6 cycrow 2441
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2442
		node->DeleteData();
2443
	m_lNeededLibrarys.clear();
2444
 
46 cycrow 2445
	if ( !version.empty() )	this->AddNeededLibrary("<package>", "<author>", version);
6 cycrow 2446
}
2447
 
2448
 
127 cycrow 2449
bool CBaseFile::GeneratePackagerScript(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons, bool datafile)
6 cycrow 2450
{
126 cycrow 2451
	list->pushBack("#");
2452
	list->pushBack("# Packager Script");
2453
	list->pushBack("# -- Generated by SPK Libraries V" + Utils::String::FromFloat(GetLibraryVersion(), 2) + " --");
2454
	list->pushBack("#");
2455
	list->pushBack("");
6 cycrow 2456
	if ( !datafile )
2457
	{
126 cycrow 2458
		list->pushBack("# Variable for your game directory, where to get files from");
2459
		list->pushBack("# $PATH variable is used to get the current path");
2460
		list->pushBack("Variable: $GAMEDIR $PATH");
2461
		list->pushBack("");
6 cycrow 2462
	}
126 cycrow 2463
	list->pushBack("# The name of the script");
2464
	list->pushBack("Name: " + this->name());
2465
	list->pushBack("");
2466
	list->pushBack("# The author of the script, ie, you");
2467
	list->pushBack("Author: " + this->author());
2468
	list->pushBack("");
2469
	list->pushBack("# The creation data, when it was created");
6 cycrow 2470
	if ( datafile )
126 cycrow 2471
		list->pushBack("Date: " + this->creationDate());
50 cycrow 2472
	else {
126 cycrow 2473
		list->pushBack("# $DATE variable is used to get the current date");
2474
		list->pushBack("Date: $DATE");
6 cycrow 2475
	}
126 cycrow 2476
	list->pushBack("");
2477
	list->pushBack("# The version of script");
6 cycrow 2478
	if ( datafile )
126 cycrow 2479
		list->pushBack("Version: " + this->version());
6 cycrow 2480
	else
2481
	{
126 cycrow 2482
		list->pushBack("# $ASK variable is used to get an input when creating");
2483
		list->pushBack("Version: $ASK");
6 cycrow 2484
	}
126 cycrow 2485
	list->pushBack("");
6 cycrow 2486
 
2487
	if ( !m_lGames.empty() ) {
126 cycrow 2488
		list->pushBack("# The game version the script is for <game> <version> (can have multiple games)");
6 cycrow 2489
		for ( SGameCompat *g = m_lGames.First(); g; g = m_lGames.Next() ) {
126 cycrow 2490
			if (game > 0 && g->iGame != game)
2491
				continue;
2492
 
13 cycrow 2493
			Utils::String game = CBaseFile::ConvertGameToString(g->iGame);
6 cycrow 2494
 
46 cycrow 2495
			if ( !g->sVersion.empty() )
6 cycrow 2496
			{
2497
				game += " ";
46 cycrow 2498
				game += g->sVersion;
6 cycrow 2499
			}
2500
			else
2501
			{
2502
				game += " ";
2503
				game += (long)g->iVersion;
2504
			}
2505
 
126 cycrow 2506
			list->pushBack("Game: " + game);
6 cycrow 2507
		}
2508
 
126 cycrow 2509
		list->pushBack("");
6 cycrow 2510
	}
2511
 
48 cycrow 2512
	if ( !this->description().empty() ) {
126 cycrow 2513
		list->pushBack("# The description of the script, displays when installing");
2514
		list->pushBack("Description: " + this->description());
2515
		list->pushBack("");
6 cycrow 2516
	}
2517
 
49 cycrow 2518
	if ( !this->webSite().empty() ) {
126 cycrow 2519
		list->pushBack("# A link to the website for the script, ie for an online help page");
2520
		list->pushBack("WebSite: " + this->webSite());
2521
		list->pushBack("");
6 cycrow 2522
	}
2523
 
49 cycrow 2524
	if ( !this->forumLink().empty() ) {
126 cycrow 2525
		list->pushBack("# A direct link to the thread in the egosoft forum");
2526
		list->pushBack("ForumLink: " + this->forumLink());
2527
		list->pushBack("");
6 cycrow 2528
	}
2529
 
49 cycrow 2530
	if ( !this->webAddress().empty() ) {
126 cycrow 2531
		list->pushBack("# A link to the address for the update file");
2532
		list->pushBack("WebAddress: " + this->webAddress());
2533
		list->pushBack("");
6 cycrow 2534
	}
2535
 
49 cycrow 2536
	if ( !this->email().empty() ) {
126 cycrow 2537
		list->pushBack("# The email address of the author, to allow users to contract if needed");
2538
		list->pushBack("Email: " + this->email());
2539
		list->pushBack("");
6 cycrow 2540
	}
2541
 
2542
	if ( m_lMirrors.Count() )
2543
	{
126 cycrow 2544
		list->pushBack("# A link to the mirror address for the update file, can have many of these");
6 cycrow 2545
		for ( SStringList *node = m_lMirrors.Head(); node; node = node->next )
126 cycrow 2546
			list->pushBack(Utils::String("WebMirror: ") + node->str.ToString());
2547
		list->pushBack("");
6 cycrow 2548
	}
2549
 
2550
	if ( m_bAutoGenerateUpdateFile )
2551
	{
126 cycrow 2552
		list->pushBack("# Auto generate the package update file when created");
2553
		list->pushBack("GenerateUpdateFile");
6 cycrow 2554
	}
2555
 
2556
	if ( m_lNeededLibrarys.size() )
2557
	{
126 cycrow 2558
		list->pushBack("# Needed Library dependacies, require these to be installed");
6 cycrow 2559
		for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
126 cycrow 2560
			list->pushBack("Depend: " + node->Data()->sName + "|" + node->Data()->sMinVersion + "|" + node->Data()->sAuthor);
6 cycrow 2561
 
126 cycrow 2562
		list->pushBack("");
6 cycrow 2563
	}
2564
 
46 cycrow 2565
	if ( !_noRatings() )
6 cycrow 2566
	{
126 cycrow 2567
		list->pushBack("# Ratings Values, 0 to 5, <ease> <changing> <recommended>");
2568
		list->pushBack(Utils::String("Ratings: ") + (long)easeOfUse() + " " + (long)gameChanging() + " " + (long)recommended());
2569
		list->pushBack("");
6 cycrow 2570
	}
2571
 
2572
	if ( m_lNames.size() )
2573
	{
126 cycrow 2574
		list->pushBack("# Package names, uses different names for different languages");
6 cycrow 2575
		for ( CListNode<SNames> *node = m_lNames.Front(); node; node = node->next() )
126 cycrow 2576
			list->pushBack(Utils::String("ScriptName: ") + (long)node->Data()->iLanguage + " " + node->Data()->sName);
2577
		list->pushBack("");
6 cycrow 2578
	}
2579
 
46 cycrow 2580
	for ( int j = 0; j < 2; j++ ) {
2581
		Utils::String installText = (j == 0) ? "Install" : "Uninstall";
2582
		const CInstallText *pText = (j == 0) ? this->installText() : this->uninstallText();
2583
		if ( pText->any() )
6 cycrow 2584
		{
126 cycrow 2585
			list->pushBack("# " + installText + " Texts, display text before and/or after " + installText + "ing to inform the use of special conditions");
46 cycrow 2586
			for ( unsigned int i = 0; i < pText->count(); i++ ) {
2587
				long iLang = pText->language(i);
126 cycrow 2588
				if ( !pText->getBefore(iLang).empty() )	list->pushBack(installText + "Before: " + iLang + " " + pText->getBefore(iLang));
2589
				if ( !pText->getAfter(iLang).empty()  )	list->pushBack(installText + "After: " + iLang + " " + pText->getAfter(iLang));
46 cycrow 2590
			}
126 cycrow 2591
			list->pushBack("");
6 cycrow 2592
		}
2593
	}
2594
 
126 cycrow 2595
	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 2596
	switch ( this->pluginType() )
6 cycrow 2597
	{
2598
		case PLUGIN_NORMAL:
126 cycrow 2599
			list->pushBack("PluginType: Normal");
6 cycrow 2600
			break;
2601
		case PLUGIN_STABLE:
126 cycrow 2602
			list->pushBack("PluginType: Stable");
6 cycrow 2603
			break;
2604
		case PLUGIN_EXPERIMENTAL:
126 cycrow 2605
			list->pushBack("PluginType: Experimental");
6 cycrow 2606
			break;
2607
		case PLUGIN_CHEAT:
126 cycrow 2608
			list->pushBack("PluginType: Cheat");
6 cycrow 2609
			break;
2610
		case PLUGIN_MOD:
126 cycrow 2611
			list->pushBack("PluginType: Mod");
6 cycrow 2612
			break;
2613
	}
126 cycrow 2614
	list->pushBack("");
6 cycrow 2615
 
2616
	return true;
2617
}
2618
 
127 cycrow 2619
bool CBaseFile::GeneratePackagerScriptFile(bool wildcard, Utils::CStringList *list, int game, const Utils::CStringList &gameAddons)
6 cycrow 2620
{
2621
	// now do files and wildcards
126 cycrow 2622
	Utils::CStringList files;
6 cycrow 2623
	for ( CListNode<C_File> *f = m_lFiles.Front(); f; f = f->next() )
2624
	{
127 cycrow 2625
		if (game && !(f->Data()->game() & 1 << game))
126 cycrow 2626
			continue;
6 cycrow 2627
 
126 cycrow 2628
		Utils::String name = "$GAMEDIR/";
2629
 
127 cycrow 2630
		// addon directory?
2631
		unsigned int checkGame = f->Data()->game() & ~(1 << 31);
2632
		unsigned int foundGame = 0;
2633
		for (int i = 0; i < 31; ++i)
2634
		{
2635
			if (checkGame == 1 << i)
2636
			{
2637
				foundGame = i;
2638
				break;
2639
			}
2640
		}
2641
 
2642
		if (foundGame)
2643
		{
2644
			Utils::String str = gameAddons.findString(Utils::String::Number(foundGame));
2645
			if(!str.empty())
2646
				name += str + "/";
2647
		}
2648
 
6 cycrow 2649
		bool done = false;
2650
		if ( wildcard )
2651
		{
131 cycrow 2652
			Utils::String base = f->Data()->baseName();
2653
			if ( f->Data()->fileType() == FILETYPE_SCRIPT )
6 cycrow 2654
			{
126 cycrow 2655
				if ( base.token(".", 1).Compare("plugin") || base.token(".", 1).Compare("lib") )
6 cycrow 2656
				{
131 cycrow 2657
					name += f->Data()->getDirectory(this) + "/" + base.tokens(".", 1, 2) + ".*";
6 cycrow 2658
					done = true;
2659
				}
126 cycrow 2660
				else if ( base.token(".", 1).Compare("al") && !base.token(".", 2).Compare("plugin") )
6 cycrow 2661
				{
131 cycrow 2662
					name += f->Data()->getDirectory(this) + "/" + base.tokens(".", 1, 2) + ".*";
6 cycrow 2663
					done = true;
2664
				}
2665
			}
131 cycrow 2666
			else if ( f->Data()->fileType() == FILETYPE_TEXT )
6 cycrow 2667
			{
126 cycrow 2668
				if ( base.contains("-L") )
6 cycrow 2669
				{
131 cycrow 2670
					name += f->Data()->getDirectory(this) + "/" + base.token("-L", 1) + "-L*";
6 cycrow 2671
					done = true;
2672
				}
2673
				else
2674
				{
131 cycrow 2675
					name += f->Data()->getDirectory(this) + "/*" + base.right(4) + ".*";
6 cycrow 2676
					done = true;
2677
				}
2678
			}
2679
		}
2680
 
2681
		if ( !done )
131 cycrow 2682
			name += f->Data()->getNameDirectory(this);
6 cycrow 2683
 
131 cycrow 2684
		if ( !f->Data()->dir().empty() )
6 cycrow 2685
		{
2686
			name += "|";
131 cycrow 2687
			name += f->Data()->dir();
6 cycrow 2688
		}
127 cycrow 2689
		Utils::String s = "GAME ";
2690
		if (!f->Data()->game() || f->Data()->game() == GAME_ALLNEW)
2691
			s += CBaseFile::ConvertGameToString(f->Data()->game());
2692
		else
2693
		{
2694
			bool first = true;
2695
			for (int i = 0; i < 31; ++i) 
2696
			{
2697
				if (f->Data()->game() & 1 << i)
2698
				{
2699
					if (first)
2700
						first = false;
2701
					else
2702
						s += "|";
2703
					s += CBaseFile::ConvertGameToString(i);
2704
				}
2705
			}
2706
		}
2707
 
2708
		s += " " + name;
126 cycrow 2709
		if(!files.contains(s))
2710
			files.pushBack(s, f->Data()->GetFileTypeString().ToString());
6 cycrow 2711
	}
2712
 
2713
 
126 cycrow 2714
	if ( !files.empty() )
6 cycrow 2715
	{
126 cycrow 2716
		list->pushBack("# Files List, all the files to add, can include wild cards");
2717
		for(auto itr = files.begin(); itr != files.end(); itr++)
2718
			list->pushBack((*itr)->data + ": " + (*itr)->str);
2719
		list->pushBack("");
6 cycrow 2720
	}
2721
 
2722
	return true;
2723
}
2724
 
50 cycrow 2725
Utils::String CBaseFile::GetAutosaveName()
6 cycrow 2726
{
50 cycrow 2727
	return this->name() + "-V" + this->version() + "-" + this->creationDate().findReplace("/", ".");
6 cycrow 2728
}
2729
 
2730
bool CBaseFile::CheckGameCompatability(int game)
2731
{
2732
	if ( m_lGames.empty() )
2733
		return true; // no game compatability added, assume its ok for all
2734
 
2735
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2736
		if ( node->Data()->iGame == 0 || node->Data()->iGame == game )
2737
			return true;
2738
	}
2739
	return false;
2740
}
2741
 
2742
bool CBaseFile::CheckGameVersionCompatability(int game, CyString sVersion, int iVersion)
2743
{
2744
	if ( m_lGames.empty() )
2745
		return true; // no game compatability added, assume its ok for all
2746
 
2747
	bool foundAll = false;
2748
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2749
		if ( node->Data()->iGame == 0 )
2750
			foundAll = true;
2751
		else if ( node->Data()->iGame == game ) { // now check the version
46 cycrow 2752
			if ( node->Data()->sVersion.empty() ) {
2753
				if ( CyString(node->Data()->sVersion).CompareVersion(sVersion) == COMPARE_OLDER )
6 cycrow 2754
					return false;
2755
				return true;
2756
			}
2757
			else {
2758
				if ( node->Data()->iVersion > iVersion )
2759
					return false;
2760
				return true;
2761
			}
2762
		}
2763
	}
2764
	return foundAll;
2765
}
2766
 
2767
bool CBaseFile::RemoveGameCompatability(int game)
2768
{
2769
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2770
		if ( node->Data()->iGame == game ) {
2771
			m_lGames.remove(node);
50 cycrow 2772
			_changed();
6 cycrow 2773
			return true;
2774
		}
2775
	}
2776
 
2777
	return false;
2778
}
2779
 
2780
SGameCompat *CBaseFile::GetGameCompatability(int game)
2781
{
2782
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2783
		if ( node->Data()->iGame == game ) {
2784
			return node->Data();
2785
		}
2786
	}
2787
 
2788
	return NULL;
2789
}
2790
 
46 cycrow 2791
void CBaseFile::AddGameCompatability(int game, const Utils::String &version)
6 cycrow 2792
{
2793
	// first check if we already have it on the list
2794
	SGameCompat *Found = this->GetGameCompatability(game);
2795
	if ( !Found ) {
2796
		Found = new SGameCompat;
2797
		m_lGames.push_back(Found);
2798
	}
2799
 
2800
	Found->iGame = game;
2801
	Found->iVersion = -1;
46 cycrow 2802
	Found->sVersion = "";
6 cycrow 2803
 
46 cycrow 2804
	if ( version.isin(".") || !version.isNumber() )
6 cycrow 2805
		Found->sVersion = version;
2806
	else
46 cycrow 2807
		Found->iVersion = version;
50 cycrow 2808
	_changed();
6 cycrow 2809
}
2810
 
131 cycrow 2811
Utils::String CBaseFile::_replaceFilename(const Utils::String &fname)
2812
{
2813
	Utils::String filename = fname;
2814
	Utils::String cdate = this->creationDate().findReplace("/", ".").remove(' ');
2815
	if (filename.isin("$AUTOSAVE"))
2816
	{
2817
		if (this->GetType() == TYPE_XSP)
2818
			filename = filename.findReplace("$AUTOSAVE", "$NAME-V$VERSION-$CDATE.xsp");
2819
		else
2820
			filename = filename.findReplace("$AUTOSAVE", "$NAME-V$VERSION-$CDATE.spk");
2821
	}
2822
	filename = filename.findReplace("$NAME", this->name().remove(' '));
2823
	filename = filename.findReplace("$AUTHOR", this->author().remove(' '));
2824
	filename = filename.findReplace("$DATE", cdate);
2825
	filename = filename.findReplace("$CDATE", cdate);
2826
	filename = filename.findReplace("$VERSION", this->version());
2827
 
2828
	return filename;
2829
}
134 cycrow 2830
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 2831
{
134 cycrow 2832
	if (sFirst.Compare("Name"))					this->setName(sRest);
2833
	else if (sFirst.Compare("Author"))			this->setAuthor(sRest);
2834
	else if (sFirst.Compare("ScriptName"))
14 cycrow 2835
		AddLanguageName(ParseLanguage(sRest.token(" ", 1)), sRest.tokens(" ", 2));
134 cycrow 2836
	else if (sFirst.Compare("UninstallBefore"))	this->addUninstallText(ParseLanguage(sRest.token(" ", 1)), true, sRest.tokens(" ", 2));
2837
	else if (sFirst.Compare("UninstallAfter"))	this->addUninstallText(ParseLanguage(sRest.token(" ", 1)), false, sRest.tokens(" ", 2));
2838
	else if (sFirst.Compare("InstallBefore"))		this->addInstallText(ParseLanguage(sRest.token(" ", 1)), true, sRest.tokens(" ", 2));
2839
	else if (sFirst.Compare("InstallAfter"))		this->addInstallText(ParseLanguage(sRest.token(" ", 1)), false, sRest.tokens(" ", 2));
2840
	else if (sFirst.Compare("Date"))				this->setCreationDate(sRest);
2841
	else if (sFirst.Compare("Version"))			this->setVersion(sRest);
2842
	else if (sFirst.Compare("GameVersion"))
14 cycrow 2843
		this->AddGameCompatability(-1, sRest);
134 cycrow 2844
	else if (sFirst.Compare("PluginType")) {
2845
		if (sRest.isNumber())						this->setPluginType(sRest);
2846
		else if (sRest.Compare("Normal"))			this->setPluginType(PLUGIN_NORMAL);
2847
		else if (sRest.Compare("Stable"))			this->setPluginType(PLUGIN_STABLE);
2848
		else if (sRest.Compare("Experimental"))	this->setPluginType(PLUGIN_EXPERIMENTAL);
2849
		else if (sRest.Compare("Cheat"))			this->setPluginType(PLUGIN_CHEAT);
2850
		else if (sRest.Compare("Mod"))			this->setPluginType(PLUGIN_MOD);
6 cycrow 2851
	}
2852
	// new version
134 cycrow 2853
	else if (sFirst.Compare("GenerateUpdateFile"))
6 cycrow 2854
		m_bAutoGenerateUpdateFile = true;
134 cycrow 2855
	else if (sFirst.Compare("Game"))
6 cycrow 2856
	{
14 cycrow 2857
		Utils::String sGame = sRest.token(" ", 1);
2858
		this->AddGameCompatability(CBaseFile::GetGameFromString(sGame), sRest.token(" ", 2));
6 cycrow 2859
	}
134 cycrow 2860
	else if (sFirst.Compare("Description"))		this->setDescription(sRest);
2861
	else if (sFirst.Compare("AutoSave") || sFirst.Compare("AutoExport") || sFirst.Compare("AutoRarExport") || sFirst.Compare("AutoZipExport"))
6 cycrow 2862
	{
131 cycrow 2863
		Utils::String filename = _replaceFilename(sRest);
6 cycrow 2864
 
134 cycrow 2865
		if (sFirst.Compare("AutoZipExport") || sFirst.Compare("AutoExport"))
131 cycrow 2866
			this->setExportFilename(CFileIO(filename).changeFileExtension("zip"));
134 cycrow 2867
		else if (sFirst.Compare("AutoRarExport"))
131 cycrow 2868
			this->setExportFilename(CFileIO(filename).changeFileExtension("rar"));
6 cycrow 2869
		else
50 cycrow 2870
			this->setFilename(filename);
6 cycrow 2871
	}
134 cycrow 2872
	else if (sFirst.Compare("WebSite"))		this->setWebSite(sRest);
2873
	else if (sFirst.Compare("ForumLink") || sFirst.Compare("Forum")) this->setForumLink(sRest);
2874
	else if (sFirst.Compare("Email"))			this->setEmail(sRest);
2875
	else if (sFirst.Compare("WebAddress"))	this->setWebAddress(sRest);
2876
	else if (sFirst.Compare("WebMirror"))
14 cycrow 2877
		this->AddWebMirror(sRest);
134 cycrow 2878
	else if (sFirst.Compare("WebMirror1"))
14 cycrow 2879
		this->AddWebMirror(sRest);
134 cycrow 2880
	else if (sFirst.Compare("WebMirror2"))
14 cycrow 2881
		this->AddWebMirror(sRest);
134 cycrow 2882
	else if (sFirst.Compare("Ftp"))
14 cycrow 2883
		m_sFtpAddr = sRest;
134 cycrow 2884
	else if (sFirst.Compare("Ratings"))		_setRatings(sRest.token(" ", 1), sRest.token(" ", 2), sRest.token(" ", 3));
2885
	else if (sFirst.Compare("EaseOfUse"))		setEaseOfUse(sRest);
2886
	else if (sFirst.Compare("GameChanging"))	setGameChanging(sRest);
2887
	else if (sFirst.Compare("Recommended"))	setRecommended(sRest);
2888
	else if (sFirst.Compare("Depend"))
6 cycrow 2889
	{
46 cycrow 2890
		Utils::String version = sRest.token("|", 2);
2891
		Utils::String name = sRest.token("|", 1);
2892
		Utils::String author = sRest.tokens("|", 3);
6 cycrow 2893
 
2894
		this->AddNeededLibrary(name, author, version);
2895
	}
134 cycrow 2896
	else if (sFirst.Compare("DependPackage"))
6 cycrow 2897
	{
2898
		CPackages p;
134 cycrow 2899
		CBaseFile *spk = p.OpenPackage(sRest, 0, 0, SPKREAD_VALUES);
2900
		if (spk)
6 cycrow 2901
		{
50 cycrow 2902
			this->AddNeededLibrary(spk->name(), spk->author(), spk->version());
6 cycrow 2903
			delete spk;
2904
		}
2905
	}
130 cycrow 2906
	else if (sFirst.Compare("Icon"))
6 cycrow 2907
	{
14 cycrow 2908
		C_File *icon = new C_File(sRest.c_str());
134 cycrow 2909
		if (icon->ReadFromFile())
14 cycrow 2910
			this->SetIcon(icon, CFileIO(sRest).GetFileExtension());
6 cycrow 2911
	}
130 cycrow 2912
	else if (sFirst.Compare("CombineGameFiles"))
2913
		_bCombineFiles = sRest.Compare("true") || sRest.Compare("yes") || sRest.toInt();
134 cycrow 2914
	else if (sFirst.Compare("UpdateFile"))
2915
		this->createUpdateFile(sRest);
131 cycrow 2916
	else if (sFirst.Compare("ExportZip"))
2917
	{
2918
		Utils::String ext = "zip";
2919
		Utils::String game = sRest.word(1);
2920
		Utils::String file = _replaceFilename(CFileIO(sRest.words(2)).fullFilename());
2921
		if (game.contains("|"))
2922
		{
2923
			int max;
2924
			Utils::String *games = game.tokenise("|", &max);
2925
			if (games)
2926
			{
2927
				for (int i = 0; i < max; ++i)
2928
				{
2929
					unsigned int g = CBaseFile::GetGameFromString(games[i]);
2930
					Utils::String filename = CFileIO(file).dir() + "/" + CFileIO(file).baseName() + "_" + CBaseFile::ConvertGameToString(g) + "." + ext;
2931
					this->addAutoExport(g, filename);
2932
				}
2933
				CLEANSPLIT(games, max);
2934
			}
2935
		}
2936
		else
2937
		{
2938
			unsigned int g = CBaseFile::GetGameFromString(game);
2939
			Utils::String filename = CFileIO(file).dir() + "/" + CFileIO(file).baseName() + "_" + CBaseFile::ConvertGameToString(g) + "." + ext;
2940
			this->addAutoExport(g, filename);
2941
		}
2942
	}
2943
	else if (sFirst.Compare("Extract"))
2944
	{
2945
		Utils::String game = sRest.word(1);
2946
		Utils::String dir = CDirIO(sRest.words(2)).dir();
2947
		if (game.contains("|"))
2948
		{
2949
			int max;
2950
			Utils::String *games = game.tokenise("|", &max);
2951
			if (games)
2952
			{
2953
				for(int i = 0; i < max; ++i)
2954
					this->addAutoExtract(CBaseFile::GetGameFromString(games[i]), dir);
2955
				CLEANSPLIT(games, max);
2956
			}
2957
		}
2958
		else
2959
			this->addAutoExtract(CBaseFile::GetGameFromString(game), dir);
2960
	}
6 cycrow 2961
	else
2962
	{
14 cycrow 2963
		Utils::String checkType = sFirst;
6 cycrow 2964
		bool shared = false;
14 cycrow 2965
		if ( checkType.left(6).Compare("Shared") )
6 cycrow 2966
		{
14 cycrow 2967
			checkType = sFirst.right(-6);
6 cycrow 2968
			shared = true;
2969
		}
2970
 
2971
		// now check type name
127 cycrow 2972
		FileType filetype = GetFileTypeFromString(checkType);
2973
		if (filetype != FILETYPE_UNKNOWN)
134 cycrow 2974
			this->AddFileScript(filetype, shared, sRest, sMainGame, otherGames, gameAddons, progress);
6 cycrow 2975
		else if ( !checkType.Compare("changelog") )
2976
			return false;
2977
	}
2978
 
2979
	return true;
2980
}
2981
 
134 cycrow 2982
void CBaseFile::AddFileScript(FileType filetype, bool shared, CyString sRest, const Utils::String &sMainGame, Utils::CStringList &otherGames, Utils::CStringList &gameAddons, CProgressInfo *progress)
6 cycrow 2983
{
127 cycrow 2984
	Utils::String dir;
2985
	Utils::String rest = sRest.ToString();
2986
 
2987
	unsigned int mainGame = CBaseFile::GetGameFromString(sMainGame);
2988
	unsigned int game = 0;
2989
	if ( rest.token(" ", 1).left(4).Compare("GAME") ) {
2990
		Utils::String gameStr = rest.token(" ", 2);
2991
		if (gameStr.contains("|"))
2992
		{
2993
			int max = 0;
2994
			Utils::String *games = gameStr.tokenise("|", &max);
2995
			for (int i = 0; i < max; ++i)
2996
			{
2997
				unsigned int g = CBaseFile::GetGameFromString(games[i]);
2998
				if (g)
2999
					game |= 1 << g;
3000
			}
3001
			CLEANSPLIT(games, max);
3002
		}
3003
		else
131 cycrow 3004
		{
3005
			unsigned int g = CBaseFile::GetGameFromString(gameStr);
3006
			if (g)
3007
				game = 1 << g;
3008
		}
127 cycrow 3009
		rest = rest.tokens(" ", 3);
6 cycrow 3010
	}
127 cycrow 3011
	if (game)
3012
		game |= 1 << 31;
6 cycrow 3013
 
127 cycrow 3014
	if (rest.contains("|"))
3015
	{
3016
		dir = rest.tokens("|", 2);
3017
		rest = rest.token("|", 1);
6 cycrow 3018
	}
3019
 
127 cycrow 3020
	rest = rest.findReplace("\\", "/");
6 cycrow 3021
 
3022
	// wild cards
127 cycrow 3023
	if ( rest.containsAny("*?") )
6 cycrow 3024
	{
102 cycrow 3025
		CDirIO Dir(CFileIO(rest).dir());
6 cycrow 3026
		CyStringList *dirList = Dir.DirList();
3027
		if ( dirList )
3028
		{
3029
			for ( SStringList *strNode = dirList->Head(); strNode; strNode = strNode->next )
3030
			{
127 cycrow 3031
				Utils::String file = Dir.file(strNode->str.ToString());
3032
				if (file.match(rest))
6 cycrow 3033
				{
98 cycrow 3034
					int addGame = game;
3035
					// check if the file exists in the subdirectory too, if it does, add for each game
3036
					if ( game == GAME_ALL && !sMainGame.empty() && !otherGames.empty() ) {
3037
						CFileIO F(file);
3038
						for(Utils::String g = otherGames.firstString(); !g.empty(); g = otherGames.nextString()) {
102 cycrow 3039
							Utils::String checkDir = F.dir() + "/" + g;
121 cycrow 3040
							if ( CDirIO(checkDir).exists(F.filename()) ) {
98 cycrow 3041
								addGame = mainGame;
3042
								C_File *newfile = this->AppendFile(CDirIO(checkDir).File(F.filename()), filetype, CBaseFile::GetGameFromString(g), dir);
134 cycrow 3043
								if (newfile && progress)
3044
									progress->UpdateFile(newfile);
98 cycrow 3045
								if ( newfile ) newfile->SetShared(shared);
3046
							}
3047
						}
3048
					}
3049
 
3050
					C_File *newfile = this->AppendFile(file, filetype, addGame, dir);
134 cycrow 3051
					if (newfile && progress)
3052
						progress->UpdateFile(newfile);
6 cycrow 3053
					if ( newfile )
3054
						newfile->SetShared(shared);
3055
				}
3056
			}
3057
			delete dirList;
3058
		}
3059
	}
3060
	else
3061
	{
127 cycrow 3062
		unsigned int addGame = game;
98 cycrow 3063
		// check if the file exists in the subdirectory too, if it does, add for each game
3064
		if ( game == GAME_ALL && !sMainGame.empty() && !otherGames.empty() ) {
3065
			CFileIO F(rest);
3066
			for(Utils::String g = otherGames.firstString(); !g.empty(); g = otherGames.nextString()) {
102 cycrow 3067
				Utils::String checkDir = F.dir() + "/" + g;
121 cycrow 3068
				if ( CDirIO(checkDir).exists(F.filename()) ) {
98 cycrow 3069
					addGame = mainGame;
121 cycrow 3070
					C_File *newfile = this->AppendFile(CDirIO(checkDir).file(F.filename()), filetype, CBaseFile::GetGameFromString(g), dir);
134 cycrow 3071
					if (newfile && progress)
3072
						progress->UpdateFile(newfile);
98 cycrow 3073
					if ( newfile ) newfile->SetShared(shared);
3074
				}
3075
			}
3076
		}
3077
 
127 cycrow 3078
		unsigned int checkGame = addGame & ~(1 << 31);
3079
		unsigned int foundGame = 0;
3080
		for (int i = 0; i < 31; ++i)
3081
		{
3082
			if (1 << i == checkGame)
3083
			{
3084
				foundGame = i;
3085
				break;
3086
			}
3087
		}
3088
 
3089
		C_File *file = NULL;
3090
		if (foundGame)
3091
		{
3092
			Utils::String addon = gameAddons.findString(Utils::String::Number(foundGame));
3093
			if (!addon.empty())
3094
			{
3095
				Utils::String dir = C_File::GetDirectory(filetype, rest, this);
3096
				Utils::String filename = rest;
130 cycrow 3097
				if (CCatFile::IsAddonDir(dir) && !filename.contains(addon))
3098
				{
127 cycrow 3099
					filename = filename.findReplace(dir + "/", addon + "/" + dir + "/");
130 cycrow 3100
					file = this->AppendFile(filename, filetype, addGame, dir);
3101
				}
127 cycrow 3102
			}
3103
		}
3104
 
3105
		if(!file)
3106
			file = this->AppendFile(rest, filetype, addGame, dir);
3107
 
134 cycrow 3108
		if (file && progress)
3109
			progress->UpdateFile(file);
127 cycrow 3110
		if (file)
6 cycrow 3111
			file->SetShared(shared);
3112
	}
3113
}
3114
 
3115
 
3116
CyString CBaseFile::GetFullFileSizeString() { return SPK::GetSizeString ( this->GetFullFileSize() ); }
3117
 
52 cycrow 3118
// used for a multiple spk file
6 cycrow 3119
unsigned char *CBaseFile::CreateData(size_t *size, CProgressInfo *progress)
3120
{
52 cycrow 3121
	if ( this->WriteFile("temp.dat", progress) ) {
3122
		CFileIO File("temp.dat");
3123
		File.setAutoDelete(true);
3124
		return File.readAll(size);
6 cycrow 3125
	}
3126
	return NULL;
3127
}
3128
 
3129
 
3130
void CBaseFile::ConvertNormalMod(C_File *f, CyString to)
3131
{
3132
	C_File *match = this->FindMatchingMod(f);
3133
	if ( match )
3134
	{
3135
		// file link
3136
		if ( !match->GetData() )
3137
			match->ReadFromFile();
3138
		match->ChangeBaseName(to);
3139
	}
3140
 
3141
	// file link
3142
	if ( !f->GetData() )
3143
		f->ReadFromFile();
3144
 
3145
	f->ChangeBaseName(to);
3146
}
3147
 
3148
void CBaseFile::ConvertAutoText(C_File *f)
3149
{
3150
	CyString to;
3151
	if ( f->GetBaseName().IsIn("-L") )
3152
		to = CyString("0000-L") + f->GetBaseName().GetToken("-L", 2, 2);
3153
	else if ( f->GetBaseName().IsIn("-l") )
3154
		to = CyString("0000-L") + f->GetBaseName().GetToken("-l", 2, 2);
3155
	else
3156
		to = f->GetBaseName().Left(-4) + "0000";
3157
 
3158
	// file link
3159
	if ( !f->GetData() )
3160
		f->ReadFromFile();
3161
 
3162
	f->ChangeBaseName(to);
3163
 
3164
}
3165
 
3166
void CBaseFile::ConvertFakePatch(C_File *f)
3167
{
3168
	// find next available fake patch
3169
	int num = 0;
3170
 
3171
	bool found = true;
3172
	while ( found )
3173
	{
3174
		++num;
3175
		found = false;
3176
		CyString find = CyString::Number(num).PadNumber(2);
3177
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3178
		{
3179
			if ( node->Data()->GetFileType() != FILETYPE_MOD )
3180
				continue;
3181
			if ( !node->Data()->IsFakePatch() )
3182
				continue;
3183
			if ( node->Data()->GetBaseName().Compare(find) )
3184
			{
3185
				found = true;
3186
				break;
3187
			}
3188
		}
3189
	}
3190
 
3191
	CyString to = CyString::Number(num).PadNumber(2);
3192
	C_File *match = this->FindMatchingMod(f);
3193
 
3194
	// file link
3195
	if ( !f->GetData() )
3196
		f->ReadFromFile();
3197
 
3198
	f->ChangeBaseName(to);
3199
	if ( match )
3200
	{
3201
		// file link
3202
		if ( !match->GetData() )
3203
			match->ReadFromFile();
3204
		match->ChangeBaseName(to);
3205
	}
3206
}
3207
 
3208
C_File *CBaseFile::FindMatchingMod(C_File *f)
3209
{
3210
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3211
	{
3212
		if ( node->Data()->GetFileType() != FILETYPE_MOD )
3213
			continue;
3214
 
3215
		if ( f->GetFileExt().Compare(node->Data()->GetFileExt()) )
3216
			continue;
3217
 
3218
		if ( f->GetBaseName().Compare(node->Data()->GetBaseName()) )
3219
			return node->Data();
3220
	}
3221
 
3222
	return NULL;
3223
}
3224
 
3225
void CBaseFile::RenameFile(C_File *f, CyString baseName)
3226
{
3227
	if ( f->GetFileType() == FILETYPE_MOD )
3228
	{
3229
		C_File *match = this->FindMatchingMod(f);
3230
		if ( match )
3231
			match->ChangeBaseName(baseName);
3232
	}
3233
 
3234
	// need to edit the file
134 cycrow 3235
	if (f->GetFileType() == FILETYPE_SCRIPT || f->GetFileType() == FILETYPE_UNINSTALL)
3236
		f->RenameScript(baseName);
6 cycrow 3237
 
134 cycrow 3238
	f->ChangeBaseName(baseName);
6 cycrow 3239
}
3240
 
134 cycrow 3241
Utils::String CBaseFile::createUpdateFile(const Utils::String &dir) const
6 cycrow 3242
{
134 cycrow 3243
	Utils::String file = this->getNameValidFile() + "_" + this->author() + ".dat";
3244
	file.removeChar(' ');
6 cycrow 3245
 
134 cycrow 3246
	Utils::CStringList write;
3247
	write.pushBack("Package: " + this->name());
3248
	write.pushBack("Author: " + this->author());
3249
	write.pushBack("Version: " + this->version());
3250
	write.pushBack("File: " + CFileIO(this->filename()).filename());
6 cycrow 3251
 
3252
	CFileIO File(dir + "/" + file);
134 cycrow 3253
	if (File.writeFile(&write))
102 cycrow 3254
		return File.fullFilename();
134 cycrow 3255
	return Utils::String::Null();
6 cycrow 3256
}
3257
 
134 cycrow 3258
Utils::String CBaseFile::ErrorString(int error, const Utils::String &errorStr)
6 cycrow 3259
{
134 cycrow 3260
	if (error == SPKERR_NONE) return Utils::String::Null();
6 cycrow 3261
 
134 cycrow 3262
	Utils::String err;
6 cycrow 3263
 
126 cycrow 3264
	switch (error)
6 cycrow 3265
	{
126 cycrow 3266
	case SPKERR_MALLOC:
3267
		err = "Memory Failed";
3268
		break;
3269
	case SPKERR_FILEOPEN:
3270
		err = "Failed to open file";
3271
		break;
3272
	case SPKERR_FILEREAD:
3273
		err = "Failed to read file";
3274
		break;
3275
	case SPKERR_UNCOMPRESS:
3276
		err = "Failed to Uncompress";
3277
		break;
3278
	case SPKERR_WRITEFILE:
3279
		err = "Failed to write file";
3280
		break;
3281
	case SPKERR_CREATEDIRECTORY:
3282
		err = "Failed to create directory";
3283
		break;
3284
	case SPKERR_FILEMISMATCH:
3285
		err = "File count mismatch";
3286
		break;
6 cycrow 3287
	}
3288
 
134 cycrow 3289
	if (!err.empty())
6 cycrow 3290
	{
134 cycrow 3291
		if (!errorStr.empty())
6 cycrow 3292
		{
3293
			err += " (";
3294
			err += errorStr + ")";
3295
		}
3296
		return err;
3297
	}
3298
 
134 cycrow 3299
	return Utils::String::Number((long)error);
6 cycrow 3300
}
3301
 
126 cycrow 3302
bool CBaseFile::SaveToArchive(CyString filename, int game, const CGameExe *exes, CProgressInfo *progress)
6 cycrow 3303
{
131 cycrow 3304
	return saveToArchive(filename.ToString(), game, exes, progress);
3305
}
3306
bool CBaseFile::saveToArchive(const Utils::String &filename, int game, const CGameExe *exes, CProgressInfo *progress)
3307
{
3308
	CDirIO Dir(CFileIO(filename).dir());
3309
	if (!Dir.exists())
3310
		Dir.create();
3311
 
6 cycrow 3312
	TCHAR buf[5000];
3313
	wsprintf(buf, L"%hs", filename.c_str());
3314
 
3315
	HZIP hz = CreateZip(buf, 0);
126 cycrow 3316
	if (!hz) return false;
6 cycrow 3317
 
3318
	// read files and compress
3319
	ReadAllFilesToMemory();
126 cycrow 3320
	if (!UncompressAllFiles(progress))
6 cycrow 3321
	{
3322
		CloseZip(hz);
3323
		return false;
3324
	}
3325
 
126 cycrow 3326
	for (CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next())
6 cycrow 3327
	{
126 cycrow 3328
		if (game != -1) {
127 cycrow 3329
			if (game && !(node->Data()->game() & (1 << game)))
6 cycrow 3330
				continue;
127 cycrow 3331
			// extracting for all games, so ignore files that have a game set
3332
			if (!game && node->Data()->game() && node->Data()->game() != GAME_ALLNEW)
6 cycrow 3333
				continue;
3334
		}
131 cycrow 3335
		Utils::String fname = node->Data()->getNameDirectory(this);
126 cycrow 3336
 
3337
		// use the addon directory
3338
		if (node->Data()->isFileInAddon() && exes)
3339
		{
127 cycrow 3340
			unsigned int whatGame = game;
126 cycrow 3341
			if (game == -1)
127 cycrow 3342
			{
3343
				unsigned int checkGame = node->Data()->game() & ~GAME_ALLNEW;
3344
				whatGame = 0;
3345
				for (int i = 0; i < 31; ++i)
3346
				{
3347
					if (1 << i == checkGame)
3348
					{
3349
						whatGame = i;
3350
						break;
3351
					}
3352
				}
3353
			}
126 cycrow 3354
 
127 cycrow 3355
			if (whatGame > 0)
126 cycrow 3356
			{
127 cycrow 3357
				SGameExe *e = exes->GetGame(whatGame - 1);
3358
				if (e)
3359
				{
3360
					if (e->iFlags & EXEFLAG_ADDON)
3361
						fname = e->sAddon + "/" + fname;
3362
				}
126 cycrow 3363
			}
3364
		}
3365
 
6 cycrow 3366
		// create the directory
3367
		wsprintf(buf, L"%hs", fname.c_str());
127 cycrow 3368
		if (node->Data()->isExternalFile())
3369
		{
3370
			CFileIO file(node->Data()->filePointer());
3371
			size_t size = 0;
3372
			unsigned char *data = file.readAll(&size);
3373
			ZipAdd(hz, buf, data, size);
3374
			delete data;
3375
		}
3376
		else
3377
			ZipAdd(hz, buf, node->Data()->GetData(), node->Data()->GetDataSize());
6 cycrow 3378
	}
3379
 
109 cycrow 3380
	// if its a ship, then add any generated files
3381
	this->addGeneratedFiles(hz);
3382
 
6 cycrow 3383
	// add the data file
126 cycrow 3384
	Utils::CStringList list;
127 cycrow 3385
	Utils::CStringList addons;
3386
	if ( this->GeneratePackagerScript(false, &list, game, addons, true) )
6 cycrow 3387
	{
126 cycrow 3388
		if ( CFileIO("test.tmp").writeFile(&list) )
6 cycrow 3389
		{
3390
			ZipAdd(hz, L"pluginmanager.txt", L"test.tmp");
52 cycrow 3391
			CFileIO::Remove("test.tmp");
6 cycrow 3392
		}
3393
	}
3394
 
3395
	CloseZip(hz);
3396
 
3397
	return true;
3398
}
3399
 
13 cycrow 3400
int CBaseFile::GetGameFromString(const Utils::String &sGame)
6 cycrow 3401
{
3402
	int iGame = GAME_ALL;
3403
	if ( sGame.Compare("ALL") )
3404
		iGame = GAME_ALL;
3405
	else if ( sGame.Compare("X3") )
3406
		iGame = GAME_X3;
3407
	else if ( sGame.Compare("X2") )
3408
		iGame = GAME_X2;
3409
	else if ( sGame.Compare("X3TC") )
3410
		iGame = GAME_X3TC;
126 cycrow 3411
	else if (sGame.Compare("X3AP"))
6 cycrow 3412
		iGame = GAME_X3AP;
126 cycrow 3413
	else if (sGame.Compare("X3FL"))
3414
		iGame = GAME_X3FL;
13 cycrow 3415
	else if ( sGame.isNumber() )
3416
		iGame = sGame;
6 cycrow 3417
 
3418
	return iGame;
3419
}
3420
 
13 cycrow 3421
Utils::String CBaseFile::ConvertGameToString(int iGame)
6 cycrow 3422
{
13 cycrow 3423
	Utils::String game = "ALL";
6 cycrow 3424
 
3425
	switch(iGame) {
3426
		case GAME_ALL:
127 cycrow 3427
		case GAME_ALLNEW:
6 cycrow 3428
			game = "ALL";
3429
			break;
3430
		case GAME_X2:
3431
			game = "X2";
3432
			break;
3433
		case GAME_X3:
3434
			game = "X3";
3435
			break;
3436
		case GAME_X3TC:
3437
			game = "X3TC";
3438
			break;
3439
		case GAME_X3AP:
3440
			game = "X3AP";
3441
			break;
126 cycrow 3442
		case GAME_X3FL:
3443
			game = "X3FL";
3444
			break;
6 cycrow 3445
		default:
3446
			game = (long)iGame;
3447
	}
3448
 
3449
	return game;
3450
}
3451
 
3452
bool CBaseFile::IsGameInPackage(int game)
3453
{
3454
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3455
		if ( node->Data()->GetGame() == game )
3456
			return true;
3457
	}
3458
 
3459
	return false;
3460
}
3461
 
3462
bool CBaseFile::IsAnyGameInPackage()
3463
{
3464
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3465
		if ( node->Data()->GetGame() )
3466
			return true;
3467
	}
3468
 
3469
	return false;
3470
}
3471
 
88 cycrow 3472
Utils::String builtInWares()
3473
{
3474
	Utils::String str;
3475
	str += "28;0;0;0;0;59;5753;0;35714;1;1;0;35714;-100000;0;0;SS_WARE_SW_NEW1;\n";
3476
	str += "28;0;0;0;0;60;5763;0;33232;1;1;0;33232;0;1043;0;SS_WARE_SW_NEW2;\n";
3477
	str += "28;0;0;0;0;61;5773;0;21428;1;1;0;21428;0;1043;0;SS_WARE_SW_NEW3;\n";
3478
	str += "28;0;0;0;0;62;5783;0;56;1;1;0;56;-100000;0;0;SS_WARE_SW_NEW4;\n";
3479
	str += "28;0;0;0;0;63;5793;0;88;1;1;0;88;-100000;0;0;SS_WARE_SW_NEW5;\n";
3480
	str += "28;0;0;0;0;64;5803;0;283;1;1;0;283;-100000;0;0;SS_WARE_SW_NEW6;\n";
3481
	str += "28;0;0;0;0;65;5813;0;383;1;1;0;383;-100000;0;0;SS_WARE_SW_NEW7;\n";
3482
	str += "28;0;0;0;0;66;5823;0;1389;1;1;0;1389;-100000;1043;0;SS_WARE_SW_NEW8;\n";
3483
	str += "28;0;0;0;0;67;5833;0;3396;1;1;0;3396;-100000;0;0;SS_WARE_SW_NEW9;\n";
3484
	str += "28;0;0;0;0;68;5843;0;4215;1;1;0;4215;-100000;0;0;SS_WARE_SW_NEW10;\n";
3485
	str += "28;0;0;0;0;69;5853;0;5635;1;1;0;5635;-100000;0;0;SS_WARE_SW_NEW11;\n";
3486
	str += "28;0;0;0;0;70;5863;0;65735;1;1;0;65735;-100000;0;0;SS_WARE_SW_NEW12;\n";
3487
	str += "28;0;0;0;0;71;5873;0;17857;1;1;0;17857;333;1043;0;SS_WARE_SW_NEW13;\n";
3488
	str += "28;0;0;0;0;72;5883;0;21428;1;1;0;21428;0;1043;0;SS_WARE_SW_NEW14;\n";
3489
	str += "28;0;0;0;0;73;5893;0;324515;1;1;0;324515;-100000;0;0;SS_WARE_SW_NEW15;\n";
3490
	str += "28;0;0;0;0;74;5903;0;638508;1;1;0;638508;-100000;0;0;SS_WARE_SW_NEW16;\n";
3491
	str += "28;0;0;0;0;75;5913;0;225755;1;1;0;225755;-100000;0;0;SS_WARE_SW_NEW17;\n";
3492
	str += "28;0;0;0;0;76;5923;0;1931535;1;1;0;1931535;1000;0;0;SS_WARE_SW_NEW18;\n";
3493
	str += "28;0;0;0;0;77;5933;0;2209150;1;1;0;2209150;-100000;0;0;SS_WARE_SW_NEW19;\n";
3494
	str += "28;0;0;0;0;78;5943;0;6727565;1;1;0;6727565;-100000;0;0;SS_WARE_SW_NEW20;\n";
3495
	str += "28;0;0;0;0;85;9999;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_1;\n";
3496
	str += "28;0;0;0;0;86;15053;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_2;\n";
3497
	str += "28;0;0;0;0;87;15063;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_3;\n";
3498
	str += "28;0;0;0;0;88;15073;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_4;\n";
3499
	str += "28;0;0;0;0;89;15083;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_5;\n";
3500
	str += "28;0;0;0;0;90;15093;0;105;1;5;0;105;0;1043;0;SS_WARE_SW_X3TC_6;\n";
3501
 
3502
	return str;
3503
}
3504
 
3505
void CBaseFile::_addWaresToList(int iLang, CLinkList<SWareEntry> &list, const Utils::String &wares, enum WareTypes eType)
3506
{
3507
	int totalWares = 0;
3508
	Utils::String *w = wares.tokenise("\n", &totalWares);
3509
 
3510
	for(int i = 0; i < totalWares; i++) {
3511
		int textId = w[i].token(";", 7).toLong();
3512
		int useLang = iLang;
3513
		if ( !_pTextDB->exists(useLang, 17, textId) )
3514
			useLang = 44;
3515
		if ( !_pTextDB->exists(useLang, 17, textId) )
3516
			useLang = 49;
3517
		if ( _pTextDB->exists(useLang, 17, textId) ) {	
3518
			SWareEntry *ware = new SWareEntry;
3519
			ware->name = _pTextDB->get(useLang, 17, textId);
3520
			ware->description = _pTextDB->get(useLang, 17, textId + 1);
3521
			ware->id = w[i].token(";", -2);
3522
			ware->relval = w[i].token(";", 9).toLong();
3523
			ware->notority = w[i].token(";", 14).toLong();
3524
			ware->type = eType;
3525
			ware->position = i;
89 cycrow 3526
			ware->package = this;
88 cycrow 3527
			list.push_back(ware);
3528
		}
3529
	}
3530
 
3531
	CLEANSPLIT(w, totalWares);
3532
}
3533
 
3534
bool CBaseFile::readWares(int iLang, CLinkList<SWareEntry> &list, const Utils::String &empWares)
3535
{
3536
	_pTextDB->setLanguage(iLang);
3537
 
3538
	// now go through all emp wares and get the ones we have text for
3539
	_addWaresToList(iLang, list, empWares, Ware_EMP);
3540
	_addWaresToList(iLang, list, builtInWares(), Ware_BuiltIn);
3541
 
89 cycrow 3542
	return true;
3543
}
88 cycrow 3544
 
89 cycrow 3545
bool CBaseFile::_readCommands(int iLang, int iStartID, CLinkList<SCommandSlot> &list)
3546
{
3547
	_pTextDB->setLanguage(iLang);
3548
 
3549
	for(int i = 2; i <= 13; i++) {
3550
		for(int j = 0; j < 64; j++) {
3551
			int id = (i * 100) + j;
3552
			if ( _pTextDB->exists(iLang, iStartID + 2, id) ) {
3553
				SCommandSlot *slot = new SCommandSlot;
3554
				list.push_back(slot);
3555
 
3556
				slot->id = _pTextDB->get(iLang, iStartID, id);
3557
				slot->name = _pTextDB->get(iLang, iStartID + 2, id);
3558
				slot->shortName = _pTextDB->get(iLang, iStartID + 3, id);
3559
				slot->info = _pTextDB->get(iLang, iStartID + 14, id);
3560
				slot->slot = id;
3561
				slot->package = this;
3562
 			}
3563
		}
3564
	}
3565
 
3566
	for(int i = 1400; i <= 2000; i++) {
3567
		if ( _pTextDB->exists(iLang, iStartID + 2, i) ) {
3568
			SCommandSlot *slot = new SCommandSlot;
3569
			list.push_back(slot);
3570
 
3571
			slot->id = _pTextDB->get(iLang, iStartID, i);
3572
			slot->name = _pTextDB->get(iLang, iStartID + 2, i);
3573
			slot->shortName = _pTextDB->get(iLang, iStartID + 3, i);
3574
			slot->info = _pTextDB->get(iLang, iStartID + 14, i);
3575
			slot->slot = i;
3576
			slot->package = this;
3577
		}
3578
	}
3579
 
3580
	for(int i = 6000; i <= 9999; i++) {
3581
		if ( _pTextDB->exists(iLang, iStartID + 2, i) ) {
3582
			SCommandSlot *slot = new SCommandSlot;
3583
			list.push_back(slot);
3584
 
3585
			slot->id = _pTextDB->get(iLang, iStartID, i);
3586
			slot->name = _pTextDB->get(iLang, iStartID + 2, i);
3587
			slot->shortName = _pTextDB->get(iLang, iStartID + 3, i);
3588
			slot->info = _pTextDB->get(iLang, iStartID + 14, i);
3589
			slot->slot = i;
3590
			slot->package = this;
3591
		}
3592
	}
3593
 
88 cycrow 3594
	return true;
3595
}
3596
 
89 cycrow 3597
bool CBaseFile::readWingCommands(int iLang, CLinkList<SCommandSlot> &list)
3598
{
3599
	return _readCommands(iLang, 2028, list);
3600
}
3601
 
3602
bool CBaseFile::readCommands(int iLang, CLinkList<SCommandSlot> &list)
3603
{
3604
	return _readCommands(iLang, 2008, list);
3605
}
3606
 
6 cycrow 3607
int CBaseFile::FindFirstGameInPackage()
3608
{
3609
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3610
		if ( node->Data()->GetGame() )
3611
			return node->Data()->GetGame();
3612
	}
3613
 
3614
	return 0;
3615
}
3616
bool CBaseFile::IsMultipleGamesInPackage()
3617
{
3618
	int game = 0;
3619
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3620
		if ( node->Data()->GetGame() ) {
3621
			if ( game != node->Data()->GetGame() )
3622
				return true;
3623
			game = node->Data()->GetGame();
3624
		}
3625
	}
3626
 
3627
	return false;
3628
}