Subversion Repositories spk

Rev

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