Subversion Repositories spk

Rev

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

Rev Author Line No. Line
1 cycrow 1
#include "spk.h"
2
#include "emp.h"
3
 
4
#include <time.h>
5
#include <vector>
6
 
7
#ifdef _RAR
8
#include <unrar.h>
9
#endif
10
 
11
enum {READ_START, READ_GLOBAL, READ_SCRIPT, READ_SCRIPTFILE, READ_WARES};
12
enum { EXTRACT, TEST, PRINT, LIST };
13
 
14
typedef struct SDummyEntry {
15
	CyString		sSection;
16
	CyStringList	lEntries;
17
} SDummyEntry;
18
 
19
typedef struct SComponantEntry2 {
20
	CyString		sSection;
21
	CyStringList	lEntries;
22
} SComponantEntry2;
23
 
24
typedef struct SComponantEntry {
25
	CyString		sSection;
26
	CLinkList<SComponantEntry2>	lEntries;
27
} SComponantEntry;
28
 
29
CPackages::CPackages()
30
{
31
	m_bRenameText = false;
32
	m_iLanguage = 0;
33
	m_iGame = -1;
34
	m_iGameVersion = -1;
35
	m_iGameFlags = 0;
36
	m_bSurpressProtectedWarning = false;
37
	m_iMaxPatch = 1;
38
	m_iLastUpdated = 0;
39
	m_iFakePatch = -1;
40
	m_bVanilla = true;
41
	m_pEnabledMod = NULL;
42
	m_bForceModInstall = false;
43
	m_bLoaded = false;
44
	m_bRedo = false;
45
	m_bOldPlugin = false;
46
	m_bUsedWare = false;
47
	m_pPackageNode = NULL;
48
	m_bRemoveDir = false;
49
	m_bDisableVanilla = false;
50
	m_bLoadVFS = true;
51
	m_iSaveGame = -1;
52
	m_iSaveGameManager = -1;
53
	m_bForceEMP = false;
54
 
55
	for ( int i = 0; i < WAREBUFFERS; i++ )
56
		m_iWareBuffer[i] = 0;
57
	m_iShipBuffer = 0;
58
}
59
 
60
CPackages::~CPackages ()
61
{
62
	m_lDisableList.clear();
63
	m_lEnableList.clear();
64
	this->Reset();
65
}
66
 
67
void CPackages::SetVanilla(bool b)
68
{
69
	if ( m_bVanilla != b && b ) {
70
		for ( CListNode<CBaseFile> *pNode = m_lPackages.Front(); pNode; pNode = pNode->next() ) {
71
			pNode->Data()->SetModifiedEnabled(pNode->Data()->IsEnabled());
72
		}
73
	}
74
	m_bVanilla = b;
75
}
76
 
77
void CPackages::Reset()
78
{
79
	m_lFiles.MemoryClear();
80
	m_lUninstallFiles.MemoryClear();
81
	for ( CListNode<CBaseFile> *pNode = m_lPackages.Front(); pNode; pNode = pNode->next() )
82
		pNode->Data()->GetFileList()->clear();
83
	m_lPackages.MemoryClear();
84
	m_lOriginalFiles.MemoryClear();
85
	m_iFakePatch = -1;
86
	m_bVanilla = false;
87
	m_pEnabledMod = NULL;
88
	m_bLoaded = false;
89
	m_iLanguage = 0;
90
	m_bOldPlugin = false;
91
	m_bRemoveDir = false;
92
	m_bDisableVanilla = false;
93
	m_bSurpressProtectedWarning = false;
94
	m_iSaveGame = -1;
95
	m_iSaveGameManager = -1;
96
	m_sSetMod = "";
97
 
98
	for ( int i = 0; i < WAREBUFFERS; i++ )
99
	{
100
		m_iWareBuffer[i] = 0;
101
		m_lGameWares[i].MemoryClear();
102
	}
103
	m_iShipBuffer = 0;
104
 
105
	m_lGameShips.MemoryClear();
106
	m_lAvailablePackages.MemoryClear();
107
	m_lInstallList.MemoryClear();
108
	m_lEnableList.MemoryClear();
109
	m_lDisableList.MemoryClear();
110
 
111
	m_lCreatedFiles.Clear();
112
	m_lNonRemovedFiles.Clear();
113
	m_lGlobals.Clear();
114
	m_lFakePatchOrder.Clear();
115
}
116
 
117
void CPackages::LoadVirtualFileSystem()
118
{
119
	if ( !m_bLoadVFS )
120
		return;
121
 
43 cycrow 122
	m_pGameVFS.SetAddon(this->GetAddonDir().ToString());
1 cycrow 123
	if ( m_pEnabledMod )
124
	{
125
		C_File *f;
126
		for ( f = m_pEnabledMod->GetFirstFile(FILETYPE_MOD); f; f = m_pEnabledMod->GetNextFile(f) )
127
		{
128
			if ( f->IsFakePatch() ) continue;
129
			if ( f->CheckFileExt("cat") ) break;
130
		}
131
 
132
		if ( f )
35 cycrow 133
			m_pGameVFS.LoadFilesystem(m_sCurrentDir.ToString(), f->GetFilePointer().ToString(), 0);
1 cycrow 134
		else
35 cycrow 135
			m_pGameVFS.LoadFilesystem(m_sCurrentDir.ToString(), 0);
1 cycrow 136
	}
137
	else
35 cycrow 138
		m_pGameVFS.LoadFilesystem(m_sCurrentDir.ToString(), 0);
1 cycrow 139
}
140
 
141
bool CPackages::IsOldDir(CyString dir)
142
{
143
	bool oldPlugin = false;
144
 
145
	dir = dir.FindReplace("\\", "/");
146
	CFileIO datFile(dir + "/PluginManager/pluginmanager.dat");
147
	if ( datFile.Exists() )
148
	{
149
		std::vector<CyString> *readFile = datFile.ReadLines();
150
		if ( readFile )
151
		{
152
			for ( int i = 0; i < (int)readFile->size(); i++ )
153
			{
154
				CyString line(readFile->at(i));
155
				CyString cmd = line.GetToken(":", 1, 1).lower();
156
				if ( cmd == "<script>" || cmd == "</scripts>" )
157
					break;
158
				else if ( cmd == "spkinstaller" || cmd == "globalfiles" )
159
					break;
160
				else if ( cmd == "pluginmanager" )
161
				{
162
					oldPlugin = true;
163
					break;
164
				}
165
			}
166
 
167
			delete readFile;
168
		}
169
	}
170
 
171
	return oldPlugin;
172
}
173
 
174
bool CPackages::Read ( CyString dir, CProgressInfo *progress )
175
{
176
	m_sCurrentDir = dir;
177
	m_sCurrentDir = m_sCurrentDir.FindReplace("\\", "/");
178
	m_bOldPlugin = false;
179
	m_lCreatedFiles.Clear();
180
	m_bRemoveDir = false;
181
	m_bSurpressProtectedWarning = false;
182
	m_iSaveGame = -1;
183
	m_iSaveGameManager = -1;
184
	m_lNonRemovedFiles.Clear();
185
	m_lGlobals.Clear();
186
	m_lFakePatchOrder.Clear();
187
 
188
	m_bVanilla = true;
189
 
190
	// check the pluginmanager data file exists
191
	CFileIO datFile(m_sCurrentDir + "/PluginManager/pluginmanager.dat");
192
	if ( datFile.Exists() )
193
	{
194
		std::vector<CyString> *readFile = datFile.ReadLines();
195
		if ( readFile )
196
		{
197
			float fVersion = 0;
198
			bool spkinstaller = false;
199
			float fBeta = 0;
200
 
201
			int iStatus = READ_START;
202
			int iCount = 0;
203
			int iWare = -1;
204
			int iShip = -1;
205
 
206
			CBaseFile *packageFile = 0;
207
 
208
			for ( int i = 0; i < (int)readFile->size(); i++ )
209
			{
210
				CyString line(readFile->at(i));
211
 
212
				CyString cmd = line.GetToken(":", 1, 1).lower();
213
				CyString rest = line.GetToken(":", 2).RemoveFirstSpace();
214
 
215
				if ( iStatus == READ_GLOBAL )
216
				{
217
					if ( cmd == "<script>" )
218
					{
219
						packageFile = new CSpkFile();
220
						iStatus = READ_SCRIPT;
221
					}
222
					else if ( cmd == "<ship>" )
223
					{
224
						packageFile = new CXspFile();
225
						iStatus = READ_SCRIPT;
226
					}
227
					else if ( cmd == "<base>" )
228
					{
229
						packageFile = new CBaseFile();
230
						iStatus = READ_SCRIPT;
231
					}
232
					else if ( cmd == "<archive>" )
233
					{
234
						packageFile = new CArchiveFile();
235
						iStatus = READ_SCRIPT;
236
					}
237
					else if ( cmd != "</scripts>" )
238
					{
239
						int game = 0;
240
						bool disabled = false;
241
						CyString fileName = line.GetToken(":", 5);
242
						CyString origName;
243
						if ( fileName.Left(2) == "D#" )
244
						{
245
							fileName.Erase(0, 2);
246
							disabled = true;
247
						}
248
						if ( fileName.Left(2) == "G#" ) {
249
							fileName.Erase(0, 2);
250
							game = fileName.GetToken("#", 1, 1).ToInt();
251
							fileName = fileName.GetToken("#", 2);
252
						}
253
						if ( fileName.IsIn("O#") )
254
						{
255
							origName = fileName.GetToken("O#", 2, 2);
256
							fileName = fileName.GetToken("O#", 1, 1);
257
						}
258
 
259
						C_File *newFile = new C_File( m_sCurrentDir + fileName );
260
						newFile->SetGame(game);
261
						newFile->SetOriginalName(origName);
262
						newFile->SetDisabled(disabled);
263
						newFile->SetFileType(line.GetToken(":", 1, 1).ToInt());
264
						newFile->SetCreationTime((time_t)line.GetToken(":", 2, 2).ToInt());
265
						newFile->SetDir(line.GetToken(":", 3, 3));
266
						if ( line.GetToken(":", 4, 4).ToInt() )
267
							newFile->SetShared(true);
268
						newFile->UpdateSigned();
269
 
270
						m_lFiles.push_back(newFile);
271
					}
272
				}
273
				else if ( iStatus == READ_SCRIPT )
274
				{
275
					if ( cmd == "installspk" )
50 cycrow 276
						packageFile->setFilename(rest.ToString());
1 cycrow 277
					else if ( cmd == "files" )
278
					{
279
						iStatus = READ_SCRIPTFILE;
280
						if ( spkinstaller )
281
						{
282
							int max;
283
							CyString *files = rest.SplitToken(' ', &max);
284
 
285
							for ( int i = 0; i < max; i++ )
286
							{
287
								int fileNum = files[i].ToInt();
288
								if ( fileNum >= 0 && fileNum < m_lFiles.size() )
289
									packageFile->AddFile(m_lFiles[fileNum]);
290
							}
291
 
292
							CLEANSPLIT(files, max);
293
						}
294
					}
295
					else if ( cmd == "disabled" )
296
						packageFile->SetEnabled(false);
297
					else if ( cmd == "modifieddisabled" )
298
						packageFile->SetModifiedEnabled(false);
299
					else if ( cmd == "icon" )
300
					{
301
						C_File *icon = new C_File(rest.GetToken(" ", 2));
302
						packageFile->SetIcon(icon, rest.GetToken(" ", 1, 1));
303
					}
304
					else
14 cycrow 305
						packageFile->ParseValueLine(line.ToString());
1 cycrow 306
				}
307
				else if ( iStatus == READ_SCRIPTFILE )
308
				{
309
					if ( cmd == "<script>" )
310
					{
311
						if ( packageFile->IsMod() && packageFile->IsEnabled() )
312
							m_pEnabledMod = packageFile;
313
 
314
						m_lPackages.push_back(packageFile);
315
						this->ConvertOldPackage(packageFile);
316
						packageFile = new CSpkFile();
317
						iStatus = READ_SCRIPT;
318
 
319
					}
320
					else if ( cmd == "<ship>" )
321
					{
322
						m_lPackages.push_back(packageFile);
323
						this->ConvertOldPackage(packageFile);
324
						packageFile = new CXspFile();
325
						iStatus = READ_SCRIPT;
326
					}
327
					else if ( cmd == "<base>" )
328
					{
329
						m_lPackages.push_back(packageFile);
330
						this->ConvertOldPackage(packageFile);
331
						packageFile = new CBaseFile();
332
						iStatus = READ_SCRIPT;
333
					}
334
					else if ( cmd == "<archive>" )
335
					{
336
						m_lPackages.push_back(packageFile);
337
						this->ConvertOldPackage(packageFile);
338
						packageFile = new CArchiveFile();
339
						iStatus = READ_SCRIPT;
340
					}
341
					else if ( cmd == "</scripts>" )
342
					{
343
						if ( packageFile->IsMod() && packageFile->IsEnabled() )
344
							m_pEnabledMod = packageFile;
345
 
346
						m_lPackages.push_back(packageFile);
347
						this->ConvertOldPackage(packageFile);
348
					}
349
					else
350
					{
351
						int fileNum = line.GetToken("::", 1, 1).ToInt();
352
						if ( fileNum >= 0 && fileNum < m_lFiles.size() )
353
							packageFile->AddFile(m_lFiles[fileNum]);
354
					}
355
				}
356
				else if ( iWare != -1 )
357
				{
358
					--iCount;
359
 
360
					SGameWare *gm = new SGameWare;
361
					gm->iPos = line.GetToken(" ", 1, 1).ToInt();
362
					gm->iType = line.GetToken(" ", 2, 2).ToInt();
363
					gm->cType = line.GetToken(" ", 3, 3)[0];
364
					gm->pWare = NULL;
365
					gm->sWareName = line.GetToken(" ", 4);
366
					m_lGameWares[iWare].push_back(gm);
367
					if ( iCount <= 0 )
368
						iWare = -1;
369
				}
370
				else if ( iShip != -1 )
371
				{
372
					--iCount;
373
 
374
					SGameShip *gm = new SGameShip;
375
					gm->iType = line.GetToken(" ", 1, 1).ToInt();
376
					if ( line.GetToken(" ", 2, 2).Left(4) == "$#C:" )
377
					{
378
						gm->sShipClass = line.GetToken(" ", 2, 2).Right(-4);
379
						gm->sShipID = line.GetToken(" ", 3);
380
					}
381
					else
382
					{
383
						gm->sShipID = line.GetToken(" ", 2);
384
						gm->sShipClass = "OBJ_SHIP_M5";
385
					}
386
					gm->pPackage = NULL;
387
					m_lGameShips.push_back(gm);
388
					if ( iCount <= 0 )
389
						iShip = -1;
390
				}
391
				else if ( cmd.Compare("savegamemanager") )
392
					m_iSaveGameManager = rest.ToInt();
393
				else if ( cmd.Compare("savegame") )
394
					m_iSaveGame = rest.ToInt();
395
				else if ( cmd == "fakepatch" )
396
					m_iFakePatch = rest.ToInt();
397
				else if ( cmd == "updatetime" )
398
					m_iLastUpdated = rest.ToInt();
399
				else if ( cmd == "surpressprotectedwarning" )
400
					m_bSurpressProtectedWarning = true;
401
				else if ( cmd == "pluginmanager" )
402
				{
403
					fVersion = rest.ToFloat();
404
					m_bOldPlugin = true;
405
				}
406
				else if ( cmd == "setmod" )
407
					m_sSetMod = rest;
408
				else if ( cmd == "shipbuffer" )
409
					m_iShipBuffer = rest.ToInt();
410
				else if ( cmd == "warebuffers" )
411
				{
412
					int max = rest.NumToken(" ");
413
					for ( int i = 0; i < WAREBUFFERS; i++ )
414
					{
415
						if ( i > max )
416
							m_iWareBuffer[i] = 0;
417
						else
418
							m_iWareBuffer[i] = rest.GetToken(" ", i + 1, i + 1).ToInt();
419
					}
420
				}
421
				else if ( cmd == "createdfile" )
422
					m_lCreatedFiles.PushBack(rest);
423
				else if ( cmd == "wares" )
424
				{
425
					iWare = rest.GetToken(" ", 1, 1).ToInt();
426
					iCount = rest.GetToken(" ", 2, 2).ToInt();
427
				}
428
				else if ( cmd == "ships" )
429
				{
430
					iShip = 1;
431
					iCount = rest.GetToken(" ", 1, 1).ToInt();
432
				}
433
				else if ( cmd == "modified" )
434
					m_bVanilla = false;
435
				else if ( cmd == "spkinstaller" )
436
				{
437
					fVersion = rest.ToFloat();
438
					spkinstaller = true;
439
				}
440
				else if ( cmd == "BetaVersion" )
441
					fBeta = rest.ToFloat();
442
				else if ( cmd == "Uninstall" )
443
				{
444
					C_File *uf = new C_File();
445
					uf->SetFileType(FILETYPE_SCRIPT);
446
					uf->SetCreationTime(rest.GetToken(" ", 1, 1).ToInt());
447
					uf->SetFilename(m_sCurrentDir + "/Scripts/" + rest.GetToken(" ", 2));
448
					m_lUninstallFiles.push_back(uf);
449
				}
450
				else if ( cmd == "NonRemovedFile" )
451
				{
452
					if ( !CFileIO(rest).Remove() )
453
						m_lNonRemovedFiles.PushBack(rest);
454
				}
455
				else if ( cmd == "Original" )
456
				{
457
					C_File *uf = new C_File();
458
					uf->SetFileType(rest.GetToken(" ", 1, 1).ToInt());
459
					if ( uf->GetFileType() == FILETYPE_EXTRA )
460
					{
461
						uf->SetDir(rest.GetToken(" ", 2).GetToken(":", 2));
462
						uf->SetFilename(rest.GetToken(" ", 2).GetToken(":", 1, 1));
463
					}
464
					else
465
						uf->SetFilename(rest.GetToken(" ", 2));
466
					uf->SetFilename(m_sCurrentDir + "/" + uf->GetNameDirectory(NULL));
467
					m_lOriginalFiles.push_back(uf);
468
				}
469
				else if ( cmd.Compare("GlobalSetting") )
470
					m_lGlobals.PushBack(rest.GetToken(":", 1, 1), rest.GetToken(":", 2));
471
				else if ( cmd.Compare("FakePatchOrder") )
472
					m_lFakePatchOrder.PushBack(rest.GetToken(":", 1, 1), rest.GetToken(":", 2));
473
				else if ( cmd == "globalfiles" )
474
					iStatus = READ_GLOBAL;
475
 
476
				if ( progress )
477
				{
478
					progress->UpdateProgress((long)i, (long)readFile->size());
479
				}
480
			}
481
			readFile->clear();
482
 
483
			if ( !spkinstaller )
484
				m_bRedo = true;
485
 
486
			delete readFile;
487
		}
488
	}
489
 
490
	if ( m_pEnabledMod && !m_pEnabledMod->IsEnabled() )
491
		m_pEnabledMod = NULL;
492
 
493
	m_iGame = m_gameExe.GetGameType(m_sCurrentDir) + 1;
494
	m_iGameVersion = m_gameExe.GetGameVersion(m_sCurrentDir, &m_sGameVersion) + 1;
495
	m_iGameFlags = m_gameExe.GetGameFlags(m_iGame - 1);
496
	m_iMaxPatch = m_gameExe.GetMaxPatch(m_iGame - 1);
497
 
498
	// find the fake patch
499
	if ( !ReadyFakePatch() )
500
		return false;
501
 
502
	this->RemoveCreatedFiles();
503
 
504
	// match up wares
505
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
506
	{
507
		if ( node->Data()->GetType() == TYPE_SPK )
508
		{
509
			CSpkFile *p = (CSpkFile *)node->Data();
510
			for ( CListNode<SWares> *wNode = p->GetWaresList()->Front(); wNode; wNode = wNode->next() )
511
			{
512
				SWares *w = wNode->Data();
513
				for ( CListNode<SGameWare> *gNode = m_lGameWares[CPackages::ConvertWareType(w->cType)].Front(); gNode; gNode = gNode->next() )
514
				{
10 cycrow 515
					if ( w->sID == gNode->Data()->sWareName.ToString() )
1 cycrow 516
					{
517
						gNode->Data()->pWare = w;
518
						break;
519
					}
520
				}
521
			}
522
		}
523
		else if ( node->Data()->GetType() == TYPE_XSP )
524
		{
525
			CXspFile *p = (CXspFile *)node->Data();
526
			for ( CListNode<SGameShip> *gNode = m_lGameShips.Front(); gNode; gNode = gNode->next() )
527
			{
14 cycrow 528
				if ( p->GetShipID().Compare(gNode->Data()->sShipID.ToString()) )
1 cycrow 529
				{
530
					gNode->Data()->pPackage = p;
531
					break;
532
				}
533
			}
534
		}
535
	}
536
 
537
	// check the purged time
538
	this->PurgeGameObjects();
539
 
540
	m_bLoaded = true;
541
 
542
	return true;
543
}
544
 
545
void CPackages::RemoveCreatedFiles()
546
{
547
	for ( SStringList *node = m_lCreatedFiles.Head(); node; node = node->next )
548
	{
549
		if ( CFileIO(node->str).Exists() )
550
			CFileIO(node->str).Remove();
551
		else if ( CFileIO(m_sCurrentDir + "/" + node->str).Exists() )
552
			CFileIO(m_sCurrentDir + "/" + node->str).Remove();
553
	}
554
 
555
	m_lCreatedFiles.Clear();
556
}
557
 
558
void CPackages::PurgeGameObjects()
559
{
560
	// check for the log file
561
	CyString logDir = GetLogDirectory();
562
	CFileIO LogFile(logDir + "/log0" + CyString::Number(PMTEXTFILE).PadNumber(4) + ".txt");
563
	if ( LogFile.Exists() )
564
	{
565
		// read the log file to memory
566
		std::vector<CyString> *lines = LogFile.ReadLines();
567
		if ( lines )
568
		{
569
			for ( int i = 0; i < (int)lines->size(); i++ )
570
			{
571
				CyString line(lines->at(i));
572
				CyString start = line.GetToken(":", 1, 1).ToLower();
573
				CyString rest = line.GetToken(":", 2, 2).RemoveFirstSpace();
574
				if ( start.Compare("purged") )
575
				{
576
					long time = rest.ToLong();
577
					if ( time == m_iLastUpdated )
578
					{
579
						this->PurgeWares();
580
						this->PurgeShips();
581
						this->RemoveUninstallScripts();
582
					}
583
				}
584
			}
585
 
586
			delete lines;
587
		}
588
		// remove the log file
589
		LogFile.Remove();
590
	}
591
}
592
 
593
void CPackages::PurgeWares()
594
{
595
	// mark all delete wares as available
596
	for ( int i = 0; i < WAREBUFFERS; i++ )
597
	{
598
		for ( CListNode<SGameWare> *node = m_lGameWares[i].Front(); node; node = node->next() )
599
		{
600
			SGameWare *w = node->Data();
601
			if ( !w ) continue;
602
			if ( w->iType == WARETYPE_DELETED )
603
				w->iType = WARETYPE_NONE;
604
		}
605
		m_lGameWares[i].RemoveEmpty();
606
	}
607
}
608
 
609
void CPackages::PurgeShips()
610
{
611
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
612
	{
613
		if ( !node->Data() )
614
			continue;
615
		if ( node->Data()->iType == WARETYPE_DELETED )
616
			node->Data()->iType = WARETYPE_NONE;
617
	}
618
	m_lGameShips.RemoveEmpty();
619
}
620
 
621
CyString CPackages::GetLogDirectory()
622
{
623
	CyString logDir = m_sCurrentDir;
624
	if ( m_iGameFlags & EXEFLAG_MYDOCLOG )
625
	{
626
		SGameExe *exe = m_gameExe.GetGame(m_iGame - 1);
627
		if ( exe )
628
		{
629
			if ( !exe->sMyDoc.Empty() )
630
				logDir = m_sMyDoc + "/" + exe->sMyDoc;
631
		}
632
	}
633
 
634
	return logDir;
635
}
636
 
637
CyString CPackages::GetLogDirectory(CyString gameExe)
638
{
639
	CyString logDir = m_sCurrentDir;
640
	if ( m_iGameFlags & EXEFLAG_MYDOCLOG )
641
	{
642
		SGameExe *exe = m_gameExe.GetGameExe(CFileIO(gameExe).GetFilename());
643
		if ( exe )
644
		{
645
			if ( !exe->sMyDoc.Empty() )
646
				logDir = m_sMyDoc + "/" + exe->sMyDoc;
647
		}
648
	}
649
 
650
	return CFileIO(logDir).GetFullFilename();
651
}
652
 
653
CyString CPackages::GetSaveDirectory()
654
{
655
	CyString logDir = this->GetLogDirectory();
656
	if ( m_iGameFlags & EXEFLAG_NOSAVESUBDIR )
657
		return logDir;
658
 
659
	return logDir + "/save";
660
}
661
 
662
bool CPackages::ReadyFakePatch()
663
{
664
	// already exists, lets skip it
665
	if ( CFileIO(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat").Exists() && CFileIO(m_sCurrentDir + "/PluginManager/PlugMan_Fake.dat").Exists() )
666
		return true;
667
 
668
	// if only one of them exists, lets remove them
669
	if ( CFileIO(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat").Exists() )
670
		CFileIO(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat").Remove();
671
	if ( CFileIO(m_sCurrentDir + "/PluginManager/PlugMan_Fake.dat").Exists() )
672
		CFileIO(m_sCurrentDir + "/PluginManager/PlugMan_Fake.dat").Remove();
673
 
674
	// now lets find the fake patch
675
	CyString useFile;
676
	if ( m_iFakePatch > 0 )
677
	{
678
		CyString file = CyString::Number(m_iFakePatch).PadNumber(2);
679
		if ( CheckValidPluginManagerFile(file) )
680
			useFile = file;
681
		else if ( CheckIfPluginManagerFile(file) )
682
			useFile = file;
683
	}
684
 
685
	if ( useFile.Empty() )
686
	{
687
		int nextfree = this->FindNextFakePatch();
688
		--nextfree; // gets the end fake patch
689
 
690
		// work backwards till we find a valid one
691
		while ( nextfree > m_iMaxPatch )
692
		{
693
			CyString file = CyString::Number(nextfree).PadNumber(2);
694
			if ( CheckValidPluginManagerFile(file) )
695
			{
696
				useFile = file;
697
				break;
698
			}
699
			--nextfree;
700
		}
701
	}
702
 
703
	CyString addonDir = this->GetAddonDir();
704
 
705
	// couldn't find the correct file, lets search for it
706
	if ( useFile.Empty() ) {
707
		int nextfree = this->FindNextFakePatch();
708
		--nextfree; // gets the end fake patch
709
 
710
		// work backwards till we find a valid one
711
		while ( nextfree > m_iMaxPatch )
712
		{
713
			CyString file = CyString::Number(nextfree).PadNumber(2);
714
			if ( CheckIfPluginManagerFile(file) )
715
			{
716
				useFile = file;
717
				break;
718
			}
719
			--nextfree;
720
		}		
721
	}
722
 
723
	if ( !useFile.Empty() )
724
	{
725
		// lets check whats in the file first
726
		CCatFile openCat;
727
		if ( openCat.Open(m_sCurrentDir + "/" + useFile + ".cat", addonDir, CATREAD_CATDECRYPT, false) == CATERR_NONE )
728
		{
729
			CLinkList<SInCatFile> *files = openCat.GetFiles();
730
			bool found = false;
731
			if ( files )
732
			{
733
				CyString useAddonDir = addonDir;
734
				if ( !useAddonDir.Empty() ) useAddonDir += "\\";
735
				for ( SInCatFile *inCat = files->First(); inCat; inCat = files->Next() )
736
				{
737
					if ( inCat->sFile.Compare("PlugMan\\TFake.pck") )
738
						continue;
739
					if ( inCat->sFile.Compare(useAddonDir + "t\\44" + CyString::Number(PMTEXTFILE).PadNumber(4) + ".pck") )
740
						continue;
741
					if ( inCat->sFile.Compare(useAddonDir + "t\\" + CyString::Number(PMTEXTFILE).PadNumber(4) + "-L044.pck") )
742
						continue;
743
 
744
					found = true;
745
					break;
746
				}
747
			}
748
 
749
			// no files, jsut delete them
750
			CFileIO catFile(m_sCurrentDir + "/" + useFile + ".cat");
751
			if ( !files || !found )
752
			{
753
				if ( catFile.Remove() )
754
				{
755
					CFileIO datFile(m_sCurrentDir + "/" + useFile + ".dat");
756
					if ( datFile.Remove() )
757
						return true;
758
				}
759
			}
760
			else
761
			{
762
				if ( catFile.Rename(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat") )
763
				{
764
					CFileIO datFile(m_sCurrentDir + "/" + useFile + ".dat");
765
 
766
					if ( datFile.Rename(m_sCurrentDir + "/PluginManager/PlugMan_Fake.dat") )
767
						return true;
768
 
769
					// TODO: it failed, restore cat file and do error
770
				}
771
			}
772
		}
773
 
774
		// if we're here, we tryed, and failed
775
		return false;
776
	}
777
 
778
	// otherwise we didn't need to (hopefully)
779
	return true;
780
}
781
 
782
 
783
bool CPackages::CheckIfPluginManagerFile(CyString filename)
784
{
785
	bool found = false;
786
 
787
	CFileIO catFile(m_sCurrentDir + "/" + filename + ".cat");
788
	if ( catFile.Exists() && CFileIO(m_sCurrentDir + "/" + filename + ".dat").Exists() )
789
	{
790
		CCatFile openFile;
791
		if ( openFile.Open(catFile.GetFullFilename(), this->GetAddonDir(), CATREAD_CATDECRYPT, false) == CATERR_NONE )
792
		{
793
			int count = 0;
794
			int noncount = 0;
795
 
796
			// check for some of the files
797
			for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() ) {
798
				if ( node->Data()->GetFullDir().IsIn("::") ) {
799
					if (CFileIO(node->Data()->GetFullDir().GetToken("::", 1, 1)).GetFilename().Compare("PlugMan_Fake.cat") ) {
800
						CyString filename = node->Data()->GetFilePointer().GetToken("::", 2, 2);
801
						filename = filename.findreplace("/", "\\");
802
						if ( openFile.FindData(filename) )
803
							++count;
804
						else
805
							++noncount;
806
					}
807
				}
808
			}
809
 
810
			if ( (count && !noncount) || (count > noncount) )
811
				found = true;
812
		}
813
	}
814
 
815
	return found;
816
}
817
 
818
bool CPackages::CheckValidPluginManagerFile(CyString filename)
819
{
820
	bool found = false;
821
 
822
	// both the cat file and dat file exists, lets open it
823
	CFileIO catFile(m_sCurrentDir + "/" + filename + ".cat");
824
	if ( catFile.Exists() && CFileIO(m_sCurrentDir + "/" + filename + ".dat").Exists() )
825
	{
826
		CCatFile openFile;
827
		if ( openFile.Open(catFile.GetFullFilename(), this->GetAddonDir(), CATREAD_CATDECRYPT, false) == CATERR_NONE )
828
		{
829
			if ( openFile.FindData("PlugMan\\TFake.pck") )
830
				return true;
831
			if ( openFile.FindData("pluginmanagerfake.pck") )
832
				return true;
833
		}
834
	}
835
 
836
	return found;
837
}
838
 
839
/**
840
 * Converts a package in old format
841
 *
842
 * Some entries in older packages need to be changed for the new installer
843
 * These Include:
844
 *		- Game Version, old format is an id of position in the Data/exe file, new is 2 values, one for game, one for the version
845
 */
846
void CPackages::ConvertOldPackage(CBaseFile *package)
847
{
848
	for ( CListNode<SGameCompat> *gNode = package->GetGameCompatabilityList()->Front(); gNode; gNode = gNode->next() ) {
849
		if ( gNode->Data()->iGame == -1 ) {
850
			// all versions
851
			if ( gNode->Data()->iVersion == 0 )
852
				gNode->Data()->iGame = 0;
853
			else
854
			{
855
				int version = 0;
856
				gNode->Data()->iGame = m_gameExe.ConvertGameType(gNode->Data()->iVersion, &version);
857
				gNode->Data()->iVersion = version;
858
			}
859
		}
860
	}
861
 
49 cycrow 862
	if ( package->forumLink().empty() && package->webSite().isin("forum.egosoft")) {
863
		package->setForumLink(package->webSite());
864
		package->setWebSite("");
1 cycrow 865
	}
866
 
867
	// convert the version
868
	if ( package->GetType() == TYPE_SPK )
869
	{
870
		CSpkFile *spk = (CSpkFile *)package;
6 cycrow 871
		if ( spk->GetScriptType() == CSpkFile::SCRIPTTYPE_CUSTOM )
1 cycrow 872
		{
873
			CyString type = spk->GetScriptTypeString(44);
874
			if ( type.Compare("Ship Upgrade") )
6 cycrow 875
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_SHIPUPGRADE);
1 cycrow 876
			else if ( type.Compare("Trade Script") )
6 cycrow 877
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_TRADE);
1 cycrow 878
			else if ( type.Compare("Fleet Management") )
6 cycrow 879
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_FLEET);
1 cycrow 880
			else if ( type.Compare("Navigation Script") )
6 cycrow 881
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_NAVIGATION);
1 cycrow 882
			else if ( type.Compare("Piracy") )
6 cycrow 883
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_PIRACY);
1 cycrow 884
			else if ( type.Compare("Other") )
6 cycrow 885
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_OTHER);
1 cycrow 886
			else if ( type.Compare("Ship Command") )
6 cycrow 887
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_SHIPCOMMAND);
1 cycrow 888
			else if ( type.Compare("Station Command") )
6 cycrow 889
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_STATIONCOMMAND);
1 cycrow 890
			else if ( type.Compare("al plugin") )
6 cycrow 891
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_ALPLUGIN);
1 cycrow 892
			else if ( type.Compare("combat script") )
6 cycrow 893
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_COMBAT);
1 cycrow 894
			else if ( type.Compare("bbs and missions") )
6 cycrow 895
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_MISSION);
1 cycrow 896
			else if ( type.Compare("extension mod") )
6 cycrow 897
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_EXTENSION);
1 cycrow 898
			else if ( type.Compare("rebalance mod") )
6 cycrow 899
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_REBALANCE);
1 cycrow 900
			else if ( type.Compare("general mod") )
6 cycrow 901
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_GENERALMOD);
1 cycrow 902
			else if ( type.Compare("total conversion") )
6 cycrow 903
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_TOTAL);
1 cycrow 904
			else if ( type.Compare("cheat script") )
6 cycrow 905
				spk->SetScriptType(CSpkFile::SCRIPTTYPE_CHEAT);
1 cycrow 906
			else if ( type == "Library Script" )
907
			{
908
				spk->SetScriptType("");
909
				spk->SetLibrary();
910
			}
911
 
6 cycrow 912
			if ( spk->GetScriptType() != CSpkFile::SCRIPTTYPE_CUSTOM )
1 cycrow 913
				spk->SetScriptType("");
914
		}
915
	}
916
	else if ( package->GetType() == TYPE_XSP )
917
	{
918
		CXspFile *xsp = (CXspFile *)package;
14 cycrow 919
		Utils::String data = xsp->GetShipData();
1 cycrow 920
 
921
		for ( int i = 17; i <= 18; i++ )
922
		{
14 cycrow 923
			Utils::String model = data.token(";", i);
924
			Utils::String modelExt = model.right(4);
1 cycrow 925
			// check file extension
926
			if ( modelExt.Compare(".bod") || modelExt.Compare(".pbd") )
14 cycrow 927
				data = data.replaceToken(";", i, model.left(-4));
1 cycrow 928
		}
929
 
930
		xsp->SetShipData(data);
931
	}
932
 
933
	// any extra files that are in director folder
934
	if ( package->AnyFileType(FILETYPE_EXTRA) )
935
	{
936
		for ( C_File *f = package->GetFirstFile(FILETYPE_EXTRA); f; f = package->GetNextFile(f) )
937
		{
938
			if ( !f->GetDir().Compare("director") )
939
				continue;
940
			if ( f->CheckFileExt("xml") || f->CheckFileExt("pck") )
941
			{
942
				f->SetDir("");
943
				f->SetFileType(FILETYPE_MISSION);
944
			}
945
		}
946
	}
947
}
948
 
949
void CPackages::UpdatePackage(CBaseFile *p)
950
{
951
	if ( p->GetType() != TYPE_SPK )
952
		return;
953
 
954
	CSpkFile *package = (CSpkFile *)p;
955
 
956
	// update the signed status
957
	package->UpdateSigned(true);
958
 
959
	// check for another mod
960
	if ( !package->IsAnotherMod() )
961
		return;
962
 
963
	package->SetParent((CSpkFile *)FindSpkPackage(package->GetOtherName(), package->GetOtherAuthor()));
964
}
965
 
966
/**
967
 * Updates the package list once data has been read
968
 *
969
 * Finds any original Files
970
 * Updates Signed status of all packages
971
 */
972
bool CPackages::UpdatePackages(int doStatus, bool individual)
973
{
974
	// if theres no original files, then set the current structure as the base
975
	if ( doStatus == 0 || doStatus == -1 )
976
	{
977
		if ( m_lOriginalFiles.empty() || m_bRedo )
978
		{
979
			StoreOriginalFiles(FILETYPE_SCRIPT, "scripts");
980
			StoreOriginalFiles(FILETYPE_TEXT, "t");
981
			StoreOriginalFiles(FILETYPE_SOUND, "soundtrack");
982
			StoreOriginalFiles(FILETYPE_EXTRA, "mov");
983
		}
984
	}
985
 
986
	// update each package
987
	// parent/child, signed status
988
	if ( doStatus == -1 || doStatus == 1 )
989
	{
990
		if ( individual )
991
		{
992
			// no package, most likly none installed
993
			if ( !m_pPackageNode )
994
				return false;
995
 
996
			this->UpdatePackage(m_pPackageNode->Data());
997
 
998
			// move to the next package
999
			m_pPackageNode = m_pPackageNode->next();
1000
			if ( !m_pPackageNode )
1001
				return false;
1002
		}
1003
		else
1004
		{
1005
			for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
1006
				this->UpdatePackage(node->Data());
1007
		}
1008
	}
1009
 
1010
	if ( doStatus == -1 || doStatus == 2 )
1011
		this->LoadVirtualFileSystem();
1012
 
1013
	// adjust save games
1014
	if ( doStatus == -1 || doStatus == 3 )
1015
	{
1016
		/*
1017
		// load our save games to the active mydocs
1018
		if ( m_iSaveGame != -1 )
1019
		{
1020
			// check we have a valid save game directory
1021
			CDirIO Dir(this->GetSaveDirectory());
1022
			if ( Dir.Exists() )
1023
			{
1024
				// now check our directory already exists
1025
				// if we are vanilla mode, then make sure we use the vanilla saves if exists
1026
				CyString d = CyString("game_") + (long)m_iSaveGame + "/";
1027
				if ( m_bVanilla )
1028
					d += "Vanilla";
1029
				else
1030
					d += "Modified";
1031
 
1032
				// copy the files back over
1033
				if ( Dir.Exists(d) )
1034
				{
1035
					// if vanilla, remove files first
1036
					if ( m_bVanilla )
1037
					{
1038
						CyStringList *lDirs = Dir.DirList();
1039
						if ( lDirs )
1040
						{
1041
							for ( SStringList *node = lDirs->Head(); node; node = node->next )
1042
							{
1043
								if ( !Dir.IsFile(node->str) || !CFileIO(node->str).CheckFileExtension("sav") )
1044
									continue;
1045
								CFileIO(Dir.File(node->str)).Remove();
1046
							}
1047
 
1048
							delete lDirs;
1049
						}
1050
					}
1051
 
1052
					// copy files to normal save game directory
1053
					CDirIO FromDir(Dir.Dir(d));
1054
					CyStringList *lDirs = FromDir.DirList();
1055
					if ( lDirs )
1056
					{
1057
						for ( SStringList *node = lDirs->Head(); node; node = node->next )
1058
						{
1059
							if ( !FromDir.IsFile(node->str) || !CFileIO(node->str).CheckFileExtension("sav") )
1060
								continue;
1061
							CFileIO F(FromDir.File(node->str));
1062
							F.Copy(Dir.File(F.GetFilename()));
1063
						}
1064
 
1065
						delete lDirs;
1066
					}
1067
				}
1068
			}
1069
		}*/
1070
	}
1071
 
1072
	m_bRedo = false;
1073
	return true;
1074
}
1075
 
1076
int CPackages::CheckOpenPackage(CyString file, int *error)
1077
{
1078
	// first check if it exists
1079
	if ( !CFileIO(file).Exists() )
1080
	{
1081
		*error = INSTALLERR_NOEXIST;
1082
		return -1;
1083
	}
1084
 
1085
	// open the spk file
1086
	float fVersion = 0.0f;
1087
	int check = CBaseFile::CheckFile(file, &fVersion);
1088
 
1089
	// wrong version
1090
	if ( fVersion > (float)FILEVERSION )
1091
	{
1092
		*error = INSTALLERR_VERSION;
1093
		return -1;
1094
	}
1095
 
1096
	return check;
1097
}
1098
 
1099
CMultiSpkFile *CPackages::OpenMultiPackage(CyString file, int *error, CProgressInfo *progress)
1100
{
1101
	int check = CheckOpenPackage(file, error);
1102
	if ( *error == -1 )
1103
		return false;
1104
 
1105
	if ( check != SPKFILE_MULTI )
1106
	{
1107
		*error = INSTALLERR_NOMULTI;
1108
		return false;
1109
	}
1110
 
1111
	*error = INSTALLERR_NONE;
1112
 
1113
	CMultiSpkFile *package = new CMultiSpkFile;
1114
 
1115
	bool ret = false;
1116
	if ( package->ReadFile(file, true) )
1117
	{
1118
		if ( package->ReadAllPackages(SPKREAD_NODATA) )
1119
		{
1120
			for ( CListNode<SMultiSpkFile> *node = package->GetFileList()->Front(); node; node = node->next() )
1121
				this->ConvertOldPackage(node->Data()->pFile);
1122
 
1123
			ret = true;
1124
		}
1125
	}
1126
 
1127
	if ( ret )
1128
		return package;
1129
 
1130
	delete package;
1131
	return NULL;
1132
}
1133
 
1134
bool CPackages::OpenMultiPackage ( CyString file, CLinkList<CBaseFile> *packageList, int *error, CProgressInfo *progress )
1135
{
1136
	int check = CheckOpenPackage(file, error);
1137
	if ( *error == -1 )
1138
		return false;
1139
 
1140
	if ( check != SPKFILE_MULTI )
1141
	{
1142
		*error = INSTALLERR_NOMULTI;
1143
		return false;
1144
	}
1145
 
1146
	*error = INSTALLERR_NONE;
1147
 
1148
	CMultiSpkFile *package = new CMultiSpkFile;
1149
 
1150
	bool ret = false;
1151
	if ( package->ReadFile(file, true) )
1152
	{
1153
		if ( package->ReadAllPackages(SPKREAD_ALL, packageList) )
1154
		{
1155
			for ( CListNode<CBaseFile> *node = packageList->Front(); node; node = node->next() )
1156
				this->ConvertOldPackage(node->Data());
1157
 
1158
 
1159
			ret = true;
1160
		}
1161
	}
1162
 
1163
	delete package;
1164
 
1165
	return ret;
1166
}
1167
 
1168
int CPackages::PrepareMultiPackage ( CyString file, CLinkList<CBaseFile> *errorPackageList, int *error, CProgressInfo *progress )
1169
{
1170
	int check = CheckOpenPackage(file, error);
1171
	if ( *error == -1 )
1172
		return 0;
1173
 
1174
	if ( check != SPKFILE_MULTI )
1175
	{
1176
		*error = INSTALLERR_NOMULTI;
1177
		return 0;
1178
	}
1179
 
1180
	*error = INSTALLERR_NONE;
1181
 
1182
	CMultiSpkFile *package = new CMultiSpkFile;
1183
 
1184
	int count = 0;
1185
	if ( package->ReadFile(file, true) )
1186
	{
1187
		CLinkList<CBaseFile> packageList;
1188
		if ( package->ReadAllPackages(SPKREAD_ALL, &packageList) )
1189
		{
1190
			for ( CListNode<CBaseFile> *node = packageList.Front(); node; node = node->next() )
1191
			{
1192
				CBaseFile *p = node->Data();
1193
				this->ConvertOldPackage(p);
1194
				int error = this->PrepareInstallPackage(p);
1195
				if ( error )
1196
				{
1197
					node->Data()->SetLoadError(error);
1198
					errorPackageList->push_back(p);
1199
				}
1200
				else
1201
					++count;
1202
			}
1203
		}
1204
		packageList.clear();
1205
	}
1206
 
1207
	for ( SMultiSpkFile *ms = package->GetFileList()->First(); ms; ms = package->GetFileList()->Next() )
1208
		ms->pFile = NULL;
1209
	delete package;
1210
 
1211
	return count;
1212
}
1213
 
1214
CBaseFile *CPackages::OpenPackage(CyString file, int *error, CProgressInfo *progress, int readtype, int flags)
1215
{
1216
	int check = CheckOpenPackage(file, error);
1217
	if ( *error == -1 )
1218
		return NULL;
1219
 
1220
	CBaseFile *installFile = 0;
1221
 
1222
	if ( progress )
1223
		progress->DoHalf();
1224
 
1225
	switch (check)
1226
	{
1227
		case SPKFILE_OLD:
1228
			*error = INSTALLERR_OLD;
1229
			return NULL;
1230
		case SPKFILE_INVALID:
1231
			// convert xsp
1232
			if ( CFileIO(file).CheckFileExtension("xsp") )
1233
			{
1234
				installFile = new CXspFile();
39 cycrow 1235
				if ( !((CXspFile *)installFile)->ConvertOld(file.ToString()) )
1 cycrow 1236
				{
1237
					delete installFile;
1238
					return NULL;
1239
				}
1240
				break;
1241
			}
1242
			*error = INSTALLERR_INVALID;
1243
			return NULL;
1244
		case SPKFILE_BASE:
1245
			installFile = new CBaseFile();
1246
			if ( !installFile->ReadFile(file, readtype, progress) )
1247
			{
1248
				delete installFile;
1249
				return NULL;
1250
			}
1251
			break;
1252
		case SPKFILE_SINGLE:
1253
			installFile = new CSpkFile();
1254
			if ( !((CSpkFile *)installFile)->ReadFile(file, readtype, progress) )
1255
			{
1256
				delete installFile;
1257
				return NULL;
1258
			}
1259
			break;
1260
		case SPKFILE_MULTI:
1261
			*error = INSTALLERR_NOMULTI;
1262
			return NULL;
1263
		case SPKFILE_SINGLESHIP:
1264
			installFile = new CXspFile();
1265
			if ( !((CXspFile *)installFile)->ReadFile(file, readtype, progress) )
1266
			{
1267
				delete installFile;
1268
				return NULL;
1269
			}
1270
			break;
1271
 
1272
		default:
1273
			*error = INSTALLERR_UNKNOWN;
1274
			return NULL;
1275
	}
1276
 
1277
	if ( progress )
1278
		progress->SecondHalf();
1279
 
1280
	// now uncomress all files
1281
	if ( !(flags & READFLAG_NOUNCOMPRESS) )
1282
		installFile->UncompressAllFiles(progress);
1283
 
1284
	this->ConvertOldPackage (installFile);
1285
 
1286
	return installFile;
1287
}
1288
 
1289
void CPackages::PurgeUninstallScripts(CBaseFile *package, CyStringList *errors)
1290
{
1291
	for ( CListNode<C_File> *fNode = m_lUninstallFiles.Front(); fNode; fNode = fNode->next() )
1292
	{
1293
		C_File *uf = fNode->Data();
1294
		// check against any script files
1295
		for ( CListNode<C_File> *checkNode = package->GetFileList()->Front(); checkNode; checkNode = checkNode->next() )
1296
		{
1297
			C_File *checkFile = checkNode->Data();
1298
			if ( !checkFile ) continue;
1299
 
1300
			if ( checkFile->GetFileType() != FILETYPE_UNINSTALL && checkFile->GetFileType() != FILETYPE_SCRIPT )
1301
				continue;
1302
 
1303
			if ( uf->GetFilename().Compare(checkFile->GetFilename()) )
1304
			{
1305
				if ( RemoveUninstallFile(uf, errors) )
1306
					fNode->DeleteData();
1307
				break;
1308
			}
1309
		}
1310
	}
1311
 
1312
	m_lUninstallFiles.RemoveEmpty();
1313
 
1314
	this->WriteData();
1315
}
1316
 
1317
int CPackages::InstallPreparedPackages(CyStringList *errors, CProgressInfo *progress, CLinkList<CBaseFile> *errored, CLinkList<CBaseFile> *installedList)
1318
{
1319
	if ( m_lInstallList.empty() ) return false;
1320
 
1321
	int installed = 0;
1322
	for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
1323
	{
1324
		CBaseFile *p = node->Data();
1325
		if ( this->InstallPackage(p, errors, progress, !p->IsEnabled()) )
1326
		{
1327
			++installed;
1328
			if ( installedList )
1329
				installedList->push_back(p);
1330
		}
1331
		else if ( errored )
1332
		{
1333
			m_lPackages.remove(p);
1334
			errored->push_back(p);
1335
		}
1336
	}
1337
 
1338
	m_lInstallList.clear();
1339
 
1340
	this->WriteData();
1341
 
1342
	return installed;
1343
}
1344
 
50 cycrow 1345
void CPackages::_addToFakePatch(CBaseFile *pPackage)
1346
{
51 cycrow 1347
	CLog::logf(CLog::Log_Install, 4, "Preparing to move files to fake patch, %s", (m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat").c_str());
50 cycrow 1348
	CCatFile cat;
1349
	if ( CCatFile::Opened(cat.Open(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat", this->GetAddonDir(), CATREAD_DAT)) ) {
1350
		for ( CListNode<C_File> *f = pPackage->GetFileList()->Front(); f; f = f->next() ) {
51 cycrow 1351
			CLog::logf(CLog::Log_Install, 4, "Start next file...");
1352
			CLog::logf(CLog::Log_Install, 4, "File: %s", f->Data()->GetFilePointer().c_str());
1353
			if ( f->Data()->GetFileType() != FILETYPE_SHIPSCENE && f->Data()->GetFileType() != FILETYPE_COCKPITSCENE && f->Data()->GetFileType() != FILETYPE_SHIPMODEL && f->Data()->GetFileType() != FILETYPE_SHIPOTHER ) {
1354
				CLog::logf(CLog::Log_Install, 4, "Not a valid file type to add to fake patch (%d)", f->Data()->GetFileType());
50 cycrow 1355
				continue;
51 cycrow 1356
			}
1357
			if ( CCatFile::IsAddonDir(f->Data()->GetNameDirectory(pPackage)) ) {
1358
				CLog::logf(CLog::Log_Install, 4, "Is an addon directory, skipping, %s", f->Data()->GetNameDirectory(pPackage).c_str());
50 cycrow 1359
				continue;
51 cycrow 1360
			}
1361
			// check if its already in the fake patch
1362
			if ( f->Data()->GetFullDir().IsIn("::") ) {
1363
				CLog::logf(CLog::Log_Install, 4, "Is already in a mod file, skipping, %s", f->Data()->GetNameDirectory(pPackage).c_str());
1364
				continue;
1365
			}
50 cycrow 1366
			CyString toFile;
51 cycrow 1367
			CLog::logf(CLog::Log_Install, 2, "Adding file: %s into the fake patch", f->Data()->GetNameDirectory(pPackage).c_str());
50 cycrow 1368
			if ( cat.AppendFile(f->Data()->GetFilePointer(), f->Data()->GetNameDirectory(pPackage), true, (m_iGameFlags & EXEFLAG_NOXOR) ? false : true, &toFile) ) {
51 cycrow 1369
				CLog::logf(CLog::Log_Install, 3, "Removing old file, %s", f->Data()->GetFilePointer().c_str());
50 cycrow 1370
				CFileIO(f->Data()->GetFilePointer()).Remove();
51 cycrow 1371
				CLog::logf(CLog::Log_Install, 3, "Adjusting file pointer to fake patch, %s => %s", f->Data()->GetFilePointer().c_str(), (m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat::" + toFile).c_str());
50 cycrow 1372
				f->Data()->SetFilename(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat::" + toFile);
1373
			}
1374
		}
1375
	}
1376
}
1377
 
1 cycrow 1378
bool CPackages::InstallPackage ( CBaseFile *package, CyStringList *errors, CProgressInfo *progress, bool disabled )
1379
{
50 cycrow 1380
	CLog::logf(CLog::Log_Install, 1, "Starting to install new package, %s by %s (Version: %s)", package->name().c_str(), package->author().c_str(), package->version().c_str());
1 cycrow 1381
	// first check if we are installed a mod
1382
	bool prevDisabled = disabled;
50 cycrow 1383
	if ( package->IsMod() && m_pEnabledMod && !m_bForceModInstall ) {
1 cycrow 1384
		disabled = true;
50 cycrow 1385
		CLog::log(CLog::Log_Install, 2, "Package is a mod and another mod is already enabled, setting to disabled");
1386
	}
1 cycrow 1387
	// if vanilla nad package aint signed
50 cycrow 1388
	if ( m_bVanilla && !package->IsSigned() ) {
1 cycrow 1389
		disabled = true;
50 cycrow 1390
		CLog::log(CLog::Log_Install, 2, "Package is a not signed and are we in vanilla mode, setting to disabled");
1391
	}
1 cycrow 1392
 
1393
	// check any depancies
50 cycrow 1394
	if ( !disabled && !this->CheckEnabledDependacy(package) ) {
1 cycrow 1395
		disabled = true;
50 cycrow 1396
		CLog::log(CLog::Log_Install, 2, "Dependacies missing for package, setting to disabled");
1397
	}
1 cycrow 1398
 
1399
	// search for an old version
1400
	CBaseFile *oldPackage = FindPackage(package);
1401
	if ( oldPackage && oldPackage == m_pEnabledMod && disabled )
1402
		disabled = prevDisabled;
1403
 
1404
	// update packages must have an old package installed already (should have been checked for already)
1405
	if ( package->GetType() == TYPE_SPK )
1406
	{
1407
		if ( ((CSpkFile *)package)->IsPackageUpdate() )
1408
		{
50 cycrow 1409
			CLog::log(CLog::Log_Install, 3, "Package is an Update, checking for existing package installed");
1410
			if ( !oldPackage ) {
1411
				CLog::log(CLog::Log_Install, 2, "Package is an Update but no existing package found, cancelling install");
1 cycrow 1412
				return false;
50 cycrow 1413
			}
1 cycrow 1414
			// change any mods to temp ones
1415
			for ( CListNode<C_File> *f = package->GetFileList()->Front(); f; f = f->next() )
1416
			{
1417
				if ( f->Data()->GetFileType() != FILETYPE_MOD )
1418
					continue;
1419
 
1420
				f->Data()->SetDir("temp");
50 cycrow 1421
				if ( f->Data()->IsFakePatch() ) {
1422
					CLog::logf(CLog::Log_Install, 2, "Moving fake package to temporary location to preper for update, %s", f->Data()->GetFilePointer().c_str());
1 cycrow 1423
					f->Data()->SetName(CyString("Fake_") + f->Data()->GetName());
50 cycrow 1424
				}
1 cycrow 1425
			}
1426
		}
1427
	}
1428
 
1429
	// no need to backup if we're disabling them
1430
	if ( !disabled )
1431
	{
1432
		// find any uninstall files and remove them
50 cycrow 1433
		CLog::log(CLog::Log_Install, 3, "Purging uninstall scripts");
1 cycrow 1434
		this->PurgeUninstallScripts(package, errors);
1435
 
1436
		// backup any original files before installing
50 cycrow 1437
		CLog::log(CLog::Log_Install, 3, "Checking for any original files to backup");
1 cycrow 1438
		CDirIO oDir(m_sCurrentDir + "/PluginManager/Original");
1439
		for ( CListNode<C_File> *oNode = m_lOriginalFiles.Front(); oNode; oNode = oNode->next() )
1440
		{
1441
			C_File *of = oNode->Data();
1442
			for ( CListNode<C_File> *checkNode = package->GetFileList()->Front(); checkNode; checkNode = checkNode->next() )
1443
			{
1444
				C_File *f = checkNode->Data();
1445
 
1446
				// match the same filetype
1447
				if ( of->GetFileType() != f->GetFileType() )
1448
					continue;
1449
 
1450
				// same file
1451
				if ( of->GetFilename().Compare(f->GetFilename()) )
1452
				{
1453
					// check if original file already exists (assume already backed up)
1454
					if ( !BackupOriginalFile(of, errors) )
1455
						continue;
1456
					break;
1457
				}
1458
			}
1459
		}
1460
	}
1461
 
1462
	// install all the files
50 cycrow 1463
	CLog::log(CLog::Log_Install, 3, "Checking for any existing files");
1 cycrow 1464
	if ( oldPackage )
1465
	{
50 cycrow 1466
		CLog::logf(CLog::Log_Install, 3, "Excluding existing package (%s) from file check list", oldPackage->version().c_str());
1 cycrow 1467
		CLinkList<CBaseFile> excludeList;
1468
		excludeList.push_back(oldPackage);
1469
		this->UpdateUsedFiles(&excludeList, true);
1470
	}
1471
	else
1472
		this->UpdateUsedFiles(0, true);
1473
 
50 cycrow 1474
	CLog::log(CLog::Log_Install, 3, "Reading all files into memory");
1 cycrow 1475
	package->ReadAllFilesToMemory();
50 cycrow 1476
 
1477
	CLog::log(CLog::Log_Install, 3, "Starting to install files");
1 cycrow 1478
	if ( !package->InstallFiles (m_sCurrentDir, progress, &m_lFiles, errors, !disabled, this) )
1479
	{
50 cycrow 1480
		CLog::log(CLog::Log_Install, 2, "There was an error installing files!!");
1481
 
1 cycrow 1482
		// TODO: clear up installed files
1483
		return false;
1484
	}
1485
 
1486
	// if we're installing an addon, lets use the fake patch method for object files
50 cycrow 1487
	if ( m_iGameFlags & EXEFLAG_ADDON ) this->_addToFakePatch(package);
1 cycrow 1488
 
1489
	bool shuffle = false;
1490
 
1491
	// merge the update into the old package
1492
	bool dontAdd = false;
1493
	if ( package->GetType() == TYPE_SPK )
1494
	{
1495
		if ( ((CSpkFile *)package)->IsPackageUpdate() )
1496
		{
1497
			// now copy any files from a mod
1498
			for ( CListNode<C_File> *f = package->GetFileList()->Front(); f; f = f->next() )
1499
			{
1500
				if ( !f->Data() )
1501
					continue;
1502
				if ( f->Data()->GetFileType() != FILETYPE_MOD )
1503
					continue;
1504
				// we only need the cat file
1505
				if ( !f->Data()->CheckFileExt("cat") )
1506
					continue;
1507
 
1508
				// if fake patch, find first fake patch in package
1509
				C_File *findMatching = NULL;
1510
				if ( f->Data()->GetBaseName().Left(5).Compare("fake_") )
1511
				{
1512
					for ( CListNode<C_File> *node = oldPackage->GetFileList()->Front(); node; node = node->next() )
1513
					{
1514
						if ( !node->Data() )
1515
							continue;
1516
						if ( node->Data()->GetFileType() != FILETYPE_MOD )
1517
							continue;
1518
						// we only need the cat file
1519
						if ( !node->Data()->CheckFileExt("cat") )
1520
							continue;
1521
						if ( !node->Data()->IsFakePatch() )
1522
							continue;
1523
 
1524
						findMatching = node->Data();
1525
						break;
1526
					}
1527
				}
1528
				// otherwise, just add to the mod of the same name
1529
				else
1530
				{
1531
					for ( CListNode<C_File> *node = oldPackage->GetFileList()->Front(); node; node = node->next() )
1532
					{
1533
						if ( !node->Data() )
1534
							continue;
1535
						if ( node->Data()->GetFileType() != FILETYPE_MOD )
1536
							continue;
1537
						// we only need the cat file
1538
						if ( !node->Data()->CheckFileExt("cat") )
1539
							continue;
1540
 
1541
						if ( node->Data()->GetName().Compare(f->Data()->GetName()) )
1542
						{
1543
							findMatching = node->Data();
1544
							break;
1545
						}
1546
					}
1547
				}
1548
 
1549
				if ( findMatching )
1550
				{
1551
					// copy accross all mods
1552
					CCatFile catTo, catFrom;
1553
					if ( catFrom.Open(f->Data()->GetFilePointer(), this->GetAddonDir(), CATREAD_CATDECRYPT, false) == CATERR_NONE )
1554
					{
1555
						if ( catTo.Open(findMatching->GetFilePointer(), this->GetAddonDir(), CATREAD_CATDECRYPT, false) == CATERR_NONE )
1556
						{
1557
							for ( int i = 0; i < catFrom.GetNumFiles(); i++ )
1558
							{
1559
								SInCatFile *c = catFrom.GetFile(i);								 
1560
								catTo.AppendFile(f->Data()->GetFilePointer() + "::" + c->sFile, c->sFile);
1561
							}
1562
						}
1563
					}
1564
 
1565
					// now remove the files
1566
					C_File *m = package->FindMatchingMod(f->Data());
1567
					RemoveFile(f->Data(), errors);
1568
					m_lFiles.remove(f->Data());
1569
					f->ChangeData(NULL);
1570
					if ( m )
1571
					{
1572
						int pos = package->GetFileList()->FindPos(m);
1573
						RemoveFile(m, errors);
1574
						m_lFiles.remove(m);
1575
						if ( pos != -1 )
1576
							package->GetFileList()->GetNode(pos)->ChangeData(NULL);
1577
					}
1578
				}
1579
				// no matching file, then we shall just renaming back
1580
				else
1581
				{
1582
					if ( f->Data()->GetBaseName().Left(5).Compare("fake_") )
1583
					{
1584
						shuffle = true;
1585
						C_File *match = package->FindMatchingMod(f->Data());
1586
						CyString next = CyString::Number(this->FindNextFakePatch()).PadNumber(2);
1587
 
1588
						CyString oldFilePointer = f->Data()->GetFilePointer();
1589
						f->Data()->SetDir("");
1590
						f->Data()->ChangeBaseName(next);
1591
						if ( CFileIO(oldFilePointer).Rename(m_sCurrentDir + "/" + f->Data()->GetNameDirectory(package)) )
1592
							f->Data()->SetFilename(m_sCurrentDir + "/" + f->Data()->GetNameDirectory(package));
1593
 
1594
						if ( match )
1595
						{
1596
							CyString oldFilePointer = match->GetFilePointer();
1597
							match->SetDir("");
1598
							match->ChangeBaseName(next);
1599
							if ( CFileIO(oldFilePointer).Rename(m_sCurrentDir + "/" + match->GetNameDirectory(package)) )
1600
								match->SetFilename(m_sCurrentDir + "/" + match->GetNameDirectory(package));
1601
						}
1602
					}
1603
					else
1604
					{
1605
						C_File *match = package->FindMatchingMod(f->Data());
1606
 
1607
						f->Data()->SetDir("");
1608
						if ( CFileIO(f->Data()->GetFilePointer()).Rename(m_sCurrentDir + "/" + f->Data()->GetNameDirectory(package)) )
1609
							f->Data()->SetFilename(m_sCurrentDir + "/" + f->Data()->GetNameDirectory(package));
1610
						if ( match )
1611
						{
1612
							match->SetDir("");
1613
							if ( CFileIO(match->GetFilePointer()).Rename(m_sCurrentDir + "/" + match->GetNameDirectory(package)) )
1614
								match->SetFilename(m_sCurrentDir + "/" + match->GetNameDirectory(package));
1615
						}
1616
					}
1617
				}
1618
			}
1619
 
1620
			package->GetFileList()->RemoveEmpty();
1621
			((CSpkFile *)oldPackage)->MergePackage(package);
1622
			delete package;
1623
			package = oldPackage;
1624
			oldPackage = false;
1625
			dontAdd = true;
1626
 
1627
			CDirIO(m_sCurrentDir).RemoveDir("temp");
1628
			CDirIO(m_sCurrentDir).RemoveDir("Mods/temp");
1629
		}
1630
	}
1631
 
1632
	// if theres an icon, write it
1633
	if ( package->GetIcon() )
1634
	{
50 cycrow 1635
		CLog::log(CLog::Log_Install, 3, "Checking to install icon display file");
1 cycrow 1636
		C_File *icon = package->GetIcon();
50 cycrow 1637
		if ( !icon->GetData() || !icon->GetDataSize() ) {
1 cycrow 1638
			package->SetIcon(NULL, "");
50 cycrow 1639
			CLog::log(CLog::Log_Install, 2, "Unable to extract icon, clearing");
1640
		}
1 cycrow 1641
		else
1642
		{
1643
			CDirIO Dir(m_sCurrentDir);
1644
			bool ready = true;
1645
			if ( !Dir.Exists("PluginManager") )
1646
			{
1647
				if ( !Dir.Create("PluginManager") )
1648
					ready = false;
1649
			}
1650
			if ( ready && !Dir.Exists("PluginManager/Icons") )
1651
			{
1652
				if ( !Dir.Create("PluginManager/Icons") )
1653
					ready = false;
1654
			}
1655
 
1656
			if ( ready )
1657
			{
1658
				if ( !icon->UncompressData() )
1659
					package->SetIcon(NULL, "");
1660
				else
1661
				{
50 cycrow 1662
					CFileIO iconFile(m_sCurrentDir + "/PluginManager/Icons/" + package->author() + "_" + package->name() + "." + package->GetIconExt());
1 cycrow 1663
					if ( iconFile.WriteData((const char *)icon->GetData(), icon->GetDataSize()) )
1664
					{
50 cycrow 1665
						icon->SetFilename(CyString(package->author()) + "_" + package->name() + "." + package->GetIconExt());
1 cycrow 1666
						icon->SetFullDir(m_sCurrentDir + "/PluginManager/Icons");
1667
					}
1668
					else
1669
						package->SetIcon(NULL, "");
1670
				}
1671
			}
1672
 
1673
			if ( package->GetIcon() )
1674
				package->GetIcon()->DeleteData();
1675
		}
1676
	}
1677
 
1678
	// remove all data
50 cycrow 1679
	CLog::log(CLog::Log_Install, 3, "Clearing all unneeded file data");
1 cycrow 1680
	package->ClearFileData();
1681
 
1682
	// add to list
1683
	if ( !dontAdd )
1684
	{
50 cycrow 1685
		CLog::log(CLog::Log_Install, 1, "Adding package into main list");
1 cycrow 1686
		if ( oldPackage )
1687
		{
1688
			m_lPackages.insert(oldPackage, package);
1689
			m_lPackages.remove(oldPackage, false);
1690
		}
1691
		else
1692
			m_lPackages.push_back (package);
1693
	}
1694
 
50 cycrow 1695
	CLog::log(CLog::Log_Install, 2, "Updating file used count");
1 cycrow 1696
	UpdateUsedFiles();
1697
 
50 cycrow 1698
	if ( disabled ) package->SetEnabled(false);
1 cycrow 1699
 
1700
	// remove any files no longer used by old package
1701
	if ( oldPackage )
1702
	{
50 cycrow 1703
		CLog::log(CLog::Log_Install, 3, "Removing any unused files from previous package");
1704
 
1 cycrow 1705
		CListNode<C_File> *fnode = oldPackage->GetFileList()->Front();
1706
		while ( fnode )
1707
		{
1708
			C_File *f = fnode->Data();
1709
 
1710
			// no longer used
1711
			if ( !f->GetUsed() )
1712
			{
1713
				// remove from file list
1714
				m_lFiles.remove(f, false);
1715
 
1716
				// remove from hard drive
1717
				if ( RemoveFile(f, errors) )
1718
				{
50 cycrow 1719
					CLog::logf(CLog::Log_Install, 1, "Removed unused file: %s", f->GetFilePointer().c_str());
1 cycrow 1720
					// if a fake patch, we need to shufle
1721
					if ( f->IsFakePatch() )
1722
						shuffle = true;
1723
					else if ( f->IsAutoTextFile() )
1724
						shuffle = true;
1725
				}
1726
			}
1727
 
1728
			fnode = fnode->next();
1729
		}
1730
 
1731
	}
1732
 
1733
	if ( shuffle )
1734
	{
50 cycrow 1735
		CLog::log(CLog::Log_Install, 2, "Shuffling Fake patches");
1 cycrow 1736
		ShuffleFakePatches(errors);
50 cycrow 1737
		CLog::log(CLog::Log_Install, 2, "Shuffling Text Files");
1 cycrow 1738
		ShuffleTextFiles(errors);
1739
	}
1740
 
1741
	// now we need to link any child/parent packages
1742
	if ( package->GetType() == TYPE_SPK )
1743
	{
1744
		CSpkFile *spk = (CSpkFile *)package;
50 cycrow 1745
		if ( spk->IsAnotherMod() ) {
1 cycrow 1746
			spk->SetParent((CSpkFile *)FindSpkPackage(spk->GetOtherName(), spk->GetOtherAuthor()));
50 cycrow 1747
			CLog::logf(CLog::Log_Install, 2, "Linking to parent package: %s by %s (Version: %s)", spk->name().c_str(), spk->author().c_str(), spk->version().c_str());
1748
		}
1 cycrow 1749
	}
1750
 
1751
	// store enabled mod
50 cycrow 1752
	if ( package->IsMod() && !disabled ) {
1 cycrow 1753
		m_pEnabledMod = package;
50 cycrow 1754
		CLog::log(CLog::Log_Install, 1, "Setting package as primary mod");
1755
	}
1 cycrow 1756
 
1757
	m_bRemoveDir = true;
1758
 
50 cycrow 1759
	CLog::log(CLog::Log_Install, 1, "Saving data to file");
1 cycrow 1760
	this->WriteData();
1761
 
50 cycrow 1762
	CLog::log(CLog::Log_Install, 1, "Installation Finished");
1 cycrow 1763
	return true;
1764
}
1765
 
1766
bool CPackages::UninstallPreparedPackages(CyStringList *errors, CProgressInfo *progress, CLinkList<CBaseFile> *uninstalledPackages, CLinkList<CBaseFile> *disabledPackages)
1767
{
1768
	if ( m_lInstallList.empty() ) return false;
1769
 
1770
	// update the used status, excluding all packages we are about to remove
1771
	UpdateUsedFiles(&m_lInstallList);
1772
 
1773
	CyStringList removeDirs;
1774
	CLinkList<C_File> uninstallFiles;
1775
	CLinkList<C_File> fileList;
1776
	bool readme = false, original = false, shuffle = false;
1777
 
1778
	// find all files that need to be removed
1779
	int maxFiles = 0;
1780
	for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
1781
	{
1782
		CBaseFile *p = node->Data();
1783
		maxFiles += this->GetAllPackageFiles(p, &fileList, true);
1784
 
1785
		// disable any dependants
1786
		if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
1787
		{
1788
			CLinkList<CBaseFile> depList;
1789
			if ( this->GetDependacyList(p, &depList) )
1790
			{
1791
				for ( CBaseFile *depP = depList.First(); depP; depP = depList.Next() )
1792
				{
1793
					if ( depP->IsEnabled() )
1794
						this->PrepareDisablePackage(depP);
1795
				}
1796
 
1797
				if ( m_lDisableList.size() )
1798
					this->DisablePreparedPackages(errors, progress, disabledPackages);
1799
			}
1800
		}
1801
	}
1802
 
1803
	// interate through all the files in the package
1804
	int fileCount = 0;
1805
	for ( CListNode<C_File> *node = fileList.Front(); node; node = node->next() )
1806
	{
1807
		C_File *f = node->Data();
1808
 
1809
		// display progress if needed
1810
		if ( progress )
1811
		{
1812
			progress->UpdateProgress(fileCount++, maxFiles);
1813
			progress->UpdateFile(f);
1814
		}
1815
 
1816
		// skip uninstall files
1817
		if ( f->GetFileType() == FILETYPE_UNINSTALL )
1818
		{
1819
			uninstallFiles.push_back(f);
1820
			continue;
1821
		}
1822
 
1823
		// only delete files that are not used
1824
		// if its a shared file, we skip it
1825
		if ( f->GetUsed() || f->IsShared() )
1826
			continue;
1827
 
1828
		if ( f->GetFileType() == FILETYPE_README )
1829
			readme = true;
1830
		else if ( f->GetFileType() == FILETYPE_UNINSTALL || f->GetFileType() == FILETYPE_MAP || f->GetFileType() == FILETYPE_SOUND || f->GetFileType() == FILETYPE_EXTRA || f->GetFileType() == FILETYPE_SHIPSCENE || f->GetFileType() == FILETYPE_COCKPITSCENE || f->GetFileType() == FILETYPE_SHIPOTHER || f->GetFileType() == FILETYPE_SHIPMODEL || f->GetFileType() == FILETYPE_ADVERT )
1831
		{
1832
			CyString dir = f->GetDirectory(NULL);
1833
			removeDirs.PushBack(dir, true);
1834
			dir = dir.FindReplace("\\", "/");
1835
			if ( dir.IsIn("/") )
1836
			{
1837
				for ( int i = dir.NumToken("/"); i; i-- )
1838
				{
1839
					CyString remDir = dir.GetToken("/", 1, i);
1840
					removeDirs.PushBack(remDir, true);
1841
				}
1842
			}
1843
		}
1844
 
1845
		if ( f->GetFileType() == FILETYPE_EXTRA && f->GetDir().Left(6).lower() == "extras" )
1846
			removeDirs.PushBack("Extras", true);
1847
 
1848
		if ( RemoveFile(f, errors) )
1849
		{
1850
			// check if its an original file and restore
1851
			if ( IsOriginalFile(f) )
1852
			{
1853
				CFileIO of(m_sCurrentDir + "/PluginManager/Original/" + f->GetNameDirectory(NULL));
1854
				if ( of.Exists() )
1855
				{
1856
					original = true;
1857
					if ( of.Rename(m_sCurrentDir + "/" + f->GetNameDirectory(NULL)) )
1858
						this->AddLogEntry(SPKINSTALL_ORIGINAL_RESTORE, f->GetNameDirectory(NULL), errors);
1859
					else
1860
						this->AddLogEntry(SPKINSTALL_ORIGINAL_RESTORE_FAIL, f->GetNameDirectory(NULL), errors);
1861
				}
1862
			}
1863
		}
1864
		else // problem removeing (try when the program closes)
1865
			m_lNonRemovedFiles.PushBack(f->GetFilePointer());
1866
 
1867
		// check for fake patchs
1868
		if ( f->IsFakePatch() )
1869
			shuffle = true;
1870
		else if ( f->IsAutoTextFile() )
1871
			shuffle = true;
1872
 
1873
		// remove the file from the main list as swell
1874
		m_lFiles.remove(f, false);
1875
		delete f;
1876
	}
1877
 
1878
	// remove all the packages from memory
1879
	for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
1880
	{
1881
		CBaseFile *p = node->Data();
1882
		p->GetFileList()->clear();
1883
		m_lPackages.remove(p);
1884
 
1885
		if ( p == m_pEnabledMod )
1886
			m_pEnabledMod = NULL;
1887
 
1888
		if ( uninstalledPackages )
1889
			uninstalledPackages->push_back(p);
1890
		else
1891
			delete p;
1892
	}
1893
 
1894
	m_lInstallList.clear();
1895
 
1896
	// check unistall files
1897
	if ( !uninstallFiles.empty() )
1898
	{
1899
		removeDirs.PushBack(CyString("PluginManager/Uninstall"));
1900
 
1901
		// make sure the scripts directory is created, even thou it should always be there anyways
1902
		CDirIO scriptDir(m_sCurrentDir);
1903
		if ( !scriptDir.Exists("scripts") )
1904
		{
1905
			if ( scriptDir.Create("Scripts") )
1906
				this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY, "Scripts", errors);
1907
			else
1908
				this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY_FAIL, "Scripts", errors);
1909
		}
1910
 
1911
		for ( C_File *uf = uninstallFiles.First(); uf; uf = uninstallFiles.Next() )
1912
		{
1913
			C_File *newFile = new C_File();
1914
			newFile->SetFileType(FILETYPE_SCRIPT);
1915
			newFile->SetFilename(uf->GetFilename());
1916
			newFile->SetCreationTime(uf->GetCreationTime());
1917
 
1918
			// other installed packages use this file as well, copy it
1919
			CyString newFilename = m_sCurrentDir + "/" + newFile->GetNameDirectory(NULL);
1920
			CFileIO file(uf->GetFilePointer());
1921
 
1922
			if ( uf->GetUsed() )
1923
			{
1924
				if ( file.Copy(newFilename) )
1925
					this->AddLogEntry(SPKINSTALL_UNINSTALL_COPY, newFile->GetNameDirectory(NULL), errors);
1926
				else
1927
				{
1928
					this->AddLogEntry(SPKINSTALL_UNINSTALL_COPY_FAIL, newFile->GetNameDirectory(NULL), errors);
1929
					delete newFile;
1930
					newFile = NULL;
1931
				}
1932
			}
1933
			// otherwise just move it
1934
			else
1935
			{
1936
				if ( file.Rename(newFilename) )
1937
					this->AddLogEntry(SPKINSTALL_UNINSTALL_MOVE, newFile->GetNameDirectory(NULL), errors);
1938
				else
1939
				{
1940
					this->AddLogEntry(SPKINSTALL_UNINSTALL_MOVE_FAIL, newFile->GetNameDirectory(NULL), errors);
1941
					delete newFile;
1942
					newFile = NULL;
1943
				}
1944
 
1945
				m_lFiles.remove(uf, false);
1946
				delete uf;
1947
			}
1948
 
1949
			// add to the list
1950
			if ( newFile )
1951
			{
1952
				// first check if theres a matching one
1953
				bool found = false;
1954
				for ( CListNode<C_File> *node = m_lUninstallFiles.Front(); node; node = node->next() )
1955
				{
1956
					C_File *checkFile = node->Data();
1957
					if ( checkFile->GetFilename().Compare(newFile->GetFilename()) )
1958
					{
1959
						found = true;
1960
						break;
1961
					}
1962
				}
1963
 
1964
				// not found, so add it
1965
				if ( !found )
1966
					m_lUninstallFiles.push_back(newFile);
1967
				else
1968
					delete newFile;
1969
			}
1970
		}
1971
	}
1972
	uninstallFiles.clear();
1973
 
1974
	// remove all directies that we're not using
1975
	if ( readme )
1976
	{
1977
		removeDirs.PushBack(CyString("PluginManager/Readme"));
1978
		removeDirs.PushBack(CyString("Readme"));
1979
	}
1980
	if ( original )
1981
		removeDirs.PushBack(CyString("PluginManager/Original"));
1982
	removeDirs.PushBack(CyString("PluginManager/Disabled"));
1983
	RemoveUnusedDirectories(removeDirs, errors);
1984
 
1985
	// finally lets shuffle any fake patchs to fill in gaps
1986
	if ( shuffle )
1987
	{
1988
		ShuffleFakePatches(errors);
1989
		ShuffleTextFiles(errors);
1990
	}
1991
 
1992
	this->WriteData();
1993
 
1994
	return true;
1995
}
1996
 
1997
/**
1998
 * Prepares a package to be uninstalled
1999
 *
2000
 * Adds the package, and all its children onto the uninstall list to be used by UninstallPreparePackages
2001
 */
2002
void CPackages::PrepareUninstallPackage(CBaseFile *package)
2003
{
2004
	// add package to list
2005
	if ( !m_lInstallList.FindData(package) )
2006
		m_lInstallList.push_back(package);
2007
 
2008
	// add all children
2009
	CLinkList<CBaseFile> children;
2010
	if ( this->GetChildPackages(package, &children, true) )
2011
	{
2012
		for ( CBaseFile *p = children.First(); p; p = children.Next() )
2013
		{
2014
			if ( !m_lInstallList.FindData(p) )
2015
				m_lInstallList.push_back(p);
2016
		}
2017
	}
2018
}
2019
 
2020
bool CPackages::PrepareEnablePackage(CBaseFile *package)
2021
{
2022
	ClearError();
2023
 
2024
	if ( package->GetParent() && !package->GetParent()->IsEnabled() )
2025
	{
2026
		m_iError = PKERR_NOPARENT;
2027
		return false;
2028
	}
2029
 
2030
	if ( m_bVanilla && !package->IsSigned() )
2031
	{
2032
		m_iError = PKERR_MODIFIED;
2033
		return false;
2034
	}
2035
 
2036
	// check if it needs depancies
2037
	if ( package->AnyDependacies() )
2038
	{
2039
		if ( this->GetMissingDependacies(package, NULL, true) )
2040
		{
2041
			m_iError = PKERR_MISSINGDEP;
2042
			return false;
2043
		}
2044
	}
2045
 
2046
	if ( !m_lEnableList.FindData(package) )
2047
	{
2048
		if ( !package->IsEnabled() )
2049
			m_lEnableList.push_back(package);
2050
 
2051
		// do all the children as well
2052
		if ( m_bAutoEnableChild )
2053
		{
2054
			CLinkList<CBaseFile> childList;
2055
			this->GetChildPackages(package, &childList, true);
2056
 
2057
			// add all disabled packages to list
2058
			for ( CBaseFile *p = childList.First(); p; p = childList.Next() )
2059
			{
2060
				if ( !p->IsEnabled() )
2061
					m_lEnableList.push_back(p);
2062
			}
2063
		}
2064
	}
2065
 
2066
	m_bRemoveDir = true;
2067
 
2068
	return true;
2069
}
2070
 
2071
bool CPackages::PrepareDisableForVanilla()
2072
{
2073
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
2074
	{
2075
		CBaseFile *p = node->Data();
2076
		if ( !p->IsSigned() && p->IsEnabled() )
2077
		{
2078
			this->PrepareDisablePackage(p);
2079
			m_bDisableVanilla = true;
2080
		}
2081
	}
2082
 
2083
	return true;
2084
}
2085
 
2086
bool CPackages::PrepareEnableLibrarys()
2087
{
2088
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
2089
	{
2090
		CBaseFile *p = node->Data();
2091
		if ( p->GetType() != TYPE_SPK )
2092
			continue;
2093
 
2094
		if ( !p->IsEnabled() && ((CSpkFile *)p)->IsLibrary() )
2095
			this->PrepareEnablePackage(p);
2096
	}
2097
 
2098
	return true;
2099
}
2100
 
2101
bool CPackages::PrepareEnableFromVanilla()
2102
{
2103
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
2104
	{
2105
		CBaseFile *p = node->Data();
2106
		if ( !p->IsEnabled() && p->IsModifiedEnabled() ) {
2107
			this->PrepareEnablePackage(p);
2108
		}
2109
	}
2110
 
2111
	return true;
2112
}
2113
 
2114
int CPackages::GetDependacyList(CBaseFile *package, CLinkList<CBaseFile> *list)
2115
{
2116
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
2117
	{
2118
		CBaseFile *p = node->Data();
50 cycrow 2119
		if ( p->IsPackageNeeded(package->name(), package->author()) )
1 cycrow 2120
			list->push_back(p);
2121
	}
2122
 
2123
	return list->size();
2124
}
2125
 
2126
bool CPackages::PrepareDisablePackage(CBaseFile *package)
2127
{
2128
	if ( !m_lDisableList.FindData(package) )
2129
	{
2130
		if ( package->IsEnabled() )
2131
			m_lDisableList.push_back(package);
2132
 
2133
		CLinkList<CBaseFile> childList;
2134
		this->GetChildPackages(package, &childList, true);
2135
 
2136
		// add all disabled packages to list
2137
		for ( CBaseFile *p = childList.First(); p; p = childList.Next() )
2138
		{
2139
			if ( p->IsEnabled() && !m_lDisableList.FindData(p) )
2140
				m_lDisableList.push_back(p);
2141
		}
2142
 
2143
		// if its a library, check for any dependacies
2144
		if ( package->GetType() == TYPE_SPK && ((CSpkFile *)package)->IsLibrary() )
2145
		{
2146
			CLinkList<CBaseFile> depList;
2147
			if ( this->GetDependacyList(package, &depList) )
2148
			{
2149
				for ( CBaseFile *p = depList.First(); p; p = depList.Next() )
2150
				{
2151
					if ( !m_lDisableList.FindData(p) )
2152
						this->PrepareDisablePackage(p);
2153
				}
2154
			}
2155
		}
2156
	}
2157
	return true;
2158
}
2159
 
2160
/**
2161
 * Prepares a package to be installed
2162
 */
2163
int CPackages::PrepareInstallPackage(CBaseFile *package, bool disabled, bool force, int check)
2164
{
2165
	// add package to list
2166
	if ( !m_lInstallList.FindData(package) )
2167
	{
2168
		int error = this->CheckInstallPackage(package, check);
2169
		if ( error == INSTALLCHECK_OK || force )
2170
		{
2171
			if ( disabled )
2172
				package->SetEnabled(false);
2173
 
2174
			bool added = false;
2175
			if ( package->GetType() == TYPE_SPK )
2176
			{
2177
				// find other mods in the currently added list
2178
				CSpkFile *spk = (CSpkFile *)package;
2179
				if ( spk->IsAnotherMod() )
2180
				{
2181
					for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
2182
					{
2183
						CBaseFile *p = node->Data();
50 cycrow 2184
						if ( spk->GetOtherName().Compare(p->name()) && spk->GetOtherAuthor().Compare(p->author()) )
1 cycrow 2185
						{
2186
							m_lInstallList.insert(m_lInstallList.FindPos(p) + 2, package);
2187
							added = true;
2188
							break;
2189
						}
2190
					}
2191
				}
2192
			}
2193
 
2194
			if ( !added )
2195
			{
2196
				// check if we are a parent
2197
				for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
2198
				{
2199
					if ( node->Data()->GetType() != TYPE_SPK )
2200
						continue;
2201
 
2202
					CSpkFile *spk = (CSpkFile *)node->Data();
2203
					if ( !spk->IsAnotherMod() )
2204
						continue;
2205
 
50 cycrow 2206
					if ( spk->GetOtherName().Compare(package->name()) && spk->GetOtherAuthor().Compare(package->author()) )
1 cycrow 2207
					{
2208
						added = true;
2209
						m_lInstallList.insert(node->Data(), package);
2210
						break;
2211
					}
2212
				}
2213
 
2214
				if ( !added )
2215
					m_lInstallList.push_back(package);
2216
			}
2217
 
2218
			return INSTALLCHECK_OK;
2219
		}
2220
 
2221
		package->SetLoadError(error);
2222
		return error;
2223
	}
2224
 
2225
	return INSTALLCHECK_ALREADYQUEUED;
2226
}
2227
 
2228
void CPackages::RemovePreparedInstall(CBaseFile *package)
2229
{
2230
	if ( !package )
2231
	{
2232
		m_lInstallList.MemoryClear();
2233
	}
2234
	else
2235
	{
2236
		if ( m_lInstallList.FindData(package) )
2237
		{
2238
			m_lInstallList.remove(package, true);
2239
		}
2240
	}
2241
}
2242
 
2243
char CPackages::ConvertWareTypeBack(int w)
2244
{
2245
	switch ( w )
2246
	{
2247
		case WARES_BIO:
2248
			return 'b';
2249
		case WARES_ENERGY:
2250
			return 'e';
2251
		case WARES_FOOD:
2252
			return 'f';
2253
		case WARES_MINERAL:
2254
			return 'm';
2255
		case WARES_TECH:
2256
			return 't';
2257
		case WARES_NATURAL:
2258
			return 'n';
2259
	}
2260
	return 't';
2261
}
2262
int CPackages::ConvertWareType(char w)
2263
{
2264
	switch ( LOWER(w) )
2265
	{
2266
		case 'b':
2267
			return WARES_BIO;
2268
		case 'e':
2269
			return WARES_ENERGY;
2270
		case 'f':
2271
			return WARES_FOOD;
2272
		case 'm':
2273
			return WARES_MINERAL;
2274
		case 't':
2275
			return WARES_TECH;
2276
		case 'n':
2277
			return WARES_NATURAL;
2278
	}
2279
	return WARES_TECH;
2280
}
2281
 
2282
void CPackages::SetupShips()
2283
{
2284
	for ( CListNode<SGameShip> *wNode = m_lGameShips.Front(); wNode; wNode = wNode->next() )
2285
	{
2286
		if ( wNode->Data()->iType != WARETYPE_NONE )
2287
			wNode->Data()->iType = WARETYPE_DELETED;
2288
	}
2289
 
2290
	// find any new ships to add to the list
2291
	for ( CListNode<CBaseFile> *pNode = m_lPackages.Front(); pNode; pNode = pNode->next() )
2292
	{
2293
		if ( pNode->Data()->GetType() != TYPE_XSP )
2294
			continue;
2295
		CXspFile *p = (CXspFile *)pNode->Data();
2296
 
2297
		bool found = false;
2298
		for ( CListNode<SGameShip> *wNode = m_lGameShips.Front(); wNode; wNode = wNode->next() )
2299
		{
14 cycrow 2300
			if ( wNode->Data()->sShipID.Compare(p->GetShipID().c_str()) )
1 cycrow 2301
			{
2302
				if ( !p->IsEnabled() )
2303
					wNode->Data()->iType = WARETYPE_DISABLED;
2304
				else
2305
					wNode->Data()->iType = WARETYPE_ADDED;
2306
				found = true;
2307
				wNode->Data()->pPackage = p;
2308
				wNode->Data()->sShipClass = p->GetShipClass();
2309
				break;
2310
			}
2311
		}
2312
 
2313
		if ( found || !p->IsEnabled() )
2314
			continue;
2315
 
2316
		// first find any free
2317
		SGameShip *gw = NULL;
2318
		for ( CListNode<SGameShip> *wNode = m_lGameShips.Front(); wNode; wNode = wNode->next() )
2319
		{
2320
			if ( (!gw) && (wNode->Data()->iType == WARETYPE_NONE) )
2321
				gw = wNode->Data();
2322
			// find an old entry for the ware and add it to the same place
14 cycrow 2323
			if ( wNode->Data()->sShipID.Compare(p->GetShipID().c_str()) )
1 cycrow 2324
			{
2325
				gw = wNode->Data();
2326
				break;
2327
			}
2328
		}
2329
 
2330
		// none found, create one
2331
		if ( !gw )
2332
		{
2333
			gw = new SGameShip;
2334
			gw->sShipID = p->GetShipID();
2335
			gw->sShipClass = p->GetShipClass();
2336
			gw->pPackage = p;
2337
			m_lGameShips.push_back(gw);
2338
		}
2339
		gw->iType = WARETYPE_ADDED;
2340
	}
2341
}
2342
 
2343
void CPackages::SetupWares()
2344
{
2345
	for ( int i = 0; i < WAREBUFFERS; i++ )
2346
	{
2347
		for ( CListNode<SGameWare> *wNode = m_lGameWares[i].Front(); wNode; wNode = wNode->next() )
2348
		{
2349
			if ( wNode->Data()->iType != WARETYPE_NONE )
2350
				wNode->Data()->iType = WARETYPE_DELETED;
2351
		}
2352
	}
2353
 
2354
	// find any new wares to add to the list
2355
	for ( CListNode<CBaseFile> *pNode = m_lPackages.Front(); pNode; pNode = pNode->next() )
2356
	{
2357
		if ( pNode->Data()->GetType() != TYPE_SPK )
2358
			continue;
2359
		CSpkFile *p = (CSpkFile *)pNode->Data();
2360
 
2361
		for ( CListNode<SWares> *node = p->GetWaresList()->Front(); node; node = node->next() )
2362
		{
2363
			SWares *w = node->Data();
2364
			int wareType = CPackages::ConvertWareType(w->cType);
2365
 
2366
			// check if its on the list
2367
			bool found = false;
2368
			for ( CListNode<SGameWare> *wNode = m_lGameWares[wareType].Front(); wNode; wNode = wNode->next() )
2369
			{
2370
				if ( wNode->Data()->sWareName == w->sID )
2371
				{
2372
					if ( !p->IsEnabled() )
2373
						wNode->Data()->iType = WARETYPE_DISABLED;
2374
					else
2375
						wNode->Data()->iType = WARETYPE_ADDED;
2376
					wNode->Data()->pWare = w;
2377
					found = true;
2378
					break;
2379
				}
2380
			}
2381
 
2382
			if ( found || !p->IsEnabled() )
2383
				continue;
2384
 
2385
			// first find any free
2386
			SGameWare *gw = NULL;
2387
			for ( CListNode<SGameWare> *wNode = m_lGameWares[wareType].Front(); wNode; wNode = wNode->next() )
2388
			{
2389
				if ( (!gw) && (wNode->Data()->iType == WARETYPE_NONE) )
2390
					gw = wNode->Data();
2391
				// find an old entry for the ware and add it to the same place
2392
				if ( wNode->Data()->sWareName == w->sID )
2393
				{
2394
					gw = wNode->Data();
2395
					break;
2396
				}
2397
			}
2398
 
2399
			// none found, create one
2400
			if ( !gw )
2401
			{
2402
				gw = new SGameWare;
2403
				gw->sWareName = w->sID;
2404
				gw->iPos = m_lGameWares[wareType].size();
2405
				gw->pWare = w;
2406
				gw->cType = w->cType;
2407
				m_lGameWares[wareType].push_back(gw);
2408
			}
2409
			gw->iType = WARETYPE_ADDED;
2410
		}
2411
	}
2412
}
2413
 
2414
/**
2415
 * Closing the current directory
2416
 *
2417
 * When existing, program needs to close the directory
2418
 */
2419
bool CPackages::CloseDir ( CyStringList *errors, CProgressInfo *progress, bool removedir )
2420
{
2421
	if ( m_sCurrentDir.Empty() )
2422
		return true;
2423
	if ( !m_bLoaded )
2424
		return true;
2425
 
2426
	m_sActiveDir = m_sCurrentDir;
2427
 
2428
	if ( m_bRenameText )
2429
		CreateLanguageTextFiles(errors);
2430
 
2431
	this->BackupSaves();
2432
 
2433
	if ( CFileIO(m_sCurrentDir + "/mods/PluginManager.dat").Exists() )
2434
		CFileIO(m_sCurrentDir + "/mods/PluginManager.dat").Remove();
2435
	if ( CFileIO(m_sCurrentDir + "/mods/PluginManager.cat").Exists() )
2436
		CFileIO(m_sCurrentDir + "/mods/PluginManager.cat").Remove();
2437
 
2438
	if ( !m_bVanilla )
2439
	{
2440
		// base mode for Reunion
2441
		if ( m_iGame == GAME_X3 && m_pEnabledMod )
2442
		{
2443
			C_File *fDat = m_pEnabledMod->GetFirstFile(FILETYPE_MOD);
2444
			while ( fDat && !fDat->IsFakePatch() && !fDat->CheckFileExt("dat") )
2445
				fDat = m_pEnabledMod->GetNextFile(fDat);
2446
 
2447
			if ( fDat )
2448
			{
2449
				C_File *fCat = m_pEnabledMod->GetFirstFile(FILETYPE_MOD);
2450
				while ( fCat && !fCat->IsFakePatch() && !fCat->CheckFileExt("cat") && !fCat->GetBaseName().Compare(fDat->GetBaseName()) )
2451
					fCat = m_pEnabledMod->GetNextFile(fCat);
2452
 
2453
				if ( fCat )
2454
				{
2455
					CFileIO(fDat->GetFilePointer()).Copy(m_sCurrentDir + "/mods/PluginManager.dat");
2456
					CFileIO(fCat->GetFilePointer()).Copy(m_sCurrentDir + "/mods/PluginManager.cat");
2457
				}
2458
			}
2459
		}
2460
		else if ( m_iGame == GAME_X3 && !m_sSetMod.Empty() && CFileIO(m_sCurrentDir + "/mods/" + m_sSetMod + ".cat").Exists() && CFileIO(m_sCurrentDir + "/mods/" + m_sSetMod + ".dat").Exists() )
2461
		{
2462
			CFileIO(m_sCurrentDir + "/mods/" + m_sSetMod + ".dat").Copy(m_sCurrentDir + "/mods/PluginManager.dat");
2463
			CFileIO(m_sCurrentDir + "/mods/" + m_sSetMod + ".cat").Copy(m_sCurrentDir + "/mods/PluginManager.cat");
2464
		}
2465
 
2466
		if ( !CDirIO(m_sCurrentDir).Exists("mods") )
2467
			CDirIO(m_sCurrentDir).Create("mods");
2468
 
2469
		SetupWares();
2470
		SetupShips();
2471
		CreateEMPFile();
2472
		CreateWareFiles();
2473
		CreateDummies();
2474
		CreateComponants();
2475
		CreateTShips();
2476
		CreateCutData();
2477
		CreateBodies();
2478
		CreateAnimations();
2479
		CreateCustomStarts();
2480
		CreateGlobals();
2481
		CreatePluginManagerText();
2482
		RestoreFakePatch();
2483
	}
2484
 
2485
	RemoveFailedFiles();
2486
 
2487
	WriteData();
2488
	if ( removedir && m_bRemoveDir )
2489
	{
2490
		m_bRemoveDir = false;
2491
		CyStringList removeDirs;
2492
		removeDirs.PushBack(".");
2493
		RemoveUnusedDirectories(removeDirs, errors);
2494
	}
2495
 
2496
	m_bLoaded = false;
2497
	return true;
2498
}
2499
 
2500
CyString CPackages::GetModKey()
2501
{
2502
	return m_gameExe.GetModKey(m_iGame - 1);
2503
}
2504
CyString CPackages::GetSelectedModName()
2505
{
2506
	if ( !m_pEnabledMod )
2507
	{
2508
		if ( m_sSetMod.Empty() && m_iGame == GAME_X3 )
2509
			return "PluginManager";
2510
		return m_sSetMod;
2511
	}
2512
 
2513
	if ( !m_pEnabledMod->IsEnabled() )
2514
		return m_sSetMod;
2515
 
2516
	C_File *f = m_pEnabledMod->GetFirstFile(FILETYPE_MOD);
2517
	if ( !f )
2518
		return m_sSetMod;
2519
 
2520
	CyString name = f->GetFilename();
2521
	name = name.Left(-4);
2522
	return name;
2523
 
2524
}
2525
 
2526
bool CPackages::RestoreFakePatch()
2527
{
2528
	m_iFakePatch = -1;
2529
 
2530
	CFileIO catFile(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat");
2531
	CFileIO datFile(m_sCurrentDir + "/PluginManager/PlugMan_Fake.dat");
2532
 
2533
	// if only 1 exists, remove it
2534
	if ( catFile.Exists() && !datFile.Exists() )
2535
	{
2536
		if ( !catFile.Remove() )
2537
			return false;
2538
	}
2539
	else if ( !catFile.Exists() && datFile.Exists() )
2540
	{
2541
		if ( !datFile.Remove() )
2542
			return false;
2543
	}
2544
 
2545
	// if both exists, lets rename them
2546
	if ( catFile.Exists() && datFile.Exists() )
2547
	{
2548
		// we need to add the plugin manager file in
2549
		CyString file = m_sTempDir;
2550
		if ( !file.Empty() )
2551
			file += "/";
2552
		file += "pluginmanagerfake.txt";
2553
		file = file.FindReplace("\\", "/");
2554
		CFileIO fakeFile(file);
2555
		std::vector<CyString> lines;
2556
		lines.push_back("//pluginmanager fake patch");
2557
		fakeFile.WriteFile(&lines);
2558
 
2559
		CCatFile fakePatch;
2560
		if ( fakePatch.Open(m_sCurrentDir + "/PluginManager/PlugMan_Fake.cat", this->GetAddonDir(), CATREAD_DAT, false) == CATERR_NONE )
2561
		{
2562
			if ( fakePatch.AppendFile(file, "PlugMan\\TFake.pck") )
2563
			{
2564
				// find next available fake patch
2565
				m_iFakePatch = this->FindNextFakePatch();
2566
 
2567
				CyString filename = CyString::Number(m_iFakePatch).PadNumber(2);
2568
				if ( catFile.Rename(m_sCurrentDir + "/" + filename + ".cat") )
2569
				{
2570
					if ( datFile.Rename(m_sCurrentDir + "/" + filename + ".dat") )
2571
					{
2572
						fakeFile.Remove();
2573
						return true;
2574
					}
2575
 
2576
					// TODO: restore cat file
2577
				}
2578
			}
2579
		}
2580
 
2581
		fakeFile.Remove();
2582
		return false;
2583
	}
2584
 
2585
	return true;
2586
}
2587
 
2588
 
2589
/**
2590
 * Save package detail to date file
2591
 *
2592
 * Writes the current package list data into pluginmanager.dat file
2593
 */
2594
void CPackages::WriteData()
2595
{
2596
	if ( m_sCurrentDir.Empty() )
2597
		return;
2598
 
2599
	CyStringList lines;
2600
	CyString version;
2601
	version.FromFloat(GetLibraryVersion(), 2);
2602
	version.Prepend("SpkInstaller: ");
2603
	lines.PushBack(version);
2604
 
2605
	lines.PushBack(CyString("UpdateTime: ") + (long)m_iLastUpdated);
2606
	if ( m_iFakePatch != -1 )
2607
		lines.PushBack(CyString("FakePatch: ") + (long)m_iFakePatch);
2608
	if ( m_iSaveGame != -1 )
2609
		lines.PushBack(CyString("SaveGame: ") + (long)m_iSaveGame);
2610
	lines.PushBack(CyString("SaveGameManager: ") + (long)m_iSaveGameManager);
2611
	if ( !m_bVanilla )
2612
		lines.PushBack("Modified");
2613
	if ( m_bUsedWare )
2614
		lines.PushBack("UsedWare");
2615
	if ( m_bSurpressProtectedWarning )
2616
		lines.PushBack("SurpressProtectedWarning");
2617
	if ( !m_sSetMod.Empty() )
2618
		lines.PushBack(CyString("SetMod: ") + m_sSetMod);
2619
	lines.PushBack(CyString("ShipBuffer: ") + (long)m_iShipBuffer);
2620
	CyString wareBuffer = "WareBuffers:";
2621
	for ( int i = 0; i < WAREBUFFERS; i++ )
2622
		wareBuffer += CyString(" ") + (long)m_iWareBuffer[i];
2623
	lines.PushBack(wareBuffer);
2624
	for ( int i = 0; i < WAREBUFFERS; i++ )
2625
	{
2626
		if ( !m_lGameWares[i].size() )
2627
			continue;
2628
		lines.PushBack(CyString("Wares: ") + (long)i + " " + (long)m_lGameWares[i].size());
2629
 
2630
		for ( CListNode<SGameWare> *node = m_lGameWares[i].Front(); node; node = node->next() )
2631
		{
2632
			SGameWare *gm = node->Data();
2633
			lines.PushBack(CyString((long)gm->iPos) + " " + (long)gm->iType + " " + CyString((char)gm->cType) + " " + gm->sWareName);
2634
		}
2635
	}
2636
 
2637
	for ( SStringList *str = m_lNonRemovedFiles.Head(); str; str = str->next )
2638
		lines.PushBack(CyString("NonRemovedFile: ") + str->str);
2639
 
2640
	if ( m_lGameShips.size() )
2641
	{
2642
		lines.PushBack(CyString("Ships: ") + (long)m_lGameShips.size());
2643
 
2644
		for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
2645
		{
2646
			SGameShip *gm = node->Data();
2647
			lines.PushBack(CyString((long)gm->iType) + " $#C:" + gm->sShipClass + " " + gm->sShipID);
2648
		}
2649
	}
2650
 
2651
	// write created Files
2652
	if ( !m_lCreatedFiles.Empty() )
2653
	{
2654
		for ( SStringList *node = m_lCreatedFiles.Head(); node; node = node->next )
2655
			lines.PushBack(CyString("CreatedFile: ") + node->str.Remove(m_sCurrentDir));
2656
	}
2657
 
2658
	// write uninstall files
2659
	if ( !m_lUninstallFiles.empty() )
2660
	{
2661
		for ( CListNode<C_File> *node = m_lUninstallFiles.Front(); node; node = node->next() )
2662
		{
2663
			C_File *uf = node->Data();
2664
			CyString uString = "Uninstall: ";
2665
			uString += CyString::Number((long)uf->GetCreationTime()) + " ";
2666
			uString += uf->GetFilename();
2667
			lines.PushBack(uString);
2668
		}
2669
	}
2670
 
2671
	// write the original file data
2672
	if ( !m_lOriginalFiles.empty() )
2673
	{
2674
		for ( CListNode<C_File> *node = m_lOriginalFiles.Front(); node; node = node->next() )
2675
		{
2676
			C_File *uf = node->Data();
2677
			CyString uString = "Original: ";
2678
			uString += CyString::Number((long)uf->GetFileType()) + " ";
2679
			uString += uf->GetFilename();
2680
			if ( uf->GetFileType() == FILETYPE_EXTRA && !uf->GetDir().Empty())
2681
			{
2682
				uString += ":";
2683
				uString += uf->GetDir();
2684
			}
2685
			lines.PushBack(uString);
2686
		}
2687
	}
2688
 
2689
	// write the global changes
2690
	if ( !m_lGlobals.Empty() )
2691
	{
2692
		for ( SStringList *str = m_lGlobals.Head(); str; str = str->next )
2693
			lines.PushBack(CyString("GlobalSetting: ") + str->str + ":" + str->data.Remove(";"));
2694
	}
2695
 
2696
	// write the fake patch ordering
2697
	if ( !m_lFakePatchOrder.Empty() )
2698
	{
2699
		for ( SStringList *str = m_lFakePatchOrder.Head(); str; str = str->next )
2700
			lines.PushBack(CyString("FakePatchOrder: ") + str->str + ":" + str->data);
2701
	}
2702
 
2703
	// write the global file list
2704
	lines.PushBack("GlobalFiles:");
2705
	int num = 0;
2706
	for ( CListNode<C_File> *fn = m_lFiles.Front(); fn; fn = fn->next() )
2707
	{
2708
		C_File *f = fn->Data();
2709
		f->SetPos(num++);
2710
 
2711
		CyString line = CyString::Number(f->GetFileType()) + ":" + CyString::Number((long)f->GetCreationTime()) + ":" + f->GetDir() + ":";
2712
		if ( f->IsShared() && !f->IsFakePatch() )
2713
			line += "1:";
2714
		else
2715
			line += "0:";
2716
 
2717
		CyString filename = f->GetFilePointer();
2718
		filename.Remove(m_sCurrentDir);
2719
 
2720
		if ( f->IsDisabled() )
2721
			line += "D#";
2722
 
2723
		line += "G#";
2724
		line += (long)f->GetGame();
2725
		line += "#";
2726
 
2727
		line += filename;
2728
 
2729
		if ( !f->GetOriginalName().Empty() )
2730
		{
2731
			line += "O#";
2732
			line += f->GetOriginalName();
2733
		}
2734
 
2735
		lines.PushBack(line);
2736
	}
2737
 
2738
	// write the package list
2739
	for ( CListNode<CBaseFile> *pn = m_lPackages.Front(); pn; pn = pn->next() )
2740
	{
2741
		CBaseFile *package = pn->Data();
2742
 
2743
		if ( package->GetType() == TYPE_SPK )
2744
			lines.PushBack("<script>");
2745
		else if ( package->GetType() == TYPE_XSP )
2746
			lines.PushBack("<ship>");
2747
		else if ( package->GetType() == TYPE_ARCHIVE )
2748
			lines.PushBack("<archive>");
2749
		else if ( package->GetType() == TYPE_BASE )
2750
			lines.PushBack("<base>");
2751
		else
2752
			continue;
2753
 
50 cycrow 2754
		if ( !package->filename().empty() )
2755
			lines.PushBack(CyString("Installspk: ") + package->filename());
1 cycrow 2756
 
2757
		CyString valuesline = package->CreateValuesLine();
2758
		if ( valuesline.Right(1) == '\n' )
2759
			valuesline.Truncate((int)valuesline.Length() - 1);
2760
		lines.PushBack(valuesline);
2761
 
2762
		if ( !package->IsEnabled() )
2763
			lines.PushBack("Disabled");
2764
		if ( !package->IsModifiedEnabled() )
2765
			lines.PushBack("ModifiedDisabled");
2766
 
2767
		if ( package->GetIcon() )
2768
			lines.PushBack(CyString("Icon: ") + package->GetIconExt() + " " + package->GetIcon()->GetFilePointer() );
2769
 
2770
		CyString fileline("Files:");
2771
		for ( CListNode<C_File> *fn = package->GetFileList()->Front(); fn; fn = fn->next() )
2772
		{
2773
			C_File *f = fn->Data();
2774
			fileline += " ";
2775
			fileline += CyString::Number(f->GetPos());
2776
		}
2777
		lines.PushBack(fileline);
2778
	}
2779
 
2780
	lines.PushBack("</scripts>");
2781
 
2782
	CFileIO datFile(m_sCurrentDir + "/PluginManager/PluginManager.dat");
2783
 
2784
	CDirIO Dir(m_sCurrentDir);
2785
	if ( !Dir.Exists("PluginManager") )
2786
		Dir.Create("PluginManager");
2787
 
2788
 
2789
	if ( !datFile.WriteFile(&lines) )
2790
	{
2791
	}
2792
}
2793
 
2794
 
2795
/**
2796
 * Get All Files
2797
 *
2798
 * Gets a list of all files, includes any child package files
2799
 */
2800
int CPackages::GetAllPackageFiles(CBaseFile *package, CLinkList<C_File> *fileList, bool includeChild)
2801
{
2802
	for ( CListNode<C_File> *node = package->GetFileList()->Front(); node; node = node->next() )
2803
	{
2804
		C_File *f = node->Data();
2805
		if ( !fileList->FindData(f) )
2806
			fileList->push_back(f);
2807
	}
2808
 
2809
	if ( includeChild )
2810
	{
2811
		CLinkList<CBaseFile> childList;
2812
		if ( this->GetChildPackages(package, &childList) )
2813
		{
2814
			for ( CBaseFile *child = childList.First(); child; child = childList.Next() )
2815
				this->GetAllPackageFiles(child, fileList, includeChild);
2816
		}
2817
	}
2818
 
2819
	// disablign for vanilla, make sure we add all files
2820
	if ( m_bDisableVanilla )
2821
	{
2822
		for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
2823
		{
2824
			C_File *f = node->Data();
2825
			if ( !fileList->FindData(f) )
2826
				fileList->push_back(f);
2827
		}
2828
	}
2829
 
2830
	return fileList->size();
2831
}
2832
 
2833
int CPackages::GetAllPackageFiles(CLinkList<CBaseFile> *list, CLinkList<C_File> *fileList, bool includeChild)
2834
{
2835
	for ( CListNode<CBaseFile> *node = list->Front(); node; node = node->next() )
2836
		this->GetAllPackageFiles(node->Data(), fileList, includeChild);
2837
	return fileList->size();
2838
}
2839
 
2840
/**
2841
 * Add Log
2842
 *
2843
 * Adds a log entry to displayed at end
2844
 */
2845
void CPackages::AddLogEntry(int type, CyString args, CyStringList *errors)
2846
{
2847
	if ( !errors )
2848
		return;
2849
 
2850
	errors->PushBack(args, ERRORLOG(type));
2851
}
2852
 
2853
/**
2854
 * Enable a package
2855
 *
2856
 * Enables all files in the package, unless they are already enabled, ie, used by other packages
2857
 * Backs up any original files before attempting to enable the file
2858
 *
2859
 * Fake patches are renamed to the next available slot
2860
 *
2861
 * Clears up the "PluginManager/Disabled" directory
2862
 *
2863
 * param: package		- The package class to be removed
2864
 * param: errors		- The string list for all the status for debugging, ie has an entry for whats happened to every file
2865
 * param: progress		- The progress class, updates a progress screen of the derived class
2866
 *
2867
 * return: boolen - Returns true if the package enabling was successful
2868
 */
2869
bool CPackages::EnablePackage ( CBaseFile *package, CyStringList *errors, CProgressInfo *progress )
2870
{
2871
	ClearError();
2872
 
2873
	// if already enabled, just skip
2874
	if ( package->IsEnabled() )
2875
		return true;
2876
 
2877
	// check if parent is enabled
2878
	if ( package->GetParent() && !package->GetParent()->IsEnabled() )
2879
	{
2880
		m_iError = PKERR_NOPARENT;
2881
		return false;
2882
	}
2883
 
2884
	// check for modified
2885
	if ( m_bVanilla && !package->IsSigned() )
2886
	{
2887
		m_iError = PKERR_MODIFIED;
2888
		return false;
2889
	}
2890
 
2891
	if ( this->PrepareEnablePackage(package) )
2892
		return this->EnablePreparedPackages(errors, progress);
2893
 
2894
	return false;
2895
}
2896
bool CPackages::EnablePreparedPackages ( CyStringList *errors, CProgressInfo *progress, CLinkList<CBaseFile> *enabledPackages )
2897
{
2898
	ClearError();
2899
 
2900
	// get all files, including children
2901
	CLinkList<C_File> fileList;
2902
	int maxFiles = this->GetAllPackageFiles(&m_lEnableList, &fileList, false);
2903
 
2904
	int fileCount = 0;
2905
	// we use this list to match cat and dat files
2906
	CyStringList fakePatches;
2907
	for ( CListNode<C_File> *node = fileList.Front(); node; node = node->next() )
2908
	{
2909
		C_File *f = node->Data();
2910
		if ( f->GetGame() && m_iGame ) {
2911
			if ( f->GetGame() != m_iGame )
2912
				continue;
2913
		}
2914
		CBaseFile *package = NULL;
2915
		for ( CListNode<CBaseFile> *node = m_lEnableList.Front(); node; node = node->next() )
2916
		{
2917
			CBaseFile *p = node->Data();
2918
			if ( p->IsFileAdded(f) )
2919
			{
2920
				package = p;
2921
				break;
2922
			}
2923
		}
2924
 
2925
		if ( progress )
2926
		{
2927
			progress->UpdateProgress(fileCount++, maxFiles);
2928
			progress->UpdateFile(f);
2929
		}
2930
 
2931
		// only move fiels that are disabled
2932
		if ( !f->IsDisabled() )
2933
			continue;
2934
 
2935
		// make the directory if it dont exist
2936
		CDirIO Dir(m_sCurrentDir);
2937
 
2938
		// fake patches are in the root, no need for directory
2939
		if ( !f->IsFakePatch() )
2940
		{
2941
			if ( !Dir.Exists(f->GetDirectory(package)) )
2942
			{
2943
				if ( !Dir.Create(f->GetDirectory(package)) )
2944
				{
2945
					this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY_FAIL, f->GetDirectory(package), errors);
2946
					continue;
2947
				}
2948
				this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY, f->GetDirectory(package), errors);
2949
			}
2950
		}
2951
 
2952
		// check if theres an original file to backup
2953
		C_File *of = GetOriginalFile(f);
2954
		if ( of )
2955
		{
2956
			// check if the orignal file is already backed up
2957
			if ( !CFileIO(m_sCurrentDir + "/PluginManager/Original/" + f->GetNameDirectory(package)).Exists() )
2958
			{
2959
				// lets back up the file now
2960
				BackupOriginalFile(of, errors);
2961
			}
2962
		}
2963
 
2964
		CyString newFilename = f->GetNameDirectory(package);
2965
 
2966
		// fake patches need to be renamed
2967
		if ( f->IsFakePatch() )
2968
		{
2969
			// first check if the matching file has been done already
2970
			SStringList *foundStr = fakePatches.FindString(f->GetBaseName());
2971
 
2972
			// already done one, simply change this to match
2973
			if ( foundStr )
2974
				newFilename = foundStr->data + "." + f->GetFileExt();
2975
 
2976
			// we need to find the next available number instead
2977
			else
2978
			{
2979
				CyString newPos = CyString::Number(FindNextFakePatch()).PadNumber(2);
2980
				newFilename = newPos + "." + f->GetFileExt();
2981
				// and wee need to push this onto the string list so we can adjust the matchign pair
2982
				fakePatches.PushBack(f->GetBaseName(), newPos);
2983
			}
2984
		}
2985
 
2986
		// lets actually move the file back now
2987
		// !!error checking!!
2988
		CFileIO currentFile(f->GetFilePointer());
2989
		CFileIO newFile(m_sCurrentDir + "/" + newFilename);
2990
		if ( !currentFile.Exists() )
2991
		{
2992
			// missing file ??
2993
			if ( !newFile.Exists() )
2994
			{
2995
				this->AddLogEntry(SPKINSTALL_MISSINGFILE, newFilename, errors);
2996
				continue;
2997
			}
2998
		}
2999
		// remove existing file
3000
		// file exists, so lets try to move it
3001
		else
3002
		{
3003
			if ( newFile.Exists() )
3004
				newFile.Remove();
3005
 
3006
			if ( !currentFile.Rename(newFile.GetFullFilename()) )
3007
			{
3008
				this->AddLogEntry(SPKINSTALL_ENABLEFILE_FAIL, newFilename, errors);
3009
				continue;
3010
			}
3011
		}
3012
 
3013
		this->AddLogEntry(SPKINSTALL_ENABLEFILE, newFilename, errors);
3014
 
3015
		// adjust the internal name to match the new filename
3016
		f->SetFilename(m_sCurrentDir + "/" + newFilename);
3017
		// no longer disabled, we need to remove the flag
3018
		f->SetDisabled(false);
3019
	}
3020
 
3021
	// recursive, auto enable all children
3022
	CBaseFile *oldMod = m_pEnabledMod;
3023
	CBaseFile *pMod = NULL;
3024
 
3025
	for ( CListNode<CBaseFile> *node = m_lEnableList.Front(); node; node = node->next() )
3026
	{
3027
		CBaseFile *p = node->Data();
3028
		p->SetEnabled(true);
3029
		if ( p->IsMod() && !pMod )
3030
			pMod = p;
3031
 
3032
		if ( enabledPackages )
3033
			enabledPackages->push_back(p);
3034
	}
3035
 
3036
	if ( pMod )
3037
		m_pEnabledMod = pMod;
3038
 
3039
	// disabled the mod
3040
	if ( oldMod && oldMod != m_pEnabledMod && !m_bForceModInstall )
3041
		this->DisablePackage(oldMod, errors, progress);
3042
 
3043
	// lets remove all the directories we might have left empty
3044
	CyStringList removeDirs;
3045
	removeDirs.PushBack(CyString("PluginManager/Disabled"));
3046
	RemoveUnusedDirectories(removeDirs, errors);
3047
 
3048
	m_lEnableList.clear();
3049
 
3050
	this->WriteData();
3051
 
3052
	return true;
3053
}
3054
 
3055
 
3056
bool CPackages::DisablePreparedPackages ( CyStringList *errors, CProgressInfo *progress, CLinkList<CBaseFile> *disabledPackages )
3057
{
3058
	if ( progress )
3059
		progress->UpdateStatus(PROGRESS_DISABLEFILE);
3060
 
3061
	UpdateUsedFiles(&m_lDisableList, false);
3062
 
3063
	// checks if there are any original files to restore and if any fake patches were disabled to reshuffle
3064
	bool original = false, shuffle = false;
3065
 
3066
	// holds our list of directories that we might need to remove, only empty ones from this list will actually be removed
3067
	CyStringList removeDirs;
3068
 
3069
	// get all files, including children
3070
	CLinkList<C_File> fileList;
3071
	int maxFiles = this->GetAllPackageFiles(&m_lDisableList, &fileList, true);
3072
 
3073
	// interate through all the files in the package
3074
	int fileCount = 0;
3075
	for ( CListNode<C_File> *node = fileList.Front(); node; node = node->next() )
3076
	{
3077
		C_File *f = node->Data();
3078
		CBaseFile *checkPackage = NULL;
3079
		for ( CListNode<CBaseFile> *node = m_lDisableList.Front(); node; node = node->next() )
3080
		{
3081
			CBaseFile *p = node->Data();
3082
			if ( p->IsFileAdded(f) )
3083
			{
3084
				checkPackage = p;
3085
				break;
3086
			}
3087
		}
3088
 
3089
		// update the progress count for the current file
3090
		if ( progress )
3091
		{
3092
			progress->UpdateProgress(++fileCount, maxFiles);
3093
			progress->UpdateFile(f);
3094
		}
3095
 
3096
		// only delete files that are not used by any other enabled packages, counter from UpdateUsedFiles()
3097
		if ( f->GetUsed() || (f->IsShared() && !m_bDisableVanilla) )
3098
			continue;
3099
 
3100
		// file is already disabled, no need to disable again
3101
		if ( f->IsDisabled() )
3102
			continue;
3103
 
3104
		// readmes, uninstall and extra files dont need to be disabled
3105
		// Extra files not in the "Extras" directory could be anywhere, so these should be disabled incase they are game changing files, ie in "types"
3106
		if ( f->GetFileType() == FILETYPE_README || f->GetFileType() == FILETYPE_UNINSTALL || (f->GetFileType() == FILETYPE_EXTRA && f->GetDir().Left(5).lower() == "Extra") )
3107
			continue;
3108
 
3109
		// check if there is a matching uninstall file, ie there the script file is also an uninstall script file for a previously uninstalled package that has yet to be removed
3110
		if ( f->GetFileType() == FILETYPE_SCRIPT )
3111
		{
3112
			bool found = false;
3113
			for ( CListNode<C_File> *uNode = m_lUninstallFiles.Front(); uNode; uNode = uNode->next() )
3114
			{
3115
				C_File *uFile = uNode->Data();
3116
				if ( uFile->GetFilename().Compare(f->GetFilename()) )
3117
				{
3118
					found = true;
3119
					break;
3120
				}
3121
			}
3122
 
3123
			if ( found )
3124
				continue;
3125
		}
3126
 
3127
		if ( f->GetFileType() == FILETYPE_MOD && !f->IsFakePatch() && f->CheckFileExt("cat") )
3128
		{
3129
			if ( f->GetBaseName().Compare(m_sSetMod) )
3130
				m_sSetMod = NullString;
3131
		}
3132
 
3133
		// file is not being used by any enabled package
3134
		// set disabled and move to disabled directory
3135
		CDirIO Dir(m_sCurrentDir);
3136
		CyString newFilename = "PluginManager/Disabled/";
3137
 
3138
		// fake patches have thier own special directory
3139
		if ( f->IsFakePatch() )
3140
			newFilename += "FakePatches";
3141
		// otherwise we put them in thier usual directory structure inside the disabled dir
3142
		else
3143
			newFilename += f->GetDirectory(NULL);
3144
 
3145
		// make sure the directory exists so we can move the file
3146
		if ( !Dir.Exists(newFilename) )
3147
		{
3148
			// we couldn't create the directory for some reason, this is not good
3149
			if ( !Dir.Create(newFilename) )
3150
			{
3151
				this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY_FAIL, newFilename, errors);
3152
				continue;
3153
			}
3154
			this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY, newFilename, errors);
3155
		}
3156
 
3157
		// fake patches need a special directory and filename so they dont overright each other as thier filenames are always changes
3158
		// if a package with a fake patch is installed while another one is disabled, it will fill the gap left by the disabled one
3159
		// they will then end up with the same filename, if it gets disabled they would overright each other, so we change the filename to prevent that from happening
3160
		if ( f->IsFakePatch() )
3161
		{
3162
			// find package the fake patch belongs to
3163
			if ( checkPackage )
3164
			{
50 cycrow 3165
				newFilename = m_sCurrentDir + "/PluginManager/Disabled/FakePatches/FakePatch_" + checkPackage->GetNameValidFile() + "_" + checkPackage->author() + "_" + f->GetName();
1 cycrow 3166
				shuffle = true;
3167
			}
3168
		}
3169
		else if ( f->IsAutoTextFile() )
3170
		{
3171
			if ( checkPackage )
3172
			{
50 cycrow 3173
				newFilename = m_sCurrentDir + "/PluginManager/Disabled/TextFiles/Text_" + checkPackage->GetNameValidFile() + "_" + checkPackage->author() + "_" + f->GetName();
1 cycrow 3174
				shuffle = true;
3175
			}
3176
		}
3177
		// otherwise we can just use the standard filename
3178
		else
3179
			newFilename = m_sCurrentDir + "/PluginManager/Disabled/" + f->GetNameDirectory(checkPackage);
3180
 
3181
		// now to move the file by renameing it to its new location
3182
		// !!error checking!!
3183
		// check the file, if it doesn't exist, and exists as disabled, we should just adjust the setting instead of an error
3184
		CFileIO currentFile(f->GetFilePointer());
3185
		if ( !currentFile.Exists() )
3186
		{
3187
			if ( !CFileIO(newFilename).Exists() )
3188
			{
3189
				this->AddLogEntry(SPKINSTALL_MISSINGFILE, f->GetNameDirectory(checkPackage), errors);
3190
				continue;
3191
			}
3192
		}
3193
		// otherwise the file must exists, so lets move it
3194
		else if ( !currentFile.Rename(newFilename) )
3195
		{
3196
			this->AddLogEntry(SPKINSTALL_DISABLEFILE_FAIL, f->GetNameDirectory(checkPackage), errors);
3197
			continue;
3198
		}
3199
 
3200
		// must have been fine
3201
		this->AddLogEntry(SPKINSTALL_DISABLEFILE, f->GetNameDirectory(checkPackage), errors);
3202
 
3203
		// check if its an original file and restore
3204
		if ( IsOriginalFile(f) )
3205
		{
3206
			CFileIO of(m_sCurrentDir + "/PluginManager/Original/" + f->GetNameDirectory(checkPackage));
3207
			if ( of.Exists() )
3208
			{
3209
				original = true;
3210
				if ( of.Rename(m_sCurrentDir + "/" + f->GetNameDirectory(checkPackage)) )
3211
					this->AddLogEntry(SPKINSTALL_ORIGINAL_RESTORE, f->GetNameDirectory(checkPackage), errors);
3212
				else
3213
					this->AddLogEntry(SPKINSTALL_ORIGINAL_RESTORE_FAIL, f->GetNameDirectory(checkPackage), errors);
3214
			}
3215
		}
3216
 
3217
		// extra file thats not in the extras directory
3218
		if ( f->GetFileType() == FILETYPE_EXTRA || f->GetFileType() == FILETYPE_MAP || f->GetFileType() == FILETYPE_SOUND )
3219
			removeDirs.PushBack(f->GetDirectory(checkPackage), NullString, true);
3220
 
3221
		// change the filename
3222
		f->SetFilename(newFilename);
3223
 
3224
		// finally mark the file as disabled so we know not to try to move it again
3225
		f->SetDisabled(true);
3226
	}
3227
 
3228
	// a fake patch has been disabled, we need to reshuffle the rest to fill in any gaps
3229
	if ( shuffle )
3230
	{
3231
		if ( progress )
3232
			progress->UpdateStatus(PROGRESS_SHUFFLEFAKE);
3233
		ShuffleFakePatches(errors);
3234
		ShuffleTextFiles(errors);
3235
	}
3236
 
3237
	// original files were restored, check to remove the original file directory if its now empty
3238
	if ( original )
3239
		removeDirs.PushBack(CyString("PluginManager/Original"));
3240
 
3241
	// remove any empty directories that we might have left
3242
	if ( !removeDirs.Empty() )
3243
		RemoveUnusedDirectories(removeDirs, errors);
3244
 
3245
	// finally mark the whole package as disabled
3246
	// recursive, we need to disable all children
3247
	for ( CBaseFile *child = m_lDisableList.First(); child; child = m_lDisableList.Next() )
3248
	{
3249
		if ( m_pEnabledMod == child )
3250
			m_pEnabledMod = NULL;
3251
		child->SetEnabled(false);
3252
 
3253
		if ( disabledPackages )
3254
			disabledPackages->push_back(child);
3255
	}
3256
 
3257
	// disabling has completed successfully, we hope
3258
	m_bDisableVanilla = false;
3259
	m_lDisableList.clear();
3260
 
3261
	this->WriteData();
3262
 
3263
	return true;
3264
}
3265
 
3266
/**
3267
 * Disables the selected package
3268
 *
3269
 * Disables all enabled files that are not being used by any other enabled package
3270
 * Any files that are being used by other enabled packages are skipped and left enabled
3271
 *
3272
 * Original Files are restored when file is disabled
3273
 *
3274
 * Fake patches are shuffled to fill in any gaps caused by disabling fake patches
3275
 *
3276
 * All files go into the Plugin/Disabled directory into thier respective directories.
3277
 *
3278
 * Any directories left empty when disabling files are then removed
3279
 *
3280
 * param: package	- Package file to be disabled
3281
 * param: errors	- A string list used to add the status as it progresses, used in debugging output
3282
 * param: progress	- The progress class, updates the progress of the current disabling.  Needs a divered class to report the progress somewhere
3283
 *
3284
 * return: boolean, true if there was no errors, otherwise false
3285
 */
3286
bool CPackages::DisablePackage ( CBaseFile *package, CyStringList *errors, CProgressInfo *progress )
3287
{
3288
	// if already disabled, just skip
3289
	if ( !package->IsEnabled() )
3290
		return true;
3291
 
3292
	m_lDisableList.clear();
3293
	if ( this->PrepareDisablePackage(package) )
3294
		return this->DisablePreparedPackages(errors, progress);
3295
 
3296
	return false;
3297
}
3298
 
3299
 
3300
/**
3301
 * Find a Package
3302
 *
3303
 * Finds a matching package so we can find if one is already installed
3304
 *
3305
 * Uses seperate functions for each package type, ie for SPK and XSP packages
3306
 */
3307
CBaseFile *CPackages::FindPackage(CBaseFile *package)
3308
{
3309
	// no point checking if we've been sent a null pointer
3310
	if ( !package )
3311
		return 0;
3312
 
3313
	// we are checking against a SPK package, so we match the name and author
3314
	if ( package->GetType() == TYPE_SPK )
50 cycrow 3315
		return FindSpkPackage(package->name(), package->author());
1 cycrow 3316
	else if ( package->GetType() == TYPE_XSP )
3317
		return FindXspPackage(((CXspFile *)package)->GetShipID());
3318
	else if ( package->GetType() == TYPE_ARCHIVE )
50 cycrow 3319
		return FindArchivePackage(package->name());
1 cycrow 3320
 
3321
	// nothing found obviously
3322
	return 0;
3323
}
3324
 
3325
CBaseFile *CPackages::FindFirstPackageWithFile(C_File *f)
3326
{
3327
	return FindNextPackageWithFile(NULL, f);
3328
}
3329
 
3330
CBaseFile *CPackages::FindNextPackageWithFile(CBaseFile *p, C_File *f)
3331
{
3332
	bool startCheck = (p) ? false : true;
3333
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
3334
	{
3335
		if ( startCheck )
3336
		{
3337
			if ( node->Data()->IsFileAdded(f) )
3338
				return node->Data();
3339
		}
3340
		else if ( p == node->Data() )
3341
			startCheck = true;
3342
	}
3343
 
3344
	return NULL;
3345
}
3346
 
3347
 
3348
/**
3349
 * Find a File
3350
 *
3351
 * Searches for a file matching the filetype and filename
3352
 * Optional dir is used for extras files
3353
 */
3354
C_File *CPackages::FindFile(int filetype, CyString filename, CyString dir)
3355
{
3356
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3357
	{
3358
		C_File *f = node->Data();
3359
		if ( f->GetFileType() != filetype )
3360
			continue;
3361
 
3362
		if ( !f->GetFilename().Compare(filename) )
3363
			continue;
3364
 
3365
		if ( !dir.Empty() && f->GetDir().Compare(dir) )
3366
			continue;
3367
 
3368
		return f;
3369
	}
3370
 
3371
	return NULL;
3372
}
3373
 
3374
CArchiveFile *CPackages::FindArchivePackage(CyString name)
3375
{
3376
	// interate through all packages
3377
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
3378
	{
3379
		CBaseFile *file = node->Data();
3380
 
3381
		// only look for archive packages
3382
		if ( file->GetType() != TYPE_ARCHIVE )
3383
			continue;
3384
 
3385
		// now compare the name and author, "Compare" is a non case senseative check, opposed to ==.
50 cycrow 3386
		if ( file->name().Compare(name.ToString()) )
1 cycrow 3387
			return (CArchiveFile *)file;
3388
	}
3389
 
3390
	// nothing found
3391
	return 0;
3392
}
3393
 
3394
/**
3395
 * Find a SPK Package
3396
 *
3397
 * This searching all installed packages for a SPK Package matching the name and author
3398
 */
3399
CBaseFile *CPackages::FindSpkPackage(CyString name, CyString author)
3400
{
3401
	// interate through all packages
3402
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
3403
	{
3404
		CBaseFile *file = node->Data();
3405
 
3406
		// only look for spk packages
3407
		if ( file->GetType() != TYPE_SPK )
3408
			continue;
3409
 
3410
		// now compare the name and author, "Compare" is a non case senseative check, opposed to ==.
50 cycrow 3411
		if ( file->name().Compare(name.ToString()) && file->author().Compare(author.ToString()) )
1 cycrow 3412
			return file;
3413
	}
3414
 
3415
	// nothing found
3416
	return 0;
3417
}
3418
 
3419
CBaseFile *CPackages::FindPackage(CyString name, CyString author)
3420
{
3421
	// interate through all packages
3422
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
3423
	{
3424
		CBaseFile *file = node->Data();
3425
 
3426
		// now compare the name and author, "Compare" is a non case senseative check, opposed to ==.
50 cycrow 3427
		if ( file->name().Compare(name.ToString()) && file->author().Compare(author.ToString()) )
1 cycrow 3428
			return file;
3429
	}
3430
 
3431
	// nothing found
3432
	return 0;
3433
}
3434
 
3435
/**
3436
 * Find a XSP Package
3437
 *
3438
 * This searching all installed packages for a XSP Package matching the object id
3439
 */
3440
CBaseFile *CPackages::FindXspPackage(CyString id)
3441
{
3442
	// interate through all packages
3443
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
3444
	{
3445
		CBaseFile *file = node->Data();
3446
 
3447
		// only look for spk packages
3448
		if ( file->GetType() != TYPE_XSP )
3449
			continue;
3450
 
3451
		// now compare the id, "Compare" is a non case senseative check, opposed to ==.
14 cycrow 3452
		if ( ((CXspFile *)file)->GetShipID().Compare(id.ToString()) )
1 cycrow 3453
			return file;
3454
	}
3455
 
3456
	// nothing found
3457
	return 0;
3458
}
3459
 
3460
/**
3461
 * Update the used files count
3462
 *
3463
 * counts how many packages are currently using the file
3464
 */
3465
void CPackages::UpdateUsedFiles(CLinkList<CBaseFile> *ignoreList, bool includedisabled)
3466
{
3467
	// clear amounts for all files
3468
	CListNode<C_File> *fnode = m_lFiles.Front();
3469
	while ( fnode )
3470
	{
3471
		fnode->Data()->ClearUsed();
3472
		fnode = fnode->next();
3473
	}
3474
 
3475
	// update for all packages
3476
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node= node->next() )
3477
	{
3478
		CBaseFile *file = node->Data();
3479
		if ( (ignoreList) && (ignoreList->FindData(file)) )
3480
			continue;
3481
		if ( !includedisabled && !file->IsEnabled() )
3482
			continue;
3483
		// now mark all files
3484
		CListNode<C_File> *fnode = file->GetFileList()->Front();
3485
		while ( fnode )
3486
		{
3487
			fnode->Data()->IncUsed();
3488
			fnode = fnode->next();
3489
		}
3490
	}
3491
}
3492
 
3493
/**
3494
 * Removes all empty directories that might have been created
3495
 */
3496
void CPackages::RemoveUnusedDirectories(CyStringList &dirs, CyStringList *errors)
3497
{
3498
	CDirIO Dir(m_sCurrentDir);
3499
	for ( SStringList *str = dirs.Head(); str; str = str->next )
3500
	{
3501
		CyString dir = str->str;
3502
 
3503
		CyStringList removedDir;
3504
		if ( Dir.RemoveDir(dir, false, true, &removedDir) || !removedDir.Empty() )
3505
		{
3506
			for ( SStringList *node = removedDir.Head(); node; node = node->next )
3507
			{
3508
				CyString displayName = node->str;
3509
				displayName = displayName.Remove(m_sCurrentDir);
3510
				this->AddLogEntry(SPKINSTALL_REMOVEDIR, displayName, errors);
3511
			}
3512
		}
3513
	}
3514
}
3515
 
3516
/**
3517
 * Update signed status
3518
 *
3519
 * Updates the signed status of all packages
3520
 */
3521
void CPackages::UpdateSigned()
3522
{
3523
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
3524
	{
3525
		CBaseFile *p = node->Data();
3526
 
3527
		if ( p->GetType() != TYPE_SPK )
3528
			continue;
3529
 
3530
		((CSpkFile *)p)->UpdateSigned(false);
3531
	}
3532
}
3533
 
3534
/**
3535
 * Check validity of package file
3536
 *
3537
 * Checks if there is a newer version of the package installed
3538
 */
3539
int CPackages::CheckInstallPackage(CBaseFile *package, int check)
3540
{
3541
	if ( package->GetType() == TYPE_XSP && m_iGame == GAME_X2 )
3542
		return INSTALLCHECK_NOSHIP;
3543
 
3544
	// search for an old version
3545
	CBaseFile *oldPackage = FindPackage(package);
3546
 
3547
	// check versions are newer
3548
	if (oldPackage && (check & IC_OLDVERSION))
3549
	{
50 cycrow 3550
		if ( oldPackage->version().compareVersion(package->version()) == COMPARE_OLDER )
1 cycrow 3551
			return INSTALLCHECK_OLDVERSION;
3552
	}
3553
 
3554
	// now check for game version
3555
	if ((check & IC_WRONGGAME) || (check & IC_WRONGVERSION))
3556
	{
3557
		if ( package->AnyGameCompatability() )
3558
		{
3559
			if ( (check & IC_WRONGGAME) && (!package->CheckGameCompatability(m_iGame)) )
3560
				return INSTALLCHECK_WRONGGAME;
3561
			else if ( (check & IC_WRONGVERSION) && (!package->CheckGameVersionCompatability(m_iGame, m_sGameVersion, m_iGameVersion)) )
3562
				return INSTALLCHECK_WRONGVERSION;
3563
		}
3564
	}
3565
 
3566
	// check for modified
3567
	if (m_bVanilla && (check & IC_MODIFIED))
3568
	{
3569
		if ( !package->IsSigned() )
3570
			return INSTALLCHECK_MODIFIED;
3571
	}
3572
 
3573
	return INSTALLCHECK_OK;
3574
}
3575
 
3576
bool CPackages::CheckOtherPackage(CBaseFile *package)
3577
{
3578
	// check the need for another mod
3579
	if ( package->GetType() == TYPE_SPK )
3580
	{
3581
		CSpkFile *spk = (CSpkFile *)package;
3582
		if ( spk->IsAnotherMod() )
3583
		{
3584
			if ( !FindSpkPackage(spk->GetOtherName(), spk->GetOtherAuthor()) )
3585
			{
3586
				// check the install list
3587
				if ( m_lInstallList.empty() )
3588
					return false;
3589
 
3590
				bool found = false;
3591
				for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
3592
				{
50 cycrow 3593
					if ( spk->GetOtherName().Compare(node->Data()->name()) && spk->GetOtherAuthor().Compare(node->Data()->author()) )
1 cycrow 3594
					{
3595
						found = true;
3596
						break;
3597
					}
3598
				}
3599
 
3600
				if ( !found )
3601
					return false;
3602
			}
3603
		}
3604
	}
3605
 
3606
	return true;
3607
}
3608
 
3609
bool CPackages::CheckEnabledDependacy(CBaseFile *p)
3610
{
3611
	// check the for another mod
3612
	if ( p->GetType() == TYPE_SPK )
3613
	{
3614
		if ( ((CSpkFile *)p)->IsAnotherMod() )
3615
		{
3616
			CBaseFile *parent = this->FindSpkPackage(((CSpkFile *)p)->GetOtherName(), ((CSpkFile *)p)->GetOtherAuthor());
3617
			if ( parent )
3618
			{
3619
				if ( !parent->IsEnabled() )
3620
					return false;
3621
			}
3622
			else
3623
				return false;
3624
		}
3625
	}
3626
 
3627
	// check any dependacies
3628
	if ( p->AnyDependacies() )
3629
	{
3630
		for ( CListNode<SNeededLibrary> *dNode = p->GetNeededLibraries()->Front(); dNode; dNode = dNode->next() )
3631
		{
3632
			if ( dNode->Data()->sName.Compare("<package>") )
3633
				continue;
3634
			if ( !this->CheckInstalledDependacy(dNode->Data()->sName, dNode->Data()->sAuthor, dNode->Data()->sMinVersion, true, true) )
3635
				return false;
3636
		}
3637
	}
3638
 
3639
	return true;
3640
}
3641
 
3642
bool CPackages::CheckInstalledDependacy(CyString name, CyString author, CyString version, bool onlyEnabled, bool includePrepared)
3643
{
3644
	CBaseFile *p = this->FindSpkPackage(name, author);
3645
	if ( p )
3646
	{
3647
		// now check version
50 cycrow 3648
		if ( version.CompareVersion(p->version()) == COMPARE_OLDER )
1 cycrow 3649
			return false;
3650
 
3651
		if ( onlyEnabled && !p->IsEnabled() )
3652
			return false;
3653
 
3654
		return true;
3655
	}
3656
 
3657
	// now check the prepared list
3658
	if ( includePrepared )
3659
	{
3660
		for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
3661
		{
3662
			p = node->Data();
3663
			if ( !p )
3664
				continue;
3665
 
50 cycrow 3666
			if ( p->name().Compare(name.ToString()) && p->author().Compare(author.ToString()) )
1 cycrow 3667
			{
50 cycrow 3668
				if ( version.CompareVersion(p->version()) == COMPARE_OLDER )
1 cycrow 3669
					continue;
3670
 
3671
				if ( onlyEnabled && !p->IsEnabled() )
3672
					continue;
3673
 
3674
				return true;
3675
			}
3676
		}
3677
 
3678
		m_lInstallList.RemoveEmpty();
3679
	}
3680
 
3681
	return false;
3682
}
3683
 
3684
int CPackages::GetMissingDependacies(CBaseFile *p, CyStringList *list, bool onlyEnabled, bool includePrepared)
3685
{
3686
	int count = 0;
3687
	CLinkList<SNeededLibrary> *neededList = p->GetNeededLibraries();
3688
	if ( neededList )
3689
	{
3690
		for ( CListNode<SNeededLibrary> *node = neededList->Front(); node; node = node->next() )
3691
		{
3692
			SNeededLibrary *nl = node->Data();
50 cycrow 3693
			if ( !CheckInstalledDependacy((nl->sName.Compare("<package>")) ? p->name() : nl->sName, (nl->sAuthor.Compare("<author>")) ? p->author() : nl->sAuthor, nl->sMinVersion, (nl->sName.Compare("<package>")) ? false : onlyEnabled, (nl->sName.Compare("<package>")) ? false : includePrepared) )
1 cycrow 3694
			{
3695
				if ( list )
50 cycrow 3696
					list->PushBack(((nl->sName.Compare("<package>")) ? CyString(p->name()) : nl->sName) + "|" + nl->sMinVersion, (nl->sAuthor.Compare("<author>")) ? CyString(p->author()) : nl->sAuthor);
1 cycrow 3697
				++count;
3698
			}
3699
		}
3700
	}
3701
 
3702
	if ( p->GetType() == TYPE_SPK )
3703
	{
3704
		CSpkFile *spk = (CSpkFile *)p;
3705
		if ( spk->IsAnotherMod() )
3706
		{
3707
			bool found = true;
3708
			if ( !this->FindSpkPackage(spk->GetOtherName(), spk->GetOtherAuthor()) )
3709
			{
3710
				if ( includePrepared )
3711
				{
3712
					found = false;
3713
					for ( CListNode<CBaseFile> *pNode = m_lInstallList.Front(); pNode; pNode = pNode->next() )
3714
					{
3715
						CBaseFile *checkP = pNode->Data();
50 cycrow 3716
						if ( p->author().Compare(checkP->author()) && p->name().Compare(checkP->name()) )
1 cycrow 3717
						{
3718
							found = true;
3719
							break;
3720
						}
3721
					}
3722
				}
3723
				else
3724
					found = false;
3725
			}
3726
 
3727
			if ( !found )
3728
			{
3729
				if ( list )
10 cycrow 3730
					list->PushBack(CyString(spk->GetOtherName()), CyString(spk->GetOtherAuthor()));
1 cycrow 3731
				++count;
3732
			}
3733
		}
3734
	}
3735
 
3736
	return count;
3737
}
3738
 
3739
int CPackages::CheckPreparedInstallRequired(CLinkList<CBaseFile> *list)
3740
{
3741
	// loop through all packages
3742
	int count = 0;
3743
	for ( CListNode<CBaseFile> *node = m_lInstallList.Front(); node; node = node->next() )
3744
	{
3745
		CBaseFile *p = node->Data();
3746
 
3747
		// no requirements found, remove from list
3748
		bool missingDep = false;
3749
		if ( !this->CheckOtherPackage(p) )
3750
			missingDep = true;
3751
		else if ( this->GetMissingDependacies(p, NULL, false, true) )
3752
			missingDep = true;
3753
 
3754
		if ( missingDep )
3755
		{
3756
			if ( list )
3757
			{
3758
				node->ChangeData(NULL);
3759
				list->push_back(p);
3760
			}
3761
			else
3762
				node->DeleteData();
3763
			++count;
3764
		}
3765
	}
3766
 
3767
	m_lInstallList.RemoveEmpty();
3768
 
3769
	return count;
3770
}
3771
 
3772
 
3773
/**
3774
 * Remove uninstall file
3775
 *
3776
 * Removes a single uninstall file
3777
 */
3778
bool CPackages::RemoveUninstallFile(C_File *file, CyStringList *errors)
3779
{
3780
	CFileIO fio(file->GetFilePointer());
3781
	if ( fio.Exists() )
3782
	{
3783
		if ( fio.Remove() )
3784
		{
3785
			this->AddLogEntry(SPKINSTALL_UNINSTALL_REMOVE, file->GetNameDirectory(NULL), errors);
3786
			return true;
3787
		}
3788
		else if ( errors )
3789
			this->AddLogEntry(SPKINSTALL_UNINSTALL_REMOVE_FAIL, file->GetNameDirectory(NULL), errors);
3790
	}
3791
 
3792
	return false;
3793
}
3794
 
3795
/**
3796
 * Remove uninstall scripts
3797
 *
3798
 * Removes any unused unisntall scripts
3799
 * Finds if any scripts are in the current package
3800
 */
3801
int CPackages::RemoveUninstallScripts(CyStringList *errors, CProgressInfo *progress)
3802
{
3803
	if ( m_lUninstallFiles.empty() )
3804
		return 0;
3805
 
3806
	UpdateUsedFiles();
3807
	int files = 0;
3808
 
3809
	for ( CListNode<C_File> *node = m_lUninstallFiles.Back(); node; node = node->prev() )
3810
	{
3811
		if ( progress )
3812
			progress->UpdateProgress(files, m_lUninstallFiles.size());
3813
		files++;
3814
 
3815
		C_File *file = node->Data();
3816
 
3817
		// first check if there is a matching file
3818
		bool found = false;
3819
		for ( CListNode<C_File> *fNode = m_lFiles.Front(); fNode; fNode = fNode->next() )
3820
		{
3821
			C_File *checkFile = fNode->Data();
3822
			if ( checkFile->GetFileType() != FILETYPE_SCRIPT )
3823
				continue;
3824
 
3825
			if ( checkFile->GetFilename().Compare(file->GetFilename()) )
3826
			{
3827
				found = true;
3828
				break;
3829
			}
3830
		}
3831
 
3832
		// not found a matching file, we can safetly remove it
3833
		if ( !found )
3834
		{
3835
			if ( RemoveUninstallFile(file) )
3836
				node->DeleteData();
3837
		}
3838
	}
3839
 
3840
	m_lUninstallFiles.RemoveEmpty();
3841
 
3842
	return files;
3843
}
3844
 
3845
 
3846
/**
3847
 * Remove unused shared file
3848
 *
3849
 * Removes a single file
3850
 */
3851
bool CPackages::RemoveSharedFile(C_File *file, CyStringList *errors)
3852
{
3853
	CFileIO fio(file->GetFilePointer());
3854
	if ( fio.Exists() )
3855
	{
3856
		if ( fio.Remove() )
3857
		{
3858
			this->AddLogEntry(SPKINSTALL_SHARED, file->GetNameDirectory(NULL), errors);
3859
			delete file;
3860
			return true;
3861
		}
3862
		else if ( errors )
3863
			this->AddLogEntry(SPKINSTALL_SHARED_FAIL, file->GetNameDirectory(NULL), errors);
3864
	}
3865
 
3866
	return false;
3867
}
3868
 
3869
/**
3870
 * Remove Unused Shared Files
3871
 *
3872
 * Files that have been marked as shared will not be removed or disabled when the last package is removed
3873
 * This function will remove any that are no longer connected with packages
3874
 *
3875
 * Marked shared fiels are mainly used for library scripts that arn't always added to packages
3876
 */
3877
int CPackages::RemoveUnusedSharedFiles(CyStringList *errors, CProgressInfo *progress)
3878
{
3879
	UpdateUsedFiles();
3880
	int files = 0;
3881
	int done = 0;
3882
 
3883
	for ( CListNode<C_File> *node = m_lFiles.Back(); node; node = node->prev() )
3884
	{
3885
		if ( progress )
3886
			progress->UpdateProgress(files, m_lFiles.size());
3887
		files++;
3888
 
3889
		C_File *file = node->Data();
3890
 
3891
		// only do marked shared files
3892
		if ( !file->IsShared() )
3893
			continue;
3894
 
3895
		// only do ones that are no longer needed
3896
		if ( file->GetUsed() )
3897
			continue;
3898
 
3899
		if ( RemoveSharedFile(file, errors) )
3900
			++done;
3901
		node->ChangeData(NULL);
3902
	}
3903
 
3904
	m_lFiles.RemoveEmpty();
3905
 
3906
	return done;
3907
}
3908
 
3909
/**
3910
 * Any Unused Shared
3911
 *
3912
 * Checks if theres any unused shared files available
3913
 *
3914
 * Any file thats marked as shared, and is no longer connected to any installed package
3915
 */
3916
bool CPackages::AnyUnusedShared()
3917
{
3918
	UpdateUsedFiles();
3919
 
3920
	for ( CListNode<C_File> *node = m_lFiles.Back(); node; node = node->prev() )
3921
	{
3922
		C_File *file = node->Data();
3923
 
3924
		// only do marked shared files
3925
		if ( !file->IsShared() )
3926
			continue;
3927
 
3928
		// only do ones that are no longer needed
3929
		if ( file->GetUsed() )
3930
			continue;
3931
 
3932
		return true;
3933
	}
3934
 
3935
	return false;
3936
 
3937
}
3938
 
3939
/**
3940
 * Update original files list
3941
 *
3942
 * Scan the current directory for any fiels that are already there, ie ones that have not been installed
3943
 * Original files should be all the scripts/sounds, etc that are in an unmodified game directory
3944
 *
3945
 * Save list of files and check if any packages overright these files
3946
 * Any file that gets overrighten from this list will automatically be backed up, and restored once the packages are removed
3947
 */
3948
void CPackages::StoreOriginalFiles(int filetype, CyString searchPath)
3949
{
3950
	CyString ext = "pck";
3951
	switch ( filetype )
3952
	{
3953
		case FILETYPE_SOUND:
3954
			ext = "mp3";
3955
			break;
3956
	}
3957
 
3958
	CDirIO Dir(m_sCurrentDir + "/" + searchPath);
3959
 
3960
	CyStringList *files = Dir.DirList();
3961
	if ( !files )
3962
		return;
3963
 
3964
	for ( SStringList *node = files->Head(); node; node = node->next )
3965
	{
3966
		CFileIO File(Dir.File(node->str));
3967
 
3968
		if ( File.GetFileExtension().Compare(ext) )
3969
			AddOriginalFile(filetype, File.GetFilename(), searchPath);
3970
	}
3971
 
3972
	delete files;
3973
}
3974
 
3975
/**
3976
 * Adds a file onto the original files list
3977
 *
3978
 * Checks if it already exists so we dont create multiples
3979
 */
3980
void CPackages::AddOriginalFile(int filetype, CyString filename, CyString searchPath)
3981
{
3982
	// dont add plugin manager as original files
3983
	if ( filetype == FILETYPE_SCRIPT && filename.IsIn("!init.pmanager") )
3984
		return;
3985
	// first check if a matching one exists
3986
	for ( CListNode<C_File> *node = m_lOriginalFiles.Front(); node; node = node->next() )
3987
	{
3988
		C_File *file = node->Data();
3989
		if ( file->GetFileType() != filetype )
3990
			continue;
3991
 
3992
		if ( file->GetFilename().Compare(filename) )
3993
			return;
3994
	}
3995
 
3996
	// check that no packages are currently using them either, not original if being used already
3997
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
3998
	{
3999
		C_File *file = node->Data();
4000
		if ( file->GetFileType() != filetype )
4001
			continue;
4002
 
4003
		if ( file->GetFilename().Compare(filename) )
4004
			return;
4005
	}
4006
 
4007
	C_File *of = new C_File(filename);
4008
	of->SetFileType(filetype);
4009
	if ( filetype == FILETYPE_EXTRA )
4010
		of->SetDir(searchPath);
4011
	of->SetFilename(m_sCurrentDir + "/" + of->GetNameDirectory(NULL));
4012
 
4013
	m_lOriginalFiles.push_back(of);
4014
}
4015
 
4016
/**
4017
 * Check for original file
4018
 *
4019
 * Checks if the file is an original file or not
4020
 */
4021
bool CPackages::IsOriginalFile(C_File *file)
4022
{
4023
	if ( GetOriginalFile(file) )
4024
		return true;
4025
	return false;
4026
}
4027
 
4028
/**
4029
 * Get original file
4030
 *
4031
 * Finds a matching original file entry and returns it
4032
 */
4033
C_File *CPackages::GetOriginalFile(C_File *file)
4034
{
4035
	if ( !file )
4036
		return NULL;
4037
 
4038
	for ( CListNode<C_File> *node = m_lOriginalFiles.Front(); node; node = node->next() )
4039
	{
4040
		C_File *of = node->Data();
4041
 
4042
		// not of the same file type
4043
		if ( of->GetFileType() != file->GetFileType() )
4044
			continue;
4045
 
4046
		// same file name, must be the same file
4047
		if ( of->GetFilename().Compare(file->GetFilename()) )
4048
			return of;
4049
	}
4050
 
4051
	return NULL;
4052
}
4053
 
4054
/**
4055
 * Backs up an original file
4056
 *
4057
 * Moves the file into the PluginManager/Original directory so packages dont replace them
4058
 * Allows these files to be restored when the package is remove to prevent breaking the game
4059
 */
4060
bool CPackages::BackupOriginalFile(C_File *f, CyStringList *errors)
4061
{
4062
	CyString newDir = CyString("PluginManager/Original/") + f->GetDirectory(NULL);
4063
	CDirIO oDir(m_sCurrentDir);
4064
	if ( oDir.Exists(newDir + "/" + f->GetFilename()) )
4065
		return true;
4066
 
4067
	// make sure the directory exissts
4068
	if ( !oDir.Exists(newDir) )
4069
	{
4070
		if ( !oDir.Create(newDir) )
4071
		{
50 cycrow 4072
			CLog::logf(CLog::Log_Install, 2, "Unable to create directory to backup original files, %s", newDir.c_str());
1 cycrow 4073
			this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY_FAIL, newDir, errors);
4074
			return false;
4075
		}
4076
 
50 cycrow 4077
		CLog::logf(CLog::Log_Install, 2, "Created new directory, %s", newDir.c_str());
1 cycrow 4078
		this->AddLogEntry(SPKINSTALL_CREATEDIRECTORY, newDir, errors);
4079
	}
4080
 
4081
	// now lets copy the file
4082
	CFileIO CopyFile(f->GetFilePointer());
4083
	if ( CopyFile.Copy(oDir.File(newDir + "/" + f->GetFilename())) )
4084
	{
50 cycrow 4085
		CLog::logf(CLog::Log_Install, 2, "Original file: %s has been backed up", f->GetNameDirectory(NULL).c_str());
1 cycrow 4086
		this->AddLogEntry(SPKINSTALL_ORIGINAL_BACKUP, f->GetNameDirectory(NULL), errors);
4087
		return true;
4088
	}
4089
	else
4090
	{
50 cycrow 4091
		CLog::logf(CLog::Log_Install, 2, "Failed to backup the original file: %s", f->GetNameDirectory(NULL).c_str());
1 cycrow 4092
		this->AddLogEntry(SPKINSTALL_ORIGINAL_BACKUP_FAIL, f->GetNameDirectory(NULL), errors);
4093
		return false;
4094
	}
4095
}
4096
 
4097
void CPackages::ShuffleTextFiles(CyStringList *errors)
4098
{
4099
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
4100
	{
4101
		C_File *f = node->Data();
4102
		// only do files that are enabled
4103
		if ( f->IsDisabled() )
4104
			continue;
4105
 
4106
		if ( f->GetFileType() != FILETYPE_TEXT )
4107
			continue;
4108
 
4109
		// check if the file is an auto text file
4110
		if ( !f->IsAutoTextFile() )
4111
			continue;
4112
 
4113
		// we need to rename it
4114
		int current = FindNextTextFile();
4115
		if ( current < f->GetTextFileID() )
4116
		{
4117
			CFileIO moveFile(f->GetFilePointer());
4118
 
4119
			CyString newName = SPK::FormatTextName(current, m_iLanguage, (m_iGameFlags & EXEFLAG_TCTEXT)) + "." + moveFile.GetFileExtension();
4120
			if ( moveFile.Rename(m_sCurrentDir + "/t/" + newName) )
4121
			{
4122
				this->AddLogEntry(SPKINSTALL_AUTOTEXT, f->GetName() + "~" + newName, errors);
4123
				f->SetName(newName);
4124
			}
4125
			else
4126
				this->AddLogEntry(SPKINSTALL_AUTOTEXT_FAIL, f->GetName() + "~" + newName, errors);
4127
		}
4128
	}
4129
}
4130
 
4131
/**
4132
 * Shuffle Fake Patches
4133
 *
4134
 * Rename the fake patches so they are always in sequence to be loaded into the game
4135
 *
4136
 * IE, when one is removed, the rest need to be shuffled down so theres no gaps
4137
 */
4138
void CPackages::ShuffleFakePatches(CyStringList *errors)
4139
{
4140
	int check = FindNextFakePatch();
4141
 
4142
	// lets make sure our order is correct
4143
	// find Lowest Fake Patch Installed
4144
	CLinkList<C_File> doneList;
4145
	int lowest = FindLowestFakePatchInstalled();
4146
	if ( check < lowest ) lowest = check; // gap at the beginning, lets use that
4147
 
4148
	// lets define the order
4149
	CyStringList fakePatchOrder;
4150
	for ( SStringList *str = m_lFakePatchOrder.Head(); str; str = str->next )
4151
		fakePatchOrder.PushBack(str->str, str->data);
4152
 
4153
	// add any other packages that need to be ordered
4154
	// we need to work out the correct order
4155
	CLinkList<CBaseFile> packages;
4156
	for ( CListNode<CBaseFile> *pNode = m_lPackages.Front(); pNode; pNode = pNode->next() )
4157
	{
4158
		CBaseFile *p = pNode->Data();
4159
		// search for any fake patches in package
4160
		if ( !p->IsEnabled() ) continue;
4161
		if ( !p->AnyFileType(FILETYPE_MOD) ) continue;
4162
 
4163
		// must have an order define
4164
		if ( !p->AnyFakePatchOrder() ) continue;
4165
 
4166
		// check if theres any already ordered (manual)
4167
		bool added = false;
4168
		for ( SStringList *sCheck = fakePatchOrder.Head(); sCheck; sCheck = sCheck->next )
4169
		{
50 cycrow 4170
			if ( sCheck->str.Compare(CyString(p->name())) && sCheck->data.Compare(CyString(p->author())) )
1 cycrow 4171
			{
4172
				added = true;
4173
				break;
4174
			}
4175
		}
4176
 
4177
		if ( added ) continue;
4178
 
4179
		bool anyFound = false;
4180
		for ( C_File *file = p->GetFirstFile(FILETYPE_MOD); file; file = p->GetNextFile(file) )
4181
		{
4182
			if ( !file->IsFakePatch() ) continue;
4183
			if ( !file->CheckFileExt("cat") ) continue;
4184
			if ( doneList.FindData(file) ) 	continue;
4185
			anyFound = true;
4186
			break;
4187
		}
4188
 
4189
		// we have some fake patches that need to be shuffled
4190
		if ( anyFound )
4191
			packages.push_back(p);
4192
	}
4193
 
4194
	// lets adjust the order (only if theres more than 1
4195
	if ( packages.size() > 1 )
4196
	{
4197
		CLinkList<CBaseFile> sortedPackages;
4198
 
4199
		// first add all the packages that dont need to be installed after
4200
		for ( CListNode<CBaseFile> *pNode = packages.Front(); pNode; pNode = pNode->next() )
4201
		{
4202
			CBaseFile *p = pNode->Data();
4203
			// we have a before and not after
4204
			if ( !p->GetFakePatchBeforeOrder().Empty() && p->GetFakePatchAfterOrder().Empty() )
4205
			{
4206
				// if we have to install before, check if any on the list
4207
				int earliestPos = -1;
4208
				bool notAdded = true;
4209
				for ( SStringList *str = p->GetFakePatchBeforeOrder().Head(); str; str = str->next )
4210
				{
4211
					int pos = 0;
4212
					for ( CListNode<CBaseFile> *sNode = sortedPackages.Front(); sNode; sNode = sNode->next() )
4213
					{
50 cycrow 4214
						if ( str->str.Compare(CyString(sNode->Data()->name())) && str->data.Compare(CyString(sNode->Data()->author())) )
1 cycrow 4215
						{
4216
							if ( earliestPos == -1 || pos < earliestPos )
4217
								earliestPos = pos;
4218
							break;
4219
						}
4220
						++pos;
4221
					}					
4222
				}
4223
 
4224
				if ( earliestPos > -1 )
4225
					sortedPackages.insert(earliestPos, p);
4226
				// otherwise just add it at the back
4227
				else
4228
					sortedPackages.push_back(p);
4229
 
4230
				// remove from the list
4231
				pNode->ChangeData(NULL);
4232
			}
4233
		}
4234
 
4235
		// now do the packages that have both before and after
4236
		packages.RemoveEmpty();
4237
		for ( CListNode<CBaseFile> *pNode = packages.Front(); pNode; pNode = pNode->next() )
4238
		{
4239
			CBaseFile *p = pNode->Data();
4240
			if ( !p->GetFakePatchBeforeOrder().Empty() && !p->GetFakePatchAfterOrder().Empty() )
4241
			{
4242
			}
4243
		}
4244
 
4245
		// add them onto the list
4246
		for ( CListNode<CBaseFile> *pNode = sortedPackages.Front(); pNode; pNode = pNode->next() )
50 cycrow 4247
			fakePatchOrder.PushBack(CyString(pNode->Data()->name()), CyString(pNode->Data()->author()));
1 cycrow 4248
	}
4249
 
4250
	// now add to do list
4251
	for ( CListNode<CBaseFile> *pNode = packages.Front(); pNode; pNode = pNode->next() )
50 cycrow 4252
		fakePatchOrder.PushBack(CyString(pNode->Data()->name()), CyString(pNode->Data()->author()));
1 cycrow 4253
 
4254
	for ( SStringList *str = fakePatchOrder.Head(); str; str = str->next )
4255
	{
4256
		CBaseFile *package = FindPackage(str->str, str->data);
4257
		if ( package )
4258
		{
4259
			// might have more than 1 fake patch for the file, so we'll need the lowest
4260
			while ( true )
4261
			{
4262
				C_File *lowestFile = NULL;
4263
				for ( C_File *file = package->GetFirstFile(FILETYPE_MOD); file; file = package->GetNextFile(file) )
4264
				{
4265
					if ( !file->IsFakePatch() ) continue;
4266
					if ( !file->CheckFileExt("cat") ) continue; // only do the cat file, we can shuffle the dat file to match later
4267
					if ( doneList.FindData(file) ) continue; // already done?
4268
 
4269
					if ( !lowestFile )
4270
						lowestFile = file;
4271
					else
4272
					{
4273
						if ( file->GetBaseName().ToInt() < lowestFile->GetBaseName().ToInt() )
4274
							lowestFile = file;
4275
					}
4276
				}
4277
 
4278
				if ( !lowestFile ) // no more files ?
4279
					break;
4280
 
4281
				// check its filename, it might already be in the correct place
4282
				if ( lowestFile->GetBaseName().ToInt() != lowest )
4283
				{
4284
					// if the file already exists, we need to move it elsewhere
4285
					CyString nextName = CyString::Number(lowest).PadNumber(2);
4286
					if ( CFileIO(m_sCurrentDir + "/" + nextName + ".cat").Exists() )
4287
					{
4288
						// find the file in our internal file list
4289
						C_File *moveFile = FindFile(FILETYPE_MOD, nextName + ".cat");
4290
						if ( !moveFile ) // must not have it in our list ? lets move to the next
4291
						{
4292
							lowest++;
4293
							continue;
4294
						}
4295
 
4296
						// now we can move the the cat/dat file elsewhere, lets shuffle it to the highest free number
4297
						ShufflePatchTo(moveFile, FindLastFakePatch(), errors);
4298
					}
4299
 
4300
					// space should be free, now lets shuffle it
4301
					ShufflePatchTo(lowestFile, lowest, errors);
4302
				}
4303
 
4304
				doneList.push_back(lowestFile); // we've done this file now
4305
				lowest++; // move up the lowest ready for the next patch
4306
			}
4307
		}
4308
	}
4309
 
4310
	// now lets shuffle the rest
4311
	// now find any packages with greater fake patchs and fill the gaps
4312
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
4313
	{
4314
		C_File *f = node->Data();
4315
		// already done?
4316
		if ( doneList.FindData(f) ) 
4317
			continue;
4318
 
4319
		// only do files that are enabled
4320
		if ( f->IsDisabled() )
4321
			continue;
4322
 
4323
		// check if the file is a fake patch
4324
		if ( !f->IsFakePatch() )
4325
			continue;
4326
 
4327
		// we only want cat and dat files, all fake patchs should be, but incase theres an error in the package somewhere we can check
4328
		if ( !f->CheckFileExt("cat") )
4329
			continue;
4330
 
4331
		// now lets check if its greater than our gap
4332
		int check = FindNextFakePatch();
4333
		int patchNum = f->GetFilename().GetToken(".", 1, 1).ToInt();
4334
		if ( patchNum <= check )
4335
			continue;
4336
 
4337
		ShufflePatchTo(f, check, errors);
4338
	}
4339
}
4340
 
4341
void CPackages::ShufflePatchTo(C_File *file, int to, CyStringList *errors)
4342
{
4343
	// it is, we need to shift this to fill the gap
4344
	CyString newName = CyString::Number(to).PadNumber(2) + "." + file->GetFileExt();
4345
 
4346
	// now rename the file
4347
	CFileIO moveFile(file->GetFilePointer());
4348
	if ( moveFile.Rename(m_sCurrentDir + "/" + newName) )
4349
	{
4350
		// display moveing
4351
		this->AddLogEntry(SPKINSTALL_FAKEPATCH, file->GetName() + "~" + newName, errors);
4352
 
4353
		// now find the matching pairing if it exists
4354
		for ( CListNode<C_File> *node2 = m_lFiles.Front(); node2; node2 = node2->next() )
4355
		{
4356
			C_File *f2 = node2->Data();
4357
 
4358
			if ( f2->IsDisabled() || !f2->IsFakePatch() || f2->CheckFileExt(file->GetFileExt()) )
4359
				continue;
4360
 
4361
			// needs to be the same file
4362
			if ( f2->GetBaseName() != file->GetBaseName() )
4363
				continue;
4364
 
4365
			CyString newName2 = CyString::Number(to).PadNumber(2) + "." + f2->GetFileExt();
4366
			CFileIO moveFile(f2->GetFilePointer());
4367
			if ( moveFile.Rename(m_sCurrentDir + "/" + newName2) )
4368
			{
4369
				this->AddLogEntry(SPKINSTALL_FAKEPATCH, f2->GetName() + "~" + newName2, errors);
4370
				f2->SetName(newName2);
4371
			}
4372
			else
4373
				this->AddLogEntry(SPKINSTALL_FAKEPATCH_FAIL, f2->GetName() + "~" + newName2, errors);
4374
		}
4375
 
4376
		// finally make sure the internal name matches the new one
4377
		file->SetName(newName);
4378
	}
4379
	else
4380
		this->AddLogEntry(SPKINSTALL_FAKEPATCH_FAIL, file->GetName() + "~" + newName, errors);
4381
}
4382
 
4383
int CPackages::FindLowestFakePatchInstalled()
4384
{
4385
	int lowest = 99;
4386
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
4387
	{
4388
		CBaseFile *p = node->Data();
4389
		if ( !p->IsEnabled() ) continue;
4390
		if ( !p->AnyFileType(FILETYPE_MOD) ) continue;
4391
 
4392
		// now get all fake patches
4393
		for ( C_File *file = p->GetFirstFile(FILETYPE_MOD); file; file = p->GetNextFile(file) )
4394
		{
4395
			if ( !file->IsFakePatch() ) continue;
4396
			if ( file->CheckFileExt("dat") ) continue;
4397
			int check = file->GetBaseName().ToInt();
4398
			if ( check < lowest )
4399
				lowest = check;
4400
		}
4401
	}
4402
 
4403
	return lowest;
4404
}
4405
 
4406
int CPackages::FindLastFakePatch(int start, CyString dir)
4407
{
4408
	CDirIO Dir((dir.Empty()) ? m_sCurrentDir : dir);
4409
 
4410
	int check = start;
4411
	while ( check > 0 )
4412
	{
4413
		CyString checkStr = CyString::Number(check).PadNumber(2);
4414
 
4415
		// check if a cat file exists
4416
		if ( !Dir.Exists(checkStr + ".cat") )
4417
		{
4418
			// it doen't, check if theres a dat file (incase of package error)
4419
			if ( !Dir.Exists(checkStr + ".dat") )
4420
				break;
4421
		}
4422
		--check;
4423
	}
4424
 
4425
	return check;
4426
}
4427
 
4428
/**
4429
 * Find next fake patch
4430
 *
4431
 * Searching for the next gap in patches, starting with 01.cat to 99.cat
4432
 */
4433
int CPackages::FindNextFakePatch(int start, CyString dir)
4434
{
4435
	CDirIO Dir((dir.Empty()) ? m_sCurrentDir : dir);
4436
 
4437
	int check = start;
4438
	while ( check < 99 )
4439
	{
4440
		++check;
4441
		CyString checkStr = CyString::Number(check).PadNumber(2);
4442
 
4443
		// check if a cat file exists
4444
		if ( !Dir.Exists(checkStr + ".cat") )
4445
		{
4446
			// it doen't, check if theres a dat file (incase of package error)
4447
			if ( !Dir.Exists(checkStr + ".dat") )
4448
				break;
4449
		}
4450
	}
4451
 
4452
	return check;
4453
}
4454
 
4455
/**
4456
 * Find next text file
4457
 *
4458
 * Searching for the next gap in automatic text files, start with 0003-LXXX or XX0003
4459
 */
4460
int CPackages::FindNextTextFile(int start, CyString dir)
4461
{
4462
	int check = start;
4463
	if ( check < 2 ) check = 2;
4464
	while ( check < 9999 )
4465
	{
4466
		++check;
43 cycrow 4467
		Utils::String newFilename = SPK::FormatTextName(check, m_iLanguage, (m_iGameFlags & EXEFLAG_TCTEXT)).ToString();
1 cycrow 4468
 
43 cycrow 4469
		// check the vfs
4470
		if ( m_pGameVFS.isFileAvailable("t/" + newFilename + ".pck") ) continue;
4471
		if ( m_pGameVFS.isFileAvailable("t/" + newFilename + ".xml") ) continue;
4472
 
4473
		break;
1 cycrow 4474
	}
4475
 
4476
	return check;
4477
}
4478
 
4479
int CPackages::FindLastTextFile(int start, CyString dir)
4480
{
4481
	CDirIO Dir((dir.Empty()) ? m_sCurrentDir : dir);
4482
	Dir.cd("t");
4483
 
4484
	int check = start;
4485
	while ( check < 9999 )
4486
	{
4487
		++check;
4488
		CyString newFilename = SPK::FormatTextName(check, m_iLanguage, (m_iGameFlags & EXEFLAG_TCTEXT));
4489
 
4490
		// check if a packed file exists
4491
		if ( !Dir.Exists(newFilename + ".pck") )
4492
		{
4493
			// it doen't, check if theres an unpacked file
4494
			if ( !Dir.Exists(newFilename + ".xml") )
4495
				break;
4496
		}
4497
	}
4498
 
4499
	return check;
4500
}
4501
 
4502
/**
4503
 * Read game language
4504
 *
4505
 * Reads the lang.dat file from the game directory for the language id
4506
 */
4507
void CPackages::ReadGameLanguage(bool force)
4508
{
4509
	// check if lauguage is already set
4510
	if ( !force && m_iLanguage )
4511
		return;
4512
 
4513
	CDirIO Dir(m_sCurrentDir);
4514
 
4515
	// check for lang.dat file
4516
	if ( Dir.Exists("lang.dat") )
4517
	{
4518
		CFileIO File(Dir.File("lang.dat"));
4519
 
4520
		size_t size;
4521
		char *data = File.ReadToData(&size);
4522
 
4523
		if ( data )
4524
		{
4525
			CyString str(data);
4526
			m_iLanguage = str.GetToken("\n", 1, 1).GetToken(" ", 1, 1).ToInt();
4527
		}
4528
	}
4529
}
4530
 
4531
/**
4532
 * Create Language Text File
4533
 *
4534
 * Creates text files for all packages into the correct language
4535
 */
4536
void CPackages::CreateLanguageTextFiles(CyStringList *errors)
4537
{
4538
	// no need to create them if theres no language to use
4539
	if ( !m_iLanguage )
4540
		return;
4541
 
4542
	// find all text files
4543
	CyStringList ids;
4544
 
4545
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
4546
	{
4547
		C_File *f = node->Data();
4548
		// only do text fiels
4549
		if ( f->GetFileType() != FILETYPE_TEXT )
4550
			continue;
4551
 
4552
		CyString id, lang;
4553
		if ( m_iGameFlags & EXEFLAG_TCTEXT )
4554
		{
4555
			id = f->GetBaseName().GetToken("-", 1, 1);
4556
			lang = f->GetBaseName().GetToken("-", 2, 2);
4557
			if ( lang.Empty() )
4558
				lang = "NULL";
4559
			else
4560
				lang = lang.Erase(0, 1);  // remove the "L"
4561
		}
4562
		else
4563
		{
4564
			lang = f->GetBaseName().Left((int)f->GetBaseName().Length() - 4).PadNumber(3);
4565
			id = f->GetBaseName().Mid(((int)f->GetBaseName().Length() - 4) + 1, 4);
4566
		}
4567
 
4568
		SStringList *strList = ids.FindString(id);
4569
 
4570
		// not added, add a new one
4571
		if ( !strList )
4572
			strList = ids.PushBack(id, lang);
4573
		else
4574
		{
4575
			if ( strList->data.Empty() )
4576
				strList->data = lang;
4577
			else
4578
			{
4579
				strList->data += ":";
4580
				strList->data += lang;
4581
			}
4582
		}
4583
	}
4584
 
4585
	// we should now have a list of all text files, we need to remove those that have a matching language
4586
	for ( SStringList *sNode = ids.Head(); sNode; sNode = sNode->next )
4587
	{
4588
		int size = 0;
4589
		CyString *data = sNode->data.SplitToken(':', &size);
4590
 
4591
		// huh ? we shouldn't have this
4592
		if ( !size )
4593
			continue;
4594
 
4595
		// lets search for a matching id
4596
		int useId = 0;
4597
		bool found = false;
4598
		for ( int i = 0; i < size; i++ )
4599
		{
4600
			if ( data[i] == "NULL" )
4601
				useId = -1;
4602
			else
4603
			{
4604
				int num = data[i].ToInt();
4605
				if ( num == m_iLanguage )
4606
				{
4607
					found = true;
4608
					useId = m_iLanguage;
4609
					break;
4610
				}
4611
				// prioities the text file to use, no language first, then 44, then 49, otherwise, we pick the first available
4612
				else if ( num == 44 && useId != -1)
4613
					useId = 44;
4614
				// if we have a german language, and its not yet found english or null
4615
				else if ( num == 49 && useId != 44 && useId != -1 )
4616
					useId = 49;
4617
				// if we have not found a valid language yet, we will use this one
4618
				else if ( !useId )
4619
					useId = num;
4620
			}
4621
		}
4622
 
4623
		if ( found )
4624
			continue;
4625
 
4626
		if ( !useId )
4627
			useId = data[0].ToInt();
4628
		if ( !useId )
4629
			useId = -1;
4630
 
4631
		CLEANSPLIT(data, size);
4632
 
4633
		RenameTextFile(sNode->str, useId, errors);
4634
	}
4635
}
4636
 
4637
/**
4638
 * Rename a text file
4639
 *
4640
 * Creates a new text file and copies an existing one
4641
 */
4642
bool CPackages::RenameTextFile(CyString textid, int languageid, CyStringList *errors)
4643
{
4644
	// lets check if the file already exists
4645
	CyString newFilename = SPK::FormatTextName(textid.ToInt(), m_iLanguage, (m_iGameFlags & EXEFLAG_TCTEXT));
4646
	C_File *addFile = FindFile(FILETYPE_TEXT, newFilename + ".xml");
4647
	if ( !addFile )
4648
		addFile = FindFile(FILETYPE_TEXT, newFilename + ".pck");
4649
 
4650
	// we have found the file, lets just add it to our scripts
4651
	if ( addFile )
4652
	{
4653
		AddTextFileToScripts(addFile, textid);
4654
		return true;
4655
	}
4656
 
4657
	// first we need to find our text file
4658
	CyString filename;
4659
	if ( languageid != -1 )
4660
		filename = SPK::FormatTextName(textid.ToInt(), languageid, (m_iGameFlags & EXEFLAG_TCTEXT));
4661
	else
4662
		filename = textid;
4663
 
4664
	C_File *textFile = FindFile(FILETYPE_TEXT, filename + ".xml");
4665
	if ( !textFile )
4666
	{
4667
		textFile = FindFile(FILETYPE_TEXT, filename + ".pck");
4668
		if ( !textFile )
4669
			return false;
4670
	}
4671
 
4672
	// now lets create out new text file
4673
	CFileIO readFile(textFile->GetFilePointer());
4674
	std::vector<CyString> *lines = readFile.ReadLines();
4675
 
4676
	// find the language id in the lines
4677
	std::vector<CyString> frontLines;
4678
	for(std::vector<CyString>::iterator it = lines->begin(); it != lines->end(); ++it)
4679
	{
4680
		CyString line = *it;
4681
		int pos = line.FindPos("<language id");
4682
		if ( pos != -1)
4683
		{
4684
			CyString newLine = "<language id=\"";
4685
			newLine += CyString::Number(m_iLanguage);
4686
			newLine += "\">";
4687
			newLine += line.GetToken(">", 2);
4688
 
4689
			frontLines.insert(frontLines.begin(), newLine);
4690
 
4691
			lines->erase(lines->begin(), ++it);
4692
			break;
4693
		}
4694
		frontLines.insert(frontLines.begin(), line);
4695
	}
4696
 
4697
	for(std::vector<CyString>::iterator it = frontLines.begin(); it != frontLines.end(); ++it)
4698
		lines->insert(lines->begin(), *it);
4699
 
4700
	addFile = new C_File(newFilename + ".xml");
4701
	addFile->SetFileType(FILETYPE_TEXT);
4702
 
4703
	CFileIO writeFile(m_sCurrentDir + "/" + addFile->GetNameDirectory(NULL));
4704
	if ( writeFile.WriteFile(lines) )
4705
	{
4706
		this->AddLogEntry(SPKINSTALL_WRITEFILE, addFile->GetNameDirectory(NULL), errors);
4707
 
4708
		addFile->SetFilename(m_sCurrentDir + "/" + addFile->GetNameDirectory(NULL));
4709
		// now we have the file wrriten, we need to add it to all scripts that need it
4710
		// first we add it to the global list
4711
		m_lFiles.push_back(addFile);
4712
 
4713
		// now add it to the scripts
4714
		AddTextFileToScripts(addFile, textid);
4715
	}
4716
	else
4717
	{
4718
		this->AddLogEntry(SPKINSTALL_WRITEFILE_FAIL, addFile->GetNameDirectory(NULL), errors);
4719
		if ( lines )
4720
			delete lines;
4721
		return false;
4722
	}
4723
 
4724
	if ( lines )
4725
		delete lines;
4726
 
4727
 
4728
	return true;
4729
}
4730
 
4731
/**
4732
 * Add file to scripts
4733
 *
4734
 * Adds a file to all scripts that need it
4735
 */
4736
void CPackages::AddTextFileToScripts(C_File *file, CyString textid)
4737
{
4738
	// now we need to find all scripts that have a matching file and add this to the list
4739
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
4740
	{
4741
		CBaseFile *package = node->Data();
4742
 
4743
		// first make sure it doesn't already exist
4744
		if ( package->GetFileList()->FindData(file) )
4745
			continue;
4746
 
4747
		bool found = false;
4748
		for ( CListNode<C_File> *fNode = package->GetFileList()->Front(); fNode; fNode = fNode->next() )
4749
		{
4750
			C_File *tFile = fNode->Data();
4751
			if ( tFile->GetFileType() != FILETYPE_TEXT )
4752
				continue;
4753
 
4754
			CyString id = tFile->GetBaseName().GetToken("-", 1, 1);
4755
			if ( id == textid )
4756
			{
4757
				found = true;
4758
				break;
4759
			}
4760
		}
4761
 
4762
		if ( found )
4763
			package->GetFileList()->push_back(file);
4764
	}
4765
}
4766
 
4767
bool CPackages::RemoveFile(C_File *file, CyStringList *errors)
4768
{
4769
	if ( !file )
4770
		return true;
4771
 
4772
	CyString remFileStr = file->GetFilePointer();
4773
	remFileStr.FindReplace(m_sCurrentDir, "");
4774
 
4775
	if ( file->GetFilePointer().IsIn("::") ) {
4776
		CFileIO CatFile(file->GetFilePointer().GetToken("::", 1, 1));
4777
		if ( CatFile.Exists() ) {
4778
			CCatFile cat;
4779
			if ( cat.Open(CatFile.GetFullFilename(), this->GetAddonDir(), CATREAD_DAT, false) == CATERR_NONE ) {
4780
				CyString fileName = file->GetFilePointer().GetToken("::", 2, 2);
4781
				if ( cat.FindData(fileName) ) {
4782
					if ( cat.RemoveFile(fileName) ) {
4783
						this->AddLogEntry(SPKINSTALL_DELETEFILE, remFileStr, errors);
4784
					}
4785
					else {
4786
						this->AddLogEntry(SPKINSTALL_DELETEFILE_FAIL, remFileStr, errors);
4787
						return false;
4788
					}
4789
				}
4790
			}
4791
		}
4792
	}
4793
	else {
4794
		CFileIO f(file->GetFilePointer());
4795
		if ( f.Exists() )
4796
		{
4797
			if ( f.Remove() )
4798
				this->AddLogEntry(SPKINSTALL_DELETEFILE, remFileStr, errors);
4799
			else if ( errors )
4800
			{
4801
				this->AddLogEntry(SPKINSTALL_DELETEFILE_FAIL, remFileStr, errors);
4802
				return false;
4803
			}
4804
			else
4805
				return false;
4806
		}
4807
	}
4808
 
4809
	return true;
4810
}
4811
 
4812
int CPackages::RemoveAllPackages(CyStringList *errors, CProgressInfo *progress)
4813
{
4814
	int files = 0;
4815
 
4816
	// remove all files
4817
	int max = m_lFiles.size() + m_lOriginalFiles.size() + m_lUninstallFiles.size();
4818
	for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
4819
	{
4820
		// update the progress
4821
		if ( progress )
4822
			progress->UpdateProgress(files, max);
4823
		++files;
4824
 
4825
		RemoveFile(node->Data());
4826
		delete node->Data();
4827
	}
4828
	m_lFiles.clear();
4829
 
4830
	// restore any original files that are backed up
4831
	for ( CListNode<C_File> *oNode = m_lOriginalFiles.Front(); oNode; oNode = oNode->next() )
4832
	{
4833
		// update the progress
4834
		if ( progress )
4835
			progress->UpdateProgress(files, max);
4836
		++files;
4837
 
4838
		C_File *f = oNode->Data();
4839
		CFileIO of(m_sCurrentDir + "/PluginManager/Original/" + f->GetNameDirectory(NULL));
4840
		if ( of.Exists() )
4841
			of.Rename(m_sCurrentDir + "/" + f->GetNameDirectory(NULL));
4842
 
4843
		//RemoveFile(oNode->Data());
4844
		delete oNode->Data();
4845
	}
4846
	m_lOriginalFiles.clear();
4847
 
4848
	// remove any uninstall files that remain
4849
	for ( CListNode<C_File> *uNode = m_lUninstallFiles.Front(); uNode; uNode = uNode->next() )
4850
	{
4851
		// update the progress
4852
		if ( progress )
4853
			progress->UpdateProgress(files, max);
4854
		++files;
4855
 
4856
		RemoveFile(uNode->Data());
4857
		delete uNode->Data();
4858
	}
4859
	m_lUninstallFiles.clear();
4860
 
4861
	// delete all packages
4862
	for ( CListNode<CBaseFile> *pNode = m_lPackages.Front(); pNode; pNode = pNode->next() )
4863
	{
4864
		// clear file list as we have removed them already
4865
		pNode->Data()->GetFileList()->clear();
4866
		// delete the memory for the package
4867
		delete pNode->Data();
4868
	}
4869
	m_lPackages.clear();
4870
 
4871
	return files;
4872
}
4873
 
4874
/**
4875
 * Get Install Before Text
4876
 *
4877
 * Returns the install before text for the package in the correct language
4878
 */
4879
CyString CPackages::GetInstallBeforeText(CBaseFile *package)
4880
{
46 cycrow 4881
	return package->installText(m_iLanguage, true);
1 cycrow 4882
}
4883
CyString CPackages::GetInstallAfterText(CBaseFile *package)
4884
{
46 cycrow 4885
	return package->installText(m_iLanguage, false);
1 cycrow 4886
}
4887
CyString CPackages::GetUninstallBeforeText(CBaseFile *package)
4888
{
46 cycrow 4889
	return package->uninstallText(m_iLanguage, true);
1 cycrow 4890
}
4891
CyString CPackages::GetUninstallAfterText(CBaseFile *package)
4892
{
46 cycrow 4893
	return package->uninstallText(m_iLanguage, false);
1 cycrow 4894
}
4895
 
4896
int CPackages::GetChildPackages(CBaseFile *package, CLinkList<CBaseFile> *children, bool recursive)
4897
{
4898
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
4899
	{
4900
		CBaseFile *p = node->Data();
4901
		if ( p->GetParent() == package )
4902
		{
4903
			children->push_back(p);
4904
 
4905
			if ( recursive )
4906
				this->GetChildPackages(p, children, recursive);
4907
		}
4908
	}
4909
 
4910
	return children->size();
4911
}
4912
 
4913
void CPackages::AssignPackageNumbers()
4914
{
4915
	int num = 0;
4916
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
4917
		node->Data()->SetNum(num++);
4918
}
4919
 
4920
CyString CPackages::FindDataDir(CyString dir, CyString file)
4921
{
4922
	CDirIO Dir(dir);
4923
	// data files could be in 4 places depending on what program is used, check for all of these
4924
	if ( Dir.Exists(file) )
4925
		return Dir.File(file);
4926
	else if ( Dir.Exists(CyString("Data/") + file) )
4927
		return Dir.File(CyString("Data/") + file);
4928
	else if ( Dir.Exists(CyString("../") + file) )
4929
		return Dir.File(CyString("../") + file);
4930
	else if ( Dir.Exists(CyString("../Data/") + file) )
4931
		return Dir.File(CyString("../Data/") + file);
4932
 
4933
	return NullString;
4934
}
4935
 
4936
void CPackages::Startup(CyString dir, CyString tempDir, CyString myDoc, CyString mod)
4937
{
4938
	this->SetTempDirectory(tempDir);
4939
	this->SetMyDocuments(myDoc);
4940
 
4941
	m_sSetMod = mod;
4942
 
4943
	// need to read the game exe versions
4944
	m_gameExe.Reset();
4945
 
4946
	CyString exeFile = this->FindDataDir(dir, "exe");
4947
 
4948
	// if file exists, read it, otherwise, just add
4949
	if ( !exeFile.Empty() && CFileIO(exeFile).Exists() )
4950
		m_gameExe.ReadFile(exeFile);
4951
	else
4952
	{
4953
		m_gameExe.ParseExe("x2.exe:5:NOSAVESUBDIR:HKCU/Software/EgoSoftware/X2/ModName:X2 The Threat:!GAMEDIR!:2:1604608!2150400:1.4 Artifical Life:1974272:1.5 Uplink");
4954
		m_gameExe.ParseExe("x3.exe:5:0:HKCU/Software/Egosoft/X3/ModName:X3 Reunion:Egosoft/X3:2:2347008:2.0 Bala Gi:2367488!2375680:2.5 Uplink");
4955
		m_gameExe.ParseExe("x3tc.exe:5:NO_XOR|TC_TEXT|MYDOCLOG:HKCU/Software/Egosoft/X3TC/ModName:X3 Terran Conflict:Egosoft/X3TC:3:1933464!1933520:2.0 Aldrin Expansion:-1:2.5 A New Home (Superbox):-1:3.0 Balance of Power");
4956
		m_gameExe.ParseExe("x3ap.exe:2:NO_XOR|TC_TEXT|MYDOCLOG|ADDON:HKCU/Software/Egosoft/X3AP/ModName:X3 Albion Prelude:Egosoft/X3AP:addon!x3tc.exe:0");
4957
	}
4958
}
4959
 
4960
int CPackages::GetGameLanguage(CyString dir)
4961
{
4962
	if ( dir.Empty() )
4963
		dir = m_sCurrentDir;
4964
 
4965
	CDirIO Dir(dir);
4966
 
4967
	// check for lang.dat file
4968
	if ( Dir.Exists("lang.dat") )
4969
	{
4970
		CFileIO File(Dir.File("lang.dat"));
4971
 
4972
		size_t size;
4973
		char *data = File.ReadToData(&size);
4974
 
4975
		if ( data )
4976
		{
4977
			CyString str(data);
4978
			return str.GetToken("\n", 1, 1).GetToken(" ", 1, 1).ToInt();
4979
		}
4980
	}
4981
 
4982
	return 0;
4983
}
4984
CyString CPackages::GetGameRunExe(CyString dir)
4985
{
4986
	return m_gameExe.GetGameRunExe((dir.Empty()) ? m_sCurrentDir : dir);
4987
}
4988
 
4989
CyString CPackages::GetGameNameFromType(int game)
4990
{
4991
	return m_gameExe.GetGameNameFromType(game - 1);
4992
}
4993
CyString CPackages::GetGameName(CyString dir)
4994
{
4995
	return m_gameExe.GetGameName((dir.Empty()) ? m_sCurrentDir : dir);
4996
}
4997
CyString CPackages::GetProperDir(CyString dir)
4998
{
4999
	return m_gameExe.GetProperDir((dir.Empty()) ? m_sCurrentDir : dir);
5000
}
5001
CyString CPackages::GetAddonDir(CyString dir)
5002
{
5003
	return m_gameExe.GetAddonDir((dir.Empty()) ? m_sCurrentDir : dir);
5004
}
5005
int CPackages::GetGameAddons(CyStringList &exes, CyString dir)
5006
{
5007
	return m_gameExe.GetGameAddons((dir.Empty()) ? m_sCurrentDir : dir, exes);
5008
}
5009
 
5010
CyString CPackages::GetGameTypesString(CBaseFile *package, bool includeVersion)
5011
{
5012
	if ( !package->AnyGameCompatability() )
5013
		return NullString;
5014
 
5015
	CyString sGames;
5016
	for ( CListNode<SGameCompat> *gNode = package->GetGameCompatabilityList()->Front(); gNode; gNode = gNode->next() ) {
5017
		if ( !sGames.Empty() )
5018
			sGames += ", ";
5019
		sGames += m_gameExe.GetGameNameFromType(gNode->Data()->iGame - 1);
5020
		if ( includeVersion ) {
5021
			sGames += " (";
5022
			sGames += m_gameExe.GetGameVersionFromType(gNode->Data()->iGame - 1, gNode->Data()->iVersion - 1, gNode->Data()->sVersion);
5023
			sGames += ")";
5024
		}
5025
	}
5026
	return sGames;
5027
}
5028
 
5029
CyString CPackages::GetGameVersionString(CBaseFile *package)
5030
{
5031
	for ( CListNode<SGameCompat> *gNode = package->GetGameCompatabilityList()->Front(); gNode; gNode = gNode->next() ) {
5032
		if ( gNode->Data()->iGame == m_iGame ) {
5033
			return m_gameExe.GetGameVersionFromType(m_iGame - 1, gNode->Data()->iVersion - 1, gNode->Data()->sVersion);
5034
		}
5035
	}
5036
 
5037
	return NullString;
5038
}
5039
 
5040
CyString CPackages::GetGameVersionFromType(int game, int version, CyString sVersion)
5041
{
5042
	return m_gameExe.GetGameVersionFromType(game - 1, version - 1, sVersion);
5043
}
5044
 
5045
int CPackages::CountBuiltInPackages(bool onlyEnabled)
5046
{
5047
	int count = 0;
5048
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
5049
	{
5050
		if ( !node->Data()->CheckGameCompatability(m_iGame) )
5051
			continue;
5052
		if ( onlyEnabled && !node->Data()->IsEnabled() )
5053
			continue;
50 cycrow 5054
		if ( node->Data()->author().Compare("PluginManager") )
1 cycrow 5055
			++count;
5056
	}
5057
 
5058
	return count;
5059
}
5060
 
5061
int CPackages::CountPackages(int type, bool onlyEnabled)
5062
{
5063
	int count = 0;
5064
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
5065
	{
5066
		CBaseFile *p = node->Data();
5067
 
5068
		if ( (type != TYPE_BASE) && (p->GetType() != type) )
5069
			continue;
5070
 
5071
		if ( (onlyEnabled) && (!p->IsEnabled()) )
5072
			continue;
5073
 
5074
		++count;
5075
	}
5076
 
5077
	return count;
5078
}
5079
 
5080
int CPackages::ExtractGameFile(CyString aFilename, CyString aTo, CyString dir, CyString addon)
5081
{
5082
	// first check the enabled mod
5083
	if ( dir.Empty() )
5084
		dir = m_sCurrentDir;
5085
 
5086
	CyString addonDir = addon;
5087
	if ( addonDir.Empty() ) addonDir = this->GetAddonDir(dir);
5088
 
5089
	if ( m_pEnabledMod && m_pEnabledMod->AnyFileType(FILETYPE_MOD) )
5090
	{
5091
		for ( C_File *file = m_pEnabledMod->GetFirstFile(FILETYPE_MOD); file; file = m_pEnabledMod->GetNextFile(file) )
5092
		{
5093
			if ( !file->CheckFileExt("cat") )
5094
				continue;
5095
 
5096
			CCatFile catFile;
5097
			if ( catFile.Open(file->GetFilePointer(), addonDir, CATREAD_CATDECRYPT, false) == CATERR_NONE )
5098
			{
5099
				if ( catFile.ExtractFile(aFilename, aTo) )
5100
					return 1;
5101
				if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR )
5102
				{
5103
					if ( catFile.ExtractFile(aFilename) )
5104
						return -1;
5105
				}
5106
 
5107
			}
5108
		}
5109
	}
5110
	else if ( !m_sSetMod.Empty() )
5111
	{
5112
		if ( CFileIO(m_sCurrentDir + "/mods/" + m_sSetMod + ".cat").Exists() )
5113
		{
5114
			CCatFile catFile;
5115
			if ( catFile.Open(m_sCurrentDir + "/mods/" + m_sSetMod + ".cat", addonDir, CATREAD_CATDECRYPT, false) == CATERR_NONE )
5116
			{
5117
				if ( catFile.ExtractFile(aFilename, aTo) )
5118
					return 1;
5119
				if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR )
5120
				{
5121
					if ( catFile.ExtractFile(aFilename) )
5122
						return -1;
5123
				}
5124
			}
5125
		}
5126
	}
5127
 
5128
	// find the highest cat number
5129
	int catNumber = 1;
5130
	while ( CFileIO(dir + "/" + CyString::Number(catNumber).PadNumber(2) + ".cat").Exists() && catNumber < 99 )
5131
		++catNumber;
5132
 
5133
	// get the last one, not the next free one
5134
	--catNumber;
5135
 
5136
	// work backwards until we find the file
5137
	for ( ; catNumber; catNumber-- )
5138
	{
5139
		CCatFile catFile;
5140
		if ( catFile.Open(dir + "/" + CyString::Number(catNumber).PadNumber(2) + ".cat", addonDir, CATREAD_CATDECRYPT, false) == CATERR_NONE )
5141
		{
5142
			// check for the file
5143
			if ( catFile.ExtractFile(aFilename, aTo) )
5144
				return 1;
5145
			if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR )
5146
			{
5147
				if ( catFile.ExtractFile(aFilename) )
5148
					return -1;
5149
			}
5150
		}
5151
	}
5152
 
5153
	return 0;
5154
}
5155
 
5156
void CPackages::CreateWareFiles()
5157
{
5158
	// do each ware
5159
	int wareTextID = WARETEXTSTART;
5160
	for ( int i = 0; i < WAREBUFFERS; i++ )
5161
	{
5162
		// its empty, no need to create ware files
5163
		if ( !m_lGameWares[i].size() )
5164
			continue;
5165
 
5166
		// lets extract the ware file
5167
		char wareType = CPackages::ConvertWareTypeBack(i);
5168
		CyString wareFile = "TWare";
5169
		wareFile += (char)UPPER(wareType);
5170
 
5171
		CyString openFile;
5172
 
5173
		int e;
5174
		if ( i == WARES_TECH && CFileIO(m_sTempDir + "/TWareT.txt").Exists() )
5175
			openFile = m_sTempDir + "/TWareT.txt";
5176
		else {
5177
			e = ExtractGameFile(CyString("types/") + wareFile + ".pck", m_sTempDir + "/" + wareFile + ".txt");
5178
			if ( e == 1 )
5179
				openFile = m_sTempDir + "/" + wareFile + ".txt";
5180
			else if ( e == -2 )
5181
				openFile = wareFile + ".txt";
5182
		}
5183
 
5184
		if ( !openFile.Empty() )
5185
		{
5186
			// read the file into memory
5187
			CyStringList wareLines;
5188
			int oldSize = -1;
5189
			int version = -1;
5190
 
5191
			// read first number
5192
			CFileIO readFile(m_sTempDir + "/" + wareFile + ".txt");
5193
			CyStringList *lines = readFile.ReadLinesStr();
5194
			if ( lines )
5195
			{
5196
				for ( SStringList *str = lines->Head(); str; str = str->next )
5197
				{
5198
					CyString line(str->str);
5199
					line.RemoveFirstSpace();
5200
					line.RemoveChar(9);
5201
					line.RemoveChar('\r');
5202
					if ( line.Empty() )
5203
						continue;
5204
					if ( line[0] == '/' )
5205
						continue;
5206
 
5207
					if ( oldSize == -1 )
5208
					{
5209
						version = line.GetToken(";", 1, 1).ToInt();
5210
						oldSize = line.GetToken(";", 2, 2).ToInt();
5211
					}
5212
					else
5213
					{
5214
						line.RemoveEndSpace();
5215
						if ( line.Right(1) != ';' )
5216
							line += ";";
5217
						wareLines.PushBack(line);
5218
						if ( wareLines.Count() >= oldSize )
5219
							break;
5220
					}
5221
				}
5222
 
5223
				delete lines;
5224
 
5225
				// apply the buffer
5226
				if ( m_iWareBuffer[i] <= 0 )
5227
					m_iWareBuffer[i] = wareLines.Count() + 10;
5228
				// last resort, readjust the buffer
5229
				else if ( wareLines.Count() > m_iWareBuffer[i] )
5230
					m_iWareBuffer[i] = wareLines.Count();
5231
 
5232
				// add the buffers
5233
				while ( wareLines.Count() < m_iWareBuffer[i] )
5234
					wareLines.PushBack("27;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;SS_WARE_FILLER;");
5235
 
5236
				// add the ware lines
5237
				bool create = false;
5238
				for ( CListNode<SGameWare> *node = m_lGameWares[i].Front(); node; node = node->next() )
5239
				{
5240
					SGameWare *w = node->Data();
5241
 
5242
					if ( w->iType == WARETYPE_NONE )
5243
						continue;
5244
					else if ( w->iType == WARETYPE_DISABLED )
5245
					{
5246
						create = true;
5247
						wareLines.PushBack(CyString("27;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;") + w->sWareName + "_DISABLED;");
5248
					}
5249
					else if ( w->iType == WARETYPE_DELETED || !w->pWare )
5250
					{
5251
						create = true;
5252
						wareLines.PushBack("27;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;SS_WARE_DELETED;");
5253
					}
5254
					else if ( w->iType == WARETYPE_ADDED )
5255
					{
5256
						create = true;
5257
						w->pWare->iDescID = wareTextID;
5258
						w->iText = wareTextID;
5259
						wareTextID += 10;
5260
						w->iPos = wareLines.Count();
5261
						wareLines.PushBack(CyString("28;0;0;0;0;") + (long)wareLines.Count() + ";" + (long)(w->iText + 3) + ";" + (long)w->pWare->iVolumn + ";" + (long)w->pWare->iPrice + ";1;1;" + (long)w->pWare->iSize + ";" + (long)w->pWare->iPrice + ";" + (long)w->pWare->iNotority + ";0;0;" + w->sWareName.ToUpper() + ";");
5262
					}
5263
				}
5264
 
5265
				if ( create )
5266
				{
5267
					wareLines.PushFront(CyString::Number(version) + ";" + CyString::Number(wareLines.Count()) + ";", NullString);
5268
					CyString strV;
5269
					strV.FromFloat(GetLibraryVersion(), 2);
5270
					wareLines.PushFront(CyString("// Created by SPKInstaller Libraries V") + strV, NullString);
5271
					if ( readFile.WriteFile(&wareLines) )
5272
						this->PackFile(&readFile, CyString("types\\") + wareFile + ".pck");
5273
				}
5274
			}
5275
			readFile.Remove();
5276
		}
5277
	}
5278
}
5279
 
5280
void CPackages::CreateEMPFile(CyString progDir)
5281
{
5282
	// do emp wares
5283
	int maxsize = 0;
5284
	CyString empWares;
5285
 
5286
	if ( m_iGame == GAME_X3TC )
5287
	{
5288
		maxsize = EMP_X3TC;
5289
		empWares = GetX3TCEmp();
5290
	}
5291
	else if ( m_iGame == GAME_X3AP )
5292
	{
5293
		maxsize = EMP_X3AP;
5294
		empWares = GetX3TCEmp();
5295
	}
5296
	else if ( m_iGame == GAME_X3 )
5297
	{
5298
		maxsize = EMP_X3;
5299
		empWares = GetX3Emp();
5300
	}
5301
 
5302
	if ( maxsize )
5303
	{
5304
		int e = ExtractGameFile("types/TWareT.pck", m_sTempDir + "/TWareT.txt");
5305
		if ( e )
5306
		{
5307
			// read the file into memory
5308
			CyStringList wareLines;
5309
			int oldSize = -1;
5310
			int version = -1;
5311
 
5312
			// read first number
5313
			CFileIO readFile((e == -1) ? "TWareT.txt" : m_sTempDir + "/TWareT.txt");
5314
			std::vector<CyString> *lines = readFile.ReadLines();
5315
			if ( lines )
5316
			{
5317
				for ( int i = 0; i < (int)lines->size(); i++ )
5318
				{
5319
					CyString line(lines->at(i));
5320
					line.RemoveFirstSpace();
5321
					line.RemoveChar('\r');
5322
					line.RemoveChar(9);
5323
					if ( line[0] == '/' )
5324
						continue;
5325
 
5326
					if ( oldSize == -1 )
5327
					{
5328
						version = line.GetToken(";", 1, 1).ToInt();
5329
						oldSize = line.GetToken(";", 2, 2).ToInt();
5330
					}
5331
					else
5332
					{
5333
						line.RemoveEndSpace();
5334
						if ( line.Right(1) != ";" )
5335
							line += ";";
5336
						wareLines.PushBack(line);
5337
						if ( wareLines.Count() >= oldSize )
5338
							break;
5339
					}
5340
				}
5341
 
5342
				delete lines;
5343
			}
5344
 
5345
			// now we too add/remove entries to match
5346
			// need filler entries
5347
			while ( wareLines.Count() < maxsize )
5348
				wareLines.PushBack("27;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;SS_WARE_FILLER;");
5349
 
5350
			int empEntries = 0;
5351
			CyString *empStr = empWares.SplitToken("\n", &empEntries);
5352
 
5353
			if ( empEntries && empStr )
5354
			{
5355
				// remove any empty end entries
5356
				while ( empStr[empEntries - 1].Empty() )
5357
					--empEntries;
5358
 
5359
				CyStringList addAfter;
5360
				if ( wareLines.Count() > maxsize )
5361
				{
5362
					// force emp, remove entries to allow them to be added
5363
					if ( m_bForceEMP )
5364
					{
5365
						// more after emp
5366
						if ( wareLines.Count() > (maxsize + empEntries) )
5367
						{
5368
							SStringList *node = wareLines.GetAt(maxsize + empEntries);
5369
							while ( node )
5370
							{
5371
								addAfter.PushBack(node->str);
5372
								node = node->next;
5373
							}
5374
						}
5375
 
5376
						// no remove them all
5377
						wareLines.DeleteFrom(maxsize);
5378
					}
5379
					else if ( m_iGame == GAME_X3TC || m_iGame == GAME_X3AP ) // check if old emp is included, and convert it
5380
					{
5381
						if ( wareLines.Count() > 128 )
5382
						{
5383
							CyString test = wareLines.GetAt(128)->str;
5384
							if ( test.GetToken(";", -2).Compare("SS_WARE_SW_CUSTOM16_1;") )
5385
							{
5386
								// if theres any at the end, remove the last emp entry
5387
								if ( wareLines.Count() > (maxsize + empEntries - 1) )
5388
								{
5389
									SStringList *node = wareLines.GetAt(maxsize + empEntries - 2);
5390
									if ( node )
5391
										node->remove = true;
5392
								}
5393
								wareLines.Insert(128, "0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;128;");
5394
								wareLines.RemoveMarked();
5395
							}
5396
						}
5397
					}
5398
				}
5399
 
5400
				// too many entries, need to remove the first set of EMP wares
5401
				int i = 0;
5402
				if ( wareLines.Count() > maxsize )
5403
					i = wareLines.Count() - maxsize;
5404
 
5405
				for ( ; i < empEntries; i++ )
5406
				{
5407
					CyString str = empStr[i];
5408
					str.RemoveEndSpace();
5409
					str.RemoveChar(9);
5410
					str.RemoveChar('\r');
5411
					if ( str.Empty() )
5412
						continue;
5413
					if ( str.Right(1) != ";" )
5414
						str += ";";
5415
					wareLines.PushBack(str);
5416
				}
5417
 
5418
				for ( SStringList *afterNode = addAfter.Head(); afterNode; afterNode = afterNode->next )
5419
					wareLines.PushBack(afterNode->str);
5420
 
5421
				// finally we write the whole file
5422
				wareLines.PushFront(CyString::Number(version) + ";" + CyString::Number(wareLines.Count()) + ";", NullString);
5423
				CyString strV;
5424
				strV.FromFloat(GetLibraryVersion(), 2);
5425
				wareLines.PushFront(CyString("// Created by SPKInstaller Libraries V") + strV, NullString);
5426
				if ( readFile.WriteFile(&wareLines) )
5427
					this->PackFile(&readFile, "types\\TWareT.pck");
5428
			}
5429
			CLEANSPLIT(empStr, empEntries);
5430
		}
5431
	}
5432
}
5433
 
17 cycrow 5434
CyString parseXmlText(const CyString &str)
5435
{
5436
	CyString newStr(str);
5437
	CyStringList changes;
5438
 
5439
	// find all XML commands, &<command>;
5440
	std::string sStr = str.ToString();
5441
	std::string::size_type pos = sStr.find_first_of("&", 0);
5442
	while ( pos != std::string::npos ) {
5443
		// find the next space and next ;.  If ; comes first, assume its acommand
5444
		std::string::size_type spacePos = sStr.find_first_of(" ", pos);
5445
		std::string::size_type colonPos = sStr.find_first_of(";", pos);
5446
		if ( colonPos != std::string::npos && colonPos < spacePos ) {
5447
			// replace with <::command::> so they the & doesn't get replaced
5448
			std::string repStr = sStr.substr(pos, (colonPos + 1) - pos);
5449
			std::string repWithStr = "<::" + sStr.substr(pos + 1, colonPos - pos - 1) + "::>";
5450
			newStr.FindReplace(repStr, repWithStr);
5451
			changes.PushBack(repStr, repWithStr);
5452
		}
5453
 
5454
		// find the next command
5455
		pos = sStr.find_first_of("&", pos + 1);
5456
	}
5457
 
5458
	// replace the & now
5459
	newStr = newStr.FindReplace("&", "&amp;");
5460
 
5461
	// restore the commands
5462
	for ( SStringList *strNode = changes.Head(); strNode; strNode = strNode->next ) {
5463
		newStr.FindReplace(strNode->data, strNode->str);
5464
	}
5465
 
5466
	return newStr;
5467
}
5468
 
1 cycrow 5469
CyString CPackages::ConvertTextString(CyString text)
5470
{
17 cycrow 5471
	//process any &
5472
	text = parseXmlText(text);
5473
 
5474
	// change special cases
1 cycrow 5475
	text = text.FindReplace("(", "\\(");
5476
	text = text.FindReplace(")", "\\)");
5477
	text = text.FindReplace("[", "{");
5478
	text = text.FindReplace("]", "}");
5479
	text = text.FindReplace(">", "&gt;");
5480
	text = text.FindReplace("<", "&lt;");
5481
	return text;
5482
}
5483
 
5484
void CPackages::CreatePluginManagerText()
5485
{
5486
	int gameNumber = 0;
5487
	if ( m_iGame == GAME_X3 )
5488
		gameNumber = 30;
5489
	else if ( m_iGame == GAME_X3TC )
5490
		gameNumber = 35;
5491
	else if ( m_iGame == GAME_X3AP )
5492
		gameNumber = 38;
5493
 
5494
	int lang = m_iLanguage;
5495
	if ( !lang || lang < 0 )
5496
		lang = 44;
5497
 
5498
	CDirIO Dir(m_sCurrentDir);
5499
	if ( !Dir.Exists("t") )
5500
		Dir.Create("t");
5501
 
5502
	m_iLastUpdated = (int)time(NULL);
5503
 
5504
	CyString filename = SPK::FormatTextName(PMTEXTFILE, lang, (m_iGameFlags & EXEFLAG_TCTEXT));
5505
	CFileIO textFile(m_sCurrentDir + "/t/" + filename + ".xml");
5506
 
5507
	std::vector<CyString> writeData;
5508
 
5509
	CLinkList<SGameWare> lWares;
5510
	CLinkList<SGameShip> lShips;
5511
	for ( int i = 0; i < WAREBUFFERS; i++ )
5512
	{
5513
		for ( CListNode<SGameWare> *node = m_lGameWares[i].Front(); node; node = node->next() )
5514
		{
5515
			SGameWare *w = node->Data();
5516
			if ( w->iType == WARETYPE_NONE )
5517
				continue;
5518
			lWares.push_back(w);
5519
		}
5520
	}
5521
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
5522
	{
5523
		if ( node->Data()->iType == WARETYPE_NONE )
5524
			continue;
5525
		lShips.push_back(node->Data());
5526
	}
5527
 
5528
	writeData.push_back(CyString("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"));
5529
	writeData.push_back(CyString("<language id=\"") + CyString::Number(lang) + "\">");
5530
 
5531
	if ( !gameNumber )
5532
		writeData.push_back(CyString("	<page id=\"") + CyString::Number(PMTEXTFILE) + "\" title=\"Plugin Manager Text File\" descr=\"Contains text used for the plugin manager, packages, settings, wares, ship, etc\">");
5533
	else
5534
		writeData.push_back(CyString("	<page id=\"") + (long)gameNumber + CyString::Number(PMTEXTFILE).PadNumber(4) + "\" title=\"Plugin Manager Text File\" descr=\"Contains text used for the plugin manager, packages, settings, wares, ship, etc\">");
5535
 
5536
	// write the heading
5537
	int start = 10000;
5538
	writeData.push_back(CyString("		<t id=\"1\">") + CyString::Number(m_iLastUpdated) + "</t>");
5539
	writeData.push_back(CyString("		<t id=\"2\">") + CyString::Number(this->CountPackages(TYPE_SPK, true)) + "</t>");
5540
	writeData.push_back(CyString("		<t id=\"3\">") + CyString::Number(start) + "</t>");
5541
	writeData.push_back(CyString("		<t id=\"4\">") + CyString::Number(lWares.size()) + "</t>");
5542
	writeData.push_back(CyString("		<t id=\"6\">") + CyString::Number(lShips.size()) + "</t>");
5543
 
5544
	// write some generic texts
5545
	writeData.push_back(CyString("		<t id=\"110\">") + (long)m_iLanguage + "</t>");
5546
	writeData.push_back(CyString("		<t id=\"109\">Plugin Manager: \\033GPoll Gui Data\\033X</t>"));
5547
	writeData.push_back(CyString("		<t id=\"107\">Plugin Manager: \\033GExport Game Data\\033X </t>"));
5548
	writeData.push_back(CyString("		<t id=\"100\">\\n</t>"));
5549
	writeData.push_back(CyString("		<t id=\"101\">\\033B</t>"));
5550
	writeData.push_back(CyString("		<t id=\"102\">\\033G</t>"));
5551
	writeData.push_back(CyString("		<t id=\"103\">\\033B</t>"));
5552
	writeData.push_back(CyString("		<t id=\"104\">\\033X</t>"));
5553
	writeData.push_back(CyString("		<t id=\"105\">\\033Y</t>"));
5554
	writeData.push_back(CyString("		<t id=\"106\">\\033C</t>"));
5555
	writeData.push_back(CyString("		<t id=\"108\">\\033</t>"));
5556
	// now write each package
5557
	int settingStart = 100000;
5558
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
5559
	{
5560
		CBaseFile *p = node->Data();
5561
		if ( !p->IsEnabled() )
5562
			continue;
5563
 
5564
		if ( p->GetType() != TYPE_SPK )
5565
			continue;
5566
 
5567
		CSpkFile *spk = (CSpkFile *)p;
5568
 
5569
		// count text files
5570
		CyString textEntries;
5571
		int textCount = 0;
5572
		C_File *f = p->GetFirstFile(FILETYPE_TEXT);
5573
		while ( f )
5574
		{
5575
			CyString sLang;
5576
			CyString id;
5577
			if ( m_iGameFlags & EXEFLAG_TCTEXT )
5578
			{
5579
				id = f->GetBaseName().GetToken("-", 1, 1);
5580
				sLang = f->GetBaseName().GetToken("-", 2, 2);
5581
				if ( sLang.Empty() )
5582
					sLang = "NULL";
5583
				else
5584
					sLang = sLang.Erase(0, 1);  // remove the "L"
5585
			}
5586
			else
5587
			{
5588
				sLang = f->GetBaseName().Left((int)f->GetBaseName().Length() - 4).PadNumber(3);
5589
				id = f->GetBaseName().Mid(((int)f->GetBaseName().Length() - 4) + 1, 4);
5590
			}
5591
 
5592
			if ( sLang != "NULL" )
5593
			{
5594
				if ( sLang.ToInt() == lang )
5595
				{
5596
					++textCount;
5597
					if ( !textEntries.Empty() )
5598
						textEntries += " ";
5599
					textEntries += id;
5600
				}
5601
			}
5602
			f = p->GetNextFile(f);
5603
		}
5604
 
5605
		CyString sTextCount((long)textCount);
50 cycrow 5606
		writeData.push_back(CyString("		<t id=\"") + (long)start + "\">" + this->ConvertTextString(p->name()) + "</t>");
5607
		writeData.push_back(CyString("		<t id=\"") + (long)(start + 1) + "\">" + this->ConvertTextString(p->author()) + "</t>");
5608
		writeData.push_back(CyString("		<t id=\"") + (long)(start + 2) + "\">" + this->ConvertTextString(p->version()) + "</t>");
1 cycrow 5609
		writeData.push_back(CyString("		<t id=\"") + (long)(start + 3) + "\">" + this->ConvertTextString(p->GetLanguageName(lang)) + "</t>");
5610
 
14 cycrow 5611
		CLinkList<SSettingType> *settings = spk->GetSettingsList();
1 cycrow 5612
		if ( settings && settings->size() )
5613
		{
5614
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 4) + "\">" + (long)settings->size() + "</t>");
5615
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 5) + "\">" + (long)settingStart + "</t>");
5616
 
14 cycrow 5617
			for ( CListNode<SSettingType> *sNode = settings->Front(); sNode; sNode = sNode->next() )
1 cycrow 5618
			{
14 cycrow 5619
				SSettingType *st = sNode->Data();
1 cycrow 5620
				writeData.push_back(CyString("		<t id=\"") + (long)(settingStart++) + "\">" + st->sKey + "</t>");
5621
				writeData.push_back(CyString("		<t id=\"") + (long)(settingStart++) + "\">" + spk->GetSetting(st) + "</t>");
5622
			}
5623
		}
5624
		else
5625
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 4) + "\">0</t>");
5626
 
5627
		writeData.push_back(CyString("		<t id=\"") + (long)(start + 6) + "\">" + sTextCount + "</t>");
5628
		if ( textCount )
5629
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 7) + "\">" + textEntries + "</t>");
5630
 
5631
		start += 10;
5632
	}
5633
 
5634
	// write ware names
5635
	if ( lWares.size() )
5636
	{
5637
		writeData.push_back(CyString("		<t id=\"5\">") + CyString::Number(start) + "</t>");
5638
		for ( CListNode<SGameWare> *node = lWares.Front(); node; node = node->next() )
5639
		{
5640
			SGameWare *w = node->Data();
5641
			if ( w->pWare && w->iType == WARETYPE_ADDED )
17 cycrow 5642
				writeData.push_back(CyString("		<t id=\"") + (long)start + "\">" + this->ConvertTextString(w->sWareName) + "</t>");
1 cycrow 5643
			else
5644
				writeData.push_back(CyString("		<t id=\"") + (long)start + "\">-1</t>");
5645
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 1) + "\">" + CyString((char)w->cType) + "</t>");
5646
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 2) + "\">" + (long)w->iPos + "</t>");
5647
			start += 10;
5648
		}
5649
	}
5650
 
5651
	if ( lShips.size() )
5652
	{
5653
		writeData.push_back(CyString("		<t id=\"7\">") + CyString::Number(start) + "</t>");
5654
		for ( CListNode<SGameShip> *node = lShips.Front(); node; node = node->next() )
5655
		{
5656
			SGameShip *gs = node->Data();
5657
			if ( gs->iType == WARETYPE_NONE )
5658
				continue;
5659
			if ( gs->pPackage && gs->iType == WARETYPE_ADDED )
5660
				writeData.push_back(CyString("		<t id=\"") + (long)start + "\">" + gs->sShipID + "</t>");
5661
			else
5662
				writeData.push_back(CyString("		<t id=\"") + (long)start + "\">-1</t>");
5663
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 1) + "\">" + (long)gs->iPos + "</t>");
5664
			writeData.push_back(CyString("		<t id=\"") + (long)(start + 2) + "\">Ship</t>");
5665
 
5666
			// write shipyard info
5667
			if ( gs->pPackage )
5668
			{
5669
				int doStart = start + 5;
5670
				for ( int i = SHIPYARD_ARGON; i <= SHIPYARD_MAX; i *= 2 )
5671
				{
5672
					writeData.push_back(CyString("		<t id=\"") + (long)(doStart) + "\">" + ((gs->pPackage->IsShipyard(i)) ? CyString::Number(1) : CyString::Number(0)) + "</t>");
5673
					++doStart;
5674
				}
5675
			}
5676
 
5677
			start += 20;
5678
		}
5679
	}
5680
 
5681
	writeData.push_back(CyString("	</page>"));
5682
 
5683
	// wares
5684
	if ( m_iGame == GAME_X3AP || m_iGame == GAME_X3TC || m_iGame == GAME_X3 || lWares.size() || lShips.size() )
5685
	{
5686
		if ( !gameNumber )
5687
			writeData.push_back(CyString("  <page id=\"17\" title=\"Plugin Manager Objects\">"));
5688
		else
5689
			writeData.push_back(CyString("  <page id=\"") + (long)gameNumber + "0017\" title=\"Plugin Manager Objects\">");
5690
 
5691
		writeData.push_back(CyString("		<t id=\"") + (long)(SHIPSTARTTEXT - 1) + "\">ZZ_BLANKSHIP</t>");
5692
		// do emp
5693
		if ( m_iGame == GAME_X3TC || m_iGame == GAME_X3 || m_iGame == GAME_X3AP )
5694
			writeData.push_back(GetEMPText());
5695
 
5696
		// object names
5697
		for ( CListNode<SGameWare> *node = lWares.Front(); node; node = node->next() )
5698
		{
5699
			SGameWare *w = node->Data();
5700
			if ( !w->pWare || w->iType != WARETYPE_ADDED )
5701
				continue;
5702
 
5703
			// find the correct text for the language
5704
			CyString name = CSpkFile::GetWareText(w->pWare, m_iLanguage);
5705
			CyString desc = CSpkFile::GetWareDesc(w->pWare, m_iLanguage);
5706
			if ( !name.Empty() )
17 cycrow 5707
				writeData.push_back(CyString("		<t id=\"") + (long)(w->iText + 3) + "\">" + this->ConvertTextString(name) + "</t>");
1 cycrow 5708
			if ( !desc.Empty() )
17 cycrow 5709
				writeData.push_back(CyString("		<t id=\"") + (long)(w->iText + 4) + "\">" + this->ConvertTextString(desc) + "</t>");
1 cycrow 5710
		}
5711
		for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
5712
		{
5713
			SGameShip *s = node->Data();
5714
			if ( !s->pPackage || s->iType != WARETYPE_ADDED )
5715
				continue;
5716
			if ( s->pPackage->GetOriginalDescription() )
5717
				continue;
5718
 
5719
			CyString name = s->pPackage->GetTextName(m_iLanguage);
5720
			CyString desc = s->pPackage->GetTextDescription(m_iLanguage);
5721
			if ( !name.Empty() )
17 cycrow 5722
				writeData.push_back(CyString("		<t id=\"") + (long)s->iText + "\">" + this->ConvertTextString(name) + "</t>");
1 cycrow 5723
			if ( !desc.Empty() )
17 cycrow 5724
				writeData.push_back(CyString("		<t id=\"") + (long)(s->iText + 1) + "\">" + this->ConvertTextString(desc) + "</t>");
1 cycrow 5725
		}
5726
		writeData.push_back(CyString("  </page>"));
5727
	}
5728
	writeData.push_back(CyString("</language>"));
5729
	textFile.WriteFileUTF(&writeData);
5730
 
5731
	size_t fileSize;
5732
	char *fileData = CFileIO(textFile.GetFullFilename()).ReadToData(&fileSize);
5733
 
5734
	if ( fileData && fileSize)
5735
	{
5736
		size_t newFileSize;
5737
		unsigned char *pckData = PCKData((unsigned char *)fileData, fileSize, &newFileSize, true);
5738
		if ( pckData )
5739
		{
5740
			CFileIO pckFile(m_sCurrentDir + "/t/" + filename + ".pck");
5741
			pckFile.WriteData((char *)pckData, newFileSize);
5742
			this->AddCreatedFile(pckFile.GetFullFilename());
5743
		}
5744
	}
5745
	textFile.Remove();
5746
}
5747
 
5748
bool CPackages::IsCurrentDir(CyString dir)
5749
{
5750
	if ( dir.Compare(m_sCurrentDir) )
5751
		return true;
5752
 
5753
	CyString checkDir = m_sCurrentDir;
5754
	checkDir = checkDir.FindReplace("/", "\\");
5755
	if ( checkDir.Compare(dir) )
5756
		return true;
5757
 
5758
	checkDir = checkDir.FindReplace("\\", "/");
5759
	if ( checkDir.Compare(dir) )
5760
		return true;
5761
 
5762
	return false;
5763
}
5764
 
5765
void CPackages::BackupSaves(bool vanilla)
5766
{
5767
	// copy any saves into the vanilla directory
5768
	CyString dir = (vanilla) ? "Vanilla" : "Modified";
5769
 
5770
	// make sure the directory exists
5771
	CDirIO saveDir(this->GetSaveDirectory());
5772
	CDirIO gameSaveDir(m_sCurrentDir);
5773
	if ( !gameSaveDir.Exists("PluginManager") )
5774
		gameSaveDir.Create("PluginManager");
5775
	gameSaveDir.cd("PluginManager");
5776
	if ( !gameSaveDir.Exists("Saves") )
5777
		gameSaveDir.Create("Saves");
5778
	gameSaveDir.cd("Saves");
5779
	if ( !gameSaveDir.Exists(dir) )
5780
		gameSaveDir.Create(dir);
5781
	gameSaveDir.cd(dir);
5782
 
5783
	// backup the saves
5784
	CyStringList *dirs = saveDir.DirList();
5785
	if ( dirs )
5786
	{
5787
		for ( SStringList *node = dirs->Head(); node; node = node->next )
5788
		{
5789
			CFileIO File(saveDir.File(node->str));
5790
			if ( !File.CheckFileExtension("sav") )
5791
				continue;
5792
			// remove the file if already exists
5793
			if ( gameSaveDir.Exists(node->str) )
5794
				CFileIO(gameSaveDir.File(node->str)).Remove();
5795
 
5796
			// copy the file into the games save dir for backup
5797
			File.Copy(gameSaveDir.File(File.GetFilename()), true);
5798
		}
5799
 
5800
		delete dirs;
5801
	}
5802
 
5803
}
5804
 
5805
void CPackages::RestoreSaves(bool vanilla)
5806
{
5807
	// get dir to restore from
5808
	CyString dir = (vanilla) ? "Vanilla" : "Modified";
5809
	CDirIO restoreDir(m_sCurrentDir + "/PluginManager/Saves/" + dir);
5810
	CDirIO toDir(this->GetSaveDirectory());
5811
 
5812
	CyStringList *dirs = restoreDir.DirList();
5813
	if ( dirs )
5814
	{
5815
		for ( SStringList *node = dirs->Head(); node; node = node->next )
5816
		{
5817
			CFileIO File(restoreDir.File(node->str));
5818
			// remove the file if already exists
5819
			if ( toDir.Exists(node->str) )
5820
				CFileIO(toDir.File(node->str)).Remove();
5821
 
5822
			// move file over
5823
			File.Copy(toDir.File(node->str), true);
5824
		}
5825
 
5826
		delete dirs;
5827
	}
5828
 
5829
}
5830
 
5831
bool CPackages::RemoveCurrentDirectory()
5832
{
5833
	if ( !m_bLoaded )
5834
		return false;
5835
 
5836
	// remove all package files
5837
	this->RemoveAllPackages();
5838
 
5839
	// remove all plugin manager files
5840
	this->RemoveCreatedFiles();
5841
 
5842
	// restore any vanilla saves
5843
	/*
5844
	if ( !m_bVanilla )
5845
	{
5846
		this->BackupSaves();
5847
		m_bVanilla = true;
5848
		this->RestoreSaves();
5849
	}*/
5850
 
5851
	this->Reset();
5852
	m_bLoaded = false;
5853
 
5854
	// clear the plugin manager directory
5855
	CDirIO Dir(m_sCurrentDir);
5856
	Dir.RemoveDir("PluginManager", true, true, 0);
5857
	Dir.RemoveDir("dds", false, true, 0);
5858
	Dir.RemoveDir("objects", false, true, 0);
5859
	Dir.RemoveDir("types", false, true, 0);
5860
	Dir.RemoveDir("textures", false, true, 0);
5861
 
5862
	// remove the plugin manager mod files
5863
	if ( Dir.Exists("mods/PluginManager.cat") )
5864
		CFileIO(Dir.File("mods/PluginManager.cat")).Remove();
5865
	if ( Dir.Exists("mods/PluginManager.dat") )
5866
		CFileIO(Dir.File("mods/PluginManager.dat")).Remove();
5867
 
5868
	return true;
5869
}
5870
 
5871
void CPackages::CreateDummies()
5872
{
5873
	// first check we have any ships
5874
	if ( m_lGameShips.empty() || !this->CountPackages(TYPE_XSP, true) )
5875
		return;
5876
 
5877
	CLinkList<SDummyEntry> dummyList;
5878
 
5879
	// now extract the existing dummies
5880
	int e = ExtractGameFile("types/Dummies.pck", m_sTempDir + "/Dummies.txt");
5881
	if ( e )
5882
	{
5883
		// read the dummies
5884
		CFileIO File;
5885
		if ( File.Open((e == -1) ? "Dummies.txt" : m_sTempDir + "/Dummies.txt") )
5886
		{
5887
			std::vector<CyString> *lines = File.ReadLines();
5888
			if ( lines )
5889
			{
5890
				int insection = 0;
5891
				SDummyEntry *currentSection = NULL;
5892
				for ( int j = 0; j < (int)lines->size(); j++ )
5893
				{
5894
					CyString line(lines->at(j));
5895
					line.RemoveChar(9);
5896
					line.RemoveChar('\r');
5897
					line.RemoveFirstSpace();
5898
					if ( line.Empty() )
5899
						continue;
5900
					if ( line[0] == '/' )
5901
						continue;
5902
 
5903
					// read the section, first entry is section, second is size
5904
					while ( !line.Empty() )
5905
					{
5906
						if ( !insection )
5907
						{
5908
							CyString section = line.GetToken(";", 1, 1);
5909
							insection = line.GetToken(";", 2, 2).ToInt();
5910
 
5911
							// search for the sections
5912
							currentSection = NULL;
5913
							for ( CListNode<SDummyEntry> *node = dummyList.Front(); node; node = node->next() )
5914
							{
5915
								SDummyEntry *d = node->Data();
5916
								if ( d->sSection.Compare(section) )
5917
								{
5918
									currentSection = node->Data();
5919
									break;
5920
								}
5921
							}
5922
 
5923
							if ( !currentSection )
5924
							{
5925
								currentSection = new struct SDummyEntry;
5926
								currentSection->sSection = section;
5927
								dummyList.push_back(currentSection);
5928
							}
5929
 
5930
							// we have some more ?
5931
							line = line.DelToken(";", 1, 2);
5932
						}
5933
						else
5934
						{
5935
							--insection;
5936
							// check the last entry for number of states
5937
							if ( currentSection->sSection.Compare("SDTYPE_GUN") )
5938
							{
5939
								int states = line.GetToken(";", 3, 3).ToInt();
5940
								int parts = line.GetToken(";", 4 + (states * 2), 4 + (states * 2)).ToInt();
5941
								CyString data = line.GetToken(";", 1, 4 + (states * 2) + (parts * 2)) + ";";
5942
								currentSection->lEntries.PushBack(data);
5943
 
5944
								// remove done
5945
								line = line.DelToken(";", 1, 4 + (states * 2) + (parts * 2));
5946
							}
5947
							else
5948
							{
5949
								int states = line.GetToken(";", 3, 3).ToInt();
5950
								CyString data = line.GetToken(";", 1, 3 + (states * 2)) + ";";
5951
								currentSection->lEntries.PushBack(data);
5952
 
5953
								// remove done
5954
								line = line.DelToken(";", 1, 3 + (states * 2));
5955
							}
5956
						}
5957
					}
5958
				}
5959
 
5960
				delete lines;
5961
			}
5962
 
5963
			File.Remove();
5964
		}
5965
 
5966
		// add the new entries for the ships
5967
		for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
5968
		{
5969
			SGameShip *s = node->Data();
5970
			if ( s->iType != WARETYPE_ADDED || !s->pPackage )
5971
				continue;
5972
 
5973
			// no dummies to add?
5974
			if ( !s->pPackage->AnyDummies() )
5975
				continue;
5976
 
5977
			// add each dummy to list
5978
			for ( CListNode<SDummy> *dNode = s->pPackage->GetDummies()->Front(); dNode; dNode = dNode->next() )
5979
			{
5980
				SDummy *dummy = dNode->Data();
5981
				SDummyEntry *found = NULL;
5982
				for ( CListNode<SDummyEntry> *eNode = dummyList.Front(); eNode; eNode = eNode->next() )
5983
				{
39 cycrow 5984
					if ( eNode->Data()->sSection.Compare(CyString(dummy->sSection)) )
1 cycrow 5985
					{
5986
						found = eNode->Data();
5987
						break;
5988
					}
5989
				}
5990
				if ( !found )
5991
				{
5992
					found = new SDummyEntry;
5993
					found->sSection = dummy->sSection;
5994
					dummyList.push_back(found);
5995
				}
5996
				// check if its already on the list
5997
				else
5998
				{
5999
					bool f = false;
6000
					for ( SStringList *strNode = found->lEntries.Head(); strNode; strNode = strNode->next )
6001
					{
39 cycrow 6002
						if ( strNode->str.GetToken(";", 1, 1).Compare(CyString(dummy->sData.token(";", 1))) )
1 cycrow 6003
						{
6004
							f = true;
6005
							break;
6006
						}
6007
					}
6008
 
6009
					if ( f )
6010
						continue;
6011
				}
6012
 
39 cycrow 6013
				found->lEntries.PushBack(CyString(dummy->sData));
1 cycrow 6014
			}
6015
		}
6016
 
6017
		// finally, write the file
6018
		std::vector<CyString> lines;
6019
		lines.push_back(CyString("// Dummies file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2));
6020
		for ( SDummyEntry *dummy = dummyList.First(); dummy; dummy = dummyList.Next() )
6021
		{
6022
			lines.push_back("");
6023
			lines.push_back(CyString("// Section: ") + dummy->sSection + " Entries: " + (long)dummy->lEntries.Count());
6024
			lines.push_back(dummy->sSection + ";" + CyString::Number(dummy->lEntries.Count()) + ";");
6025
			for ( SStringList *str = dummy->lEntries.Head(); str; str = str->next )
6026
			{
6027
				CyString strLine = str->str;
6028
				strLine.RemoveChar(9);
6029
				strLine.RemoveChar('\r');
6030
				strLine.RemoveEndSpace();
6031
				strLine.RemoveFirstSpace();
6032
				strLine = strLine.FindReplace("<::PiPe::>", "|");
6033
				if ( strLine.Right(1) != ";" )
6034
					strLine += ";";
6035
				lines.push_back(strLine);
6036
			}
6037
		}
6038
		lines.push_back("");
6039
 
6040
		// write the file to disk
6041
		CFileIO WriteFile(m_sTempDir + "/dummies.txt");
6042
		if ( WriteFile.WriteFile(&lines) )
6043
		{
6044
			this->PackFile(&WriteFile, "types\\dummies.pck");
6045
			WriteFile.Remove();
6046
		}
6047
	}
6048
}
6049
 
6050
void CPackages::CreateCutData()
6051
{
6052
	// first check we have any ships
6053
	if ( m_lGameShips.empty() || !this->CountPackages(TYPE_XSP, true) )
6054
		return;
6055
 
6056
	bool found = false;
6057
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6058
	{
6059
		SGameShip *s = node->Data();
6060
		if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6061
			continue;
6062
 
6063
		// no dummies to add?
6064
		if ( !s->pPackage->AnyCutData() )
6065
			continue;
6066
		found = true;
6067
		break;
6068
	}
6069
 
6070
	if ( !found )
6071
		return;
6072
 
6073
	CyStringList cutList;
6074
	int e = ExtractGameFile("types/CutData.pck", m_sTempDir + "/CutData.txt");
6075
	if ( e )
6076
	{
6077
		CFileIO File;
6078
		if ( File.Open((e == -1) ? "CutData.txt" : m_sTempDir + "/CutData.txt") )
6079
		{
6080
			std::vector<CyString> *lines = File.ReadLines();
6081
			if ( lines )
6082
			{
6083
				int entries = -1;
6084
				for ( int j = 0; j < (int)lines->size(); j++ )
6085
				{
6086
					CyString line(lines->at(j));
6087
					line.RemoveChar(9);
6088
					line.RemoveChar('\r');
6089
					line.RemoveChar(' ');
6090
					if ( line.Empty() || line[0] == '/' )
6091
						continue;
6092
					if ( entries == -1 )
6093
						entries = line.GetToken(";", 1, 1).ToInt();
6094
					else
6095
					{
6096
						if ( line.Right(1) != ";" )
6097
							line += ";";
6098
						cutList.PushBack(line);
6099
						if ( cutList.Count() == entries )
6100
							break;
6101
					}
6102
				}
6103
 
6104
				delete lines;
6105
			}
6106
 
6107
			File.Remove();
6108
		}
6109
	}
6110
 
6111
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6112
	{
6113
		SGameShip *s = node->Data();
6114
		if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6115
			continue;
6116
 
6117
		// no dummies to add?
6118
		if ( !s->pPackage->AnyCutData() )
6119
			continue;
6120
 
6121
		// add each dummy to list
6122
		for ( SStringList *strNode = s->pPackage->GetCutData()->Head(); strNode; strNode = strNode->next )
6123
		{
6124
			CyString str = strNode->str;
6125
			str.RemoveChar(' ');
6126
			if ( str.Right(1) != ";" )
6127
				str += ";";
6128
			cutList.PushBack(str, true);
6129
		}
6130
	}
6131
 
6132
	cutList.PushFront(CyString::Number(cutList.Count()) + ";");
6133
	cutList.PushFront("/cut id;filename (leave blank to use id)");
6134
	cutList.PushFront(CyString("// Cut Data file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2));
6135
 
6136
	// write the file to disk
6137
	CFileIO WriteFile(m_sTempDir + "/CutData.txt");
6138
	if ( WriteFile.WriteFile(&cutList) )
6139
	{
6140
		this->PackFile(&WriteFile, "types\\CutData.pck");
6141
		WriteFile.Remove();
6142
	}
6143
}
6144
 
6145
void CPackages::CreateAnimations()
6146
{
6147
	// first check we have any ships
6148
	if ( m_lGameShips.empty() || !this->CountPackages(TYPE_XSP, true) )
6149
		return;
6150
 
6151
	bool found = false;
6152
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6153
	{
6154
		SGameShip *s = node->Data();
6155
		if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6156
			continue;
6157
 
6158
		// no dummies to add?
6159
		if ( !s->pPackage->AnyAnimations() )
6160
			continue;
6161
		found = true;
6162
		break;
6163
	}
6164
 
6165
	if ( !found )
6166
		return;
6167
 
6168
	CyStringList aniList;
6169
	int e = ExtractGameFile("types/Animations.pck", m_sTempDir + "/Animations.txt");
6170
	if ( e )
6171
	{
6172
		CFileIO File;
6173
		if ( File.Open((e == -1) ? "Animations.txt" : m_sTempDir + "/Animations.txt") )
6174
		{
6175
			std::vector<CyString> *lines = File.ReadLines();
6176
			if ( lines )
6177
			{
6178
				for ( int j = 0; j < (int)lines->size(); j++ )
6179
				{
6180
					CyString line(lines->at(j));
6181
					aniList.PushBack(line);
6182
				}
6183
 
6184
				delete lines;
6185
			}
6186
 
6187
			File.Remove();
6188
		}
6189
	}
6190
 
6191
	CyStringList parsedAniList;
6192
	CXspFile::ReadAnimations(&aniList, &parsedAniList, 0);
6193
 
6194
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6195
	{
6196
		SGameShip *s = node->Data();
6197
		if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6198
			continue;
6199
 
6200
		// no dummies to add?
6201
		if ( !s->pPackage->AnyAnimations() )
6202
			continue;
6203
 
6204
		// add each dummy to list
6205
		for ( SStringList *strNode = s->pPackage->GetAnimations()->Head(); strNode; strNode = strNode->next )
6206
			parsedAniList.PushBack(strNode->str);
6207
	}
6208
 
6209
	// format the list with added spaces
6210
	CyStringList formatedAniList;
6211
	int lineCount = -1;
6212
	for ( SStringList *strNode = parsedAniList.Head(); strNode; strNode = strNode->next )
6213
	{
6214
		// format the comment to match the line number
6215
		lineCount++;
6216
		CyString oldComment = strNode->str.GetToken("//", 2);
6217
		CyString comment = CyString("//") + CyString::Number(lineCount);
6218
		if ( !oldComment.Empty() )
6219
		{
6220
			comment += " ";
6221
			oldComment.RemoveFirstSpace();
6222
			if ( oldComment.GetToken(" ", 1, 1).IsNumber() )
6223
				comment += oldComment.GetToken(" ", 2);
6224
			else
6225
				comment += oldComment;
6226
		}
6227
		CyString line = strNode->str.GetToken("//", 1, 1);
6228
 
6229
		// split into seperate lines
6230
		CyString first = line.GetToken(";", 1, 1);
6231
		if ( first.Compare("TAT_TAGSINGLESTEP") )
6232
		{
6233
			formatedAniList.PushBack(line.GetToken(";", 1, 5) + ";");
6234
			int max;
6235
			CyString *sLines = line.GetToken(";", 6).SplitToken(";", &max);
6236
			if ( max && sLines )
6237
			{
6238
				if ( sLines[max - 1].Empty() )
6239
					--max; // remove the last ";"
6240
 
6241
				for ( int i = 0; i < max; i++ )
6242
				{
6243
					CyString l = CyString("\t") + sLines[i] + ";";
6244
					if ( i == (max - 1) )
6245
						formatedAniList.PushBack(l + comment);
6246
					else
6247
						formatedAniList.PushBack(l);
6248
				}
6249
			}
6250
			CLEANSPLIT(sLines, max);
6251
		}
6252
		else if ( (first.Compare("TAT_TAGONESHOT") || first.Compare("TAT_TAGLOOP")) && (line.IsIn("TATF_COORDS")) )
6253
		{
6254
			formatedAniList.PushBack(line.GetToken(";", 1, 5) + ";");
6255
			int max;
6256
			CyString *sLines = line.GetToken(";", 6).SplitToken(";", &max);
6257
			if ( max && sLines )
6258
			{
6259
				if ( sLines[max - 1].Empty() )
6260
					--max; // remove the last ";"
6261
 
6262
				CyString prevLine;
6263
				for ( int i = 0; i < max; i++ )
6264
				{
6265
					CyString l = sLines[i] + ";";
6266
					if ( l.IsIn("TATF_COORDS") && !prevLine.Empty() )
6267
					{
6268
						formatedAniList.PushBack(CyString("\t") + prevLine);
6269
						prevLine = "";
6270
					}
6271
					prevLine += l;
6272
				}
6273
 
6274
				if ( !prevLine.Empty() )
6275
					formatedAniList.PushBack(CyString("\t") + prevLine + comment);
6276
 
6277
			}
6278
			CLEANSPLIT(sLines, max);
6279
		}
6280
		else
6281
			formatedAniList.PushBack(line + comment);
6282
	}
6283
 
6284
	formatedAniList.PushFront(CyString::Number(parsedAniList.Count()) + ";");
6285
	formatedAniList.PushFront(CyString("// Animations, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2));
6286
 
6287
	// write the file to disk
6288
	CFileIO WriteFile(m_sTempDir + "/Animations.txt");
6289
	if ( WriteFile.WriteFile(&formatedAniList) )
6290
	{
6291
		this->PackFile(&WriteFile, "types\\Animations.pck");
6292
		WriteFile.Remove();
6293
	}
6294
}
6295
 
6296
void CPackages::CreateBodies()
6297
{
6298
	// first check we have any ships
6299
	if ( m_lGameShips.empty() || !this->CountPackages(TYPE_XSP, true) )
6300
		return;
6301
 
6302
	bool found = false;
6303
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6304
	{
6305
		SGameShip *s = node->Data();
6306
		if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6307
			continue;
6308
 
6309
		// no dummies to add?
6310
		if ( !s->pPackage->AnyBodies() )
6311
			continue;
6312
		found = true;
6313
		break;
6314
	}
6315
 
6316
	if ( !found )
6317
		return;
6318
 
6319
	// lets read our current bodies file
6320
	CLinkList<SBodies> bodiesList;
6321
	SBodies *currentSection = NULL;
6322
	int e = ExtractGameFile("types/Bodies.pck", m_sTempDir + "/Bodies.txt");
6323
	if ( e )
6324
	{
6325
		CFileIO File;
6326
		if ( File.Open((e == -1) ? "Bodies.txt" : m_sTempDir + "/Bodies.txt") )
6327
		{
6328
			std::vector<CyString> *lines = File.ReadLines();
6329
			if ( lines )
6330
			{
6331
				int entries = 0;
6332
				for ( int j = 0; j < (int)lines->size(); j++ )
6333
				{
6334
					CyString line(lines->at(j));
6335
					line.RemoveChar(' ');
6336
					line.RemoveChar(9);
6337
					if ( line.Empty() || line[0] == '/' )
6338
						continue;
6339
					if ( entries <= 0 )
6340
					{
6341
						entries = line.GetToken(";", 2, 2).ToInt();
6342
						currentSection = new SBodies;
6343
						currentSection->sSection = line.GetToken(";", 1, 1);
6344
						bodiesList.push_back(currentSection);
6345
					}
6346
					else if ( currentSection )
6347
					{
6348
						int num;
6349
						CyString *strs = line.SplitToken(";", &num);
6350
						if ( num && strs )
6351
						{
6352
							for ( int i = 0; i < num; i++ )
6353
							{
6354
								if ( strs[i].Empty() )
6355
									continue;
6356
								currentSection->lEntries.PushBack(strs[i] + ";", true);
6357
								--entries;
6358
							}
6359
						}
6360
						CLEANSPLIT(strs, num);
6361
					}
6362
				}
6363
 
6364
				delete lines;
6365
			}
6366
			File.Remove();
6367
		}
6368
	}
6369
 
6370
	// lets now add any new entries
6371
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6372
	{
6373
		SGameShip *s = node->Data();
6374
		if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6375
			continue;
6376
 
6377
		// no dummies to add?
6378
		if ( !s->pPackage->AnyBodies() )
6379
			continue;
6380
 
6381
		// add each dummy to list
6382
		for ( SStringList *strNode = s->pPackage->GetBodies()->Head(); strNode; strNode = strNode->next )
6383
		{
6384
			CyString section = strNode->str.GetToken(";", 1, 1);
6385
			CyString body = strNode->str.GetToken(";", 2).Remove(' ');
6386
			if ( body.Right(1) != ";" )
6387
				body += ";";
6388
 
6389
			// find the section to add into
6390
			SBodies *foundSection = NULL;
6391
			for ( CListNode<SBodies> *checkBody = bodiesList.Front(); checkBody; checkBody = checkBody->next() )
6392
			{
6393
				if ( checkBody->Data()->sSection.Compare(section) )
6394
				{
6395
					foundSection = checkBody->Data();
6396
					break;
6397
				}
6398
			}
6399
 
6400
			if ( !foundSection )
6401
			{
6402
				foundSection = new SBodies;
6403
				foundSection->sSection = section;
6404
				bodiesList.push_back(foundSection);
6405
			}
6406
			foundSection->lEntries.PushBack(body, true);
6407
		}
6408
	}
6409
 
6410
	// now write the file
6411
	CyStringList writeList;
6412
	// the header first
6413
	writeList.PushBack(CyString("// Bodies file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2));
6414
	writeList.PushBack("//body type;num bodies;");
6415
	writeList.PushBack("//[body id/name]");
6416
 
6417
	// now our sections
6418
	for ( SBodies *bSection = bodiesList.First(); bSection; bSection = bodiesList.Next() )
6419
	{
6420
		writeList.PushBack("");
6421
		writeList.PushBack(CyString("// Section: ") + bSection->sSection);
6422
		writeList.PushBack(bSection->sSection + ";" + CyString::Number(bSection->lEntries.Count()) + ";");
6423
		for ( SStringList *strNode = bSection->lEntries.Head(); strNode; strNode = strNode->next )
6424
		{
6425
			CyString str = strNode->str;
6426
			str.RemoveChar(9);
6427
			str.RemoveChar(' ');
6428
			if ( str.Right(1) != ";" )
6429
				str += ";";
6430
			writeList.PushBack(str);
6431
		}
6432
	}
6433
 
6434
	// write the file to disk
6435
	CFileIO WriteFile(m_sTempDir + "/Bodies.txt");
6436
	if ( WriteFile.WriteFile(&writeList) )
6437
	{
6438
		this->PackFile(&WriteFile, "types\\Bodies.pck");
6439
		WriteFile.Remove();
6440
	}
6441
}
6442
 
6443
void CPackages::CreateCustomStarts()
6444
{
6445
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
6446
	{
6447
		// find all spk files (only ones that can be custom starts
6448
		if ( node->Data()->GetType() != TYPE_SPK )
6449
			continue;
6450
 
6451
		CSpkFile *p = (CSpkFile *)node->Data();
6452
 
6453
		// only use custom starts
6454
		if ( !p->IsCustomStart() )
6455
			continue;
6456
 
6457
		// get the name of the start to use
6458
		CyString name = p->GetCustomStartName();
6459
		if ( name.Empty() )
6460
			continue;
6461
 
6462
		// find if maps file exists
6463
		CyStringList createFiles;
6464
		createFiles.PushBack(name, "maps/x3_universe");
6465
		createFiles.PushBack(name, "types/Jobs");
6466
		createFiles.PushBack(name, "types/JobWings");
6467
 
6468
		for ( SStringList *str = createFiles.Head(); str; str = str->next )
6469
		{
6470
			CyString dir = CFileIO(str->data).GetDir();
6471
			int type = FILETYPE_EXTRA;
6472
			if ( dir.Compare("maps") )
6473
				type = FILETYPE_MAP;
6474
 
6475
			if ( !p->FindFile(str->str + ".xml", type) && !p->FindFile(str->str + ".pck", type) )
6476
			{
6477
				// create a maps files
6478
				int e = this->ExtractGameFile(str->data + ".pck", m_sTempDir + "/" + str->data + ".pck");
6479
				if ( e )
6480
				{
6481
					CFileIO File((e == -1) ? (str->data + ".pck") : (m_sTempDir + "/" + str->data + ".pck"));
6482
					if ( File.Exists() )
6483
					{
6484
						File.Rename(m_sCurrentDir + "/" + dir + "/" + str->str + ".pck");
6485
						this->AddCreatedFile(dir + "/" + str->str + ".pck");
6486
					}
6487
				}
6488
			}
6489
		}
6490
	}
6491
}
6492
 
6493
void CPackages::AddCreatedFile(CyString file)
6494
{
6495
	file = file.Remove(m_sCurrentDir);
6496
	while ( file[0] == '/' )
6497
		file.Erase(0, 1);
6498
	while ( file[0] == '\\' )
6499
		file.Erase(0, 1);
6500
 
6501
	m_lCreatedFiles.PushBack(file, true);
6502
}
6503
 
6504
void CPackages::CreateComponants()
6505
{
6506
	// first check we have any ships
6507
	if ( m_lGameShips.empty() || !this->CountPackages(TYPE_XSP, true) )
6508
		return;
6509
 
6510
	CLinkList<SComponantEntry> dummyList;
6511
 
6512
	// now extract the existing dummies
6513
	int e = ExtractGameFile("types/Components.pck", m_sTempDir + "/Components.txt");
6514
	if ( e )
6515
	{
6516
		// read the dummies
6517
		CFileIO File;
6518
		if ( File.Open((e == -1) ? "Components.txt" : m_sTempDir + "/Components.txt") )
6519
		{
6520
			std::vector<CyString> *lines = File.ReadLines();
6521
			if ( lines )
6522
			{
6523
				int insection = 0;
6524
				int insubsection = 0;
6525
				SComponantEntry *currentSection = NULL;
6526
				SComponantEntry2 *currentSubSection = NULL;
6527
				for ( int j = 0; j < (int)lines->size(); j++ )
6528
				{
6529
					CyString line(lines->at(j));
6530
					if ( line[0] == '/' )
6531
						continue;
6532
					line.RemoveChar('\r');
6533
					line = line.RemoveFirstSpace();
6534
					line = line.RemoveEndSpace();
6535
					if ( line.Empty() )
6536
						continue;
6537
 
6538
 
6539
					// read the section, first entry is section, second is size
6540
					while ( !line.Empty() )
6541
					{
6542
						line = line.RemoveFirstSpace();
6543
						if ( line.Empty() )
6544
							break;
6545
 
6546
						if ( !insection && !insubsection )
6547
						{
6548
							CyString section = line.GetToken(";", 1, 1);
6549
							insection = line.GetToken(";", 2, 2).ToInt();
6550
 
6551
							// search for the sections
6552
							currentSection = NULL;
6553
							for ( CListNode<SComponantEntry> *node = dummyList.Front(); node; node = node->next() )
6554
							{
6555
								SComponantEntry *d = node->Data();
6556
								if ( d->sSection.Compare(section) )
6557
								{
6558
									currentSection = node->Data();
6559
									break;
6560
								}
6561
							}
6562
 
6563
							if ( !currentSection )
6564
							{
6565
								currentSection = new SComponantEntry;
6566
								currentSection->sSection = section;
6567
								dummyList.push_back(currentSection);
6568
							}
6569
 
6570
							// we have some more ?
6571
							line = line.DelToken(";", 1, 2);
6572
						}
6573
						else if ( !insubsection )
6574
						{
6575
							--insection;
6576
							CyString section = line.GetToken(";", 1, 1);
6577
							insubsection = line.GetToken(";", 2, 2).ToInt();
6578
 
6579
							currentSubSection = new SComponantEntry2;
6580
							currentSubSection->sSection = section;
6581
							currentSection->lEntries.push_back(currentSubSection);
6582
 
6583
							line = line.DelToken(";", 1, 2);
6584
						}
6585
						else
6586
						{
6587
							--insubsection;
6588
							currentSubSection->lEntries.PushBack(line.Remove(' '), true);
6589
							line = "";
6590
						}
6591
					}
6592
				}
6593
 
6594
				delete lines;
6595
			}
6596
 
6597
			File.Remove();
6598
		}
6599
 
6600
		// add the new entries for the ships
6601
		for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6602
		{
6603
			SGameShip *s = node->Data();
6604
			if ( s->iType != WARETYPE_ADDED || !s->pPackage )
6605
				continue;
6606
 
6607
			// no dummies to add?
6608
			if ( !s->pPackage->AnyComponents() )
6609
				continue;
6610
 
6611
			// add each dummy to list
6612
			for ( CListNode<SComponent> *dNode = s->pPackage->GetComponents()->Front(); dNode; dNode = dNode->next() )
6613
			{
6614
				SComponent *dummy = dNode->Data();
6615
				SComponantEntry *found = NULL;
6616
				SComponantEntry2 *found2 = NULL;
6617
				for ( CListNode<SComponantEntry> *eNode = dummyList.Front(); eNode; eNode = eNode->next() )
6618
				{
39 cycrow 6619
					if ( eNode->Data()->sSection.Compare(CyString(dummy->sSection)) )
1 cycrow 6620
					{
6621
						found = eNode->Data();
6622
						break;
6623
					}
6624
				}
6625
				if ( !found )
6626
				{
6627
					found = new SComponantEntry;
6628
					found->sSection = dummy->sSection;
6629
					dummyList.push_back(found);
6630
					found2 = new SComponantEntry2;
6631
					found2->sSection = dummy->sSection2;
6632
					found->lEntries.push_back(found2);
6633
				}
6634
				// else check for the 2nd section
6635
				else
6636
				{
6637
					for ( CListNode<SComponantEntry2> *cNode = found->lEntries.Front(); cNode; cNode = cNode->next() )
6638
					{
39 cycrow 6639
						if ( cNode->Data()->sSection.Compare(CyString(dummy->sSection2)) )
1 cycrow 6640
						{
6641
							found2 = cNode->Data();
6642
							break;
6643
						}
6644
					}
6645
 
6646
					if ( !found2 )
6647
					{
6648
						found2 = new SComponantEntry2;
6649
						found2->sSection = dummy->sSection2;
6650
						found->lEntries.push_back(found2);
6651
					}
6652
					else
6653
					{
6654
						bool f = false;
6655
						for ( SStringList *strNode = found2->lEntries.Head(); strNode; strNode = strNode->next )
6656
						{
39 cycrow 6657
							if ( dummy->sData.remove(' ').Compare(strNode->str.ToString()) )
1 cycrow 6658
							{
6659
								f = true;
6660
								break;
6661
							}
6662
						}
6663
 
6664
						if ( f )
6665
							continue;
6666
					}
6667
				}
6668
 
39 cycrow 6669
				found2->lEntries.PushBack(CyString(dummy->sData.remove(' ')));
1 cycrow 6670
			}
6671
		}
6672
 
6673
		// finally, write the file
6674
		std::vector<CyString> lines;
6675
		lines.push_back(CyString("// Components file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2));
6676
		for ( SComponantEntry *dummy = dummyList.First(); dummy; dummy = dummyList.Next() )
6677
		{
6678
			lines.push_back("");
6679
			lines.push_back(CyString("// Section: ") + dummy->sSection + " Entries: " + (long)dummy->lEntries.size());
6680
			lines.push_back(dummy->sSection + ";" + CyString::Number(dummy->lEntries.size()) + ";");
6681
			for ( CListNode<SComponantEntry2> *comp = dummy->lEntries.Front(); comp; comp = comp->next() )
6682
			{
6683
				lines.push_back(comp->Data()->sSection + ";" + (long)comp->Data()->lEntries.Count() + ";");
6684
				for ( SStringList *str = comp->Data()->lEntries.Head(); str; str = str->next )
6685
				{
6686
					CyString cStr = str->str;
6687
					cStr.RemoveEndSpace();
6688
					cStr.RemoveChar(9);
6689
					cStr.RemoveChar('\r');
6690
					if ( cStr.Right(1) != ";" )
6691
						cStr += ";";
6692
					lines.push_back(cStr);
6693
				}
6694
			}
6695
		}
6696
 
6697
		// write the file to disk
6698
		CFileIO WriteFile(m_sTempDir + "/Components.txt");
6699
		if ( WriteFile.WriteFile(&lines) )
6700
		{
6701
			this->PackFile(&WriteFile, "types\\Components.pck");
6702
			WriteFile.Remove();
6703
		}
6704
	}
6705
}
6706
 
6707
bool CPackages::ReadGlobals(CyStringList &globals)
6708
{
6709
	int e = ExtractGameFile("types/Globals.pck", m_sTempDir);
6710
	if ( e )
6711
	{
6712
		CFileIO File(((e == -1) ? "." : m_sTempDir) + "/Globals.txt");
6713
		if ( File.Exists() )
6714
		{
6715
			CyStringList *lines = File.ReadLinesStr();
6716
			if ( lines )
6717
			{
6718
				int entries = -1;
6719
				for ( SStringList *str = lines->Head(); str; str = str->next )
6720
				{
6721
					str->str.RemoveChar('\r');
6722
					str->str.RemoveChar(9);
6723
					str->str.RemoveFirstSpace();
6724
 
6725
					if ( str->str.Empty() )
6726
						continue;
6727
					if ( str->str[0] == '/' )
6728
						continue;
6729
 
6730
					// remove comments
6731
					CyString l = str->str;
6732
					if ( l.IsIn("/") ) 
6733
						l = l.GetToken("/", 1, 1);
6734
 
6735
					if ( entries == -1 )
6736
						entries = l.GetToken(";", 1, 1).ToInt();
6737
					else
6738
						globals.PushBack(l.GetToken(";", 1, 1), l.GetToken(";", 2, 2));
6739
				}
6740
 
6741
				delete lines;
6742
 
6743
				return true;
6744
			}
6745
		}
6746
	}
6747
 
6748
	return false;
6749
}
6750
 
6751
void CPackages::CreateGlobals()
6752
{
6753
	if ( m_lGlobals.Empty() )
6754
		return; // no global settings
6755
 
6756
	CyStringList globals;
6757
	if ( ReadGlobals(globals) )
6758
	{
6759
		// apply out settings
6760
		for ( SStringList *str = m_lGlobals.Head(); str; str = str->next )
6761
		{
6762
			SStringList *found = globals.FindString(str->str);
6763
			if ( found )
6764
				found->data = str->data;
6765
		}
6766
 
6767
		// now write it
6768
		CyStringList writeList;
6769
		for ( SStringList *str = globals.Head(); str; str = str->next )
6770
			writeList.PushBack(str->str + ";" + str->data + ";");
6771
 
6772
		// finally, write the file
6773
		writeList.PushFront(CyString::Number(writeList.Count()) + "; /globals amount", "");
6774
		writeList.PushFront(CyString("// Globals file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2), "");
6775
 
6776
		CFileIO WriteFile(m_sTempDir + "/Globals.txt");
6777
		if ( WriteFile.WriteFile(&writeList) )
6778
		{
6779
			this->PackFile(&WriteFile, "types/Globals.pck");
6780
			WriteFile.Remove();
6781
		}
6782
	}
6783
}
6784
 
6785
void CPackages::CreateTShips()
6786
{
6787
	// no ships ?
6788
	if ( m_lGameShips.empty() )
6789
		return;
6790
 
6791
	// get the cockpit list to match with ships turrets
6792
	CyStringList Cockpits;
6793
	CyStringList *cockpitList = this->CreateCockpits();
6794
	if ( cockpitList )
6795
	{
6796
		for ( SStringList *str = cockpitList->Head(); str; str = str->next )
6797
		{
6798
			CyString id = str->str.GetToken(";", 19, 19);
6799
			Cockpits.PushBack(id);
6800
		}
6801
 
6802
		delete cockpitList;
6803
	}
6804
 
6805
	CLinkList<SGameShip> shipOverrides;
6806
	for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6807
	{
6808
		if ( node->Data()->iType != WARETYPE_ADDED || !node->Data()->pPackage )
6809
			continue;		
6810
		if ( !node->Data()->pPackage->IsExistingShip() )
6811
			continue;
6812
		shipOverrides.push_back(node->Data());
6813
	}
6814
 
6815
	// read the existing tships file
6816
	int e = ExtractGameFile("types/TShips.pck", m_sTempDir + "/TShips.txt");
6817
	if ( e )
6818
	{
6819
		int fileType = 51;
6820
		CyStringList tshipsList;
6821
 
6822
		// if we have no buffer, lets create one
6823
		CFileIO File;
6824
		if ( File.Open((e == -1) ? "TShips.txt" : m_sTempDir + "/TShips.txt") )
6825
		{
6826
			int shiptext = SHIPSTARTTEXT;
6827
 
6828
			std::vector<CyString> *lines = File.ReadLines();
6829
			if ( lines )
6830
			{
6831
				int count = -1;
6832
				for ( int j = 0; j < (int)lines->size(); j++ )
6833
				{
6834
					CyString line(lines->at(j));
6835
					if ( line[0] == '/' )
6836
						continue;
6837
					line.RemoveChar('\r');
6838
					line.RemoveChar(9);
6839
					line = line.RemoveFirstSpace();
6840
					line = line.RemoveEndSpace();
6841
					if ( line.Empty() )
6842
						continue;
6843
 
6844
					if ( count == -1 )
6845
					{
6846
						fileType = line.GetToken(";", 1, 1).ToInt();
6847
						count = line.GetToken(";", 2, 2).ToInt();
6848
					}
6849
					else
6850
					{
6851
						if ( line.Right(1) != ";" )
6852
							line += ";";
6853
 
6854
						// check for any ship overrides
6855
						bool added = false;
6856
						if ( !shipOverrides.empty() )
6857
						{
6858
							CShipData shipData;
6859
							if ( shipData.ReadShipData(line) )
6860
							{
6861
								for ( CListNode<SGameShip> *node = shipOverrides.Front(); node; node = node->next() )
6862
								{
6863
									SGameShip *s = node->Data();
14 cycrow 6864
									if ( !s->pPackage->GetShipID().Compare(shipData.sID.ToString()) )
1 cycrow 6865
										continue;
6866
									s->iText = shiptext;
6867
									if ( !s->pPackage->GetOriginalDescription() )
6868
										shiptext += 2;
6869
									s->iPos = tshipsList.Count();
6870
									added = true;
39 cycrow 6871
									tshipsList.PushBack(CyString(s->pPackage->FormatShipData(&Cockpits, &s->iText, m_iGame)));
1 cycrow 6872
									shipOverrides.remove(node);
6873
									break;
6874
								}
6875
							}
6876
						}
6877
 
6878
						if ( !added )
6879
							tshipsList.PushBack(line);
6880
						--count;
6881
						if ( count < 0 )
6882
							break;
6883
					}
6884
				}
6885
 
6886
				delete lines;
6887
 
6888
			}
6889
 
6890
			File.Remove();
6891
 
6892
			// assign the ship buffer
6893
			if ( !m_iShipBuffer )
6894
				m_iShipBuffer = tshipsList.Count() + 15;
6895
			// there seems to be too many additional entries, we have no choise but to change the buffer
6896
			else if ( m_iShipBuffer <= tshipsList.Count() )
6897
				m_iShipBuffer = tshipsList.Count() + 15;
6898
 
6899
			CyString bufferStart;
6900
			if ( m_iGame == GAME_X3 )
6901
				bufferStart = "0;0;0;0;0;2;499999;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0.049988;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;4;1;0;0;0;0;0;0;2092;1;1;-1;0;0;1;1;0;1;1;1;0;0;0;;-1;0;0;0;0;0;0;0;0;0;";
6902
			else
6903
				bufferStart = "0;0;0;0;0;SG_SH_M5;499999;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0.049988;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;4;1;0;0;0;0;0;0;OBJ_BEACON;1;1;-1;0;0;1;1;0;1;1;1;0;0;0;;-1;0;0;0;0;0;0;0;0;0;";
6904
			// add the buffers now
6905
			for ( int i = tshipsList.Count(); i < m_iShipBuffer; i++ )
6906
				tshipsList.PushBack(bufferStart + "SHIP_BUFFER;");
6907
 
6908
			// now lets add our tships line
6909
			for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
6910
			{
6911
				SGameShip *s = node->Data();
6912
				if ( s->pPackage && s->pPackage->IsExistingShip() )
6913
					continue;
6914
				s->iPos = tshipsList.Count();
6915
				if ( s->iType == WARETYPE_ADDED && s->pPackage )
6916
				{
6917
					s->iText = shiptext;
6918
					if ( !s->pPackage->GetOriginalDescription() )
6919
						shiptext += 2;
6920
 
39 cycrow 6921
					tshipsList.PushBack(CyString(s->pPackage->FormatShipData(&Cockpits, &s->iText, m_iGame)));
1 cycrow 6922
				}
6923
				else if ( s->iType == WARETYPE_DELETED )
6924
					tshipsList.PushBack(bufferStart + "SHIP_DELETED;");
6925
				else if ( s->iType == WARETYPE_DISABLED )
6926
					tshipsList.PushBack(bufferStart + "SHIP_DISABLED;");
6927
				else
6928
					tshipsList.PushBack(bufferStart + "SHIP_SPACER;");
6929
			}
6930
 
6931
			// finally, write the file
6932
			tshipsList.PushFront(CyString::Number(fileType) + ";" + CyString::Number(tshipsList.Count()) + ";", "");
6933
			tshipsList.PushFront(CyString("// TShips file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2), "");
6934
 
6935
			CFileIO WriteFile(m_sTempDir + "/TShips.txt");
6936
			if ( WriteFile.WriteFile(&tshipsList) )
6937
			{
6938
				this->PackFile(&WriteFile, "types/TShips.pck");
6939
				WriteFile.Remove();
6940
			}
6941
		}
6942
	}
6943
 
6944
}
6945
 
6946
bool CPackages::PackFile(CyString filename)
6947
{
6948
	// compress the file
6949
	CFileIO File(filename);
6950
	size_t fileSize;
6951
	char *fileData = File.ReadToData(&fileSize);
6952
 
6953
	if ( fileData && fileSize)
6954
	{
6955
		size_t newFileSize;
6956
		unsigned char *pckData = PCKData((unsigned char *)fileData, fileSize, &newFileSize, true);
6957
		if ( pckData )
6958
		{
6959
			CyString ext = "pck";
6960
			if ( File.CheckFileExtension("bob") )
6961
				ext = "pbb";
6962
			else if ( File.CheckFileExtension("bod") )
6963
				ext = "pbd";
6964
			CFileIO pckFile(File.ChangeFileExtension(ext));
6965
			if ( !CDirIO(pckFile.GetDir()).Exists() )
6966
				CDirIO(pckFile.GetDir()).Create();
6967
			pckFile.WriteData((char *)pckData, newFileSize);
6968
			return true;
6969
		}
6970
	}
6971
 
6972
	return false;
6973
}
6974
 
6975
bool CPackages::UnPackFile(CyString filename, bool checkxml)
6976
{
6977
	// compress the file
6978
	CFileIO File(filename);
6979
	size_t fileSize;
6980
	char *fileData = File.ReadToData(&fileSize);
6981
 
6982
	if ( fileData && fileSize)
6983
	{
6984
		size_t newFileSize;
6985
		unsigned char *pckData = UnPCKData((unsigned char *)fileData, fileSize, &newFileSize, true);
6986
		if ( pckData )
6987
		{
6988
			CyString ext = "txt";
6989
			if ( File.CheckFileExtension("pbb") )
6990
				ext = "bob";
6991
			else if ( File.CheckFileExtension("pbd") )
6992
				ext = "bod";
6993
			CFileIO pckFile(File.ChangeFileExtension(ext));
6994
			if ( !CDirIO(pckFile.GetDir()).Exists() )
6995
				CDirIO(pckFile.GetDir()).Create();
6996
			pckFile.WriteData((char *)pckData, newFileSize);
6997
 
6998
			// check for xml and rename
6999
			if ( checkxml )
7000
			{
7001
				int readmaxlines = 20;
7002
				bool isxml = false;
7003
				do {
7004
					CyString line = pckFile.ReadToEndLine(true);
7005
					if ( line.IsIn("<language id=") )
7006
					{
7007
						isxml = true;
7008
						break;
7009
					}
7010
					else if ( line.IsIn("<page id=") )
7011
					{
7012
						isxml = true;
7013
						break;
7014
					}
7015
					else if ( line.IsIn("<?xml") || line.IsIn("<script>") )
7016
					{
7017
						isxml = true;
7018
						break;
7019
					}
7020
					--readmaxlines;
7021
					if ( readmaxlines <= 0 )
7022
						break;
7023
				} while (pckFile.IsOpened());
7024
 
7025
				if ( pckFile.IsOpened() )
7026
					pckFile.StopRead();
7027
 
7028
				if ( isxml )
7029
					pckFile.Rename(pckFile.ChangeFileExtension("xml"));
7030
			}
7031
 
7032
			return true;
7033
		}
7034
	}
7035
 
7036
	return false;
7037
}
7038
 
7039
bool CPackages::PackFile(CFileIO *File, CyString filename)
7040
{
7041
	filename = filename.FindReplace("\\", "/");
7042
	if ( m_iGame == GAME_X3 )
7043
	{
7044
		CCatFile catFile;
7045
		int error = catFile.Open(m_sCurrentDir + "/mods/PluginManager.cat", this->GetAddonDir(), CATREAD_CATDECRYPT, true);
7046
		if ( error == CATERR_NONE || error == CATERR_CREATED )
7047
		{
7048
			// it it wrote ok, remove the old ones
7049
			if ( !catFile.AppendFile(File->GetFullFilename(), filename, true, true) )
7050
				return false;
7051
			return true;
7052
		}
7053
	}
7054
	else
7055
	{
7056
		// compress the file
7057
		size_t fileSize;
7058
		char *fileData = CFileIO(File->GetFullFilename()).ReadToData(&fileSize);
7059
 
7060
		if ( fileData && fileSize)
7061
		{
7062
			size_t newFileSize;
7063
			unsigned char *pckData = PCKData((unsigned char *)fileData, fileSize, &newFileSize, true);
7064
			if ( pckData )
7065
			{
7066
//				if ( !this->GetAddonDir().Empty() && CCatFile::IsAddonDir(filename) )
7067
//					filename = this->GetAddonDir() + "/" + filename;
7068
				CFileIO pckFile(m_sCurrentDir + "/" + filename);
7069
				if ( !CDirIO(pckFile.GetDir()).Exists() )
7070
					CDirIO(pckFile.GetDir()).Create();
7071
				pckFile.WriteData((char *)pckData, newFileSize);
7072
				this->AddCreatedFile(pckFile.GetFullFilename());
7073
				return true;
7074
			}
7075
		}
7076
	}
7077
 
7078
	return false;
7079
}
7080
 
7081
CyStringList *CPackages::CreateCockpits()
7082
{
7083
	// first check we have any ships
7084
	if ( m_lGameShips.empty() || !this->CountPackages(TYPE_XSP, true) )
7085
		return NULL;
7086
 
7087
	CyStringList *cockpitList = new CyStringList;
7088
 
7089
	// now extract the existing cockpits
7090
	int fileType = 51;
7091
	int e = ExtractGameFile("types/TCockpits.pck", m_sTempDir + "/TCockpits.txt");
7092
	if ( e )
7093
	{
7094
		// read the dummies
7095
		CFileIO File;
7096
		if ( File.Open((e == -1) ? "TCockpits.txt" : m_sTempDir + "/TCockpits.txt") )
7097
		{
7098
			CyStringList *lines = File.ReadLinesStr();
7099
			if ( lines )
7100
			{
7101
				int count = -1;
7102
				for ( SStringList *str = lines->Head(); str; str = str->next )
7103
				{
7104
					CyString line(str->str);
7105
					line.RemoveChar('\r');
7106
					line.RemoveChar(9);
7107
					line = line.RemoveFirstSpace();
7108
					line = line.RemoveEndSpace();
7109
					if ( line.Empty() )
7110
						continue;
7111
					if ( line[0] == '/' )
7112
						continue;
7113
 
7114
					if ( count == -1 )
7115
					{
7116
						fileType = line.GetToken(";", 1, 1).ToInt();
7117
						count = line.GetToken(";", 2, 2).ToInt();
7118
					}
7119
					else
7120
					{
7121
						while ( !line.Empty() )
7122
						{
7123
							CyString data = line.GetToken(";", 1, 19);
7124
							cockpitList->PushBack(data + ";");
7125
							line = line.DelToken(";", 1, 19);
7126
 
7127
							--count;
7128
							if ( count < 1 )
7129
								break;
7130
						}
7131
					}
7132
				}
7133
 
7134
				delete lines;
7135
			}
7136
 
7137
			File.Remove();
7138
		}
7139
 
7140
		// now add the new ones
7141
		for ( CListNode<SGameShip> *node = m_lGameShips.Front(); node; node = node->next() )
7142
		{
7143
			SGameShip *s = node->Data();
7144
			if ( s->iType != WARETYPE_ADDED || !s->pPackage )
7145
				continue;
7146
 
7147
			if ( !s->pPackage->AnyCockpits() )
7148
				continue;
7149
 
7150
			for ( CListNode<SCockpit> *cn = s->pPackage->GetCockpits()->Front(); cn; cn = cn->next() )
7151
			{
7152
				bool foundEntry = false;
7153
				CyString cockpitStr = cn->Data()->sCockpit;
7154
				// search for matching game entry
7155
				for ( CListNode<SWeaponMask> *wm = cn->Data()->lWeaponMask.Front(); wm; wm = wm->next() )
7156
				{
7157
					if ( wm->Data()->iGame == (m_iGame - 1) )
7158
					{
7159
						if ( wm->Data()->iMask != -1 )
7160
							cockpitStr = cockpitStr.RepToken(";", 9, CyString::Number(wm->Data()->iMask));
7161
						foundEntry = true;
7162
						break;
7163
					}
7164
				}
39 cycrow 7165
 
7166
				bool found = false;
1 cycrow 7167
				for ( SStringList *str = cockpitList->Head(); str; str = str->next )
7168
				{
39 cycrow 7169
					if ( str->str.GetToken(";", 19, 19).Compare(CyString(cn->Data()->sCockpit.token(";", 19))) )
1 cycrow 7170
					{
7171
						// only replace existing entry if we have sepeperate weapon masks set
7172
						if ( foundEntry )
7173
							str->str = cockpitStr;
7174
						found = true;
7175
						break;
7176
					}
7177
				}
7178
 
7179
				if ( !found )
7180
					cockpitList->PushBack(cockpitStr);
7181
			}
7182
		}
7183
 
7184
		// finally, write the file
7185
		cockpitList->PushFront(CyString::Number(fileType) + ";" + CyString::Number(cockpitList->Count()) + ";", "");
7186
		cockpitList->PushFront(CyString("// TCockpits file, created by SPK Libraries V") + CyString::CreateFromFloat(GetLibraryVersion(), 2), "");
7187
 
7188
		CFileIO WriteFile(m_sTempDir + "/TCockpits.txt");
7189
		if ( WriteFile.WriteFile(cockpitList) )
7190
		{
7191
			this->PackFile(&WriteFile, "types\\TCockpits.pck");
7192
			WriteFile.Remove();
7193
		}
7194
 
7195
		// remove those entrys
7196
		cockpitList->PopFront();
7197
		cockpitList->PopFront();
7198
	}
7199
 
7200
	return cockpitList;
7201
}
7202
 
7203
CBaseFile *CPackages::FindScriptByAuthor(CyString author, CBaseFile *prev)
7204
{
7205
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
7206
	{
7207
		CBaseFile *p = node->Data();
7208
		if ( prev )
7209
		{
7210
			if ( p == prev )
7211
				prev = NULL;
7212
			continue;
7213
		}
50 cycrow 7214
		if ( p->author().Compare(author.ToString()) )
1 cycrow 7215
			return p;
7216
	}
7217
 
7218
	return NULL;
7219
}
7220
 
7221
CBaseFile *CPackages::LoadPackagerScript(CyString filename, int compression, CyString (*askFunc)(CyString), CyStringList *malformedLines, CyStringList *unknownCommands, CyStringList *variables)
7222
{
7223
	// check the file exists
7224
	if ( !CFileIO(filename).Exists() )
7225
		return NULL;
7226
 
7227
	// read all the lines
7228
	CFileIO File(filename);
7229
	std::vector<CyString> *lines = File.ReadLines();
7230
 
7231
	if ( !lines )
7232
		return NULL;
7233
 
7234
	CBaseFile *package = NULL;
7235
 
7236
	// filter out blank lines and comments
7237
	CyStringList fileData;
7238
	for ( int i = 0; i < (int)lines->size(); i++ )
7239
	{
7240
		CyString line(lines->at(i));
7241
 
7242
		// dont include empty lines (whitespace)
7243
		if ( line.Empty() )
7244
			continue;
7245
 
7246
		// filter out any spaces, tabs in front
7247
		line.RemoveChar('\t');
7248
		line.RemoveChar('\r');
7249
		CyString linenospace = line;
7250
		linenospace.RemoveFirstSpace();
7251
		if ( linenospace.Empty() )
7252
			continue;
7253
 
7254
		// check for any comments
7255
		if ( linenospace.Left(2) == "//" )
7256
			continue;
7257
		if ( linenospace[0] == '#' )
7258
			continue;
7259
 
7260
		// all commands start with a keyword followed by a colon, if one doesn't exist, it cant be a valid line
7261
		if ( !line.IsIn(':') )
7262
		{
7263
			// there are some exeptions, and these are one word entrys only
7264
			line.RemoveEndSpace();
7265
			if ( line.IsIn(" ") )
7266
			{
7267
				if ( malformedLines )
7268
					malformedLines->PushBack(line, CyString::Number(i));
7269
				continue;
7270
			}
7271
		}
7272
 
7273
		// check for the type line
7274
		if ( !package && line.GetToken(":", 1, 1).Compare("FileType") )
7275
		{
7276
			CyString sFileType = line.GetToken(":", 2).RemoveFirstSpace();
7277
			if ( sFileType.Compare("Ship") )
7278
				package = new CXspFile();
7279
			else if ( sFileType.Compare("Script") )
7280
				package = new CSpkFile();
7281
			else if ( sFileType.Compare("Base") )
7282
				package = new CBaseFile();
7283
		}
7284
 
7285
		fileData.PushBack(line, CyString::Number(i));
7286
	}
7287
 
7288
	delete lines;
7289
 
7290
	// assume its a script if no type is set (all old versions are scripts)
7291
	if ( !package )
7292
		package = new CSpkFile();
7293
 
7294
	if ( compression != -1 )
7295
		package->SetDataCompression(compression);
7296
 
7297
	CyString ftpaddr;
7298
	CyString ftpuser;
7299
	CyString ftppass;
7300
	CyString ftpdir;
7301
	// now lets read the rest of the day
7302
	CyStringList listVaribles;
7303
	for ( SStringList *strNode = fileData.Head(); strNode; strNode = strNode->next )
7304
	{
7305
		CyString first = strNode->str.GetToken(":", 1, 1);
7306
		CyString rest = strNode->str.GetToken(":", 2).RemoveFirstSpace();
7307
 
7308
		if ( first.Compare("Varible") || first.Compare("Variable") )
7309
			listVaribles.PushBack(rest.GetToken(" ", 1, 1), rest.GetToken(" ", 2), true);
7310
		else
7311
		{
7312
			// replace variables
7313
			if ( rest.IsIn("$") )
7314
			{
7315
				for ( SStringList *strVar = listVaribles.Head(); strVar; strVar = strVar->next )
7316
				{
7317
					if ( rest.IsIn(strVar->str) )
7318
						rest.FindReplace(strVar->str, strVar->data);
7319
				}
7320
 
7321
				if ( variables )
7322
				{
7323
					for ( SStringList *strVar = variables->Head(); strVar; strVar = strVar->next )
7324
					{
7325
						if ( rest.IsIn(strVar->str) )
7326
							rest.FindReplace(strVar->str, strVar->data);
7327
					}
7328
				}
7329
			}
7330
 
7331
			//check for the built in varibles
7332
			if ( rest.IsIn("$ASK") )
7333
			{
7334
				CyString replace = "$ASK";
7335
				CyString result;
7336
 
7337
				if ( askFunc )
7338
					result = askFunc(first);
7339
 
7340
				if ( rest.IsIn("$ASK(") )
7341
				{
7342
					replace = rest.GetToken("$ASK(", 2).GetToken(")", 1, 1);
7343
					if ( result.Empty() )
7344
						result = replace;
7345
					replace = CyString("$ASK(") + replace + ")";
7346
				}
7347
 
7348
 
7349
				if ( !result.Empty() )
7350
					rest.FindReplace(replace, result);
7351
			}
7352
			// todays date
7353
			if ( rest.IsIn("$DATE") )
7354
			{
7355
				time_t now;
7356
				time(&now);
7357
				struct tm *timeinfo = localtime(&now);
7358
				CyString result = CyString::Number(timeinfo->tm_mday) + "." + CyString::Number(timeinfo->tm_mon + 1) + "." + CyString::Number(timeinfo->tm_year + 1900);
7359
				if ( !result.Empty() )
7360
					rest.FindReplace("$DATE", result);
7361
			}
7362
			// mydocuments
7363
			if ( rest.IsIn("$MYDOCUMENTS") )
7364
			{
7365
				if ( !m_sMyDoc.Empty() )
7366
					rest.FindReplace("$MYDOCUMENTS", m_sMyDoc);
7367
			}
7368
 
7369
			// current path
7370
			if ( rest.IsIn("$PATH") )
7371
			{
7372
				CyString currentDir = CFileIO(filename).GetDir();
7373
				if ( !currentDir.Empty() )
7374
					rest.FindReplace("$PATH", currentDir);
7375
			}
7376
 
7377
			// now parse the rest of the values
7378
			if ( first.Compare("FtpUpload") )
7379
				ftpaddr = rest.GetToken(" ", 1, 1) + ":" + rest.GetToken(" ", 2, 2);
7380
			else if ( first.Compare("FtpUser") )
7381
				ftpuser = rest;
7382
			else if ( first.Compare("FtpPass") )
7383
				ftppass = rest;
7384
			else if ( first.Compare("FtpDir") )
7385
				ftpdir = rest;
14 cycrow 7386
			else if ( !package->LoadPackageData(first.ToString(), rest.ToString()) )
1 cycrow 7387
			{
7388
				if ( unknownCommands )
7389
					unknownCommands->PushBack(first, rest);
7390
			}
7391
		}
7392
	}
7393
 
50 cycrow 7394
	if ( package->filename().empty() )
1 cycrow 7395
		package->LoadPackageData("AutoSave", "$AUTOSAVE");
7396
 
7397
	if ( !ftpaddr.Empty() )
7398
	{
7399
		if ( !ftpuser.Empty() )
7400
		{
7401
			if ( !ftppass.Empty() )
7402
				ftpaddr = ftpuser + ":" + ftppass + "@" + ftpaddr;
7403
			else
7404
				ftpaddr = ftpuser + "@" + ftpaddr;
7405
		}
7406
 
7407
		if ( !ftpdir.Empty() )
7408
			ftpaddr += ftpdir;
7409
 
7410
		package->SetFtpAddr(ftpaddr);
7411
	}
7412
 
7413
	return package;
7414
}
7415
 
7416
CyString CPackages::GetLanguageName()
7417
{
7418
	return CPackages::ConvertLanguage(m_iLanguage);
7419
}
7420
 
7421
CyString CPackages::ConvertLanguage(int lang)
7422
{
7423
	switch ( lang )
7424
	{
7425
		case 44:
7426
			return "English";
7427
		case 49:
7428
			return "German";
7429
		case 7:
7430
			return "Russian";
7431
		case 33:
7432
			return "French";
7433
		case 30:
7434
			return "Greek";
7435
		case 31:
7436
			return "Dutch";
7437
		case 32:
7438
			return "Belgian";
7439
		case 34:
7440
			return "Spanish";
7441
		case 36:
7442
			return "Hungarian";
7443
		case 39:
7444
			return "Italian";
7445
		case 40:
7446
			return "Romanian";
7447
		case 41:
7448
			return "Swiss";
7449
		case 42:
7450
			return "Czech";
7451
		case 43:
7452
			return "Austrian";
7453
		case 45:
7454
			return "Danish";
7455
		case 46:
7456
			return "Swedish";
7457
		case 47:
7458
			return "Norweigen";
7459
		case 48:
7460
			return "Polish";
7461
	}
7462
 
7463
	return CyString::Number(lang);
7464
}
7465
 
7466
bool CPackages::CheckAccessRights(CyString dir)
7467
{
7468
	if ( dir.Empty() )
7469
		dir = m_sCurrentDir;
7470
 
7471
	// write a file, then read the contents
7472
	CFileIO File(dir + "/accessrightscheck.dat");
7473
 
7474
	// check if file exists and remove it
7475
	if ( File.Exists() )
7476
	{
7477
		// if we cant remove it, we dont have enough rights
7478
		if ( !File.Remove() )
7479
			return false;
7480
 
7481
		// if its still there, we dont have enough rights
7482
		if ( File.Exists() )
7483
			return false;
7484
	}
7485
 
7486
	// now create the file
7487
	if ( !File.WriteString("testing access rights") )
7488
		return false;
7489
 
7490
	// now check it exists
7491
	if ( !File.Exists() )
7492
		return false;
7493
 
7494
	// now read the file for the correct contents
7495
	CyStringList *lines = File.ReadLinesStr();
7496
	if ( !lines )
7497
		return false;
7498
 
7499
	// check that one of the lines is correct
7500
	for ( SStringList *s = lines->Head(); s; s = s->next )
7501
	{
7502
		if ( s->str == "testing access rights" )
7503
		{
7504
			delete lines;
7505
			return true;
7506
		}
7507
	}
7508
 
7509
	delete lines;
7510
 
7511
	return false;
7512
}
7513
 
7514
bool CPackages::LoadShipData(CyString file, CyStringList *list)
7515
{
7516
	CFileIO File;
7517
	bool deleteFile = false;
7518
 
7519
	// load from cat file
7520
	if ( CFileIO(file).CheckFileExtension("cat") )
7521
	{
7522
		CCatFile cat;
7523
		if ( cat.Open(file, this->GetAddonDir(), CATREAD_CATDECRYPT, false) != CATERR_NONE )
7524
			return false;
7525
 
7526
		if ( !cat.ExtractFile("types\\TShips.pck", m_sTempDir + "/tships.txt") )
7527
			return false;
7528
 
7529
		File.Open(m_sTempDir + "/tships.txt");
7530
		deleteFile = true;
7531
	}
7532
	// otherwise its a normal file
7533
	else if ( CFileIO(file).CheckFileExtension("pck") )
7534
	{
7535
		C_File f(file);
7536
		if ( !f.ReadFromFile() )
7537
			return false;
7538
		f.UnPCKFile();
7539
 
7540
		f.SetFilename(m_sTempDir + "/tships.txt");
7541
		if ( !f.WriteFilePointer() )
7542
			return false;
7543
 
7544
		File.Open(m_sTempDir + "/tships.txt");
7545
		deleteFile = true;
7546
	}
7547
	else
7548
		File.Open(file);
7549
 
7550
	if ( !File.Exists() )
7551
		return false;
7552
 
7553
	bool ret = false;
7554
	CyStringList *lines = File.ReadLinesStr();
7555
	if ( lines )
7556
	{
7557
		bool readFirst = false;
7558
		for ( SStringList *str = lines->Head(); str; str = str->next )
7559
		{
7560
			if ( str->str.Empty() )
7561
				continue;
7562
			str->str.RemoveChar('\r');
7563
			str->str.RemoveChar(9);
7564
			str->str.RemoveFirstSpace();
7565
			if ( str->str.Empty() )
7566
				continue;
7567
			if ( str->str[0] == '/' || str->str[0] == '#' )
7568
				continue;
7569
 
7570
			if ( !readFirst )
7571
				readFirst = true;
7572
			else
7573
			{
7574
				CyString t = str->str.GetToken(";", -2);
7575
				while ( t.Right(1) == ";" )
7576
					t.Truncate((int)t.Length() - 1);
7577
				list->PushBack(t, str->str);
7578
			}
7579
		}
7580
 
7581
		delete lines;
7582
		ret = true;
7583
	}
7584
 
7585
	if ( deleteFile )
7586
		File.Remove();
7587
 
7588
	return ret;
7589
}
7590
 
7591
CyString CPackages::ReadShipData(CyString file, CyString id)
7592
{
7593
	CyStringList *list = this->LoadShipData(file);
7594
	if ( !list )
7595
		return NullString;
7596
 
7597
	CShipData data;
7598
	for ( SStringList *str = list->Head(); str; str = str->next )
7599
	{
7600
		if ( str->str.Compare(id) )
7601
		{
7602
			delete list;
7603
			return str->data;
7604
		}
7605
	}
7606
 
7607
	delete list;
7608
	return NullString;
7609
}
7610
 
7611
CyStringList *CPackages::LoadShipData(CyString file)
7612
{
7613
	CyStringList *list = new CyStringList;
7614
	if ( this->LoadShipData(file, list) )
7615
		return list;
7616
 
7617
	delete list;
7618
	return NULL;
7619
}
7620
 
7621
bool CPackages::ReadTextPage(CyString file, CyStringList *list, bool search, int page)
7622
{
7623
	CFileIO File;
7624
	bool deleteFile = false;
7625
 
7626
	// read all text files from mod
7627
	if ( CFileIO(file).CheckFileExtension("cat") )
7628
	{
7629
		bool done = false;
7630
 
7631
		CCatFile cat;
7632
		if ( cat.Open(file, this->GetAddonDir(), CATREAD_CATDECRYPT, false) != CATERR_NONE )
7633
			return false;
7634
 
7635
		// extract 1 at a time
7636
		for ( int i = 0; i < cat.GetNumFiles(); i++ )
7637
		{
7638
			SInCatFile *f = cat.GetFile(i);
7639
			CyString sF = f->sFile;
7640
			// is a text file
7641
			sF = sF.FindReplace("\\", "/");
7642
			if ( !sF.GetToken("/", 1, 1).Compare("t") )
7643
				continue;
7644
 
7645
			CyString baseFile = CFileIO(sF).GetBaseName();
7646
			// check language
7647
			int lang = 0;
7648
			if ( baseFile.FindPos("-L") != -1 ) // new language file
7649
				lang = baseFile.Right(3).ToInt();
7650
			else
7651
			{
7652
				baseFile.Truncate((int)baseFile.Length() - 4);
7653
				lang = baseFile.ToInt();
7654
			}
7655
 
7656
			if ( lang != m_iLanguage )
7657
				continue;
7658
 
7659
			// now extract and parse
7660
			if ( cat.ExtractFile(f->sFile, m_sTempDir + "/" + CFileIO(f->sFile).GetBaseName() + ".xml") )
7661
			{
7662
				if ( this->ReadTextPage(m_sTempDir + "/" + CFileIO(f->sFile).GetBaseName() + ".xml", list, search, page) )
7663
					done = true;
7664
			}
7665
		}
7666
 
7667
		return done;
7668
	}
7669
	// otherwise its a normal file
7670
	else if ( CFileIO(file).CheckFileExtension("pck") )
7671
	{
7672
		C_File f(file);
7673
		if ( !f.ReadFromFile() )
7674
			return false;
7675
		f.UnPCKFile();
7676
 
7677
		f.SetFilename(m_sTempDir + "/textfile.xml");
7678
		if ( !f.WriteFilePointer() )
7679
			return false;
7680
 
7681
		File.Open(m_sTempDir + "/textfile.xml");
7682
		deleteFile = true;
7683
	}
7684
	else
7685
		File.Open(file);
7686
 
7687
	if ( !File.Exists() )
7688
		return false;
7689
 
7690
	// open and read file
7691
	CyStringList *lines = File.ReadLinesStr();
7692
	if ( !lines )
7693
		return false;
7694
 
7695
	bool inPage = false;
7696
	for ( SStringList *str = lines->Head(); str; str = str->next )
7697
	{
7698
		// search for page
7699
		if ( !inPage )
7700
		{
7701
			if ( str->str.FindPos("<page") > -1 )
7702
			{
7703
				// find the page id
7704
				int pos = str->str.FindPos("\"");
7705
				if ( pos > -1 )
7706
				{
7707
					int endpos = str->str.FindPos("\"", pos + 1);
7708
					if ( endpos > -1 )
7709
					{
7710
						CyString p = str->str.Mid(pos + 2, endpos - pos - 1);
7711
						if ( p.Length() > 4 )
7712
							p = p.Right(4);
7713
						int checkPage = p.ToInt();
7714
						if ( checkPage == page )
7715
							inPage = true;
7716
					}
7717
				}
7718
			}
7719
 
7720
		}
7721
		// add each id
7722
		else
7723
		{
7724
			if ( str->str.FindPos("</page") > -1 )
7725
				break;
7726
 
7727
			if ( str->str.FindPos("<t id") > -1 )
7728
			{
7729
				int pos = str->str.FindPos("\"");
7730
				if ( pos > -1 )
7731
				{
7732
					int endpos = str->str.FindPos("\"", pos + 1);
7733
					if ( endpos > -1 )
7734
					{
7735
						int id = str->str.Mid(pos + 2, endpos - pos - 1).ToInt();
7736
						pos = str->str.FindPos(">", endpos);
7737
						if ( pos > -1 )
7738
						{
7739
							++pos;
7740
							endpos = str->str.FindPos("</", pos);
7741
							if ( endpos > -1 )
7742
							{
7743
								CyString text = str->str.Mid(pos + 1, endpos - pos);
7744
								while ( text.FindPos('(') != -1 && text.FindPos(')') != -1 )
7745
								{
7746
									int s = text.FindPos('(');
7747
									text = text.Erase(s, text.FindPos(')') - s + 1);
7748
								}
7749
								list->PushBack(CyString::Number(id), text, search);
7750
							}
7751
						}
7752
					}
7753
				}
7754
			}
7755
		}
7756
	}
7757
 
7758
	delete lines;
7759
 
7760
	return true;
7761
}
7762
 
7763
CyStringList *CPackages::ReadTextPage(CyString file, bool search, int page)
7764
{
7765
	CyStringList *list = new CyStringList;
7766
	if ( this->ReadTextPage(file, list, search, page) )
7767
		return list;
7768
 
7769
	delete list;
7770
	return NULL;
7771
}
7772
 
7773
int CPackages::AdjustFileType(CyString file, int filetype)
7774
{
7775
	CFileIO File(file);
7776
	CyString dir = File.GetDirIO().TopDir();
7777
	CyString basename = File.GetBaseName();
7778
 
7779
	// mod files
7780
	if ( File.CheckFileExtension("cat") || File.CheckFileExtension("dat") )
7781
		return FILETYPE_MOD;
7782
	// check for text files
7783
	if ( File.GetFilename().IsIn("-L") && File.GetFilename().Left(4).ToInt() )
7784
		return FILETYPE_TEXT;
43 cycrow 7785
	if ( File.GetBaseName().Compare("conversations") )
7786
		return FILETYPE_TEXT;
1 cycrow 7787
	if ( basename.Length() <= 4 && basename.IsNumber() && (File.CheckFileExtension("xml") || File.CheckFileExtension("pck")) )
7788
		return FILETYPE_TEXT;
7789
	// X2/X3 text file
7790
	if ( basename.Length() >= 5 && basename.Length() <= 8 && File.GetBaseName().ToInt() )
7791
		return FILETYPE_TEXT;
7792
	if ( filetype == FILETYPE_TEXT ) // should no longer be anything text
7793
		return FILETYPE_SCRIPT;
7794
	if ( File.CheckFileExtension("wav") || File.CheckFileExtension("mp3") )
7795
		return FILETYPE_SOUND;
7796
	return filetype;
7797
}
7798
 
7799
void CPackages::RemoveFailedFiles()
7800
{
7801
	for ( SStringList *str = m_lNonRemovedFiles.Head(); str; str = str->next )
7802
	{
7803
		if ( CFileIO(str->str).Remove() )
7804
			str->remove = true;
7805
	}
7806
 
7807
	m_lNonRemovedFiles.RemoveMarked();
7808
}
7809
 
35 cycrow 7810
CXspFile *CPackages::extractShip(const Utils::String &sCatFile, const Utils::String &sId, CProgressInfo *progress)
1 cycrow 7811
{
35 cycrow 7812
	CVirtualFileSystem *pVfs = new CVirtualFileSystem();
7813
	if ( !pVfs->addMod(sCatFile) ) {
7814
		delete pVfs;
1 cycrow 7815
		return NULL;
35 cycrow 7816
	}
1 cycrow 7817
 
7818
	CXspFile *newShip = new CXspFile;
35 cycrow 7819
	if ( !newShip->extractShip(pVfs, sId, progress) ) {
1 cycrow 7820
		delete newShip;
35 cycrow 7821
		newShip = NULL;
1 cycrow 7822
	}
7823
 
35 cycrow 7824
	delete pVfs;
7825
 
1 cycrow 7826
	return newShip;
7827
}
7828
 
7829
CyStringList *CPackages::GetMergedFiles(CCatFile *cat1, CCatFile *cat2)
7830
{
7831
	CyStringList *list = new CyStringList;
7832
 
7833
	// first add all files from the "primary" mod
7834
	for ( SInCatFile *f = cat1->GetFiles()->First(); f; f = cat1->GetFiles()->Next() )
7835
		list->PushBack(f->sFile.findreplace("\\", "/"), "1");
7836
 
7837
	// now add the ones from the secondary
7838
	for ( SInCatFile *f = cat2->GetFiles()->First(); f; f = cat2->GetFiles()->Next() )
7839
	{
7840
		CyString sFile = f->sFile.findreplace("\\", "/");
7841
		// if its found on the 2nd list, dont add, just adjust the type
7842
		SStringList *found = list->FindString(sFile);
7843
		if ( found )
7844
			found->data = "-1"; // found in both
7845
		else
7846
			list->PushBack(sFile, "2");
7847
	}
7848
 
7849
	return list;
7850
}
7851
 
7852
bool CPackages::CanWeMerge(CyString file)
7853
{
7854
	return CModDiff::CanBeDiffed(file);
7855
}
7856
 
7857
bool CPackages::NeedToMerge(CyString file)
7858
{
7859
	CyString firstDir = file.GetToken("/", 1, 1);
7860
	if ( firstDir.Compare("t") )
7861
		return true;
7862
	if ( firstDir.Compare("types") )
7863
		return true;
7864
	if ( firstDir.Compare("maps") )
7865
		return true;
7866
 
7867
	return false;
7868
}
7869
 
7870
bool CPackages::MergeMods(CCatFile *mod1, CCatFile *mod2, CyString outFile, CyStringList *cantMerge)
7871
{
7872
	CyStringList *list = this->GetMergedFiles(mod1, mod2);
7873
	if ( !list )
7874
		return false;
7875
 
7876
	CCatFile newCat;
7877
	if ( newCat.Open(outFile, this->GetAddonDir()) != CATERR_CREATED )
7878
		return false;
7879
 
7880
	// add all the files to the new mod first
7881
	CyStringList conflicts;
7882
	for ( SStringList *str = list->Head(); str; str = str->next )
7883
	{
7884
		int status = str->data.ToInt();
7885
		if ( status == 1 )
7886
		{
7887
			if ( !newCat.WriteFromCat(mod1, str->str) )
7888
			{
7889
				if ( cantMerge )
7890
					cantMerge->PushBack(str->str, "1");
7891
			}
7892
		}
7893
		else if ( status == 2 )
7894
		{
7895
			if ( !newCat.WriteFromCat(mod2, str->str) )
7896
			{
7897
				if ( cantMerge )
7898
					cantMerge->PushBack(str->str, "2");
7899
			}
7900
		}
7901
		else if ( status == -1 )
7902
		{
7903
			if ( this->NeedToMerge(str->str) )
7904
			{
7905
				if ( this->CanWeMerge(str->str) )
7906
					conflicts.PushBack(str->str);
7907
				else if ( cantMerge )
7908
					cantMerge->PushBack(str->str, "-1");
7909
			}
7910
			else
7911
			{
7912
				if ( !newCat.WriteFromCat(mod1, str->str) )
7913
				{
7914
					if ( cantMerge )
7915
						cantMerge->PushBack(str->str, "1");
7916
				}
7917
			}
7918
		}
7919
	}
7920
 
7921
	delete list;
7922
 
7923
	/* 
7924
		Merging Files
7925
 
7926
		* Text Files: Join all text entries into a single file (excluding page 17)
7927
		* Weapons: TBullets and TLaser (grab matching entrys from text files and adjust ids)
7928
	*/
7929
	// new merge the conflicting files
7930
	// first the text files
7931
//	CyStringList *text = this->MergeTextFiles(conflicts, mod1, mod2);
7932
//	delete text;
7933
 
7934
	// write the cat file when we're done
7935
	if ( !newCat.WriteCatFile() )
7936
		return false;
7937
 
7938
	return true;
7939
}
7940
 
7941
/**
7942
 * Gets the file list from a mod that might have compatability problems
7943
 *
7944
 * This includes all types and text files
7945
 *
7946
 * Returns true if it finds any files
7947
 */
7948
bool CPackages::GetModCompatabilityList(C_File *file, CyStringList *list)
7949
{
7950
	// not a valid file
7951
	if ( !file ) return false;
7952
	if ( file->GetFileType() != FILETYPE_MOD ) return false;
7953
	if ( !file->GetFileExt().Compare("cat") ) return false;
7954
 
7955
	// we need to read the file list for the mod
7956
	CCatFile cat;
7957
	if ( cat.Open(file->GetFilePointer(), this->GetAddonDir(), CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
7958
	{
7959
		for ( int i = 0; i < cat.GetNumFiles(); i++ )
7960
		{
7961
			SInCatFile *f = cat.GetFile(i);
7962
			CyString filename = f->sFile;
7963
			filename = filename.FindReplace("\\", "/");
50 cycrow 7964
			bool found = false;
1 cycrow 7965
			if ( filename.Left(2).Compare("t/") || filename.Left(6).Compare("types/") )
50 cycrow 7966
				found = true;
7967
			else if ( filename.Left(8).Compare("addon/t/") || filename.Left(12).Compare("addon/types/") )
7968
				found = true;
7969
 
7970
			if ( found ) {
1 cycrow 7971
				if ( list )
7972
					list->PushBack(filename, CyString(f->lSize));
7973
				else
7974
					return true;
7975
			}
7976
		}
7977
	}
7978
 
7979
	if ( list && !list->Empty() )
7980
		return true;
7981
 
7982
	return false;
7983
}
7984
 
7985
/**
7986
 * Gets the files that are not compatable with each other
7987
 *
7988
 * Returns true if theres any files that are not compatable
7989
 *
7990
 * If list is specified, fills up with all files that were found
7991
 */
7992
bool CPackages::CheckCompatabilityBetweenModFiles(C_File *from, C_File *to, CyStringList *list)
7993
{
7994
	// not a valid file
7995
	if ( !from || !to ) return false;
7996
	if ( from->GetFileType() != FILETYPE_MOD ) return false;
7997
	if ( to->GetFileType() != FILETYPE_MOD ) return false;
7998
	if ( !from->GetFileExt().Compare("cat") ) return false;
7999
	if ( !to->GetFileExt().Compare("cat") ) return false;
8000
 
8001
	// get file lists from each file
8002
	CyStringList fromList;
8003
	if ( GetModCompatabilityList(from, &fromList) )
8004
	{
8005
		CyStringList toList;
8006
		if ( GetModCompatabilityList(to, &toList) )
8007
		{
8008
			// both have files we need to check, compare them
8009
			for ( SStringList *str = fromList.Head(); str; str = str->next )
8010
			{
8011
				CyString fromFile = str->str;
8012
				fromFile = fromFile.FindReplace("\\", "/");
8013
				fromFile = fromFile.FindReplace("//", "/");
8014
				for ( SStringList *toStr = toList.Head(); toStr; toStr = toStr->next )
8015
				{
8016
					CyString toFile = toStr->str;
8017
					toFile = toFile.FindReplace("\\", "/");
8018
					toFile = toFile.FindReplace("//", "/");
8019
					if ( fromFile.Compare(toFile) )
8020
					{
8021
						if ( list )
8022
							list->PushBack(from->GetFilename() + "::" + str->str, to->GetFilename() + "::" + toStr->str);
8023
						else
8024
							return true;
8025
					}
8026
				}
8027
			}
8028
		}
8029
	}
8030
 
8031
	if ( list && !list->Empty() )
8032
		return true;
8033
 
8034
	return false;
8035
}
8036
 
8037
bool CPackages::CheckCompatabilityBetweenMods(CBaseFile *from, CBaseFile *to, CyStringList *list)
8038
{
8039
	if ( !from || !to ) return false;
8040
	if ( !from->IsEnabled() || !to->IsEnabled() ) return false;
8041
	if ( !from->AnyFileType(FILETYPE_MOD) ) return false;
8042
	if ( !to->AnyFileType(FILETYPE_MOD) ) return false;
8043
 
8044
	if ( from == to ) return false; // cant have incompatabilities to itself
8045
 
8046
	int count = 0;
8047
	for ( C_File *f = from->GetFirstFile(FILETYPE_MOD); f; f = from->GetNextFile(f) )
8048
	{
8049
		if ( !f->IsFakePatch() ) continue;
8050
		if ( f->GetFileExt().Compare("dat") ) continue;
8051
 
8052
		for ( C_File *compareFile = to->GetFirstFile(FILETYPE_MOD); compareFile; compareFile = to->GetNextFile(compareFile) )
8053
		{
8054
			if ( compareFile == f ) continue; // same file we're checking against
8055
			if ( !compareFile->IsFakePatch() ) continue;
8056
			if ( compareFile->GetFileExt().Compare("dat") ) continue;
8057
 
8058
			// now we have to files to compare
8059
			if ( CheckCompatabilityBetweenModFiles(f, compareFile, list) )
8060
				++count;
8061
		}
8062
	}
8063
 
8064
	if ( count )
8065
		return true;
8066
 
8067
	return false;
8068
}
8069
 
8070
int CPackages::CheckCompatabilityAgainstPackages(CBaseFile *newFile, CyStringList *list, CLinkList<CBaseFile> *packages)
8071
{
8072
	if ( !newFile->IsEnabled() ) return 0;
8073
	if ( !newFile->AnyFileType(FILETYPE_MOD) ) return 0;
8074
 
8075
	// we need to extract all mod files
8076
	for ( CListNode<C_File> *fNode = newFile->GetFileList()->Front(); fNode; fNode = fNode->next() )
8077
	{
8078
		C_File *f = fNode->Data();
8079
		if ( f->GetFileType() != FILETYPE_MOD ) continue;
8080
		if ( !f->IsFakePatch() ) continue;
8081
		if ( !f->CheckFileExt("cat") ) continue;
8082
 
8083
		if ( newFile->ExtractFile(f, m_sTempDir) )
8084
			f->SetFullDir(m_sTempDir);
8085
	}
8086
 
8087
	// compare mod files against all installed packages
8088
	int count = 0;
8089
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
8090
	{
8091
		if ( !node->Data() ) continue;
8092
		CBaseFile *p = node->Data();
8093
		if ( !p->IsEnabled() ) continue;
8094
		if ( !p->AnyFileType(FILETYPE_MOD) ) continue;
8095
 
8096
		if ( this->IsSamePackage(p, newFile) ) continue; // dont include self
8097
 
8098
		if ( CheckCompatabilityBetweenMods(newFile, p, list) )
8099
		{
8100
			++count;
8101
			if ( packages && !packages->FindData(p) )
8102
				packages->push_back(p);
8103
		}
8104
	}
8105
 
8106
	for ( CListNode<C_File> *fNode = newFile->GetFileList()->Front(); fNode; fNode = fNode->next() )
8107
	{
8108
		C_File *f = fNode->Data();
8109
		CFileIO(f->GetFilePointer()).Remove();
8110
		f->SetFullDir("");
8111
	}
8112
 
8113
	return count;
8114
}
8115
 
8116
bool CPackages::IsSamePackage(CBaseFile *p1, CBaseFile *p2)
8117
{
8118
	if ( !p1 || !p2 ) return false;
8119
	if ( p1 == p2 ) return true;
8120
 
50 cycrow 8121
	if ( p1->name().Compare(p2->name()) && p1->author().Compare(p2->author()) )
1 cycrow 8122
		return true;
8123
	return false;
8124
}
8125
 
8126
void CPackages::ApplyFakePatchOrder(CyStringList *list)
8127
{
8128
	if ( !list ) return;
8129
	m_lFakePatchOrder.Clear();
8130
	for ( SStringList *str = list->Head(); str; str = str->next )
8131
		m_lFakePatchOrder.PushBack(str->str, str->data);
8132
}
8133
 
8134
SAvailablePackage *CPackages::CreateAvailablePackageData(CBaseFile *package)
8135
{
8136
	if ( !package ) return NULL;
8137
 
8138
	SAvailablePackage *p = new SAvailablePackage;
8139
 
8140
	for ( CListNode<SGameCompat> *node = package->GetGameCompatabilityList()->Front(); node; node = node->next() ) {
8141
		SGameCompat *gc = new SGameCompat;
8142
		gc->iGame = node->Data()->iGame;
8143
		gc->iVersion = node->Data()->iVersion;
8144
		gc->sVersion = node->Data()->sVersion;
8145
		p->lGames.push_back(gc);
8146
	}
8147
	p->bSigned = package->IsSigned();
46 cycrow 8148
	p->iChanging = package->gameChanging();
8149
	p->iEase = package->easeOfUse();
48 cycrow 8150
	p->iPluginType = package->pluginType();
46 cycrow 8151
	p->iRec = package->recommended();
1 cycrow 8152
	p->iScriptType = -1;
8153
	if ( package->GetType() == TYPE_XSP )
8154
		p->iType = PACKAGETYPE_SHIP;
8155
	else if ( package->IsMod() )
8156
		p->iType = PACKAGETYPE_MOD;
8157
	else 
8158
	{
8159
		p->iType = ((CSpkFile *)package)->GetPackageType();
8160
		p->iScriptType = ((CSpkFile *)package)->GetScriptType();
8161
	}
50 cycrow 8162
	p->sAuthor = package->author();
48 cycrow 8163
	p->sDesc = package->description().findReplace("\n", "::newline::");
50 cycrow 8164
	p->sName = package->name();
8165
	p->sUpdated = package->creationDate();
8166
	p->sVersion = package->version();
8167
	p->sFilename = CFileIO(package->filename()).GetFilename();
1 cycrow 8168
 
8169
	return p;
8170
}
8171
 
8172
CyString CPackages::FormatAvailablePackageData(CBaseFile *package)
8173
{
8174
	SAvailablePackage *p = CPackages::CreateAvailablePackageData(package);
8175
	CyString ret = CPackages::FormatAvailablePackageData(p);
8176
	delete p;
8177
	return ret;
8178
}
8179
 
8180
CyString CPackages::FormatAvailablePackageData(SAvailablePackage *package)
8181
{
8182
	CyString ret = (long)package->iType;
8183
 
8184
	CyString gameCompat;
8185
	for ( CListNode<SGameCompat> *node = package->lGames.Front(); node; node = node->next() ) {
8186
		if ( !gameCompat.Empty() )
8187
			gameCompat += "!";
8188
		gameCompat += CyString::Number(node->Data()->iGame);
8189
	}
8190
 
8191
	if ( gameCompat.Empty() )
8192
		gameCompat = "0";
8193
 
8194
	ret.AddToken("::", gameCompat);
8195
	ret.AddToken("::", package->sName);
8196
	ret.AddToken("::", package->sAuthor);
8197
	ret.AddToken("::", package->sVersion);
8198
	ret.AddToken("::", package->sUpdated);
8199
	ret.AddToken("::", package->sFilename);
8200
	ret.AddToken("::", CyString::Number(package->iEase));
8201
	ret.AddToken("::", CyString::Number(package->iChanging));
8202
	ret.AddToken("::", CyString::Number(package->iRec));
8203
	ret.AddToken("::", CyString::Number(package->iPluginType));
8204
	ret.AddToken("::", CyString::Number(package->iScriptType));
8205
	ret.AddToken("::", (package->bSigned) ? "1" : "0");
8206
	ret.AddToken("::", package->sDesc);
8207
 
8208
	return ret;
8209
}
8210
 
8211
void CPackages::ParseAvailablePackage(CyString str, CyString webaddress)
8212
{
8213
	// first check game
8214
	int game = str.GetToken("::", 2, 2).ToInt();
8215
	if ( game && m_iGame && m_iGame != game )
8216
		return;
8217
 
8218
	int num = 0;
8219
	CyString *tok = str.SplitToken("::", &num);
8220
	if ( !num || !tok ) return;
8221
	if ( num < 7 ) { CLEANSPLIT(tok, num); return; }
8222
 
8223
	SAvailablePackage *p = new SAvailablePackage;
8224
	p->iType = tok[0].ToInt();
8225
	p->bSigned = false;
8226
 
8227
	CyString gameCompat = tok[1];
8228
	if ( gameCompat.IsIn("!") ) {
8229
		int maxSplit = 0;
8230
		CyString *games = gameCompat.SplitToken("!", &maxSplit);
8231
		if ( games && maxSplit ) {
8232
			for ( int i = 0; i < maxSplit; i++ ) {
8233
				SGameCompat *gc = new SGameCompat;
8234
				gc->iVersion = 0;
8235
				gc->iGame = games[i].ToInt();
8236
				p->lGames.push_back(gc);
8237
			}
8238
		}
8239
		CLEANSPLIT(games, maxSplit);
8240
	}
8241
	else {
8242
		SGameCompat *gc = new SGameCompat;
8243
		gc->iVersion = 0;
8244
		gc->iGame = gameCompat.ToInt();
8245
		p->lGames.push_back(gc);
8246
	}
8247
	p->sName = tok[2];
8248
	p->sAuthor = tok[3];
8249
	p->sVersion = tok[4];
8250
	p->sUpdated = tok[5];
8251
	p->sFilename = tok[6];
8252
 
8253
	if ( !webaddress.Empty() )
8254
		p->sFilename = webaddress + "/" + p->sFilename;
8255
 
8256
	p->iChanging = p->iEase = p->iPluginType = p->iRec = p->iScriptType = -1;
8257
 
8258
	if ( num >= 12 )
8259
	{
8260
		p->iEase = tok[7].ToInt();
8261
		p->iChanging = tok[8].ToInt();
8262
		p->iRec = tok[9].ToInt();
8263
		p->iPluginType = tok[10].ToInt();
8264
		p->iScriptType = tok[11].ToInt();
8265
		if ( num > 12 ) {
8266
			if ( num > 13 ) {
8267
				p->sDesc = str.GetToken("::", 14); 
8268
				p->bSigned = str.GetToken("::", 13).ToBool();
8269
			}
8270
			else
8271
				p->sDesc = str.GetToken("::", 13); 
8272
		}
8273
	}
8274
	else if ( num > 7 )
8275
		p->sDesc = str.GetToken("::", 8); 
8276
 
8277
	if ( !p->sDesc.Empty() )
8278
		p->sDesc.FindReplace("::newline::", "\\n");
8279
 
8280
	AddAvailablePackage(p);
8281
 
8282
	CLEANSPLIT(tok, num);
8283
}
8284
 
8285
SAvailablePackage *CPackages::FindAvailablePackage(CyString &filename)
8286
{
8287
	for ( CListNode<SAvailablePackage> *node = m_lAvailablePackages.Front(); node; node = node->next() )
8288
	{
8289
		if ( node->Data()->sFilename.Compare(filename) )
8290
			return node->Data();
8291
	}
8292
	return NULL;
8293
}
8294
 
8295
bool CPackages::AddAvailablePackage(SAvailablePackage *package)	
8296
{ 
8297
	if ( !package->lGames.empty() ) {
8298
		bool found = false;
8299
		for ( CListNode<SGameCompat> *node = package->lGames.Front(); node; node = node->next() ) {
8300
			if ( !node->Data()->iGame || node->Data()->iGame == m_iGame ) {
8301
				found = true;
8302
				break;
8303
			}
8304
		}
8305
 
8306
		if ( !found )
8307
			return false;
8308
	}
8309
 
8310
	SAvailablePackage *p = FindAvailablePackage(package->sFilename);
8311
	if ( p )
8312
		m_lAvailablePackages.remove(p);
8313
	m_lAvailablePackages.push_back(package); 
8314
 
8315
	return true;
8316
}
8317
 
8318
bool CPackages::AnyAvailablePackages(int type)
8319
{
8320
	if ( type == -1 ) return !m_lAvailablePackages.empty();
8321
 
8322
	for ( CListNode<SAvailablePackage> *node = m_lAvailablePackages.Front(); node; node = node->next() )
8323
	{
8324
		if ( node->Data()->iType == type )
8325
			return true;
8326
	}
8327
 
8328
	return false;
8329
}
8330
 
8331
int CPackages::FindAllServers(CyStringList *list)
8332
{
8333
	for ( CListNode<CBaseFile> *node = m_lPackages.Front(); node; node = node->next() )
8334
	{
49 cycrow 8335
		if ( !node->Data()->webAddress().empty() )
8336
			list->PushBack(CyString(node->Data()->webAddress()), true);
1 cycrow 8337
		if ( node->Data()->AnyWebMirrors() )
8338
		{
8339
			for ( SStringList *str = node->Data()->GetWebMirrors()->Head(); str; str = str->next )
8340
				list->PushBack(str->str, true);
8341
		}
8342
	}
8343
 
8344
	return list->Count();
8345
}
8346
 
8347
void CPackages::ReadArchiveData(CyString filename, CBaseFile *archive)
8348
{
8349
	size_t size;
8350
	char *data = CFileIO(filename).ReadToData(&size);
8351
	if ( size && data )
8352
		ReadArchiveData(data, size, archive);
8353
}
8354
 
8355
void CPackages::ReadArchiveData(const char *buf, size_t len, CBaseFile *archive)
8356
{
8357
	CyString data(buf);
8358
	int max;
8359
	CyString *str = data.SplitToken("\n", &max);
8360
	if ( str && max )
8361
	{
8362
		for ( int i = 0; i < max; i++ )
8363
		{
8364
			CyString line = str[i];
8365
			if ( line.Empty() )
8366
				continue;
8367
 
8368
			// filter out any spaces, tabs in front
8369
			line.RemoveChar('\t');
8370
			line.RemoveChar('\r');
8371
			CyString linenospace = line;
8372
			linenospace.RemoveFirstSpace();
8373
			if ( linenospace.Empty() )
8374
				continue;
8375
 
8376
			// check for any comments
8377
			if ( linenospace.Left(2) == "//" )
8378
				continue;
8379
			if ( linenospace[0] == '#' )
8380
				continue;
8381
 
8382
			// all commands start with a keyword followed by a colon, if one doesn't exist, it cant be a valid line
8383
			if ( !line.IsIn(':') )
8384
				continue;
8385
 
8386
			CyString first = line.GetToken(":", 1, 1);
8387
			CyString rest = line.GetToken(":", 2).RemoveFirstSpace();
8388
 
8389
			CyString checkType = first;
8390
			bool shared = false;
8391
			if ( checkType.Left(6).Compare("Shared") )
8392
			{
8393
				checkType = first.Right(-6);
8394
				shared = true;
8395
			}
8396
 
8397
			// now check type name
8398
			int filetype = GetFileTypeFromString(checkType);
8399
			if ( filetype == -1 )
14 cycrow 8400
				archive->LoadPackageData(first.ToString(), rest.ToString());
1 cycrow 8401
		}
8402
	}
8403
 
8404
	CLEANSPLIT(str, max)
8405
}
8406
 
43 cycrow 8407
CBaseFile *CPackages::_archive_fromRar(CyString filename, bool toInstall )
1 cycrow 8408
{
8409
	// make sure we can open the zip file
8410
	CBaseFile *archive = NULL;
8411
#ifdef _RAR
43 cycrow 8412
	HANDLE hArcData;
8413
	int RHCode,PFCode;
8414
	char CmtBuf[16384];
8415
	struct RARHeaderDataEx HeaderData;
8416
	struct RAROpenArchiveDataEx OpenArchiveData;
1 cycrow 8417
 
43 cycrow 8418
	// find the pluginmanager text to covnert to spkfile
8419
	if ( toInstall ) {
1 cycrow 8420
		memset(&OpenArchiveData,0,sizeof(OpenArchiveData));
8421
		OpenArchiveData.ArcName=(char *)filename.c_str();
8422
		OpenArchiveData.CmtBuf=CmtBuf;
8423
		OpenArchiveData.CmtBufSize=sizeof(CmtBuf);
43 cycrow 8424
		OpenArchiveData.OpenMode=RAR_OM_LIST;
1 cycrow 8425
		hArcData=RAROpenArchiveEx(&OpenArchiveData);
8426
 
43 cycrow 8427
		if (OpenArchiveData.OpenResult!=0) return NULL;
1 cycrow 8428
 
8429
		HeaderData.CmtBuf=NULL;
8430
		memset(&OpenArchiveData.Reserved,0,sizeof(OpenArchiveData.Reserved));
8431
 
43 cycrow 8432
		while ((RHCode=RARReadHeaderEx(hArcData,&HeaderData))==0) {
8433
			if ( CyString(HeaderData.FileName).Compare("pluginmanager.txt") ) {
8434
				toInstall = false;
1 cycrow 8435
				break;
8436
			}
43 cycrow 8437
		}
8438
		RARCloseArchive(hArcData);
8439
	}
1 cycrow 8440
 
43 cycrow 8441
	if ( toInstall )
8442
		archive = new CArchiveFile(); // just installing an archive file
8443
	else
8444
		archive = new CSpkFile(); // converting to a spk file
8445
 
8446
	memset(&OpenArchiveData,0,sizeof(OpenArchiveData));
8447
	OpenArchiveData.ArcName=(char *)filename.c_str();
8448
	OpenArchiveData.CmtBuf=CmtBuf;
8449
	OpenArchiveData.CmtBufSize=sizeof(CmtBuf);
8450
	OpenArchiveData.OpenMode=RAR_OM_EXTRACT;
8451
	OpenArchiveData.UserData=EXTRACT;
8452
	hArcData=RAROpenArchiveEx(&OpenArchiveData);
8453
 
8454
	if (OpenArchiveData.OpenResult!=0) return NULL;
8455
 
8456
	HeaderData.CmtBuf=NULL;
8457
	memset(&OpenArchiveData.Reserved,0,sizeof(OpenArchiveData.Reserved));
8458
 
8459
	bool error = false;
8460
	CyString extractedFile = CDirIO(m_sTempDir).File("extracted.tst").findreplace("/", "\\").findreplace("\\\\", "\\");
8461
	while ((RHCode=RARReadHeaderEx(hArcData,&HeaderData))==0)
8462
	{
8463
		CyString fileName = HeaderData.FileName;
8464
 
8465
		if ( HeaderData.FileAttr == 16 )
8466
			continue;
8467
		wchar_t wText[200];
8468
		::MultiByteToWideChar(CP_ACP, NULL, (char *)extractedFile.c_str(), -1, wText, extractedFile.Length() + 1);
8469
		PFCode=RARProcessFileW(hArcData, RAR_EXTRACT, NULL, NULL);
8470
		if (PFCode!=0)
8471
		{
8472
			error = true;
8473
			break;
8474
		}
8475
 
8476
		CFileIO File(fileName);
8477
		if ( File.Exists() )
8478
		{
8479
			if ( fileName.Compare("pluginmanager.txt") )
8480
				this->ReadArchiveData(File.GetFullFilename(), archive);
8481
			else
1 cycrow 8482
			{
43 cycrow 8483
				CyString extradir;
8484
				int type = SPK::GetAutomaticFiletype(fileName, &extradir, true);
8485
				// check for special file types
8486
				C_File *f = NULL;
1 cycrow 8487
 
43 cycrow 8488
				if ( type == FILETYPE_SCRIPT_UNINSTALL ) {
45 cycrow 8489
					f = archive->AddFile(CFileIO(fileName).GetFilename(), "", FILETYPE_SCRIPT);
43 cycrow 8490
					if ( f ) {
8491
						f->ReadFromFile(File.GetFullFilename());
17 cycrow 8492
					}
43 cycrow 8493
					type = FILETYPE_UNINSTALL;
1 cycrow 8494
				}
8495
 
43 cycrow 8496
				if ( type == -1 )
8497
					f = archive->AddFile(CFileIO(fileName).GetFilename(), CFileIO(fileName).GetDir(), FILETYPE_EXTRA);
8498
				else
8499
					f = archive->AddFile(CFileIO(fileName).GetFilename(), extradir, type);
8500
				f->ReadFromFile(File.GetFullFilename());
1 cycrow 8501
			}
43 cycrow 8502
 
8503
			File.Remove();
1 cycrow 8504
		}
43 cycrow 8505
	}
1 cycrow 8506
 
43 cycrow 8507
	RARCloseArchive(hArcData);
1 cycrow 8508
 
43 cycrow 8509
	if ( error )
8510
	{
8511
		delete archive;
8512
		archive = NULL;
8513
	}
1 cycrow 8514
#endif
8515
 
43 cycrow 8516
	return archive;
8517
}
1 cycrow 8518
 
43 cycrow 8519
CBaseFile *CPackages::_archive_fromZip(CyString filename, bool toInstall)
8520
{
8521
	CBaseFile *archive = NULL;
1 cycrow 8522
 
43 cycrow 8523
	TCHAR buf[5000];
8524
	wsprintf(buf, L"%hs", filename.c_str());
8525
	HZIP hz = OpenZip(buf, 0);
8526
	if ( !hz ) 
8527
		return NULL;
1 cycrow 8528
 
43 cycrow 8529
	int index;
8530
	// move the files from the zip to the package
8531
	ZIPENTRY ze;
8532
	if ( FindZipItem(hz, L"pluginmanager.txt", true, &index, &ze) == Z_OK )
8533
		toInstall = false;
8534
	CloseZip(hz);
1 cycrow 8535
 
43 cycrow 8536
	hz = OpenZip(buf, 0);
8537
	if ( !hz ) 
8538
		return NULL;
8539
 
8540
	// create the correct package
8541
	if ( toInstall )
8542
		archive = new CArchiveFile(); // just installing an archive file
8543
	else
8544
		archive = new CSpkFile(); // converting to a spk file
8545
 
8546
	GetZipItem(hz, -1, &ze);
8547
	int numitems = ze.index;
8548
 
8549
	bool error = false;
8550
	for ( int zi = 0; zi < numitems; zi++ )
8551
	{
8552
		ZIPENTRY ze;
8553
		if ( GetZipItem(hz, zi, &ze) != Z_OK )
1 cycrow 8554
		{
43 cycrow 8555
			error = true;
8556
			break;
8557
		}
1 cycrow 8558
 
43 cycrow 8559
		if ( ze.attr & FILE_ATTRIBUTE_DIRECTORY )
8560
			continue; // dont do directories
1 cycrow 8561
 
8562
 
43 cycrow 8563
		char *iBuf = new char[ze.unc_size];
8564
		UnzipItem(hz, zi, iBuf, ze.unc_size);
1 cycrow 8565
 
43 cycrow 8566
		CyString Name(ze.name);
1 cycrow 8567
 
43 cycrow 8568
		// if its the data file, dont add it, but extract to get settings from
8569
		if ( Name.Compare("pluginmanager.txt") )
8570
		{
8571
			this->ReadArchiveData(iBuf, ze.unc_size, archive);
8572
			delete[] iBuf;
8573
		}
8574
		else
8575
		{
8576
			CyString extradir;
8577
			int type = SPK::GetAutomaticFiletype(Name, &extradir, true);
1 cycrow 8578
 
43 cycrow 8579
			C_File *f = NULL;
17 cycrow 8580
 
43 cycrow 8581
			// check for special file types
8582
			if ( type == FILETYPE_SCRIPT_UNINSTALL ) {
8583
				f = archive->AddFile(CFileIO(Name).GetFilename(), CFileIO(Name).GetDir(), FILETYPE_SCRIPT);
8584
				if ( f ) {
8585
					f->copyData((const unsigned char *)iBuf, ze.unc_size);
17 cycrow 8586
				}
43 cycrow 8587
				type = FILETYPE_UNINSTALL;
8588
			}
17 cycrow 8589
 
43 cycrow 8590
			if ( type == -1 )
8591
				f = archive->AddFile(CFileIO(Name).GetFilename(), CFileIO(Name).GetDir(), FILETYPE_EXTRA);
8592
			else
8593
				f = archive->AddFile(CFileIO(Name).GetFilename(), extradir, type);
1 cycrow 8594
 
43 cycrow 8595
			if ( f )
8596
				f->SetData((const unsigned char *)iBuf, ze.unc_size);
8597
			else
8598
				delete[] iBuf;
1 cycrow 8599
		}
43 cycrow 8600
	}
1 cycrow 8601
 
43 cycrow 8602
	CloseZip(hz);
1 cycrow 8603
 
43 cycrow 8604
	if ( error )
8605
	{
8606
		delete archive;
8607
		archive = NULL;
1 cycrow 8608
	}
43 cycrow 8609
	return archive;
8610
}
1 cycrow 8611
 
43 cycrow 8612
CBaseFile *CPackages::CreateFromArchive(CyString filename, bool toInstall )
8613
{
8614
	// make sure we can open the zip file
8615
	CBaseFile *archive = NULL;
8616
	if ( CFileIO(filename).CheckFileExtension("rar") )
8617
		archive = this->_archive_fromRar(filename, toInstall);
8618
	else if ( CFileIO(filename).CheckFileExtension("zip") )
8619
		archive = this->_archive_fromZip(filename, toInstall);
8620
 
8621
	if ( archive ) {
50 cycrow 8622
		archive->setFilename(CFileIO(filename).ChangeFileExtension("spk").ToString());
1 cycrow 8623
		if ( toInstall )
50 cycrow 8624
			archive->setName(CFileIO(filename).GetFilename().ToString());
1 cycrow 8625
		else
50 cycrow 8626
			archive->setName(CFileIO(filename).GetBaseName().ToString());
1 cycrow 8627
	}
8628
 
8629
	return archive;
8630
}
8631
 
8632
CyString CPackages::CreateFromPackagerScript(CyString filename)
8633
{
8634
	CyString curDir = CFileIO(filename).GetDir();
8635
	CyStringList variables;
8636
	variables.PushBack("$PATH", curDir);
8637
	CPackages p;
8638
	CBaseFile *package = p.LoadPackagerScript(filename, NULL, NULL, NULL, &variables);
8639
 
8640
	if ( !package )
8641
		return NullString;
8642
 
50 cycrow 8643
	CyString saveto = package->filename();
1 cycrow 8644
	saveto = saveto.FindReplace("$DEFAULTDIR", curDir + "/");
8645
	saveto = saveto.FindReplace("$PATH", curDir);
8646
	saveto = saveto.FindReplace("\\", "/");
8647
	saveto = saveto.FindReplace("//", "/");
8648
	if ( !saveto.Right(4).Compare(".spk") && package->GetType() != TYPE_XSP )
8649
		saveto += ".spk";
8650
	else if ( !saveto.Right(4).Compare(".xsp") && package->GetType() == TYPE_XSP )
8651
		saveto += ".xsp";
8652
 
8653
	// write script
8654
	if ( package->WriteFile(saveto) )
8655
	{
8656
		if ( package->AutoGenerateUpdateFile() )
8657
			package->CreateUpdateFile(CFileIO(saveto).GetDir());
8658
		return saveto;
8659
	}
8660
 
8661
	return NullString;
8662
}
8663
 
8664
int CPackages::GeneratePackageUpdateData(CyString dir, bool includeSingle)
8665
{
8666
	CyStringList filedata;
8667
 
8668
	CPackages packages;
8669
 
8670
	CDirIO Dir(dir);
8671
	for ( int i = 0; i < 2; i++ )
8672
	{
8673
		CyString pattern;
8674
		if ( i == 0 ) pattern = "*.spk";
8675
		else if ( i == 1 ) pattern = ".xsp";
8676
		else break;
8677
 
8678
		CyStringList *files = Dir.DirList("", pattern);
8679
		if ( files )
8680
		{
8681
			for ( SStringList *node = files->Head(); node; node = node->next )
8682
			{
8683
				int error = 0;
8684
				CBaseFile *p = packages.OpenPackage(Dir.File(node->str), &error, 0, SPKREAD_NODATA);
8685
				if ( !p )
8686
					continue;
8687
 
8688
				if ( includeSingle )
8689
					p->CreateUpdateFile(dir);
8690
				filedata.PushBack(CPackages::FormatAvailablePackageData(p));
8691
				delete p;
8692
			}
8693
			delete files;
8694
		}
8695
	}
8696
 
8697
	if ( !filedata.Empty() )
8698
	{
8699
		CFileIO File(dir + "/xpackagedata.dat");
8700
		if ( File.WriteFile(&filedata) )
8701
			return filedata.Count();
8702
	}
8703
 
8704
	return 0;
8705
}
8706
 
8707
int CPackages::VerifyInstalledFiles(CyStringList *missingFiles, bool getPackages)
8708
{
8709
	int count = 0;
8710
	for ( CListNode<C_File> *fn = m_lFiles.Front(); fn; fn = fn->next() )
8711
	{
8712
		C_File *f = fn->Data();
8713
		bool exists = false;
8714
		if ( f->GetFilePointer().IsIn("::") ) {
8715
			CyString modFile = f->GetFilePointer().GetToken("::", 1, 1);
8716
			CyString file = f->GetFilePointer().GetToken("::", 2, 2);
8717
 
8718
			if ( CFileIO(modFile).Exists() ) {
8719
				CCatFile catFile;
8720
				if ( catFile.Open(modFile, "", CATREAD_CATDECRYPT, false) == CATERR_NONE ) {
8721
					if ( catFile.FindData(file) )
8722
						exists = true;
8723
				}
8724
			}
8725
		}
8726
		else {
8727
			exists = CFileIO(f->GetFilePointer()).Exists();
8728
		}
8729
 
8730
		if ( !exists )
8731
		{
8732
			++count;
8733
			if ( missingFiles )
8734
			{
8735
				CyString packages;
8736
				if ( getPackages )
8737
				{
8738
					for ( CListNode<CBaseFile> *p = m_lPackages.Front(); p; p = p->next() )
8739
					{
8740
						CBaseFile *package = p->Data();
8741
						if ( package->IsFileAdded(f) )
8742
						{
8743
							if ( !packages.Empty() )
8744
								packages += "\n";
8745
							packages += package->GetFullPackageName(m_iLanguage);
8746
						}
8747
					}
8748
				}
8749
				CyString filename = f->GetFilePointer();
8750
				filename = filename.Remove(m_sCurrentDir);
8751
				missingFiles->PushBack(filename, packages, false);
8752
			}
8753
		}
8754
	}
8755
	return count;
35 cycrow 8756
}