Subversion Repositories spk

Rev

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