Subversion Repositories spk

Rev

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

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