Subversion Repositories spk

Rev

Rev 48 | Rev 50 | 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;
49 cycrow 1387
	else if ( first.Compare("WebAddress:") )		this->setWebAddress(rest);
1388
	else if ( first.Compare("WebSite:") )			this->setWebSite(rest);
1389
	else if ( first.Compare("Email:") )				this->setEmail(rest);
14 cycrow 1390
	else if ( first.Compare("WebMirror1:") || first.Compare("Mirror1:") || first.Compare("WebMirror:") )
6 cycrow 1391
		this->AddWebMirror(rest);
14 cycrow 1392
	else if ( first.Compare("WebMirror2:") || first.Compare("Mirror2:") )
6 cycrow 1393
		this->AddWebMirror(rest);
48 cycrow 1394
	else if ( first.Compare("PluginType:") )		this->setPluginType(rest);
1395
	else if ( first.Compare("Desc:") )				this->setDescription(rest);
1396
	else if ( first.Compare("UninstallAfter:") )	this->addUninstallText(ParseLanguage(rest.token("|", 1)), false, rest.tokens("|", 2));
1397
	else if ( first.Compare("UninstallBefore:") )	this->addUninstallText(ParseLanguage(rest.token("|", 1)), true, rest.tokens("|", 2));
1398
	else if ( first.Compare("InstallAfter:") )		this->addInstallText(ParseLanguage(rest.token("|", 1)), false, rest.tokens("|", 2));
1399
	else if ( first.Compare("InstallBefore:") )		this->addInstallText(ParseLanguage(rest.token("|", 1)), true, rest.tokens("|", 2));
14 cycrow 1400
	else if ( first.Compare("ScriptName:") )
1401
		AddLanguageName(ParseLanguage(rest.token(":", 1)), rest.token(":", 2));
48 cycrow 1402
	else if ( first.Compare("GameChanging:") )		this->setGameChanging(rest);
1403
	else if ( first.Compare("EaseOfUse:") )			this->setEaseOfUse(rest);
1404
	else if ( first.Compare("Recommended:") )		this->setRecommended(rest);
14 cycrow 1405
	else if ( first.Compare("NeededLibrary:") )
1406
		this->AddNeededLibrary(rest.token("||", 1), rest.token("||", 2), rest.token("||", 3));
1407
	else if ( first.Compare("FakePatchBefore:") )
1408
		this->AddFakePatchOrder(false, rest.token("||", 1), rest.token("||", 2));
1409
	else if ( first.Compare("FakePatchAfter:") )
1410
		this->AddFakePatchOrder(true, rest.token("||", 1), rest.token("||", 2));
49 cycrow 1411
	else if ( first.Compare("ForumLink:") )			this->setForumLink(rest);
6 cycrow 1412
	else
1413
		return false;
1414
 
1415
	return true;
1416
}
1417
 
48 cycrow 1418
int CBaseFile::ParseLanguage(const Utils::String &lang) const
6 cycrow 1419
{
48 cycrow 1420
	int langID = lang;
1421
	if ( !langID ) {
1422
		if ( lang.Compare("english") )		return 44;
1423
		else if ( lang.Compare("default") )	return 0;
1424
		else if ( lang.Compare("german") )	return 49;
1425
		else if ( lang.Compare("russian") )	return 7;
1426
		else if ( lang.Compare("spanish") )	return 34;
1427
		else if ( lang.Compare("french") )	return 33;
6 cycrow 1428
	}
1429
 
1430
	return langID;
1431
}
1432
 
1433
/*
1434
	Func:   ReadValues
1435
	Input:  String - values in one long line
1436
	Desc:   splits the values data into each line to read the data
1437
*/
1438
void CBaseFile::ReadValues ( CyString values )
1439
{
1440
	int num = 0;
1441
	CyString *lines = values.SplitToken ( '\n', &num );
1442
 
1443
	for ( int i = 0; i < num; i++ )
14 cycrow 1444
		ParseValueLine ( lines[i].ToString() );
6 cycrow 1445
 
1446
	CLEANSPLIT(lines, num)
1447
}
1448
 
1449
/*
1450
	Func:   ParseFilesLine
1451
	Input:  String - single line from a file to set
1452
	Return: Boolean - returns true if value exists
1453
	Desc:   Reads the line and assigns the parameters for the file
1454
*/
1455
bool CBaseFile::ParseFilesLine ( CyString line )
1456
{
1457
	if ( !line.IsIn(":") )
1458
		return false;
1459
 
1460
	CyString command = line.GetToken ( 1, ':' );
1461
 
1462
	long size = line.GetToken ( 2, ':').ToInt ();
1463
	long usize = line.GetToken ( 3, ':').ToInt ();
1464
	long compression = line.GetToken ( 4, ':').ToInt ();
1465
 
1466
	if ( command == "Icon" )
1467
	{
1468
		m_sIconExt = line.GetToken ( 5, ':' );
1469
		m_pIconFile = new C_File ();
1470
		m_pIconFile->SetDataSize ( size - 4 );
1471
		m_pIconFile->SetDataCompression ( compression );
1472
		m_pIconFile->SetUncompressedDataSize ( usize );
1473
 
1474
		return true;
1475
	}
1476
 
1477
	time_t time = line.GetToken ( 5,':' ).ToLong();
1478
	bool compressToFile = (line.GetToken ( 6, ':').ToInt() == 1) ? true : false;
1479
	CyString name  = line.GetToken ( 7, ':' );
1480
	CyString dir = line.GetToken ( 8, ':' );
1481
 
1482
	if ( name.Empty() )
1483
		return true;
1484
 
1485
	bool shared = false;
1486
	if ( command.Left(1) == "$" )
1487
	{
1488
		shared = true;
1489
		command.Erase ( 0, 1 );
1490
	}
1491
 
1492
	int type = -1;
1493
	if ( command == "Script" )
1494
		type = FILETYPE_SCRIPT;
1495
	else if ( command == "Text" )
1496
		type = FILETYPE_TEXT;
1497
	else if ( command == "Readme" )
1498
		type = FILETYPE_README;
1499
	else if ( command == "Map" )
1500
		type = FILETYPE_MAP;
1501
	else if ( command == "Mod" )
1502
		type = FILETYPE_MOD;
1503
	else if ( command == "Uninstall" )
1504
		type = FILETYPE_UNINSTALL;
1505
	else if ( command == "Sound" )
1506
		type = FILETYPE_SOUND;
1507
	else if ( command == "Mission" )
1508
		type = FILETYPE_MISSION;
1509
	else if ( command == "Extra" )
1510
		type = FILETYPE_EXTRA;
1511
	else if ( command == "Screen" )
1512
		type = FILETYPE_SCREEN;
1513
	else if ( command == "Backup" )
1514
		type = FILETYPE_BACKUP;
1515
	else if ( command == "Advert" )
1516
		type = FILETYPE_ADVERT;
1517
	else if ( command == "ShipScene" )
1518
		type = FILETYPE_SHIPSCENE;
1519
	else if ( command == "CockpitScene" )
1520
		type = FILETYPE_COCKPITSCENE;
1521
	else if ( command == "ShipOther" )
1522
		type = FILETYPE_SHIPOTHER;
1523
	else if ( command == "ShipModel" )
1524
		type = FILETYPE_SHIPMODEL;
1525
 
1526
	if ( type == -1 )
1527
		return false;
1528
 
1529
	C_File *file = new C_File ();
1530
 
1531
	if ( dir.Left(5).Compare("GAME_") ) {
1532
		file->SetGame(dir.GetToken("_", 2, 2).ToInt());
1533
		dir = NullString;
1534
	} 
1535
	else if ( line.NumToken(":") >= 9 ) {
1536
		file->SetGame(line.GetToken(":", 9, 9).GetToken("_", 2, 2).ToInt());
1537
	}
1538
 
1539
	file->SetFileType ( type );
1540
	file->SetCreationTime ( time );
1541
	file->SetName ( name );
1542
	file->SetDir ( dir );
1543
	file->SetDataSize ( size - 4 );
1544
	file->SetDataCompression ( compression );
1545
	file->SetUncompressedDataSize ( usize );
1546
	file->SetShared ( shared );
1547
	file->SetCompressedToFile ( compressToFile );
1548
 
1549
	m_lFiles.push_back ( file );
1550
 
1551
	return true;
1552
}
1553
 
1554
 
1555
/*
1556
	Func:   ParseFiles
1557
	Input:  String - values in one long line
1558
	Desc:   splits the files data into each line to read the data
1559
*/
1560
void CBaseFile::ReadFiles ( CyString values )
1561
{
1562
	int num = 0;
1563
	CyString *lines = values.SplitToken ( '\n', &num );
1564
 
1565
	for ( int i = 0; i < num; i++ )
1566
		ParseFilesLine ( lines[i] );
1567
 
1568
	CLEANSPLIT(lines, num)
1569
}
1570
 
1571
 
1572
 
1573
/*
1574
	Func:   ReadFile
1575
	Input:  filename - the name of the file to open and read
1576
			readdata - If falses, dont read the files to memory, just read the headers and values
1577
	Return: boolean - return ture if acceptable format
1578
	Desc:   Opens and reads the spk file and loads all data into class
1579
*/
1580
bool CBaseFile::ReadFile ( CyString filename, int readtype, CProgressInfo *progress )
1581
{
1582
	FILE *id = fopen ( filename.c_str(), "rb" );
1583
	if ( !id )
1584
		return false;
1585
 
1586
	bool ret = ReadFile ( id, readtype, progress );
1587
	if ( ret )
1588
		m_sFilename = filename;
1589
 
1590
	fclose ( id );
1591
 
1592
	return ret;
1593
}
1594
bool CBaseFile::ReadFile ( FILE *id, int readtype, CProgressInfo *progress )
1595
{
1596
	ClearError ();
1597
 
1598
	// first read the header
1599
	if ( !ParseHeader ( GetEndOfLine ( id, NULL, false ) ) )
1600
		return false;
1601
 
1602
	if ( readtype == SPKREAD_HEADER )
1603
		return true;
1604
 
1605
	// update the progress for each section
1606
	int maxProgress = (readtype == SPKREAD_VALUES) ? 3 : 6;
1607
	if ( readtype != SPKREAD_ALL && progress )
1608
		progress->UpdateProgress(1, maxProgress);
1609
 
1610
	long doneLen = 0;
1611
	// next read the data values for the spk files
1612
	if ( m_SHeader.lValueCompressSize )
1613
	{
1614
		// read data to memory
1615
		unsigned char *readData = new unsigned char[m_SHeader.lValueCompressSize];
1616
		unsigned char size[4];
1617
		fread ( size, 4, 1, id );
1618
		fread ( readData, sizeof(unsigned char), m_SHeader.lValueCompressSize, id );
1619
		unsigned long uncomprLen = (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
1620
 
1621
		// check for zlib compression
1622
		if ( m_SHeader.iValueCompression == SPKCOMPRESS_ZLIB )
1623
		{
1624
			// uncomress the data
1625
			unsigned char *uncompr = new unsigned char[uncomprLen];
1626
 
1627
			int err = uncompress ( uncompr, &uncomprLen, readData, m_SHeader.lValueCompressSize );
1628
			// update the progress for each section
1629
			if ( readtype != SPKREAD_ALL && progress )
1630
				progress->UpdateProgress(2, maxProgress);
1631
			if ( err == Z_OK )
1632
				ReadValues ( CyString ((char *)uncompr) );
1633
			doneLen = uncomprLen;
1634
			delete uncompr;
1635
		}
1636
		else if ( m_SHeader.iValueCompression == SPKCOMPRESS_7ZIP )
1637
		{
1638
			long len = uncomprLen;
1639
			unsigned char *compr = LZMADecode_C ( readData, m_SHeader.lValueCompressSize, (size_t*)&len, NULL );
1640
			// update the progress for each section
1641
			if ( readtype != SPKREAD_ALL && progress )
1642
				progress->UpdateProgress(2, maxProgress);
1643
 
1644
			if ( compr )
1645
				ReadValues ( CyString ((char *)compr) );
1646
		}
1647
		// no compression
1648
		else
1649
			ReadValues ( CyString ((char *)readData) );
1650
 
1651
		delete readData;
1652
	}
1653
 
1654
	// update the progress for each section
1655
	if ( readtype != SPKREAD_ALL && progress )
1656
		progress->UpdateProgress(3, maxProgress);
1657
 
1658
	if ( readtype == SPKREAD_VALUES )
1659
		return true;
1660
 
1661
	// next should be the next header
1662
	if ( !ParseFileHeader ( GetEndOfLine (id, NULL, false) ) )
1663
		return false;
1664
 
1665
	// clear the current file list
1666
	m_lFiles.clear(true);
1667
 
1668
	// update the progress for each section
1669
	if ( readtype != SPKREAD_ALL && progress )
1670
		progress->UpdateProgress(4, maxProgress);
1671
 
1672
	if ( m_SHeader2.lSize )
1673
	{
1674
		unsigned char *readData = new unsigned char[m_SHeader2.lSize];
1675
		unsigned char size[4];
1676
		fread ( size, 4, 1, id );
1677
		fread ( readData, sizeof(char), m_SHeader2.lSize, id );
1678
 
1679
		unsigned long uncomprLen = (size[0] << 24) + (size[1] << 16) + (size[2] << 8) + size[3];
1680
		// check for zlib compression
1681
		if ( m_SHeader.iValueCompression == SPKCOMPRESS_ZLIB )
1682
		{
1683
 
1684
			if ( uncomprLen < (unsigned long)doneLen )
1685
				uncomprLen = doneLen;
1686
 
1687
			unsigned char *uncompr = new unsigned char[uncomprLen];
1688
			int err = uncompress ( uncompr, &uncomprLen, readData, m_SHeader2.lSize );
1689
			// update the progress for each section
1690
			if ( readtype != SPKREAD_ALL && progress )
1691
				progress->UpdateProgress(5, maxProgress);
1692
			if ( err == Z_OK )
1693
				ReadFiles ( CyString ((char *)uncompr) );
1694
			delete uncompr;
1695
		}
1696
		else if ( m_SHeader.iValueCompression == SPKCOMPRESS_7ZIP )
1697
		{
1698
			long len = uncomprLen;
1699
			unsigned char *compr = LZMADecode_C ( readData, m_SHeader2.lSize, (size_t*)&len, NULL );
1700
			// update the progress for each section
1701
			if ( readtype != SPKREAD_ALL && progress )
1702
				progress->UpdateProgress(5, maxProgress);
1703
			if ( compr )
1704
				ReadFiles ( CyString ((char *)compr) );
1705
		}
1706
		else
1707
			ReadFiles ( CyString ((char *)readData) );
1708
 
1709
		delete readData;
1710
	}
1711
 
1712
	// file mismatch
1713
	long numfiles = m_lFiles.size();
1714
	if ( m_pIconFile )
1715
		++numfiles;
1716
	if ( m_SHeader2.iNumFiles != numfiles )
1717
	{
1718
		m_iLastError = SPKERR_FILEMISMATCH;
1719
		return false;
1720
	}
1721
 
1722
	// update the progress for each section
1723
	if ( readtype != SPKREAD_ALL && progress )
1724
		progress->UpdateProgress(6, maxProgress);
1725
 
1726
	if ( readtype == SPKREAD_ALL )
1727
	{
1728
		int fileCount = 2;
1729
		if ( m_pIconFile )
1730
			m_pIconFile->ReadFromFile ( id, m_pIconFile->GetDataSize() );
1731
 
1732
		// ok finally we need to read all the files
1733
 
1734
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
1735
		{
1736
			node->Data()->ReadFromFile ( id, node->Data()->GetDataSize() );
1737
			if ( progress )
1738
				progress->UpdateProgress(fileCount++, m_lFiles.size() + 2);
1739
		}
1740
 
1741
		m_bFullyLoaded = true;
1742
	}
1743
 
1744
	return true;
1745
}
1746
 
1747
bool CBaseFile::IsMod()
1748
{
1749
	// check for any mod files that are not fake patchs
1750
	for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
1751
	{
1752
		C_File *f = fNode->Data();
1753
		if ( f->GetFileType() != FILETYPE_MOD )
1754
			continue;
1755
 
1756
		if ( !f->IsFakePatch() )
1757
			return true;
1758
	}
1759
 
1760
	return false;
1761
}
1762
 
13 cycrow 1763
bool CBaseFile::IsFakePatch() const
6 cycrow 1764
{
1765
	// check for any mod files that are not fake patchs
1766
	for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
1767
	{
1768
		C_File *f = fNode->Data();
1769
		if ( f->GetFileType() != FILETYPE_MOD )
1770
			continue;
1771
 
1772
		if ( f->IsFakePatch() )
1773
			return true;
1774
	}
1775
 
1776
	return false;
1777
}
1778
 
14 cycrow 1779
Utils::String CBaseFile::CreateValuesLine () const
6 cycrow 1780
{
14 cycrow 1781
	Utils::String values("Name: ");
1782
	values += Utils::String(m_sName.ToString()) + "\n";
1783
	values += Utils::String("Author: ") + m_sAuthor.ToString() + "\n";
1784
	values += Utils::String("Version: ") + m_sVersion.ToString() + "\n";
6 cycrow 1785
	if ( !m_sCreationDate.Empty() )
14 cycrow 1786
		values += Utils::String("Date: ") + m_sCreationDate.ToString() + "\n";
49 cycrow 1787
 
1788
	if ( !this->webAddress().empty() )	values += "WebAddress: "	+ this->webAddress()	+ "\n";
1789
	if ( !this->webSite().empty() )		values += "WebSite: "		+ this->webSite()		+ "\n";
1790
	if ( !this->email().empty() )		values += "Email: "			+ this->email()			+ "\n";
1791
	if ( !this->forumLink().empty() )	values += "ForumLink: "		+ this->forumLink()		+ "\n";
1792
 
6 cycrow 1793
	for ( SStringList *str = m_lMirrors.Head(); str; str = str->next )
14 cycrow 1794
		values += Utils::String("WebMirror: ") + str->str.ToString() + "\n";
48 cycrow 1795
	if ( !this->description().empty() ) {
1796
		Utils::String desc = this->description();
14 cycrow 1797
		desc = desc.findReplace("<newline>", "<br>");
1798
		desc = desc.findReplace("\n", "<br>");
1799
		desc.remove('\r');
1800
		values += "Desc: " + desc + "\n";
6 cycrow 1801
	}
1802
 
1803
	for ( CListNode<SGameCompat> *gc = m_lGames.Front(); gc; gc = gc->next() ) {
46 cycrow 1804
		if ( !gc->Data()->sVersion.empty() )
1805
			values += Utils::String("GameCompatExact: ") + (long)gc->Data()->iGame + " " + gc->Data()->sVersion + "\n";
6 cycrow 1806
		else
14 cycrow 1807
			values += Utils::String("GameCompat: ") + (long)gc->Data()->iGame + " " + (long)gc->Data()->iVersion + "\n";
6 cycrow 1808
	}
1809
 
1810
	if ( m_bSigned )
1811
		values += "Signed\n";
1812
 
46 cycrow 1813
	for ( int j = 0; j < 2; j++ ) {
1814
		const CInstallText *text;
1815
		Utils::String textStart;
1816
		switch(j) {
1817
			case 0:	textStart = "Uninstall"; text = this->uninstallText(); break;
1818
			case 1: textStart = "Install"; text = this->installText(); break;
1819
		}
1820
		for ( unsigned int i = 0; i < text->count(); i++ ) {
1821
			int iLang = text->language(i);
1822
			if ( !text->getBefore(iLang).empty() ) values += textStart + "Before: " + (long)iLang + "|" + text->getBefore(iLang) + "\n";
1823
			if ( !text->getAfter(iLang).empty() ) values += textStart + "After: " + (long)iLang + "|" + text->getAfter(iLang) + "\n";
1824
		}
6 cycrow 1825
	}
1826
 
46 cycrow 1827
	values += Utils::String("GameChanging: ") + (long)gameChanging() + "\n";
1828
	values += Utils::String("EaseOfUse: ") + (long)easeOfUse() + "\n";
1829
	values += Utils::String("Recommended: ") + (long)recommended() + "\n";
6 cycrow 1830
 
14 cycrow 1831
	for ( CListNode<SNames> *nNode = m_lNames.Front(); nNode; nNode = nNode->next() )
46 cycrow 1832
		values += Utils::String("ScriptName: ") + (long)nNode->Data()->iLanguage + ":" + nNode->Data()->sName + "\n";
6 cycrow 1833
 
14 cycrow 1834
	for ( CListNode<SNeededLibrary> *libNode = m_lNeededLibrarys.Front(); libNode; libNode = libNode->next() ) {
6 cycrow 1835
		SNeededLibrary *l = libNode->Data();
14 cycrow 1836
		values += (CyString("NeededLibrary: ") + l->sName + "||" + l->sAuthor + "||" + l->sMinVersion + "\n").ToString();
6 cycrow 1837
	}
1838
 
1839
	for ( SStringList *fpbNode = m_lFakePatchBefore.Head(); fpbNode; fpbNode = fpbNode->next )
14 cycrow 1840
		values += (CyString("FakePatchBefore: ") + fpbNode->str + "||" + fpbNode->data + "\n").ToString();
6 cycrow 1841
	for ( SStringList *fpaNode = m_lFakePatchAfter.Head(); fpaNode; fpaNode = fpaNode->next )
14 cycrow 1842
		values += (CyString("FakePatchAfter: ") + fpaNode->str + "||" + fpaNode->data + "\n").ToString();
6 cycrow 1843
 
48 cycrow 1844
	values += Utils::String("PluginType: ") + (long)this->pluginType() + "\n";
6 cycrow 1845
 
1846
	return values;
1847
}
1848
 
1849
 
1850
/*
1851
	Func:   WriteFile
1852
	Input:  filename - The filename of the spk file to write to
1853
	Desc:   Writes the data to an spk file
1854
*/
1855
bool CBaseFile::WriteFile ( CyString filename, CProgressInfo *progress )
1856
{
1857
	FILE *id = fopen ( filename.c_str(), "wb" );
1858
	if ( !id )
1859
		return false;
1860
 
1861
	bool ret = WriteData ( id, progress );
1862
	fclose ( id );
1863
 
1864
	return ret;
1865
}
1866
 
1867
bool CBaseFile::WriteHeader(FILE *id, int valueheader, int valueComprLen)
1868
{
1869
	fprintf ( id, "BaseCycrow;%.2f;%d;%d\n", FILEVERSION, valueheader, valueComprLen );
1870
	if ( ferror(id) )
1871
		return false;
1872
	return true;
1873
}
1874
 
1875
bool CBaseFile::WriteData ( FILE *id, CProgressInfo *progress )
1876
{
1877
	int valueheader = m_SHeader.iValueCompression, fileheader = m_SHeader.iValueCompression;
1878
	if ( valueheader == SPKCOMPRESS_7ZIP )
1879
		valueheader = SPKCOMPRESS_ZLIB;
1880
	if ( fileheader == SPKCOMPRESS_7ZIP )
1881
		fileheader = SPKCOMPRESS_ZLIB;
1882
 
1883
	// get the script values
1884
	this->UpdateSigned(true);
1885
	CyString values = this->CreateValuesLine();
1886
 
1887
	// compress the values
1888
	int valueUncomprLen = (int)values.Length();
1889
	unsigned long valueComprLen = 0;
1890
	unsigned char *valueCompr = NULL;
1891
	bool compressed = false;
1892
	if ( valueheader == SPKCOMPRESS_ZLIB )
1893
	{
1894
		valueComprLen = valueUncomprLen;
1895
		if ( valueComprLen < 100 )
1896
			valueComprLen = 200;
1897
		else if ( valueComprLen < 1000 )
1898
			valueComprLen *= 2;
1899
 
1900
		valueCompr = (unsigned char *)calloc((unsigned int)valueComprLen, 1);
1901
		int err = compress ( (unsigned char *)valueCompr, &valueComprLen, (const unsigned char *)values.c_str(), (unsigned long)values.Length(), 0 );
1902
		if ( err == Z_OK )
1903
			compressed = true;
1904
	}
1905
 
1906
	if ( !compressed )
1907
	{
1908
		valueComprLen = valueUncomprLen;
1909
		valueCompr = (unsigned char *)calloc((unsigned int)valueComprLen, 1);
1910
		memcpy ( valueCompr, values.c_str(), valueComprLen );
1911
		valueheader = SPKCOMPRESS_NONE;
1912
	}
1913
 
1914
	// write the main header to the file
1915
	if ( !this->WriteHeader(id, valueheader, valueComprLen) )
1916
		return false;
1917
 
1918
	// write the compressed data to file
1919
	fputc ( (unsigned char)(valueUncomprLen >> 24), id );
1920
	fputc ( (unsigned char)(valueUncomprLen >> 16), id );
1921
	fputc ( (unsigned char)(valueUncomprLen >> 8), id );
1922
	fputc ( (unsigned char)valueUncomprLen, id );
1923
	fwrite ( valueCompr, sizeof(char), valueComprLen, id );
1924
 
1925
	free ( valueCompr );
1926
 
1927
	// now compress the files header
1928
	// create the files values
1929
	CyString files = CreateFilesLine ( true, progress );
1930
 
1931
	// compress the files values
1932
	long fileUncomprLen = (long)files.Length(), fileComprLen = fileUncomprLen;
1933
	unsigned char *fileCompr = NULL;
1934
 
1935
	compressed = false;
1936
	if ( fileUncomprLen )
1937
	{
1938
		if ( fileheader == SPKCOMPRESS_ZLIB )
1939
		{
1940
			if ( fileComprLen < 100 )
1941
				fileComprLen = 200;
1942
			else if ( fileComprLen < 1000 )
1943
				fileComprLen *= 2;
1944
			fileCompr = (unsigned char *)calloc((unsigned int)fileComprLen, 1);
1945
			int err = compress ( (unsigned char *)fileCompr, (unsigned long *)&fileComprLen, (const unsigned char *)files.c_str(), (unsigned long)files.Length(), 0 );
1946
			if ( err == Z_OK )
1947
				compressed = true;
1948
		}
1949
	}
1950
 
1951
	// if unable to compress, store it as plain text
1952
	if ( !compressed )
1953
	{
1954
		fileComprLen = fileUncomprLen;
1955
		fileCompr = (unsigned char *)calloc((unsigned int)fileComprLen, 1);
1956
		memcpy ( fileCompr, files.c_str(), fileComprLen );
1957
		fileheader = SPKCOMPRESS_NONE;
1958
	}
1959
 
1960
	// now write the file header
1961
	m_SHeader2.lSize = fileComprLen;
1962
	fprintf ( id, "FileHeader;%d;%ld;%ld;%d;%d\n", m_SHeader2.iNumFiles, m_SHeader2.lSize, m_SHeader2.lFullSize, fileheader, m_SHeader2.iDataCompression );
1963
 
1964
	fputc ( (unsigned char)(fileUncomprLen >> 24), id );
1965
	fputc ( (unsigned char)(fileUncomprLen >> 16), id );
1966
	fputc ( (unsigned char)(fileUncomprLen >> 8), id );
1967
	fputc ( (unsigned char)fileUncomprLen, id );
1968
	fwrite ( fileCompr, sizeof(char), fileComprLen, id );
1969
 
1970
	free ( fileCompr );
1971
 
1972
	if ( progress )
1973
	{
1974
		progress->UpdateStatus(STATUS_WRITE);
1975
		progress->SetDone(0);
1976
		long max = 0;
1977
		for ( C_File *file = m_lFiles.First(); file; file = m_lFiles.Next() )
1978
			max += file->GetDataSize();
1979
		if ( m_pIconFile )
1980
			max += m_pIconFile->GetDataSize();
1981
		progress->SetMax(max);
1982
	}
1983
 
1984
	// now finally, write all the file data
1985
	if ( m_pIconFile )
1986
	{
1987
		if ( progress )
1988
			progress->UpdateFile(m_pIconFile);
1989
		fputc ( (unsigned char)(m_pIconFile->GetUncompressedDataSize() >> 24), id );
1990
		fputc ( (unsigned char)(m_pIconFile->GetUncompressedDataSize() >> 16), id );
1991
		fputc ( (unsigned char)(m_pIconFile->GetUncompressedDataSize() >> 8), id );
1992
		fputc ( (unsigned char)m_pIconFile->GetUncompressedDataSize(), id );
1993
		fwrite ( m_pIconFile->GetData(), sizeof(char), m_pIconFile->GetDataSize(), id );
1994
		if ( progress )
1995
			progress->IncDone(m_pIconFile->GetDataSize());
1996
	}
1997
 
1998
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
1999
	{
2000
		C_File *file = node->Data();
2001
		if ( progress )
2002
			progress->UpdateFile(file);
2003
		fputc ( (unsigned char)(file->GetUncompressedDataSize() >> 24), id );
2004
		fputc ( (unsigned char)(file->GetUncompressedDataSize() >> 16), id );
2005
		fputc ( (unsigned char)(file->GetUncompressedDataSize() >> 8), id );
2006
		fputc ( (unsigned char)file->GetUncompressedDataSize(), id );
2007
		unsigned char *data = file->GetData();
2008
		size_t remaining = file->GetDataSize();
2009
		while ( remaining )
2010
		{
2011
			size_t writeSize = WRITECHUNK;
2012
			if ( writeSize > remaining )
2013
				writeSize = remaining;
2014
 
2015
			size_t written = fwrite ( data, sizeof(char), writeSize, id );
2016
			data += written;
2017
			remaining -= written;
2018
 
2019
			if ( progress )
2020
				progress->IncDone((int)written);
2021
		}
2022
 
2023
	}
2024
 
2025
	m_bChanged = false;
2026
 
2027
	return true;
2028
}
2029
 
2030
 
2031
 
2032
 
2033
 
2034
bool CBaseFile::ExtractFile ( C_File *file, CyString dir, bool includedir, CProgressInfo *progress )
2035
{
2036
	if ( ReadFileToMemory ( file ) )
2037
	{
2038
		// now finally, uncompress the file
2039
		long len = 0;
2040
		unsigned char *data = file->UncompressData ( &len, progress );
2041
		if ( !data )
2042
		{
2043
			// attempt a file decompress
2044
			if ( file->GetCompressionType() == SPKCOMPRESS_7ZIP )
2045
			{
2046
				if ( file->UncompressToFile ( dir, this, includedir, progress ) )
2047
					return true;
2048
			}
2049
			return false;
2050
		}
2051
 
2052
		if ( !file->WriteToDir ( dir, this, includedir, NullString, data, len ) )
2053
			return false;
2054
 
2055
		return true;
2056
 
2057
	}
2058
	else
2059
		return false;
2060
}
2061
 
2062
bool CBaseFile::ExtractFile ( int filenum, CyString dir, bool includedir, CProgressInfo *progress )
2063
{
2064
	// invalid valus
2065
	if ( filenum < 0 )
2066
		return false;
2067
	// out of range
2068
	if ( filenum > m_lFiles.size() )
2069
		return false;
2070
 
2071
	// get the file pointer
2072
	C_File *file = m_lFiles.Get ( filenum );
2073
	return ExtractFile ( file, dir, includedir, progress );
2074
}
2075
 
2076
 
2077
 
2078
bool CBaseFile::ExtractAll ( CyString dir, int game, bool includedir, CProgressInfo *progress )
2079
{
2080
	// no file to read from
2081
	if ( m_sFilename.Empty() )
2082
		return false;
2083
 
2084
	// now open the file
2085
	FILE *id = fopen ( m_sFilename.c_str(), "rb" );
2086
	if ( !id )
2087
		return false;
2088
 
2089
	fseek ( id, 0, SEEK_SET );
2090
	// read the header
2091
	GetEndOfLine ( id, NULL, false );
2092
	// skip past values
2093
	fseek ( id, m_SHeader.lValueCompressSize, SEEK_CUR );
2094
 
2095
	// read the next header
2096
	GetEndOfLine ( id, NULL, false );
2097
	// skip past files
2098
	fseek ( id, 4, SEEK_CUR );
2099
	fseek ( id, m_SHeader2.lSize, SEEK_CUR );
2100
 
2101
	// now were in the file section
2102
	// skip past each one
2103
	if ( m_pIconFile )
2104
	{
2105
		fseek ( id, 4, SEEK_CUR );
2106
		fseek ( id, m_pIconFile->GetDataSize (), SEEK_CUR );
2107
	}
2108
 
2109
	CDirIO Dir(dir);
2110
 
2111
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2112
	{
2113
		C_File *fit = node->Data();
2114
 
2115
		if ( progress )
2116
			progress->UpdateFile ( fit );
2117
 
2118
		if ( (!fit->GetDataSize ()) || (!fit->GetData()) )
2119
		{
2120
			if ( !fit->ReadFromFile ( id, fit->GetDataSize() ) )
2121
			{
2122
				fclose ( id );
2123
				return false;
2124
			}
2125
		}
2126
		else
2127
			fseek ( id, fit->GetDataSize(), SEEK_CUR );
2128
 
2129
		if ( game ) {
2130
			if ( fit->GetGame() && fit->GetGame() != game )
2131
				continue;
2132
		}
2133
 
2134
		// create directory first
2135
		Dir.Create(fit->GetDirectory(this));
2136
 
2137
		long size = 0;
2138
		unsigned char *data = fit->UncompressData (&size, progress);
2139
		if ( (!data) && (fit->GetCompressionType() == SPKCOMPRESS_7ZIP) )
2140
		{
2141
			if ( !fit->UncompressToFile ( dir, this, includedir, progress ) )
2142
			{
2143
				fclose ( id );
2144
				return false;
2145
			}
2146
		}
2147
		else if ( (!data) || (!fit->WriteToDir ( dir, this, includedir, NullString, data, size )) )
2148
		{
2149
			fclose ( id );
2150
			return false;
2151
		}
2152
	}
2153
 
2154
	fclose ( id );
2155
 
2156
	return true;
2157
}
2158
 
2159
 
2160
 
2161
 
2162
bool CBaseFile::UpdateSigned (bool updateFiles)
2163
{
2164
	m_bSigned = true;
2165
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2166
	{
2167
		C_File *file = node->Data();
2168
		if ( updateFiles )
2169
			file->UpdateSigned();
2170
		// extra, text, soundtrack, readmes and screen files do not require a modified game directly
2171
		if ( (file->GetFileType() == FILETYPE_EXTRA) || (file->GetFileType() == FILETYPE_TEXT) || (file->GetFileType() == FILETYPE_SOUND) || (file->GetFileType() == FILETYPE_SCREEN) || (file->GetFileType() == FILETYPE_README) )
2172
			continue;
2173
		// mods and maps always need modified game
2174
		else if ( (file->GetFileType() == FILETYPE_MOD) || (file->GetFileType() == FILETYPE_MAP) )
2175
		{
2176
			m_bSigned = false;
2177
			break;
2178
		}
2179
 
2180
		// else should be a script file, script or uninstall type
2181
		// all scripts must be signed, if any are not, then no signed status
2182
		if ( !file->IsSigned () )
2183
		{
2184
			m_bSigned = false;
2185
			break;
2186
		}
2187
	}
2188
 
2189
	return m_bSigned;
2190
}
2191
 
2192
 
46 cycrow 2193
bool CBaseFile::IsPackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2194
{
2195
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2196
	{
2197
		SNeededLibrary *l = node->Data();
2198
		if ( l->sName.Compare(scriptName) && l->sAuthor.Compare(author) )
2199
			return true;
2200
	}
2201
 
2202
	return false;
2203
}
2204
 
46 cycrow 2205
SNeededLibrary *CBaseFile::FindPackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2206
{
2207
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2208
	{
2209
		SNeededLibrary *l = node->Data();
2210
		if ( l->sName.Compare(scriptName) && l->sAuthor.Compare(author) )
2211
			return l;
2212
	}
2213
 
2214
	return NULL;
2215
}
2216
 
2217
void CBaseFile::RemoveFakePatchOrder(CyString scriptName, CyString author)
2218
{
2219
	RemoveFakePatchOrder(true, scriptName, author);
2220
	RemoveFakePatchOrder(false, scriptName, author);
2221
}
2222
void CBaseFile::RemoveFakePatchOrder(bool after, CyString scriptName, CyString author)
2223
{
2224
	CyStringList *list;
2225
	if ( after )
2226
		list = &m_lFakePatchAfter;
2227
	else
2228
		list = &m_lFakePatchBefore;
2229
 
2230
	for ( SStringList *str = list->Head(); str; str = str->next )
2231
	{
2232
		// already added
2233
		if ( str->str.Compare(scriptName) && str->data.Compare(author) )
2234
			str->remove = true;
2235
	}
2236
 
2237
	list->RemoveMarked();
2238
}
2239
 
2240
void CBaseFile::AddFakePatchOrder(bool after, CyString scriptName, CyString author)
2241
{
2242
	CyStringList *list;
2243
	if ( after )
2244
		list = &m_lFakePatchAfter;
2245
	else
2246
		list = &m_lFakePatchBefore;
2247
 
2248
	// check if the package already exists
2249
	for ( SStringList *str = list->Head(); str; str = str->next )
2250
	{
2251
		// already added
2252
		if ( str->str.Compare(scriptName) && str->data.Compare(author) )
2253
			return;
2254
	}
2255
 
2256
	// cant have it on both list
2257
	RemoveFakePatchOrder(!after, scriptName, author);
2258
 
2259
	list->PushBack(scriptName, author);
2260
}
46 cycrow 2261
void CBaseFile::AddNeededLibrary(const Utils::String &scriptName, const Utils::String &author, const Utils::String &minVersion)
6 cycrow 2262
{
2263
	SNeededLibrary *l = this->FindPackageNeeded(scriptName, author);
2264
	if ( !l )
2265
	{
2266
		l = new SNeededLibrary;
2267
		l->sName = scriptName;
2268
		l->sAuthor = author;
2269
		m_lNeededLibrarys.push_back(l);
2270
	}
2271
	l->sMinVersion = minVersion;
2272
}
2273
 
46 cycrow 2274
void CBaseFile::RemovePackageNeeded(const Utils::String &scriptName, const Utils::String &author)
6 cycrow 2275
{
2276
	SNeededLibrary *l = this->FindPackageNeeded(scriptName, author);
2277
	if ( l )
2278
	{
2279
		m_lNeededLibrarys.remove(l);
2280
		delete l;
2281
	}
2282
}
2283
 
2284
void CBaseFile::ClearNeededPackages()
2285
{
2286
	SNeededLibrary *ns = this->FindPackageNeeded("<package>", "<author>");
46 cycrow 2287
	Utils::String version;
2288
	if ( ns ) version = ns->sMinVersion;
2289
 
6 cycrow 2290
	for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2291
		node->DeleteData();
2292
	m_lNeededLibrarys.clear();
2293
 
46 cycrow 2294
	if ( !version.empty() )	this->AddNeededLibrary("<package>", "<author>", version);
6 cycrow 2295
}
2296
 
2297
 
2298
bool CBaseFile::GeneratePackagerScript(bool wildcard, CyStringList *list, bool datafile)
2299
{
2300
	list->PushBack("#");
2301
	list->PushBack(CyString("# Packager Script"));
2302
	list->PushBack(CyString("# -- Generated by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2) + " --");
2303
	list->PushBack("#");
2304
	list->PushBack("");
2305
	if ( !datafile )
2306
	{
2307
		list->PushBack("# Variable for your game directory, where to get files from");
2308
		list->PushBack("# $PATH variable is used to get the current path");
2309
		list->PushBack("Variable: $GAMEDIR $PATH");
2310
		list->PushBack("");
2311
	}
2312
	list->PushBack("# The name of the script");
2313
	list->PushBack(CyString("Name: ") + m_sName);
2314
	list->PushBack("");
2315
	list->PushBack("# The author of the script, ie, you");
2316
	list->PushBack(CyString("Author: ") + m_sAuthor);
2317
	list->PushBack("");
2318
	list->PushBack("# The creation data, when it was created");
2319
	if ( datafile )
2320
		list->PushBack(CyString("Date: ") + m_sCreationDate);
2321
	else
2322
	{
2323
		list->PushBack("# $DATE variable is used to get the current date");
2324
		list->PushBack("Date: $DATE");
2325
	}
2326
	list->PushBack("");
2327
	list->PushBack("# The version of script");
2328
	if ( datafile )
2329
		list->PushBack(CyString("Version: ") + m_sVersion);
2330
	else
2331
	{
2332
		list->PushBack("# $ASK variable is used to get an input when creating");
2333
		list->PushBack("Version: $ASK");
2334
	}
2335
	list->PushBack("");
2336
 
2337
	if ( !m_lGames.empty() ) {
2338
		list->PushBack("# The game version the script is for <game> <version> (can have multiple games)");
2339
		for ( SGameCompat *g = m_lGames.First(); g; g = m_lGames.Next() ) {
13 cycrow 2340
			Utils::String game = CBaseFile::ConvertGameToString(g->iGame);
6 cycrow 2341
 
46 cycrow 2342
			if ( !g->sVersion.empty() )
6 cycrow 2343
			{
2344
				game += " ";
46 cycrow 2345
				game += g->sVersion;
6 cycrow 2346
			}
2347
			else
2348
			{
2349
				game += " ";
2350
				game += (long)g->iVersion;
2351
			}
2352
 
2353
			list->PushBack(CyString("Game: ") + game);
2354
		}
2355
 
2356
		list->PushBack("");
2357
	}
2358
 
48 cycrow 2359
	if ( !this->description().empty() ) {
6 cycrow 2360
		list->PushBack("# The description of the script, displays when installing");
48 cycrow 2361
		list->PushBack(CyString("Description: ") + this->description());
6 cycrow 2362
		list->PushBack("");
2363
	}
2364
 
49 cycrow 2365
	if ( !this->webSite().empty() ) {
6 cycrow 2366
		list->PushBack("# A link to the website for the script, ie for an online help page");
49 cycrow 2367
		list->PushBack(CyString("WebSite: ") + this->webSite());
6 cycrow 2368
		list->PushBack("");
2369
	}
2370
 
49 cycrow 2371
	if ( !this->forumLink().empty() ) {
6 cycrow 2372
		list->PushBack("# A direct link to the thread in the egosoft forum");
49 cycrow 2373
		list->PushBack(CyString("ForumLink: ") + this->forumLink());
6 cycrow 2374
		list->PushBack("");
2375
	}
2376
 
49 cycrow 2377
	if ( !this->webAddress().empty() ) {
6 cycrow 2378
		list->PushBack("# A link to the address for the update file");
49 cycrow 2379
		list->PushBack(CyString("WebAddress: ") + this->webAddress());
6 cycrow 2380
		list->PushBack("");
2381
	}
2382
 
49 cycrow 2383
	if ( !this->email().empty() ) {
6 cycrow 2384
		list->PushBack("# The email address of the author, to allow users to contract if needed");
49 cycrow 2385
		list->PushBack(CyString("Email: ") + this->email());
6 cycrow 2386
		list->PushBack("");
2387
	}
2388
 
2389
	if ( m_lMirrors.Count() )
2390
	{
2391
		list->PushBack("# A link to the mirror address for the update file, can have many of these");
2392
		for ( SStringList *node = m_lMirrors.Head(); node; node = node->next )
2393
			list->PushBack(CyString("WebMirror: ") + node->str);
2394
		list->PushBack("");
2395
	}
2396
 
2397
	if ( m_bAutoGenerateUpdateFile )
2398
	{
2399
		list->PushBack("# Auto generate the package update file when created");
2400
		list->PushBack("GenerateUpdateFile");
2401
	}
2402
 
2403
	if ( m_lNeededLibrarys.size() )
2404
	{
2405
		list->PushBack("# Needed Library dependacies, require these to be installed");
2406
		for ( CListNode<SNeededLibrary> *node = m_lNeededLibrarys.Front(); node; node = node->next() )
2407
			list->PushBack(CyString("Depend: ") + node->Data()->sName + "|" + node->Data()->sMinVersion + "|" + node->Data()->sAuthor);
2408
 
2409
		list->PushBack("");
2410
	}
2411
 
46 cycrow 2412
	if ( !_noRatings() )
6 cycrow 2413
	{
2414
		list->PushBack("# Ratings Values, 0 to 5, <ease> <changing> <recommended>");
46 cycrow 2415
		list->PushBack(CyString("Ratings: ") + (long)easeOfUse() + " " + (long)gameChanging() + " " + (long)recommended());
6 cycrow 2416
		list->PushBack("");
2417
	}
2418
 
2419
	if ( m_lNames.size() )
2420
	{
2421
		list->PushBack("# Package names, uses different names for different languages");
2422
		for ( CListNode<SNames> *node = m_lNames.Front(); node; node = node->next() )
2423
			list->PushBack(CyString("ScriptName: ") + (long)node->Data()->iLanguage + " " + node->Data()->sName);
2424
		list->PushBack("");
2425
	}
2426
 
46 cycrow 2427
	for ( int j = 0; j < 2; j++ ) {
2428
		Utils::String installText = (j == 0) ? "Install" : "Uninstall";
2429
		const CInstallText *pText = (j == 0) ? this->installText() : this->uninstallText();
2430
		if ( pText->any() )
6 cycrow 2431
		{
46 cycrow 2432
			list->PushBack(CyString("# " + installText + " Texts, display text before and/or after " + installText + "ing to inform the use of special conditions"));
2433
			for ( unsigned int i = 0; i < pText->count(); i++ ) {
2434
				long iLang = pText->language(i);
2435
				if ( !pText->getBefore(iLang).empty() )	list->PushBack(CyString(installText + "Before: ") + iLang + " " + pText->getBefore(iLang));
2436
				if ( !pText->getAfter(iLang).empty()  )	list->PushBack(CyString(installText + "After: ") + iLang + " " + pText->getAfter(iLang));
2437
			}
2438
			list->PushBack("");
6 cycrow 2439
		}
2440
	}
2441
 
2442
	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 2443
	switch ( this->pluginType() )
6 cycrow 2444
	{
2445
		case PLUGIN_NORMAL:
2446
			list->PushBack("PluginType: Normal");
2447
			break;
2448
		case PLUGIN_STABLE:
2449
			list->PushBack("PluginType: Stable");
2450
			break;
2451
		case PLUGIN_EXPERIMENTAL:
2452
			list->PushBack("PluginType: Experimental");
2453
			break;
2454
		case PLUGIN_CHEAT:
2455
			list->PushBack("PluginType: Cheat");
2456
			break;
2457
		case PLUGIN_MOD:
2458
			list->PushBack("PluginType: Mod");
2459
			break;
2460
	}
2461
	list->PushBack("");
2462
 
2463
	return true;
2464
}
2465
 
2466
bool CBaseFile::GeneratePackagerScriptFile(bool wildcard, CyStringList *list)
2467
{
2468
	// now do files and wildcards
2469
	CyStringList files;
2470
	for ( CListNode<C_File> *f = m_lFiles.Front(); f; f = f->next() )
2471
	{
2472
		CyString name = "$GAMEDIR/";
2473
 
2474
		bool done = false;
2475
		if ( wildcard )
2476
		{
2477
			CyString base = f->Data()->GetBaseName();
2478
			if ( f->Data()->GetFileType() == FILETYPE_SCRIPT )
2479
			{
2480
				if ( base.GetToken(".", 1, 1).Compare("plugin") || base.GetToken(".", 1, 1).Compare("lib") )
2481
				{
2482
					name += f->Data()->GetDirectory(this) + "/" + base.GetToken(".", 1, 2) + ".*";
2483
					done = true;
2484
				}
2485
				else if ( base.GetToken(".", 1, 1).Compare("al") && !base.GetToken(".", 2, 2).Compare("plugin") )
2486
				{
2487
					name += f->Data()->GetDirectory(this) + "/" + base.GetToken(".", 1, 2) + ".*";
2488
					done = true;
2489
				}
2490
			}
2491
			else if ( f->Data()->GetFileType() == FILETYPE_TEXT )
2492
			{
2493
				if ( base.IsIn("-L") )
2494
				{
2495
					name += f->Data()->GetDirectory(this) + "/" + base.GetToken("-L", 1, 1) + "-L*";
2496
					done = true;
2497
				}
2498
				else
2499
				{
2500
					name += f->Data()->GetDirectory(this) + "/*" + base.Right(4) + ".*";
2501
					done = true;
2502
				}
2503
			}
2504
		}
2505
 
2506
		if ( !done )
2507
			name += f->Data()->GetNameDirectory(this);
2508
 
2509
		if ( !f->Data()->GetDir().Empty() )
2510
		{
2511
			name += "|";
2512
			name += f->Data()->GetDir();
2513
		}
2514
		files.PushBack(CyString("GAME ") + CBaseFile::ConvertGameToString(f->Data()->GetGame()) + " " + name, f->Data()->GetFileTypeString(), true);
2515
	}
2516
 
2517
 
2518
	if ( !files.Empty() )
2519
	{
2520
		list->PushBack("# Files List, all the files to add, can include wild cards");
2521
		for ( SStringList *node = files.Head(); node; node = node->next )
2522
			list->PushBack(node->data + ": " + node->str);
2523
		list->PushBack("");
2524
	}
2525
 
2526
	return true;
2527
}
2528
 
2529
CyString CBaseFile::GetAutosaveName()
2530
{
2531
	CyString cdate = m_sCreationDate;
2532
	cdate = cdate.FindReplace("/", ".");
2533
	CyString name = m_sName + "-V" + m_sVersion + "-" + cdate;
2534
 
2535
	return name;
2536
}
2537
 
2538
bool CBaseFile::CheckGameCompatability(int game)
2539
{
2540
	if ( m_lGames.empty() )
2541
		return true; // no game compatability added, assume its ok for all
2542
 
2543
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2544
		if ( node->Data()->iGame == 0 || node->Data()->iGame == game )
2545
			return true;
2546
	}
2547
	return false;
2548
}
2549
 
2550
bool CBaseFile::CheckGameVersionCompatability(int game, CyString sVersion, int iVersion)
2551
{
2552
	if ( m_lGames.empty() )
2553
		return true; // no game compatability added, assume its ok for all
2554
 
2555
	bool foundAll = false;
2556
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2557
		if ( node->Data()->iGame == 0 )
2558
			foundAll = true;
2559
		else if ( node->Data()->iGame == game ) { // now check the version
46 cycrow 2560
			if ( node->Data()->sVersion.empty() ) {
2561
				if ( CyString(node->Data()->sVersion).CompareVersion(sVersion) == COMPARE_OLDER )
6 cycrow 2562
					return false;
2563
				return true;
2564
			}
2565
			else {
2566
				if ( node->Data()->iVersion > iVersion )
2567
					return false;
2568
				return true;
2569
			}
2570
		}
2571
	}
2572
	return foundAll;
2573
}
2574
 
2575
bool CBaseFile::RemoveGameCompatability(int game)
2576
{
2577
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2578
		if ( node->Data()->iGame == game ) {
2579
			m_lGames.remove(node);
2580
			m_bChanged = true;
2581
			return true;
2582
		}
2583
	}
2584
 
2585
	return false;
2586
}
2587
 
2588
SGameCompat *CBaseFile::GetGameCompatability(int game)
2589
{
2590
	for ( CListNode<SGameCompat> *node = m_lGames.Front(); node; node = node->next() ) {
2591
		if ( node->Data()->iGame == game ) {
2592
			return node->Data();
2593
		}
2594
	}
2595
 
2596
	return NULL;
2597
}
2598
 
46 cycrow 2599
void CBaseFile::AddGameCompatability(int game, const Utils::String &version)
6 cycrow 2600
{
2601
	// first check if we already have it on the list
2602
	SGameCompat *Found = this->GetGameCompatability(game);
2603
	if ( !Found ) {
2604
		Found = new SGameCompat;
2605
		m_lGames.push_back(Found);
2606
	}
2607
 
2608
	Found->iGame = game;
2609
	Found->iVersion = -1;
46 cycrow 2610
	Found->sVersion = "";
6 cycrow 2611
 
46 cycrow 2612
	if ( version.isin(".") || !version.isNumber() )
6 cycrow 2613
		Found->sVersion = version;
2614
	else
46 cycrow 2615
		Found->iVersion = version;
6 cycrow 2616
	m_bChanged = true;
2617
}
2618
 
14 cycrow 2619
bool CBaseFile::LoadPackageData(const Utils::String &sFirst, const Utils::String &sRest)
6 cycrow 2620
{
14 cycrow 2621
	if ( sFirst.Compare("Name") )
2622
		m_sName = sRest;
2623
	else if ( sFirst.Compare("Author") )
2624
		m_sAuthor = sRest;
2625
	else if ( sFirst.Compare("ScriptName") )
2626
		AddLanguageName(ParseLanguage(sRest.token(" ", 1)), sRest.tokens(" ", 2));
46 cycrow 2627
	else if ( sFirst.Compare("UninstallBefore") )	this->addUninstallText(ParseLanguage(sRest.token(" ", 1)), true, sRest.tokens(" ", 2));
2628
	else if ( sFirst.Compare("UninstallAfter") )	this->addUninstallText(ParseLanguage(sRest.token(" ", 1)), false, sRest.tokens(" ", 2));
2629
	else if ( sFirst.Compare("InstallBefore") )		this->addInstallText(ParseLanguage(sRest.token(" ", 1)), true, sRest.tokens(" ", 2));
2630
	else if ( sFirst.Compare("InstallAfter") )		this->addInstallText(ParseLanguage(sRest.token(" ", 1)), false, sRest.tokens(" ", 2));
14 cycrow 2631
	else if ( sFirst.Compare("Date") )
2632
		m_sCreationDate = sRest;
2633
	else if ( sFirst.Compare("Version") )
2634
		m_sVersion = sRest;
6 cycrow 2635
	// old version
14 cycrow 2636
	else if ( sFirst.Compare("GameVersion") )
2637
		this->AddGameCompatability(-1, sRest);
2638
	else if ( sFirst.Compare("PluginType") )
6 cycrow 2639
	{
48 cycrow 2640
		if ( sRest.isNumber() )						this->setPluginType(sRest);
2641
		else if ( sRest.Compare("Normal") )			this->setPluginType(PLUGIN_NORMAL);
2642
		else if ( sRest.Compare("Stable") )			this->setPluginType(PLUGIN_STABLE);
2643
		else if ( sRest.Compare("Experimental") )	this->setPluginType(PLUGIN_EXPERIMENTAL);
2644
		else if ( sRest.Compare("Cheat") )			this->setPluginType(PLUGIN_CHEAT);
2645
		else if ( sRest.Compare("Mod") )			this->setPluginType(PLUGIN_MOD);
6 cycrow 2646
	}
2647
	// new version
14 cycrow 2648
	else if ( sFirst.Compare("GenerateUpdateFile") )
6 cycrow 2649
		m_bAutoGenerateUpdateFile = true;
14 cycrow 2650
	else if ( sFirst.Compare("Game") )
6 cycrow 2651
	{
14 cycrow 2652
		Utils::String sGame = sRest.token(" ", 1);
2653
		this->AddGameCompatability(CBaseFile::GetGameFromString(sGame), sRest.token(" ", 2));
6 cycrow 2654
	}
48 cycrow 2655
	else if ( sFirst.Compare("Description") )		this->setDescription(sRest);
14 cycrow 2656
	else if ( sFirst.Compare("AutoSave") || sFirst.Compare("AutoExport") || sFirst.Compare("AutoRarExport") || sFirst.Compare("AutoZipExport") )
6 cycrow 2657
	{
14 cycrow 2658
		CyString filename = sRest;
6 cycrow 2659
		CyString name = m_sName;
2660
		CyString author = m_sAuthor;
2661
		CyString cdate = m_sCreationDate;
2662
		cdate = cdate.FindReplace("/", ".").Remove(" ");
2663
		if ( filename.IsIn("$AUTOSAVE") )
2664
		{
48 cycrow 2665
			if ( this->GetType() )
6 cycrow 2666
				filename.FindReplace("$AUTOSAVE", "$NAME-V$VERSION-$CDATE.xsp");
2667
			else
2668
				filename.FindReplace("$AUTOSAVE", "$NAME-V$VERSION-$CDATE.spk");
2669
		}
2670
		if ( filename.IsIn("$NAME") )
2671
			filename.FindReplace("$NAME", name.Remove(" "));
2672
		if ( filename.IsIn("$AUTHOR") )
2673
			filename.FindReplace("$AUTHOR", author.Remove(" "));
2674
		if ( filename.IsIn("$DATE") )
2675
			filename.FindReplace("$DATE", cdate);
2676
		if ( filename.IsIn("$CDATE") )
2677
			filename.FindReplace("$CDATE", cdate);
2678
		if ( filename.IsIn("$VERSION") )
2679
			filename.FindReplace("$VERSION", m_sVersion);
2680
 
14 cycrow 2681
		if ( sFirst.Compare("AutoZipExport") || sFirst.Compare("AutoExport") )
6 cycrow 2682
			m_sExportFilename = CFileIO(filename).ChangeFileExtension("zip");
14 cycrow 2683
		else if ( sFirst.Compare("AutoRarExport") )
6 cycrow 2684
			m_sExportFilename = CFileIO(filename).ChangeFileExtension("rar");
2685
		else
2686
			m_sFilename = filename;
2687
	}
49 cycrow 2688
	else if ( sFirst.Compare("WebSite") )		this->setWebSite(sRest);
2689
	else if ( sFirst.Compare("ForumLink") || sFirst.Compare("Forum") ) this->setForumLink(sRest);
2690
	else if ( sFirst.Compare("Email") )			this->setEmail(sRest);
2691
	else if ( sFirst.Compare("WebAddress") )	this->setWebAddress(sRest);
14 cycrow 2692
	else if ( sFirst.Compare("WebMirror") )
2693
		this->AddWebMirror(sRest);
2694
	else if ( sFirst.Compare("WebMirror1") )
2695
		this->AddWebMirror(sRest);
2696
	else if ( sFirst.Compare("WebMirror2") )
2697
		this->AddWebMirror(sRest);
2698
	else if ( sFirst.Compare("Ftp") )
2699
		m_sFtpAddr = sRest;
46 cycrow 2700
	else if ( sFirst.Compare("Ratings") )		_setRatings(sRest.token(" ", 1), sRest.token(" ", 2), sRest.token(" ", 3));
2701
	else if ( sFirst.Compare("EaseOfUse") )		setEaseOfUse(sRest);
2702
	else if ( sFirst.Compare("GameChanging"))	setGameChanging(sRest);
2703
	else if ( sFirst.Compare("Recommended") )	setRecommended(sRest);
14 cycrow 2704
	else if ( sFirst.Compare("Depend") )
6 cycrow 2705
	{
46 cycrow 2706
		Utils::String version = sRest.token("|", 2);
2707
		Utils::String name = sRest.token("|", 1);
2708
		Utils::String author = sRest.tokens("|", 3);
6 cycrow 2709
 
2710
		this->AddNeededLibrary(name, author, version);
2711
	}
14 cycrow 2712
	else if ( sFirst.Compare("DependPackage") )
6 cycrow 2713
	{
2714
		CPackages p;
14 cycrow 2715
		CBaseFile *spk =  p.OpenPackage(sRest, 0, 0, SPKREAD_VALUES);
6 cycrow 2716
		if ( spk )
2717
		{
46 cycrow 2718
			this->AddNeededLibrary(spk->GetName().ToString(), spk->GetAuthor().ToString(), spk->GetVersion().ToString());
6 cycrow 2719
			delete spk;
2720
		}
2721
	}
14 cycrow 2722
	else if ( sFirst.Compare("Icon") )
6 cycrow 2723
	{
14 cycrow 2724
		C_File *icon = new C_File(sRest.c_str());
6 cycrow 2725
		if ( icon->ReadFromFile() )
14 cycrow 2726
			this->SetIcon(icon, CFileIO(sRest).GetFileExtension());
6 cycrow 2727
	}
2728
	else
2729
	{
14 cycrow 2730
		Utils::String checkType = sFirst;
6 cycrow 2731
		bool shared = false;
14 cycrow 2732
		if ( checkType.left(6).Compare("Shared") )
6 cycrow 2733
		{
14 cycrow 2734
			checkType = sFirst.right(-6);
6 cycrow 2735
			shared = true;
2736
		}
2737
 
2738
		// now check type name
2739
		int filetype = GetFileTypeFromString(checkType);
2740
		if ( filetype != -1 )
14 cycrow 2741
			this->AddFileScript(filetype, shared, sRest);
6 cycrow 2742
		else if ( !checkType.Compare("changelog") )
2743
			return false;
2744
	}
2745
 
2746
	return true;
2747
}
2748
 
2749
void CBaseFile::AddFileScript(int filetype, bool shared, CyString rest)
2750
{
2751
	CyString dir;
2752
	if ( rest.IsIn("|") )
2753
	{
2754
		dir = rest.GetToken("|", 2);
2755
		rest = rest.GetToken("|", 1, 1);
2756
	}
2757
 
2758
	int game = 0;
2759
	if ( rest.GetToken(" ", 1, 1).Left(4).Compare("GAME") ) {
13 cycrow 2760
		game = CBaseFile::GetGameFromString(rest.GetToken(" ", 2, 2).ToString());
6 cycrow 2761
		rest = rest.GetToken(" ", 3);
2762
	}
2763
 
2764
	rest = rest.FindReplace("\\", "/");
2765
 
2766
	// wild cards
2767
	if ( rest.IsAnyIn("*?") )
2768
	{
2769
		CDirIO Dir(CFileIO(rest).GetDir());
2770
		CyStringList *dirList = Dir.DirList();
2771
		if ( dirList )
2772
		{
2773
			for ( SStringList *strNode = dirList->Head(); strNode; strNode = strNode->next )
2774
			{
2775
				CyString file = Dir.File(strNode->str);
2776
				if ( file.WildMatch(rest) )
2777
				{
2778
					C_File *newfile = this->AppendFile(file, filetype, game, dir);
2779
					if ( newfile )
2780
						newfile->SetShared(shared);
2781
				}
2782
			}
2783
			delete dirList;
2784
		}
2785
	}
2786
	else
2787
	{
2788
		C_File *file = this->AppendFile(rest, filetype, game, dir);
2789
		if ( file )
2790
			file->SetShared(shared);
2791
	}
2792
}
2793
 
2794
 
2795
CyString CBaseFile::GetFullFileSizeString() { return SPK::GetSizeString ( this->GetFullFileSize() ); }
2796
 
2797
unsigned char *CBaseFile::CreateData(size_t *size, CProgressInfo *progress)
2798
{
2799
	if ( this->WriteFile("temp.dat", progress) )
2800
	{
2801
		FILE *id = fopen("temp.dat", "rb");
2802
		if ( id )
2803
		{
2804
			fseek(id, 0, SEEK_END);
2805
			*size = ftell(id);
2806
			fseek(id, 0, SEEK_SET);
2807
			unsigned char *data = new unsigned char[*size];
2808
			fread(data, sizeof(unsigned char), *size, id);
2809
			fclose(id);
2810
 
2811
			remove("temp.dat");
2812
			return data;
2813
		}
2814
	}
2815
 
2816
	remove("temp.dat");
2817
	return NULL;
2818
}
2819
 
2820
 
2821
void CBaseFile::ConvertNormalMod(C_File *f, CyString to)
2822
{
2823
	C_File *match = this->FindMatchingMod(f);
2824
	if ( match )
2825
	{
2826
		// file link
2827
		if ( !match->GetData() )
2828
			match->ReadFromFile();
2829
		match->ChangeBaseName(to);
2830
	}
2831
 
2832
	// file link
2833
	if ( !f->GetData() )
2834
		f->ReadFromFile();
2835
 
2836
	f->ChangeBaseName(to);
2837
}
2838
 
2839
void CBaseFile::ConvertAutoText(C_File *f)
2840
{
2841
	CyString to;
2842
	if ( f->GetBaseName().IsIn("-L") )
2843
		to = CyString("0000-L") + f->GetBaseName().GetToken("-L", 2, 2);
2844
	else if ( f->GetBaseName().IsIn("-l") )
2845
		to = CyString("0000-L") + f->GetBaseName().GetToken("-l", 2, 2);
2846
	else
2847
		to = f->GetBaseName().Left(-4) + "0000";
2848
 
2849
	// file link
2850
	if ( !f->GetData() )
2851
		f->ReadFromFile();
2852
 
2853
	f->ChangeBaseName(to);
2854
 
2855
}
2856
 
2857
void CBaseFile::ConvertFakePatch(C_File *f)
2858
{
2859
	// find next available fake patch
2860
	int num = 0;
2861
 
2862
	bool found = true;
2863
	while ( found )
2864
	{
2865
		++num;
2866
		found = false;
2867
		CyString find = CyString::Number(num).PadNumber(2);
2868
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2869
		{
2870
			if ( node->Data()->GetFileType() != FILETYPE_MOD )
2871
				continue;
2872
			if ( !node->Data()->IsFakePatch() )
2873
				continue;
2874
			if ( node->Data()->GetBaseName().Compare(find) )
2875
			{
2876
				found = true;
2877
				break;
2878
			}
2879
		}
2880
	}
2881
 
2882
	CyString to = CyString::Number(num).PadNumber(2);
2883
	C_File *match = this->FindMatchingMod(f);
2884
 
2885
	// file link
2886
	if ( !f->GetData() )
2887
		f->ReadFromFile();
2888
 
2889
	f->ChangeBaseName(to);
2890
	if ( match )
2891
	{
2892
		// file link
2893
		if ( !match->GetData() )
2894
			match->ReadFromFile();
2895
		match->ChangeBaseName(to);
2896
	}
2897
}
2898
 
2899
C_File *CBaseFile::FindMatchingMod(C_File *f)
2900
{
2901
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2902
	{
2903
		if ( node->Data()->GetFileType() != FILETYPE_MOD )
2904
			continue;
2905
 
2906
		if ( f->GetFileExt().Compare(node->Data()->GetFileExt()) )
2907
			continue;
2908
 
2909
		if ( f->GetBaseName().Compare(node->Data()->GetBaseName()) )
2910
			return node->Data();
2911
	}
2912
 
2913
	return NULL;
2914
}
2915
 
2916
void CBaseFile::RenameFile(C_File *f, CyString baseName)
2917
{
2918
	if ( f->GetFileType() == FILETYPE_MOD )
2919
	{
2920
		C_File *match = this->FindMatchingMod(f);
2921
		if ( match )
2922
			match->ChangeBaseName(baseName);
2923
	}
2924
 
2925
	// need to edit the file
2926
	if ( f->GetFileType() == FILETYPE_SCRIPT || f->GetFileType() == FILETYPE_UNINSTALL )
2927
		f->RenameScript(baseName);
2928
 
2929
	f->ChangeBaseName(baseName);
2930
}
2931
 
2932
CyString CBaseFile::CreateUpdateFile(CyString dir)
2933
{
2934
	CyString file = this->GetNameValidFile() + "_" + m_sAuthor + ".dat";
2935
	file.RemoveChar(' ');
2936
 
2937
	CyStringList write;
2938
	write.PushBack(CyString("Package: ") + m_sName);
2939
	write.PushBack(CyString("Author: ") + m_sAuthor);
2940
	write.PushBack(CyString("Version: ") + m_sVersion);
2941
	write.PushBack(CyString("File: ") + CFileIO(m_sFilename).GetFilename());
2942
 
2943
	CFileIO File(dir + "/" + file);
2944
	if ( File.WriteFile(&write) )
2945
		return File.GetFullFilename();
2946
	return NullString;
2947
}
2948
 
2949
CyString CBaseFile::ErrorString(int error, CyString errorStr)
2950
{
2951
	if ( error == SPKERR_NONE ) return NullString;
2952
 
2953
	CyString err;
2954
 
2955
	switch(error)
2956
	{
2957
		case SPKERR_MALLOC:
2958
			err = "Memory Failed";
2959
			break;
2960
		case SPKERR_FILEOPEN:
2961
			err = "Failed to open file";
2962
			break;
2963
		case SPKERR_FILEREAD:
2964
			err = "Failed to read file";
2965
			break;
2966
		case SPKERR_UNCOMPRESS:
2967
			err = "Failed to Uncompress";
2968
			break;
2969
		case SPKERR_WRITEFILE:
2970
			err = "Failed to write file";
2971
			break;
2972
		case SPKERR_CREATEDIRECTORY:
2973
			err = "Failed to create directory";
2974
			break;
2975
		case SPKERR_FILEMISMATCH:
2976
			err = "File count mismatch";
2977
			break;
2978
	}
2979
 
2980
	if ( !err.Empty() )
2981
	{
2982
		if ( !errorStr.Empty() )
2983
		{
2984
			err += " (";
2985
			err += errorStr + ")";
2986
		}
2987
		return err;
2988
	}
2989
 
2990
	return CyString((long)error);
2991
}
2992
 
2993
bool CBaseFile::SaveToArchive(CyString filename, int game, CProgressInfo *progress)
2994
{
2995
	TCHAR buf[5000];
2996
	wsprintf(buf, L"%hs", filename.c_str());
2997
 
2998
	HZIP hz = CreateZip(buf, 0);
2999
	if ( !hz ) return false;
3000
 
3001
	// read files and compress
3002
	ReadAllFilesToMemory();
3003
	if ( !UncompressAllFiles(progress) )
3004
	{
3005
		CloseZip(hz);
3006
		return false;
3007
	}
3008
 
3009
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3010
	{
3011
		if ( game != -1 ) {
3012
			if ( game && node->Data()->GetGame() && node->Data()->GetGame() != game )
3013
				continue;
3014
			if ( !game && node->Data()->GetGame() )
3015
				continue;
3016
		}
3017
		CyString fname = node->Data()->GetNameDirectory(this);
3018
		// create the directory
3019
		wsprintf(buf, L"%hs", fname.c_str());
3020
		ZipAdd(hz, buf, node->Data()->GetData(), node->Data()->GetDataSize());
3021
	}
3022
 
3023
	// add the data file
3024
	CyStringList list;
3025
	if ( this->GeneratePackagerScript(false, &list, true) )
3026
	{
3027
		if ( CFileIO("test.tmp").WriteFile(&list) )
3028
		{
3029
			ZipAdd(hz, L"pluginmanager.txt", L"test.tmp");
3030
			CFileIO("test.tmp").Remove();
3031
		}
3032
	}
3033
 
3034
	CloseZip(hz);
3035
 
3036
	return true;
3037
}
3038
 
13 cycrow 3039
int CBaseFile::GetGameFromString(const Utils::String &sGame)
6 cycrow 3040
{
3041
	int iGame = GAME_ALL;
3042
	if ( sGame.Compare("ALL") )
3043
		iGame = GAME_ALL;
3044
	else if ( sGame.Compare("X3") )
3045
		iGame = GAME_X3;
3046
	else if ( sGame.Compare("X2") )
3047
		iGame = GAME_X2;
3048
	else if ( sGame.Compare("X3TC") )
3049
		iGame = GAME_X3TC;
3050
	else if ( sGame.Compare("X3AP") )
3051
		iGame = GAME_X3AP;
3052
	else if ( sGame.Compare("XREBIRTH") )
3053
		iGame = GAME_XREBIRTH;
13 cycrow 3054
	else if ( sGame.isNumber() )
3055
		iGame = sGame;
6 cycrow 3056
 
3057
	return iGame;
3058
}
3059
 
13 cycrow 3060
Utils::String CBaseFile::ConvertGameToString(int iGame)
6 cycrow 3061
{
13 cycrow 3062
	Utils::String game = "ALL";
6 cycrow 3063
 
3064
	switch(iGame) {
3065
		case GAME_ALL:
3066
			game = "ALL";
3067
			break;
3068
		case GAME_X2:
3069
			game = "X2";
3070
			break;
3071
		case GAME_X3:
3072
			game = "X3";
3073
			break;
3074
		case GAME_X3TC:
3075
			game = "X3TC";
3076
			break;
3077
		case GAME_X3AP:
3078
			game = "X3AP";
3079
			break;
3080
		case GAME_XREBIRTH:
3081
			game = "XREBIRTH";
3082
			break;
3083
		default:
3084
			game = (long)iGame;
3085
	}
3086
 
3087
	return game;
3088
}
3089
 
3090
bool CBaseFile::IsGameInPackage(int game)
3091
{
3092
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3093
		if ( node->Data()->GetGame() == game )
3094
			return true;
3095
	}
3096
 
3097
	return false;
3098
}
3099
 
3100
bool CBaseFile::IsAnyGameInPackage()
3101
{
3102
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3103
		if ( node->Data()->GetGame() )
3104
			return true;
3105
	}
3106
 
3107
	return false;
3108
}
3109
 
3110
int CBaseFile::FindFirstGameInPackage()
3111
{
3112
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3113
		if ( node->Data()->GetGame() )
3114
			return node->Data()->GetGame();
3115
	}
3116
 
3117
	return 0;
3118
}
3119
bool CBaseFile::IsMultipleGamesInPackage()
3120
{
3121
	int game = 0;
3122
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
3123
		if ( node->Data()->GetGame() ) {
3124
			if ( game != node->Data()->GetGame() )
3125
				return true;
3126
			game = node->Data()->GetGame();
3127
		}
3128
	}
3129
 
3130
	return false;
3131
}