Subversion Repositories spk

Rev

Rev 175 | Rev 178 | 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() )
117
			p->setFilename(_S(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())
133
			p->setFilename(_S(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();
158 cycrow 149
				Utils::String sIconFile = _S(IO::Path::GetTempPath()) + "\\" + CFileIO(package->sName).baseName() + "." + package->pFile->iconExt();
175 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
					{
155
						String ^file = SystemStringFromCyString(package->sName);
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
		{
262
			CyString sF = CyStringFromSystemString(filename);
160 cycrow 263
			if ( CFileIO(sF).isFileExtension("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)));
1 cycrow 291
		item->SubItems->Add(SystemStringFromCyString(file->GetUncompressedSizeString()));
292
		item->SubItems->Add(SystemStringFromCyString(file->GetFileTypeString()));
293
 
294
		item->SubItems->Add(SystemStringFromCyString(file->GetCreationTimeString()));
295
 
128 cycrow 296
		item->Tag = _US(Utils::String::Number(file->GetPos()));
1 cycrow 297
 
128 cycrow 298
		item->ImageKey = _US(Utils::String::Number(file->GetFileType()));
1 cycrow 299
 
128 cycrow 300
		if ( !file->game() || file->game() == GAME_ALLNEW) {
1 cycrow 301
			item->SubItems->Add("All Games");
302
		}
303
		else {
128 cycrow 304
			if (file->game() & GAME_ALLNEW)
305
			{
306
				Utils::String sGames = "";
307
				for (int i = 0; i < 31; ++i)
308
				{
309
					if (file->game() & (1 << i))
310
					{
311
						Utils::String sGame;
312
						SGameExe *exe = _pPackages->GetGameExe()->GetGame(i - 1);
313
						sGame = (exe) ? exe->sName : ("Game: " + Utils::String::Number(i));
314
 
315
						if (sGames.empty())
316
							sGames = sGame;
317
						else
130 cycrow 318
							sGames = sGames + ", " + sGame;
128 cycrow 319
					}
320
				}
321
				if(sGames.empty())
322
					item->SubItems->Add(_US("All Games"));
323
				else
324
					item->SubItems->Add(_US(sGames));
1 cycrow 325
			}
128 cycrow 326
			else
327
			{
328
				SGameExe *exe = _pPackages->GetGameExe()->GetGame(file->game() - 1);
329
				if (exe) {
330
					item->SubItems->Add(_US(exe->sName));
331
				}
332
				else {
333
					item->SubItems->Add("Game: " + file->game());
334
				}
1 cycrow 335
			}
336
		}
337
 
338
		this->listView1->Items->Add(item);
339
	}
340
 
341
	void SpkForm::AddMultiFile(SMultiSpkFile *file)
342
	{
343
		ListViewItem ^item = gcnew ListViewItem(SystemStringFromCyString(file->sName));
344
		if ( file->iType == TYPE_XSP )
345
			item->SubItems->Add("Ship");
346
		else
347
			item->SubItems->Add("Package");
348
		item->SubItems->Add(SystemStringFromCyString(SPK::GetSizeString(file->lSize)));
349
 
350
 
128 cycrow 351
		item->Tag = _US(Utils::String::Number(file->iPos));
1 cycrow 352
 
353
		if ( this->imageList1->Images->IndexOfKey(SystemStringFromCyString(file->sName)) == -1 )
354
			item->ImageKey = "package";
355
		else
356
			item->ImageKey = SystemStringFromCyString(file->sName);
357
 
358
		this->listView2->Items->Add(item);
359
	}
360
 
361
	System::Void SpkForm::Event_Activated(System::Object^  sender, System::EventArgs^  e)
362
	{
363
		m_pTabCtrl->SelectedTab = m_pTabPage;
364
		this->Text = m_pTabPage->Text;
365
 
366
		((Form1 ^)this->MdiParent)->UpdateDisplay();
367
 
368
		this->SelectedCheck();
369
	}
370
 
371
	System::Void SpkForm::Event_Closing(System::Object^  sender, CancelEventArgs^  e)
372
	{
373
		delete m_pPackage;
374
		delete m_pTabPage;
375
 
376
		m_sFilename = "";
377
 
378
		((Form1 ^)this->MdiParent)->RemoveCopied(true);
379
 
380
		((Form1 ^)this->MdiParent)->UpdateDisplay();
381
		((Form1 ^)this->MdiParent)->SelectedFile(false);
382
	}
383
 
384
	void SpkForm::SelectedCheck()
385
	{
386
		if ( m_pMultiPackage )
387
		{
388
			((Form1 ^)this->MdiParent)->SelectedPackage(this->IsSelected(), this->IsPackageSelected());
389
			if ( this->IsPackageSelected() )
390
			{
391
				Point ^mousePoint = this->listView2->PointToClient(this->listView2->MousePosition);
392
				ListViewItem ^item = this->listView2->GetItemAt(mousePoint->X, mousePoint->Y);
393
				if ( item )
394
				{
395
					int num = System::Convert::ToInt32(item->Tag);
396
					m_pSelectedPackage = m_pMultiPackage->GetFileList()->Get(num);
397
					if ( m_pSelectedPackage )
398
					{
399
						m_pPackage = m_pSelectedPackage->pFile;
400
						this->UpdateView(true);
401
					}
402
				}
403
			}
404
			else
405
			{
406
				m_pPackage = NULL;
407
				m_pSelectedPackage = NULL;
408
				this->UpdateView(true);
409
			}
410
		}
411
		else
412
			((Form1 ^)this->MdiParent)->SelectedFile(this->IsSelected());
413
	}
414
 
415
	bool SpkForm::IsSelected()
416
	{
417
		if ( this->listView1->SelectedItems->Count )
418
			return true;
419
		return false;
420
	}
421
 
422
	bool SpkForm::IsPackageSelected()
423
	{
424
		if ( this->listView2->SelectedItems->Count )
425
			return true;
426
		return false;
427
	}
428
 
429
	void SpkForm::DoExtract(CLinkList<C_File> *list, System::String ^toDir, int game)
430
	{
431
		if ( !toDir )
432
			return;
433
 
434
 
435
		// read the spk file to memory before trying to extract its files
436
		if ( !this->ReadMultiPackage() )
437
			return;
438
 
128 cycrow 439
		ExtractDialog ^ed = gcnew ExtractDialog(list, toDir, m_pPackage, game, _pPackages);
1 cycrow 440
		ed->ShowDialog((Form1 ^)this->MdiParent);
441
 
442
		if ( ed->OK() && !ed->Canceled() && ed->ExtractedCount() )
443
			MessageBox::Show(this, "Files have been extracting successfully to:\n" + toDir, "Extacted", MessageBoxButtons::OK, MessageBoxIcon::Information);
444
	}
445
 
446
	bool SpkForm::ReadSelectedMultiPackage()
447
	{
448
		if ( m_pMultiPackage && m_pSelectedPackage )
449
		{
450
			if ( !m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL) )
451
				return false;
452
		}
453
 
454
		return true;
455
	}
456
	bool SpkForm::ReadMultiPackage()
457
	{
458
		if ( m_pMultiPackage )
459
		{
177 cycrow 460
			if ( !m_pMultiPackage->readAllPackages(SPKREAD_ALL) )
1 cycrow 461
				return false;
462
		}
463
 
464
		return true;
465
	}
466
	void SpkForm::DoPackageExtract(CLinkList<SMultiSpkFile> *list, System::String ^toDir)
467
	{
468
		if ( !toDir )
469
			return;
470
 
128 cycrow 471
		ExtractDialog ^ed = gcnew ExtractDialog(list, toDir, m_pMultiPackage, _pPackages);
1 cycrow 472
		ed->ShowDialog((Form1 ^)this->MdiParent);
473
 
474
		if ( ed->OK() && !ed->Canceled() )
475
			MessageBox::Show(this, "Files have been extracting successfully to:\n" + toDir, "Extacted", MessageBoxButtons::OK, MessageBoxIcon::Information);
476
	}
477
 
478
	System::String ^SpkForm::GetExtractDir(bool package)
479
	{
480
		FolderBrowserDialog ^fbd = gcnew FolderBrowserDialog;
481
		if ( package )
482
			fbd->Description = "Select the path to extract packages to";
483
		else
484
			fbd->Description = "Select the path to extract files to";
485
		if ( fbd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
486
			return fbd->SelectedPath;
487
 
488
		return nullptr;
489
	}
490
 
491
	void SpkForm::ExtractSelected(System::String ^toDir)
492
	{
493
		if ( !m_pPackage )
494
			return;
495
 
496
		CyString sToDir = CyStringFromSystemString(toDir);
497
 
498
		CLinkList<C_File> list;
499
 
500
		for ( int i = 0; i < this->listView1->SelectedItems->Count; i++ )
501
		{
502
			int pos = CyStringFromSystemString(cli::safe_cast<System::String ^>(this->listView1->SelectedItems[i]->Tag)).ToInt();
503
			C_File *f = m_pPackage->GetFileList()->Get(pos);
504
			list.push_back(f);
505
		}
506
 
507
		this->DoExtract(&list, toDir, -1);
508
		list.clear();
509
	}
510
 
511
	void SpkForm::ExtractSelectedPackage(System::String ^toDir)
512
	{
513
		if ( !m_pMultiPackage )
514
			return;
515
 
516
		CyString sToDir = CyStringFromSystemString(toDir);
517
 
518
		CLinkList<SMultiSpkFile> list;
519
 
520
		for ( int i = 0; i < this->listView2->SelectedItems->Count; i++ )
521
		{
522
			int pos = CyStringFromSystemString(cli::safe_cast<System::String ^>(this->listView2->SelectedItems[i]->Tag)).ToInt();
523
			SMultiSpkFile *mp = m_pMultiPackage->GetFileList()->Get(pos);
524
			if ( mp )
525
				list.push_back(mp);
526
		}
527
 
528
		this->DoPackageExtract(&list, toDir);
529
		list.clear();
530
	}
531
 
532
	void SpkForm::RemoveItem(ListViewItem ^item)
533
	{
534
		if ( !item )
535
			return;
536
 
537
		AddDialog ^ad = gcnew AddDialog(m_pPackage, m_pMultiPackage);
538
		ad->SetRemove();
539
 
160 cycrow 540
		int pos = _S(cli::safe_cast<System::String ^>(item->Tag)).toInt();
1 cycrow 541
		C_File *f = m_pPackage->GetFileList()->Get(pos);
542
		if ( f )
160 cycrow 543
			ad->AddFile(_US(f->name()), _US(f->dir()), f->GetFileType(), f->GetGame());
1 cycrow 544
 
545
		// remove the dat or cat file as well
546
		if ( f->GetFileType() == FILETYPE_MOD )
547
		{
548
			if ( f->CheckFileExt("cat") )
549
			{
170 cycrow 550
				C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension("dat"), FILETYPE_MOD, "", f->GetGame());
1 cycrow 551
				if ( datFile )
160 cycrow 552
					ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 553
			}
554
			else if ( f->CheckFileExt("dat") )
555
			{
170 cycrow 556
				C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension("cat"), FILETYPE_MOD, "", f->GetGame());
1 cycrow 557
				if ( datFile )
160 cycrow 558
					ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 559
			}
560
		}
561
		ad->ShowDialog(this);
562
		this->UpdateView(true);
563
	}
564
	void SpkForm::RemoveSelected()
565
	{
566
		if ( !m_pPackage )
567
			return;
568
 
569
		AddDialog ^ad = gcnew AddDialog(m_pPackage, m_pMultiPackage);
570
		ad->SetRemove();
571
 
572
		for ( int i = 0; i < this->listView1->SelectedItems->Count; i++ )
573
		{
160 cycrow 574
			int pos = _S(cli::safe_cast<System::String ^>(this->listView1->SelectedItems[i]->Tag)).toInt();
1 cycrow 575
			C_File *f = m_pPackage->GetFileList()->Get(pos);
576
			if ( f )
160 cycrow 577
				ad->AddFile(_US(f->name()), _US(f->dir()), f->GetFileType(), f->GetGame());
1 cycrow 578
 
579
			// remove the dat or cat file as well
580
			if ( f->GetFileType() == FILETYPE_MOD )
581
			{
582
				if ( f->CheckFileExt("cat") )
583
				{
170 cycrow 584
					C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension("dat"), FILETYPE_MOD, "", f->GetGame());
1 cycrow 585
					if ( datFile )
160 cycrow 586
						ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 587
				}
588
				else if ( f->CheckFileExt("dat") )
589
				{
170 cycrow 590
					C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension("cat"), FILETYPE_MOD, "", f->GetGame());
1 cycrow 591
					if ( datFile )
160 cycrow 592
						ad->AddFile(_US(datFile->name()), _US(datFile->dir()), datFile->GetFileType(), datFile->GetGame());
1 cycrow 593
				}
594
			}
595
		}
596
 
597
		ad->ShowDialog(this);
598
 
599
		this->UpdateView(true);
600
	}
601
 
602
	void SpkForm::RemoveSelectedPackage()
603
	{
604
		if ( !m_pMultiPackage )
605
			return;
606
 
607
		AddDialog ^ad = gcnew AddDialog(NULL, m_pMultiPackage);
608
		ad->SetRemove();
609
 
610
		for ( int i = 0; i < this->listView2->SelectedItems->Count; i++ )
611
		{
612
			int pos = System::Convert::ToInt32(this->listView2->SelectedItems[i]->Tag);
613
			SMultiSpkFile *mp = m_pMultiPackage->GetFileList()->Get(pos);
614
			if ( mp )
615
				ad->AddFile(SystemStringFromCyString(mp->sName), "", -1, 0);
616
		}
617
 
618
		if ( ad->AnyFiles() )
619
		{
620
			ad->ShowDialog(this);
621
			this->UpdateView(false);
622
		}
623
	}
624
 
625
	void SpkForm::ExtractAll(System::String ^toDir)
626
	{
627
		int game = 0;
628
		if ( m_pPackage->IsMultipleGamesInPackage() ) {
629
			Creator::SelectGame ^selGame = gcnew Creator::SelectGame("Select game to extract", NULL);
630
			if ( selGame->ShowDialog(this) == Windows::Forms::DialogResult::OK ) {
631
				game = selGame->GetGame() + 1;
632
			}
633
			else
634
				return;
635
 
636
		}
637
		else if ( m_pPackage->IsAnyGameInPackage() ) {
638
			game = m_pPackage->FindFirstGameInPackage();
639
		}
640
		this->DoExtract(m_pPackage->GetFileList(), toDir, game);
641
	}
642
 
643
	void SpkForm::ExtractAllPackage(System::String ^toDir)
644
	{
645
		if ( !m_pMultiPackage )
646
			return;
647
		this->DoPackageExtract(m_pMultiPackage->GetFileList(), toDir);
648
	}
649
 
650
	System::Void SpkForm::DragEnterEvent(System::Object ^Sender, DragEventArgs ^E)
651
	{
652
		E->Effect = DragDropEffects::None;
653
 
654
		if ( !m_pPackage )
655
			return;
656
 
657
		if ( ((Form1 ^)this->MdiParent)->DragFromForm() && ((Form1 ^)this->MdiParent)->DragFromForm() != this )
658
		{
659
			if ((E->KeyState & 4) == 4 && (E->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move) 
660
				E->Effect = DragDropEffects::Move;
661
			else if ((E->KeyState & 8) == 8 && (E->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) 
662
				E->Effect = DragDropEffects::Copy;
663
			else if ((E->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move)  
664
				E->Effect = DragDropEffects::Move;
665
		}
666
		else if ( E->Data->GetDataPresent(DataFormats::FileDrop) )
667
			E->Effect = DragDropEffects::Copy;
668
	}
669
 
670
	void SpkForm::DropGetDirectories(String ^dir, CyStringList *list, bool packages)
671
	{
672
		cli::array<String ^> ^dirList = IO::Directory::GetFileSystemEntries(dir);
673
		for ( int j = 0; j < dirList->Length; j++ )
674
		{
675
			if ( IO::DirectoryInfo(dirList[j]).Exists )
676
				this->DropGetDirectories(dirList[j], list, packages);
677
			else
678
			{
679
				if ( packages )
680
					list->PushBack(CyStringFromSystemString(dirList[j]), "-1");
681
				else
170 cycrow 682
					list->PushBack(CyStringFromSystemString(dirList[j]), CyString::Number(SPK::GetAutomaticFiletype(_S(dirList[j]), NULL, false)));
1 cycrow 683
			}
684
		}
685
	}
686
 
687
	System::Void SpkForm::listView2_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  E)
688
	{
689
		if ( !m_pMultiPackage )
690
			return;
691
 
692
		if ( E->Data->GetDataPresent(DataFormats::FileDrop) )
693
		{
694
			CyStringList fileList;
695
			cli::array<String ^> ^a = (cli::array<String ^> ^)E->Data->GetData(DataFormats::FileDrop, false);
696
			int i;
697
			for(i = 0; i < a->Length; i++)
698
			{
699
				if ( IO::DirectoryInfo(a[i]).Exists )
700
					this->DropGetDirectories(a[i], &fileList, true);
701
				else
702
					fileList.PushBack(CyStringFromSystemString(a[i]), "-1");
703
			}
704
 
705
			// remove invalid files
706
			if ( !fileList.Empty() )
707
			{
708
				AddDialog ^ad = gcnew AddDialog(NULL, m_pMultiPackage);
709
				for ( SStringList *str = fileList.Head(); str; str = str->next )
710
				{
711
					if ( str->data != "-1" )
712
						continue;
713
 
160 cycrow 714
					Utils::String ext = CFileIO(str->str).extension();
1 cycrow 715
					if ( !ext.Compare("xsp") && !ext.Compare("spk") )
716
						continue;
717
 
718
					ad->AddFile(SystemStringFromCyString(str->str), "", -1, 0);
719
				}
720
 
721
				ad->ShowDialog(this);
722
				this->UpdateView(false);
723
			}
724
		}
725
	}
726
 
727
	System::Void SpkForm::listView1_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^ E)
728
	{
729
		// dropping from outside
730
		if ( E->Data->GetDataPresent(DataFormats::FileDrop) )
731
		{
732
			CyStringList fileList;
733
			cli::array<String ^> ^a = (cli::array<String ^> ^)E->Data->GetData(DataFormats::FileDrop, false);
734
			int i;
735
			for(i = 0; i < a->Length; i++)
736
			{
737
				if ( IO::DirectoryInfo(a[i]).Exists )
738
					this->DropGetDirectories(a[i], &fileList, false);
739
				else
170 cycrow 740
					fileList.PushBack(CyStringFromSystemString(a[i]), CyString::Number(SPK::GetAutomaticFiletype(_S(a[i]), NULL, false)));
1 cycrow 741
			}
742
 
743
			SPK::AssignAutomaticFiletypes(&fileList);
744
 
745
			if ( !fileList.Empty() )
746
			{
747
				DropFileDialog ^dropType = gcnew DropFileDialog(&fileList);
748
				if ( dropType->ShowDialog(this) == Forms::DialogResult::OK )
749
				{
750
					AddDialog ^ad = gcnew AddDialog(m_pPackage, NULL);
751
					for ( SStringList *str = fileList.Head(); str; str = str->next )
752
					{
753
						int type = str->data.GetToken(" ", 1, 1).ToInt();
754
						if ( type == -1 )
755
							continue;
756
						ad->AddFile(SystemStringFromCyString(str->str), "", type, 0);
757
					}
758
					if ( ad->AnyFiles() )
759
					{
760
						ad->ShowDialog(this);
761
						this->UpdateView(false);
762
					}
763
				}
764
			}
765
			return;
766
		}
767
 
768
		bool copy = false;
769
 
770
		if ((E->KeyState & 8) == 8 && (E->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) 
771
			copy = true;
772
 
773
		((Form1 ^)this->MdiParent)->Dragged(this, copy);
774
	}
775
 
776
	System::Void SpkForm::ItemDragEvent(System::Object ^Sender, ItemDragEventArgs ^E)
777
	{
778
		if ( this->listView1->SelectedItems->Count )
779
		{
780
			ListView::SelectedListViewItemCollection^ selected = this->listView1->SelectedItems;
781
			System::Collections::IEnumerator^ myEnum = selected->GetEnumerator();
782
			ArrayList ^list = gcnew ArrayList();
783
			while ( myEnum->MoveNext() )
784
			{
785
				C_File *f = this->GetFileFromItem(safe_cast<ListViewItem ^>(myEnum->Current));
786
				((Form1 ^)this->MdiParent)->DragFile(f, this);
787
				list->Add(safe_cast<ListViewItem ^>(myEnum->Current)->Text);
788
			}
789
 
790
			DataObject ^data = gcnew DataObject(DataFormats::FileDrop, list);
791
 
792
			for ( int i = 0; i < list->Count; i++ )
793
				data->SetData(DataFormats::StringFormat, Convert::ToString(list[i]));
794
 
795
			this->listView1->DoDragDrop(data, DragDropEffects::Move | DragDropEffects::Copy);
796
		}		
797
	}
798
 
799
	System::Void SpkForm::OpenContextMenu(System::Object ^Sender, CancelEventArgs ^E)
800
	{
801
		ListViewItem ^item = this->FindSelectedItem();
802
		if ( item )
803
		{
804
			m_pListItem = item;
805
			this->ContextName->Text = System::IO::FileInfo(item->Text).Name;
806
			if ( item->ImageKey )
807
			{
808
				int key = this->listView1->LargeImageList->Images->IndexOfKey(item->ImageKey);
809
				if ( key != -1 )
810
					this->ContextName->Image = this->listView1->LargeImageList->Images[key];
811
			}
812
 
813
			if ( ((Form1 ^)this->MdiParent)->CopiedFiles()->empty() )
814
				this->ToolPaste->Enabled = false;
815
			else
816
				this->ToolPaste->Enabled = true;
817
 
818
			E->Cancel = false;
819
		}
820
		else
821
			E->Cancel = true;
822
 
823
	}
824
 
825
	ListViewItem ^SpkForm::FindSelectedItem()
826
	{
827
		Point ^mousePoint = this->listView1->PointToClient(this->contextMenuStrip1->MousePosition);
828
		return  this->listView1->GetItemAt(mousePoint->X, mousePoint->Y);
829
	}
830
 
831
	C_File *SpkForm::GetFileFromItem(ListViewItem ^item)
832
	{
833
		int num = System::Convert::ToInt32(item->Tag);
834
		return m_pPackage->GetFileList()->Get(num);
835
	}
836
 
837
	void SpkForm::CopyFile()
838
	{
839
		if ( !m_pListItem )
840
			return;
841
		C_File *f = this->GetFileFromItem(m_pListItem);
842
		if ( f )
843
		{
844
			if ( m_pMultiPackage && m_pSelectedPackage )
845
				m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL);
846
			else if ( !m_pMultiPackage )
847
				m_pPackage->ReadFileToMemory(f);
848
			else
849
				return;
850
 
851
			f = this->GetFileFromItem(m_pListItem);
852
			((Form1 ^)this->MdiParent)->CopyFile(f, true);
853
		}
854
	}
855
 
856
	void SpkForm::CutFile()
857
	{
858
		if ( !m_pListItem )
859
			return;
860
		C_File *f = this->GetFileFromItem(m_pListItem);
861
		if ( f )
862
		{
863
			if ( !this->ReadPackageFiles(false) )
864
				return;
865
			f = this->GetFileFromItem(m_pListItem);
866
			if ( f )
867
			{
868
				((Form1 ^)this->MdiParent)->CutFile(f, this, true);
869
				m_pPackage->GetFileList()->remove(f, false);
870
 
871
				this->WritePackage();
872
				this->UpdateView(true);
873
			}
874
		}
875
	}
876
 
877
	void SpkForm::DroppedFile(C_File *f)
878
	{
879
		if ( m_pPackage->GetFileList()->FindData(f) )
880
			return;
881
 
882
		this->ReadPackageFiles(false);
883
		m_pPackage->AddFile(f);
884
		this->WritePackage();
885
		this->UpdateView(true);
886
	}
887
 
888
	void SpkForm::DraggedFile(C_File *f)
889
	{
890
		if ( !m_pPackage )
891
			return;
892
 
893
		if ( !m_pPackage->GetFileList()->FindData(f) )
894
			return;
895
 
896
		this->ReadPackageFiles(false);
897
		m_pPackage->GetFileList()->remove(f, false);
898
		this->WritePackage();
899
		this->UpdateView(true);
900
	}
901
 
902
	bool SpkForm::ReadPackageFiles(bool all)
903
	{
904
		if ( m_pMultiPackage )
905
		{
906
			if ( all )
907
			{
177 cycrow 908
				if ( !m_pMultiPackage->readAllPackages(SPKREAD_ALL) )
1 cycrow 909
					return false;
910
			}
911
			else if ( m_pSelectedPackage )
912
			{
913
				if ( !m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL) )
914
					return false;
915
			}
916
		}
917
		else
918
			m_pPackage->ReadAllFilesToMemory();
919
		return true;
920
	}
921
 
922
	void SpkForm::WritePackage()
923
	{
924
		if ( m_pMultiPackage )
925
		{
177 cycrow 926
			m_pMultiPackage->readAllPackages();
1 cycrow 927
			m_pMultiPackage->UpdatedPackage(m_pPackage);
177 cycrow 928
			m_pMultiPackage->writeFile(m_pMultiPackage->filename());
1 cycrow 929
		}
930
		else
175 cycrow 931
			m_pPackage->writeFile(m_pPackage->filename());
1 cycrow 932
	}
933
 
934
	void SpkForm::PasteFiles(CLinkList<C_File> *list)
935
	{
936
		if ( m_pMultiPackage && m_pSelectedPackage )
937
			m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL);
938
		else if ( !m_pMultiPackage )
939
			m_pPackage->ReadAllFilesToMemory();
940
		else
941
			return;
942
 
943
		for ( C_File *f = list->First(); f; f= list->Next() )
944
		{
945
			// check if we copied this file already
946
			if ( m_pPackage->GetFileList()->FindData(f) )
947
				continue;
948
 
949
			m_pPackage->AddFile(f);
950
		}
951
		((Form1 ^)this->MdiParent)->PastedFiles();
952
		this->WritePackage();
953
		this->UpdateView(true);
954
	}
955
 
956
	void SpkForm::PasteFile()
957
	{
958
		if ( ((Form1 ^)this->MdiParent)->CopiedFiles()->empty() )
959
			return;
960
 
961
		this->PasteFiles(((Form1 ^)this->MdiParent)->CopiedFiles());
962
	}
963
 
964
	void SpkForm::RestoreCut(CLinkList<C_File> *files)
965
	{
966
		for ( C_File *f = files->First(); f; f = files->Next() )
967
			m_pPackage->AddFile(f);
968
		this->WritePackage();
969
		this->UpdateView(true);
970
	}
971
 
972
	void SpkForm::RunFile(ListViewItem ^item)
973
	{
974
		if ( !item )
975
			return;
976
 
977
		C_File *f = this->GetFileFromItem(item);
978
		if ( !f )
979
			return;
980
 
981
		if ( m_pMultiPackage && m_pSelectedPackage )
982
		{
983
			m_pMultiPackage->ReadSpk(m_pSelectedPackage, SPKREAD_ALL);
984
			C_File *f = this->GetFileFromItem(item);
985
			if ( !f )
986
				return;
987
		}
988
 
989
		Diagnostics::Process ^process = gcnew Diagnostics::Process();
990
		try {
991
			process->StartInfo->WorkingDirectory = IO::Path::GetTempPath();
992
 
993
			switch ( f->GetFileType() )
994
			{
995
				case FILETYPE_SCRIPT:
996
				case FILETYPE_UNINSTALL:
175 cycrow 997
					if ( m_pPackage->extractFile(f, _S(IO::Path::GetTempPath()), false) )
1 cycrow 998
					{
170 cycrow 999
						SPK::WriteScriptStyleSheet(_S(IO::Path::GetTempPath()));
1000
						process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(f->name());
1 cycrow 1001
					}
1002
					break;
1003
 
1004
				case FILETYPE_MOD:
175 cycrow 1005
					if ( m_pPackage->extractFile(f, _S(IO::Path::GetTempPath()), false) )
1 cycrow 1006
					{
1007
						if ( f->CheckFileExt("cat") )
1008
						{
170 cycrow 1009
							C_File *datFile = m_pPackage->findFile(CFileIO(f).changeFileExtension("dat"), FILETYPE_MOD, "");
1 cycrow 1010
							if ( datFile )
1011
							{
175 cycrow 1012
								if ( m_pPackage->extractFile(datFile, _S(IO::Path::GetTempPath()), false) )
160 cycrow 1013
									process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(f->name());
1 cycrow 1014
							}
1015
						}
1016
						else if ( f->CheckFileExt("dat") )
1017
						{
170 cycrow 1018
							C_File *catFile = m_pPackage->findFile(CFileIO(f).changeFileExtension("cat"), FILETYPE_MOD, "");
1 cycrow 1019
							if ( catFile )
1020
							{
175 cycrow 1021
								if ( m_pPackage->extractFile(catFile, _S(IO::Path::GetTempPath()), false) )
160 cycrow 1022
									process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(catFile->name());
1 cycrow 1023
							}
1024
						}
1025
					}
1026
					break;
1027
 
1028
				case FILETYPE_MAP:
1029
					break;
1030
 
1031
				default:
175 cycrow 1032
					if ( m_pPackage->extractFile(f, _S(IO::Path::GetTempPath()), false) )
160 cycrow 1033
						process->StartInfo->FileName = IO::Path::GetTempPath() + "\\" + _US(f->name());
1 cycrow 1034
			}
1035
 
1036
			if ( process->StartInfo->FileName && process->StartInfo->FileName->Length )
1037
			{
1038
				process->StartInfo->CreateNoWindow = true;
1039
				process->Start();
1040
			}
1041
		}
1042
		catch (System::Exception ^E) 
1043
		{
170 cycrow 1044
			MessageBox::Show(this, "Error opening file:\n" + _US(f->name()) + "\n\nError: " + E->GetType()->Name, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
1 cycrow 1045
		}
1046
	}
1047
}