MainWindow.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #region License information
  2. // ----------------------------------------------------------------------------
  3. //
  4. // Eq2VpkTool - A tool to extract Everquest II VPK files
  5. // Blaz (blaz@blazlabs.com)
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. //
  21. // ( The full text of the license can be found in the License.txt file )
  22. //
  23. // ----------------------------------------------------------------------------
  24. #endregion
  25. #region Using directives
  26. using System;
  27. using System.IO;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Windows.Forms;
  33. using System.Threading;
  34. using System.Runtime.InteropServices;
  35. using Eq2FileSystem = Everquest2.IO.FileSystem;
  36. using Eq2FileSystemInfo = Everquest2.IO.FileSystemInfo;
  37. using Eq2FileInfo = Everquest2.IO.FileInfo;
  38. using Eq2DirectoryInfo = Everquest2.IO.DirectoryInfo;
  39. #endregion
  40. namespace Eq2VpkTool
  41. {
  42. public partial class MainWindow : Form
  43. {
  44. #region Methods
  45. #region Constructors
  46. public MainWindow()
  47. {
  48. InitializeComponent();
  49. Text = ApplicationName;
  50. if (File.Exists(ConfigurationFileName))
  51. {
  52. try { Configuration.Instance.Load(new FileStream(ConfigurationFileName, FileMode.Open, FileAccess.Read)); }
  53. catch (Exception e) { MessageBox.Show("Error loading configuration file '" + ConfigurationFileName + "'\n\n" + e); }
  54. }
  55. loadingUpdateTimer = new System.Windows.Forms.Timer();
  56. loadingUpdateTimer.Interval = 200;
  57. loadingUpdateTimer.Tick += OnLoadingUpdateTimerTick;
  58. fileSystemViewController = new FileSystemViewController();
  59. extractionManager = new ExtractionManager();
  60. extractToolStripContextMenuItem.Click += extractToolStripContextMenuItem_Click;
  61. extractWithPathInformationToolStripContextMenuItem.Click += extractWithPathInformationToolStripContextMenuItem_Click;
  62. decryptToolStripContextMenuItem.Click += decryptToolStripContextMenuItem_Click;
  63. }
  64. #endregion
  65. private void mainHelpWebsiteMenuItem_Click(object sender, EventArgs e)
  66. {
  67. System.Diagnostics.Process.Start(ApplicationWebsite);
  68. }
  69. private void mainFileExitMenuItem_Click(object sender, EventArgs e)
  70. {
  71. Close();
  72. }
  73. private void mainFileOpenMenuItem_Click(object sender, EventArgs e)
  74. {
  75. if (openFileDialog.ShowDialog() == DialogResult.OK)
  76. {
  77. loadingUpdateTimer.Enabled = false;
  78. extractionManager.Close();
  79. fileSystemViewController.Close();
  80. extractionProgress = null;
  81. fileSystemViewController.Open(openFileDialog.FileName, directoryTreeView, fileListView);
  82. loadingUpdateTimer.Enabled = true;
  83. }
  84. }
  85. private void extractToolStripMenuItem_Click(object sender, EventArgs e)
  86. {
  87. if (extractFolderDialog.ShowDialog() == DialogResult.OK)
  88. {
  89. ExtractFromMainMenu(extractFolderDialog.SelectedPath, false);
  90. }
  91. }
  92. private void extractWithPathInformationToolStripMenuItem_Click(object sender, EventArgs e)
  93. {
  94. if (extractFolderDialog.ShowDialog() == DialogResult.OK)
  95. {
  96. ExtractFromMainMenu(extractFolderDialog.SelectedPath, true);
  97. }
  98. }
  99. private void extractToolStripContextMenuItem_Click(object sender, EventArgs e)
  100. {
  101. if (extractFolderDialog.ShowDialog() == DialogResult.OK)
  102. {
  103. ExtractFromContextMenu(extractFolderDialog.SelectedPath, false);
  104. }
  105. }
  106. private void extractWithPathInformationToolStripContextMenuItem_Click(object sender, EventArgs e)
  107. {
  108. if (extractFolderDialog.ShowDialog() == DialogResult.OK)
  109. {
  110. ExtractFromContextMenu(extractFolderDialog.SelectedPath, true);
  111. }
  112. }
  113. private void decryptToolStripContextMenuItem_Click(object sender, EventArgs e)
  114. {
  115. if (extractFolderDialog.ShowDialog() == DialogResult.OK)
  116. {
  117. DecryptFromContextMenu(extractFolderDialog.SelectedPath, false);
  118. }
  119. }
  120. private void decryptWithPathInformationToolStripContextMenuItem_Click(object sender, EventArgs e)
  121. {
  122. if (extractFolderDialog.ShowDialog() == DialogResult.OK)
  123. {
  124. DecryptFromContextMenu(extractFolderDialog.SelectedPath, true);
  125. }
  126. }
  127. private void DecryptFromContextMenu(string path, bool withPathInfo)
  128. {
  129. if (fileListView.SelectedItems.Count != 1) return;
  130. Decrypt(path, withPathInfo, fileListView.SelectedItems[0].Tag as Eq2FileInfo);
  131. }
  132. private void Decrypt(string outputPath, bool withPathInfo, Eq2FileInfo file)
  133. {
  134. if (!TextureDecryptor.CanDecrypt(file)) return;
  135. // Make sure the path ends with a directory separator char.
  136. if (outputPath[outputPath.Length - 1] != Path.DirectorySeparatorChar)
  137. {
  138. outputPath += Path.DirectorySeparatorChar;
  139. }
  140. try
  141. {
  142. using (FileStream stream = new FileStream(outputPath + file.Name, FileMode.Create, FileAccess.Write))
  143. {
  144. byte[] decryptedData = TextureDecryptor.Decrypt(file);
  145. stream.Write(decryptedData, 0, decryptedData.Length);
  146. }
  147. statusMainPanel.Text = file.Name + " decrypted successfully.";
  148. }
  149. catch (Exception e)
  150. {
  151. MessageBox.Show("Error saving decrypted file:\n\n" + e, "Error");
  152. }
  153. }
  154. private void ExtractFromMainMenu(string path, bool withPathInfo)
  155. {
  156. if (directoryTreeView.Focused)
  157. {
  158. ExtractTreeViewSelection(path, withPathInfo);
  159. }
  160. else if (fileListView.Focused)
  161. {
  162. ExtractListViewSelection(path, withPathInfo);
  163. }
  164. }
  165. private void ExtractFromContextMenu(string path, bool withPathInfo)
  166. {
  167. if (contextMenu.Tag == directoryTreeView)
  168. {
  169. ExtractTreeViewSelection(path, withPathInfo);
  170. }
  171. else if (contextMenu.Tag == fileListView)
  172. {
  173. ExtractListViewSelection(path, withPathInfo);
  174. }
  175. }
  176. private void OnLoadingUpdateTimerTick(object sender, EventArgs args)
  177. {
  178. if (extractionProgress != null)
  179. {
  180. ExtractionProgress progress = extractionProgress.AsyncState as ExtractionProgress;
  181. if (extractionProgress.IsCompleted)
  182. {
  183. statusMainPanel.Text = progress.extractedFileCount + " files extracted.";
  184. System.Windows.Forms.Timer timer = sender as System.Windows.Forms.Timer;
  185. timer.Enabled = false;
  186. extractionProgress = null;
  187. }
  188. else
  189. {
  190. statusMainPanel.Text = progress.extractedFileCount + " of " + progress.totalFileCount + " files extracted.";
  191. }
  192. }
  193. else
  194. {
  195. int fileCount = fileSystemViewController.FileCount;
  196. int totalFileCount = fileSystemViewController.FileSystem.FileCount;
  197. if (totalFileCount > 0 && fileCount >= totalFileCount)
  198. {
  199. statusMainPanel.Text = totalFileCount + " files processed.";
  200. System.Windows.Forms.Timer timer = sender as System.Windows.Forms.Timer;
  201. timer.Enabled = false;
  202. }
  203. else
  204. {
  205. statusMainPanel.Text = fileCount + " of " + totalFileCount + " files processed.";
  206. }
  207. }
  208. }
  209. private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
  210. {
  211. loadingUpdateTimer.Enabled = false;
  212. fileSystemViewController.Close();
  213. extractionManager.Close();
  214. }
  215. private void directoryTreeView_Enter(object sender, EventArgs e)
  216. {
  217. UpdateExtractionMenus(directoryTreeView);
  218. }
  219. private void directoryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
  220. {
  221. UpdateExtractionMenus(directoryTreeView);
  222. }
  223. private void fileListView_Enter(object sender, EventArgs e)
  224. {
  225. UpdateExtractionMenus(fileListView);
  226. }
  227. private void fileListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  228. {
  229. UpdateExtractionMenus(fileListView);
  230. }
  231. private void UpdateExtractionMenus(object sender)
  232. {
  233. bool enabled;
  234. string text = string.Empty;
  235. if (sender == directoryTreeView)
  236. {
  237. enabled = directoryTreeView.SelectedNode != null && extractionProgress == null;
  238. if (enabled)
  239. {
  240. text = directoryTreeView.SelectedNode.Text;
  241. }
  242. }
  243. else
  244. {
  245. int selectedItemCount = fileListView.SelectedItems.Count;
  246. enabled = selectedItemCount > 0 && extractionProgress == null;
  247. if (enabled)
  248. {
  249. if (selectedItemCount == 1)
  250. {
  251. text = fileListView.SelectedItems[0].Text;
  252. }
  253. else
  254. {
  255. text = selectedItemCount + " items";
  256. }
  257. }
  258. }
  259. if (enabled)
  260. {
  261. extractToolStripMenuItem.Text = "Extract " + text + "...";
  262. extractToolStripContextMenuItem.Text = "Extract " + text + "...";
  263. extractWithPathInformationToolStripMenuItem.Text = "Extract " + text + " with path information...";
  264. extractWithPathInformationToolStripContextMenuItem.Text = "Extract " + text + " with path information...";
  265. }
  266. else
  267. {
  268. extractToolStripMenuItem.Text = "Extract...";
  269. extractToolStripContextMenuItem.Text = "Extract...";
  270. extractWithPathInformationToolStripMenuItem.Text = "Extract with path information...";
  271. extractWithPathInformationToolStripContextMenuItem.Text = "Extract with path information...";
  272. }
  273. extractToolStripMenuItem.Enabled = enabled;
  274. extractToolStripContextMenuItem.Enabled = enabled;
  275. extractWithPathInformationToolStripMenuItem.Enabled = enabled;
  276. extractWithPathInformationToolStripContextMenuItem.Enabled = enabled;
  277. bool visible;
  278. if (sender == directoryTreeView)
  279. {
  280. enabled = false;
  281. visible = false;
  282. }
  283. else
  284. {
  285. enabled = false;
  286. visible = false;
  287. if (fileListView.SelectedItems.Count == 1)
  288. {
  289. Eq2FileSystemInfo item = fileListView.SelectedItems[0].Tag as Eq2FileSystemInfo;
  290. if (item is Eq2FileInfo)
  291. {
  292. Eq2FileInfo file = item as Eq2FileInfo;
  293. if (TextureDecryptor.CanDecrypt(file))
  294. {
  295. enabled = true;
  296. text = file.Name;
  297. }
  298. visible = StringComparer.InvariantCultureIgnoreCase.Equals(file.DirectoryName, "nrvobm");
  299. }
  300. }
  301. }
  302. if (enabled)
  303. {
  304. decryptToolStripContextMenuItem.Text = "Decrypt and extract " + text + "...";
  305. }
  306. else
  307. {
  308. decryptToolStripContextMenuItem.Text = "Decrypt and extract...";
  309. }
  310. decryptToolStripContextMenuItem.Enabled = enabled;
  311. decryptToolStripContextMenuItem.Visible = visible;
  312. }
  313. private void ExtractTreeViewSelection(string outputPath, bool withPathInfo)
  314. {
  315. if (directoryTreeView.SelectedNode == null) return;
  316. Eq2FileSystemInfo[] children = null;
  317. children = new Eq2FileSystemInfo[1];
  318. children[0] = directoryTreeView.SelectedNode.Tag as Eq2DirectoryInfo;
  319. // Make sure the path ends with a directory separator char.
  320. if (outputPath[outputPath.Length - 1] != Path.DirectorySeparatorChar)
  321. {
  322. outputPath += Path.DirectorySeparatorChar;
  323. }
  324. string newOutputPath = outputPath;
  325. if (withPathInfo)
  326. {
  327. Eq2DirectoryInfo directory = directoryTreeView.SelectedNode.Tag as Eq2DirectoryInfo;
  328. Eq2DirectoryInfo parentDirectory = directory.Parent;
  329. string parentDirectoryName = parentDirectory != null ? parentDirectory.FullName : string.Empty;
  330. newOutputPath = outputPath + parentDirectoryName.Replace('/', Path.DirectorySeparatorChar);
  331. }
  332. if (newOutputPath[newOutputPath.Length-1] != Path.DirectorySeparatorChar)
  333. {
  334. newOutputPath += Path.DirectorySeparatorChar;
  335. }
  336. loadingUpdateTimer.Enabled = true;
  337. extractionProgress = extractionManager.BeginExtract(children, newOutputPath, OnFileExtracted, new ExtractionProgress(children));
  338. UpdateExtractionMenus(directoryTreeView);
  339. }
  340. private void ExtractListViewSelection(string outputPath, bool withPathInfo)
  341. {
  342. if (fileListView.SelectedItems.Count == 0) return;
  343. Eq2FileSystemInfo[] children = null;
  344. children = new Eq2FileSystemInfo[fileListView.SelectedItems.Count];
  345. for (int i = 0; i < children.Length; ++i)
  346. {
  347. children[i] = fileListView.SelectedItems[i].Tag as Eq2FileSystemInfo;
  348. }
  349. // Make sure the path ends with a directory separator char.
  350. if (outputPath[outputPath.Length - 1] != Path.DirectorySeparatorChar)
  351. {
  352. outputPath += Path.DirectorySeparatorChar;
  353. }
  354. string newOutputPath = outputPath;
  355. if (withPathInfo)
  356. {
  357. Eq2DirectoryInfo parentDirectory = null;
  358. if (children[0] is Eq2FileInfo)
  359. {
  360. Eq2FileInfo file = children[0] as Eq2FileInfo;
  361. parentDirectory = file.Directory;
  362. }
  363. else if (children[0] is Eq2DirectoryInfo)
  364. {
  365. Eq2DirectoryInfo directory = children[0] as Eq2DirectoryInfo;
  366. parentDirectory = directory.Parent;
  367. }
  368. newOutputPath = outputPath + parentDirectory.FullName.Replace('/', Path.DirectorySeparatorChar);
  369. }
  370. if (newOutputPath[newOutputPath.Length-1] != Path.DirectorySeparatorChar)
  371. {
  372. newOutputPath += Path.DirectorySeparatorChar;
  373. }
  374. loadingUpdateTimer.Enabled = true;
  375. extractionProgress = extractionManager.BeginExtract(children, newOutputPath, OnFileExtracted, new ExtractionProgress(children));
  376. UpdateExtractionMenus(fileListView);
  377. }
  378. private void OnFileExtracted(IAsyncResult result)
  379. {
  380. ExtractionProgress progress = result.AsyncState as ExtractionProgress;
  381. ++progress.extractedFileCount;
  382. }
  383. private void fileListView_MouseUp(object sender, MouseEventArgs e)
  384. {
  385. if (e.Button == MouseButtons.Right)
  386. {
  387. contextMenu.Tag = fileListView;
  388. UpdateExtractionMenus(fileListView);
  389. }
  390. }
  391. private void directoryTreeView_MouseUp(object sender, MouseEventArgs e)
  392. {
  393. if (e.Button == MouseButtons.Right)
  394. {
  395. TreeNode node = directoryTreeView.GetNodeAt(e.Location);
  396. if (node != null) directoryTreeView.SelectedNode = node;
  397. contextMenu.Tag = directoryTreeView;
  398. UpdateExtractionMenus(directoryTreeView);
  399. }
  400. }
  401. private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  402. {
  403. MessageBox.Show(ApplicationName + "\n\n" + "Send any feedback to blaz@blazlabs.com", "About...");
  404. }
  405. #endregion
  406. #region Types
  407. private class ExtractionProgress
  408. {
  409. public ExtractionProgress(Eq2FileSystemInfo[] children)
  410. {
  411. totalFileCount = 0;
  412. foreach (Eq2FileSystemInfo child in children)
  413. {
  414. totalFileCount += GetChildrenCount(child);
  415. }
  416. }
  417. private int GetChildrenCount(Eq2FileSystemInfo parent)
  418. {
  419. int count = 0;
  420. if (parent is Eq2FileInfo)
  421. {
  422. ++count;
  423. }
  424. else if (parent is Eq2DirectoryInfo)
  425. {
  426. Eq2DirectoryInfo directory = parent as Eq2DirectoryInfo;
  427. count += directory.FileCount;
  428. Eq2DirectoryInfo[] subdirectories = directory.GetDirectories();
  429. foreach (Eq2DirectoryInfo subdirectory in subdirectories)
  430. {
  431. count += GetChildrenCount(subdirectory);
  432. }
  433. }
  434. return count;
  435. }
  436. public int extractedFileCount;
  437. public int totalFileCount;
  438. }
  439. #endregion
  440. #region Fields
  441. private System.Windows.Forms.Timer loadingUpdateTimer;
  442. private FileSystemViewController fileSystemViewController;
  443. private ExtractionManager extractionManager;
  444. private IAsyncResult extractionProgress;
  445. #region Constants
  446. private static string ApplicationName = "Eq2VpkTool v1.2.3";
  447. private static string ApplicationWebsite = "http://eq2.blazlabs.com";
  448. private static string ConfigurationFileName = "Configuration.xml";
  449. #endregion
  450. #endregion
  451. }
  452. }