Subversion Repositories spk

Rev

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