Subversion Repositories spk

Rev

Rev 163 | Rev 168 | 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)
164 cycrow 885
			m_lAvailablePackages->clear();
133 cycrow 886
		if (!m_lAvailablePackages)
887
			m_lAvailablePackages = new CLinkList<CBaseFile>;
888
 
889
		String ^curDir = System::IO::FileInfo(System::Windows::Forms::Application::ExecutablePath).DirectoryName;
164 cycrow 890
		m_pPackages->updateFoundPackages(_S(curDir));
133 cycrow 891
 
892
		// find packages from registry key
893
		RegistryKey ^searchKey = Registry::CurrentUser->OpenSubKey("Software\\Egosoft\\SuperBox");
894
		if (searchKey)
895
		{
896
			String ^dir = System::Convert::ToString(searchKey->GetValue("Addons"));
897
			if (dir)
164 cycrow 898
				m_pPackages->addFoundPackages(_S(dir));
133 cycrow 899
		}
900
 
901
		// find packages from DVD
902
		cli::array<IO::DriveInfo ^> ^Drives = IO::DriveInfo::GetDrives();
903
		if (Drives)
904
		{
905
			for (int i = 0; i < Drives->Length; i++)
906
			{
907
				IO::DriveInfo ^info = Drives[i];
908
				if (info->DriveType != IO::DriveType::CDRom)
909
					continue;
910
				if (info->IsReady)
164 cycrow 911
					m_pPackages->addFoundPackages(_S(info->RootDirectory + "XPluginManager"));
133 cycrow 912
			}
913
		}
914
 
164 cycrow 915
		for (auto p = m_pPackages->getFoundPackageList()->First(); p; p = m_pPackages->getFoundPackageList()->Next())
916
			m_lAvailablePackages->push_back(p);
917
 
133 cycrow 918
		int num = 0;
919
		for (CBaseFile *p = m_lAvailablePackages->First(); p; p = m_lAvailablePackages->Next())
920
		{
921
			p->SetNum(num);
922
			++num;
923
		}
924
	}
925
 
926
 
1 cycrow 927
	bool MainGui::StartInstalling(bool builtin, bool background, bool archive)
928
	{
929
		// no packages to install
930
		if ( !m_pPackages->GetNumPackagesInQueue() )
931
			return false;
932
 
933
		// clear selected
934
		this->ClearSelectedItems();
935
 
936
		// lets install them now
937
		CLinkList<CBaseFile> lCheckPackages;
938
		if ( m_pPackages->CheckPreparedInstallRequired(&lCheckPackages) )
939
		{
133 cycrow 940
			CLinkList<CBaseFile> *installRequired = new CLinkList<CBaseFile>();
1 cycrow 941
			for ( CListNode<CBaseFile> *pNode = lCheckPackages.Front(); pNode; pNode = pNode->next() )
942
			{
943
				CBaseFile *package = pNode->Data();
944
				CSpkFile *spk = (CSpkFile *)package;
945
 
133 cycrow 946
				if (m_pPackages->findAllNeededDependacies(package, *m_lAvailablePackages, installRequired, false, true))
947
				{
948
					bool added = false;
949
					for (auto itr = installRequired->Front(); itr; itr = itr->next())
950
					{
951
						if (itr->Data() == package || (itr->Data()->name().Compare(package->name()) && itr->Data()->author().Compare(package->author())))
952
						{
953
							added = true;
954
							break;
955
						}
956
					}
957
					if (!added)
958
						installRequired->push_back(package);
959
					continue;
960
				}
961
 
962
				Utils::CStringList missingList;
1 cycrow 963
				if ( m_pPackages->GetMissingDependacies(package, &missingList) )
964
				{
133 cycrow 965
					Utils::String requires;
966
					for (auto itr = missingList.begin(); itr != missingList.end(); itr++)
1 cycrow 967
					{
133 cycrow 968
						Utils::String name = (*itr)->str;;
969
						Utils::String author = (*itr)->data;
970
						Utils::String version;
971
						if (name.contains("|"))
972
						{
973
							version = name.token("|", 2);
974
							name = name.token("|", 1);
975
						}
976
 
977
						if (!version.empty())
978
							requires += name + " V" + version + " by " + author;
1 cycrow 979
						else
133 cycrow 980
							requires += name + " by " + author;
1 cycrow 981
						requires += "\n";
982
					}
133 cycrow 983
 
984
					this->DisplayMessageBox(false, "Installing", "Missing Package for " + SystemStringFromCyString(package->GetLanguageName(m_pPackages->GetLanguage())) + "\nRequires:\n" + _US(requires), MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1 cycrow 985
				}
986
			}
133 cycrow 987
 
988
			bool restart = false;
989
			if (installRequired->size())
990
			{
991
				bool errored = false;
992
				for (auto itr = installRequired->Front(); itr; itr = itr->next())
993
				{
994
					CBaseFile *package = NULL;
995
					int error = 0;
996
					if (CFileIO::Exists(itr->Data()->filename()))
997
						if (!this->InstallPackage(_US(itr->Data()->filename()), false, builtin, background))
998
							errored = true;
999
				}
1000
				if (!errored)
1001
					restart = true;
1002
			}
1003
 
1004
			delete installRequired;
1005
			lCheckPackages.MemoryClear();
1006
			if(restart)
1007
				return StartInstalling(builtin, background);
1 cycrow 1008
		}
1009
 
1010
		// no packages to install
1011
		if ( !m_pPackages->GetNumPackagesInQueue() )
1012
		{
1013
			this->Enabled = true;
1014
			this->ProgressBar->Hide();
1015
			return false;
1016
		}
1017
 
1018
		ProgressBar->Show();
1019
		this->Enabled = false;
1020
 
1021
		if ( builtin )
1022
		{
1023
			if ( background )
1024
				this->StartBackground(MGUI_BACKGROUND_INSTALLBUILTIN);
1025
			else
1026
				this->DoInstall(builtin, false);
1027
		}
1028
		else if ( archive )
1029
		{
1030
			if ( background )
1031
				this->StartBackground(MGUI_BACKGROUND_INSTALL);
1032
			else
1033
				this->DoInstall(false, false);
1034
		}
1035
		else
1036
		{
1037
			InstallPackageDialog ^installDialog = gcnew InstallPackageDialog(m_pPackages);
1038
			if ( installDialog->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
1039
			{
1040
				// no packages to install
1041
				if ( !m_pPackages->GetNumPackagesInQueue() )
1042
				{
1043
					this->Enabled = true;
1044
					this->ProgressBar->Hide();
1045
					return false;
1046
				}
1047
				if ( background )
1048
					this->StartBackground(MGUI_BACKGROUND_INSTALL);
1049
				else
1050
					this->DoInstall(false, false);
1051
			}
1052
			else
1053
			{
1054
				m_pPackages->RemovePreparedInstall(NULL);
1055
				this->Enabled = true;
1056
				this->ProgressBar->Hide();
1057
				return false;
1058
			}
1059
		}
1060
		return true;
1061
	}
1062
 
1063
	bool MainGui::StartBackground(int type, System::String ^info)
1064
	{
1065
		if ( backgroundWorker1->IsBusy )
1066
			return false;
1067
		if ( m_bRunningBackground )
1068
			return false;
1069
 
1070
		m_sBackgroundInfo = info;
1071
		return this->StartBackground(type);
1072
	}
1073
 
1074
	bool MainGui::StartBackground(int type)
1075
	{
1076
		if ( backgroundWorker1->IsBusy )
1077
			return false;
1078
		if ( m_bRunningBackground )
1079
			return false;
1080
 
1081
		m_iBackgroundTask = type;
1082
 
1083
		backgroundWorker1->RunWorkerAsync();
1084
		m_bRunningBackground = true;
1085
		return true;
1086
	}
1087
 
121 cycrow 1088
	void MainGui::CloseCurrentDirectory()
1 cycrow 1089
	{
121 cycrow 1090
		if (m_pPackages->IsLoaded())
1 cycrow 1091
		{
121 cycrow 1092
			if (m_pPackages->CloseDir(0, 0, true))
1 cycrow 1093
			{
1094
				// write the modname
121 cycrow 1095
				if (!m_pPackages->GetModKey().Empty())
160 cycrow 1096
					PluginManager::WriteRegistryValue(m_pPackages->GetModKey(), m_pPackages->selectedModName());
1 cycrow 1097
				m_pPackages->Reset();
1098
			}
1099
			else
1100
			{
1101
				this->DisplayMessageBox(true, "Error", "unable to close directory", MessageBoxButtons::OK, MessageBoxIcon::Error);
1102
				return;
1103
			}
1104
		}
1105
 
1106
		m_pPackages->Reset();
121 cycrow 1107
	}
1 cycrow 1108
 
121 cycrow 1109
	void MainGui::ChangeDirectory(const Utils::String &dir)
1110
	{
1111
		if ( m_pPackages->isCurrentDir(dir) )
1112
			return;
1113
 
1114
		CloseCurrentDirectory();
1115
 
1116
		if ( m_pPackages->read(dir, 0) )
1 cycrow 1117
		{
126 cycrow 1118
			//if ( m_iSaveGameManager == 1 )
1119
				//m_pPackages->RestoreSaves();
1 cycrow 1120
			m_pPackages->UpdatePackages();
1121
			m_pPackages->ReadGameLanguage(true);
1122
			System::String ^mod = PluginManager::ReadRegistryValue(m_pPackages->GetModKey());
160 cycrow 1123
			m_pPackages->setMod(_S(mod));
133 cycrow 1124
			if(m_lAvailablePackages)
164 cycrow 1125
				m_lAvailablePackages->clear();
133 cycrow 1126
			this->FindPackages();
1 cycrow 1127
		}
1128
		else
1129
		{
1130
			this->DisplayMessageBox(true, "Error", "unable to open new directory", MessageBoxButtons::OK, MessageBoxIcon::Error);
121 cycrow 1131
			this->CloseCurrentDirectory();
1 cycrow 1132
		}
1133
	}
1134
 
1135
	CBaseFile *MainGui::FindPackageFromList(ListViewItem ^item)
1136
	{
1137
		if ( !item )
1138
			return NULL;
1139
 
1140
		System::String ^sNum = System::Convert::ToString(item->Tag);
1141
		int iNum = CyStringFromSystemString(sNum).ToInt();
1142
 
1143
		CBaseFile *p = m_pPackages->GetPackageAt(iNum);
1144
		return p;
1145
	}
1146
 
1147
	void MainGui::FindPackagesOnline()
1148
	{
162 cycrow 1149
		Utils::CStringList servers;
1150
		m_pPackages->findAllServers(&servers);
1151
		if ( servers.empty() )
1 cycrow 1152
		{
1153
			MessageBox::Show(this, "Found now web address to check for packages", "No Web Address", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1154
			return;
1155
		}
1156
 
1157
		DownloadPackageList ^dpl = gcnew DownloadPackageList(m_pPackages, &servers);
1158
		dpl->ShowDialog(this);
1159
 
1160
		if ( m_pPackages->AnyAvailablePackages() )
161 cycrow 1161
			MessageBox::Show(this, "Package update completed\n" + m_pPackages->getAvailablePackageList()->size() + " packages have been added to the package browser", "Found Packages", MessageBoxButtons::OK, MessageBoxIcon::Information);
1 cycrow 1162
		else
1163
			MessageBox::Show(this, "Unable to find any packages\n", "No Packages Found", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1164
	}
1165
 
1166
	//
1167
	// Event Handlers
1168
	//
1169
	void MainGui::SetupEventHandlers()
1170
	{
1171
		// setup Event Handlers
1172
		ButClose->Click += gcnew EventHandler(this, &PluginManager::MainGui::ClosedEvent);
1173
		ButInstall->Click += gcnew EventHandler(this, &PluginManager::MainGui::InstallEvent);
1174
		ButUninstall->Click += gcnew EventHandler(this, &PluginManager::MainGui::UninstallEvent);
1175
		ButDisable->Click += gcnew EventHandler(this, &PluginManager::MainGui::DisableEvent);
1176
		ListPackages->SelectedIndexChanged += gcnew EventHandler(this, &PluginManager::MainGui::PackageListSelected);
1177
		ListPackages->ColumnClick  += gcnew ColumnClickEventHandler(this, &PluginManager::MainGui::PackageListSort);
1178
		ComboDir->SelectedIndexChanged += gcnew EventHandler(this, &PluginManager::MainGui::ChangeDirectoryEvent);
1179
 
1180
 
1181
		// background worker
1182
		backgroundWorker1->DoWork += gcnew DoWorkEventHandler( this, &MainGui::Background_DoWork );
1183
		backgroundWorker1->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainGui::Background_Finished );
1184
		backgroundWorker1->ProgressChanged += gcnew ProgressChangedEventHandler( this, &MainGui::Background_Progress );
1185
 
1186
		// auto update
1187
		backgroundUpdater->DoWork += gcnew DoWorkEventHandler( this, &MainGui::Updater_DoWork );
1188
		backgroundUpdater->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainGui::Updater_Finished );
1189
	}
1190
 
1191
	void MainGui::PackageListSort(System::Object ^Sender, ColumnClickEventArgs ^E)
1192
	{
1193
		if ( E->Column != m_iSortingColumn )
1194
		{
1195
			m_iSortingColumn = E->Column;
1196
			m_bSortingAsc = true;
1197
		}
1198
		else
1199
			m_bSortingAsc = !m_bSortingAsc;
1200
		this->UpdatePackages();
1201
	}
1202
 
1203
	void MainGui::PackageListSelected(System::Object ^Sender, System::EventArgs ^E)
1204
	{
1205
		// is there any selected items
1206
		this->PictureDisplay->Image = nullptr;
1207
		this->PanelDisplay->Hide();
1208
		TextDesc->Text = "";
1209
		bool buttonEnabled = false;
1210
		if ( ListPackages->SelectedItems->Count )
1211
		{
1212
			buttonEnabled = true;
1213
 
1214
			ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1215
			System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1216
			while ( myEnum->MoveNext() )
1217
			{
1218
				CBaseFile *p = this->FindPackageFromList(safe_cast<ListViewItem ^>(myEnum->Current));
1219
				if ( p )
1220
				{
1221
					if ( p->IsEnabled() )
1222
						ButDisable->Text = "Disable";
1223
					else
1224
						ButDisable->Text = "Enable";
48 cycrow 1225
					if ( !p->description().empty() )	TextDesc->Text = _US(p->description().findReplace("<br>", "\n").stripHtml());
1 cycrow 1226
 
1227
					this->PictureDisplay->Show();
1228
					bool addedIcon = false;
1229
					C_File *picFile = p->GetFirstFile(FILETYPE_ADVERT);
1230
					if ( picFile )
1231
					{
129 cycrow 1232
						System::String ^pic = _US(picFile->filePointer());
1 cycrow 1233
						if ( System::IO::File::Exists(pic) )
1234
						{
1235
							Bitmap ^myBitmap = gcnew Bitmap(pic);
1236
							if ( myBitmap )
1237
							{
1238
								this->PictureDisplay->Image = dynamic_cast<Image ^>(myBitmap);
1239
								addedIcon = true;
1240
							}
1241
						}
1242
					}
1243
 
1244
					if ( !addedIcon )
1245
					{
1246
 
1247
						System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainGui::typeid));
1248
						this->PictureDisplay->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"PictureDisplay.Image")));
1249
					}
1250
 
1251
					if ( p->GetType() != TYPE_ARCHIVE )
1252
						this->PanelDisplay->Show();
1253
				}	
1254
			}				
1255
		}
1256
 
1257
		// enable/disable the buttons connected to the package list
1258
		ButUninstall->Enabled = buttonEnabled;
1259
		ButDisable->Enabled = buttonEnabled;
1260
	}
1261
 
1262
	bool MainGui::EnablePackage(CBaseFile *p)
1263
	{
1264
		if ( !m_pPackages->PrepareEnablePackage(p) )
1265
		{
1266
			if ( m_pPackages->GetError() == PKERR_NOPARENT )
1267
				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);
1268
			else if ( m_pPackages->GetError() == PKERR_MODIFIED )
1269
				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);
1270
			else if ( m_pPackages->GetError() == PKERR_MISSINGDEP )
1271
			{
133 cycrow 1272
				Utils::CStringList depList;
1 cycrow 1273
				m_pPackages->GetMissingDependacies(p, &depList, true);
133 cycrow 1274
				Utils::String sDep;
1275
				for(auto itr = depList.begin(); itr != depList.end(); itr++)
1 cycrow 1276
				{
133 cycrow 1277
					if ( (*itr)->str.contains("|") )
1278
						sDep = (*itr)->str.token("|", 1) + " V" + (*itr)->str.token("|", 2) + " by " + (*itr)->data + "\n";
1 cycrow 1279
					else
133 cycrow 1280
						sDep = (*itr)->str + " by " + (*itr)->data + "\n";
1 cycrow 1281
				}
133 cycrow 1282
				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 1283
			}
1284
			else
1285
				this->DisplayMessageBox(false, "Enable Error", "Error enabling package\n" + SystemStringFromCyString(p->GetFullPackageName(m_pPackages->GetLanguage())) + "\n\nUnknown Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1286
			return false;
1287
		}
1288
		return true;
1289
	}
1290
 
1291
	void MainGui::DisableList(ArrayList ^List)
1292
	{
1293
		bool skipShips = false;
1294
		int count = 0;
1295
 
1296
		for ( int i = 0; i < List->Count; i++ )
1297
		{
1298
			CBaseFile *p = this->FindPackageFromList(cli::safe_cast<ListViewItem ^>(List[i]));
1299
			if ( p )
1300
			{
1301
				if ( p->IsEnabled() )
1302
				{
1303
					if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
1304
					{
1305
						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 )
1306
							continue;
1307
					}
1308
					if ( p->GetType() == TYPE_XSP )
1309
					{
1310
						if ( !this->DisplayTip(TIPSECTION_YESNO, TIP_SHIPDISABLE) )
1311
							skipShips = true;
1312
 
1313
						if ( skipShips )
1314
							continue;
1315
					}
1316
 
1317
					m_pPackages->PrepareDisablePackage(p);
1318
					++count;
1319
				}
1320
				else
1321
				{
1322
					this->EnablePackage(p);
1323
					++count;
1324
				}
1325
			}
1326
		}
1327
 
1328
		if ( count )
1329
		{
1330
			this->Enabled = false;
1331
			this->StartBackground(MGUI_BACKGROUND_DISABLE);
1332
		}
1333
	}
1334
 
1335
	void MainGui::DisableEvent(System::Object ^Sender, System::EventArgs ^E)
1336
	{
1337
		if ( !ListPackages->SelectedItems->Count )
1338
			return;
1339
 
1340
		bool skipShips = false;
1341
 
1342
		ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1343
		System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1344
		int count = 0;
1345
		ArrayList ^List = gcnew ArrayList();
1346
 
1347
		while ( myEnum->MoveNext() )
1348
			List->Add(safe_cast<ListViewItem ^>(myEnum->Current));
1349
 
1350
		if ( List->Count )
1351
			this->DisableList(List);
1352
	}
1353
 
1354
	void MainGui::PackageBrowserEvent(System::Object ^Sender, System::EventArgs ^E)
1355
	{
1356
		this->Enabled = false;
163 cycrow 1357
		PackageBrowser ^mod = gcnew PackageBrowser(m_pPackages, m_lAvailablePackages, this->imageList1, this);
1 cycrow 1358
		if ( !mod->AnyPackages() )
1359
		{
121 cycrow 1360
			System::String ^game = _US(m_pPackages->getGameName());
1 cycrow 1361
			this->DisplayMessageBox(false, "No Available Packages", "No available packages found for " + game, MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1362
 
1363
			this->Enabled = true;
1364
			return;
1365
		}
1366
 
1367
		if ( m_bDirLocked ) {
1368
			this->DisplayLocked(false);
1369
			this->Enabled = true;
1370
			return;
1371
		}
1372
 
1373
		mod->SetExperimental(m_bExperimental);
1374
		mod->SetCheat(m_bCheat);
1375
		mod->SetShips(m_bShips);
1376
		if ( m_pPackages->IsVanilla() )
1377
			mod->SetSigned(true);
1378
		else
1379
			mod->SetSigned(m_bSigned);
1380
		mod->SetDownload(m_bDownloadable);
1381
		mod->UpdatePackages();
1382
		System::Windows::Forms::DialogResult result = mod->ShowDialog(this);
1383
 
1384
		m_bDownloadable = mod->IsDownload();
1385
		m_bCheat = mod->IsCheat();
1386
		m_bExperimental = mod->IsExperimental();
1387
		m_bShips = mod->IsShips();
1388
		m_bSigned = mod->IsSigned();
1389
 
1390
		bool doenable = true;
1391
		CBaseFile *p = mod->SelectedMod();
1392
		if ( result == System::Windows::Forms::DialogResult::OK )
1393
		{
1394
			if ( p )
1395
			{
50 cycrow 1396
				if ( this->InstallPackage(_US(p->filename()), true, false, true) )
1 cycrow 1397
					doenable = false;
1398
			}
1399
		}
1400
		else if ( result == System::Windows::Forms::DialogResult::Yes )
1401
			this->StartInstalling(false, true);
1402
 
1403
		this->Enabled = doenable;
1404
	}
1405
 
1406
	void MainGui::CloseEvent(System::Object ^Sender, FormClosingEventArgs ^E)
1407
	{
1408
		int h = this->Size.Height;
1409
		int w = this->Size.Width;
1410
	}
1411
 
1412
	void MainGui::DisplayLocked(bool inthread) 
1413
	{
1414
		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);
1415
	}
1416
 
126 cycrow 1417
	void MainGui::OpenModSelecter()
1 cycrow 1418
	{
126 cycrow 1419
		if (m_pPackages->IsVanilla())
1 cycrow 1420
		{
1421
			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);
1422
			return;
1423
		}
1424
		this->Enabled = false;
1425
		ModSelector ^mod = gcnew ModSelector(m_pPackages, this->imageList1);
126 cycrow 1426
 
1427
		if (!mod->AnyPackages())
1 cycrow 1428
		{
1429
			this->DisplayMessageBox(false, "Mod Selector", "No available mods have been found", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1430
			this->Enabled = true;
1431
			return;
1432
		}
1433
 
126 cycrow 1434
		if (m_bDirLocked) {
1 cycrow 1435
			this->DisplayLocked(false);
1436
			this->Enabled = true;
1437
			return;
1438
		}
1439
 
126 cycrow 1440
		if (!m_bModSelectorDetails)
1 cycrow 1441
		{
1442
			mod->HideDetails();
1443
			mod->Update();
1444
		}
126 cycrow 1445
 
1 cycrow 1446
		System::Windows::Forms::DialogResult result = mod->ShowDialog(this);
1447
		m_bModSelectorDetails = mod->ShowingDetails();
1448
 
1449
		// install the selected mod
1450
		bool reEnable = true;
1451
 
1452
		CBaseFile *p = mod->SelectedMod();
126 cycrow 1453
		if (result == System::Windows::Forms::DialogResult::OK)
1 cycrow 1454
		{
126 cycrow 1455
			if (p)
1 cycrow 1456
			{
1457
				// from file
126 cycrow 1458
				if (p->GetNum() < 0)
1 cycrow 1459
				{
126 cycrow 1460
					if (m_pPackages->GetEnabledMod())
1 cycrow 1461
						m_pPackages->DisablePackage(m_pPackages->GetEnabledMod(), 0, 0);
126 cycrow 1462
					if (this->InstallPackage(_US(p->filename()), true, false, true))
1 cycrow 1463
						reEnable = false;
1464
				}
1465
				// otherwise just enable it
1466
				else
1467
				{
126 cycrow 1468
					if (this->EnablePackage(p))
1 cycrow 1469
					{
1470
						this->StartBackground(MGUI_BACKGROUND_DISABLE);
1471
						reEnable = false;
1472
					}
1473
				}
1474
			}
1475
		}
1476
 
1477
		// install downloaded mods
126 cycrow 1478
		else if (result == Windows::Forms::DialogResult::Yes)
1 cycrow 1479
			this->StartInstalling(false, true);
1480
 
1481
		// remove the current mod
126 cycrow 1482
		else if (result == System::Windows::Forms::DialogResult::Abort)
1 cycrow 1483
		{
126 cycrow 1484
			if (m_pPackages->GetEnabledMod())
1 cycrow 1485
			{
1486
				CBaseFile *mod = m_pPackages->GetEnabledMod();
1487
				CyString message = mod->GetFullPackageName(m_pPackages->GetLanguage());
126 cycrow 1488
				m_pPackages->DisablePackage(m_pPackages->GetEnabledMod(), 0, 0);
1 cycrow 1489
				this->DisplayMessageBox(false, "Mod Disabed", SystemStringFromCyString(message) + " has been disabled\nYour game is no longer using any mods\n", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
1490
			}
1491
		}
1492
 
1493
		// uninstall the selected mod
126 cycrow 1494
		else if (result == System::Windows::Forms::DialogResult::Retry)
1 cycrow 1495
		{
126 cycrow 1496
			if (p && p->GetNum() >= 0)
1 cycrow 1497
			{
1498
				m_pPackages->PrepareUninstallPackage(p);
126 cycrow 1499
				if (m_pPackages->GetNumPackagesInQueue())
1 cycrow 1500
				{
1501
					reEnable = false;
1502
					this->StartBackground(MGUI_BACKGROUND_UNINSTALL);
1503
				}
1504
			}
1505
		}
1506
 
126 cycrow 1507
		if (reEnable)
1 cycrow 1508
			this->Enabled = true;
1509
 
1510
		mod->RemovePackages();
126 cycrow 1511
		this->UpdatePackages();
1 cycrow 1512
	}
1513
 
126 cycrow 1514
	void MainGui::ModSelectorEvent(System::Object ^Sender, System::EventArgs ^E)
1515
	{
1516
		OpenModSelecter();
1517
	}
1518
 
1 cycrow 1519
	void MainGui::UninstallList(ArrayList ^List)
1520
	{
1521
		bool skipShips = false;
1522
 
1523
		for ( int i = 0; i < List->Count; i++ )
1524
		{
1525
			CBaseFile *p = this->FindPackageFromList(safe_cast<ListViewItem ^>(List[i]));
1526
			if ( p )
1527
			{
1528
				if ( p->GetType() == TYPE_XSP )
1529
				{
1530
					if ( !this->DisplayTip(TIPSECTION_YESNO, TIP_SHIPUNINSTALL) )
1531
						skipShips = true;
1532
 
1533
					if ( skipShips )
1534
						continue;
1535
				}
1536
 
46 cycrow 1537
				// display uninstall text
1538
				Utils::String beforeText = m_pPackages->GetUninstallBeforeText(p).ToString();
1539
				if ( !beforeText.empty() ) {
1540
					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 )
1541
						continue;
1542
				}
1543
 
1 cycrow 1544
				m_pPackages->PrepareUninstallPackage(p);
1545
			}
1546
		}
1547
 
1548
		if ( m_pPackages->GetNumPackagesInQueue() )
1549
		{
1550
			this->Enabled = false;
1551
			this->StartBackground(MGUI_BACKGROUND_UNINSTALL);
1552
		}
1553
	}
1554
	void MainGui::UninstallEvent(System::Object ^Sender, System::EventArgs ^E)
1555
	{
1556
		if ( !ListPackages->SelectedItems->Count )
1557
			return;
1558
 
1559
		ArrayList ^List = gcnew ArrayList();
1560
 
1561
		ListView::SelectedListViewItemCollection^ selected = this->ListPackages->SelectedItems;
1562
		System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
1563
 
1564
		while ( myEnum->MoveNext() )
1565
			List->Add(safe_cast<ListViewItem ^>(myEnum->Current));
1566
 
1567
		this->UninstallList(List);
1568
	}
1569
 
1570
	void MainGui::ModifiedEvent(System::Object ^Sender, System::EventArgs ^E)
1571
	{
1572
		if ( m_bDirLocked ) {
1573
			this->DisplayLocked(false);
1574
			return;
1575
		}
1576
		if ( m_pPackages->IsVanilla() )
1577
		{
1578
			this->Enabled = false;
1579
			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 )
1580
			{
1581
				if ( m_iSaveGameManager == 1 )
1582
				{
126 cycrow 1583
					m_pPackages->backupSaves(true);
1584
					m_pPackages->restoreSaves(false);
1 cycrow 1585
				}
1586
				m_pPackages->SetVanilla(false);
1587
				m_pMenuBar->Modified();
1588
				m_pPackages->PrepareEnableLibrarys();
1589
				m_pPackages->PrepareEnableFromVanilla();
1590
				this->StartBackground(MGUI_BACKGROUND_DISABLE);
1591
			}
1592
			else
1593
				this->Enabled = true;
1594
		}
1595
	}
1596
 
1597
	void MainGui::VanillaEvent(System::Object ^Sender, System::EventArgs ^E)
1598
	{
1599
		if ( m_bDirLocked ) {
1600
			this->DisplayLocked(false);
1601
			return;
1602
		}
1603
		if ( !m_pPackages->IsVanilla() )
1604
		{
1605
			this->Enabled = false;
1606
			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 )
1607
			{
1608
				if ( m_iSaveGameManager == 1 )
1609
				{
126 cycrow 1610
					m_pPackages->backupSaves(false);
1611
					m_pPackages->restoreSaves(true);
1 cycrow 1612
				}
1613
				m_pPackages->SetVanilla(true);
1614
				m_pMenuBar->Vanilla();
1615
				m_pPackages->PrepareDisableForVanilla();
1616
				this->StartBackground(MGUI_BACKGROUND_DISABLE);
1617
			}
1618
			else
1619
				this->Enabled = true;
1620
		}
1621
	}
1622
 
1623
	void MainGui::InstallEvent(System::Object ^Sender, System::EventArgs ^E)
1624
	{
1625
		if ( m_bDirLocked ) {
1626
			this->DisplayLocked(false);
1627
			return;
1628
		}
1629
 
1630
		OpenFileDialog ^ofd = gcnew OpenFileDialog();
1631
		ofd->Filter = "All (*.spk, *.xsp)|*.spk;*.xsp|Package Files (*.spk)|*.spk|Ship Files (*.xsp)|*.xsp";
1632
		ofd->FilterIndex = 1;
1633
		ofd->RestoreDirectory = true;
1634
		ofd->Multiselect = true;
1635
 
1636
		this->Enabled = false;
1637
		if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
1638
		{
1639
			bool anytoinstall = false;
1640
			array<System::String ^> ^fileArray = ofd->FileNames;
1641
			for ( int i = 0; i < fileArray->Length; i++ )
1642
			{
1643
				System::String ^file = fileArray[i];
1644
				if ( this->InstallPackage(file, false, false, true) )
1645
					anytoinstall = true;
1646
			}
1647
 
1648
			if ( anytoinstall )
1649
				this->StartInstalling(false, true);
1650
		}
1651
		else
1652
		{
1653
			ProgressBar->Hide();
1654
			this->Enabled = true;
1655
		}
1656
	}
1657
 
1658
	void MainGui::CheckUnusedShared()
1659
	{
1660
		if ( m_pPackages->AnyUnusedShared() )
1661
		{
1662
			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)
1663
				m_pPackages->RemoveUnusedSharedFiles();
1664
		}
1665
	}
1666
 
1667
	void MainGui::ChangeDirectoryEvent(System::Object ^Sender, System::EventArgs ^E)
1668
	{
121 cycrow 1669
		if (ComboDir->SelectedIndex == (ComboDir->Items->Count - 1))
1 cycrow 1670
		{
121 cycrow 1671
			if (m_pPackages && m_pPackages->IsLoaded())
1672
			{
1673
				this->Enabled = false;
1674
				ProgressBar->Show();
1675
				this->CheckUnusedShared();
1676
				this->StartBackground(MGUI_BACKGROUND_CLOSEDIR, "");
1677
			}
1 cycrow 1678
		}
121 cycrow 1679
		else if (ComboDir->SelectedIndex >= 0)
1680
		{
1681
			Utils::String dir = m_pDirList->get(ComboDir->SelectedIndex)->str;
1682
			if (!m_pPackages->isCurrentDir(dir))
1683
			{
1684
				this->Enabled = false;
1685
				ProgressBar->Show();
1686
				this->CheckUnusedShared();
1687
				this->StartBackground(MGUI_BACKGROUND_CHANGEDIR, _US(dir));
1688
			}
1689
		}
1 cycrow 1690
	}
1691
 
1692
	void MainGui::Background_DoWork(System::Object ^Sender, DoWorkEventArgs ^E)
1693
	{
1694
		m_bDisplayMessage = false;
1695
		m_bDisplayDialog = false;
1696
 
1697
		switch ( m_iBackgroundTask )
1698
		{
1699
			case MGUI_BACKGROUND_INSTALL:
1700
				this->DoInstall(false, true);
1701
				break;
1702
			case MGUI_BACKGROUND_INSTALLBUILTIN:
1703
				this->DoInstall(true, true);
1704
				break;
1705
			case MGUI_BACKGROUND_UNINSTALL:
1706
				this->DoUninstall();
1707
				break;
1708
			case MGUI_BACKGROUND_DISABLE:
1709
				this->DoDisable();
1710
				break;
121 cycrow 1711
			case MGUI_BACKGROUND_CLOSEDIR:
1712
				this->CloseCurrentDirectory();
1713
				break;
1 cycrow 1714
			case MGUI_BACKGROUND_CHANGEDIR:
121 cycrow 1715
				this->ChangeDirectory(_S(m_sBackgroundInfo));
1 cycrow 1716
				break;
1717
		}
1718
	}
1719
 
1720
	void MainGui::Background_Finished()
1721
	{
1722
		ProgressBar->Hide();
1723
 
1724
		if ( m_bDisplayMessage )
1725
			MessageBox::Show(this, m_sMessageText, m_sMessageTitle, m_messageButtons, m_messageIcon);
1726
 
1727
		if ( m_bDisplayDialog )
1728
		{
1729
			if ( m_pPi->PackageCount() )
1730
			{
1731
				m_pPi->AdjustColumns();
1732
				m_pPi->ShowDialog(this);
1733
			}
1734
		}
1735
 
1736
		m_bDisplayDialog = false;
1737
		m_bDisplayMessage = false;
1738
 
121 cycrow 1739
		if ( m_iBackgroundTask == MGUI_BACKGROUND_CHANGEDIR || m_iBackgroundTask == MGUI_BACKGROUND_CLOSEDIR)
1 cycrow 1740
		{
1741
			// switch the dir list
121 cycrow 1742
			Utils::String selectedDir;
1743
			if (ComboDir->SelectedIndex == ComboDir->Items->Count - 1)
1744
				selectedDir = _S(ComboDir->Text);
1745
			else if (ComboDir->SelectedIndex >= 0)
1 cycrow 1746
			{
121 cycrow 1747
				Utils::String dir = m_pDirList->get(ComboDir->SelectedIndex)->str;
1748
				selectedDir = dir;
1749
				if ( !dir.empty() )
1 cycrow 1750
				{
121 cycrow 1751
					if ( (m_iBackgroundTask == MGUI_BACKGROUND_CHANGEDIR) && (dir.countToken(" [")) )
1752
						dir = dir.tokens(" [", 1);
1753
					if ( m_pDirList->findString(dir) )
1 cycrow 1754
					{
121 cycrow 1755
						Utils::String data = m_pDirList->findString(dir);
1756
						m_pDirList->remove(dir, false);
1757
						m_pDirList->pushFront(dir, data);
1 cycrow 1758
					}
1759
					else
1760
					{
1761
						int lang = m_pPackages->GetGameLanguage(dir);
1762
						if ( lang )
121 cycrow 1763
							m_pDirList->pushFront(dir, Utils::String::Number(lang) + "|" + m_pPackages->getGameName(dir));
1 cycrow 1764
						else
121 cycrow 1765
							m_pDirList->pushFront(dir, m_pPackages->getGameName(dir));
1 cycrow 1766
					}
1767
				}
1768
			}
121 cycrow 1769
 
1770
			this->UpdateDirList(selectedDir);
1771
			this->UpdateRunButton();
1 cycrow 1772
		}
1773
 
1774
		// display any files that failed
1775
		if ( m_iBackgroundTask == MGUI_BACKGROUND_INSTALL )
1776
		{
1777
			String ^files = "";
1778
			for ( SStringList *str = m_pFileErrors->Head(); str; str = str->next )
1779
			{
1780
				if ( str->data.ToInt() == SPKINSTALL_WRITEFILE_FAIL )
1781
				{
1782
					files += "\n";
1783
					files += SystemStringFromCyString(str->str);
1784
				}
1785
			}
1786
 
1787
			if ( files->Length )
1788
				MessageBox::Show(this, "These files failed to install\n" + files, "Failed Files", MessageBoxButtons::OK, MessageBoxIcon::Warning);
1789
		}
1790
 
1791
		switch ( m_iBackgroundTask )
1792
		{
121 cycrow 1793
			case MGUI_BACKGROUND_CLOSEDIR:
1 cycrow 1794
			case MGUI_BACKGROUND_CHANGEDIR:
1795
				this->UpdateControls();
1796
				this->UpdatePackages();
1797
				this->CheckProtectedDir();
1798
				m_bRunningBackground = false;
1799
				if ( this->UpdateBuiltInPackages() )
1800
					return;
1801
				break;
1802
 
1803
			case MGUI_BACKGROUND_INSTALL:
1804
			case MGUI_BACKGROUND_INSTALLBUILTIN:
1805
			case MGUI_BACKGROUND_UNINSTALL:
1806
			case MGUI_BACKGROUND_DISABLE:
1807
				this->UpdatePackages();
1808
				break;
1809
		}
1810
 
1811
		m_iBackgroundTask = MGUI_BACKGROUND_NONE;
1812
 
1813
		this->Enabled = true;
121 cycrow 1814
		ProgressBar->Hide();
1 cycrow 1815
		m_bRunningBackground = false;
1816
	}
1817
 
1818
	void MainGui::Background_Progress(System::Object ^Sender, ProgressChangedEventArgs ^E)
1819
	{
1820
		this->ProgressBar->Value = E->ProgressPercentage;
1821
	}
1822
 
1823
	bool MainGui::UpdateBuiltInPackages()
1824
	{
121 cycrow 1825
		// no current directory
1826
		if (!m_pPackages || !m_pPackages->IsLoaded())
1827
			return false;
1828
 
1 cycrow 1829
		// find all built-in packages
53 cycrow 1830
		System::String ^dir = ".\\Required";
78 cycrow 1831
 
53 cycrow 1832
		if ( System::IO::Directory::Exists(dir) ) 
1 cycrow 1833
		{
1834
			bool installing = false;
53 cycrow 1835
			array <System::String ^> ^Files = System::IO::Directory::GetFiles(dir, "*.spk");
1 cycrow 1836
 
1837
			for ( int i = 0; i < Files->Length; i++ )
1838
			{
1839
				CyString file = CyStringFromSystemString(Files[i]);
1840
				int error;
1841
				CBaseFile *p = m_pPackages->OpenPackage(file, &error, 0, SPKREAD_NODATA);
1842
				if ( !p )
1843
					continue;
1844
 
1845
				if ( !((CSpkFile *)p)->IsLibrary() )
1846
					continue;
1847
 
1848
				if ( !p->CheckGameCompatability(m_pPackages->GetGame()) )
1849
					continue;
1850
 
1851
				// if its installed, check if we have a newer version
50 cycrow 1852
				CBaseFile *check = m_pPackages->FindSpkPackage(p->name(), p->author());
1 cycrow 1853
				if ( check )
1854
				{
50 cycrow 1855
					if ( check->version().compareVersion(p->version()) != COMPARE_OLDER )
1 cycrow 1856
					{
1857
						this->InstallPackage(Files[i], false, true, true);
1858
						installing = true;
1859
					}
1860
				}
1861
				else
1862
				{
1863
					this->InstallPackage(Files[i], false, true, true);
1864
					installing = true;
1865
				}
1866
 
1867
				delete p;
1868
			}
1869
 
1870
			if ( installing )
1871
				this->StartInstalling(true, true);
1872
			return installing;
1873
		}
1874
 
1875
		return false;
1876
	}
1877
 
1878
	////
1879
	// Auto Update
1880
	////
1881
	void MainGui::AutoUpdate()
1882
	{
1883
		if ( !m_bAutoUpdate || !System::IO::File::Exists( ".\\AutoUpdater.exe") )
1884
			return;
1885
 
1886
		// load the dir list
1887
		if ( !m_pUpdateList )
1888
		{
1889
			m_pUpdateList = new CyStringList;
1890
 
1891
			// TODO: read addresses from data
1892
 
1893
			// hardcoded address
80 cycrow 1894
			m_pUpdateList->PushBack("http://xpluginmanager.co.uk/pmupdate.dat", "", true);
1 cycrow 1895
			if ( (int)PMLBETA )
80 cycrow 1896
				m_pUpdateList->PushBack("http://xpluginmanager/Beta/pmupdatebeta.dat", "", true);
1 cycrow 1897
		}
1898
 
1899
		backgroundUpdater->RunWorkerAsync();
1900
	}
1901
 
1902
	void MainGui::Updater_Finished(System::Object ^Sender, RunWorkerCompletedEventArgs ^E)
1903
	{
1904
		if ( !m_pUpdateList )
1905
			return;
1906
		if ( !m_pUpdateList->Head() )
1907
		{
1908
			delete m_pUpdateList;
1909
			m_pUpdateList = NULL;
1910
			return;
1911
		}
1912
 
1913
		this->Enabled = false;
1914
 
1915
		CyString server = m_pUpdateList->Head()->str;
1916
		CyString data = m_pUpdateList->Head()->data;
1917
 
1918
		m_pUpdateList->PopFront();
1919
 
1920
		// lets check if we have an update
1921
		if ( data.GetToken(" ", 1, 1) != "!ERROR!" )
1922
		{
1923
			CyString download;
1924
			CyString message;
1925
			int max;
1926
			CyString *strs = data.SplitToken("\n", &max);
1927
			if ( strs )
1928
			{
1929
				for ( int i = 0; i < max; i++ )
1930
				{
1931
					CyString cmd = strs[i].GetToken(":", 1, 1);
1932
					CyString rest = strs[i].GetToken(":", 2);
1933
					rest.RemoveFirstSpace();
1934
					if ( cmd.Compare("SPKVERSION") )
1935
					{
1936
						float v = rest.GetToken(" ", 1, 1).ToFloat();
1937
						if ( v > GetLibraryVersion() )
1938
						{
1939
							message += "New version of the SPK Libraries available\nCurrent = ";
1940
							message += CyString::CreateFromFloat(GetLibraryVersion(), 2);
1941
							message += "\nNew Version = ";
1942
							message += CyString::CreateFromFloat(v, 2);
1943
							message += "\n\n";
1944
 
1945
							CyString filename = rest.GetToken(" ", 2);
1946
							if ( download.Empty() )
1947
								download = filename;
1948
							else
1949
							{
1950
								download += "|";
1951
								download += filename;
1952
							}
1953
						}
1954
					}
1955
					else if ( cmd.Compare("PMLVERSION") )
1956
					{
1957
						float v = rest.GetToken(" ", 1, 1).ToFloat();
1958
						int beta = rest.GetToken(" ", 2, 2).ToInt();
1959
 
1960
						bool newVersion = false;
1961
						// new version
1962
						if ( v > (float)PMLVERSION )
1963
							newVersion = true;
1964
						// same version, check beta/rc
1965
						if ( v == (float)PMLVERSION )
1966
						{
1967
							// newer beta version
1968
							if ( beta > (int)PMLBETA && (int)PMLBETA > 0 )
1969
								newVersion = true;
1970
							// current is beta, new is an RC
1971
							else if ( (int)PMLBETA > 0 && beta < 0 )
1972
								newVersion = true;
1973
							// current is rc, new is an rc
1974
							else if ( (int)PMLBETA < 0 && beta < 0 && beta < (int)PMLBETA )
1975
								newVersion = true;
1976
							// current is beta or rc, new is not, so its newer
1977
							else if ( (int)PMLBETA != 0 && beta == 0 )
1978
								newVersion = true;
1979
						}
1980
 
1981
						if ( newVersion )
1982
						{
1983
							message += "New version of the ";
1984
							message += CyStringFromSystemString(GetProgramName(m_bAdvanced));
1985
							message += " available\nCurrent = ";
1986
							message += CyStringFromSystemString(PluginManager::GetVersionString());
1987
							message += "\nNew Version = ";
1988
							message += CyStringFromSystemString(PluginManager::GetVersionString(v, beta));
1989
							message += "\n\n";
1990
							if ( download.Empty() )
1991
								download = rest.GetToken(" ", 3);
1992
							else
1993
							{
1994
								download += "|";
1995
								download += rest.GetToken(" ", 3);
1996
							}
1997
						}
1998
					}
1999
				}
2000
			}
2001
 
2002
			CLEANSPLIT(strs, max)
2003
 
2004
			if ( !download.Empty() && !message.Empty() )
2005
			{
2006
				if ( this->DisplayMessageBox(false, "Updater", SystemStringFromCyString(message) + "Do You wish to download and install it?", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
2007
				{
2008
					// absolute address
2009
					CyString downloadFile;
2010
 
2011
					int max;
2012
					CyString *strs = download.SplitToken("|", &max);
2013
					for ( int i = 0; i < max; i++ )
2014
					{
2015
						CyString d = strs[i];
2016
						// relative address
2017
						if ( !d.Left(7).Compare("http://") && !d.Left(4).Compare("www.") )
2018
							d = server.DelToken("/", server.NumToken("/")) + "/" + d;
2019
 
2020
						if ( downloadFile.Empty() )
2021
							downloadFile = d;
2022
						else
2023
						{
2024
							downloadFile += "|";
2025
							downloadFile += d;
2026
						}
2027
					}
2028
 
2029
					CLEANSPLIT(strs, max);
2030
 
2031
					if ( !downloadFile.Empty() )
2032
					{
2033
						m_sDownload = SystemStringFromCyString(downloadFile);
2034
						this->Close();
2035
						return;
2036
					}
2037
				}
2038
			}
2039
		}
2040
 
2041
		// otherwise, lets continue with the next server
2042
		if ( m_pUpdateList->Head() )
2043
			backgroundUpdater->RunWorkerAsync();
2044
		else
2045
		{
2046
			delete m_pUpdateList;
2047
			m_pUpdateList = NULL;
2048
		}
2049
 
2050
		this->Enabled = true;
2051
	}
2052
 
2053
	void MainGui::TimerEvent_CheckFile(System::Object ^Sender, System::EventArgs ^E)
2054
	{
2055
		if ( m_bRunningBackground )
2056
			return;
2057
 
2058
		System::String ^mydoc = Environment::GetFolderPath(Environment::SpecialFolder::Personal );
2059
 
2060
		bool anytoinstall = false;
2061
 
2062
		if ( System::IO::File::Exists(mydoc + "\\Egosoft\\pluginmanager_load.dat") )
2063
		{
2064
			System::String ^lines = System::IO::File::ReadAllText(mydoc + "\\Egosoft\\pluginmanager_load.dat");
2065
			System::IO::File::Delete(mydoc + "\\Egosoft\\pluginmanager_load.dat");
2066
			if ( lines )
2067
			{
2068
				CyString strLines = CyStringFromSystemString(lines);
2069
				int num;
2070
				CyString *aLines = strLines.SplitToken("\n", &num);
2071
				if ( num && aLines )
2072
				{
2073
					for ( int i = 0; i < num; i++ )
2074
					{
2075
						CyString l = aLines[i];
2076
						l = l.Remove("\r");
2077
						CyString first = l.GetToken(":", 1, 1);
2078
						CyString rest = l.GetToken(":", 2);
2079
						rest.RemoveFirstSpace();
2080
 
2081
						if ( first.Compare("File") )
2082
						{
2083
							if ( m_bDirLocked ) {
2084
								this->DisplayLocked(false);
2085
								return;
2086
							}
2087
							if ( this->InstallPackage(SystemStringFromCyString(rest), false, false, true) )
2088
								anytoinstall = true;
2089
						}
2090
					}
2091
 
2092
					CLEANSPLIT(aLines, num);
2093
				}
2094
			}
2095
		}
2096
 
2097
		if ( anytoinstall )
2098
			this->StartInstalling(false, true);
2099
	}
2100
 
2101
	void MainGui::Updater_DoWork(System::Object ^Sender, DoWorkEventArgs ^E)
2102
	{
2103
		if ( !m_pUpdateList )
2104
			return;
2105
		if ( !m_pUpdateList->Head() )
2106
		{
2107
			delete m_pUpdateList;
2108
			m_pUpdateList = NULL;
2109
			return;
2110
		}
2111
 
2112
		try 
2113
		{
2114
			System::Net::WebClient ^Client = gcnew System::Net::WebClient();
2115
 
2116
			System::IO::Stream ^strm = Client->OpenRead(SystemStringFromCyString(m_pUpdateList->Head()->str));
2117
			System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
2118
			System::String ^read = sr->ReadToEnd();
2119
			strm->Close();
2120
			sr->Close();
2121
 
2122
			m_pUpdateList->Head()->data = CyStringFromSystemString(read);
2123
		}
2124
		catch (System::Net::WebException ^ex)
2125
		{
2126
			m_pUpdateList->Head()->data = CyStringFromSystemString("!ERROR! " + ex->ToString());
2127
			if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
2128
			{
2129
				m_pUpdateList->Head()->data = CyStringFromSystemString("!ERROR! " + ex->ToString());
2130
 
2131
			}
2132
		}
2133
	}
2134
 
2135
	void MainGui::LaunchGame()
2136
	{
2137
		if ( !System::IO::File::Exists(".\\GameLauncher.exe") )
2138
			return;
2139
 
121 cycrow 2140
		m_sRun = _US(m_pPackages->GetGameRunExe());
1 cycrow 2141
		this->Close();
2142
	}
2143
 
2144
	bool MainGui::DisplayTip(int tipsection, int tip)
2145
	{
2146
		if ( tipsection < 0 || tipsection >= MAXTIPS )
2147
			return false;
2148
 
2149
		STips ^tips = (STips ^)m_lTips[tipsection];
2150
		if ( !(tips->iTips & tip) )
2151
		{
2152
			System::String ^sTip = cli::safe_cast<System::String ^>(tips->sTips[(tip >> 1)]);
2153
 
2154
			tips->iTips |= tip;
2155
 
2156
			if ( this->DisplayMessageBox(false, "Plugin Manager Tip", sTip, MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes )
2157
				return true;
2158
			else 
2159
				return false;
2160
		}
2161
 
2162
		return true;
2163
	}
2164
 
2165
	void MainGui::SetTipStrings(int section)
2166
	{
2167
		STips ^t = (STips ^)m_lTips[section];
2168
		t->sTips = gcnew ArrayList();
2169
 
2170
		switch ( section )
2171
		{
2172
			case TIPSECTION_YESNO:
2173
				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?");
2174
				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?");
2175
				break;
2176
		}
2177
	}
2178
 
126 cycrow 2179
	void MainGui::SetSaveGameManager(int i) 
2180
	{ 
2181
		m_iSaveGameManager = i; 
2182
		if (m_iSaveGameManager != -1)
2183
		{
2184
			if (m_pPackages && m_pPackages->IsLoaded())
2185
				m_pPackages->setSaveGameManager(m_iSaveGameManager == 1);
2186
		}
2187
	}
2188
 
2189
 
1 cycrow 2190
	System::Windows::Forms::DialogResult MainGui::DisplayMessageBox(bool inthread, System::String ^title, System::String ^text, MessageBoxButtons buttons, MessageBoxIcon icon)
2191
	{
2192
		if ( !inthread )
2193
			return MessageBox::Show(this, text, title, buttons, icon);
2194
		else
2195
		{
2196
			m_bDisplayMessage = true;
2197
			m_sMessageText = text;
2198
			m_sMessageTitle = title;
2199
			m_messageIcon = icon;
2200
			m_messageButtons = buttons;
2201
 
2202
			return System::Windows::Forms::DialogResult::Abort;
2203
		}
2204
	}
2205
 
2206
	ListViewItem ^MainGui::FindSelectedItem()
2207
	{
2208
		Point ^mousePoint = this->ListPackages->PointToClient(this->contextMenuStrip1->MousePosition);
2209
		return  this->ListPackages->GetItemAt(mousePoint->X, mousePoint->Y);
2210
	}
2211
 
2212
	CBaseFile *MainGui::GetFileFromItem(ListViewItem ^item)
2213
	{
2214
		int num = System::Convert::ToInt32(item->Tag);
2215
		return m_pPackages->GetPackageAt(num);
2216
	}
2217
 
2218
	System::Void MainGui::OpenContextMenu(System::Object ^Sender, CancelEventArgs ^E)
2219
	{
2220
		m_pListItem = nullptr;
2221
		E->Cancel = true;
2222
		bool showSep = false;
2223
		bool showSep2 = false;
2224
 
2225
		ListViewItem ^item = this->FindSelectedItem();
2226
		CBaseFile *p = NULL;
2227
		if ( item )
2228
				p = this->GetFileFromItem(item);
2229
 
2230
		this->emailAuthorToolStripMenuItem->Visible = false;
2231
		this->visitForumPageToolStripMenuItem->Visible = false;
2232
		this->visitWebSiteToolStripMenuItem->Visible = false;
2233
		this->ContextDisable->Visible = false;
2234
		this->ContextEnable->Visible = false;
2235
		this->ContextName->Image = nullptr;
2236
		this->UninstallSelectedContext->Visible = false;
2237
		this->viewReadmeToolStripMenuItem->Visible = false;
2238
		this->extrasToolStripMenuItem->Visible = false;
2239
		this->checkForUpdatesToolStripMenuItem->Visible = false;
2240
 
2241
		if ( p 	|| this->ListPackages->SelectedItems->Count )
2242
		{
2243
 
2244
			if ( item && p )
2245
			{
2246
				m_pListItem = item;
2247
				this->ContextName->Text = item->Text;
2248
				if ( item->ImageIndex != -1 )
2249
					this->ContextName->Image = this->ListPackages->LargeImageList->Images[item->ImageIndex];
2250
				else if ( item->ImageKey )
2251
				{
2252
					int key = this->ListPackages->LargeImageList->Images->IndexOfKey(item->ImageKey);
2253
					if ( key != -1 )
2254
						this->ContextName->Image = this->ListPackages->LargeImageList->Images[key];
2255
				}
2256
				else if ( p->GetIcon() )
2257
					PluginManager::DisplayContextIcon(p, this->ContextName, nullptr);
2258
 
2259
				this->uninstallToolStripMenuItem->Text = "Uninstall: " + item->Text;
2260
 
2261
				this->viewReadmeToolStripMenuItem->DropDownItems->Clear();
2262
				if ( p->CountFiles(FILETYPE_README) )
2263
				{
2264
					for ( C_File *f = p->GetFirstFile(FILETYPE_README); f; f = p->GetNextFile(f) )
2265
					{
2266
						if ( f->GetBaseName().GetToken(".", 1, 1).IsNumber() )
2267
						{
2268
							if ( f->GetBaseName().GetToken(".", 1, 1).ToInt() != m_pPackages->GetLanguage() )
2269
								continue;
2270
						}
2271
						if ( f->GetBaseName().IsIn("-L") )
2272
						{
2273
							int pos = f->GetBaseName().FindPos("-L");
2274
							int l = f->GetBaseName().Mid(pos + 2, 3).ToInt();
2275
							if ( l != m_pPackages->GetLanguage() )
2276
								continue;
2277
						}
2278
 
2279
						Windows::Forms::ToolStripMenuItem ^item = gcnew Windows::Forms::ToolStripMenuItem();
158 cycrow 2280
						item->Text = _US(f->filename());
1 cycrow 2281
						item->Image = this->viewReadmeToolStripMenuItem->Image;
2282
						item->ImageScaling = ToolStripItemImageScaling::None;
2283
						item->Click += gcnew System::EventHandler(this, &MainGui::RunItem);
129 cycrow 2284
						item->Tag = _US(f->filePointer());
1 cycrow 2285
						this->viewReadmeToolStripMenuItem->DropDownItems->Add(item);
2286
					}
2287
 
2288
					if ( this->viewReadmeToolStripMenuItem->DropDownItems->Count )
2289
					{
2290
						this->viewReadmeToolStripMenuItem->Visible = true;
2291
						showSep = true;
2292
					}
2293
				}
2294
 
2295
				this->extrasToolStripMenuItem->DropDownItems->Clear();
2296
				if ( p->CountFiles(FILETYPE_EXTRA) )
2297
				{
2298
					showSep = true;
2299
					for ( C_File *f = p->GetFirstFile(FILETYPE_EXTRA); f; f = p->GetNextFile(f) )
2300
					{
2301
						if ( !f->GetDir().Left(6).Compare("extras") )
2302
							continue;
2303
 
2304
						Windows::Forms::ToolStripMenuItem ^item = gcnew Windows::Forms::ToolStripMenuItem();
158 cycrow 2305
						item->Text = _US(f->filename());
2306
						if ( this->imageList2->Images->IndexOfKey(_US(f->fileExt().lower())) > -1 )
2307
							item->Image = this->imageList2->Images[this->imageList2->Images->IndexOfKey(_US(f->fileExt().lower()))];
1 cycrow 2308
						else
2309
						{
129 cycrow 2310
							Utils::String exe = f->filePointer();
2311
							exe = exe.findReplace("/", "\\");
1 cycrow 2312
							wchar_t wText[200];
129 cycrow 2313
							::MultiByteToWideChar(CP_ACP, NULL, (char *)exe.c_str(), -1, wText, exe.length() + 1);
1 cycrow 2314
 
2315
							System::Drawing::Icon ^myIcon;
2316
							SHFILEINFO *shinfo = new SHFILEINFO();
2317
 
2318
							if ( FAILED(SHGetFileInfo(wText, 0, shinfo, sizeof(shinfo), SHGFI_ICON | SHGFI_LARGEICON)) )
2319
							{
2320
								if ( FAILED(SHGetFileInfo(wText, 0, shinfo, sizeof(shinfo), SHGFI_ICON | SHGFI_SMALLICON)) )
2321
									item->Image = this->imageList2->Images[0];
2322
								else
2323
								{
2324
									myIcon = System::Drawing::Icon::FromHandle(IntPtr(shinfo->hIcon));
2325
									item->Image = myIcon->ToBitmap();
2326
								}
2327
							}
2328
							else
2329
							{
2330
								myIcon = System::Drawing::Icon::FromHandle(IntPtr(shinfo->hIcon));
2331
								item->Image = myIcon->ToBitmap();
2332
							}
2333
 
2334
							delete shinfo;
2335
						}
2336
						item->ImageScaling = ToolStripItemImageScaling::None;
2337
						item->Click += gcnew System::EventHandler(this, &MainGui::RunItem);
129 cycrow 2338
						item->Tag = _US(f->filePointer());
1 cycrow 2339
						this->extrasToolStripMenuItem->DropDownItems->Add(item);
2340
					}
2341
				}
2342
 
2343
				if ( this->extrasToolStripMenuItem->DropDownItems->Count )
2344
					this->extrasToolStripMenuItem->Visible = true;
2345
 
2346
				// email/website/forum
49 cycrow 2347
				if ( !p->forumLink().empty() ) {
2348
					Utils::String web = p->forumLink();
2349
					if ( web.isNumber() )
2350
						web = Utils::String("http://forum.egosoft.com/viewtopic.php?t=") + web; 
1 cycrow 2351
 
2352
					this->visitForumPageToolStripMenuItem->Visible = true;
49 cycrow 2353
					if ( !web.isin("http://") )
2354
						this->visitForumPageToolStripMenuItem->Tag = "http://" + _US(web);
1 cycrow 2355
					else
49 cycrow 2356
						this->visitForumPageToolStripMenuItem->Tag = _US(web);
1 cycrow 2357
					showSep2 = true;
2358
				}
49 cycrow 2359
				if ( !p->email().empty() )
1 cycrow 2360
				{
2361
					this->emailAuthorToolStripMenuItem->Visible = true;
50 cycrow 2362
					this->emailAuthorToolStripMenuItem->Tag = "mailto://" + _US(p->email()) + "?subject=Re: " + _US(p->name().findReplace(" ", "%20"));
1 cycrow 2363
					showSep2 = true;
2364
				}
49 cycrow 2365
				if ( !p->webSite().empty() ) {
1 cycrow 2366
					this->visitWebSiteToolStripMenuItem->Visible = true;
49 cycrow 2367
					if ( !p->webSite().isin("http://") ) 
2368
						this->visitWebSiteToolStripMenuItem->Tag = "http://" + _US(p->webSite());
2369
					else	
2370
						this->visitWebSiteToolStripMenuItem->Tag = _US(p->webSite());
1 cycrow 2371
					showSep2 = true;
2372
				}
2373
 
49 cycrow 2374
				if ( !p->webAddress().empty() )
1 cycrow 2375
					this->checkForUpdatesToolStripMenuItem->Visible = true;
2376
			}
2377
			else
2378
				m_pListItem = nullptr;
2379
 
2380
			if ( this->ListPackages->SelectedItems->Count > 1 || !p )
2381
			{
2382
				this->UninstallSelectedContext->Visible = true;
2383
				this->UninstallSelectedContext->Text = "Uninstall Selected (" + System::Convert::ToString(this->ListPackages->SelectedItems->Count) + " packages)";
2384
			}
2385
 
2386
			if ( p )
2387
			{
2388
				if ( p->IsEnabled() )
2389
					this->ContextDisable->Visible = true;
2390
				else
2391
					this->ContextEnable->Visible = true;
2392
			}
2393
 
126 cycrow 2394
			if (p->IsMod())
2395
			{
2396
				this->UninstallSelectedContext->Visible = false;
2397
				this->uninstallToolStripMenuItem->Visible = false;
2398
				this->viewModSelectedToolStripMenuItem->Visible = true;
2399
				this->ContextDisable->Visible = false;
2400
				this->ContextEnable->Visible = false;
2401
			}
2402
			else 
2403
				this->viewModSelectedToolStripMenuItem->Visible = false;
2404
 
1 cycrow 2405
			this->ContextSeperator->Visible = showSep;
2406
			this->ContextSeperator2->Visible = showSep2;
2407
			E->Cancel = false;
2408
		}
2409
	}
2410
 
2411
	System::Void MainGui::ListPackages_DragOver(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e)
2412
	{
2413
		e->Effect = DragDropEffects::None;
2414
 
2415
		if (e->Data->GetDataPresent(DataFormats::FileDrop)) 
2416
		{
2417
			cli::array<String ^> ^a = (cli::array<String ^> ^)e->Data->GetData(DataFormats::FileDrop, false);
2418
			int i;
2419
			for(i = 0; i < a->Length; i++)
2420
			{
2421
				String ^s = a[i];
2422
				String ^ext = IO::FileInfo(s).Extension;
2423
				if ( String::Compare(IO::FileInfo(s).Extension, ".xsp", true) == 0 || String::Compare(IO::FileInfo(s).Extension, ".spk", true) == 0 )
2424
				{
2425
					e->Effect = DragDropEffects::Copy;
2426
					break;
2427
				}
2428
			}
2429
		}
2430
	}
2431
 
2432
	System::Void MainGui::ListPackages_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e)
2433
	{
2434
		if (e->Data->GetDataPresent(DataFormats::FileDrop)) 
2435
		{
2436
			cli::array<String ^> ^a = (cli::array<String ^> ^)e->Data->GetData(DataFormats::FileDrop, false);
2437
			int i;
2438
			for(i = 0; i < a->Length; i++)
2439
			{
2440
				String ^s = a[i];
2441
				String ^ext = IO::FileInfo(s).Extension;
2442
				if ( String::Compare(IO::FileInfo(s).Extension, ".xsp", true) == 0 || String::Compare(IO::FileInfo(s).Extension, ".spk", true) == 0 )
2443
				{
2444
					if ( m_bDirLocked ) {
2445
						this->DisplayLocked(false);
2446
						return;
2447
					}
2448
					this->InstallPackage(s, false, false, true);
2449
				}
2450
			}
2451
 
2452
			this->StartInstalling(false, true);
2453
		}
2454
	}
2455
 
2456
	bool MainGui::CheckAccessRights(String ^dir)
2457
	{
2458
		/*
2459
		// check if already exists
2460
		String ^file = dir + "\\accessrightscheck.dat";
2461
		String ^writeStr = "testing file access";
2462
		if ( IO::File::Exists(file) )
2463
		{
2464
			// remove it
2465
			IO::File::Delete(file);
2466
			// still exists, cant delete it
2467
			if ( IO::File::Exists(file) )
2468
				return false;
2469
		}
2470
 
2471
		IO::DirectoryInfo ^dInfo = gcnew IO::DirectoryInfo(dir);
2472
		Security::AccessControl::DirectorySecurity ^dSecurity = dInfo->GetAccessControl();
2473
		dSecurity->
2474
 
2475
 
2476
		System::IO::FileStream ^writeStream = nullptr;
2477
		IO::BinaryWriter ^writer = nullptr;
2478
		try {
2479
			 writeStream = gcnew System::IO::FileStream(file, System::IO::FileMode::Create);
2480
			 writer = gcnew IO::BinaryWriter(writeStream);
2481
		}
2482
		catch (System::IO::IOException ^e)
2483
		{
2484
			MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2485
		}
2486
		catch (System::Exception ^e)
2487
		{
2488
			MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2489
		}
2490
		finally 
2491
		{
2492
			writer->Write(writeStr);
2493
			writer->Close();
2494
			writeStream->Close();
2495
		}
2496
 
2497
		// check if its written
2498
		if ( !IO::File::Exists(file) )
2499
			return false;
2500
 
2501
		// remove the file again
2502
		IO::File::Delete(file);
2503
		if ( IO::File::Exists(file) )
2504
			return false;
2505
*/
2506
		return true;
2507
	}
2508
 
2509
	System::Void MainGui::RunItem(System::Object ^sender, System::EventArgs ^e)
2510
	{
2511
		Windows::Forms::ToolStripMenuItem ^item = cli::safe_cast<ToolStripMenuItem ^>(sender);
2512
		String ^file = Convert::ToString(item->Tag);
2513
 
2514
		if ( IO::File::Exists(file) )
2515
		{
2516
			System::Diagnostics::Process::Start(file);
2517
		}
2518
	}
2519
 
2520
	void MainGui::RunFromToolItem(ToolStripMenuItem ^item)
2521
	{
2522
		if ( !item ) return;
2523
		if ( !item->Tag ) return;
2524
 
2525
		String ^file = Convert::ToString(item->Tag);
2526
		System::Diagnostics::Process::Start(file);
2527
	}
2528
 
2529
	void MainGui::FakePatchControlDialog()
2530
	{
2531
		FakePatchControl ^fpc = gcnew FakePatchControl(m_pPackages);
2532
		if ( fpc->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2533
		{
2534
			m_pPackages->ApplyFakePatchOrder(fpc->GetPatchOrder());
2535
			m_pPackages->ShuffleFakePatches(0);
2536
		}
2537
	}
2538
 
2539
	void MainGui::CheckFakePatchCompatability()
2540
	{
2541
		CyStringList errorList;
2542
		int count = 0;
2543
		int packageCount = 0;
2544
		for ( CBaseFile *p = m_pPackages->GetFirstPackage(); p; p = m_pPackages->GetNextPackage(p) )
2545
		{
2546
			if ( !p->IsEnabled() ) continue;
2547
			if ( !p->AnyFileType(FILETYPE_MOD) ) continue;
2548
 
2549
			CyString packageName = p->GetFullPackageName(m_pPackages->GetLanguage());
2550
 
2551
			// compare this file against all other packages
2552
			for ( CBaseFile *comparePackage = m_pPackages->GetNextPackage(p); comparePackage; comparePackage = m_pPackages->GetNextPackage(comparePackage) )
2553
			{
2554
				if ( comparePackage == p ) continue; // dont include the same package
2555
				if ( !comparePackage->IsEnabled() ) continue;
2556
				if ( !comparePackage->AnyFileType(FILETYPE_MOD) ) continue;
2557
 
2558
				CyStringList list;
2559
				if ( m_pPackages->CheckCompatabilityBetweenMods(p, comparePackage, &list) )
2560
				{
2561
					CyString package2Name = comparePackage->GetFullPackageName(m_pPackages->GetLanguage());
2562
					for ( SStringList *str = list.Head(); str; str = str->next )
2563
					{
2564
						errorList.PushBack(str->str + " (" + packageName + ")", str->data + " (" + package2Name + ")");
2565
						++count;
2566
					}
2567
					++packageCount;
2568
				}
2569
			}
2570
		}
2571
 
2572
		if ( count )
2573
		{
2574
			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 )
2575
			{
2576
				CompareList ^cl = gcnew CompareList("Fake Patch Incompatabilities");
2577
				cl->AddStringList(errorList);
2578
				cl->ShowDialog(this);
2579
			}
2580
		}
2581
		else
2582
			MessageBox::Show(this, "No incompatabilities found between fake patches", "Fake Patch Compatability", MessageBoxButtons::OK, MessageBoxIcon::Information);
2583
	}
2584
 
88 cycrow 2585
	void MainGui::EditWaresDialog()
2586
	{
2587
		if ( m_bDirLocked ) {
2588
			this->DisplayLocked(false);
2589
			return;
2590
		}
2591
 
2592
		EditWares ^edit = gcnew EditWares(m_pPackages);
2593
 
2594
		if ( edit->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2595
		{
2596
		}
2597
	}
2598
 
89 cycrow 2599
	void MainGui::CommandSlotsDialog()
2600
	{
2601
		CommandSlots ^slots = gcnew CommandSlots(m_pPackages);
2602
 
2603
		slots->ShowDialog(this);
2604
	}
2605
 
1 cycrow 2606
	void MainGui::EditGlobalsDialog()
2607
	{
2608
		if ( m_pPackages->IsVanilla() ) {
2609
			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);
2610
			return;
2611
		}
2612
		if ( m_bDirLocked ) {
2613
			this->DisplayLocked(false);
2614
			return;
2615
		}
2616
 
2617
		//load globals
2618
		CyStringList globals;
2619
		m_pPackages->ReadGlobals(globals);
2620
 
2621
		EditGlobals ^edit = gcnew EditGlobals(&globals);
2622
 
2623
		// make our saved changes
160 cycrow 2624
		auto& packageGlobals = m_pPackages->GetGlobals();
2625
		for(auto itr = packageGlobals.begin(); itr != packageGlobals.end(); itr++)
2626
			edit->SetEditedItem(_US((*itr)->str), _US((*itr)->data));
1 cycrow 2627
 
2628
		if ( edit->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2629
		{
2630
			// compare whats different and save
160 cycrow 2631
			packageGlobals.clear();
2632
			for (SStringList* str = edit->GetSavedSettings()->Head(); str; str = str->next)
2633
				packageGlobals.pushBack(str->str.ToString(), str->data.ToString());
1 cycrow 2634
		}
2635
	}
2636
 
2637
	void MainGui::ViewFileLog()
2638
	{
2639
		if ( m_pFileErrors->Empty() )
2640
			MessageBox::Show(this, "No messages to view in file log", "Empty File Log", MessageBoxButtons::OK, MessageBoxIcon::Warning);
2641
		else
2642
		{
2643
			FileLog ^log = gcnew FileLog;
2644
			for ( SStringList *str = m_pFileErrors->Head(); str; str = str->next )
2645
			{
2646
				bool add = true;
2647
				String ^status = "Unknown Error";
2648
				switch(str->data.GetToken(" ", 1, 1).ToInt())
2649
				{
2650
					case SPKINSTALL_CREATEDIRECTORY:
2651
						status = "Created Directory";
2652
						break;
2653
					case SPKINSTALL_CREATEDIRECTORY_FAIL:
2654
						status = "Failed to create Directory";
2655
						break;
2656
					case SPKINSTALL_WRITEFILE:
2657
						status = "File Written";
2658
						break;
2659
					case SPKINSTALL_WRITEFILE_FAIL:
2660
						status = "Failed to Write File";
2661
						break;
2662
					case SPKINSTALL_DELETEFILE:
2663
						status = "Deleted File";
2664
						break;
2665
					case SPKINSTALL_DELETEFILE_FAIL:
2666
						status = "Failed to Delete File";
2667
						break;
2668
					case SPKINSTALL_SKIPFILE:
2669
						status = "File Skipped";
2670
						break;
2671
					case SPKINSTALL_REMOVEDIR:
2672
						status = "Removed Directory";
2673
						break;
2674
					case SPKINSTALL_ENABLEFILE:
2675
						status = "Enabled File";
2676
						break;
2677
					case SPKINSTALL_DISABLEFILE:
2678
						status = "Disabled File";
2679
						break;
2680
					case SPKINSTALL_ENABLEFILE_FAIL:
2681
						status = "Failed to Enable File";
2682
						break;
2683
					case SPKINSTALL_DISABLEFILE_FAIL:
2684
						status = "Failed to Disable File";
2685
						break;
2686
					case SPKINSTALL_UNINSTALL_MOVE:
2687
						status = "Moved Uninstall File";
2688
						break;
2689
					case SPKINSTALL_UNINSTALL_COPY:
2690
						status = "Copied Uninstall File";
2691
						break;
2692
					case SPKINSTALL_UNINSTALL_MOVE_FAIL:
2693
						status = "Failed to move uninstall file";
2694
						break;
2695
					case SPKINSTALL_UNINSTALL_COPY_FAIL:
2696
						status = "Failed to copy uninstall file";
2697
						break;
2698
					case SPKINSTALL_UNINSTALL_REMOVE:
2699
						status = "Removed uninstall file";
2700
						break;
2701
					case SPKINSTALL_UNINSTALL_REMOVE_FAIL:
2702
						status = "Failed to remove uninstall file";
2703
						break;
2704
					case SPKINSTALL_ORIGINAL_BACKUP:
2705
						status = "Backed up Original";
2706
						break;
2707
					case SPKINSTALL_ORIGINAL_RESTORE:
2708
						status = "Restored Original";
2709
						break;
2710
					case SPKINSTALL_ORIGINAL_BACKUP_FAIL:
2711
						status = "Failed to Backup Original";
2712
						break;
2713
					case SPKINSTALL_ORIGINAL_RESTORE_FAIL:
2714
						status = "Failed to restore Original";
2715
						break;
2716
					case SPKINSTALL_FAKEPATCH:
2717
						status = "Adjusting Fakepatch";
2718
						break;
2719
					case SPKINSTALL_FAKEPATCH_FAIL:
2720
						status = "Failed to adjust Fakepatch";
2721
						break;
2722
					case SPKINSTALL_AUTOTEXT:
2723
						status = "Adjusting Text File";
2724
						break;
2725
					case SPKINSTALL_AUTOTEXT_FAIL:
2726
						status = "Failed to adjust Text File";
2727
						break;
2728
					case SPKINSTALL_MISSINGFILE:
2729
						status = "Missing File";
2730
						break;
2731
					case SPKINSTALL_SHARED:
2732
						status = "Shared File";
2733
						break;
2734
					case SPKINSTALL_SHARED_FAIL:
2735
						status = "Shared File Failed";
2736
						break;
2737
					case SPKINSTALL_ORPHANED:
2738
						status = "File Orphaned";
2739
						break;
2740
					case SPKINSTALL_ORPHANED_FAIL:
2741
						status = "Failed to Orphan file";
2742
						break;
2743
					case SPKINSTALL_UNCOMPRESS_FAIL:
2744
						status = "Failed to Uncompress";
2745
						break;
2746
				}
2747
 
2748
				if ( add )
2749
				{
2750
					if ( str->data.NumToken(" ") > 1 )
2751
						log->AddItem(SystemStringFromCyString(str->str.findreplace("~", " => ")), status, SystemStringFromCyString(SPK::ConvertTimeString((long)str->data.GetToken(" ", 2, 2).ToLong())));
2752
					else
2753
						log->AddItem(SystemStringFromCyString(str->str.findreplace("~", " => ")), status, nullptr);
2754
				}
2755
			}
2756
			if ( log->ShowDialog(this) == Windows::Forms::DialogResult::Cancel )
2757
			{
2758
				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 )
2759
				{
2760
					m_pFileErrors->Clear();
2761
					MessageBox::Show(this, "The file log has been cleared", "File Log Cleared", MessageBoxButtons::OK, MessageBoxIcon::Information);
2762
				}
2763
			}
2764
 
2765
		}
2766
	}
2767
 
2768
	void MainGui::VerifyInstalledFiles()
2769
	{
2770
		CyStringList missing;
2771
		int amount = m_pPackages->VerifyInstalledFiles(&missing);
2772
		if ( !amount )
2773
			MessageBox::Show(this, "All files are currently installed", "Verifying Installed Files", MessageBoxButtons::OK, MessageBoxIcon::Information);
2774
		else
2775
		{
2776
			String ^text;
2777
			for ( SStringList *str = missing.Head(); str; str = str->next )
2778
			{
2779
				text += SystemStringFromCyString(str->str);
2780
				text += "\n\t";
2781
				CyString data = str->data.findreplace("\n", "\t\n");
2782
				text += SystemStringFromCyString(data);
2783
				text += "\n\n";
2784
			}
2785
			MessageBoxDetails::Show(this, "Verifing Installed Files", "Missing files detected\nAmount = " + amount, text, false, 600);
2786
		}
2787
	}
2788
 
2789
	void MainGui::ExportPackageList()
2790
	{
2791
		bool enabled = false;
2792
		if ( MessageBox::Show(this, "Do you only want to export enabled packages?", "Only Enabled", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == Windows::Forms::DialogResult::Yes )
2793
			enabled =true;
2794
		SaveFileDialog ^ofd = gcnew SaveFileDialog();
2795
		ofd->Filter = "Log Files (*.log)|*.log";
2796
		ofd->FilterIndex = 1;
2797
		ofd->RestoreDirectory = true;
2798
		ofd->AddExtension =  true;
2799
		ofd->Title = "Select the file to save the package list to";
2800
		if ( ofd->ShowDialog(this) == Windows::Forms::DialogResult::OK )
2801
		{
2802
			if ( IO::File::Exists(ofd->FileName) )
2803
				IO::File::Delete(ofd->FileName);
2804
 
2805
			StreamWriter ^sw = File::CreateText(ofd->FileName);
2806
			try 
2807
			{
2808
				for ( CBaseFile *package = m_pPackages->FirstPackage(); package; package = m_pPackages->NextPackage() )
2809
				{
2810
					if ( enabled && !package->IsEnabled() ) continue;
50 cycrow 2811
					Utils::String line = package->name() + " :: " + package->author() + " :: " + package->version() + " :: " + package->creationDate() + " :: ";
1 cycrow 2812
 
2813
					if ( package->GetType() == TYPE_XSP )
2814
						line += "Ship :: ";
2815
					else if ( package->GetType() == TYPE_ARCHIVE )
2816
						line += "- Archive - :: ";
50 cycrow 2817
					else if ( package->GetType() == TYPE_SPK ) {
2818
						Utils::String type = ((CSpkFile *)package)->GetScriptTypeString(m_pPackages->GetLanguage());
2819
						if ( !type.empty() ) line += type + " :: ";
1 cycrow 2820
					}
2821
 
50 cycrow 2822
					line = line + ((package->IsEnabled()) ? "Yes" : "No") + " :: " + ((package->IsSigned()) ? "Yes" : "No");
2823
					sw->WriteLine(_US(line));
1 cycrow 2824
				}
2825
			}
2826
			finally
2827
			{
2828
				if ( sw )
2829
					delete (IDisposable ^)sw;
2830
			}				
2831
 
2832
			if ( IO::File::Exists(ofd->FileName) )
2833
			{
2834
				if ( enabled )
2835
					MessageBox::Show(this, "Enabled Packages have been saved to:\n" + ofd->FileName, "Package List Saved", MessageBoxButtons::OK, MessageBoxIcon::Information);
2836
				else
2837
					MessageBox::Show(this, "Complete Package List has been saved to:\n" + ofd->FileName, "Package List Saved", MessageBoxButtons::OK, MessageBoxIcon::Information);
2838
			}
2839
			else
2840
				MessageBox::Show(this, "There was an error writing file:\n" + ofd->FileName, "File Write Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
2841
		}
2842
	}
121 cycrow 2843
	System::Void MainGui::MainGui_Shown(System::Object^  sender, System::EventArgs^  e)
2844
	{
2845
		if (m_pDirList->empty())
2846
		{
2847
			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)
2848
				this->OpenDirectoryControl();
2849
		}
2850
	}
2851
 
2852
	System::Void MainGui::MainGui_Load(System::Object^  sender, System::EventArgs^  e)
2853
	{
2854
 
2855
		if ( m_iSaveGameManager == -1 )
2856
		{
2857
			m_iSaveGameManager = 0;
2858
			/*
2859
			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 )
2860
			{
2861
				m_iSaveGameManager = 1;
2862
				this->PrepareSaveGameManager();
2863
			}
2864
			else
2865
				m_iSaveGameManager = 0;
2866
			*/
2867
		}
2868
		m_pMenuBar->SetSaveGameManager((m_iSaveGameManager == 1) ? true : false);
2869
 
2870
		// auto update
2871
		if (m_iSizeX != -1 && m_iSizeY != -1)
2872
			this->Size = System::Drawing::Size(m_iSizeX, m_iSizeY);
2873
 
2874
		this->UpdateBuiltInPackages();
2875
		this->AutoUpdate();
2876
	}
126 cycrow 2877
	System::Void MainGui::viewModSelectedToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
2878
	{		
2879
		this->OpenModSelecter();
2880
	}
1 cycrow 2881
}