Subversion Repositories spk

Rev

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

Rev Author Line No. Line
1 cycrow 1
#include "../StdAfx.h"
2
#include "MainGui.h"
3
#include "ModSelector.h"
4
#include "PackageBrowser.h"
5
#include "InstallPackageDialog.h"
6
#include "CompareList.h"
7
#include "EditGlobals.h"
8
#include "DownloadPackageList.h"
9
#include "FileLog.h"
10
#include "MessageBoxDetails.h"
11
#include <shellapi.h>
12
 
13
using System::Configuration::ApplicationSettingsBase;
14
 
15
#undef GetEnvironmentVariable
16
 
17
enum {LISTGROUP_INSTALLED, LISTGROUP_SHIP, LISTGROUP_FAKE, LISTGROUP_LIBRARY, LISTGROUP_MOD, LISTGROUP_ARCHIVE};
18
 
19
namespace PluginManager {
20
 
21
	void MainGui::CheckProtectedDir()
22
	{
23
		CyString dir;
24
		if ( m_pDirList->Head() ) {
25
			m_bDirLocked = false;
26
			dir = m_pDirList->Head()->str;
27
 
28
			// write a file in the directory
29
			String ^sDir = SystemStringFromCyString(dir.findreplace("/", "\\"));
30
			bool written = true;
31
			StreamWriter ^sw = nullptr;
32
			try {
33
				sw = System::IO::File::CreateText(sDir + "\\checklock.xpmtest");
34
				sw->WriteLine("can write");
35
			}	
36
			catch (System::Exception ^) {
37
				written = false;
38
			}
39
			finally	{
40
				if ( sw )
41
					delete (IDisposable ^)sw;
42
			}
43
 
44
			// check its there
45
			if ( written ) {
46
				written = false;
47
				cli::array<String ^> ^dirList = IO::Directory::GetFiles(sDir, "*.xpmtest");
48
				if ( dirList && dirList->Length ) {
49
					for ( int i = 0; i < dirList->Length; i++ ) {
50
						if ( IO::FileInfo(dirList[i]).Name == "checklock.xpmtest" ) {
51
							written = true;
52
							break;
53
						}
54
					}
55
				}
56
			}
57
 
58
			// remove it
59
			if ( !IO::File::Exists(sDir + "\\checklock.xpmtest") )
60
				written = false;
61
			else {
62
				try {
63
					IO::File::Delete(sDir + "\\checklock.xpmtest");
64
					if ( IO::File::Exists(sDir + "\\checklock.xpmtest") )
65
						written = false;
66
				}
67
				catch (System::Exception ^) {
68
					written = false;
69
				}
70
			}
71
 
72
			if ( !written ) {
73
				MessageBox::Show(this, "ERROR: The game directory:\n" + SystemStringFromCyString(dir) + "\n\nIs locked and unable to add/remove any plugins", "Directory Locked", MessageBoxButtons::OK, MessageBoxIcon::Error);
74
				m_bDirLocked = true;
75
			}
76
			else {
77
				dir = dir.findreplace("/", "\\");
78
				bool found = false;
79
 
80
				String ^folder = Environment::GetFolderPath(Environment::SpecialFolder::ProgramFiles);
81
				if ( dir.IsIn(CyStringFromSystemString(folder)) )
82
					found = true;
83
				else {
84
					folder = Environment::GetEnvironmentVariable("ProgramFiles(x86)");
85
					if ( dir.IsIn(CyStringFromSystemString(folder)) )
86
						found = true;
87
				}
88
				if ( !found ) {
89
					folder = Environment::GetEnvironmentVariable("ProgramFiles");
90
					if ( dir.IsIn(CyStringFromSystemString(folder)) )
91
						found = true;
92
				}
93
 
94
				if ( found ) {
95
					if ( !m_pPackages->IsSupressProtectedWarning() ) {
96
						if ( MessageBox::Show(this, "WARNING: The game directory:\n" + SystemStringFromCyString(dir) + "\n\nIs in a protected directory (" + SystemStringFromCyString(CFileIO(dir).GetDir().GetToken("/", 1, 2).findreplace("/", "\\")) + ")\n\nThis might cause problems with installing anything, its better to move to game elsewhere, or you may need to run the Plugin Manager with admin access rights\n\nWould you like to surpress this warning in the future?", "Protected Directory", MessageBoxButtons::YesNo, MessageBoxIcon::Warning) == Windows::Forms::DialogResult::Yes ) {
97
							m_pPackages->SurpressProtectedWarning();
98
						}
99
					}
100
				}
101
			}
102
		}
103
	}
104
 
105
	void MainGui::GetGameDirs(CyString &dir, bool askDir, bool askDirAddon)
106
	{
107
		if ( CDirIO(dir).Exists() )
108
		{
109
			CyString gameName = m_pPackages->GetGameName(dir);
110
			if ( !gameName.Empty() )
111
			{
112
				if ( !m_pDirList->FindString(dir) && !m_pRemovedDirList->FindString(dir) ) {
113
					int lang = m_pPackages->GetGameLanguage(dir);
114
					if ( !askDir || MessageBox::Show(this, "New Game Directory Found:\n" + SystemStringFromCyString(dir) + " [" + SystemStringFromCyString(gameName) + "]\n\nDo you wish to add this?", "Add New Game", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == Windows::Forms::DialogResult::Yes ) {
115
						if ( lang )
116
							m_pDirList->PushFront(dir, CyString::Number(lang) + "|" + gameName);
117
						else
118
							m_pDirList->PushFront(dir, gameName);
119
					}
120
					else {
121
						m_pRemovedDirList->PushBack(dir);
122
					}
123
				}
124
 
125
				// check for any addons
126
				CyStringList addons;
127
				if ( m_pPackages->GetGameAddons(addons, dir) ) {
128
					for ( SStringList *str = addons.Head(); str; str = str->next ) {
129
						CyString exe = CDirIO(dir).File(str->str);
130
						dir = CDirIO(dir).Dir(str->data);
131
						if ( CDirIO(dir).Exists() && !m_pDirList->FindString(dir) && !m_pRemovedDirList->FindString(dir) ) {
132
							gameName = m_pPackages->GetGameName(exe);
133
							if ( !gameName.Empty() ) {
134
								int lang = m_pPackages->GetGameLanguage(dir);
135
								if ( !askDirAddon || MessageBox::Show(this, "New Game Directory Found:\n" + SystemStringFromCyString(dir) + " [" + SystemStringFromCyString(gameName) + "]\n\nDo you wish to add this?", "Add New Game", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == Windows::Forms::DialogResult::Yes ) {
136
									if ( lang )
137
										m_pDirList->PushFront(dir, CyString::Number(lang) + "|" + gameName);
138
									else
139
										m_pDirList->PushFront(dir, gameName);
140
								}
141
								else {
142
									m_pRemovedDirList->PushBack(dir);
143
								}
144
							}
145
						}
146
					}
147
				}
148
			}
149
		}
150
	}
151
 
152
	void MainGui::UpdateDirList()
153
	{
154
		ComboDir->Items->Clear();
155
 
156
		if ( !m_pDirList || !m_pDirList->Head() )
157
			return;
158
 
159
		for ( SStringList *node = m_pDirList->Head(); node; node = node->next )
160
		{
161
			System::String ^str;
162
			if ( node->data.IsIn("|") )
163
				str = SystemStringFromCyString(node->str + " [" + node->data.GetToken("|", 2) + "] (Language: " + CPackages::ConvertLanguage(node->data.GetToken("|", 1, 1).ToInt()) + ")");
164
			else
165
				str = SystemStringFromCyString(node->str + " [" + node->data + "]");
166
			ComboDir->Items->Add(str);
167
 
168
			// first item 
169
			if ( node == m_pDirList->Head() )
170
				ComboDir->Text = str;
171
		}
172
 
173
		this->UpdateRunButton();
174
	}
175
 
176
	void MainGui::UpdateRunButton()
177
	{
178
		ButRun->Text = "Run: " + SystemStringFromCyString(m_pPackages->GetGameName());
179
 
180
		CyString exe = m_pPackages->GetGameExe()->GetGameRunExe(m_pPackages->GetCurrentDirectory());
181
		if ( CFileIO(exe).Exists() )
182
		{
183
			exe = exe.FindReplace("/", "\\");
184
			wchar_t wText[200];
185
			::MultiByteToWideChar(CP_ACP, NULL, (char *)exe.c_str(), -1, wText, exe.Length() + 1);
186
 
187
			System::Drawing::Icon ^myIcon;
188
			SHFILEINFO *shinfo = new SHFILEINFO();
189
 
190
			if ( FAILED(SHGetFileInfo(wText, 0, shinfo, sizeof(shinfo), SHGFI_ICON | SHGFI_LARGEICON)) )
191
			{
192
				if ( FAILED(SHGetFileInfo(wText, 0, shinfo, sizeof(shinfo), SHGFI_ICON | SHGFI_SMALLICON)) )
193
				{
194
					System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainGui::typeid));
195
					this->ButRun->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"ButRun.Image")));
196
				}
197
				else
198
				{
199
					myIcon = System::Drawing::Icon::FromHandle(IntPtr(shinfo->hIcon));
200
					ButRun->Image = myIcon->ToBitmap();
201
				}
202
			}
203
			else
204
			{
205
				myIcon = System::Drawing::Icon::FromHandle(IntPtr(shinfo->hIcon));
206
				ButRun->Image = myIcon->ToBitmap();
207
			}
208
 
209
			delete shinfo;
210
		}
211
	}
212
 
213
	void MainGui::_DisplayPackages(CBaseFile *currentParent, ListViewGroup ^useGroup)
214
	{
215
		CLinkList<CBaseFile> packageList;
216
 
217
		for ( CBaseFile *p = m_pPackages->FirstPackage(); p; p = m_pPackages->NextPackage() )
218
		{
219
			if ( p->IsMod() )
220
				continue;
221
			if ( p->GetAuthor().Compare("PluginManager") )
222
				continue;
223
			// if thier parent is a library dont add unless top level
224
			if ( currentParent && p->GetParent() && p->GetParent()->GetType() == TYPE_SPK && ((CSpkFile *)p->GetParent())->IsLibrary() )
225
				continue;
226
			else if ( p->GetParent() == currentParent )
227
				packageList.push_back(p);
228
			// add any mod addons as the top level
229
			else if ( p->GetParent() && p->GetParent()->IsMod() && !currentParent && p->GetParent() == m_pPackages->GetEnabledMod() )
230
				packageList.push_back(p);
231
			// if thier parent is a library add at top level
232
			else if ( !currentParent && p->GetParent() && p->GetParent()->GetType() == TYPE_SPK && ((CSpkFile *)p->GetParent())->IsLibrary() )
233
				packageList.push_back(p);
234
		}
235
 
236
		array<SortPackage ^> ^aPackages = gcnew array<SortPackage ^>(packageList.size());
237
		array<System::String ^> ^aNames = gcnew array<System::String ^>(packageList.size());
238
 
239
		int i = 0;
240
		for ( CBaseFile *p = packageList.First(); p; p = packageList.Next(), i++ )
241
		{
242
			aPackages[i] = gcnew SortPackage(p);
243
			if ( m_iSortingColumn == SORT_AUTHOR ) // sort by author
244
				aNames[i] = SystemStringFromCyString(p->GetAuthor())->ToLower();
245
			else if ( m_iSortingColumn == SORT_VERSION ) // sort by author
246
				aNames[i] = SystemStringFromCyString(p->GetVersion())->ToLower();
247
			else if ( m_iSortingColumn == SORT_CREATED ) // sort by author
248
			{
249
				CyString date = p->GetCreationDate().GetToken("/", 3, 3) + p->GetCreationDate().GetToken("/", 2, 2) + p->GetCreationDate().GetToken("/", 1, 1);
250
				aNames[i] = SystemStringFromCyString(date);
251
			}
252
			else if ( m_iSortingColumn == SORT_ENABLE ) // sort by author
253
			{
254
				if ( p->IsEnabled() )
255
					aNames[i] = SystemStringFromCyString(CyString::Number(1));
256
				else
257
					aNames[i] = SystemStringFromCyString(CyString::Number(0));
258
			}
259
			else if ( m_iSortingColumn == SORT_SIGNED ) // sort by author
260
			{
261
				if ( p->IsEnabled() )
262
					aNames[i] = SystemStringFromCyString(CyString::Number(1));
263
				else
264
					aNames[i] = SystemStringFromCyString(CyString::Number(0));
265
			}
266
			else if ( m_iSortingColumn == SORT_TYPE ) // sort by type
267
			{
268
				if ( p->GetType() == TYPE_XSP )
269
					aNames[i] = "Ship";
270
				else if ( p->GetType() == TYPE_ARCHIVE )
271
					aNames[i] = "- Archive -";
272
				else if ( p->GetType() == TYPE_SPK )
273
					aNames[i] = SystemStringFromCyString(((CSpkFile *)p)->GetScriptTypeString(m_pPackages->GetLanguage()));
274
				else
275
					aNames[i] = "";
276
			}
277
			else
278
				aNames[i] = SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage()))->ToLower();
279
		}
280
 
281
		Array::Sort(aNames, aPackages);
282
 
283
		// now display
284
		for ( i = 0; i < aPackages->Length; i++ )
285
		{
286
			CBaseFile *p = (m_bSortingAsc) ? aPackages[i]->Package : aPackages[(aPackages->Length - 1 - i)]->Package;
287
			CyString name;
288
			if ( p->GetType() == TYPE_ARCHIVE )
289
				name = CFileIO(p->GetFilename()).GetFilename();
290
			else
291
				name = p->GetLanguageName(m_pPackages->GetLanguage());
292
 
293
			int indent = 0;
294
			CBaseFile *parent = p;
295
 
296
			if ( p->GetParent() && p->GetParent()->GetType() == TYPE_SPK && ((CSpkFile *)p->GetParent())->IsLibrary() )
297
				indent = 0;
298
			else
299
			{
300
				while ( parent->GetParent() )
301
				{
302
					parent = parent->GetParent();
303
					++indent;
304
				}
305
 
306
				if ( p->GetParent() && p->GetParent() == m_pPackages->GetEnabledMod() )
307
					--indent;
308
			}
309
 
310
			ListViewItem ^item = gcnew ListViewItem(SystemStringFromCyString(name));
311
			item->IndentCount = (indent * 2);
312
			item->SubItems->Add(SystemStringFromCyString(p->GetAuthor()));
313
			item->SubItems->Add(SystemStringFromCyString(p->GetVersion()));
314
			item->SubItems->Add(SystemStringFromCyString(p->GetCreationDate()));
315
			if ( p->GetType() == TYPE_XSP )
316
				item->SubItems->Add("Ship");
317
			else if ( p->GetType() == TYPE_ARCHIVE )
318
				item->SubItems->Add("- Archive -");
319
			else if ( p->GetType() == TYPE_SPK )
320
			{
321
				CSpkFile *spk = (CSpkFile *)p;
322
				item->SubItems->Add(SystemStringFromCyString(spk->GetScriptTypeString(m_pPackages->GetLanguage())));
323
			}
324
			else
325
				item->SubItems->Add("");
326
 
327
			if ( p->IsEnabled() )
328
				item->SubItems->Add("Yes");
329
			else
330
				item->SubItems->Add("No");
331
			if ( p->IsSigned() )
332
				item->SubItems->Add("Yes");
333
			else
334
				item->SubItems->Add("No");
335
			item->Tag = SystemStringFromCyString(CyString::Number(p->GetNum()));
336
 
337
			ListPackages->Items->Add(item);
338
 
339
			if ( useGroup )
340
				item->Group = useGroup;
341
			else
342
			{
343
				int addGroup = LISTGROUP_INSTALLED;
344
				if ( p->GetParent() && p->GetParent() == m_pPackages->GetEnabledMod() )
345
					addGroup = LISTGROUP_MOD;
346
				else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
347
					addGroup = LISTGROUP_LIBRARY;
348
				else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsFakePatch() )
349
					addGroup = LISTGROUP_FAKE;
350
				else if ( p->GetType() == TYPE_XSP )
351
					addGroup = LISTGROUP_SHIP;
352
				else if ( p->GetType() == TYPE_ARCHIVE )
353
					addGroup = LISTGROUP_ARCHIVE;
354
 
355
				item->Group = ListPackages->Groups[addGroup];
356
			}
357
 
358
			CyString groupName = CyStringFromSystemString(item->Group->Header);
359
			if ( groupName.IsIn(" [") )
360
			{
361
				int enabled = groupName.GetToken(" [", 2, 2).GetToken("/", 1, 1).ToInt();
362
				int total = groupName.GetToken(" [", 2, 2).GetToken("/", 2, 2).GetToken("]", 1, 1).ToInt() + 1;
363
				if ( p->IsEnabled() ) ++enabled;
364
				groupName = groupName.GetToken(" [", 1, 1) + " [" + CyString::Number(enabled) + "/" + CyString::Number(total) + "]";
365
			}
366
			else
367
			{
368
				if ( p->IsEnabled() )
369
					groupName = groupName + " [1/1]";
370
				else
371
					groupName = groupName + " [0/1]";
372
			}
373
			item->Group->Header = SystemStringFromCyString(groupName);
374
 
375
			// get the icon
376
			item->ImageIndex = -1;
377
			if ( p->GetIcon() )
378
				PluginManager::DisplayListIcon(p, ListPackages, item);
379
 
380
			if ( item->ImageIndex == -1 )
381
			{
382
				if ( p->GetType() == TYPE_XSP )
383
					item->ImageKey = "ship";
384
				else if ( p->GetType() == TYPE_ARCHIVE )
385
					item->ImageKey = "archive";
386
				else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
387
					item->ImageKey = "library";
388
				else if ( p->IsFakePatch() )
389
					item->ImageKey = "fake";
390
				else
391
					item->ImageKey = "package";
392
			}
393
 
394
			// check for any children
395
			this->_DisplayPackages(p, item->Group);
396
		}
397
	}
398
 
399
	void MainGui::UpdatePackages()
400
	{
401
		ListPackages->Items->Clear();
402
		ListPackages->Groups->Clear();
403
		ListPackages->SmallImageList = gcnew ImageList();
404
		ListPackages->LargeImageList = gcnew ImageList();
405
		ListPackages->LargeImageList->ImageSize = System::Drawing::Size(32, 32);
406
		ListPackages->LargeImageList->ColorDepth = Windows::Forms::ColorDepth::Depth32Bit;
407
		ListPackages->SmallImageList->ImageSize = System::Drawing::Size(16, 16);
408
		ListPackages->SmallImageList->ColorDepth = Windows::Forms::ColorDepth::Depth32Bit;
409
 
410
		int index = this->imageList1->Images->IndexOfKey("package.png");
411
		if ( index != -1 )
412
		{
413
			ListPackages->SmallImageList->Images->Add("package", this->imageList1->Images[index]);
414
			ListPackages->LargeImageList->Images->Add("package", this->imageList1->Images[index]);
415
		}
416
		index = this->imageList1->Images->IndexOfKey("ship.png");
417
		if ( index != -1 )
418
		{
419
			ListPackages->SmallImageList->Images->Add("ship", this->imageList1->Images[index]);
420
			ListPackages->LargeImageList->Images->Add("ship", this->imageList1->Images[index]);
421
		}
422
		index = this->imageList1->Images->IndexOfKey("fake.png");
423
		if ( index != -1 )
424
		{
425
			ListPackages->SmallImageList->Images->Add("fake", this->imageList1->Images[index]);
426
			ListPackages->LargeImageList->Images->Add("fake", this->imageList1->Images[index]);
427
		}
428
		index = this->imageList1->Images->IndexOfKey("library.png");
429
		if ( index != -1 )
430
		{
431
			ListPackages->SmallImageList->Images->Add("library", this->imageList1->Images[index]);
432
			ListPackages->LargeImageList->Images->Add("library", this->imageList1->Images[index]);
433
		}
434
 
435
		index = this->imageList1->Images->IndexOfKey("archive.png");
436
		if ( index != -1 )
437
		{
438
			ListPackages->SmallImageList->Images->Add("archive", this->imageList1->Images[index]);
439
			ListPackages->LargeImageList->Images->Add("archive", this->imageList1->Images[index]);
440
		}
441
 
442
		ListViewGroup ^group = gcnew ListViewGroup("Installed Scripts", HorizontalAlignment::Left);
443
		ListPackages->Groups->Add(group);
444
		ListViewGroup ^shipGroup = gcnew ListViewGroup("Installed Ships", HorizontalAlignment::Left);
445
		ListPackages->Groups->Add(shipGroup);
446
		ListViewGroup ^fakeGroup = gcnew ListViewGroup("Fake Patches", HorizontalAlignment::Left);
447
		ListPackages->Groups->Add(fakeGroup);
448
		ListViewGroup ^libGroup = gcnew ListViewGroup("Script Libraries", HorizontalAlignment::Left);
449
		ListPackages->Groups->Add(libGroup);
450
		ListViewGroup ^modGroup = gcnew ListViewGroup("Current Mod Addons", HorizontalAlignment::Left);
451
		ListPackages->Groups->Add(modGroup);
452
		ListViewGroup ^arcGroup = gcnew ListViewGroup("Installed Archives", HorizontalAlignment::Left);
453
		ListPackages->Groups->Add(arcGroup);
454
 
455
		// sort the items
456
		if ( m_pPackages )
457
		{
458
			m_pPackages->AssignPackageNumbers();
459
			this->_DisplayPackages(NULL, nullptr);
460
		}
461
 
462
		PackageListSelected(ListPackages, gcnew System::EventArgs());
463
		ListPackages->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
464
 
465
		// update the status bar
466
		LabelStatus->Text = "Plugins Available: " + (m_pPackages->CountPackages(TYPE_BASE, false) - m_pPackages->CountBuiltInPackages(false))+ " (" + (m_pPackages->CountPackages(TYPE_BASE, true) - m_pPackages->CountBuiltInPackages(true)) + " Enabled)";
467
	}
468
 
469
	void MainGui::StartCheckTimer()
470
	{
471
		System::Windows::Forms::Timer ^timer = gcnew System::Windows::Forms::Timer();
472
		timer->Interval = 5000;
473
		timer->Tick += gcnew System::EventHandler(this, &MainGui::TimerEvent_CheckFile);
474
		timer->Start();
475
	}
476
 
477
 
478
	//
479
	// Update Controls
480
	// Updates any additional controls, ie adding columns to package list
481
	//
482
	void MainGui::UpdateControls()
483
	{
484
		// Package List Columns
485
		int csize = ListPackages->Width - 200 - 5;
486
		int psize = ((csize * 7) / 10);
487
		int asize = ((csize * 3) / 10);
488
		if ( psize < 60 ) psize = 60;
489
		if ( asize < 60 ) asize = 60;
490
		ListPackages->Columns->Clear();
491
		ListPackages->Columns->Add("Package", psize, HorizontalAlignment::Left);
492
		ListPackages->Columns->Add("Author", asize, HorizontalAlignment::Left);
493
		ListPackages->Columns->Add("Version", 60, HorizontalAlignment::Right);
494
		ListPackages->Columns->Add("Updated", 80, HorizontalAlignment::Left);
495
		ListPackages->Columns->Add("Type", 100, HorizontalAlignment::Left);
496
		ListPackages->Columns->Add("Enabled", 60, HorizontalAlignment::Right);
497
		ListPackages->Columns->Add("Signed", 60, HorizontalAlignment::Right);
498
		ListPackages->FullRowSelect = true;
499
		ListPackages->Focus();
500
 
501
		if ( m_pPackages->IsVanilla() )
502
			m_pMenuBar->Vanilla();
503
		else
504
			m_pMenuBar->Modified();
505
	}
506
 
507
	//
508
	// Install a package
509
	// Called from either install button, or from command line
510
	//
511
	bool MainGui::InstallPackage(System::String ^file, bool straightAway, bool builtin, bool background)
512
	{
513
		bool errored = false;
514
 
515
		if ( file->Length )
516
		{
517
			int error;
518
			CBaseFile *package = m_pPackages->OpenPackage(CyStringFromSystemString(file), &error, 0, SPKREAD_NODATA, READFLAG_NOUNCOMPRESS);
519
			if ( error == INSTALLERR_NOMULTI )
520
			{
521
				CLinkList<CBaseFile> erroredList;
522
				m_pPackages->PrepareMultiPackage(CyStringFromSystemString(file), &erroredList, &error, 0);
523
				if ( erroredList.size() )
524
				{
525
					System::String ^modified;
526
					for ( CBaseFile *p = erroredList.First(); p; p = erroredList.Next() )
527
					{
528
						p->SetOverrideFiles(builtin);
529
						if ( m_pPackages->PrepareInstallPackage(p, false, false, IC_MODIFIED) != INSTALLCHECK_OK )
530
						{
531
							modified += SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage()));
532
							modified += "\n";
533
							errored = true;
534
						}
535
					}
536
 
537
					if ( errored )
538
						this->DisplayMessageBox(false, "Installing", "Currently in Vanilla Mode, Package is not an Vanilla Package\n\n" + modified + "\nSwitch to modified mode if you wish to install this package", MessageBoxButtons::OK, MessageBoxIcon::Question);
539
				}
540
			}
541
			else if ( !package )
542
			{
543
				System::String ^errorStr;
544
				switch ( error )
545
				{
546
					case INSTALLERR_OLD:
547
						errorStr = "File is in old format no longer supported";
548
						break;
549
					case INSTALLERR_NOEXIST:
550
						errorStr = "file doesn't exist";
551
						break;
552
					case INSTALLERR_INVALID:
553
						errorStr = "Invalid package file";
554
						break;
555
					case INSTALLERR_NOSHIP:
556
						errorStr = "Ship Packages are currently not supported";
557
						break;
558
					case INSTALLERR_VERSION:
559
						errorStr = "Package file was created in a newer version, unable to open";
560
						break;
561
 
562
					default:
563
						errorStr = "Unknown Error";
564
				}
565
 
566
				if ( !builtin )
567
					this->DisplayMessageBox(false, "Open Package Error", "Error Opening: " + file + "\n" + errorStr, MessageBoxButtons::OK, MessageBoxIcon::Stop);
568
				errored = true;
569
			}
570
			else
571
			{
572
				if ( builtin )
573
				{
574
					package->SetOverrideFiles(builtin);
575
					if ( m_pPackages->PrepareInstallPackage(package, false, false, IC_WRONGGAME|IC_WRONGVERSION|IC_OLDVERSION) != INSTALLCHECK_OK )
576
						errored = true;
577
				}
578
				else
579
				{
580
					int errorNum = m_pPackages->PrepareInstallPackage(package, false, false, IC_ALL);
581
					if ( errorNum != INSTALLCHECK_OK )
582
					{
583
						if ( errorNum == INSTALLCHECK_NOSHIP )
584
						{
585
							this->DisplayMessageBox(false, "No Ships", "Ships are not supported for " + SystemStringFromCyString(m_pPackages->GetGameName()), MessageBoxButtons::OK, MessageBoxIcon::Stop);
586
							errored = true;
587
						}
588
						else if ( m_pPackages->PrepareInstallPackage(package, false, false, IC_MODIFIED) != INSTALLCHECK_OK )
589
						{
590
							this->DisplayMessageBox(false, "Installing", "Currently in Vanilla Mode, Package is not an Vanilla Package\n" + SystemStringFromCyString(package->GetLanguageName(m_pPackages->GetLanguage())) + "\n\nSwitch to modified mode if you wish to install this package", MessageBoxButtons::OK, MessageBoxIcon::Question);
591
							errored = true;
592
						}
593
					}
594
 
595
					// check for compatabilities
596
					CLinkList<CBaseFile> packages;
597
					int compat = m_pPackages->CheckCompatabilityAgainstPackages(package, NULL, &packages);
598
					if ( compat )
599
					{
600
						String ^message = "\nConflicts with:\n";
601
						for ( CBaseFile *p = packages.First(); p; p = packages.Next() )
602
							message += SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n";
603
 
604
						if ( MessageBox::Show(this, SystemStringFromCyString(package->GetFullPackageName(m_pPackages->GetLanguage())) + ":\nIncompatabilities found with installed fake patches\n" + message + "\n\nDo you still wish to install?", "Packages Incompatable", MessageBoxButtons::YesNo, MessageBoxIcon::Warning) != Windows::Forms::DialogResult::Yes )
605
						{
606
							m_pPackages->RemovePreparedInstall(package);
607
							errored = true;
608
						}
609
					}
610
				}
611
 
612
				// not installing
613
				if ( errored )
614
				{
615
					delete package;
616
					package = NULL;
617
				}
618
			}
619
		}
620
 
621
		if ( errored )
622
		{
623
			this->Enabled= true;
624
			this->ProgressBar->Hide();
625
			return false;
626
		}
627
 
628
		// start installing
629
		if ( straightAway )
630
			return this->StartInstalling(builtin, background);
631
 
632
		return true;
633
	}
634
 
635
	void MainGui::DoUninstall()
636
	{
637
		m_pPi = gcnew PackageInstalled("Uninstall Packages");
638
 
639
		CLinkList<CBaseFile> packageList;
640
		CLinkList<CBaseFile> disableList;
641
 
642
		if ( m_pPackages->UninstallPreparedPackages(m_pFileErrors, 0, &packageList, &disableList) )
643
		{
644
			CyString sDisplay;
645
			CyString sAfterText;
646
			for ( CBaseFile *p = packageList.First(); p; p = packageList.Next() )
647
			{
648
				sAfterText = m_pPackages->GetUninstallAfterText(p);
649
				m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), (sAfterText.Empty() ? "Uninstalled" : SystemStringFromCyString(sAfterText)), "Uninstalled");
650
				sDisplay = p->GetFullPackageName(m_pPackages->GetLanguage());
651
				delete p;
652
			}
653
			for ( CBaseFile *p = disableList.First(); p; p = disableList.Next() )
654
				m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), "Disabled", "Dependants Disabled");
655
			packageList.clear();			
656
 
657
			if ( m_pPi->PackageCount() == 1 )
658
			{
659
				if ( sAfterText.Empty() )
660
					this->DisplayMessageBox(true, "Uninstalled", "Package Uninstalled\n" + SystemStringFromCyString(sDisplay), MessageBoxButtons::OK, MessageBoxIcon::Information);
661
				else
662
					this->DisplayMessageBox(true, "Uninstalled", "Package Uninstalled\n" + SystemStringFromCyString(sDisplay) + "\n\n" + SystemStringFromCyString(sAfterText), MessageBoxButtons::OK, MessageBoxIcon::Information);
663
			}
664
			else
665
				m_bDisplayDialog = true;
666
		}
667
		else
668
			this->DisplayMessageBox(true, "Uninstall Error", "Error Uninstalling", MessageBoxButtons::OK, MessageBoxIcon::Stop);
669
	}
670
 
671
	void MainGui::DoDisable()
672
	{
673
		CLinkList<CBaseFile> packageList;
674
 
675
		System::String ^display;
676
		m_pPi = gcnew PackageInstalled("Enabled/Disabled Packages");
677
 
678
		if ( m_pPackages->GetNumPackagesInDisabledQueue() )
679
		{
680
			if ( m_pPackages->DisablePreparedPackages(0, 0, &packageList) )
681
			{
682
				for ( CBaseFile *p = packageList.First(); p; p = packageList.Next() )
683
				{
684
					m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), "Disabled", "Disabled Packages");
685
					display = "Package Disabled\n\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage()));
686
				}
687
			}
688
			else
689
				this->DisplayMessageBox(true, "Disable Error", "Error Disabling packages", MessageBoxButtons::OK, MessageBoxIcon::Stop);
690
		}
691
 
692
		packageList.clear();
693
		if ( m_pPackages->GetNumPackagesInEnabledQueue() )
694
		{
695
			if ( m_pPackages->EnablePreparedPackages(0, 0, &packageList) )
696
			{
697
				for ( CBaseFile *p = packageList.First(); p; p = packageList.Next() )
698
				{
699
					m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), "Enabled", "Enable Packages");
700
					display = "Package Enabled\n\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage()));
701
				}
702
			}
703
			else
704
				this->DisplayMessageBox(true, "Enable Error", "Error Enabling packages", MessageBoxButtons::OK, MessageBoxIcon::Stop);
705
		}
706
 
707
		if ( m_pPi->PackageCount() == 1 )
708
			this->DisplayMessageBox(true, "Packages Enabled/Disabled", display, MessageBoxButtons::OK, MessageBoxIcon::Information);
709
		else
710
		{
711
			m_bDisplayDialog = true;
712
		}
713
	}
714
 
715
	void MainGui::DoInstall(bool builtin, bool frombackground)
716
	{
717
		CLinkList<CBaseFile> erroredPackages;
718
		CLinkList<CBaseFile> installedPackages;
719
		if ( m_pPackages->InstallPreparedPackages(m_pFileErrors, 0, &erroredPackages, &installedPackages) )
720
		{
721
			if ( !builtin )
722
			{
723
				if ( installedPackages.size() == 1 && erroredPackages.size() == 0 )
724
				{
725
					CBaseFile *p = installedPackages.Front()->Data();
726
					CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
727
					CyString afterText = m_pPackages->GetInstallAfterText(p);
728
					if ( afterText.Empty() )
729
						this->DisplayMessageBox(frombackground, "Installed", "Package: " + SystemStringFromCyString(packageName) + " installed!\n\n", MessageBoxButtons::OK, MessageBoxIcon::Information);
730
					else
731
					{
732
						afterText.StripHTML();
733
						this->DisplayMessageBox(frombackground, "Installed", "Package: " + SystemStringFromCyString(packageName) + " installed!\n\n" + SystemStringFromCyString(afterText), MessageBoxButtons::OK, MessageBoxIcon::Information);
734
					}
735
				}
736
				else
737
				{
738
					m_pPi = gcnew PackageInstalled("Packages Installed");
739
 
740
					CyStringList packages;
741
					for ( CListNode<CBaseFile> *node = installedPackages.Front(); node; node = node->next() )
742
					{
743
						CBaseFile *p = node->Data();
744
						CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
745
						CyString afterText = m_pPackages->GetInstallAfterText(p);
746
 
747
						if ( afterText.Empty() )
748
							afterText = "Installed";
749
						m_pPi->AddPackage(SystemStringFromCyString(packageName), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), SystemStringFromCyString(afterText));
750
					}
751
					for ( CListNode<CBaseFile> *node = erroredPackages.Front(); node; node = node->next() )
752
					{
753
						CBaseFile *p = node->Data();
754
						CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
755
						m_pPi->AddPackage(SystemStringFromCyString(packageName), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), SystemStringFromCyString("Failed to Install"));
756
					}
757
 
758
					m_bDisplayDialog = true;
759
				}
760
			}
761
		}
762
		// no packages were installed
763
		else 
764
		{
765
			if ( !builtin )
766
			{
767
				if ( erroredPackages.size() == 1 )
768
				{
769
					CBaseFile *p = erroredPackages.Front()->Data();
770
					CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
771
					this->DisplayMessageBox(frombackground, "Error Installing", "Package: " + SystemStringFromCyString(packageName) + " failed to install!\nError: " + SystemStringFromCyString(CBaseFile::ErrorString(p->GetLastError(), p->GetLastErrorString())) + "\n", MessageBoxButtons::OK, MessageBoxIcon::Error);
772
				}
773
				else
774
				{
775
					m_pPi = gcnew PackageInstalled("Packages Failed To Install");
776
 
777
					CyStringList packages;
778
					for ( CListNode<CBaseFile> *node = erroredPackages.Front(); node; node = node->next() )
779
					{
780
						CBaseFile *p = node->Data();
781
						CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
782
						m_pPi->AddPackage(SystemStringFromCyString(packageName), SystemStringFromCyString(p->GetAuthor()), SystemStringFromCyString(p->GetVersion()), "Failed: " + SystemStringFromCyString(CBaseFile::ErrorString(p->GetLastError(), p->GetLastErrorString())));
783
					}
784
 
785
					m_bDisplayDialog = true;
786
				}
787
			}
788
		}
789
 
790
		if ( !frombackground )
791
			this->Background_Finished();
792
	}
793
 
794
	void MainGui::ClearSelectedItems()
795
	{
796
		for ( int i = 0; i < this->ListPackages->Items->Count; i++ )
797
			this->ListPackages->Items[i]->Selected = false;
798
		PackageListSelected(ListPackages, gcnew System::EventArgs());
799
	}
800
 
801
	bool MainGui::StartInstalling(bool builtin, bool background, bool archive)
802
	{
803
		// no packages to install
804
		if ( !m_pPackages->GetNumPackagesInQueue() )
805
			return false;
806
 
807
		// clear selected
808
		this->ClearSelectedItems();
809
 
810
		// lets install them now
811
		CLinkList<CBaseFile> lCheckPackages;
812
		if ( m_pPackages->CheckPreparedInstallRequired(&lCheckPackages) )
813
		{
814
			for ( CListNode<CBaseFile> *pNode = lCheckPackages.Front(); pNode; pNode = pNode->next() )
815
			{
816
				CBaseFile *package = pNode->Data();
817
				CSpkFile *spk = (CSpkFile *)package;
818
 
819
				CyStringList missingList;
820
				if ( m_pPackages->GetMissingDependacies(package, &missingList) )
821
				{
822
					CyString requires;
823
					for ( SStringList *strNode = missingList.Head(); strNode; strNode = strNode->next )
824
					{
825
						if ( strNode->str.IsIn("|") )
826
							requires += strNode->str.GetToken("|", 1, 1) + " V" + strNode->str.GetToken("|", 2, 2) + " by " + strNode->data;
827
						else
828
							requires += strNode->str + " by " + strNode->data;
829
						requires += "\n";
830
					}
831
					this->DisplayMessageBox(false, "Installing", "Missing Package for " + SystemStringFromCyString(package->GetLanguageName(m_pPackages->GetLanguage())) + "\nRequires:\n" + SystemStringFromCyString(requires), MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
832
				}
833
			}
834
		}
835
 
836
		// no packages to install
837
		if ( !m_pPackages->GetNumPackagesInQueue() )
838
		{
839
			this->Enabled = true;
840
			this->ProgressBar->Hide();
841
			return false;
842
		}
843
 
844
		ProgressBar->Show();
845
		this->Enabled = false;
846
 
847
		if ( builtin )
848
		{
849
			if ( background )
850
				this->StartBackground(MGUI_BACKGROUND_INSTALLBUILTIN);
851
			else
852
				this->DoInstall(builtin, false);
853
		}
854
		else if ( archive )
855
		{
856
			if ( background )
857
				this->StartBackground(MGUI_BACKGROUND_INSTALL);
858
			else
859
				this->DoInstall(false, false);
860
		}
861
		else
862
		{
863
			InstallPackageDialog ^installDialog = gcnew InstallPackageDialog(m_pPackages);
864
			if ( installDialog->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
865
			{
866
				// no packages to install
867
				if ( !m_pPackages->GetNumPackagesInQueue() )
868
				{
869
					this->Enabled = true;
870
					this->ProgressBar->Hide();
871
					return false;
872
				}
873
				if ( background )
874
					this->StartBackground(MGUI_BACKGROUND_INSTALL);
875
				else
876
					this->DoInstall(false, false);
877
			}
878
			else
879
			{
880
				m_pPackages->RemovePreparedInstall(NULL);
881
				this->Enabled = true;
882
				this->ProgressBar->Hide();
883
				return false;
884
			}
885
		}
886
		return true;
887
	}
888
 
889
	bool MainGui::StartBackground(int type, System::String ^info)
890
	{
891
		if ( backgroundWorker1->IsBusy )
892
			return false;
893
		if ( m_bRunningBackground )
894
			return false;
895
 
896
		m_sBackgroundInfo = info;
897
		return this->StartBackground(type);
898
	}
899
 
900
	bool MainGui::StartBackground(int type)
901
	{
902
		if ( backgroundWorker1->IsBusy )
903
			return false;
904
		if ( m_bRunningBackground )
905
			return false;
906
 
907
		m_iBackgroundTask = type;
908
 
909
		backgroundWorker1->RunWorkerAsync();
910
		m_bRunningBackground = true;
911
		return true;
912
	}
913
 
914
	void MainGui::ChangeDirectory(CyString dir)
915
	{
916
		if ( m_pPackages->IsCurrentDir(dir) )
917
			return;
918
 
919
		if ( m_pPackages->IsLoaded() )
920
		{
921
			if ( m_pPackages->CloseDir(0, 0, true) )
922
			{
923
				// write the modname
924
				if ( !m_pPackages->GetModKey().Empty() )
925
					PluginManager::WriteRegistryValue(m_pPackages->GetModKey(), m_pPackages->GetSelectedModName());
926
				m_pPackages->Reset();
927
			}
928
			else
929
			{
930
				this->DisplayMessageBox(true, "Error", "unable to close directory", MessageBoxButtons::OK, MessageBoxIcon::Error);
931
				return;
932
			}
933
		}
934
 
935
		m_pPackages->Reset();
936
 
937
		if ( m_pPackages->Read(dir, 0) )
938
		{
939
			if ( m_iSaveGameManager == 1 )
940
				m_pPackages->RestoreSaves();
941
			m_pPackages->UpdatePackages();
942
			m_pPackages->ReadGameLanguage(true);
943
			this->UpdateRunButton();
944
			System::String ^mod = PluginManager::ReadRegistryValue(m_pPackages->GetModKey());
945
			m_pPackages->SetMod(CyStringFromSystemString(mod));
946
		}
947
		else
948
		{
949
			this->DisplayMessageBox(true, "Error", "unable to open new directory", MessageBoxButtons::OK, MessageBoxIcon::Error);
950
			this->Close();
951
		}
952
	}
953
 
954
	CBaseFile *MainGui::FindPackageFromList(ListViewItem ^item)
955
	{
956
		if ( !item )
957
			return NULL;
958
 
959
		System::String ^sNum = System::Convert::ToString(item->Tag);
960
		int iNum = CyStringFromSystemString(sNum).ToInt();
961
 
962
		CBaseFile *p = m_pPackages->GetPackageAt(iNum);
963
		return p;
964
	}
965
 
966
	void MainGui::FindPackagesOnline()
967
	{
968
		CyStringList servers;
969
		m_pPackages->FindAllServers(&servers);
970
		if ( servers.Empty() )
971
		{
972
			MessageBox::Show(this, "Found now web address to check for packages", "No Web Address", MessageBoxButtons::OK, MessageBoxIcon::Warning);
973
			return;
974
		}
975
 
976
		DownloadPackageList ^dpl = gcnew DownloadPackageList(m_pPackages, &servers);
977
		dpl->ShowDialog(this);
978
 
979
		if ( m_pPackages->AnyAvailablePackages() )
980
			MessageBox::Show(this, "Package update completed\n" + m_pPackages->GetAvailablePackageList()->size() + " packages have been added to the package browser", "Found Packages", MessageBoxButtons::OK, MessageBoxIcon::Information);
981
		else
982
			MessageBox::Show(this, "Unable to find any packages\n", "No Packages Found", MessageBoxButtons::OK, MessageBoxIcon::Warning);
983
	}
984
 
985
	//
986
	// Event Handlers
987
	//
988
	void MainGui::SetupEventHandlers()
989
	{
990
		// setup Event Handlers
991
		ButClose->Click += gcnew EventHandler(this, &PluginManager::MainGui::ClosedEvent);
992
		ButInstall->Click += gcnew EventHandler(this, &PluginManager::MainGui::InstallEvent);
993
		ButUninstall->Click += gcnew EventHandler(this, &PluginManager::MainGui::UninstallEvent);
994
		ButDisable->Click += gcnew EventHandler(this, &PluginManager::MainGui::DisableEvent);
995
		ListPackages->SelectedIndexChanged += gcnew EventHandler(this, &PluginManager::MainGui::PackageListSelected);
996
		ListPackages->ColumnClick  += gcnew ColumnClickEventHandler(this, &PluginManager::MainGui::PackageListSort);
997
		ComboDir->SelectedIndexChanged += gcnew EventHandler(this, &PluginManager::MainGui::ChangeDirectoryEvent);
998
 
999
 
1000
		// background worker
1001
		backgroundWorker1->DoWork += gcnew DoWorkEventHandler( this, &MainGui::Background_DoWork );
1002
		backgroundWorker1->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainGui::Background_Finished );
1003
		backgroundWorker1->ProgressChanged += gcnew ProgressChangedEventHandler( this, &MainGui::Background_Progress );
1004
 
1005
		// auto update
1006
		backgroundUpdater->DoWork += gcnew DoWorkEventHandler( this, &MainGui::Updater_DoWork );
1007
		backgroundUpdater->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainGui::Updater_Finished );
1008
	}
1009
 
1010
	void MainGui::PackageListSort(System::Object ^Sender, ColumnClickEventArgs ^E)
1011
	{
1012
		if ( E->Column != m_iSortingColumn )
1013
		{
1014
			m_iSortingColumn = E->Column;
1015
			m_bSortingAsc = true;
1016
		}
1017
		else
1018
			m_bSortingAsc = !m_bSortingAsc;
1019
		this->UpdatePackages();
1020
	}
1021
 
1022
	void MainGui::PackageListSelected(System::Object ^Sender, System::EventArgs ^E)
1023
	{
1024
		// is there any selected items
1025
		this->PictureDisplay->Image = nullptr;
1026
		this->PanelDisplay->Hide();
1027
		TextDesc->Text = "";
1028
		bool buttonEnabled = false;
1029
		if ( ListPackages->SelectedItems->Count )
1030
		{
1031
			buttonEnabled = true;
1032
 
1033
			ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1034
			System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1035
			while ( myEnum->MoveNext() )
1036
			{
1037
				CBaseFile *p = this->FindPackageFromList(safe_cast<ListViewItem ^>(myEnum->Current));
1038
				if ( p )
1039
				{
1040
					if ( p->IsEnabled() )
1041
						ButDisable->Text = "Disable";
1042
					else
1043
						ButDisable->Text = "Enable";
48 cycrow 1044
					if ( !p->description().empty() )	TextDesc->Text = _US(p->description().findReplace("<br>", "\n").stripHtml());
1 cycrow 1045
 
1046
					this->PictureDisplay->Show();
1047
					bool addedIcon = false;
1048
					C_File *picFile = p->GetFirstFile(FILETYPE_ADVERT);
1049
					if ( picFile )
1050
					{
1051
						System::String ^pic = SystemStringFromCyString(picFile->GetFilePointer());
1052
						if ( System::IO::File::Exists(pic) )
1053
						{
1054
							Bitmap ^myBitmap = gcnew Bitmap(pic);
1055
							if ( myBitmap )
1056
							{
1057
								this->PictureDisplay->Image = dynamic_cast<Image ^>(myBitmap);
1058
								addedIcon = true;
1059
							}
1060
						}
1061
					}
1062
 
1063
					if ( !addedIcon )
1064
					{
1065
 
1066
						System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainGui::typeid));
1067
						this->PictureDisplay->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"PictureDisplay.Image")));
1068
					}
1069
 
1070
					if ( p->GetType() != TYPE_ARCHIVE )
1071
						this->PanelDisplay->Show();
1072
				}	
1073
			}				
1074
		}
1075
 
1076
		// enable/disable the buttons connected to the package list
1077
		ButUninstall->Enabled = buttonEnabled;
1078
		ButDisable->Enabled = buttonEnabled;
1079
	}
1080
 
1081
	void MainGui::AddDirectoryEvent(System::Object ^Sender, System::EventArgs ^E)
1082
	{
1083
		OpenFileDialog ^ofd = gcnew OpenFileDialog();
1084
		ofd->Filter = "X-Universe Executable|";
1085
		String ^games = "";
1086
		for ( int i = 0; i < m_pPackages->GetGameExe()->GetNumGames(); i++ )
1087
		{
1088
			SGameExe *exe = m_pPackages->GetGameExe()->GetGame(i);
1089
			if ( i ) ofd->Filter += ";";
1090
			ofd->Filter += SystemStringFromCyString(exe->sExe);
1091
			games += "|" + SystemStringFromCyString(exe->sName) + "|" + SystemStringFromCyString(exe->sExe);
1092
		}
1093
		ofd->Filter += games;
1094
		ofd->FilterIndex = 1;
1095
		ofd->RestoreDirectory = true;
1096
		if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
1097
		{
1098
			CyString dir = CyStringFromSystemString(IO::FileInfo(ofd->FileName).DirectoryName);
1099
			// check its a valid directory
1100
			if ( !dir.Empty() )
1101
			{
1102
				CyString gameName = m_pPackages->GetGameName(dir);
1103
				if ( gameName.Empty() )
1104
					this->DisplayMessageBox(false, "Add Directory Error", "No X-Universe game found in folder:\n" + SystemStringFromCyString(dir), MessageBoxButtons::OK, MessageBoxIcon::Error);
1105
				else
1106
				{
1107
					// lets check if theres an old folder
1108
					if ( m_pPackages->IsOldDir(dir) )
1109
					{
1110
						if ( this->DisplayMessageBox(false, "Update Game Directory", "Game directory: " + SystemStringFromCyString(dir) + "\nIs currently being controled by the old plugin manager\nIf you continue, you will be unable to use the old version again\nDo you wish to continue?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) != System::Windows::Forms::DialogResult::Yes )
1111
							return;
1112
					}
1113
					m_iBackgroundTask = MGUI_BACKGROUND_ADDDIR;
1114
					this->Enabled = false;
1115
					CyString properDir = m_pPackages->GetProperDir(dir);
1116
					ComboDir->Items->Add(SystemStringFromCyString(properDir));
1117
					ComboDir->Text = SystemStringFromCyString(properDir);
1118
 
1119
					m_pRemovedDirList->Remove(properDir, true);
1120
					GetGameDirs(dir, false, true);
1121
					/*int lang = m_pPackages->GetGameLanguage(dir);
1122
					if ( lang )
1123
						m_pDirList->PushFront(properDir, CyString::Number(lang) + "|" + gameName);
1124
					else
1125
						m_pDirList->PushFront(properDir, gameName);*/
1126
					this->StartBackground(MGUI_BACKGROUND_ADDDIR, SystemStringFromCyString(properDir));
1127
				}
1128
			}
1129
		}
1130
	}
1131
 
1132
	bool MainGui::EnablePackage(CBaseFile *p)
1133
	{
1134
		if ( !m_pPackages->PrepareEnablePackage(p) )
1135
		{
1136
			if ( m_pPackages->GetError() == PKERR_NOPARENT )
1137
				this->DisplayMessageBox(false, "Enable Error", "Error enabling package\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n\nParent mod is not enabled", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1138
			else if ( m_pPackages->GetError() == PKERR_MODIFIED )
1139
				this->DisplayMessageBox(false, "Enable Error", "Error enabling package\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n\nPackage is modified and game is currently set to vanilla\nSwitch to modified mode to enable", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1140
			else if ( m_pPackages->GetError() == PKERR_MISSINGDEP )
1141
			{
1142
				CyStringList depList;
1143
				m_pPackages->GetMissingDependacies(p, &depList, true);
1144
				CyString sDep;
1145
				for ( SStringList *strNode = depList.Head(); strNode; strNode = strNode->next )
1146
				{
1147
					if ( strNode->str.IsIn("|") )
1148
						sDep = strNode->str.GetToken("|", 1, 1) + " V" + strNode->str.GetToken("|", 2, 2) + " by " + strNode->data + "\n";
1149
					else
1150
						sDep = strNode->str + " by " + strNode->data + "\n";
1151
				}
1152
				this->DisplayMessageBox(false, "Enable Error", "Error enabling package\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n\nMissing Enabled Dependacies:\n" + SystemStringFromCyString(sDep), MessageBoxButtons::OK, MessageBoxIcon::Warning);
1153
			}
1154
			else
1155
				this->DisplayMessageBox(false, "Enable Error", "Error enabling package\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n\nUnknown Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1156
			return false;
1157
		}
1158
		return true;
1159
	}
1160
 
1161
	void MainGui::DisableList(ArrayList ^List)
1162
	{
1163
		bool skipShips = false;
1164
		int count = 0;
1165
 
1166
		for ( int i = 0; i < List->Count; i++ )
1167
		{
1168
			CBaseFile *p = this->FindPackageFromList(cli::safe_cast<ListViewItem ^>(List[i]));
1169
			if ( p )
1170
			{
1171
				if ( p->IsEnabled() )
1172
				{
1173
					if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
1174
					{
1175
						if ( this->DisplayMessageBox(false, "Disable Library", "Package: " + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\nThis is a library package and might be required for other installed packages\nDo you still wish to disable it?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::No )
1176
							continue;
1177
					}
1178
					if ( p->GetType() == TYPE_XSP )
1179
					{
1180
						if ( !this->DisplayTip(TIPSECTION_YESNO, TIP_SHIPDISABLE) )
1181
							skipShips = true;
1182
 
1183
						if ( skipShips )
1184
							continue;
1185
					}
1186
 
1187
					m_pPackages->PrepareDisablePackage(p);
1188
					++count;
1189
				}
1190
				else
1191
				{
1192
					this->EnablePackage(p);
1193
					++count;
1194
				}
1195
			}
1196
		}
1197
 
1198
		if ( count )
1199
		{
1200
			this->Enabled = false;
1201
			this->StartBackground(MGUI_BACKGROUND_DISABLE);
1202
		}
1203
	}
1204
 
1205
	void MainGui::DisableEvent(System::Object ^Sender, System::EventArgs ^E)
1206
	{
1207
		if ( !ListPackages->SelectedItems->Count )
1208
			return;
1209
 
1210
		bool skipShips = false;
1211
 
1212
		ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1213
		System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1214
		int count = 0;
1215
		ArrayList ^List = gcnew ArrayList();
1216
 
1217
		while ( myEnum->MoveNext() )
1218
			List->Add(safe_cast<ListViewItem ^>(myEnum->Current));
1219
 
1220
		if ( List->Count )
1221
			this->DisableList(List);
1222
	}
1223
 
1224
	void MainGui::PackageBrowserEvent(System::Object ^Sender, System::EventArgs ^E)
1225
	{
1226
		this->Enabled = false;
1227
		PackageBrowser ^mod = gcnew PackageBrowser(m_pPackages, this->imageList1);
1228
		if ( !mod->AnyPackages() )
1229
		{
1230
			System::String ^game;
1231
			if ( m_pDirList && m_pDirList->Count() && m_pDirList->Head() )
1232
			{
1233
				CyString sGame = m_pDirList->Head()->data;
1234
				if ( sGame.IsIn("|") )
1235
					sGame = sGame.GetToken("|", 2);
1236
				game = SystemStringFromCyString(sGame);
1237
			}
1238
			this->DisplayMessageBox(false, "No Available Packages", "No available packages found for " + game, MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1239
 
1240
			this->Enabled = true;
1241
			return;
1242
		}
1243
 
1244
		if ( m_bDirLocked ) {
1245
			this->DisplayLocked(false);
1246
			this->Enabled = true;
1247
			return;
1248
		}
1249
 
1250
		mod->SetExperimental(m_bExperimental);
1251
		mod->SetCheat(m_bCheat);
1252
		mod->SetShips(m_bShips);
1253
		if ( m_pPackages->IsVanilla() )
1254
			mod->SetSigned(true);
1255
		else
1256
			mod->SetSigned(m_bSigned);
1257
		mod->SetDownload(m_bDownloadable);
1258
		mod->UpdatePackages();
1259
		System::Windows::Forms::DialogResult result = mod->ShowDialog(this);
1260
 
1261
		m_bDownloadable = mod->IsDownload();
1262
		m_bCheat = mod->IsCheat();
1263
		m_bExperimental = mod->IsExperimental();
1264
		m_bShips = mod->IsShips();
1265
		m_bSigned = mod->IsSigned();
1266
 
1267
		bool doenable = true;
1268
		CBaseFile *p = mod->SelectedMod();
1269
		if ( result == System::Windows::Forms::DialogResult::OK )
1270
		{
1271
			if ( p )
1272
			{
1273
				if ( this->InstallPackage(SystemStringFromCyString(p->GetFilename()), true, false, true) )
1274
					doenable = false;
1275
			}
1276
		}
1277
		else if ( result == System::Windows::Forms::DialogResult::Yes )
1278
			this->StartInstalling(false, true);
1279
 
1280
		this->Enabled = doenable;
1281
	}
1282
 
1283
	void MainGui::CloseEvent(System::Object ^Sender, FormClosingEventArgs ^E)
1284
	{
1285
		int h = this->Size.Height;
1286
		int w = this->Size.Width;
1287
	}
1288
 
1289
	void MainGui::DisplayLocked(bool inthread) 
1290
	{
1291
		this->DisplayMessageBox(inthread, "Directory Locked", "The current directory is locked and unable to make any changes\nYou may need to adjust the directory permissions", MessageBoxButtons::OK, MessageBoxIcon::Error);
1292
	}
1293
 
1294
	void MainGui::ModSelectorEvent(System::Object ^Sender, System::EventArgs ^E)
1295
	{
1296
		if ( m_pPackages->IsVanilla() )
1297
		{
1298
			this->DisplayMessageBox(false, "Mod Selector", "Currently in Vanilla Mode, You can only enable mods when in Modified Mode\n\nSwitch to modified mode if you wish to install mods", MessageBoxButtons::OK, MessageBoxIcon::Question);
1299
			return;
1300
		}
1301
		this->Enabled = false;
1302
		ModSelector ^mod = gcnew ModSelector(m_pPackages, this->imageList1);
1303
 
1304
		if ( !mod->AnyPackages() )
1305
		{
1306
			this->DisplayMessageBox(false, "Mod Selector", "No available mods have been found", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1307
			this->Enabled = true;
1308
			return;
1309
		}
1310
 
1311
		if ( m_bDirLocked ) {
1312
			this->DisplayLocked(false);
1313
			this->Enabled = true;
1314
			return;
1315
		}
1316
 
1317
		if ( !m_bModSelectorDetails )
1318
		{
1319
			mod->HideDetails();
1320
			mod->Update();
1321
		}
1322
 
1323
		System::Windows::Forms::DialogResult result = mod->ShowDialog(this);
1324
		m_bModSelectorDetails = mod->ShowingDetails();
1325
 
1326
		// install the selected mod
1327
		bool reEnable = true;
1328
 
1329
		CBaseFile *p = mod->SelectedMod();
1330
		if ( result == System::Windows::Forms::DialogResult::OK )
1331
		{
1332
			if ( p )
1333
			{
1334
				// from file
1335
				if ( p->GetNum() < 0 )
1336
				{
1337
					if ( m_pPackages->GetEnabledMod() )
1338
						m_pPackages->DisablePackage(m_pPackages->GetEnabledMod(), 0, 0);
1339
					if ( this->InstallPackage(SystemStringFromCyString(p->GetFilename()), true, false, true) )
1340
						reEnable = false;
1341
				}
1342
				// otherwise just enable it
1343
				else
1344
				{
1345
					if ( this->EnablePackage(p) )
1346
					{
1347
						this->StartBackground(MGUI_BACKGROUND_DISABLE);
1348
						reEnable = false;
1349
					}
1350
				}
1351
			}
1352
		}
1353
 
1354
		// install downloaded mods
1355
		else if ( result == Windows::Forms::DialogResult::Yes )
1356
			this->StartInstalling(false, true);
1357
 
1358
		// remove the current mod
1359
		else if ( result == System::Windows::Forms::DialogResult::Abort )
1360
		{
1361
			if ( m_pPackages->GetEnabledMod() )
1362
			{
1363
				CBaseFile *mod = m_pPackages->GetEnabledMod();
1364
				CyString message = mod->GetFullPackageName(m_pPackages->GetLanguage());
1365
				m_pPackages->DisablePackage(m_pPackages->GetEnabledMod(), 0, 0);			
1366
				this->DisplayMessageBox(false, "Mod Disabed", SystemStringFromCyString(message) + " has been disabled\nYour game is no longer using any mods\n", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1367
			}
1368
		}
1369
 
1370
		// uninstall the selected mod
1371
		else if ( result == System::Windows::Forms::DialogResult::Retry )
1372
		{
1373
			if ( p && p->GetNum() >= 0 )
1374
			{
1375
				m_pPackages->PrepareUninstallPackage(p);
1376
				if ( m_pPackages->GetNumPackagesInQueue() )
1377
				{
1378
					reEnable = false;
1379
					this->StartBackground(MGUI_BACKGROUND_UNINSTALL);
1380
				}
1381
			}
1382
		}
1383
 
1384
		if ( reEnable )
1385
			this->Enabled = true;
1386
 
1387
		mod->RemovePackages();
1388
	}
1389
 
1390
	void MainGui::UninstallList(ArrayList ^List)
1391
	{
1392
		bool skipShips = false;
1393
 
1394
		for ( int i = 0; i < List->Count; i++ )
1395
		{
1396
			CBaseFile *p = this->FindPackageFromList(safe_cast<ListViewItem ^>(List[i]));
1397
			if ( p )
1398
			{
1399
				if ( p->GetType() == TYPE_XSP )
1400
				{
1401
					if ( !this->DisplayTip(TIPSECTION_YESNO, TIP_SHIPUNINSTALL) )
1402
						skipShips = true;
1403
 
1404
					if ( skipShips )
1405
						continue;
1406
				}
1407
 
46 cycrow 1408
				// display uninstall text
1409
				Utils::String beforeText = m_pPackages->GetUninstallBeforeText(p).ToString();
1410
				if ( !beforeText.empty() ) {
1411
					if ( this->DisplayMessageBox(false, "Uninstall Package", _US(p->GetFullPackageName(m_pPackages->GetLanguage()).ToString() + "\n" + beforeText + "\n\nDo you want to uninstall this package?"), MessageBoxButtons::YesNo, MessageBoxIcon::Question) != System::Windows::Forms::DialogResult::Yes )
1412
						continue;
1413
				}
1414
 
1 cycrow 1415
				m_pPackages->PrepareUninstallPackage(p);
1416
			}
1417
		}
1418
 
1419
		if ( m_pPackages->GetNumPackagesInQueue() )
1420
		{
1421
			this->Enabled = false;
1422
			this->StartBackground(MGUI_BACKGROUND_UNINSTALL);
1423
		}
1424
	}
1425
	void MainGui::UninstallEvent(System::Object ^Sender, System::EventArgs ^E)
1426
	{
1427
		if ( !ListPackages->SelectedItems->Count )
1428
			return;
1429
 
1430
		ArrayList ^List = gcnew ArrayList();
1431
 
1432
		ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1433
		System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1434
 
1435
		while ( myEnum->MoveNext() )
1436
			List->Add(safe_cast<ListViewItem ^>(myEnum->Current));
1437
 
1438
		this->UninstallList(List);
1439
	}
1440
 
1441
	void MainGui::ModifiedEvent(System::Object ^Sender, System::EventArgs ^E)
1442
	{
1443
		if ( m_bDirLocked ) {
1444
			this->DisplayLocked(false);
1445
			return;
1446
		}
1447
		if ( m_pPackages->IsVanilla() )
1448
		{
1449
			this->Enabled = false;
1450
			if ( this->DisplayMessageBox(false, "Modified Mode", "Enabling modified mode will allow you to use any modified content\nThis will however mark any games you save as modified and you will be unable to participate in Uplink\nAny current save games wil also be backed up and kept seperate from your modified save games\n\nDo you wish to enable modified mode?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
1451
			{
1452
				if ( m_iSaveGameManager == 1 )
1453
				{
1454
					m_pPackages->BackupSaves(true);
1455
					m_pPackages->RestoreSaves(false);
1456
				}
1457
				m_pPackages->SetVanilla(false);
1458
				m_pMenuBar->Modified();
1459
				m_pPackages->PrepareEnableLibrarys();
1460
				m_pPackages->PrepareEnableFromVanilla();
1461
				this->StartBackground(MGUI_BACKGROUND_DISABLE);
1462
			}
1463
			else
1464
				this->Enabled = true;
1465
		}
1466
	}
1467
 
1468
	void MainGui::VanillaEvent(System::Object ^Sender, System::EventArgs ^E)
1469
	{
1470
		if ( m_bDirLocked ) {
1471
			this->DisplayLocked(false);
1472
			return;
1473
		}
1474
		if ( !m_pPackages->IsVanilla() )
1475
		{
1476
			this->Enabled = false;
1477
			if ( this->DisplayMessageBox(false, "Vanilla Mode", "Switching back to vanilla mode you will no longer be able to use any modifying packages, these will be disabled\nYour current save games will be backed up as modified saves, and any vanilla save games will be restored\n\nDo you wish to go back to Vanilla?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
1478
			{
1479
				if ( m_iSaveGameManager == 1 )
1480
				{
1481
					m_pPackages->RestoreSaves(true);
1482
					m_pPackages->BackupSaves(false);
1483
				}
1484
				m_pPackages->SetVanilla(true);
1485
				m_pMenuBar->Vanilla();
1486
				m_pPackages->PrepareDisableForVanilla();
1487
				this->StartBackground(MGUI_BACKGROUND_DISABLE);
1488
			}
1489
			else
1490
				this->Enabled = true;
1491
		}
1492
	}
1493
 
1494
	void MainGui::InstallEvent(System::Object ^Sender, System::EventArgs ^E)
1495
	{
1496
		if ( m_bDirLocked ) {
1497
			this->DisplayLocked(false);
1498
			return;
1499
		}
1500
 
1501
		OpenFileDialog ^ofd = gcnew OpenFileDialog();
1502
		ofd->Filter = "All (*.spk, *.xsp)|*.spk;*.xsp|Package Files (*.spk)|*.spk|Ship Files (*.xsp)|*.xsp";
1503
		ofd->FilterIndex = 1;
1504
		ofd->RestoreDirectory = true;
1505
		ofd->Multiselect = true;
1506
 
1507
		this->Enabled = false;
1508
		if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
1509
		{
1510
			bool anytoinstall = false;
1511
			array<System::String ^> ^fileArray = ofd->FileNames;
1512
			for ( int i = 0; i < fileArray->Length; i++ )
1513
			{
1514
				System::String ^file = fileArray[i];
1515
				if ( this->InstallPackage(file, false, false, true) )
1516
					anytoinstall = true;
1517
			}
1518
 
1519
			if ( anytoinstall )
1520
				this->StartInstalling(false, true);
1521
		}
1522
		else
1523
		{
1524
			ProgressBar->Hide();
1525
			this->Enabled = true;
1526
		}
1527
	}
1528
 
1529
	void MainGui::CheckUnusedShared()
1530
	{
1531
		if ( m_pPackages->AnyUnusedShared() )
1532
		{
1533
			if ( this->DisplayMessageBox(false, "Remove Shared Files", "You have some unused shared files, would you like to remove these?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes)
1534
				m_pPackages->RemoveUnusedSharedFiles();
1535
		}
1536
	}
1537
 
1538
	void MainGui::ChangeDirectoryEvent(System::Object ^Sender, System::EventArgs ^E)
1539
	{
1540
		if ( m_iBackgroundTask == MGUI_BACKGROUND_ADDDIR )
1541
			return;
1542
 
1543
		CyString dir = CyStringFromSystemString(ComboDir->Text);
1544
		if ( dir.NumToken(" [") )
1545
			dir = dir.GetToken(" [", 1, dir.NumToken(" [") - 1);
1546
 
1547
		if ( !m_pPackages->IsCurrentDir(dir) )
1548
		{
1549
			this->Enabled = false;
1550
			this->CheckUnusedShared();
1551
			this->StartBackground(MGUI_BACKGROUND_CHANGEDIR, SystemStringFromCyString(dir));
1552
		}
1553
	}
1554
 
1555
	void MainGui::Background_DoWork(System::Object ^Sender, DoWorkEventArgs ^E)
1556
	{
1557
		m_bDisplayMessage = false;
1558
		m_bDisplayDialog = false;
1559
 
1560
		switch ( m_iBackgroundTask )
1561
		{
1562
			case MGUI_BACKGROUND_INSTALL:
1563
				this->DoInstall(false, true);
1564
				break;
1565
			case MGUI_BACKGROUND_INSTALLBUILTIN:
1566
				this->DoInstall(true, true);
1567
				break;
1568
			case MGUI_BACKGROUND_UNINSTALL:
1569
				this->DoUninstall();
1570
				break;
1571
			case MGUI_BACKGROUND_DISABLE:
1572
				this->DoDisable();
1573
				break;
1574
			case MGUI_BACKGROUND_CHANGEDIR:
1575
			case MGUI_BACKGROUND_ADDDIR:
1576
				this->ChangeDirectory(CyStringFromSystemString(m_sBackgroundInfo));
1577
				break;
1578
			case MGUI_BACKGROUND_REMOVEDIR:
1579
				this->RemoveCurrentDirectory();
1580
		}
1581
	}
1582
 
1583
	void MainGui::Background_Finished()
1584
	{
1585
		ProgressBar->Hide();
1586
 
1587
		if ( m_bDisplayMessage )
1588
			MessageBox::Show(this, m_sMessageText, m_sMessageTitle, m_messageButtons, m_messageIcon);
1589
 
1590
		if ( m_bDisplayDialog )
1591
		{
1592
			if ( m_pPi->PackageCount() )
1593
			{
1594
				m_pPi->AdjustColumns();
1595
				m_pPi->ShowDialog(this);
1596
			}
1597
		}
1598
 
1599
		m_bDisplayDialog = false;
1600
		m_bDisplayMessage = false;
1601
 
1602
		if ( m_iBackgroundTask == MGUI_BACKGROUND_CHANGEDIR || m_iBackgroundTask == MGUI_BACKGROUND_ADDDIR )
1603
		{
1604
			// switch the dir list
1605
			if ( ComboDir->Text )
1606
			{
1607
				CyString dir = CyStringFromSystemString(ComboDir->Text);
1608
				if ( !dir.Empty() )
1609
				{
1610
					if ( (m_iBackgroundTask == MGUI_BACKGROUND_CHANGEDIR) && (dir.NumToken(" [")) )
1611
						dir = dir.GetToken(" [", 1, dir.NumToken(" [") - 1);
1612
					if ( m_pDirList->FindString(dir) )
1613
					{
1614
						CyString data = m_pDirList->FindString(dir)->data;
1615
						m_pDirList->Remove(dir, false);
1616
						m_pDirList->PushFront(dir, data);
1617
					}
1618
					else
1619
					{
1620
						int lang = m_pPackages->GetGameLanguage(dir);
1621
						if ( lang )
1622
							m_pDirList->PushFront(dir, CyString::Number(lang) + "|" + m_pPackages->GetGameName(dir));
1623
						else
1624
							m_pDirList->PushFront(dir, m_pPackages->GetGameName(dir));
1625
					}
1626
 
1627
					this->UpdateDirList();
1628
				}
1629
			}
1630
		}
1631
 
1632
		// display any files that failed
1633
		if ( m_iBackgroundTask == MGUI_BACKGROUND_INSTALL )
1634
		{
1635
			String ^files = "";
1636
			for ( SStringList *str = m_pFileErrors->Head(); str; str = str->next )
1637
			{
1638
				if ( str->data.ToInt() == SPKINSTALL_WRITEFILE_FAIL )
1639
				{
1640
					files += "\n";
1641
					files += SystemStringFromCyString(str->str);
1642
				}
1643
			}
1644
 
1645
			if ( files->Length )
1646
				MessageBox::Show(this, "These files failed to install\n" + files, "Failed Files", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1647
		}
1648
 
1649
		switch ( m_iBackgroundTask )
1650
		{
1651
			case MGUI_BACKGROUND_CHANGEDIR:
1652
			case MGUI_BACKGROUND_ADDDIR:
1653
				this->UpdateControls();
1654
				this->UpdatePackages();
1655
				this->CheckProtectedDir();
1656
				m_bRunningBackground = false;
1657
				if ( this->UpdateBuiltInPackages() )
1658
					return;
1659
				break;
1660
 
1661
			case MGUI_BACKGROUND_INSTALL:
1662
			case MGUI_BACKGROUND_INSTALLBUILTIN:
1663
			case MGUI_BACKGROUND_UNINSTALL:
1664
			case MGUI_BACKGROUND_DISABLE:
1665
				this->UpdatePackages();
1666
				break;
1667
			case MGUI_BACKGROUND_REMOVEDIR:
1668
				ComboDir->Items->RemoveAt(0);
1669
				this->DisplayMessageBox(false, "Remove Directory", "Directory has been removed\n" + SystemStringFromCyString(m_pRemovedDirList->Tail()->str), MessageBoxButtons::OK, MessageBoxIcon::Information);
1670
				break;
1671
		}
1672
 
1673
		if ( m_iBackgroundTask == MGUI_BACKGROUND_ADDDIR )
1674
			this->DisplayMessageBox(false, "Add Game Folder", "Folder \"" + m_sBackgroundInfo + "\" added\nGame: " + SystemStringFromCyString(m_pPackages->GetGameName(CyStringFromSystemString(m_sBackgroundInfo))), MessageBoxButtons::OK, MessageBoxIcon::Information);
1675
 
1676
		else if ( m_iBackgroundTask == MGUI_BACKGROUND_REMOVEDIR )
1677
		{
1678
			// no more directories left
1679
			if ( m_pDirList->Empty() )
1680
			{
1681
				m_bRunningBackground = false;
1682
				this->Close();
1683
			}
1684
			// otherwise, open the next directory
1685
			else
1686
			{
1687
				m_bRunningBackground = false;
1688
				ComboDir->SelectedItem = ComboDir->Items[0];
1689
//				this->StartBackground(MGUI_BACKGROUND_CHANGEDIR, SystemStringFromCyString(m_pDirList->Head()->str));
1690
				return;
1691
			}
1692
		}
1693
 
1694
		m_iBackgroundTask = MGUI_BACKGROUND_NONE;
1695
 
1696
		this->Enabled = true;
1697
 
1698
		m_bRunningBackground = false;
1699
	}
1700
 
1701
	void MainGui::Background_Progress(System::Object ^Sender, ProgressChangedEventArgs ^E)
1702
	{
1703
		this->ProgressBar->Value = E->ProgressPercentage;
1704
	}
1705
 
1706
	bool MainGui::UpdateBuiltInPackages()
1707
	{
1708
		// find all built-in packages
1709
		if ( System::IO::Directory::Exists(".\\Required") ) 
1710
		{
1711
			bool installing = false;
1712
			array <System::String ^> ^Files = System::IO::Directory::GetFiles(".\\Required", "*.spk");
1713
 
1714
			for ( int i = 0; i < Files->Length; i++ )
1715
			{
1716
				CyString file = CyStringFromSystemString(Files[i]);
1717
				int error;
1718
				CBaseFile *p = m_pPackages->OpenPackage(file, &error, 0, SPKREAD_NODATA);
1719
				if ( !p )
1720
					continue;
1721
 
1722
				if ( !((CSpkFile *)p)->IsLibrary() )
1723
					continue;
1724
 
1725
				if ( !p->CheckGameCompatability(m_pPackages->GetGame()) )
1726
					continue;
1727
 
1728
				// if its installed, check if we have a newer version
1729
				CBaseFile *check = m_pPackages->FindSpkPackage(p->GetName(), p->GetAuthor());
1730
				if ( check )
1731
				{
1732
					if ( check->GetVersion().CompareVersion(p->GetVersion()) != COMPARE_OLDER )
1733
					{
1734
						this->InstallPackage(Files[i], false, true, true);
1735
						installing = true;
1736
					}
1737
				}
1738
				else
1739
				{
1740
					this->InstallPackage(Files[i], false, true, true);
1741
					installing = true;
1742
				}
1743
 
1744
				delete p;
1745
			}
1746
 
1747
			if ( installing )
1748
				this->StartInstalling(true, true);
1749
			return installing;
1750
		}
1751
 
1752
		return false;
1753
	}
1754
 
1755
	////
1756
	// Auto Update
1757
	////
1758
	void MainGui::AutoUpdate()
1759
	{
1760
		if ( !m_bAutoUpdate || !System::IO::File::Exists( ".\\AutoUpdater.exe") )
1761
			return;
1762
 
1763
		// load the dir list
1764
		if ( !m_pUpdateList )
1765
		{
1766
			m_pUpdateList = new CyStringList;
1767
 
1768
			// TODO: read addresses from data
1769
 
1770
			// hardcoded address
1771
			m_pUpdateList->PushBack("http://cycrow.thexuniverse.us/pmupdate.dat", "", true);
1772
			if ( (int)PMLBETA )
1773
				m_pUpdateList->PushBack("http://cycrow.thexuniverse.us/Beta/pmupdatebeta.dat", "", true);
1774
		}
1775
 
1776
		backgroundUpdater->RunWorkerAsync();
1777
	}
1778
 
1779
	void MainGui::Updater_Finished(System::Object ^Sender, RunWorkerCompletedEventArgs ^E)
1780
	{
1781
		if ( !m_pUpdateList )
1782
			return;
1783
		if ( !m_pUpdateList->Head() )
1784
		{
1785
			delete m_pUpdateList;
1786
			m_pUpdateList = NULL;
1787
			return;
1788
		}
1789
 
1790
		this->Enabled = false;
1791
 
1792
		CyString server = m_pUpdateList->Head()->str;
1793
		CyString data = m_pUpdateList->Head()->data;
1794
 
1795
		m_pUpdateList->PopFront();
1796
 
1797
		// lets check if we have an update
1798
		if ( data.GetToken(" ", 1, 1) != "!ERROR!" )
1799
		{
1800
			CyString download;
1801
			CyString message;
1802
			int max;
1803
			CyString *strs = data.SplitToken("\n", &max);
1804
			if ( strs )
1805
			{
1806
				for ( int i = 0; i < max; i++ )
1807
				{
1808
					CyString cmd = strs[i].GetToken(":", 1, 1);
1809
					CyString rest = strs[i].GetToken(":", 2);
1810
					rest.RemoveFirstSpace();
1811
					if ( cmd.Compare("SPKVERSION") )
1812
					{
1813
						float v = rest.GetToken(" ", 1, 1).ToFloat();
1814
						if ( v > GetLibraryVersion() )
1815
						{
1816
							message += "New version of the SPK Libraries available\nCurrent = ";
1817
							message += CyString::CreateFromFloat(GetLibraryVersion(), 2);
1818
							message += "\nNew Version = ";
1819
							message += CyString::CreateFromFloat(v, 2);
1820
							message += "\n\n";
1821
 
1822
							CyString filename = rest.GetToken(" ", 2);
1823
							if ( download.Empty() )
1824
								download = filename;
1825
							else
1826
							{
1827
								download += "|";
1828
								download += filename;
1829
							}
1830
						}
1831
					}
1832
					else if ( cmd.Compare("PMLVERSION") )
1833
					{
1834
						float v = rest.GetToken(" ", 1, 1).ToFloat();
1835
						int beta = rest.GetToken(" ", 2, 2).ToInt();
1836
 
1837
						bool newVersion = false;
1838
						// new version
1839
						if ( v > (float)PMLVERSION )
1840
							newVersion = true;
1841
						// same version, check beta/rc
1842
						if ( v == (float)PMLVERSION )
1843
						{
1844
							// newer beta version
1845
							if ( beta > (int)PMLBETA && (int)PMLBETA > 0 )
1846
								newVersion = true;
1847
							// current is beta, new is an RC
1848
							else if ( (int)PMLBETA > 0 && beta < 0 )
1849
								newVersion = true;
1850
							// current is rc, new is an rc
1851
							else if ( (int)PMLBETA < 0 && beta < 0 && beta < (int)PMLBETA )
1852
								newVersion = true;
1853
							// current is beta or rc, new is not, so its newer
1854
							else if ( (int)PMLBETA != 0 && beta == 0 )
1855
								newVersion = true;
1856
						}
1857
 
1858
						if ( newVersion )
1859
						{
1860
							message += "New version of the ";
1861
							message += CyStringFromSystemString(GetProgramName(m_bAdvanced));
1862
							message += " available\nCurrent = ";
1863
							message += CyStringFromSystemString(PluginManager::GetVersionString());
1864
							message += "\nNew Version = ";
1865
							message += CyStringFromSystemString(PluginManager::GetVersionString(v, beta));
1866
							message += "\n\n";
1867
							if ( download.Empty() )
1868
								download = rest.GetToken(" ", 3);
1869
							else
1870
							{
1871
								download += "|";
1872
								download += rest.GetToken(" ", 3);
1873
							}
1874
						}
1875
					}
1876
				}
1877
			}
1878
 
1879
			CLEANSPLIT(strs, max)
1880
 
1881
			if ( !download.Empty() && !message.Empty() )
1882
			{
1883
				if ( this->DisplayMessageBox(false, "Updater", SystemStringFromCyString(message) + "Do You wish to download and install it?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
1884
				{
1885
					// absolute address
1886
					CyString downloadFile;
1887
 
1888
					int max;
1889
					CyString *strs = download.SplitToken("|", &max);
1890
					for ( int i = 0; i < max; i++ )
1891
					{
1892
						CyString d = strs[i];
1893
						// relative address
1894
						if ( !d.Left(7).Compare("http://") && !d.Left(4).Compare("www.") )
1895
							d = server.DelToken("/", server.NumToken("/")) + "/" + d;
1896
 
1897
						if ( downloadFile.Empty() )
1898
							downloadFile = d;
1899
						else
1900
						{
1901
							downloadFile += "|";
1902
							downloadFile += d;
1903
						}
1904
					}
1905
 
1906
					CLEANSPLIT(strs, max);
1907
 
1908
					if ( !downloadFile.Empty() )
1909
					{
1910
						m_sDownload = SystemStringFromCyString(downloadFile);
1911
						this->Close();
1912
						return;
1913
					}
1914
				}
1915
			}
1916
		}
1917
 
1918
		// otherwise, lets continue with the next server
1919
		if ( m_pUpdateList->Head() )
1920
			backgroundUpdater->RunWorkerAsync();
1921
		else
1922
		{
1923
			delete m_pUpdateList;
1924
			m_pUpdateList = NULL;
1925
		}
1926
 
1927
		this->Enabled = true;
1928
	}
1929
 
1930
	void MainGui::TimerEvent_CheckFile(System::Object ^Sender, System::EventArgs ^E)
1931
	{
1932
		if ( m_bRunningBackground )
1933
			return;
1934
 
1935
		System::String ^mydoc = Environment::GetFolderPath(Environment::SpecialFolder::Personal );
1936
 
1937
		bool anytoinstall = false;
1938
 
1939
		if ( System::IO::File::Exists(mydoc + "\\Egosoft\\pluginmanager_load.dat") )
1940
		{
1941
			System::String ^lines = System::IO::File::ReadAllText(mydoc + "\\Egosoft\\pluginmanager_load.dat");
1942
			System::IO::File::Delete(mydoc + "\\Egosoft\\pluginmanager_load.dat");
1943
			if ( lines )
1944
			{
1945
				CyString strLines = CyStringFromSystemString(lines);
1946
				int num;
1947
				CyString *aLines = strLines.SplitToken("\n", &num);
1948
				if ( num && aLines )
1949
				{
1950
					for ( int i = 0; i < num; i++ )
1951
					{
1952
						CyString l = aLines[i];
1953
						l = l.Remove("\r");
1954
						CyString first = l.GetToken(":", 1, 1);
1955
						CyString rest = l.GetToken(":", 2);
1956
						rest.RemoveFirstSpace();
1957
 
1958
						if ( first.Compare("File") )
1959
						{
1960
							if ( m_bDirLocked ) {
1961
								this->DisplayLocked(false);
1962
								return;
1963
							}
1964
							if ( this->InstallPackage(SystemStringFromCyString(rest), false, false, true) )
1965
								anytoinstall = true;
1966
						}
1967
					}
1968
 
1969
					CLEANSPLIT(aLines, num);
1970
				}
1971
			}
1972
		}
1973
 
1974
		if ( anytoinstall )
1975
			this->StartInstalling(false, true);
1976
	}
1977
 
1978
	void MainGui::Updater_DoWork(System::Object ^Sender, DoWorkEventArgs ^E)
1979
	{
1980
		if ( !m_pUpdateList )
1981
			return;
1982
		if ( !m_pUpdateList->Head() )
1983
		{
1984
			delete m_pUpdateList;
1985
			m_pUpdateList = NULL;
1986
			return;
1987
		}
1988
 
1989
		try 
1990
		{
1991
			System::Net::WebClient ^Client = gcnew System::Net::WebClient();
1992
 
1993
			System::IO::Stream ^strm = Client->OpenRead(SystemStringFromCyString(m_pUpdateList->Head()->str));
1994
			System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
1995
			System::String ^read = sr->ReadToEnd();
1996
			strm->Close();
1997
			sr->Close();
1998
 
1999
			m_pUpdateList->Head()->data = CyStringFromSystemString(read);
2000
		}
2001
		catch (System::Net::WebException ^ex)
2002
		{
2003
			m_pUpdateList->Head()->data = CyStringFromSystemString("!ERROR! " + ex->ToString());
2004
			if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
2005
			{
2006
				m_pUpdateList->Head()->data = CyStringFromSystemString("!ERROR! " + ex->ToString());
2007
 
2008
			}
2009
		}
2010
	}
2011
 
2012
	void MainGui::RemoveDirectory()
2013
	{
2014
		if ( !m_pPackages->IsLoaded() )
2015
			return;
2016
 
2017
		System::String ^remDir = SystemStringFromCyString(m_pPackages->GetCurrentDirectory());
2018
		this->Enabled = false;
2019
		if ( this->DisplayMessageBox(false, "Remove Directory", "Are you sure you want to remove this directory:\n" + remDir + "\n\nThis will remove all installed packages and clear any settings", MessageBoxButtons::YesNo, MessageBoxIcon::Question) != System::Windows::Forms::DialogResult::Yes)
2020
		{
2021
			this->Enabled = true;
2022
			return;
2023
		}
2024
 
2025
		this->StartBackground(MGUI_BACKGROUND_REMOVEDIR, remDir);
2026
	}
2027
 
2028
	bool MainGui::RemoveCurrentDirectory()
2029
	{
2030
		if ( !m_pPackages->RemoveCurrentDirectory() )
2031
			return false;
2032
 
2033
		// remove directory from list
2034
		m_pRemovedDirList->PushBack(m_pDirList->Head()->str, "", true);
2035
		m_pDirList->PopFront();
2036
 
2037
		return true;
2038
	}
2039
 
2040
	void MainGui::LaunchGame()
2041
	{
2042
		if ( !System::IO::File::Exists(".\\GameLauncher.exe") )
2043
			return;
2044
 
2045
		m_sRun = SystemStringFromCyString(m_pPackages->GetGameRunExe());
2046
		this->Close();
2047
	}
2048
 
2049
	bool MainGui::DisplayTip(int tipsection, int tip)
2050
	{
2051
		if ( tipsection < 0 || tipsection >= MAXTIPS )
2052
			return false;
2053
 
2054
		STips ^tips = (STips ^)m_lTips[tipsection];
2055
		if ( !(tips->iTips & tip) )
2056
		{
2057
			System::String ^sTip = cli::safe_cast<System::String ^>(tips->sTips[(tip >> 1)]);
2058
 
2059
			tips->iTips |= tip;
2060
 
2061
			if ( this->DisplayMessageBox(false, "Plugin Manager Tip", sTip, MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
2062
				return true;
2063
			else 
2064
				return false;
2065
		}
2066
 
2067
		return true;
2068
	}
2069
 
2070
	void MainGui::SetTipStrings(int section)
2071
	{
2072
		STips ^t = (STips ^)m_lTips[section];
2073
		t->sTips = gcnew ArrayList();
2074
 
2075
		switch ( section )
2076
		{
2077
			case TIPSECTION_YESNO:
2078
				t->sTips->Add("You are about to uninstall a ship, you need to make sure that there are no ships in the sector you was in when you saved, otherwise it could prevent the save from loading\n\nContinue Uninstalling Ship?");
2079
				t->sTips->Add("You are about to disable a ship, you need to make sure that there are no ships in the sector you was in when you saved, otherwise it could prevent the save from loading\n\nContinue Disabling Ship?");
2080
				break;
2081
		}
2082
	}
2083
 
2084
	System::Windows::Forms::DialogResult MainGui::DisplayMessageBox(bool inthread, System::String ^title, System::String ^text, MessageBoxButtons buttons, MessageBoxIcon icon)
2085
	{
2086
		if ( !inthread )
2087
			return MessageBox::Show(this, text, title, buttons, icon);
2088
		else
2089
		{
2090
			m_bDisplayMessage = true;
2091
			m_sMessageText = text;
2092
			m_sMessageTitle = title;
2093
			m_messageIcon = icon;
2094
			m_messageButtons = buttons;
2095
 
2096
			return System::Windows::Forms::DialogResult::Abort;
2097
		}
2098
	}
2099
 
2100
	ListViewItem ^MainGui::FindSelectedItem()
2101
	{
2102
		Point ^mousePoint = this->ListPackages->PointToClient(this->contextMenuStrip1->MousePosition);
2103
		return  this->ListPackages->GetItemAt(mousePoint->X, mousePoint->Y);
2104
	}
2105
 
2106
	CBaseFile *MainGui::GetFileFromItem(ListViewItem ^item)
2107
	{
2108
		int num = System::Convert::ToInt32(item->Tag);
2109
		return m_pPackages->GetPackageAt(num);
2110
	}
2111
 
2112
	System::Void MainGui::OpenContextMenu(System::Object ^Sender, CancelEventArgs ^E)
2113
	{
2114
		m_pListItem = nullptr;
2115
		E->Cancel = true;
2116
		bool showSep = false;
2117
		bool showSep2 = false;
2118
 
2119
		ListViewItem ^item = this->FindSelectedItem();
2120
		CBaseFile *p = NULL;
2121
		if ( item )
2122
				p = this->GetFileFromItem(item);
2123
 
2124
		this->emailAuthorToolStripMenuItem->Visible = false;
2125
		this->visitForumPageToolStripMenuItem->Visible = false;
2126
		this->visitWebSiteToolStripMenuItem->Visible = false;
2127
		this->ContextDisable->Visible = false;
2128
		this->ContextEnable->Visible = false;
2129
		this->ContextName->Image = nullptr;
2130
		this->UninstallSelectedContext->Visible = false;
2131
		this->viewReadmeToolStripMenuItem->Visible = false;
2132
		this->extrasToolStripMenuItem->Visible = false;
2133
		this->checkForUpdatesToolStripMenuItem->Visible = false;
2134
 
2135
		if ( p 	|| this->ListPackages->SelectedItems->Count )
2136
		{
2137
 
2138
			if ( item && p )
2139
			{
2140
				m_pListItem = item;
2141
				this->ContextName->Text = item->Text;
2142
				if ( item->ImageIndex != -1 )
2143
					this->ContextName->Image = this->ListPackages->LargeImageList->Images[item->ImageIndex];
2144
				else if ( item->ImageKey )
2145
				{
2146
					int key = this->ListPackages->LargeImageList->Images->IndexOfKey(item->ImageKey);
2147
					if ( key != -1 )
2148
						this->ContextName->Image = this->ListPackages->LargeImageList->Images[key];
2149
				}
2150
				else if ( p->GetIcon() )
2151
					PluginManager::DisplayContextIcon(p, this->ContextName, nullptr);
2152
 
2153
				this->uninstallToolStripMenuItem->Text = "Uninstall: " + item->Text;
2154
 
2155
				this->viewReadmeToolStripMenuItem->DropDownItems->Clear();
2156
				if ( p->CountFiles(FILETYPE_README) )
2157
				{
2158
					for ( C_File *f = p->GetFirstFile(FILETYPE_README); f; f = p->GetNextFile(f) )
2159
					{
2160
						if ( f->GetBaseName().GetToken(".", 1, 1).IsNumber() )
2161
						{
2162
							if ( f->GetBaseName().GetToken(".", 1, 1).ToInt() != m_pPackages->GetLanguage() )
2163
								continue;
2164
						}
2165
						if ( f->GetBaseName().IsIn("-L") )
2166
						{
2167
							int pos = f->GetBaseName().FindPos("-L");
2168
							int l = f->GetBaseName().Mid(pos + 2, 3).ToInt();
2169
							if ( l != m_pPackages->GetLanguage() )
2170
								continue;
2171
						}
2172
 
2173
						Windows::Forms::ToolStripMenuItem ^item = gcnew Windows::Forms::ToolStripMenuItem();
2174
						item->Text = SystemStringFromCyString(f->GetFilename());
2175
						item->Image = this->viewReadmeToolStripMenuItem->Image;
2176
						item->ImageScaling = ToolStripItemImageScaling::None;
2177
						item->Click += gcnew System::EventHandler(this, &MainGui::RunItem);
2178
						item->Tag = SystemStringFromCyString(f->GetFilePointer());
2179
						this->viewReadmeToolStripMenuItem->DropDownItems->Add(item);
2180
					}
2181
 
2182
					if ( this->viewReadmeToolStripMenuItem->DropDownItems->Count )
2183
					{
2184
						this->viewReadmeToolStripMenuItem->Visible = true;
2185
						showSep = true;
2186
					}
2187
				}
2188
 
2189
				this->extrasToolStripMenuItem->DropDownItems->Clear();
2190
				if ( p->CountFiles(FILETYPE_EXTRA) )
2191
				{
2192
					showSep = true;
2193
					for ( C_File *f = p->GetFirstFile(FILETYPE_EXTRA); f; f = p->GetNextFile(f) )
2194
					{
2195
						if ( !f->GetDir().Left(6).Compare("extras") )
2196
							continue;
2197
 
2198
						Windows::Forms::ToolStripMenuItem ^item = gcnew Windows::Forms::ToolStripMenuItem();
2199
						item->Text = SystemStringFromCyString(f->GetFilename());
2200
						if ( this->imageList2->Images->IndexOfKey(SystemStringFromCyString(f->GetFileExt().ToLower())) > -1 )
2201
							item->Image = this->imageList2->Images[this->imageList2->Images->IndexOfKey(SystemStringFromCyString(f->GetFileExt().ToLower()))];
2202
						else
2203
						{
2204
							CyString exe = f->GetFilePointer();
2205
							exe = exe.FindReplace("/", "\\");
2206
							wchar_t wText[200];
2207
							::MultiByteToWideChar(CP_ACP, NULL, (char *)exe.c_str(), -1, wText, exe.Length() + 1);
2208
 
2209
							System::Drawing::Icon ^myIcon;
2210
							SHFILEINFO *shinfo = new SHFILEINFO();
2211
 
2212
							if ( FAILED(SHGetFileInfo(wText, 0, shinfo, sizeof(shinfo), SHGFI_ICON | SHGFI_LARGEICON)) )
2213
							{
2214
								if ( FAILED(SHGetFileInfo(wText, 0, shinfo, sizeof(shinfo), SHGFI_ICON | SHGFI_SMALLICON)) )
2215
									item->Image = this->imageList2->Images[0];
2216
								else
2217
								{
2218
									myIcon = System::Drawing::Icon::FromHandle(IntPtr(shinfo->hIcon));
2219
									item->Image = myIcon->ToBitmap();
2220
								}
2221
							}
2222
							else
2223
							{
2224
								myIcon = System::Drawing::Icon::FromHandle(IntPtr(shinfo->hIcon));
2225
								item->Image = myIcon->ToBitmap();
2226
							}
2227
 
2228
							delete shinfo;
2229
						}
2230
						item->ImageScaling = ToolStripItemImageScaling::None;
2231
						item->Click += gcnew System::EventHandler(this, &MainGui::RunItem);
2232
						item->Tag = SystemStringFromCyString(f->GetFilePointer());
2233
						this->extrasToolStripMenuItem->DropDownItems->Add(item);
2234
					}
2235
				}
2236
 
2237
				if ( this->extrasToolStripMenuItem->DropDownItems->Count )
2238
					this->extrasToolStripMenuItem->Visible = true;
2239
 
2240
				// email/website/forum
2241
				if ( !p->GetForumLink().Empty() )
2242
				{
2243
					CyString web = p->GetForumLink();
2244
					if ( web.IsNumber() )
2245
						web = CyString("http://forum.egosoft.com/viewtopic.php?t=") + web; 
2246
 
2247
					this->visitForumPageToolStripMenuItem->Visible = true;
2248
					if ( !web.IsIn("http://") )
2249
						this->visitForumPageToolStripMenuItem->Tag = "http://" + SystemStringFromCyString(web);
2250
					else
2251
						this->visitForumPageToolStripMenuItem->Tag = SystemStringFromCyString(web);
2252
					showSep2 = true;
2253
				}
2254
				if ( !p->GetEmail().Empty() )
2255
				{
2256
					this->emailAuthorToolStripMenuItem->Visible = true;
2257
					this->emailAuthorToolStripMenuItem->Tag = "mailto://" + SystemStringFromCyString(p->GetEmail()) + "?subject=Re: " + SystemStringFromCyString(p->GetName().FindReplace(" ", "%20"));
2258
					showSep2 = true;
2259
				}
2260
				if ( !p->GetWebSite().Empty() )
2261
				{
2262
					this->visitWebSiteToolStripMenuItem->Visible = true;
2263
					if ( !p->GetWebSite().IsIn("http://") )
2264
						this->visitWebSiteToolStripMenuItem->Tag = "http://" + SystemStringFromCyString(p->GetWebSite());
2265
					else
2266
						this->visitWebSiteToolStripMenuItem->Tag = SystemStringFromCyString(p->GetWebSite());
2267
					showSep2 = true;
2268
				}
2269
 
2270
				if ( !p->GetWebAddress().Empty() )
2271
					this->checkForUpdatesToolStripMenuItem->Visible = true;
2272
			}
2273
			else
2274
				m_pListItem = nullptr;
2275
 
2276
			if ( this->ListPackages->SelectedItems->Count > 1 || !p )
2277
			{
2278
				this->UninstallSelectedContext->Visible = true;
2279
				this->UninstallSelectedContext->Text = "Uninstall Selected (" + System::Convert::ToString(this->ListPackages->SelectedItems->Count) + " packages)";
2280
			}
2281
 
2282
			if ( p )
2283
			{
2284
				if ( p->IsEnabled() )
2285
					this->ContextDisable->Visible = true;
2286
				else
2287
					this->ContextEnable->Visible = true;
2288
			}
2289
 
2290
			this->ContextSeperator->Visible = showSep;
2291
			this->ContextSeperator2->Visible = showSep2;
2292
			E->Cancel = false;
2293
		}
2294
	}
2295
 
2296
	System::Void MainGui::ListPackages_DragOver(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e)
2297
	{
2298
		e->Effect = DragDropEffects::None;
2299
 
2300
		if (e->Data->GetDataPresent(DataFormats::FileDrop)) 
2301
		{
2302
			cli::array<String ^> ^a = (cli::array<String ^> ^)e->Data->GetData(DataFormats::FileDrop, false);
2303
			int i;
2304
			for(i = 0; i < a->Length; i++)
2305
			{
2306
				String ^s = a[i];
2307
				String ^ext = IO::FileInfo(s).Extension;
2308
				if ( String::Compare(IO::FileInfo(s).Extension, ".xsp", true) == 0 || String::Compare(IO::FileInfo(s).Extension, ".spk", true) == 0 )
2309
				{
2310
					e->Effect = DragDropEffects::Copy;
2311
					break;
2312
				}
2313
			}
2314
		}
2315
	}
2316
 
2317
	System::Void MainGui::ListPackages_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e)
2318
	{
2319
		if (e->Data->GetDataPresent(DataFormats::FileDrop)) 
2320
		{
2321
			cli::array<String ^> ^a = (cli::array<String ^> ^)e->Data->GetData(DataFormats::FileDrop, false);
2322
			int i;
2323
			for(i = 0; i < a->Length; i++)
2324
			{
2325
				String ^s = a[i];
2326
				String ^ext = IO::FileInfo(s).Extension;
2327
				if ( String::Compare(IO::FileInfo(s).Extension, ".xsp", true) == 0 || String::Compare(IO::FileInfo(s).Extension, ".spk", true) == 0 )
2328
				{
2329
					if ( m_bDirLocked ) {
2330
						this->DisplayLocked(false);
2331
						return;
2332
					}
2333
					this->InstallPackage(s, false, false, true);
2334
				}
2335
			}
2336
 
2337
			this->StartInstalling(false, true);
2338
		}
2339
	}
2340
 
2341
	bool MainGui::CheckAccessRights(String ^dir)
2342
	{
2343
		/*
2344
		// check if already exists
2345
		String ^file = dir + "\\accessrightscheck.dat";
2346
		String ^writeStr = "testing file access";
2347
		if ( IO::File::Exists(file) )
2348
		{
2349
			// remove it
2350
			IO::File::Delete(file);
2351
			// still exists, cant delete it
2352
			if ( IO::File::Exists(file) )
2353
				return false;
2354
		}
2355
 
2356
		IO::DirectoryInfo ^dInfo = gcnew IO::DirectoryInfo(dir);
2357
		Security::AccessControl::DirectorySecurity ^dSecurity = dInfo->GetAccessControl();
2358
		dSecurity->
2359
 
2360
 
2361
		System::IO::FileStream ^writeStream = nullptr;
2362
		IO::BinaryWriter ^writer = nullptr;
2363
		try {
2364
			 writeStream = gcnew System::IO::FileStream(file, System::IO::FileMode::Create);
2365
			 writer = gcnew IO::BinaryWriter(writeStream);
2366
		}
2367
		catch (System::IO::IOException ^e)
2368
		{
2369
			MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2370
		}
2371
		catch (System::Exception ^e)
2372
		{
2373
			MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2374
		}
2375
		finally 
2376
		{
2377
			writer->Write(writeStr);
2378
			writer->Close();
2379
			writeStream->Close();
2380
		}
2381
 
2382
		// check if its written
2383
		if ( !IO::File::Exists(file) )
2384
			return false;
2385
 
2386
		// remove the file again
2387
		IO::File::Delete(file);
2388
		if ( IO::File::Exists(file) )
2389
			return false;
2390
*/
2391
		return true;
2392
	}
2393
 
2394
	System::Void MainGui::RunItem(System::Object ^sender, System::EventArgs ^e)
2395
	{
2396
		Windows::Forms::ToolStripMenuItem ^item = cli::safe_cast<ToolStripMenuItem ^>(sender);
2397
		String ^file = Convert::ToString(item->Tag);
2398
 
2399
		if ( IO::File::Exists(file) )
2400
		{
2401
			System::Diagnostics::Process::Start(file);
2402
		}
2403
	}
2404
 
2405
	void MainGui::RunFromToolItem(ToolStripMenuItem ^item)
2406
	{
2407
		if ( !item ) return;
2408
		if ( !item->Tag ) return;
2409
 
2410
		String ^file = Convert::ToString(item->Tag);
2411
		System::Diagnostics::Process::Start(file);
2412
	}
2413
 
2414
	void MainGui::FakePatchControlDialog()
2415
	{
2416
		FakePatchControl ^fpc = gcnew FakePatchControl(m_pPackages);
2417
		if ( fpc->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2418
		{
2419
			m_pPackages->ApplyFakePatchOrder(fpc->GetPatchOrder());
2420
			m_pPackages->ShuffleFakePatches(0);
2421
		}
2422
	}
2423
 
2424
	void MainGui::CheckFakePatchCompatability()
2425
	{
2426
		CyStringList errorList;
2427
		int count = 0;
2428
		int packageCount = 0;
2429
		for ( CBaseFile *p = m_pPackages->GetFirstPackage(); p; p = m_pPackages->GetNextPackage(p) )
2430
		{
2431
			if ( !p->IsEnabled() ) continue;
2432
			if ( !p->AnyFileType(FILETYPE_MOD) ) continue;
2433
 
2434
			CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
2435
 
2436
			// compare this file against all other packages
2437
			for ( CBaseFile *comparePackage = m_pPackages->GetNextPackage(p); comparePackage; comparePackage = m_pPackages->GetNextPackage(comparePackage) )
2438
			{
2439
				if ( comparePackage == p ) continue; // dont include the same package
2440
				if ( !comparePackage->IsEnabled() ) continue;
2441
				if ( !comparePackage->AnyFileType(FILETYPE_MOD) ) continue;
2442
 
2443
				CyStringList list;
2444
				if ( m_pPackages->CheckCompatabilityBetweenMods(p, comparePackage, &list) )
2445
				{
2446
					CyString package2Name = comparePackage->GetFullPackageName(m_pPackages->GetLanguage());
2447
					for ( SStringList *str = list.Head(); str; str = str->next )
2448
					{
2449
						errorList.PushBack(str->str + " (" + packageName + ")", str->data + " (" + package2Name + ")");
2450
						++count;
2451
					}
2452
					++packageCount;
2453
				}
2454
			}
2455
		}
2456
 
2457
		if ( count )
2458
		{
2459
			if ( MessageBox::Show(this, "Found incompatability between fake patches\n" + count + " errors found\n\nDo you wish to view the errors?", "Fake Patch Compatability", MessageBoxButtons::YesNo, MessageBoxIcon::Information) == Windows::Forms::DialogResult::Yes )
2460
			{
2461
				CompareList ^cl = gcnew CompareList("Fake Patch Incompatabilities");
2462
				cl->AddStringList(errorList);
2463
				cl->ShowDialog(this);
2464
			}
2465
		}
2466
		else
2467
			MessageBox::Show(this, "No incompatabilities found between fake patches", "Fake Patch Compatability", MessageBoxButtons::OK, MessageBoxIcon::Information);
2468
	}
2469
 
2470
	void MainGui::EditGlobalsDialog()
2471
	{
2472
		if ( m_pPackages->IsVanilla() ) {
2473
			this->DisplayMessageBox(false, "Edit Globals", "Currently in Vanilla Mode, Cant change globals without being modified\n\nSwitch to modified mode if you wish to edit globals", MessageBoxButtons::OK, MessageBoxIcon::Question);
2474
			return;
2475
		}
2476
		if ( m_bDirLocked ) {
2477
			this->DisplayLocked(false);
2478
			return;
2479
		}
2480
 
2481
		//load globals
2482
		CyStringList globals;
2483
		m_pPackages->ReadGlobals(globals);
2484
 
2485
		EditGlobals ^edit = gcnew EditGlobals(&globals);
2486
 
2487
		// make our saved changes
2488
		for ( SStringList *str = m_pPackages->GetGlobals()->Head(); str; str = str->next )
2489
			edit->SetEditedItem(SystemStringFromCyString(str->str), SystemStringFromCyString(str->data));
2490
 
2491
		if ( edit->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2492
		{
2493
			// compare whats different and save
2494
			m_pPackages->GetGlobals()->Clear();
2495
			for ( SStringList *str = edit->GetSavedSettings()->Head(); str; str = str->next )
2496
				m_pPackages->GetGlobals()->PushBack(str->str, str->data);
2497
		}
2498
	}
2499
 
2500
	void MainGui::ViewFileLog()
2501
	{
2502
		if ( m_pFileErrors->Empty() )
2503
			MessageBox::Show(this, "No messages to view in file log", "Empty File Log", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2504
		else
2505
		{
2506
			FileLog ^log = gcnew FileLog;
2507
			for ( SStringList *str = m_pFileErrors->Head(); str; str = str->next )
2508
			{
2509
				bool add = true;
2510
				String ^status = "Unknown Error";
2511
				switch(str->data.GetToken(" ", 1, 1).ToInt())
2512
				{
2513
					case SPKINSTALL_CREATEDIRECTORY:
2514
						status = "Created Directory";
2515
						break;
2516
					case SPKINSTALL_CREATEDIRECTORY_FAIL:
2517
						status = "Failed to create Directory";
2518
						break;
2519
					case SPKINSTALL_WRITEFILE:
2520
						status = "File Written";
2521
						break;
2522
					case SPKINSTALL_WRITEFILE_FAIL:
2523
						status = "Failed to Write File";
2524
						break;
2525
					case SPKINSTALL_DELETEFILE:
2526
						status = "Deleted File";
2527
						break;
2528
					case SPKINSTALL_DELETEFILE_FAIL:
2529
						status = "Failed to Delete File";
2530
						break;
2531
					case SPKINSTALL_SKIPFILE:
2532
						status = "File Skipped";
2533
						break;
2534
					case SPKINSTALL_REMOVEDIR:
2535
						status = "Removed Directory";
2536
						break;
2537
					case SPKINSTALL_ENABLEFILE:
2538
						status = "Enabled File";
2539
						break;
2540
					case SPKINSTALL_DISABLEFILE:
2541
						status = "Disabled File";
2542
						break;
2543
					case SPKINSTALL_ENABLEFILE_FAIL:
2544
						status = "Failed to Enable File";
2545
						break;
2546
					case SPKINSTALL_DISABLEFILE_FAIL:
2547
						status = "Failed to Disable File";
2548
						break;
2549
					case SPKINSTALL_UNINSTALL_MOVE:
2550
						status = "Moved Uninstall File";
2551
						break;
2552
					case SPKINSTALL_UNINSTALL_COPY:
2553
						status = "Copied Uninstall File";
2554
						break;
2555
					case SPKINSTALL_UNINSTALL_MOVE_FAIL:
2556
						status = "Failed to move uninstall file";
2557
						break;
2558
					case SPKINSTALL_UNINSTALL_COPY_FAIL:
2559
						status = "Failed to copy uninstall file";
2560
						break;
2561
					case SPKINSTALL_UNINSTALL_REMOVE:
2562
						status = "Removed uninstall file";
2563
						break;
2564
					case SPKINSTALL_UNINSTALL_REMOVE_FAIL:
2565
						status = "Failed to remove uninstall file";
2566
						break;
2567
					case SPKINSTALL_ORIGINAL_BACKUP:
2568
						status = "Backed up Original";
2569
						break;
2570
					case SPKINSTALL_ORIGINAL_RESTORE:
2571
						status = "Restored Original";
2572
						break;
2573
					case SPKINSTALL_ORIGINAL_BACKUP_FAIL:
2574
						status = "Failed to Backup Original";
2575
						break;
2576
					case SPKINSTALL_ORIGINAL_RESTORE_FAIL:
2577
						status = "Failed to restore Original";
2578
						break;
2579
					case SPKINSTALL_FAKEPATCH:
2580
						status = "Adjusting Fakepatch";
2581
						break;
2582
					case SPKINSTALL_FAKEPATCH_FAIL:
2583
						status = "Failed to adjust Fakepatch";
2584
						break;
2585
					case SPKINSTALL_AUTOTEXT:
2586
						status = "Adjusting Text File";
2587
						break;
2588
					case SPKINSTALL_AUTOTEXT_FAIL:
2589
						status = "Failed to adjust Text File";
2590
						break;
2591
					case SPKINSTALL_MISSINGFILE:
2592
						status = "Missing File";
2593
						break;
2594
					case SPKINSTALL_SHARED:
2595
						status = "Shared File";
2596
						break;
2597
					case SPKINSTALL_SHARED_FAIL:
2598
						status = "Shared File Failed";
2599
						break;
2600
					case SPKINSTALL_ORPHANED:
2601
						status = "File Orphaned";
2602
						break;
2603
					case SPKINSTALL_ORPHANED_FAIL:
2604
						status = "Failed to Orphan file";
2605
						break;
2606
					case SPKINSTALL_UNCOMPRESS_FAIL:
2607
						status = "Failed to Uncompress";
2608
						break;
2609
				}
2610
 
2611
				if ( add )
2612
				{
2613
					if ( str->data.NumToken(" ") > 1 )
2614
						log->AddItem(SystemStringFromCyString(str->str.findreplace("~", " => ")), status, SystemStringFromCyString(SPK::ConvertTimeString((long)str->data.GetToken(" ", 2, 2).ToLong())));
2615
					else
2616
						log->AddItem(SystemStringFromCyString(str->str.findreplace("~", " => ")), status, nullptr);
2617
				}
2618
			}
2619
			if ( log->ShowDialog(this) == Windows::Forms::DialogResult::Cancel )
2620
			{
2621
				if ( MessageBox::Show(this, "Are you sure you want to clear the file log?", "Clear File Log", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == Windows::Forms::DialogResult::Yes )
2622
				{
2623
					m_pFileErrors->Clear();
2624
					MessageBox::Show(this, "The file log has been cleared", "File Log Cleared", MessageBoxButtons::OK, MessageBoxIcon::Information);
2625
				}
2626
			}
2627
 
2628
		}
2629
	}
2630
 
2631
	void MainGui::VerifyInstalledFiles()
2632
	{
2633
		CyStringList missing;
2634
		int amount = m_pPackages->VerifyInstalledFiles(&missing);
2635
		if ( !amount )
2636
			MessageBox::Show(this, "All files are currently installed", "Verifying Installed Files", MessageBoxButtons::OK, MessageBoxIcon::Information);
2637
		else
2638
		{
2639
			String ^text;
2640
			for ( SStringList *str = missing.Head(); str; str = str->next )
2641
			{
2642
				text += SystemStringFromCyString(str->str);
2643
				text += "\n\t";
2644
				CyString data = str->data.findreplace("\n", "\t\n");
2645
				text += SystemStringFromCyString(data);
2646
				text += "\n\n";
2647
			}
2648
			MessageBoxDetails::Show(this, "Verifing Installed Files", "Missing files detected\nAmount = " + amount, text, false, 600);
2649
		}
2650
	}
2651
 
2652
	void MainGui::ExportPackageList()
2653
	{
2654
		bool enabled = false;
2655
		if ( MessageBox::Show(this, "Do you only want to export enabled packages?", "Only Enabled", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == Windows::Forms::DialogResult::Yes )
2656
			enabled =true;
2657
		SaveFileDialog ^ofd = gcnew SaveFileDialog();
2658
		ofd->Filter = "Log Files (*.log)|*.log";
2659
		ofd->FilterIndex = 1;
2660
		ofd->RestoreDirectory = true;
2661
		ofd->AddExtension =  true;
2662
		ofd->Title = "Select the file to save the package list to";
2663
		if ( ofd->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2664
		{
2665
			if ( IO::File::Exists(ofd->FileName) )
2666
				IO::File::Delete(ofd->FileName);
2667
 
2668
			StreamWriter ^sw = File::CreateText(ofd->FileName);
2669
			try 
2670
			{
2671
				for ( CBaseFile *package = m_pPackages->FirstPackage(); package; package = m_pPackages->NextPackage() )
2672
				{
2673
					if ( enabled && !package->IsEnabled() ) continue;
2674
					CyString line = package->GetName() + " :: " + package->GetAuthor() + " :: " + package->GetVersion() + " :: " + package->GetCreationDate() + " :: ";
2675
 
2676
					if ( package->GetType() == TYPE_XSP )
2677
						line += "Ship :: ";
2678
					else if ( package->GetType() == TYPE_ARCHIVE )
2679
						line += "- Archive - :: ";
2680
					else if ( package->GetType() == TYPE_SPK )
2681
					{
2682
						CyString type = ((CSpkFile *)package)->GetScriptTypeString(m_pPackages->GetLanguage());
2683
						if ( !type.Empty() )
2684
							line += type + " :: ";
2685
					}
2686
 
2687
					line += ((package->IsEnabled()) ? CyString("Yes") : CyString("No")) + " :: " + ((package->IsSigned()) ? "Yes" : "No");
2688
					sw->WriteLine(SystemStringFromCyString(line));
2689
				}
2690
			}
2691
			finally
2692
			{
2693
				if ( sw )
2694
					delete (IDisposable ^)sw;
2695
			}				
2696
 
2697
			if ( IO::File::Exists(ofd->FileName) )
2698
			{
2699
				if ( enabled )
2700
					MessageBox::Show(this, "Enabled Packages have been saved to:\n" + ofd->FileName, "Package List Saved", MessageBoxButtons::OK, MessageBoxIcon::Information);
2701
				else
2702
					MessageBox::Show(this, "Complete Package List has been saved to:\n" + ofd->FileName, "Package List Saved", MessageBoxButtons::OK, MessageBoxIcon::Information);
2703
			}
2704
			else
2705
				MessageBox::Show(this, "There was an error writing file:\n" + ofd->FileName, "File Write Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
2706
		}
2707
	}
2708
}
2709