Subversion Repositories spk

Rev

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