Subversion Repositories spk

Rev

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