Subversion Repositories spk

Rev

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