Subversion Repositories spk

Rev

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