Subversion Repositories spk

Rev

Rev 80 | Rev 86 | 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
 
56 cycrow 180
		CyString exe = m_pPackages->GetGameExe()->GetGameRunExe(m_pPackages->GetCurrentDirectory().ToString());
52 cycrow 181
		if ( CFileIO(exe).ExistsOld() )
1 cycrow 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;
50 cycrow 221
			if ( p->author().Compare("PluginManager") )
1 cycrow 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
50 cycrow 244
				aNames[i] = _US(p->author())->ToLower();
1 cycrow 245
			else if ( m_iSortingColumn == SORT_VERSION ) // sort by author
50 cycrow 246
				aNames[i] = _US(p->version())->ToLower();
1 cycrow 247
			else if ( m_iSortingColumn == SORT_CREATED ) // sort by author
50 cycrow 248
				aNames[i] = _US(p->creationDate().token("/", 3) + p->creationDate().token("/", 2) + p->creationDate().token("/", 1));
1 cycrow 249
			else if ( m_iSortingColumn == SORT_ENABLE ) // sort by author
250
			{
251
				if ( p->IsEnabled() )
252
					aNames[i] = SystemStringFromCyString(CyString::Number(1));
253
				else
254
					aNames[i] = SystemStringFromCyString(CyString::Number(0));
255
			}
256
			else if ( m_iSortingColumn == SORT_SIGNED ) // sort by author
257
			{
258
				if ( p->IsEnabled() )
259
					aNames[i] = SystemStringFromCyString(CyString::Number(1));
260
				else
261
					aNames[i] = SystemStringFromCyString(CyString::Number(0));
262
			}
263
			else if ( m_iSortingColumn == SORT_TYPE ) // sort by type
264
			{
265
				if ( p->GetType() == TYPE_XSP )
266
					aNames[i] = "Ship";
267
				else if ( p->GetType() == TYPE_ARCHIVE )
268
					aNames[i] = "- Archive -";
269
				else if ( p->GetType() == TYPE_SPK )
270
					aNames[i] = SystemStringFromCyString(((CSpkFile *)p)->GetScriptTypeString(m_pPackages->GetLanguage()));
271
				else
272
					aNames[i] = "";
273
			}
274
			else
275
				aNames[i] = SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage()))->ToLower();
276
		}
277
 
278
		Array::Sort(aNames, aPackages);
279
 
280
		// now display
281
		for ( i = 0; i < aPackages->Length; i++ )
282
		{
283
			CBaseFile *p = (m_bSortingAsc) ? aPackages[i]->Package : aPackages[(aPackages->Length - 1 - i)]->Package;
284
			CyString name;
285
			if ( p->GetType() == TYPE_ARCHIVE )
50 cycrow 286
				name = CFileIO(p->filename()).GetFilename();
1 cycrow 287
			else
288
				name = p->GetLanguageName(m_pPackages->GetLanguage());
289
 
290
			int indent = 0;
291
			CBaseFile *parent = p;
292
 
293
			if ( p->GetParent() && p->GetParent()->GetType() == TYPE_SPK && ((CSpkFile *)p->GetParent())->IsLibrary() )
294
				indent = 0;
295
			else
296
			{
297
				while ( parent->GetParent() )
298
				{
299
					parent = parent->GetParent();
300
					++indent;
301
				}
302
 
303
				if ( p->GetParent() && p->GetParent() == m_pPackages->GetEnabledMod() )
304
					--indent;
305
			}
306
 
307
			ListViewItem ^item = gcnew ListViewItem(SystemStringFromCyString(name));
308
			item->IndentCount = (indent * 2);
50 cycrow 309
			item->SubItems->Add(_US(p->author()));
310
			item->SubItems->Add(_US(p->version()));
311
			item->SubItems->Add(_US(p->creationDate()));
1 cycrow 312
			if ( p->GetType() == TYPE_XSP )
313
				item->SubItems->Add("Ship");
314
			else if ( p->GetType() == TYPE_ARCHIVE )
315
				item->SubItems->Add("- Archive -");
316
			else if ( p->GetType() == TYPE_SPK )
317
			{
318
				CSpkFile *spk = (CSpkFile *)p;
319
				item->SubItems->Add(SystemStringFromCyString(spk->GetScriptTypeString(m_pPackages->GetLanguage())));
320
			}
321
			else
322
				item->SubItems->Add("");
323
 
324
			if ( p->IsEnabled() )
325
				item->SubItems->Add("Yes");
326
			else
327
				item->SubItems->Add("No");
328
			if ( p->IsSigned() )
329
				item->SubItems->Add("Yes");
330
			else
331
				item->SubItems->Add("No");
332
			item->Tag = SystemStringFromCyString(CyString::Number(p->GetNum()));
333
 
334
			ListPackages->Items->Add(item);
335
 
336
			if ( useGroup )
337
				item->Group = useGroup;
338
			else
339
			{
340
				int addGroup = LISTGROUP_INSTALLED;
341
				if ( p->GetParent() && p->GetParent() == m_pPackages->GetEnabledMod() )
342
					addGroup = LISTGROUP_MOD;
343
				else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
344
					addGroup = LISTGROUP_LIBRARY;
345
				else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsFakePatch() )
346
					addGroup = LISTGROUP_FAKE;
347
				else if ( p->GetType() == TYPE_XSP )
348
					addGroup = LISTGROUP_SHIP;
349
				else if ( p->GetType() == TYPE_ARCHIVE )
350
					addGroup = LISTGROUP_ARCHIVE;
351
 
352
				item->Group = ListPackages->Groups[addGroup];
353
			}
354
 
355
			CyString groupName = CyStringFromSystemString(item->Group->Header);
356
			if ( groupName.IsIn(" [") )
357
			{
358
				int enabled = groupName.GetToken(" [", 2, 2).GetToken("/", 1, 1).ToInt();
359
				int total = groupName.GetToken(" [", 2, 2).GetToken("/", 2, 2).GetToken("]", 1, 1).ToInt() + 1;
360
				if ( p->IsEnabled() ) ++enabled;
361
				groupName = groupName.GetToken(" [", 1, 1) + " [" + CyString::Number(enabled) + "/" + CyString::Number(total) + "]";
362
			}
363
			else
364
			{
365
				if ( p->IsEnabled() )
366
					groupName = groupName + " [1/1]";
367
				else
368
					groupName = groupName + " [0/1]";
369
			}
370
			item->Group->Header = SystemStringFromCyString(groupName);
371
 
372
			// get the icon
373
			item->ImageIndex = -1;
374
			if ( p->GetIcon() )
375
				PluginManager::DisplayListIcon(p, ListPackages, item);
376
 
377
			if ( item->ImageIndex == -1 )
378
			{
379
				if ( p->GetType() == TYPE_XSP )
380
					item->ImageKey = "ship";
381
				else if ( p->GetType() == TYPE_ARCHIVE )
382
					item->ImageKey = "archive";
383
				else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
384
					item->ImageKey = "library";
385
				else if ( p->IsFakePatch() )
386
					item->ImageKey = "fake";
387
				else
388
					item->ImageKey = "package";
389
			}
390
 
391
			// check for any children
392
			this->_DisplayPackages(p, item->Group);
393
		}
394
	}
395
 
396
	void MainGui::UpdatePackages()
397
	{
398
		ListPackages->Items->Clear();
399
		ListPackages->Groups->Clear();
400
		ListPackages->SmallImageList = gcnew ImageList();
401
		ListPackages->LargeImageList = gcnew ImageList();
402
		ListPackages->LargeImageList->ImageSize = System::Drawing::Size(32, 32);
403
		ListPackages->LargeImageList->ColorDepth = Windows::Forms::ColorDepth::Depth32Bit;
404
		ListPackages->SmallImageList->ImageSize = System::Drawing::Size(16, 16);
405
		ListPackages->SmallImageList->ColorDepth = Windows::Forms::ColorDepth::Depth32Bit;
406
 
407
		int index = this->imageList1->Images->IndexOfKey("package.png");
408
		if ( index != -1 )
409
		{
410
			ListPackages->SmallImageList->Images->Add("package", this->imageList1->Images[index]);
411
			ListPackages->LargeImageList->Images->Add("package", this->imageList1->Images[index]);
412
		}
413
		index = this->imageList1->Images->IndexOfKey("ship.png");
414
		if ( index != -1 )
415
		{
416
			ListPackages->SmallImageList->Images->Add("ship", this->imageList1->Images[index]);
417
			ListPackages->LargeImageList->Images->Add("ship", this->imageList1->Images[index]);
418
		}
419
		index = this->imageList1->Images->IndexOfKey("fake.png");
420
		if ( index != -1 )
421
		{
422
			ListPackages->SmallImageList->Images->Add("fake", this->imageList1->Images[index]);
423
			ListPackages->LargeImageList->Images->Add("fake", this->imageList1->Images[index]);
424
		}
425
		index = this->imageList1->Images->IndexOfKey("library.png");
426
		if ( index != -1 )
427
		{
428
			ListPackages->SmallImageList->Images->Add("library", this->imageList1->Images[index]);
429
			ListPackages->LargeImageList->Images->Add("library", this->imageList1->Images[index]);
430
		}
431
 
432
		index = this->imageList1->Images->IndexOfKey("archive.png");
433
		if ( index != -1 )
434
		{
435
			ListPackages->SmallImageList->Images->Add("archive", this->imageList1->Images[index]);
436
			ListPackages->LargeImageList->Images->Add("archive", this->imageList1->Images[index]);
437
		}
438
 
439
		ListViewGroup ^group = gcnew ListViewGroup("Installed Scripts", HorizontalAlignment::Left);
440
		ListPackages->Groups->Add(group);
441
		ListViewGroup ^shipGroup = gcnew ListViewGroup("Installed Ships", HorizontalAlignment::Left);
442
		ListPackages->Groups->Add(shipGroup);
443
		ListViewGroup ^fakeGroup = gcnew ListViewGroup("Fake Patches", HorizontalAlignment::Left);
444
		ListPackages->Groups->Add(fakeGroup);
445
		ListViewGroup ^libGroup = gcnew ListViewGroup("Script Libraries", HorizontalAlignment::Left);
446
		ListPackages->Groups->Add(libGroup);
447
		ListViewGroup ^modGroup = gcnew ListViewGroup("Current Mod Addons", HorizontalAlignment::Left);
448
		ListPackages->Groups->Add(modGroup);
449
		ListViewGroup ^arcGroup = gcnew ListViewGroup("Installed Archives", HorizontalAlignment::Left);
450
		ListPackages->Groups->Add(arcGroup);
451
 
452
		// sort the items
453
		if ( m_pPackages )
454
		{
455
			m_pPackages->AssignPackageNumbers();
456
			this->_DisplayPackages(NULL, nullptr);
457
		}
458
 
459
		PackageListSelected(ListPackages, gcnew System::EventArgs());
460
		ListPackages->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
461
 
462
		// update the status bar
463
		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)";
464
	}
465
 
466
	void MainGui::StartCheckTimer()
467
	{
468
		System::Windows::Forms::Timer ^timer = gcnew System::Windows::Forms::Timer();
469
		timer->Interval = 5000;
470
		timer->Tick += gcnew System::EventHandler(this, &MainGui::TimerEvent_CheckFile);
471
		timer->Start();
472
	}
473
 
474
 
475
	//
476
	// Update Controls
477
	// Updates any additional controls, ie adding columns to package list
478
	//
479
	void MainGui::UpdateControls()
480
	{
481
		// Package List Columns
482
		int csize = ListPackages->Width - 200 - 5;
483
		int psize = ((csize * 7) / 10);
484
		int asize = ((csize * 3) / 10);
485
		if ( psize < 60 ) psize = 60;
486
		if ( asize < 60 ) asize = 60;
487
		ListPackages->Columns->Clear();
488
		ListPackages->Columns->Add("Package", psize, HorizontalAlignment::Left);
489
		ListPackages->Columns->Add("Author", asize, HorizontalAlignment::Left);
490
		ListPackages->Columns->Add("Version", 60, HorizontalAlignment::Right);
491
		ListPackages->Columns->Add("Updated", 80, HorizontalAlignment::Left);
492
		ListPackages->Columns->Add("Type", 100, HorizontalAlignment::Left);
493
		ListPackages->Columns->Add("Enabled", 60, HorizontalAlignment::Right);
494
		ListPackages->Columns->Add("Signed", 60, HorizontalAlignment::Right);
495
		ListPackages->FullRowSelect = true;
496
		ListPackages->Focus();
497
 
498
		if ( m_pPackages->IsVanilla() )
499
			m_pMenuBar->Vanilla();
500
		else
501
			m_pMenuBar->Modified();
502
	}
503
 
504
	//
505
	// Install a package
506
	// Called from either install button, or from command line
507
	//
508
	bool MainGui::InstallPackage(System::String ^file, bool straightAway, bool builtin, bool background)
509
	{
510
		bool errored = false;
511
 
512
		if ( file->Length )
513
		{
514
			int error;
515
			CBaseFile *package = m_pPackages->OpenPackage(CyStringFromSystemString(file), &error, 0, SPKREAD_NODATA, READFLAG_NOUNCOMPRESS);
516
			if ( error == INSTALLERR_NOMULTI )
517
			{
518
				CLinkList<CBaseFile> erroredList;
519
				m_pPackages->PrepareMultiPackage(CyStringFromSystemString(file), &erroredList, &error, 0);
520
				if ( erroredList.size() )
521
				{
522
					System::String ^modified;
523
					for ( CBaseFile *p = erroredList.First(); p; p = erroredList.Next() )
524
					{
525
						p->SetOverrideFiles(builtin);
526
						if ( m_pPackages->PrepareInstallPackage(p, false, false, IC_MODIFIED) != INSTALLCHECK_OK )
527
						{
528
							modified += SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage()));
529
							modified += "\n";
530
							errored = true;
531
						}
532
					}
533
 
534
					if ( errored )
535
						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);
536
				}
537
			}
538
			else if ( !package )
539
			{
540
				System::String ^errorStr;
541
				switch ( error )
542
				{
543
					case INSTALLERR_OLD:
544
						errorStr = "File is in old format no longer supported";
545
						break;
546
					case INSTALLERR_NOEXIST:
547
						errorStr = "file doesn't exist";
548
						break;
549
					case INSTALLERR_INVALID:
550
						errorStr = "Invalid package file";
551
						break;
552
					case INSTALLERR_NOSHIP:
553
						errorStr = "Ship Packages are currently not supported";
554
						break;
555
					case INSTALLERR_VERSION:
556
						errorStr = "Package file was created in a newer version, unable to open";
557
						break;
558
 
559
					default:
560
						errorStr = "Unknown Error";
561
				}
562
 
563
				if ( !builtin )
564
					this->DisplayMessageBox(false, "Open Package Error", "Error Opening: " + file + "\n" + errorStr, MessageBoxButtons::OK, MessageBoxIcon::Stop);
565
				errored = true;
566
			}
567
			else
568
			{
569
				if ( builtin )
570
				{
571
					package->SetOverrideFiles(builtin);
572
					if ( m_pPackages->PrepareInstallPackage(package, false, false, IC_WRONGGAME|IC_WRONGVERSION|IC_OLDVERSION) != INSTALLCHECK_OK )
573
						errored = true;
574
				}
575
				else
576
				{
577
					int errorNum = m_pPackages->PrepareInstallPackage(package, false, false, IC_ALL);
578
					if ( errorNum != INSTALLCHECK_OK )
579
					{
580
						if ( errorNum == INSTALLCHECK_NOSHIP )
581
						{
582
							this->DisplayMessageBox(false, "No Ships", "Ships are not supported for " + SystemStringFromCyString(m_pPackages->GetGameName()), MessageBoxButtons::OK, MessageBoxIcon::Stop);
583
							errored = true;
584
						}
585
						else if ( m_pPackages->PrepareInstallPackage(package, false, false, IC_MODIFIED) != INSTALLCHECK_OK )
586
						{
587
							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);
588
							errored = true;
589
						}
590
					}
591
 
592
					// check for compatabilities
593
					CLinkList<CBaseFile> packages;
594
					int compat = m_pPackages->CheckCompatabilityAgainstPackages(package, NULL, &packages);
595
					if ( compat )
596
					{
597
						String ^message = "\nConflicts with:\n";
598
						for ( CBaseFile *p = packages.First(); p; p = packages.Next() )
599
							message += SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n";
600
 
601
						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 )
602
						{
603
							m_pPackages->RemovePreparedInstall(package);
604
							errored = true;
605
						}
606
					}
607
				}
608
 
609
				// not installing
610
				if ( errored )
611
				{
612
					delete package;
613
					package = NULL;
614
				}
615
			}
616
		}
617
 
618
		if ( errored )
619
		{
620
			this->Enabled= true;
621
			this->ProgressBar->Hide();
622
			return false;
623
		}
624
 
625
		// start installing
626
		if ( straightAway )
627
			return this->StartInstalling(builtin, background);
628
 
629
		return true;
630
	}
631
 
632
	void MainGui::DoUninstall()
633
	{
634
		m_pPi = gcnew PackageInstalled("Uninstall Packages");
635
 
636
		CLinkList<CBaseFile> packageList;
637
		CLinkList<CBaseFile> disableList;
638
 
639
		if ( m_pPackages->UninstallPreparedPackages(m_pFileErrors, 0, &packageList, &disableList) )
640
		{
641
			CyString sDisplay;
642
			CyString sAfterText;
643
			for ( CBaseFile *p = packageList.First(); p; p = packageList.Next() )
644
			{
645
				sAfterText = m_pPackages->GetUninstallAfterText(p);
50 cycrow 646
				m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), _US(p->author()), _US(p->version()), (sAfterText.Empty() ? "Uninstalled" : SystemStringFromCyString(sAfterText)), "Uninstalled");
1 cycrow 647
				sDisplay = p->GetFullPackageName(m_pPackages->GetLanguage());
648
				delete p;
649
			}
650
			for ( CBaseFile *p = disableList.First(); p; p = disableList.Next() )
50 cycrow 651
				m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), _US(p->author()), _US(p->version()), "Disabled", "Dependants Disabled");
1 cycrow 652
			packageList.clear();			
653
 
654
			if ( m_pPi->PackageCount() == 1 )
655
			{
656
				if ( sAfterText.Empty() )
657
					this->DisplayMessageBox(true, "Uninstalled", "Package Uninstalled\n" + SystemStringFromCyString(sDisplay), MessageBoxButtons::OK, MessageBoxIcon::Information);
658
				else
659
					this->DisplayMessageBox(true, "Uninstalled", "Package Uninstalled\n" + SystemStringFromCyString(sDisplay) + "\n\n" + SystemStringFromCyString(sAfterText), MessageBoxButtons::OK, MessageBoxIcon::Information);
660
			}
661
			else
662
				m_bDisplayDialog = true;
663
		}
664
		else
665
			this->DisplayMessageBox(true, "Uninstall Error", "Error Uninstalling", MessageBoxButtons::OK, MessageBoxIcon::Stop);
666
	}
667
 
668
	void MainGui::DoDisable()
669
	{
670
		CLinkList<CBaseFile> packageList;
671
 
672
		System::String ^display;
673
		m_pPi = gcnew PackageInstalled("Enabled/Disabled Packages");
674
 
675
		if ( m_pPackages->GetNumPackagesInDisabledQueue() )
676
		{
677
			if ( m_pPackages->DisablePreparedPackages(0, 0, &packageList) )
678
			{
679
				for ( CBaseFile *p = packageList.First(); p; p = packageList.Next() )
680
				{
50 cycrow 681
					m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), _US(p->author()), _US(p->version()), "Disabled", "Disabled Packages");
1 cycrow 682
					display = "Package Disabled\n\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage()));
683
				}
684
			}
685
			else
686
				this->DisplayMessageBox(true, "Disable Error", "Error Disabling packages", MessageBoxButtons::OK, MessageBoxIcon::Stop);
687
		}
688
 
689
		packageList.clear();
690
		if ( m_pPackages->GetNumPackagesInEnabledQueue() )
691
		{
692
			if ( m_pPackages->EnablePreparedPackages(0, 0, &packageList) )
693
			{
694
				for ( CBaseFile *p = packageList.First(); p; p = packageList.Next() )
695
				{
50 cycrow 696
					m_pPi->AddPackageWithGroup(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())), _US(p->author()), _US(p->version()), "Enabled", "Enable Packages");
1 cycrow 697
					display = "Package Enabled\n\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage()));
698
				}
699
			}
700
			else
701
				this->DisplayMessageBox(true, "Enable Error", "Error Enabling packages", MessageBoxButtons::OK, MessageBoxIcon::Stop);
702
		}
703
 
704
		if ( m_pPi->PackageCount() == 1 )
705
			this->DisplayMessageBox(true, "Packages Enabled/Disabled", display, MessageBoxButtons::OK, MessageBoxIcon::Information);
706
		else
707
		{
708
			m_bDisplayDialog = true;
709
		}
710
	}
711
 
712
	void MainGui::DoInstall(bool builtin, bool frombackground)
713
	{
714
		CLinkList<CBaseFile> erroredPackages;
715
		CLinkList<CBaseFile> installedPackages;
716
		if ( m_pPackages->InstallPreparedPackages(m_pFileErrors, 0, &erroredPackages, &installedPackages) )
717
		{
718
			if ( !builtin )
719
			{
720
				if ( installedPackages.size() == 1 && erroredPackages.size() == 0 )
721
				{
722
					CBaseFile *p = installedPackages.Front()->Data();
723
					CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
724
					CyString afterText = m_pPackages->GetInstallAfterText(p);
725
					if ( afterText.Empty() )
726
						this->DisplayMessageBox(frombackground, "Installed", "Package: " + SystemStringFromCyString(packageName) + " installed!\n\n", MessageBoxButtons::OK, MessageBoxIcon::Information);
727
					else
728
					{
729
						afterText.StripHTML();
730
						this->DisplayMessageBox(frombackground, "Installed", "Package: " + SystemStringFromCyString(packageName) + " installed!\n\n" + SystemStringFromCyString(afterText), MessageBoxButtons::OK, MessageBoxIcon::Information);
731
					}
732
				}
733
				else
734
				{
735
					m_pPi = gcnew PackageInstalled("Packages Installed");
736
 
737
					CyStringList packages;
738
					for ( CListNode<CBaseFile> *node = installedPackages.Front(); node; node = node->next() )
739
					{
740
						CBaseFile *p = node->Data();
741
						CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
742
						CyString afterText = m_pPackages->GetInstallAfterText(p);
743
 
744
						if ( afterText.Empty() )
745
							afterText = "Installed";
50 cycrow 746
						m_pPi->AddPackage(SystemStringFromCyString(packageName), _US(p->author()), _US(p->version()), SystemStringFromCyString(afterText));
1 cycrow 747
					}
748
					for ( CListNode<CBaseFile> *node = erroredPackages.Front(); node; node = node->next() )
749
					{
750
						CBaseFile *p = node->Data();
751
						CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
50 cycrow 752
						m_pPi->AddPackage(SystemStringFromCyString(packageName), _US(p->author()), _US(p->version()), SystemStringFromCyString("Failed to Install"));
1 cycrow 753
					}
754
 
755
					m_bDisplayDialog = true;
756
				}
757
			}
758
		}
759
		// no packages were installed
760
		else 
761
		{
762
			if ( !builtin )
763
			{
764
				if ( erroredPackages.size() == 1 )
765
				{
766
					CBaseFile *p = erroredPackages.Front()->Data();
767
					CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
768
					this->DisplayMessageBox(frombackground, "Error Installing", "Package: " + SystemStringFromCyString(packageName) + " failed to install!\nError: " + SystemStringFromCyString(CBaseFile::ErrorString(p->GetLastError(), p->GetLastErrorString())) + "\n", MessageBoxButtons::OK, MessageBoxIcon::Error);
769
				}
770
				else
771
				{
772
					m_pPi = gcnew PackageInstalled("Packages Failed To Install");
773
 
774
					CyStringList packages;
775
					for ( CListNode<CBaseFile> *node = erroredPackages.Front(); node; node = node->next() )
776
					{
777
						CBaseFile *p = node->Data();
778
						CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
50 cycrow 779
						m_pPi->AddPackage(SystemStringFromCyString(packageName), _US(p->author()), _US(p->version()), "Failed: " + SystemStringFromCyString(CBaseFile::ErrorString(p->GetLastError(), p->GetLastErrorString())));
1 cycrow 780
					}
781
 
782
					m_bDisplayDialog = true;
783
				}
784
			}
785
		}
786
 
787
		if ( !frombackground )
788
			this->Background_Finished();
789
	}
790
 
791
	void MainGui::ClearSelectedItems()
792
	{
793
		for ( int i = 0; i < this->ListPackages->Items->Count; i++ )
794
			this->ListPackages->Items[i]->Selected = false;
795
		PackageListSelected(ListPackages, gcnew System::EventArgs());
796
	}
797
 
798
	bool MainGui::StartInstalling(bool builtin, bool background, bool archive)
799
	{
800
		// no packages to install
801
		if ( !m_pPackages->GetNumPackagesInQueue() )
802
			return false;
803
 
804
		// clear selected
805
		this->ClearSelectedItems();
806
 
807
		// lets install them now
808
		CLinkList<CBaseFile> lCheckPackages;
809
		if ( m_pPackages->CheckPreparedInstallRequired(&lCheckPackages) )
810
		{
811
			for ( CListNode<CBaseFile> *pNode = lCheckPackages.Front(); pNode; pNode = pNode->next() )
812
			{
813
				CBaseFile *package = pNode->Data();
814
				CSpkFile *spk = (CSpkFile *)package;
815
 
816
				CyStringList missingList;
817
				if ( m_pPackages->GetMissingDependacies(package, &missingList) )
818
				{
819
					CyString requires;
820
					for ( SStringList *strNode = missingList.Head(); strNode; strNode = strNode->next )
821
					{
822
						if ( strNode->str.IsIn("|") )
823
							requires += strNode->str.GetToken("|", 1, 1) + " V" + strNode->str.GetToken("|", 2, 2) + " by " + strNode->data;
824
						else
825
							requires += strNode->str + " by " + strNode->data;
826
						requires += "\n";
827
					}
828
					this->DisplayMessageBox(false, "Installing", "Missing Package for " + SystemStringFromCyString(package->GetLanguageName(m_pPackages->GetLanguage())) + "\nRequires:\n" + SystemStringFromCyString(requires), MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
829
				}
830
			}
831
		}
832
 
833
		// no packages to install
834
		if ( !m_pPackages->GetNumPackagesInQueue() )
835
		{
836
			this->Enabled = true;
837
			this->ProgressBar->Hide();
838
			return false;
839
		}
840
 
841
		ProgressBar->Show();
842
		this->Enabled = false;
843
 
844
		if ( builtin )
845
		{
846
			if ( background )
847
				this->StartBackground(MGUI_BACKGROUND_INSTALLBUILTIN);
848
			else
849
				this->DoInstall(builtin, false);
850
		}
851
		else if ( archive )
852
		{
853
			if ( background )
854
				this->StartBackground(MGUI_BACKGROUND_INSTALL);
855
			else
856
				this->DoInstall(false, false);
857
		}
858
		else
859
		{
860
			InstallPackageDialog ^installDialog = gcnew InstallPackageDialog(m_pPackages);
861
			if ( installDialog->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
862
			{
863
				// no packages to install
864
				if ( !m_pPackages->GetNumPackagesInQueue() )
865
				{
866
					this->Enabled = true;
867
					this->ProgressBar->Hide();
868
					return false;
869
				}
870
				if ( background )
871
					this->StartBackground(MGUI_BACKGROUND_INSTALL);
872
				else
873
					this->DoInstall(false, false);
874
			}
875
			else
876
			{
877
				m_pPackages->RemovePreparedInstall(NULL);
878
				this->Enabled = true;
879
				this->ProgressBar->Hide();
880
				return false;
881
			}
882
		}
883
		return true;
884
	}
885
 
886
	bool MainGui::StartBackground(int type, System::String ^info)
887
	{
888
		if ( backgroundWorker1->IsBusy )
889
			return false;
890
		if ( m_bRunningBackground )
891
			return false;
892
 
893
		m_sBackgroundInfo = info;
894
		return this->StartBackground(type);
895
	}
896
 
897
	bool MainGui::StartBackground(int type)
898
	{
899
		if ( backgroundWorker1->IsBusy )
900
			return false;
901
		if ( m_bRunningBackground )
902
			return false;
903
 
904
		m_iBackgroundTask = type;
905
 
906
		backgroundWorker1->RunWorkerAsync();
907
		m_bRunningBackground = true;
908
		return true;
909
	}
910
 
911
	void MainGui::ChangeDirectory(CyString dir)
912
	{
913
		if ( m_pPackages->IsCurrentDir(dir) )
914
			return;
915
 
916
		if ( m_pPackages->IsLoaded() )
917
		{
918
			if ( m_pPackages->CloseDir(0, 0, true) )
919
			{
920
				// write the modname
921
				if ( !m_pPackages->GetModKey().Empty() )
922
					PluginManager::WriteRegistryValue(m_pPackages->GetModKey(), m_pPackages->GetSelectedModName());
923
				m_pPackages->Reset();
924
			}
925
			else
926
			{
927
				this->DisplayMessageBox(true, "Error", "unable to close directory", MessageBoxButtons::OK, MessageBoxIcon::Error);
928
				return;
929
			}
930
		}
931
 
932
		m_pPackages->Reset();
933
 
934
		if ( m_pPackages->Read(dir, 0) )
935
		{
936
			if ( m_iSaveGameManager == 1 )
937
				m_pPackages->RestoreSaves();
938
			m_pPackages->UpdatePackages();
939
			m_pPackages->ReadGameLanguage(true);
940
			this->UpdateRunButton();
941
			System::String ^mod = PluginManager::ReadRegistryValue(m_pPackages->GetModKey());
942
			m_pPackages->SetMod(CyStringFromSystemString(mod));
943
		}
944
		else
945
		{
946
			this->DisplayMessageBox(true, "Error", "unable to open new directory", MessageBoxButtons::OK, MessageBoxIcon::Error);
947
			this->Close();
948
		}
949
	}
950
 
951
	CBaseFile *MainGui::FindPackageFromList(ListViewItem ^item)
952
	{
953
		if ( !item )
954
			return NULL;
955
 
956
		System::String ^sNum = System::Convert::ToString(item->Tag);
957
		int iNum = CyStringFromSystemString(sNum).ToInt();
958
 
959
		CBaseFile *p = m_pPackages->GetPackageAt(iNum);
960
		return p;
961
	}
962
 
963
	void MainGui::FindPackagesOnline()
964
	{
965
		CyStringList servers;
966
		m_pPackages->FindAllServers(&servers);
967
		if ( servers.Empty() )
968
		{
969
			MessageBox::Show(this, "Found now web address to check for packages", "No Web Address", MessageBoxButtons::OK, MessageBoxIcon::Warning);
970
			return;
971
		}
972
 
973
		DownloadPackageList ^dpl = gcnew DownloadPackageList(m_pPackages, &servers);
974
		dpl->ShowDialog(this);
975
 
976
		if ( m_pPackages->AnyAvailablePackages() )
977
			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);
978
		else
979
			MessageBox::Show(this, "Unable to find any packages\n", "No Packages Found", MessageBoxButtons::OK, MessageBoxIcon::Warning);
980
	}
981
 
982
	//
983
	// Event Handlers
984
	//
985
	void MainGui::SetupEventHandlers()
986
	{
987
		// setup Event Handlers
988
		ButClose->Click += gcnew EventHandler(this, &PluginManager::MainGui::ClosedEvent);
989
		ButInstall->Click += gcnew EventHandler(this, &PluginManager::MainGui::InstallEvent);
990
		ButUninstall->Click += gcnew EventHandler(this, &PluginManager::MainGui::UninstallEvent);
991
		ButDisable->Click += gcnew EventHandler(this, &PluginManager::MainGui::DisableEvent);
992
		ListPackages->SelectedIndexChanged += gcnew EventHandler(this, &PluginManager::MainGui::PackageListSelected);
993
		ListPackages->ColumnClick  += gcnew ColumnClickEventHandler(this, &PluginManager::MainGui::PackageListSort);
994
		ComboDir->SelectedIndexChanged += gcnew EventHandler(this, &PluginManager::MainGui::ChangeDirectoryEvent);
995
 
996
 
997
		// background worker
998
		backgroundWorker1->DoWork += gcnew DoWorkEventHandler( this, &MainGui::Background_DoWork );
999
		backgroundWorker1->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainGui::Background_Finished );
1000
		backgroundWorker1->ProgressChanged += gcnew ProgressChangedEventHandler( this, &MainGui::Background_Progress );
1001
 
1002
		// auto update
1003
		backgroundUpdater->DoWork += gcnew DoWorkEventHandler( this, &MainGui::Updater_DoWork );
1004
		backgroundUpdater->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainGui::Updater_Finished );
1005
	}
1006
 
1007
	void MainGui::PackageListSort(System::Object ^Sender, ColumnClickEventArgs ^E)
1008
	{
1009
		if ( E->Column != m_iSortingColumn )
1010
		{
1011
			m_iSortingColumn = E->Column;
1012
			m_bSortingAsc = true;
1013
		}
1014
		else
1015
			m_bSortingAsc = !m_bSortingAsc;
1016
		this->UpdatePackages();
1017
	}
1018
 
1019
	void MainGui::PackageListSelected(System::Object ^Sender, System::EventArgs ^E)
1020
	{
1021
		// is there any selected items
1022
		this->PictureDisplay->Image = nullptr;
1023
		this->PanelDisplay->Hide();
1024
		TextDesc->Text = "";
1025
		bool buttonEnabled = false;
1026
		if ( ListPackages->SelectedItems->Count )
1027
		{
1028
			buttonEnabled = true;
1029
 
1030
			ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1031
			System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1032
			while ( myEnum->MoveNext() )
1033
			{
1034
				CBaseFile *p = this->FindPackageFromList(safe_cast<ListViewItem ^>(myEnum->Current));
1035
				if ( p )
1036
				{
1037
					if ( p->IsEnabled() )
1038
						ButDisable->Text = "Disable";
1039
					else
1040
						ButDisable->Text = "Enable";
48 cycrow 1041
					if ( !p->description().empty() )	TextDesc->Text = _US(p->description().findReplace("<br>", "\n").stripHtml());
1 cycrow 1042
 
1043
					this->PictureDisplay->Show();
1044
					bool addedIcon = false;
1045
					C_File *picFile = p->GetFirstFile(FILETYPE_ADVERT);
1046
					if ( picFile )
1047
					{
1048
						System::String ^pic = SystemStringFromCyString(picFile->GetFilePointer());
1049
						if ( System::IO::File::Exists(pic) )
1050
						{
1051
							Bitmap ^myBitmap = gcnew Bitmap(pic);
1052
							if ( myBitmap )
1053
							{
1054
								this->PictureDisplay->Image = dynamic_cast<Image ^>(myBitmap);
1055
								addedIcon = true;
1056
							}
1057
						}
1058
					}
1059
 
1060
					if ( !addedIcon )
1061
					{
1062
 
1063
						System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainGui::typeid));
1064
						this->PictureDisplay->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"PictureDisplay.Image")));
1065
					}
1066
 
1067
					if ( p->GetType() != TYPE_ARCHIVE )
1068
						this->PanelDisplay->Show();
1069
				}	
1070
			}				
1071
		}
1072
 
1073
		// enable/disable the buttons connected to the package list
1074
		ButUninstall->Enabled = buttonEnabled;
1075
		ButDisable->Enabled = buttonEnabled;
1076
	}
1077
 
1078
	void MainGui::AddDirectoryEvent(System::Object ^Sender, System::EventArgs ^E)
1079
	{
1080
		OpenFileDialog ^ofd = gcnew OpenFileDialog();
1081
		ofd->Filter = "X-Universe Executable|";
1082
		String ^games = "";
1083
		for ( int i = 0; i < m_pPackages->GetGameExe()->GetNumGames(); i++ )
1084
		{
1085
			SGameExe *exe = m_pPackages->GetGameExe()->GetGame(i);
1086
			if ( i ) ofd->Filter += ";";
1087
			ofd->Filter += SystemStringFromCyString(exe->sExe);
1088
			games += "|" + SystemStringFromCyString(exe->sName) + "|" + SystemStringFromCyString(exe->sExe);
1089
		}
1090
		ofd->Filter += games;
1091
		ofd->FilterIndex = 1;
1092
		ofd->RestoreDirectory = true;
1093
		if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
1094
		{
68 cycrow 1095
			CyString file = CyStringFromSystemString(ofd->FileName);
1 cycrow 1096
			CyString dir = CyStringFromSystemString(IO::FileInfo(ofd->FileName).DirectoryName);
1097
			// check its a valid directory
1098
			if ( !dir.Empty() )
1099
			{
68 cycrow 1100
				CyString gameName = m_pPackages->GetGameName(file);
1 cycrow 1101
				if ( gameName.Empty() )
1102
					this->DisplayMessageBox(false, "Add Directory Error", "No X-Universe game found in folder:\n" + SystemStringFromCyString(dir), MessageBoxButtons::OK, MessageBoxIcon::Error);
1103
				else
1104
				{
1105
					// lets check if theres an old folder
83 cycrow 1106
					if ( m_pPackages->isOldDir(dir.ToString()) )
1 cycrow 1107
					{
1108
						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 )
1109
							return;
1110
					}
1111
					m_iBackgroundTask = MGUI_BACKGROUND_ADDDIR;
1112
					this->Enabled = false;
68 cycrow 1113
					CyString properDir = m_pPackages->GetProperDir(file);
1 cycrow 1114
					ComboDir->Items->Add(SystemStringFromCyString(properDir));
1115
					ComboDir->Text = SystemStringFromCyString(properDir);
1116
 
1117
					m_pRemovedDirList->Remove(properDir, true);
68 cycrow 1118
					GetGameDirs(dir, false, m_pPackages->GetAddonDir(file).Empty());
1 cycrow 1119
					/*int lang = m_pPackages->GetGameLanguage(dir);
1120
					if ( lang )
1121
						m_pDirList->PushFront(properDir, CyString::Number(lang) + "|" + gameName);
1122
					else
1123
						m_pDirList->PushFront(properDir, gameName);*/
1124
					this->StartBackground(MGUI_BACKGROUND_ADDDIR, SystemStringFromCyString(properDir));
1125
				}
1126
			}
1127
		}
1128
	}
1129
 
1130
	bool MainGui::EnablePackage(CBaseFile *p)
1131
	{
1132
		if ( !m_pPackages->PrepareEnablePackage(p) )
1133
		{
1134
			if ( m_pPackages->GetError() == PKERR_NOPARENT )
1135
				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);
1136
			else if ( m_pPackages->GetError() == PKERR_MODIFIED )
1137
				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);
1138
			else if ( m_pPackages->GetError() == PKERR_MISSINGDEP )
1139
			{
1140
				CyStringList depList;
1141
				m_pPackages->GetMissingDependacies(p, &depList, true);
1142
				CyString sDep;
1143
				for ( SStringList *strNode = depList.Head(); strNode; strNode = strNode->next )
1144
				{
1145
					if ( strNode->str.IsIn("|") )
1146
						sDep = strNode->str.GetToken("|", 1, 1) + " V" + strNode->str.GetToken("|", 2, 2) + " by " + strNode->data + "\n";
1147
					else
1148
						sDep = strNode->str + " by " + strNode->data + "\n";
1149
				}
1150
				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);
1151
			}
1152
			else
1153
				this->DisplayMessageBox(false, "Enable Error", "Error enabling package\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n\nUnknown Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1154
			return false;
1155
		}
1156
		return true;
1157
	}
1158
 
1159
	void MainGui::DisableList(ArrayList ^List)
1160
	{
1161
		bool skipShips = false;
1162
		int count = 0;
1163
 
1164
		for ( int i = 0; i < List->Count; i++ )
1165
		{
1166
			CBaseFile *p = this->FindPackageFromList(cli::safe_cast<ListViewItem ^>(List[i]));
1167
			if ( p )
1168
			{
1169
				if ( p->IsEnabled() )
1170
				{
1171
					if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
1172
					{
1173
						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 )
1174
							continue;
1175
					}
1176
					if ( p->GetType() == TYPE_XSP )
1177
					{
1178
						if ( !this->DisplayTip(TIPSECTION_YESNO, TIP_SHIPDISABLE) )
1179
							skipShips = true;
1180
 
1181
						if ( skipShips )
1182
							continue;
1183
					}
1184
 
1185
					m_pPackages->PrepareDisablePackage(p);
1186
					++count;
1187
				}
1188
				else
1189
				{
1190
					this->EnablePackage(p);
1191
					++count;
1192
				}
1193
			}
1194
		}
1195
 
1196
		if ( count )
1197
		{
1198
			this->Enabled = false;
1199
			this->StartBackground(MGUI_BACKGROUND_DISABLE);
1200
		}
1201
	}
1202
 
1203
	void MainGui::DisableEvent(System::Object ^Sender, System::EventArgs ^E)
1204
	{
1205
		if ( !ListPackages->SelectedItems->Count )
1206
			return;
1207
 
1208
		bool skipShips = false;
1209
 
1210
		ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1211
		System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1212
		int count = 0;
1213
		ArrayList ^List = gcnew ArrayList();
1214
 
1215
		while ( myEnum->MoveNext() )
1216
			List->Add(safe_cast<ListViewItem ^>(myEnum->Current));
1217
 
1218
		if ( List->Count )
1219
			this->DisableList(List);
1220
	}
1221
 
1222
	void MainGui::PackageBrowserEvent(System::Object ^Sender, System::EventArgs ^E)
1223
	{
1224
		this->Enabled = false;
1225
		PackageBrowser ^mod = gcnew PackageBrowser(m_pPackages, this->imageList1);
1226
		if ( !mod->AnyPackages() )
1227
		{
1228
			System::String ^game;
1229
			if ( m_pDirList && m_pDirList->Count() && m_pDirList->Head() )
1230
			{
1231
				CyString sGame = m_pDirList->Head()->data;
1232
				if ( sGame.IsIn("|") )
1233
					sGame = sGame.GetToken("|", 2);
1234
				game = SystemStringFromCyString(sGame);
1235
			}
1236
			this->DisplayMessageBox(false, "No Available Packages", "No available packages found for " + game, MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1237
 
1238
			this->Enabled = true;
1239
			return;
1240
		}
1241
 
1242
		if ( m_bDirLocked ) {
1243
			this->DisplayLocked(false);
1244
			this->Enabled = true;
1245
			return;
1246
		}
1247
 
1248
		mod->SetExperimental(m_bExperimental);
1249
		mod->SetCheat(m_bCheat);
1250
		mod->SetShips(m_bShips);
1251
		if ( m_pPackages->IsVanilla() )
1252
			mod->SetSigned(true);
1253
		else
1254
			mod->SetSigned(m_bSigned);
1255
		mod->SetDownload(m_bDownloadable);
1256
		mod->UpdatePackages();
1257
		System::Windows::Forms::DialogResult result = mod->ShowDialog(this);
1258
 
1259
		m_bDownloadable = mod->IsDownload();
1260
		m_bCheat = mod->IsCheat();
1261
		m_bExperimental = mod->IsExperimental();
1262
		m_bShips = mod->IsShips();
1263
		m_bSigned = mod->IsSigned();
1264
 
1265
		bool doenable = true;
1266
		CBaseFile *p = mod->SelectedMod();
1267
		if ( result == System::Windows::Forms::DialogResult::OK )
1268
		{
1269
			if ( p )
1270
			{
50 cycrow 1271
				if ( this->InstallPackage(_US(p->filename()), true, false, true) )
1 cycrow 1272
					doenable = false;
1273
			}
1274
		}
1275
		else if ( result == System::Windows::Forms::DialogResult::Yes )
1276
			this->StartInstalling(false, true);
1277
 
1278
		this->Enabled = doenable;
1279
	}
1280
 
1281
	void MainGui::CloseEvent(System::Object ^Sender, FormClosingEventArgs ^E)
1282
	{
1283
		int h = this->Size.Height;
1284
		int w = this->Size.Width;
1285
	}
1286
 
1287
	void MainGui::DisplayLocked(bool inthread) 
1288
	{
1289
		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);
1290
	}
1291
 
1292
	void MainGui::ModSelectorEvent(System::Object ^Sender, System::EventArgs ^E)
1293
	{
1294
		if ( m_pPackages->IsVanilla() )
1295
		{
1296
			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);
1297
			return;
1298
		}
1299
		this->Enabled = false;
1300
		ModSelector ^mod = gcnew ModSelector(m_pPackages, this->imageList1);
1301
 
1302
		if ( !mod->AnyPackages() )
1303
		{
1304
			this->DisplayMessageBox(false, "Mod Selector", "No available mods have been found", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1305
			this->Enabled = true;
1306
			return;
1307
		}
1308
 
1309
		if ( m_bDirLocked ) {
1310
			this->DisplayLocked(false);
1311
			this->Enabled = true;
1312
			return;
1313
		}
1314
 
1315
		if ( !m_bModSelectorDetails )
1316
		{
1317
			mod->HideDetails();
1318
			mod->Update();
1319
		}
1320
 
1321
		System::Windows::Forms::DialogResult result = mod->ShowDialog(this);
1322
		m_bModSelectorDetails = mod->ShowingDetails();
1323
 
1324
		// install the selected mod
1325
		bool reEnable = true;
1326
 
1327
		CBaseFile *p = mod->SelectedMod();
1328
		if ( result == System::Windows::Forms::DialogResult::OK )
1329
		{
1330
			if ( p )
1331
			{
1332
				// from file
1333
				if ( p->GetNum() < 0 )
1334
				{
1335
					if ( m_pPackages->GetEnabledMod() )
1336
						m_pPackages->DisablePackage(m_pPackages->GetEnabledMod(), 0, 0);
50 cycrow 1337
					if ( this->InstallPackage(_US(p->filename()), true, false, true) )
1 cycrow 1338
						reEnable = false;
1339
				}
1340
				// otherwise just enable it
1341
				else
1342
				{
1343
					if ( this->EnablePackage(p) )
1344
					{
1345
						this->StartBackground(MGUI_BACKGROUND_DISABLE);
1346
						reEnable = false;
1347
					}
1348
				}
1349
			}
1350
		}
1351
 
1352
		// install downloaded mods
1353
		else if ( result == Windows::Forms::DialogResult::Yes )
1354
			this->StartInstalling(false, true);
1355
 
1356
		// remove the current mod
1357
		else if ( result == System::Windows::Forms::DialogResult::Abort )
1358
		{
1359
			if ( m_pPackages->GetEnabledMod() )
1360
			{
1361
				CBaseFile *mod = m_pPackages->GetEnabledMod();
1362
				CyString message = mod->GetFullPackageName(m_pPackages->GetLanguage());
1363
				m_pPackages->DisablePackage(m_pPackages->GetEnabledMod(), 0, 0);			
1364
				this->DisplayMessageBox(false, "Mod Disabed", SystemStringFromCyString(message) + " has been disabled\nYour game is no longer using any mods\n", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1365
			}
1366
		}
1367
 
1368
		// uninstall the selected mod
1369
		else if ( result == System::Windows::Forms::DialogResult::Retry )
1370
		{
1371
			if ( p && p->GetNum() >= 0 )
1372
			{
1373
				m_pPackages->PrepareUninstallPackage(p);
1374
				if ( m_pPackages->GetNumPackagesInQueue() )
1375
				{
1376
					reEnable = false;
1377
					this->StartBackground(MGUI_BACKGROUND_UNINSTALL);
1378
				}
1379
			}
1380
		}
1381
 
1382
		if ( reEnable )
1383
			this->Enabled = true;
1384
 
1385
		mod->RemovePackages();
1386
	}
1387
 
1388
	void MainGui::UninstallList(ArrayList ^List)
1389
	{
1390
		bool skipShips = false;
1391
 
1392
		for ( int i = 0; i < List->Count; i++ )
1393
		{
1394
			CBaseFile *p = this->FindPackageFromList(safe_cast<ListViewItem ^>(List[i]));
1395
			if ( p )
1396
			{
1397
				if ( p->GetType() == TYPE_XSP )
1398
				{
1399
					if ( !this->DisplayTip(TIPSECTION_YESNO, TIP_SHIPUNINSTALL) )
1400
						skipShips = true;
1401
 
1402
					if ( skipShips )
1403
						continue;
1404
				}
1405
 
46 cycrow 1406
				// display uninstall text
1407
				Utils::String beforeText = m_pPackages->GetUninstallBeforeText(p).ToString();
1408
				if ( !beforeText.empty() ) {
1409
					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 )
1410
						continue;
1411
				}
1412
 
1 cycrow 1413
				m_pPackages->PrepareUninstallPackage(p);
1414
			}
1415
		}
1416
 
1417
		if ( m_pPackages->GetNumPackagesInQueue() )
1418
		{
1419
			this->Enabled = false;
1420
			this->StartBackground(MGUI_BACKGROUND_UNINSTALL);
1421
		}
1422
	}
1423
	void MainGui::UninstallEvent(System::Object ^Sender, System::EventArgs ^E)
1424
	{
1425
		if ( !ListPackages->SelectedItems->Count )
1426
			return;
1427
 
1428
		ArrayList ^List = gcnew ArrayList();
1429
 
1430
		ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1431
		System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1432
 
1433
		while ( myEnum->MoveNext() )
1434
			List->Add(safe_cast<ListViewItem ^>(myEnum->Current));
1435
 
1436
		this->UninstallList(List);
1437
	}
1438
 
1439
	void MainGui::ModifiedEvent(System::Object ^Sender, System::EventArgs ^E)
1440
	{
1441
		if ( m_bDirLocked ) {
1442
			this->DisplayLocked(false);
1443
			return;
1444
		}
1445
		if ( m_pPackages->IsVanilla() )
1446
		{
1447
			this->Enabled = false;
1448
			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 )
1449
			{
1450
				if ( m_iSaveGameManager == 1 )
1451
				{
1452
					m_pPackages->BackupSaves(true);
1453
					m_pPackages->RestoreSaves(false);
1454
				}
1455
				m_pPackages->SetVanilla(false);
1456
				m_pMenuBar->Modified();
1457
				m_pPackages->PrepareEnableLibrarys();
1458
				m_pPackages->PrepareEnableFromVanilla();
1459
				this->StartBackground(MGUI_BACKGROUND_DISABLE);
1460
			}
1461
			else
1462
				this->Enabled = true;
1463
		}
1464
	}
1465
 
1466
	void MainGui::VanillaEvent(System::Object ^Sender, System::EventArgs ^E)
1467
	{
1468
		if ( m_bDirLocked ) {
1469
			this->DisplayLocked(false);
1470
			return;
1471
		}
1472
		if ( !m_pPackages->IsVanilla() )
1473
		{
1474
			this->Enabled = false;
1475
			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 )
1476
			{
1477
				if ( m_iSaveGameManager == 1 )
1478
				{
1479
					m_pPackages->RestoreSaves(true);
1480
					m_pPackages->BackupSaves(false);
1481
				}
1482
				m_pPackages->SetVanilla(true);
1483
				m_pMenuBar->Vanilla();
1484
				m_pPackages->PrepareDisableForVanilla();
1485
				this->StartBackground(MGUI_BACKGROUND_DISABLE);
1486
			}
1487
			else
1488
				this->Enabled = true;
1489
		}
1490
	}
1491
 
1492
	void MainGui::InstallEvent(System::Object ^Sender, System::EventArgs ^E)
1493
	{
1494
		if ( m_bDirLocked ) {
1495
			this->DisplayLocked(false);
1496
			return;
1497
		}
1498
 
1499
		OpenFileDialog ^ofd = gcnew OpenFileDialog();
1500
		ofd->Filter = "All (*.spk, *.xsp)|*.spk;*.xsp|Package Files (*.spk)|*.spk|Ship Files (*.xsp)|*.xsp";
1501
		ofd->FilterIndex = 1;
1502
		ofd->RestoreDirectory = true;
1503
		ofd->Multiselect = true;
1504
 
1505
		this->Enabled = false;
1506
		if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
1507
		{
1508
			bool anytoinstall = false;
1509
			array<System::String ^> ^fileArray = ofd->FileNames;
1510
			for ( int i = 0; i < fileArray->Length; i++ )
1511
			{
1512
				System::String ^file = fileArray[i];
1513
				if ( this->InstallPackage(file, false, false, true) )
1514
					anytoinstall = true;
1515
			}
1516
 
1517
			if ( anytoinstall )
1518
				this->StartInstalling(false, true);
1519
		}
1520
		else
1521
		{
1522
			ProgressBar->Hide();
1523
			this->Enabled = true;
1524
		}
1525
	}
1526
 
1527
	void MainGui::CheckUnusedShared()
1528
	{
1529
		if ( m_pPackages->AnyUnusedShared() )
1530
		{
1531
			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)
1532
				m_pPackages->RemoveUnusedSharedFiles();
1533
		}
1534
	}
1535
 
1536
	void MainGui::ChangeDirectoryEvent(System::Object ^Sender, System::EventArgs ^E)
1537
	{
1538
		if ( m_iBackgroundTask == MGUI_BACKGROUND_ADDDIR )
1539
			return;
1540
 
1541
		CyString dir = CyStringFromSystemString(ComboDir->Text);
1542
		if ( dir.NumToken(" [") )
1543
			dir = dir.GetToken(" [", 1, dir.NumToken(" [") - 1);
1544
 
1545
		if ( !m_pPackages->IsCurrentDir(dir) )
1546
		{
1547
			this->Enabled = false;
1548
			this->CheckUnusedShared();
1549
			this->StartBackground(MGUI_BACKGROUND_CHANGEDIR, SystemStringFromCyString(dir));
1550
		}
1551
	}
1552
 
1553
	void MainGui::Background_DoWork(System::Object ^Sender, DoWorkEventArgs ^E)
1554
	{
1555
		m_bDisplayMessage = false;
1556
		m_bDisplayDialog = false;
1557
 
1558
		switch ( m_iBackgroundTask )
1559
		{
1560
			case MGUI_BACKGROUND_INSTALL:
1561
				this->DoInstall(false, true);
1562
				break;
1563
			case MGUI_BACKGROUND_INSTALLBUILTIN:
1564
				this->DoInstall(true, true);
1565
				break;
1566
			case MGUI_BACKGROUND_UNINSTALL:
1567
				this->DoUninstall();
1568
				break;
1569
			case MGUI_BACKGROUND_DISABLE:
1570
				this->DoDisable();
1571
				break;
1572
			case MGUI_BACKGROUND_CHANGEDIR:
1573
			case MGUI_BACKGROUND_ADDDIR:
1574
				this->ChangeDirectory(CyStringFromSystemString(m_sBackgroundInfo));
1575
				break;
1576
			case MGUI_BACKGROUND_REMOVEDIR:
1577
				this->RemoveCurrentDirectory();
1578
		}
1579
	}
1580
 
1581
	void MainGui::Background_Finished()
1582
	{
1583
		ProgressBar->Hide();
1584
 
1585
		if ( m_bDisplayMessage )
1586
			MessageBox::Show(this, m_sMessageText, m_sMessageTitle, m_messageButtons, m_messageIcon);
1587
 
1588
		if ( m_bDisplayDialog )
1589
		{
1590
			if ( m_pPi->PackageCount() )
1591
			{
1592
				m_pPi->AdjustColumns();
1593
				m_pPi->ShowDialog(this);
1594
			}
1595
		}
1596
 
1597
		m_bDisplayDialog = false;
1598
		m_bDisplayMessage = false;
1599
 
1600
		if ( m_iBackgroundTask == MGUI_BACKGROUND_CHANGEDIR || m_iBackgroundTask == MGUI_BACKGROUND_ADDDIR )
1601
		{
1602
			// switch the dir list
1603
			if ( ComboDir->Text )
1604
			{
1605
				CyString dir = CyStringFromSystemString(ComboDir->Text);
1606
				if ( !dir.Empty() )
1607
				{
1608
					if ( (m_iBackgroundTask == MGUI_BACKGROUND_CHANGEDIR) && (dir.NumToken(" [")) )
1609
						dir = dir.GetToken(" [", 1, dir.NumToken(" [") - 1);
1610
					if ( m_pDirList->FindString(dir) )
1611
					{
1612
						CyString data = m_pDirList->FindString(dir)->data;
1613
						m_pDirList->Remove(dir, false);
1614
						m_pDirList->PushFront(dir, data);
1615
					}
1616
					else
1617
					{
1618
						int lang = m_pPackages->GetGameLanguage(dir);
1619
						if ( lang )
1620
							m_pDirList->PushFront(dir, CyString::Number(lang) + "|" + m_pPackages->GetGameName(dir));
1621
						else
1622
							m_pDirList->PushFront(dir, m_pPackages->GetGameName(dir));
1623
					}
1624
 
1625
					this->UpdateDirList();
1626
				}
1627
			}
1628
		}
1629
 
1630
		// display any files that failed
1631
		if ( m_iBackgroundTask == MGUI_BACKGROUND_INSTALL )
1632
		{
1633
			String ^files = "";
1634
			for ( SStringList *str = m_pFileErrors->Head(); str; str = str->next )
1635
			{
1636
				if ( str->data.ToInt() == SPKINSTALL_WRITEFILE_FAIL )
1637
				{
1638
					files += "\n";
1639
					files += SystemStringFromCyString(str->str);
1640
				}
1641
			}
1642
 
1643
			if ( files->Length )
1644
				MessageBox::Show(this, "These files failed to install\n" + files, "Failed Files", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1645
		}
1646
 
1647
		switch ( m_iBackgroundTask )
1648
		{
1649
			case MGUI_BACKGROUND_CHANGEDIR:
1650
			case MGUI_BACKGROUND_ADDDIR:
1651
				this->UpdateControls();
1652
				this->UpdatePackages();
1653
				this->CheckProtectedDir();
1654
				m_bRunningBackground = false;
1655
				if ( this->UpdateBuiltInPackages() )
1656
					return;
1657
				break;
1658
 
1659
			case MGUI_BACKGROUND_INSTALL:
1660
			case MGUI_BACKGROUND_INSTALLBUILTIN:
1661
			case MGUI_BACKGROUND_UNINSTALL:
1662
			case MGUI_BACKGROUND_DISABLE:
1663
				this->UpdatePackages();
1664
				break;
1665
			case MGUI_BACKGROUND_REMOVEDIR:
1666
				ComboDir->Items->RemoveAt(0);
1667
				this->DisplayMessageBox(false, "Remove Directory", "Directory has been removed\n" + SystemStringFromCyString(m_pRemovedDirList->Tail()->str), MessageBoxButtons::OK, MessageBoxIcon::Information);
1668
				break;
1669
		}
1670
 
1671
		if ( m_iBackgroundTask == MGUI_BACKGROUND_ADDDIR )
1672
			this->DisplayMessageBox(false, "Add Game Folder", "Folder \"" + m_sBackgroundInfo + "\" added\nGame: " + SystemStringFromCyString(m_pPackages->GetGameName(CyStringFromSystemString(m_sBackgroundInfo))), MessageBoxButtons::OK, MessageBoxIcon::Information);
1673
 
1674
		else if ( m_iBackgroundTask == MGUI_BACKGROUND_REMOVEDIR )
1675
		{
1676
			// no more directories left
1677
			if ( m_pDirList->Empty() )
1678
			{
1679
				m_bRunningBackground = false;
1680
				this->Close();
1681
			}
1682
			// otherwise, open the next directory
1683
			else
1684
			{
1685
				m_bRunningBackground = false;
1686
				ComboDir->SelectedItem = ComboDir->Items[0];
1687
//				this->StartBackground(MGUI_BACKGROUND_CHANGEDIR, SystemStringFromCyString(m_pDirList->Head()->str));
1688
				return;
1689
			}
1690
		}
1691
 
1692
		m_iBackgroundTask = MGUI_BACKGROUND_NONE;
1693
 
1694
		this->Enabled = true;
1695
 
1696
		m_bRunningBackground = false;
1697
	}
1698
 
1699
	void MainGui::Background_Progress(System::Object ^Sender, ProgressChangedEventArgs ^E)
1700
	{
1701
		this->ProgressBar->Value = E->ProgressPercentage;
1702
	}
1703
 
1704
	bool MainGui::UpdateBuiltInPackages()
1705
	{
1706
		// find all built-in packages
53 cycrow 1707
		System::String ^dir = ".\\Required";
78 cycrow 1708
 
53 cycrow 1709
		if ( System::IO::Directory::Exists(dir) ) 
1 cycrow 1710
		{
1711
			bool installing = false;
53 cycrow 1712
			array <System::String ^> ^Files = System::IO::Directory::GetFiles(dir, "*.spk");
1 cycrow 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
50 cycrow 1729
				CBaseFile *check = m_pPackages->FindSpkPackage(p->name(), p->author());
1 cycrow 1730
				if ( check )
1731
				{
50 cycrow 1732
					if ( check->version().compareVersion(p->version()) != COMPARE_OLDER )
1 cycrow 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
80 cycrow 1771
			m_pUpdateList->PushBack("http://xpluginmanager.co.uk/pmupdate.dat", "", true);
1 cycrow 1772
			if ( (int)PMLBETA )
80 cycrow 1773
				m_pUpdateList->PushBack("http://xpluginmanager/Beta/pmupdatebeta.dat", "", true);
1 cycrow 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
49 cycrow 2241
				if ( !p->forumLink().empty() ) {
2242
					Utils::String web = p->forumLink();
2243
					if ( web.isNumber() )
2244
						web = Utils::String("http://forum.egosoft.com/viewtopic.php?t=") + web; 
1 cycrow 2245
 
2246
					this->visitForumPageToolStripMenuItem->Visible = true;
49 cycrow 2247
					if ( !web.isin("http://") )
2248
						this->visitForumPageToolStripMenuItem->Tag = "http://" + _US(web);
1 cycrow 2249
					else
49 cycrow 2250
						this->visitForumPageToolStripMenuItem->Tag = _US(web);
1 cycrow 2251
					showSep2 = true;
2252
				}
49 cycrow 2253
				if ( !p->email().empty() )
1 cycrow 2254
				{
2255
					this->emailAuthorToolStripMenuItem->Visible = true;
50 cycrow 2256
					this->emailAuthorToolStripMenuItem->Tag = "mailto://" + _US(p->email()) + "?subject=Re: " + _US(p->name().findReplace(" ", "%20"));
1 cycrow 2257
					showSep2 = true;
2258
				}
49 cycrow 2259
				if ( !p->webSite().empty() ) {
1 cycrow 2260
					this->visitWebSiteToolStripMenuItem->Visible = true;
49 cycrow 2261
					if ( !p->webSite().isin("http://") ) 
2262
						this->visitWebSiteToolStripMenuItem->Tag = "http://" + _US(p->webSite());
2263
					else	
2264
						this->visitWebSiteToolStripMenuItem->Tag = _US(p->webSite());
1 cycrow 2265
					showSep2 = true;
2266
				}
2267
 
49 cycrow 2268
				if ( !p->webAddress().empty() )
1 cycrow 2269
					this->checkForUpdatesToolStripMenuItem->Visible = true;
2270
			}
2271
			else
2272
				m_pListItem = nullptr;
2273
 
2274
			if ( this->ListPackages->SelectedItems->Count > 1 || !p )
2275
			{
2276
				this->UninstallSelectedContext->Visible = true;
2277
				this->UninstallSelectedContext->Text = "Uninstall Selected (" + System::Convert::ToString(this->ListPackages->SelectedItems->Count) + " packages)";
2278
			}
2279
 
2280
			if ( p )
2281
			{
2282
				if ( p->IsEnabled() )
2283
					this->ContextDisable->Visible = true;
2284
				else
2285
					this->ContextEnable->Visible = true;
2286
			}
2287
 
2288
			this->ContextSeperator->Visible = showSep;
2289
			this->ContextSeperator2->Visible = showSep2;
2290
			E->Cancel = false;
2291
		}
2292
	}
2293
 
2294
	System::Void MainGui::ListPackages_DragOver(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e)
2295
	{
2296
		e->Effect = DragDropEffects::None;
2297
 
2298
		if (e->Data->GetDataPresent(DataFormats::FileDrop)) 
2299
		{
2300
			cli::array<String ^> ^a = (cli::array<String ^> ^)e->Data->GetData(DataFormats::FileDrop, false);
2301
			int i;
2302
			for(i = 0; i < a->Length; i++)
2303
			{
2304
				String ^s = a[i];
2305
				String ^ext = IO::FileInfo(s).Extension;
2306
				if ( String::Compare(IO::FileInfo(s).Extension, ".xsp", true) == 0 || String::Compare(IO::FileInfo(s).Extension, ".spk", true) == 0 )
2307
				{
2308
					e->Effect = DragDropEffects::Copy;
2309
					break;
2310
				}
2311
			}
2312
		}
2313
	}
2314
 
2315
	System::Void MainGui::ListPackages_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e)
2316
	{
2317
		if (e->Data->GetDataPresent(DataFormats::FileDrop)) 
2318
		{
2319
			cli::array<String ^> ^a = (cli::array<String ^> ^)e->Data->GetData(DataFormats::FileDrop, false);
2320
			int i;
2321
			for(i = 0; i < a->Length; i++)
2322
			{
2323
				String ^s = a[i];
2324
				String ^ext = IO::FileInfo(s).Extension;
2325
				if ( String::Compare(IO::FileInfo(s).Extension, ".xsp", true) == 0 || String::Compare(IO::FileInfo(s).Extension, ".spk", true) == 0 )
2326
				{
2327
					if ( m_bDirLocked ) {
2328
						this->DisplayLocked(false);
2329
						return;
2330
					}
2331
					this->InstallPackage(s, false, false, true);
2332
				}
2333
			}
2334
 
2335
			this->StartInstalling(false, true);
2336
		}
2337
	}
2338
 
2339
	bool MainGui::CheckAccessRights(String ^dir)
2340
	{
2341
		/*
2342
		// check if already exists
2343
		String ^file = dir + "\\accessrightscheck.dat";
2344
		String ^writeStr = "testing file access";
2345
		if ( IO::File::Exists(file) )
2346
		{
2347
			// remove it
2348
			IO::File::Delete(file);
2349
			// still exists, cant delete it
2350
			if ( IO::File::Exists(file) )
2351
				return false;
2352
		}
2353
 
2354
		IO::DirectoryInfo ^dInfo = gcnew IO::DirectoryInfo(dir);
2355
		Security::AccessControl::DirectorySecurity ^dSecurity = dInfo->GetAccessControl();
2356
		dSecurity->
2357
 
2358
 
2359
		System::IO::FileStream ^writeStream = nullptr;
2360
		IO::BinaryWriter ^writer = nullptr;
2361
		try {
2362
			 writeStream = gcnew System::IO::FileStream(file, System::IO::FileMode::Create);
2363
			 writer = gcnew IO::BinaryWriter(writeStream);
2364
		}
2365
		catch (System::IO::IOException ^e)
2366
		{
2367
			MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2368
		}
2369
		catch (System::Exception ^e)
2370
		{
2371
			MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2372
		}
2373
		finally 
2374
		{
2375
			writer->Write(writeStr);
2376
			writer->Close();
2377
			writeStream->Close();
2378
		}
2379
 
2380
		// check if its written
2381
		if ( !IO::File::Exists(file) )
2382
			return false;
2383
 
2384
		// remove the file again
2385
		IO::File::Delete(file);
2386
		if ( IO::File::Exists(file) )
2387
			return false;
2388
*/
2389
		return true;
2390
	}
2391
 
2392
	System::Void MainGui::RunItem(System::Object ^sender, System::EventArgs ^e)
2393
	{
2394
		Windows::Forms::ToolStripMenuItem ^item = cli::safe_cast<ToolStripMenuItem ^>(sender);
2395
		String ^file = Convert::ToString(item->Tag);
2396
 
2397
		if ( IO::File::Exists(file) )
2398
		{
2399
			System::Diagnostics::Process::Start(file);
2400
		}
2401
	}
2402
 
2403
	void MainGui::RunFromToolItem(ToolStripMenuItem ^item)
2404
	{
2405
		if ( !item ) return;
2406
		if ( !item->Tag ) return;
2407
 
2408
		String ^file = Convert::ToString(item->Tag);
2409
		System::Diagnostics::Process::Start(file);
2410
	}
2411
 
2412
	void MainGui::FakePatchControlDialog()
2413
	{
2414
		FakePatchControl ^fpc = gcnew FakePatchControl(m_pPackages);
2415
		if ( fpc->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2416
		{
2417
			m_pPackages->ApplyFakePatchOrder(fpc->GetPatchOrder());
2418
			m_pPackages->ShuffleFakePatches(0);
2419
		}
2420
	}
2421
 
2422
	void MainGui::CheckFakePatchCompatability()
2423
	{
2424
		CyStringList errorList;
2425
		int count = 0;
2426
		int packageCount = 0;
2427
		for ( CBaseFile *p = m_pPackages->GetFirstPackage(); p; p = m_pPackages->GetNextPackage(p) )
2428
		{
2429
			if ( !p->IsEnabled() ) continue;
2430
			if ( !p->AnyFileType(FILETYPE_MOD) ) continue;
2431
 
2432
			CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
2433
 
2434
			// compare this file against all other packages
2435
			for ( CBaseFile *comparePackage = m_pPackages->GetNextPackage(p); comparePackage; comparePackage = m_pPackages->GetNextPackage(comparePackage) )
2436
			{
2437
				if ( comparePackage == p ) continue; // dont include the same package
2438
				if ( !comparePackage->IsEnabled() ) continue;
2439
				if ( !comparePackage->AnyFileType(FILETYPE_MOD) ) continue;
2440
 
2441
				CyStringList list;
2442
				if ( m_pPackages->CheckCompatabilityBetweenMods(p, comparePackage, &list) )
2443
				{
2444
					CyString package2Name = comparePackage->GetFullPackageName(m_pPackages->GetLanguage());
2445
					for ( SStringList *str = list.Head(); str; str = str->next )
2446
					{
2447
						errorList.PushBack(str->str + " (" + packageName + ")", str->data + " (" + package2Name + ")");
2448
						++count;
2449
					}
2450
					++packageCount;
2451
				}
2452
			}
2453
		}
2454
 
2455
		if ( count )
2456
		{
2457
			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 )
2458
			{
2459
				CompareList ^cl = gcnew CompareList("Fake Patch Incompatabilities");
2460
				cl->AddStringList(errorList);
2461
				cl->ShowDialog(this);
2462
			}
2463
		}
2464
		else
2465
			MessageBox::Show(this, "No incompatabilities found between fake patches", "Fake Patch Compatability", MessageBoxButtons::OK, MessageBoxIcon::Information);
2466
	}
2467
 
2468
	void MainGui::EditGlobalsDialog()
2469
	{
2470
		if ( m_pPackages->IsVanilla() ) {
2471
			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);
2472
			return;
2473
		}
2474
		if ( m_bDirLocked ) {
2475
			this->DisplayLocked(false);
2476
			return;
2477
		}
2478
 
2479
		//load globals
2480
		CyStringList globals;
2481
		m_pPackages->ReadGlobals(globals);
2482
 
2483
		EditGlobals ^edit = gcnew EditGlobals(&globals);
2484
 
2485
		// make our saved changes
2486
		for ( SStringList *str = m_pPackages->GetGlobals()->Head(); str; str = str->next )
2487
			edit->SetEditedItem(SystemStringFromCyString(str->str), SystemStringFromCyString(str->data));
2488
 
2489
		if ( edit->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2490
		{
2491
			// compare whats different and save
2492
			m_pPackages->GetGlobals()->Clear();
2493
			for ( SStringList *str = edit->GetSavedSettings()->Head(); str; str = str->next )
2494
				m_pPackages->GetGlobals()->PushBack(str->str, str->data);
2495
		}
2496
	}
2497
 
2498
	void MainGui::ViewFileLog()
2499
	{
2500
		if ( m_pFileErrors->Empty() )
2501
			MessageBox::Show(this, "No messages to view in file log", "Empty File Log", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2502
		else
2503
		{
2504
			FileLog ^log = gcnew FileLog;
2505
			for ( SStringList *str = m_pFileErrors->Head(); str; str = str->next )
2506
			{
2507
				bool add = true;
2508
				String ^status = "Unknown Error";
2509
				switch(str->data.GetToken(" ", 1, 1).ToInt())
2510
				{
2511
					case SPKINSTALL_CREATEDIRECTORY:
2512
						status = "Created Directory";
2513
						break;
2514
					case SPKINSTALL_CREATEDIRECTORY_FAIL:
2515
						status = "Failed to create Directory";
2516
						break;
2517
					case SPKINSTALL_WRITEFILE:
2518
						status = "File Written";
2519
						break;
2520
					case SPKINSTALL_WRITEFILE_FAIL:
2521
						status = "Failed to Write File";
2522
						break;
2523
					case SPKINSTALL_DELETEFILE:
2524
						status = "Deleted File";
2525
						break;
2526
					case SPKINSTALL_DELETEFILE_FAIL:
2527
						status = "Failed to Delete File";
2528
						break;
2529
					case SPKINSTALL_SKIPFILE:
2530
						status = "File Skipped";
2531
						break;
2532
					case SPKINSTALL_REMOVEDIR:
2533
						status = "Removed Directory";
2534
						break;
2535
					case SPKINSTALL_ENABLEFILE:
2536
						status = "Enabled File";
2537
						break;
2538
					case SPKINSTALL_DISABLEFILE:
2539
						status = "Disabled File";
2540
						break;
2541
					case SPKINSTALL_ENABLEFILE_FAIL:
2542
						status = "Failed to Enable File";
2543
						break;
2544
					case SPKINSTALL_DISABLEFILE_FAIL:
2545
						status = "Failed to Disable File";
2546
						break;
2547
					case SPKINSTALL_UNINSTALL_MOVE:
2548
						status = "Moved Uninstall File";
2549
						break;
2550
					case SPKINSTALL_UNINSTALL_COPY:
2551
						status = "Copied Uninstall File";
2552
						break;
2553
					case SPKINSTALL_UNINSTALL_MOVE_FAIL:
2554
						status = "Failed to move uninstall file";
2555
						break;
2556
					case SPKINSTALL_UNINSTALL_COPY_FAIL:
2557
						status = "Failed to copy uninstall file";
2558
						break;
2559
					case SPKINSTALL_UNINSTALL_REMOVE:
2560
						status = "Removed uninstall file";
2561
						break;
2562
					case SPKINSTALL_UNINSTALL_REMOVE_FAIL:
2563
						status = "Failed to remove uninstall file";
2564
						break;
2565
					case SPKINSTALL_ORIGINAL_BACKUP:
2566
						status = "Backed up Original";
2567
						break;
2568
					case SPKINSTALL_ORIGINAL_RESTORE:
2569
						status = "Restored Original";
2570
						break;
2571
					case SPKINSTALL_ORIGINAL_BACKUP_FAIL:
2572
						status = "Failed to Backup Original";
2573
						break;
2574
					case SPKINSTALL_ORIGINAL_RESTORE_FAIL:
2575
						status = "Failed to restore Original";
2576
						break;
2577
					case SPKINSTALL_FAKEPATCH:
2578
						status = "Adjusting Fakepatch";
2579
						break;
2580
					case SPKINSTALL_FAKEPATCH_FAIL:
2581
						status = "Failed to adjust Fakepatch";
2582
						break;
2583
					case SPKINSTALL_AUTOTEXT:
2584
						status = "Adjusting Text File";
2585
						break;
2586
					case SPKINSTALL_AUTOTEXT_FAIL:
2587
						status = "Failed to adjust Text File";
2588
						break;
2589
					case SPKINSTALL_MISSINGFILE:
2590
						status = "Missing File";
2591
						break;
2592
					case SPKINSTALL_SHARED:
2593
						status = "Shared File";
2594
						break;
2595
					case SPKINSTALL_SHARED_FAIL:
2596
						status = "Shared File Failed";
2597
						break;
2598
					case SPKINSTALL_ORPHANED:
2599
						status = "File Orphaned";
2600
						break;
2601
					case SPKINSTALL_ORPHANED_FAIL:
2602
						status = "Failed to Orphan file";
2603
						break;
2604
					case SPKINSTALL_UNCOMPRESS_FAIL:
2605
						status = "Failed to Uncompress";
2606
						break;
2607
				}
2608
 
2609
				if ( add )
2610
				{
2611
					if ( str->data.NumToken(" ") > 1 )
2612
						log->AddItem(SystemStringFromCyString(str->str.findreplace("~", " => ")), status, SystemStringFromCyString(SPK::ConvertTimeString((long)str->data.GetToken(" ", 2, 2).ToLong())));
2613
					else
2614
						log->AddItem(SystemStringFromCyString(str->str.findreplace("~", " => ")), status, nullptr);
2615
				}
2616
			}
2617
			if ( log->ShowDialog(this) == Windows::Forms::DialogResult::Cancel )
2618
			{
2619
				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 )
2620
				{
2621
					m_pFileErrors->Clear();
2622
					MessageBox::Show(this, "The file log has been cleared", "File Log Cleared", MessageBoxButtons::OK, MessageBoxIcon::Information);
2623
				}
2624
			}
2625
 
2626
		}
2627
	}
2628
 
2629
	void MainGui::VerifyInstalledFiles()
2630
	{
2631
		CyStringList missing;
2632
		int amount = m_pPackages->VerifyInstalledFiles(&missing);
2633
		if ( !amount )
2634
			MessageBox::Show(this, "All files are currently installed", "Verifying Installed Files", MessageBoxButtons::OK, MessageBoxIcon::Information);
2635
		else
2636
		{
2637
			String ^text;
2638
			for ( SStringList *str = missing.Head(); str; str = str->next )
2639
			{
2640
				text += SystemStringFromCyString(str->str);
2641
				text += "\n\t";
2642
				CyString data = str->data.findreplace("\n", "\t\n");
2643
				text += SystemStringFromCyString(data);
2644
				text += "\n\n";
2645
			}
2646
			MessageBoxDetails::Show(this, "Verifing Installed Files", "Missing files detected\nAmount = " + amount, text, false, 600);
2647
		}
2648
	}
2649
 
2650
	void MainGui::ExportPackageList()
2651
	{
2652
		bool enabled = false;
2653
		if ( MessageBox::Show(this, "Do you only want to export enabled packages?", "Only Enabled", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == Windows::Forms::DialogResult::Yes )
2654
			enabled =true;
2655
		SaveFileDialog ^ofd = gcnew SaveFileDialog();
2656
		ofd->Filter = "Log Files (*.log)|*.log";
2657
		ofd->FilterIndex = 1;
2658
		ofd->RestoreDirectory = true;
2659
		ofd->AddExtension =  true;
2660
		ofd->Title = "Select the file to save the package list to";
2661
		if ( ofd->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2662
		{
2663
			if ( IO::File::Exists(ofd->FileName) )
2664
				IO::File::Delete(ofd->FileName);
2665
 
2666
			StreamWriter ^sw = File::CreateText(ofd->FileName);
2667
			try 
2668
			{
2669
				for ( CBaseFile *package = m_pPackages->FirstPackage(); package; package = m_pPackages->NextPackage() )
2670
				{
2671
					if ( enabled && !package->IsEnabled() ) continue;
50 cycrow 2672
					Utils::String line = package->name() + " :: " + package->author() + " :: " + package->version() + " :: " + package->creationDate() + " :: ";
1 cycrow 2673
 
2674
					if ( package->GetType() == TYPE_XSP )
2675
						line += "Ship :: ";
2676
					else if ( package->GetType() == TYPE_ARCHIVE )
2677
						line += "- Archive - :: ";
50 cycrow 2678
					else if ( package->GetType() == TYPE_SPK ) {
2679
						Utils::String type = ((CSpkFile *)package)->GetScriptTypeString(m_pPackages->GetLanguage());
2680
						if ( !type.empty() ) line += type + " :: ";
1 cycrow 2681
					}
2682
 
50 cycrow 2683
					line = line + ((package->IsEnabled()) ? "Yes" : "No") + " :: " + ((package->IsSigned()) ? "Yes" : "No");
2684
					sw->WriteLine(_US(line));
1 cycrow 2685
				}
2686
			}
2687
			finally
2688
			{
2689
				if ( sw )
2690
					delete (IDisposable ^)sw;
2691
			}				
2692
 
2693
			if ( IO::File::Exists(ofd->FileName) )
2694
			{
2695
				if ( enabled )
2696
					MessageBox::Show(this, "Enabled Packages have been saved to:\n" + ofd->FileName, "Package List Saved", MessageBoxButtons::OK, MessageBoxIcon::Information);
2697
				else
2698
					MessageBox::Show(this, "Complete Package List has been saved to:\n" + ofd->FileName, "Package List Saved", MessageBoxButtons::OK, MessageBoxIcon::Information);
2699
			}
2700
			else
2701
				MessageBox::Show(this, "There was an error writing file:\n" + ofd->FileName, "File Write Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
2702
		}
2703
	}
2704
}
2705