Subversion Repositories spk

Rev

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