Subversion Repositories spk

Rev

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