Subversion Repositories spk

Rev

Rev 225 | 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 "SpkForm.h"
3
 
4
#include "Form1.h"
5
#include "ExtractDialog.h"
6
#include "AddDialog.h"
7
#include "DropFileDialog.h"
8
 
9
#include "..\..\Creator\src\Forms\SelectGame.h"
10
 
11
#include <spk.h>
12
 
13
using namespace System::Windows;
14
using namespace System::Windows::Forms;
15
 
16
namespace SpkExplorer {
129 cycrow 17
	SpkForm::SpkForm(System::Windows::Forms::Form ^parent, System::Windows::Forms::TabControl ^ctrl, System::Windows::Forms::TabPage ^page, CPackages *packages)
128 cycrow 18
	{
19
		InitializeComponent();
20
		this->AllowDrop = true;
21
 
22
		ImageList ^list1 = gcnew ImageList();
23
		ImageList ^list2 = gcnew ImageList();
24
		list1->ImageSize.Height = 32;
25
		list1->ImageSize.Width = 32;
26
		list2->ImageSize.Height = 16;
27
		list2->ImageSize.Width = 16;
28
 
29
		this->AddImage("$IMAGELIST:script.png", list1, _US(Utils::String::Number(FILETYPE_SCRIPT)));
30
		this->AddImage("$IMAGELIST:textfile.png", list1, _US(Utils::String::Number(FILETYPE_TEXT)));
31
		this->AddImage("$IMAGELIST:readme.png", list1, _US(Utils::String::Number(FILETYPE_README)));
32
		this->AddImage("$IMAGELIST:map.png", list1, _US(Utils::String::Number(FILETYPE_MAP)));
33
		this->AddImage("$IMAGELIST:mods.png", list1, _US(Utils::String::Number(FILETYPE_MOD)));
34
		this->AddImage("$IMAGELIST:uninstall.png", list1, _US(Utils::String::Number(FILETYPE_UNINSTALL)));
35
		this->AddImage("$IMAGELIST:sound.png", list1, _US(Utils::String::Number(FILETYPE_SOUND)));
36
		this->AddImage("$IMAGELIST:extras.png", list1, _US(Utils::String::Number(FILETYPE_EXTRA)));
37
		this->AddImage("$IMAGELIST:screenshot.png", list1, _US(Utils::String::Number(FILETYPE_SCREEN)));
38
		this->AddImage("$IMAGELIST:backup.png", list1, _US(Utils::String::Number(FILETYPE_BACKUP)));
39
		this->AddImage("$IMAGELIST:mission.png", list1, _US(Utils::String::Number(FILETYPE_MISSION)));
40
		this->AddImage("$IMAGELIST:advert.png", list1, _US(Utils::String::Number(FILETYPE_ADVERT)));
41
		this->AddImage("$IMAGELIST:shipother.png", list1, _US(Utils::String::Number(FILETYPE_SHIPOTHER)));
42
		this->AddImage("$IMAGELIST:shipmodel.png", list1, _US(Utils::String::Number(FILETYPE_SHIPMODEL)));
43
		this->AddImage("$IMAGELIST:shipscene.png", list1, _US(Utils::String::Number(FILETYPE_SHIPSCENE)));
44
		this->AddImage("$IMAGELIST:cockpitscene.png", list1, _US(Utils::String::Number(FILETYPE_COCKPITSCENE)));
45
 
46
		this->AddImage("$IMAGELIST:script.png", list2, _US(Utils::String::Number(FILETYPE_SCRIPT)));
47
		this->AddImage("$IMAGELIST:textfile.png", list2, _US(Utils::String::Number(FILETYPE_TEXT)));
48
		this->AddImage("$IMAGELIST:readme.png", list2, _US(Utils::String::Number(FILETYPE_README)));
49
		this->AddImage("$IMAGELIST:map.png", list2, _US(Utils::String::Number(FILETYPE_MAP)));
50
		this->AddImage("$IMAGELIST:mods.png", list2, _US(Utils::String::Number(FILETYPE_MOD)));
51
		this->AddImage("$IMAGELIST:uninstall.png", list2, _US(Utils::String::Number(FILETYPE_UNINSTALL)));
52
		this->AddImage("$IMAGELIST:sound.png", list2, _US(Utils::String::Number(FILETYPE_SOUND)));
53
		this->AddImage("$IMAGELIST:extras.png", list2, _US(Utils::String::Number(FILETYPE_EXTRA)));
54
		this->AddImage("$IMAGELIST:screenshot.png", list2, _US(Utils::String::Number(FILETYPE_SCREEN)));
55
		this->AddImage("$IMAGELIST:backup.png", list2, _US(Utils::String::Number(FILETYPE_BACKUP)));
56
		this->AddImage("$IMAGELIST:mission.png", list2, _US(Utils::String::Number(FILETYPE_MISSION)));
57
		this->AddImage("$IMAGELIST:advert.png", list2, _US(Utils::String::Number(FILETYPE_ADVERT)));
58
		this->AddImage("$IMAGELIST:shipother.png", list2, _US(Utils::String::Number(FILETYPE_SHIPOTHER)));
59
		this->AddImage("$IMAGELIST:shipmodel.png", list2, _US(Utils::String::Number(FILETYPE_SHIPMODEL)));
60
		this->AddImage("$IMAGELIST:shipscene.png", list2, _US(Utils::String::Number(FILETYPE_SHIPSCENE)));
61
		this->AddImage("$IMAGELIST:cockpitscene.png", list2, _US(Utils::String::Number(FILETYPE_COCKPITSCENE)));
62
 
63
		listView1->LargeImageList = list1;
64
		listView1->SmallImageList = list2;
65
 
66
		m_bLoading = false;
67
		this->MdiParent = parent;
68
		m_pTabCtrl = ctrl;
69
		m_pTabPage = page;
70
 
71
		m_bSortingAsc = true;
72
		m_iSortingCol = 1;
73
		m_bSortingAsc2 = true;
74
		m_iSortingCol2 = 1;
75
		m_pListItem = nullptr;
76
 
129 cycrow 77
		_pPackages = packages;
128 cycrow 78
 
79
		this->Closing += gcnew CancelEventHandler(this, &SpkForm::Event_Closing);
80
		this->Activated += gcnew System::EventHandler(this, &SpkForm::Event_Activated);
81
		this->listView1->ColumnClick += gcnew ColumnClickEventHandler(this, &SpkForm::SortList);
82
		this->contextMenuStrip1->Opening += gcnew CancelEventHandler(this, &SpkExplorer::SpkForm::OpenContextMenu);
83
		this->listView1->DragOver += gcnew DragEventHandler(this, &SpkExplorer::SpkForm::DragEnterEvent);
84
		this->listView1->ItemDrag += gcnew ItemDragEventHandler(this, &SpkExplorer::SpkForm::ItemDragEvent);
85
 
86
		m_pSelectedPackage = NULL;
87
		m_pPackage = NULL;
88
		m_pMultiPackage = NULL;
89
	}
90
 
91
	SpkForm::~SpkForm()
92
	{
93
		delete m_pMenuItem;
94
		if (components)
95
		{
96
			delete components;
97
		}
98
	}
99
 
100
	bool SpkForm::IsMultiPackage()
101
	{ 
102
		return (m_pMultiPackage) ? true : false; 
103
	}
104
 
105
	void SpkForm::SetToolButton(ToolStripMenuItem ^menu)
106
	{
107
		m_pMenuItem = menu;
108
	}
109
 
1 cycrow 110
	void SpkForm::SetPackage(CBaseFile *p, System::String ^filename)
111
	{
112
		this->splitContainer1->Panel1Collapsed = true;
113
		m_pPackage = p;
114
 
115
		m_pTabPage->Text = System::IO::FileInfo(filename).Name;
50 cycrow 116
		if ( p->filename().empty() )
224 cycrow 117
			p->setFilename(_WS(filename));
1 cycrow 118
		this->UpdateView(false);
50 cycrow 119
		m_pPackage->adjustChanged(false);
1 cycrow 120
 
121
		this->Text = filename;
122
 
123
		m_sFilename = filename;
124
	}
125
 
126
	void SpkForm::SetMultiPackage(CMultiSpkFile *p, System::String ^filename)
127
	{
128
		this->splitContainer1->Panel1Collapsed = false;
129
		m_pMultiPackage = p;
130
 
131
		m_pTabPage->Text = System::IO::FileInfo(filename).Name;
177 cycrow 132
		if (p->filename().empty())
224 cycrow 133
			p->setFilename(_WS(filename));
1 cycrow 134
 
135
		this->UpdateView(false);
136
 
137
		this->Text = filename;
138
 
139
		m_sFilename = filename;
140
 
141
		// read the icons
142
		for ( SMultiSpkFile *package = p->GetFileList()->First(); package; package = p->GetFileList()->Next() )
143
		{
144
			if ( !package->pFile )
145
				continue;
170 cycrow 146
			if ( package->pFile->icon() )
1 cycrow 147
			{
148
				package->pFile->ReadIconFileToMemory();
197 cycrow 149
				Utils::WString sIconFile = _WS(IO::Path::GetTempPath()) + L"\\" + CFileIO(package->sName).baseName() + L"." + package->pFile->iconExt();
224 cycrow 150
				if ( package->pFile->extractFile(package->pFile->icon(), CFileIO(sIconFile).fullFilename(), false))
1 cycrow 151
				{
158 cycrow 152
					String ^iconFile = _US(sIconFile);
1 cycrow 153
					if ( IO::File::Exists(iconFile) )
154
					{
191 cycrow 155
						String ^file = _US(package->sName);
1 cycrow 156
						String ^ext = System::IO::FileInfo(iconFile).Extension;
157
						if ( !String::Compare(ext, "bmp", false) || !String::Compare(ext, "ico", false) )
158
							this->imageList1->Images->Add(file, Bitmap::FromFile(iconFile));
159
						else
160
						{
161
							Bitmap ^myBitmap = gcnew Bitmap(iconFile);
162
							if ( myBitmap )
163
								this->imageList1->Images->Add(file, myBitmap);
164
						}
165
					}
166
				}
167
			}
168
		}
169
	}
170
 
171
	bool SpkForm::CheckFilename(System::String ^filename)
172
	{
173
		if ( filename == m_sFilename )
174
			return true;
175
		return false;
176
	}
177
 
178
	void SpkForm::UpdateView(bool onlyPackage)
179
	{
180
		m_bLoading = true;
181
 
182
		m_bSortingAsc = true;
183
		m_iSortingCol = 0;
184
 
185
		m_bSortingAsc2 = true;
186
		m_iSortingCol2 = 0;
187
 
188
		this->listView1->Items->Clear();
189
 
190
		if ( m_pPackage )
191
		{
192
			int pos = 0;
193
			for ( C_File *file = m_pPackage->GetFileList()->First(); file; file = m_pPackage->GetFileList()->Next() )
194
			{
195
				file->SetPos(pos++);
196
				this->AddFile(file);
197
			}
198
		}
199
		listView1->ArrangeIcons();
200
 
201
		this->listView1->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
202
		this->listView1->ListViewItemSorter = gcnew ListViewItemComparer(m_iSortingCol, !m_bSortingAsc);
203
 
204
		if ( m_pMultiPackage && !onlyPackage )
205
		{
206
			this->listView2->Items->Clear();
207
			int pos = 0;
208
			for ( SMultiSpkFile *file = m_pMultiPackage->GetFileList()->First(); file; file = m_pMultiPackage->GetFileList()->Next() )
209
			{
210
				file->iPos = pos++;
211
				this->AddMultiFile(file);
212
			}
213
			listView2->ArrangeIcons();
214
 
215
			this->listView2->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
216
			this->listView2->ListViewItemSorter = gcnew ListViewItemComparer(m_iSortingCol2, !m_bSortingAsc2);
217
		}
218
 
219
 
220
		m_bLoading = false;
221
	}
222
 
223
	void SpkForm::AddImage(System::String ^filename, ImageList ^list, System::String ^key)
224
	{
225
		System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(SpkForm::typeid));
226
 
227
		bool added = false;
228
		if ( filename->StartsWith("$RESOURCE:") )
229
		{
230
			filename = filename->Remove(0, 10);
231
			if ( filename->StartsWith("BITMAP:") )
232
			{
233
				filename = filename->Remove(0, 7);
234
				System::Drawing::Bitmap ^bitmap = (cli::safe_cast<System::Drawing::Bitmap^  >(resources->GetObject(filename)));
235
				if ( bitmap )
236
				{
237
					IntPtr Hicon = bitmap->GetHicon();
238
					System::Drawing::Icon ^newIcon = ::Icon::FromHandle(Hicon);
239
					list->Images->Add(key, newIcon);
240
					added = true;
241
				}
242
			}
243
			else
244
			{
245
				System::Drawing::Icon ^icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(filename)));
246
				list->Images->Add(key, icon);
247
				added = true;
248
			}
249
		}
250
		else if ( filename->StartsWith("$IMAGELIST:") )
251
		{
252
			filename = filename->Remove(0, 11);
253
			int index = this->imageList1->Images->IndexOfKey(filename);
254
			if ( index != -1 )
255
			{
256
				list->Images->Add(key, this->imageList1->Images[index]);
257
				added = true;
258
			}
259
		}
260
		else if ( System::IO::File::Exists(filename) )
261
		{
224 cycrow 262
			Utils::WString sF = _WS(filename);
196 cycrow 263
			if ( CFileIO(sF).isFileExtension(L"bmp") )
1 cycrow 264
			{
265
				list->Images->Add(key, Bitmap::FromFile(filename));
266
				added = true;
267
			}
268
			else
269
			{
270
				Bitmap ^myBitmap = gcnew Bitmap(filename);
271
				if ( myBitmap )
272
				{
273
					IntPtr Hicon = myBitmap->GetHicon();
274
					System::Drawing::Icon ^newIcon = ::Icon::FromHandle(Hicon);
275
					list->Images->Add(key, newIcon);
276
					added = true;
277
				}
278
			}
279
		}
280
 
281
		if ( !added )
282
		{
283
			System::Drawing::Icon ^icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
284
			list->Images->Add(key, icon);
285
		}
286
	}
287
 
288
	void SpkForm::AddFile(C_File *file)
289
	{
130 cycrow 290
		ListViewItem ^item = gcnew ListViewItem(_US(file->getNameDirectory(NULL)));
178 cycrow 291
		item->SubItems->Add(_US(file->uncompressedSizeString()));
292
		item->SubItems->Add(_US(file->fileTypeString()));
293
		item->SubItems->Add(_US(file->creationTimeString()));
1 cycrow 294
 
128 cycrow 295
		item->Tag = _US(Utils::String::Number(file->GetPos()));
1 cycrow 296
 
128 cycrow 297
		item->ImageKey = _US(Utils::String::Number(file->GetFileType()));
1 cycrow 298
 
128 cycrow 299
		if ( !file->game() || file->game() == GAME_ALLNEW) {
1 cycrow 300
			item->SubItems->Add("All Games");
301
		}
302
		else {
128 cycrow 303
			if (file->game() & GAME_ALLNEW)
304
			{
197 cycrow 305
				Utils::WString sGames = L"";
128 cycrow 306
				for (int i = 0; i < 31; ++i)
307
				{
308
					if (file->game() & (1 << i))
309
					{
197 cycrow 310
						Utils::WString sGame;
311
						SGameExe *exe = _pPackages->GetGameExe()->game(i - 1);
312
						sGame = (exe) ? exe->sName : (L"Game: " + Utils::WString::Number(i));
128 cycrow 313
 
314
						if (sGames.empty())
315
							sGames = sGame;
316
						else
197 cycrow 317
							sGames = sGames + L", " + sGame;
128 cycrow 318
					}
319
				}
320
				if(sGames.empty())
226 cycrow 321
					item->SubItems->Add(L"All Games");
128 cycrow 322
				else
323
					item->SubItems->Add(_US(sGames));
1 cycrow 324
			}
128 cycrow 325
			else
326
			{
197 cycrow 327
				SGameExe *exe = _pPackages->GetGameExe()->game(file->game() - 1);
128 cycrow 328
				if (exe) {
329
					item->SubItems->Add(_US(exe->sName));
330
				}
331
				else {
332
					item->SubItems->Add("Game: " + file->game());
333
				}
1 cycrow 334
			}
335
		}
336
 
337
		this->listView1->Items->Add(item);
338
	}
339
 
340
	void SpkForm::AddMultiFile(SMultiSpkFile *file)
341
	{
191 cycrow 342
		ListViewItem ^item = gcnew ListViewItem(_US(file->sName));
1 cycrow 343
		if ( file->iType == TYPE_XSP )
344
			item->SubItems->Add("Ship");
345
		else
346
			item->SubItems->Add("Package");
191 cycrow 347
		item->SubItems->Add(_US(SPK::GetSizeString(file->lSize)));
1 cycrow 348
 
349
 
128 cycrow 350
		item->Tag = _US(Utils::String::Number(file->iPos));
1 cycrow 351
 
191 cycrow 352
		if ( this->imageList1->Images->IndexOfKey(_US(file->sName)) == -1 )
1 cycrow 353
			item->ImageKey = "package";
354
		else
191 cycrow 355
			item->ImageKey = _US(file->sName);
1 cycrow 356
 
357
		this->listView2->Items->Add(item);
358
	}
359
 
360
	System::Void SpkForm::Event_Activated(System::Object^  sender, System::EventArgs^  e)
361
	{
362
		m_pTabCtrl->SelectedTab = m_pTabPage;
363
		this->Text = m_pTabPage->Text;
364
 
365
		((Form1 ^)this->MdiParent)->UpdateDisplay();
366
 
367
		this->SelectedCheck();
368
	}
369
 
370
	System::Void SpkForm::Event_Closing(System::Object^  sender, CancelEventArgs^  e)
371
	{
372
		delete m_pPackage;
373
		delete m_pTabPage;
374
 
375
		m_sFilename = "";
376
 
377
		((Form1 ^)this->MdiParent)->RemoveCopied(true);
378
 
379
		((Form1 ^)this->MdiParent)->UpdateDisplay();
380
		((Form1 ^)this->MdiParent)->SelectedFile(false);
381
	}
382
 
383
	void SpkForm::SelectedCheck()
384
	{
385
		if ( m_pMultiPackage )
386
		{
387
			((Form1 ^)this->MdiParent)->SelectedPackage(this->IsSelected(), this->IsPackageSelected());
388
			if ( this->IsPackageSelected() )
389
			{
390
				Point ^mousePoint = this->listView2->PointToClient(this->listView2->MousePosition);
391
				ListViewItem ^item = this->listView2->GetItemAt(mousePoint->X, mousePoint->Y);
392
				if ( item )
393
				{
394
					int num = System::Convert::ToInt32(item->Tag);
395
					m_pSelectedPackage = m_pMultiPackage->GetFileList()->Get(num);
396
					if ( m_pSelectedPackage )
397
					{
398
						m_pPackage = m_pSelectedPackage->pFile;
399
						this->UpdateView(true);
400
					}
401
				}
402
			}
403
			else
404
			{
405
				m_pPackage = NULL;
406
				m_pSelectedPackage = NULL;
407
				this->UpdateView(true);
408
			}
409
		}
410
		else
411
			((Form1 ^)this->MdiParent)->SelectedFile(this->IsSelected());
412
	}
413
 
414
	bool SpkForm::IsSelected()
415
	{
416
		if ( this->listView1->SelectedItems->Count )
417
			return true;
418
		return false;
419
	}
420
 
421
	bool SpkForm::IsPackageSelected()
422
	{
423
		if ( this->listView2->SelectedItems->Count )
424
			return true;
425
		return false;
426
	}
427
 
428
	void SpkForm::DoExtract(CLinkList<C_File> *list, System::String ^toDir, int game)
429
	{
430
		if ( !toDir )
431
			return;
432
 
433
 
434
		// read the spk file to memory before trying to extract its files
435
		if ( !this->ReadMultiPackage() )
436
			return;
437
 
128 cycrow 438
		ExtractDialog ^ed = gcnew ExtractDialog(list, toDir, m_pPackage, game, _pPackages);
1 cycrow 439
		ed->ShowDialog((Form1 ^)this->MdiParent);
440
 
441
		if ( ed->OK() && !ed->Canceled() && ed->ExtractedCount() )
442
			MessageBox::Show(this, "Files have been extracting successfully to:\n" + toDir, "Extacted", MessageBoxButtons::OK, MessageBoxIcon::Information);
443
	}
444
 
445
	bool SpkForm::ReadSelectedMultiPackage()
446
	{
447
		if ( m_pMultiPackage && m_pSelectedPackage )
448
		{
449
			if ( !m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL) )
450
				return false;
451
		}
452
 
453
		return true;
454
	}
455
	bool SpkForm::ReadMultiPackage()
456
	{
457
		if ( m_pMultiPackage )
458
		{
177 cycrow 459
			if ( !m_pMultiPackage->readAllPackages(SPKREAD_ALL) )
1 cycrow 460
				return false;
461
		}
462
 
463
		return true;
464
	}
465
	void SpkForm::DoPackageExtract(CLinkList<SMultiSpkFile> *list, System::String ^toDir)
466
	{
467
		if ( !toDir )
468
			return;
469
 
128 cycrow 470
		ExtractDialog ^ed = gcnew ExtractDialog(list, toDir, m_pMultiPackage, _pPackages);
1 cycrow 471
		ed->ShowDialog((Form1 ^)this->MdiParent);
472
 
473
		if ( ed->OK() && !ed->Canceled() )
474
			MessageBox::Show(this, "Files have been extracting successfully to:\n" + toDir, "Extacted", MessageBoxButtons::OK, MessageBoxIcon::Information);
475
	}
476
 
477
	System::String ^SpkForm::GetExtractDir(bool package)
478
	{
479
		FolderBrowserDialog ^fbd = gcnew FolderBrowserDialog;
480
		if ( package )
481
			fbd->Description = "Select the path to extract packages to";
482
		else
483
			fbd->Description = "Select the path to extract files to";
484
		if ( fbd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
485
			return fbd->SelectedPath;
486
 
487
		return nullptr;
488
	}
489
 
490
	void SpkForm::ExtractSelected(System::String ^toDir)
491
	{
492
		if ( !m_pPackage )
493
			return;
494
 
225 cycrow 495
		Utils::WString sToDir = _WS(toDir);
1 cycrow 496
 
497
		CLinkList<C_File> list;
498
 
499
		for ( int i = 0; i < this->listView1->SelectedItems->Count; i++ )
500
		{
225 cycrow 501
			int pos = _WS(cli::safe_cast<System::String ^>(this->listView1->SelectedItems[i]->Tag)).toInt();
1 cycrow 502
			C_File *f = m_pPackage->GetFileList()->Get(pos);
503
			list.push_back(f);
504
		}
505
 
506
		this->DoExtract(&list, toDir, -1);
507
		list.clear();
508
	}
509
 
510
	void SpkForm::ExtractSelectedPackage(System::String ^toDir)
511
	{
512
		if ( !m_pMultiPackage )
513
			return;
514
 
225 cycrow 515
		Utils::WString sToDir = _WS(toDir);
1 cycrow 516
 
517
		CLinkList<SMultiSpkFile> list;
518
 
519
		for ( int i = 0; i < this->listView2->SelectedItems->Count; i++ )
520
		{
225 cycrow 521
			int pos = _WS(cli::safe_cast<System::String ^>(this->listView2->SelectedItems[i]->Tag)).toInt();
1 cycrow 522
			SMultiSpkFile *mp = m_pMultiPackage->GetFileList()->Get(pos);
523
			if ( mp )
524
				list.push_back(mp);
525
		}
526
 
527
		this->DoPackageExtract(&list, toDir);
528
		list.clear();
529
	}
530
 
531
	void SpkForm::RemoveItem(ListViewItem ^item)
532
	{
533
		if ( !item )
534
			return;
535
 
536
		AddDialog ^ad = gcnew AddDialog(m_pPackage, m_pMultiPackage);
537
		ad->SetRemove();
538
 
224 cycrow 539
		int pos = _WS(cli::safe_cast<System::String ^>(item->Tag)).toInt();
1 cycrow 540
		C_File *f = m_pPackage->GetFileList()->Get(pos);
541
		if ( f )
160 cycrow 542
			ad->AddFile(_US(f->name()), _US(f->dir()), f->GetFileType(), f->GetGame());
1 cycrow 543
 
544
		// remove the dat or cat file as well
545
		if ( f->GetFileType() == FILETYPE_MOD )
546
		{
225 cycrow 547
			if ( f->checkFileExt(L"cat") )
1 cycrow 548
			{
224 cycrow 549
				C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension(L"dat"), FILETYPE_MOD, L"", f->GetGame());
1 cycrow 550
				if ( datFile )
160 cycrow 551
					ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 552
			}
225 cycrow 553
			else if ( f->checkFileExt(L"dat") )
1 cycrow 554
			{
224 cycrow 555
				C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension(L"cat"), FILETYPE_MOD, L"", f->GetGame());
1 cycrow 556
				if ( datFile )
160 cycrow 557
					ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 558
			}
559
		}
560
		ad->ShowDialog(this);
561
		this->UpdateView(true);
562
	}
563
	void SpkForm::RemoveSelected()
564
	{
565
		if ( !m_pPackage )
566
			return;
567
 
568
		AddDialog ^ad = gcnew AddDialog(m_pPackage, m_pMultiPackage);
569
		ad->SetRemove();
570
 
571
		for ( int i = 0; i < this->listView1->SelectedItems->Count; i++ )
572
		{
224 cycrow 573
			int pos = _WS(cli::safe_cast<System::String ^>(this->listView1->SelectedItems[i]->Tag)).toInt();
1 cycrow 574
			C_File *f = m_pPackage->GetFileList()->Get(pos);
575
			if ( f )
160 cycrow 576
				ad->AddFile(_US(f->name()), _US(f->dir()), f->GetFileType(), f->GetGame());
1 cycrow 577
 
578
			// remove the dat or cat file as well
579
			if ( f->GetFileType() == FILETYPE_MOD )
580
			{
225 cycrow 581
				if ( f->checkFileExt(L"cat") )
1 cycrow 582
				{
224 cycrow 583
					C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension(L"dat"), FILETYPE_MOD, L"", f->GetGame());
1 cycrow 584
					if ( datFile )
160 cycrow 585
						ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 586
				}
225 cycrow 587
				else if ( f->checkFileExt(L"dat") )
1 cycrow 588
				{
224 cycrow 589
					C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension(L"cat"), FILETYPE_MOD, L"", f->GetGame());
1 cycrow 590
					if ( datFile )
160 cycrow 591
						ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 592
				}
593
			}
594
		}
595
 
596
		ad->ShowDialog(this);
597
 
598
		this->UpdateView(true);
599
	}
600
 
601
	void SpkForm::RemoveSelectedPackage()
602
	{
603
		if ( !m_pMultiPackage )
604
			return;
605
 
606
		AddDialog ^ad = gcnew AddDialog(NULL, m_pMultiPackage);
607
		ad->SetRemove();
608
 
609
		for ( int i = 0; i < this->listView2->SelectedItems->Count; i++ )
610
		{
611
			int pos = System::Convert::ToInt32(this->listView2->SelectedItems[i]->Tag);
612
			SMultiSpkFile *mp = m_pMultiPackage->GetFileList()->Get(pos);
613
			if ( mp )
191 cycrow 614
				ad->AddFile(_US(mp->sName), "", -1, 0);
1 cycrow 615
		}
616
 
617
		if ( ad->AnyFiles() )
618
		{
619
			ad->ShowDialog(this);
620
			this->UpdateView(false);
621
		}
622
	}
623
 
624
	void SpkForm::ExtractAll(System::String ^toDir)
625
	{
626
		int game = 0;
627
		if ( m_pPackage->IsMultipleGamesInPackage() ) {
628
			Creator::SelectGame ^selGame = gcnew Creator::SelectGame("Select game to extract", NULL);
629
			if ( selGame->ShowDialog(this) == Windows::Forms::DialogResult::OK ) {
630
				game = selGame->GetGame() + 1;
631
			}
632
			else
633
				return;
634
 
635
		}
636
		else if ( m_pPackage->IsAnyGameInPackage() ) {
637
			game = m_pPackage->FindFirstGameInPackage();
638
		}
639
		this->DoExtract(m_pPackage->GetFileList(), toDir, game);
640
	}
641
 
642
	void SpkForm::ExtractAllPackage(System::String ^toDir)
643
	{
644
		if ( !m_pMultiPackage )
645
			return;
646
		this->DoPackageExtract(m_pMultiPackage->GetFileList(), toDir);
647
	}
648
 
649
	System::Void SpkForm::DragEnterEvent(System::Object ^Sender, DragEventArgs ^E)
650
	{
651
		E->Effect = DragDropEffects::None;
652
 
653
		if ( !m_pPackage )
654
			return;
655
 
656
		if ( ((Form1 ^)this->MdiParent)->DragFromForm() && ((Form1 ^)this->MdiParent)->DragFromForm() != this )
657
		{
658
			if ((E->KeyState & 4) == 4 && (E->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move) 
659
				E->Effect = DragDropEffects::Move;
660
			else if ((E->KeyState & 8) == 8 && (E->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) 
661
				E->Effect = DragDropEffects::Copy;
662
			else if ((E->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move)  
663
				E->Effect = DragDropEffects::Move;
664
		}
665
		else if ( E->Data->GetDataPresent(DataFormats::FileDrop) )
666
			E->Effect = DragDropEffects::Copy;
667
	}
668
 
197 cycrow 669
	void SpkForm::DropGetDirectories(String ^dir, Utils::WStringList *list, bool packages)
1 cycrow 670
	{
671
		cli::array<String ^> ^dirList = IO::Directory::GetFileSystemEntries(dir);
672
		for ( int j = 0; j < dirList->Length; j++ )
673
		{
674
			if ( IO::DirectoryInfo(dirList[j]).Exists )
675
				this->DropGetDirectories(dirList[j], list, packages);
676
			else
677
			{
678
				if ( packages )
197 cycrow 679
					list->pushBack(_WS(dirList[j]), L"-1");
1 cycrow 680
				else
197 cycrow 681
					list->pushBack(_WS(dirList[j]), Utils::WString::Number(SPK::GetAutomaticFiletype(_WS(dirList[j]), NULL, false)));
1 cycrow 682
			}
683
		}
684
	}
685
 
686
	System::Void SpkForm::listView2_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  E)
687
	{
688
		if ( !m_pMultiPackage )
689
			return;
690
 
691
		if ( E->Data->GetDataPresent(DataFormats::FileDrop) )
692
		{
197 cycrow 693
			Utils::WStringList fileList;
1 cycrow 694
			cli::array<String ^> ^a = (cli::array<String ^> ^)E->Data->GetData(DataFormats::FileDrop, false);
695
			int i;
696
			for(i = 0; i < a->Length; i++)
697
			{
698
				if ( IO::DirectoryInfo(a[i]).Exists )
699
					this->DropGetDirectories(a[i], &fileList, true);
700
				else
197 cycrow 701
					fileList.pushBack(_WS(a[i]), L"-1");
1 cycrow 702
			}
703
 
704
			// remove invalid files
180 cycrow 705
			if ( !fileList.empty() )
1 cycrow 706
			{
707
				AddDialog ^ad = gcnew AddDialog(NULL, m_pMultiPackage);
180 cycrow 708
				for(auto itr = fileList.begin(); itr != fileList.end(); itr++)
1 cycrow 709
				{
226 cycrow 710
					if ((*itr)->data != L"-1")
1 cycrow 711
						continue;
712
 
196 cycrow 713
					Utils::WString ext = CFileIO((*itr)->str).extension();
714
					if ( !ext.Compare(L"xsp") && !ext.Compare(L"spk") )
1 cycrow 715
						continue;
716
 
180 cycrow 717
					ad->AddFile(_US((*itr)->str), "", -1, 0);
1 cycrow 718
				}
719
 
720
				ad->ShowDialog(this);
721
				this->UpdateView(false);
722
			}
723
		}
724
	}
725
 
726
	System::Void SpkForm::listView1_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^ E)
727
	{
728
		// dropping from outside
729
		if ( E->Data->GetDataPresent(DataFormats::FileDrop) )
730
		{
197 cycrow 731
			Utils::WStringList fileList;
1 cycrow 732
			cli::array<String ^> ^a = (cli::array<String ^> ^)E->Data->GetData(DataFormats::FileDrop, false);
733
			int i;
734
			for(i = 0; i < a->Length; i++)
735
			{
736
				if ( IO::DirectoryInfo(a[i]).Exists )
737
					this->DropGetDirectories(a[i], &fileList, false);
738
				else
197 cycrow 739
					fileList.pushBack(_WS(a[i]), Utils::WString::Number(SPK::GetAutomaticFiletype(_WS(a[i]), NULL, false)));
1 cycrow 740
			}
741
 
180 cycrow 742
			SPK::AssignAutomaticFiletypes(fileList);
1 cycrow 743
 
180 cycrow 744
			if ( !fileList.empty() )
1 cycrow 745
			{
746
				DropFileDialog ^dropType = gcnew DropFileDialog(&fileList);
747
				if ( dropType->ShowDialog(this) == Forms::DialogResult::OK )
748
				{
749
					AddDialog ^ad = gcnew AddDialog(m_pPackage, NULL);
180 cycrow 750
					for(auto itr = fileList.begin(); itr != fileList.end(); itr++)
1 cycrow 751
					{
197 cycrow 752
						int type = (*itr)->data.token(L" ", 1).toInt();
1 cycrow 753
						if ( type == -1 )
754
							continue;
180 cycrow 755
						ad->AddFile(_US((*itr)->str), "", type, 0);
1 cycrow 756
					}
757
					if ( ad->AnyFiles() )
758
					{
759
						ad->ShowDialog(this);
760
						this->UpdateView(false);
761
					}
762
				}
763
			}
764
			return;
765
		}
766
 
767
		bool copy = false;
768
 
769
		if ((E->KeyState & 8) == 8 && (E->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) 
770
			copy = true;
771
 
772
		((Form1 ^)this->MdiParent)->Dragged(this, copy);
773
	}
774
 
775
	System::Void SpkForm::ItemDragEvent(System::Object ^Sender, ItemDragEventArgs ^E)
776
	{
777
		if ( this->listView1->SelectedItems->Count )
778
		{
779
			ListView::SelectedListViewItemCollection^ selected = this->listView1->SelectedItems;
780
			System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
781
			ArrayList ^list = gcnew ArrayList();
782
			while ( myEnum->MoveNext() )
783
			{
784
				C_File *f = this->GetFileFromItem(safe_cast<ListViewItem ^>(myEnum->Current));
785
				((Form1 ^)this->MdiParent)->DragFile(f, this);
786
				list->Add(safe_cast<ListViewItem ^>(myEnum->Current)->Text);
787
			}
788
 
789
			DataObject ^data = gcnew DataObject(DataFormats::FileDrop, list);
790
 
791
			for ( int i = 0; i < list->Count; i++ )
792
				data->SetData(DataFormats::StringFormat, Convert::ToString(list[i]));
793
 
794
			this->listView1->DoDragDrop(data, DragDropEffects::Move | DragDropEffects::Copy);
795
		}		
796
	}
797
 
798
	System::Void SpkForm::OpenContextMenu(System::Object ^Sender, CancelEventArgs ^E)
799
	{
800
		ListViewItem ^item = this->FindSelectedItem();
801
		if ( item )
802
		{
803
			m_pListItem = item;
804
			this->ContextName->Text = System::IO::FileInfo(item->Text).Name;
805
			if ( item->ImageKey )
806
			{
807
				int key = this->listView1->LargeImageList->Images->IndexOfKey(item->ImageKey);
808
				if ( key != -1 )
809
					this->ContextName->Image = this->listView1->LargeImageList->Images[key];
810
			}
811
 
812
			if ( ((Form1 ^)this->MdiParent)->CopiedFiles()->empty() )
813
				this->ToolPaste->Enabled = false;
814
			else
815
				this->ToolPaste->Enabled = true;
816
 
817
			E->Cancel = false;
818
		}
819
		else
820
			E->Cancel = true;
821
 
822
	}
823
 
824
	ListViewItem ^SpkForm::FindSelectedItem()
825
	{
826
		Point ^mousePoint = this->listView1->PointToClient(this->contextMenuStrip1->MousePosition);
827
		return  this->listView1->GetItemAt(mousePoint->X, mousePoint->Y);
828
	}
829
 
830
	C_File *SpkForm::GetFileFromItem(ListViewItem ^item)
831
	{
832
		int num = System::Convert::ToInt32(item->Tag);
833
		return m_pPackage->GetFileList()->Get(num);
834
	}
835
 
836
	void SpkForm::CopyFile()
837
	{
838
		if ( !m_pListItem )
839
			return;
840
		C_File *f = this->GetFileFromItem(m_pListItem);
841
		if ( f )
842
		{
843
			if ( m_pMultiPackage && m_pSelectedPackage )
844
				m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL);
845
			else if ( !m_pMultiPackage )
846
				m_pPackage->ReadFileToMemory(f);
847
			else
848
				return;
849
 
850
			f = this->GetFileFromItem(m_pListItem);
851
			((Form1 ^)this->MdiParent)->CopyFile(f, true);
852
		}
853
	}
854
 
855
	void SpkForm::CutFile()
856
	{
857
		if ( !m_pListItem )
858
			return;
859
		C_File *f = this->GetFileFromItem(m_pListItem);
860
		if ( f )
861
		{
862
			if ( !this->ReadPackageFiles(false) )
863
				return;
864
			f = this->GetFileFromItem(m_pListItem);
865
			if ( f )
866
			{
867
				((Form1 ^)this->MdiParent)->CutFile(f, this, true);
868
				m_pPackage->GetFileList()->remove(f, false);
869
 
870
				this->WritePackage();
871
				this->UpdateView(true);
872
			}
873
		}
874
	}
875
 
876
	void SpkForm::DroppedFile(C_File *f)
877
	{
878
		if ( m_pPackage->GetFileList()->FindData(f) )
879
			return;
880
 
881
		this->ReadPackageFiles(false);
882
		m_pPackage->AddFile(f);
883
		this->WritePackage();
884
		this->UpdateView(true);
885
	}
886
 
887
	void SpkForm::DraggedFile(C_File *f)
888
	{
889
		if ( !m_pPackage )
890
			return;
891
 
892
		if ( !m_pPackage->GetFileList()->FindData(f) )
893
			return;
894
 
895
		this->ReadPackageFiles(false);
896
		m_pPackage->GetFileList()->remove(f, false);
897
		this->WritePackage();
898
		this->UpdateView(true);
899
	}
900
 
901
	bool SpkForm::ReadPackageFiles(bool all)
902
	{
903
		if ( m_pMultiPackage )
904
		{
905
			if ( all )
906
			{
177 cycrow 907
				if ( !m_pMultiPackage->readAllPackages(SPKREAD_ALL) )
1 cycrow 908
					return false;
909
			}
910
			else if ( m_pSelectedPackage )
911
			{
912
				if ( !m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL) )
913
					return false;
914
			}
915
		}
916
		else
917
			m_pPackage->ReadAllFilesToMemory();
918
		return true;
919
	}
920
 
921
	void SpkForm::WritePackage()
922
	{
923
		if ( m_pMultiPackage )
924
		{
177 cycrow 925
			m_pMultiPackage->readAllPackages();
1 cycrow 926
			m_pMultiPackage->UpdatedPackage(m_pPackage);
177 cycrow 927
			m_pMultiPackage->writeFile(m_pMultiPackage->filename());
1 cycrow 928
		}
929
		else
224 cycrow 930
			m_pPackage->writeFile(m_pPackage->filename());
1 cycrow 931
	}
932
 
933
	void SpkForm::PasteFiles(CLinkList<C_File> *list)
934
	{
935
		if ( m_pMultiPackage && m_pSelectedPackage )
936
			m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL);
937
		else if ( !m_pMultiPackage )
938
			m_pPackage->ReadAllFilesToMemory();
939
		else
940
			return;
941
 
942
		for ( C_File *f = list->First(); f; f= list->Next() )
943
		{
944
			// check if we copied this file already
945
			if ( m_pPackage->GetFileList()->FindData(f) )
946
				continue;
947
 
948
			m_pPackage->AddFile(f);
949
		}
950
		((Form1 ^)this->MdiParent)->PastedFiles();
951
		this->WritePackage();
952
		this->UpdateView(true);
953
	}
954
 
955
	void SpkForm::PasteFile()
956
	{
957
		if ( ((Form1 ^)this->MdiParent)->CopiedFiles()->empty() )
958
			return;
959
 
960
		this->PasteFiles(((Form1 ^)this->MdiParent)->CopiedFiles());
961
	}
962
 
963
	void SpkForm::RestoreCut(CLinkList<C_File> *files)
964
	{
965
		for ( C_File *f = files->First(); f; f = files->Next() )
966
			m_pPackage->AddFile(f);
967
		this->WritePackage();
968
		this->UpdateView(true);
969
	}
970
 
971
	void SpkForm::RunFile(ListViewItem ^item)
972
	{
973
		if ( !item )
974
			return;
975
 
976
		C_File *f = this->GetFileFromItem(item);
977
		if ( !f )
978
			return;
979
 
980
		if ( m_pMultiPackage && m_pSelectedPackage )
981
		{
982
			m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL);
983
			C_File *f = this->GetFileFromItem(item);
984
			if ( !f )
985
				return;
986
		}
987
 
988
		Diagnostics::Process ^process = gcnew Diagnostics::Process();
989
		try {
990
			process->StartInfo->WorkingDirectory = IO::Path::GetTempPath();
991
 
992
			switch ( f->GetFileType() )
993
			{
994
				case FILETYPE_SCRIPT:
995
				case FILETYPE_UNINSTALL:
224 cycrow 996
					if ( m_pPackage->extractFile(f, _WS(IO::Path::GetTempPath()), false) )
1 cycrow 997
					{
224 cycrow 998
						SPK::WriteScriptStyleSheet(_WS(IO::Path::GetTempPath()));
170 cycrow 999
						process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(f->name());
1 cycrow 1000
					}
1001
					break;
1002
 
1003
				case FILETYPE_MOD:
224 cycrow 1004
					if ( m_pPackage->extractFile(f, _WS(IO::Path::GetTempPath()), false) )
1 cycrow 1005
					{
226 cycrow 1006
						if ( f->checkFileExt(L"cat") )
1 cycrow 1007
						{
224 cycrow 1008
							C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension(L"dat"), FILETYPE_MOD, L"");
1 cycrow 1009
							if ( datFile )
1010
							{
224 cycrow 1011
								if ( m_pPackage->extractFile(datFile, _WS(IO::Path::GetTempPath()), false) )
160 cycrow 1012
									process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(f->name());
1 cycrow 1013
							}
1014
						}
226 cycrow 1015
						else if ( f->checkFileExt(L"dat") )
1 cycrow 1016
						{
224 cycrow 1017
							C_File *catFile = m_pPackage->findFile(CFileIO(f).changeFileExtension(L"cat"), FILETYPE_MOD, L"");
1 cycrow 1018
							if ( catFile )
1019
							{
224 cycrow 1020
								if ( m_pPackage->extractFile(catFile, _WS(IO::Path::GetTempPath()), false) )
160 cycrow 1021
									process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(catFile->name());
1 cycrow 1022
							}
1023
						}
1024
					}
1025
					break;
1026
 
1027
				case FILETYPE_MAP:
1028
					break;
1029
 
1030
				default:
224 cycrow 1031
					if ( m_pPackage->extractFile(f, _WS(IO::Path::GetTempPath()), false) )
160 cycrow 1032
						process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(f->name());
1 cycrow 1033
			}
1034
 
1035
			if ( process->StartInfo->FileName && process->StartInfo->FileName->Length )
1036
			{
1037
				process->StartInfo->CreateNoWindow = true;
1038
				process->Start();
1039
			}
1040
		}
1041
		catch (System::Exception ^E) 
1042
		{
170 cycrow 1043
			MessageBox::Show(this, "Error opening file:\n" + _US(f->name()) + "\n\nError: " + E->GetType()->Name, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
1 cycrow 1044
		}
1045
	}
1046
}