Subversion Repositories spk

Rev

Rev 53 | Rev 62 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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