Subversion Repositories spk

Rev

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

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