Subversion Repositories spk

Rev

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