Subversion Repositories spk

Rev

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

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