FileSystemViewController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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.Threading;
  28. using System.Windows.Forms;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using Eq2FileSystem = Everquest2.IO.FileSystem;
  32. using Eq2FileSystemInfo = Everquest2.IO.FileSystemInfo;
  33. using Eq2FileInfo = Everquest2.IO.FileInfo;
  34. using Eq2DirectoryInfo = Everquest2.IO.DirectoryInfo;
  35. #endregion
  36. namespace Eq2VpkTool
  37. {
  38. public class FileSystemViewController
  39. {
  40. #region Methods
  41. public void Open(string filename, TreeView treeview, ListView listview)
  42. {
  43. this.treeview = treeview;
  44. this.listview = listview;
  45. nodeDictionary.Clear();
  46. fileCount = 0;
  47. filesystem = new Eq2FileSystem();
  48. extractionManager = new ExtractionManager();
  49. // Initialize treeview and listview
  50. treeview.Nodes.Clear();
  51. listview.Items.Clear();
  52. listview.Sorting = SortOrder.Ascending;
  53. listview.ListViewItemSorter = new DirectoryContentsComparer();
  54. iconManager = new IconManager(new ImageList());
  55. treeview.ImageList = iconManager.ImageList;
  56. treeview.ImageIndex = iconManager.GetDirectoryImageIndex();
  57. listview.SmallImageList = iconManager.ImageList;
  58. // We must first de-register the event handlers, in case they were registered in a previous call to Open().
  59. treeview.BeforeExpand -= OnExpandNode;
  60. treeview.BeforeExpand += OnExpandNode;
  61. treeview.BeforeCollapse -= OnCollapseNode;
  62. treeview.BeforeCollapse += OnCollapseNode;
  63. treeview.BeforeSelect -= OnSelectNode;
  64. treeview.BeforeSelect += OnSelectNode;
  65. listview.DoubleClick -= OnListViewDoubleClick;
  66. listview.DoubleClick += OnListViewDoubleClick;
  67. thread = new Thread(new ParameterizedThreadStart(OpenFileSystem));
  68. thread.Start(filename);
  69. }
  70. /// <summary>
  71. /// Start function for the processing thread.
  72. /// </summary>
  73. /// <param name="obj">String that represents the path to the VPL file.</param>
  74. private void OpenFileSystem(object obj)
  75. {
  76. filename = obj as string;
  77. try
  78. {
  79. filesystem.DirectoryAdded += OnRootDirectoryAdded;
  80. filesystem.FileAdded += OnFileAdded;
  81. filesystem.Open(filename);
  82. }
  83. catch (ThreadAbortException)
  84. {
  85. }
  86. catch (Exception e)
  87. {
  88. MessageBox.Show("Error processing file.\n\n" + e, "Error");
  89. }
  90. }
  91. /// <summary>
  92. /// Stops the processing thread and deletes all generated temporary files.
  93. /// </summary>
  94. public void Close()
  95. {
  96. if (thread != null)
  97. {
  98. thread.Abort();
  99. thread.Join();
  100. thread = null;
  101. // Delete temporary files
  102. foreach (string path in temporaryFiles)
  103. {
  104. try { File.Delete(path); }
  105. catch (UnauthorizedAccessException) {}
  106. catch (IOException) {}
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Callback invoked every time a file is added to the file system.
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void OnFileAdded(object sender, Eq2FileSystem.FileAddedEventArgs e)
  116. {
  117. lock (this) ++fileCount;
  118. }
  119. /// <summary>
  120. /// Callback invoked when the first directory (the root directory) is added to the file system.
  121. /// </summary>
  122. /// <param name="sender"></param>
  123. /// <param name="e"></param>
  124. private void OnRootDirectoryAdded(object sender, Eq2FileSystem.DirectoryAddedEventArgs e)
  125. {
  126. filesystem.DirectoryAdded -= OnRootDirectoryAdded;
  127. e.directory.DirectoryAdded += OnDirectoryAdded;
  128. treeview.Invoke(new Action<Eq2DirectoryInfo>(AddRootDirectory), e.directory);
  129. }
  130. /// <summary>
  131. /// Adds the root directory node to the TreeView.
  132. /// </summary>
  133. /// <remarks>This method is called from the UI thread.</remarks>
  134. /// <param name="rootDirectory">Root directory.</param>
  135. private void AddRootDirectory(Eq2DirectoryInfo rootDirectory)
  136. {
  137. string rootNodeText = System.IO.Path.GetFileName(filename);
  138. TreeNode node = new TreeNode(rootNodeText);
  139. node.Tag = rootDirectory;
  140. treeview.Nodes.Add(node);
  141. // No need to acquire the lock on nodeDictionary because we're sure
  142. // no other thread will be accessing it at this point.
  143. nodeDictionary.Add(rootDirectory, new NodeInfo(node));
  144. }
  145. /// <summary>
  146. /// Callback invoked every time a new directory is added to an existing directory.
  147. /// </summary>
  148. /// <param name="sender"></param>
  149. /// <param name="e"></param>
  150. private void OnDirectoryAdded(object sender, Eq2FileSystem.DirectoryAddedEventArgs e)
  151. {
  152. Eq2DirectoryInfo directory = e.directory;
  153. Eq2DirectoryInfo parentDirectory = directory.Parent;
  154. NodeInfo parentDirectoryNodeInfo;
  155. lock (nodeDictionary) parentDirectoryNodeInfo = nodeDictionary[parentDirectory];
  156. if (parentDirectoryNodeInfo.Expanded)
  157. {
  158. // The parent directory is expanded, so we must reflect the addition of the new directory.
  159. // This new directory doesn't have any subdirectories yet, so we will install a listener on it.
  160. TreeNode directoryNode = treeview.Invoke(new AddDirectoryDelegate(AddDirectoryToExpandedDirectory),
  161. parentDirectoryNodeInfo.Node,
  162. directory) as TreeNode;
  163. lock (nodeDictionary) nodeDictionary.Add(directory, new NodeInfo(directoryNode));
  164. directory.DirectoryAdded += OnDirectoryAdded;
  165. }
  166. else
  167. {
  168. // The parent directory didn't have any subdirectories, but it now has one.
  169. // We'll add a dummy child node so the plus icon appears and the node can be expanded.
  170. // Note: More than one thread can invoke this method before we deregister from the DirectoryAdded event.
  171. // The means more than one dummy subnode can be added to this directory.
  172. parentDirectory.DirectoryAdded -= OnDirectoryAdded;
  173. treeview.Invoke(new Action<TreeNode>(AddDirectoryToCollapsedDirectory), parentDirectoryNodeInfo.Node);
  174. }
  175. }
  176. /// <summary>
  177. /// Adds a new tree node as a child of an expanded directory node.
  178. /// </summary>
  179. /// <remarks>This method is called from the UI thread.</remarks>
  180. /// <param name="parentNode"></param>
  181. /// <param name="directory"></param>
  182. /// <returns></returns>
  183. private TreeNode AddDirectoryToExpandedDirectory(TreeNode parentNode, Eq2DirectoryInfo directory)
  184. {
  185. string directoryName = directory.Name;
  186. TreeNode node = parentNode.Nodes.Add(directoryName, directoryName);
  187. node.Tag = directory;
  188. return node;
  189. }
  190. /// <summary>
  191. /// Adds a dummy tree node as a child of a collapsed directory node.
  192. /// This dummy node makes the treeview render a plus sign used to expand the directory node.
  193. /// </summary>
  194. /// <remarks>This method is called from the UI thread.</remarks>
  195. /// <param name="directoryNode"></param>
  196. private void AddDirectoryToCollapsedDirectory(TreeNode directoryNode)
  197. {
  198. directoryNode.Nodes.Add(new TreeNode());
  199. }
  200. /// <summary>
  201. /// Callback invoked when a tree node is expanded.
  202. /// This method must create the nodes for the subdirectories of the expanded directory.
  203. /// </summary>
  204. /// <remarks>This method is called from the UI thread.</remarks>
  205. /// <param name="sender"></param>
  206. /// <param name="e"></param>
  207. private void OnExpandNode(object sender, TreeViewCancelEventArgs e)
  208. {
  209. TreeNode node = e.Node;
  210. Eq2DirectoryInfo directory = node.Tag as Eq2DirectoryInfo;
  211. // Remove dummy nodes if present
  212. if (node.Nodes[0].Text.Length == 0) node.Nodes.Clear();
  213. lock (nodeDictionary)
  214. {
  215. treeview.BeginUpdate();
  216. // Add new subdirectories as nodes to the treeview and to the node dictionary.
  217. Eq2DirectoryInfo[] subdirectories = directory.GetDirectories();
  218. foreach (Eq2DirectoryInfo subdirectory in subdirectories)
  219. {
  220. if (!node.Nodes.ContainsKey(subdirectory.Name))
  221. {
  222. TreeNode subdirectoryNode = AddDirectoryToExpandedDirectory(node, subdirectory);
  223. nodeDictionary.Add(subdirectory, new NodeInfo(subdirectoryNode));
  224. // Check whether this subdirectory has subdirectories of its own or not.
  225. // If it does, we will add a dummy tree node to it so the plus icon appears.
  226. // If it does not, we will track any changes to it.
  227. if (subdirectory.DirectoryCount > 0)
  228. {
  229. subdirectoryNode.Nodes.Add(new TreeNode());
  230. }
  231. else
  232. {
  233. subdirectory.DirectoryAdded += OnDirectoryAdded;
  234. }
  235. }
  236. }
  237. treeview.EndUpdate();
  238. // Mark this directory as expanded
  239. nodeDictionary[directory].Expanded = true;
  240. directory.DirectoryAdded += OnDirectoryAdded;
  241. }
  242. }
  243. /// <summary>
  244. /// Callback invoked when a tree node is collapsed.
  245. /// We won't delete the tree nodes already added to the collapsed directory,
  246. /// but we won't add any more until the node is expanded again.
  247. /// </summary>
  248. /// <remarks>This method is called from the UI thread.</remarks>
  249. /// <param name="sender"></param>
  250. /// <param name="e"></param>
  251. private void OnCollapseNode(object sender, TreeViewCancelEventArgs e)
  252. {
  253. TreeNode node = e.Node;
  254. Eq2DirectoryInfo directory = node.Tag as Eq2DirectoryInfo;
  255. directory.DirectoryAdded -= OnDirectoryAdded;
  256. // Mark this directory as collapsed
  257. lock (nodeDictionary) nodeDictionary[directory].Expanded = false;
  258. }
  259. /// <summary>
  260. /// Callback invoked when a tree node is selected.
  261. /// Updates the list view with the directory contents.
  262. /// </summary>
  263. /// <remarks>This method is called from the UI thread.</remarks>
  264. /// <param name="sender"></param>
  265. /// <param name="e"></param>
  266. private void OnSelectNode(object sender, TreeViewCancelEventArgs e)
  267. {
  268. TreeView treeview = sender as TreeView;
  269. TreeNode node = e.Node;
  270. Eq2DirectoryInfo newDirectory = node.Tag as Eq2DirectoryInfo;
  271. if (treeview.SelectedNode != null)
  272. {
  273. // Deregister from the old directory
  274. Eq2DirectoryInfo oldDirectory = treeview.SelectedNode.Tag as Eq2DirectoryInfo;
  275. oldDirectory.ChildAdded -= OnDirectoryChildAdded;
  276. }
  277. // FIXME: There is a race condition here. It is possible that after having deregistered from
  278. // the old directory there is still a child of that directory in the process of being
  279. // added. In that case, it will be added to the listview erroneously as if it were a
  280. // child of the newly selected directory.
  281. // Clear the old items from the listview
  282. listview.Items.Clear();
  283. listview.BeginUpdate();
  284. // Add the current children of the new directory to the listview
  285. Eq2FileSystemInfo[] children = newDirectory.GetFileSystemInfos();
  286. foreach (Eq2FileSystemInfo child in children)
  287. {
  288. ListViewItem item = listview.Items.Add(child.Name);
  289. item.Tag = child;
  290. item.ImageIndex = iconManager.GetImageIndex(child);
  291. }
  292. listview.EndUpdate();
  293. // Register to receive child added events in the future to the new directory
  294. newDirectory.ChildAdded += OnDirectoryChildAdded;
  295. }
  296. /// <summary>
  297. /// Callback invoked when a file or directory is added to an existing directory.
  298. /// </summary>
  299. /// <param name="sender"></param>
  300. /// <param name="e"></param>
  301. private void OnDirectoryChildAdded(object sender, Eq2DirectoryInfo.ChildAddedEventArgs e)
  302. {
  303. listview.Invoke(new AddChildToDirectoryDelegate(AddChildToDirectory), sender as Eq2DirectoryInfo, e.child);
  304. }
  305. /// <summary>
  306. /// Adds a file or directory to the list view.
  307. /// </summary>
  308. /// <remarks>This method is called from the UI thread.</remarks>
  309. /// <param name="directory"></param>
  310. /// <param name="child"></param>
  311. private void AddChildToDirectory(Eq2DirectoryInfo directory, Eq2FileSystemInfo child)
  312. {
  313. // Note: The second condition is probably not needed.
  314. if (treeview.SelectedNode != null && treeview.SelectedNode.Tag == directory)
  315. {
  316. ListViewItem item = listview.Items.Add(child.Name);
  317. item.Tag = child;
  318. item.ImageIndex = iconManager.GetImageIndex(child);
  319. }
  320. }
  321. /// <summary>
  322. /// Callback invoked when the user double-clicks on the list view.
  323. /// This operation opens the selected directory or extracts and opens the selected file.
  324. /// </summary>
  325. /// <remarks>This method is called from the UI thread.</remarks>
  326. /// <param name="sender"></param>
  327. /// <param name="e"></param>
  328. private void OnListViewDoubleClick(object sender, EventArgs e)
  329. {
  330. ListView listview = sender as ListView;
  331. if (listview.SelectedItems.Count != 1) return;
  332. Eq2FileSystemInfo child = listview.SelectedItems[0].Tag as Eq2FileSystemInfo;
  333. if (child is Eq2DirectoryInfo)
  334. {
  335. OnListViewDoubleClickDirectory(child as Eq2DirectoryInfo);
  336. }
  337. else if (child is Eq2FileInfo)
  338. {
  339. OnListViewDoubleClickFile(child as Eq2FileInfo);
  340. }
  341. }
  342. private void OnListViewDoubleClickDirectory(Eq2DirectoryInfo directory)
  343. {
  344. Stack<Eq2DirectoryInfo> directoryHierarchy = new Stack<Eq2DirectoryInfo>();
  345. directoryHierarchy.Push(directory);
  346. Eq2DirectoryInfo parent = directory.Parent;
  347. while (parent != null)
  348. {
  349. directoryHierarchy.Push(parent);
  350. parent = parent.Parent;
  351. }
  352. // Pop root directory
  353. directoryHierarchy.Pop();
  354. TreeNode rootNode = treeview.Nodes[0];
  355. rootNode.Expand();
  356. TreeNodeCollection directoryNodes = rootNode.Nodes;
  357. TreeNode nodeToExpand = null;
  358. while (directoryHierarchy.Count > 0)
  359. {
  360. Eq2DirectoryInfo directoryToExpand = directoryHierarchy.Pop();
  361. nodeToExpand = directoryNodes[directoryToExpand.Name];
  362. if (!nodeToExpand.IsExpanded) nodeToExpand.Expand();
  363. directoryNodes = nodeToExpand.Nodes;
  364. }
  365. treeview.SelectedNode = nodeToExpand;
  366. }
  367. private void OnListViewDoubleClickFile(Eq2FileInfo file)
  368. {
  369. OpenFile(file);
  370. }
  371. private void OpenFile(Eq2FileInfo file)
  372. {
  373. string path = Path.GetTempPath() + file.DirectoryName.Replace('/', Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
  374. string filename = path + file.Name;
  375. try
  376. {
  377. Directory.CreateDirectory(path);
  378. FileStream stream = extractionManager.ExtractFile(file, path);
  379. stream.Close();
  380. }
  381. catch (UnauthorizedAccessException) { return; }
  382. catch (IOException) { return; }
  383. try
  384. {
  385. temporaryFiles.Add(filename);
  386. System.Diagnostics.Process.Start(filename);
  387. }
  388. catch (System.ComponentModel.Win32Exception)
  389. {
  390. // There is no associated program for this file type.
  391. // Ignore the error.
  392. }
  393. }
  394. #endregion
  395. #region Types
  396. /// <summary>
  397. /// Contains information about a tree view node. This information is used by the processing thread,
  398. /// not the UI thread. It's a way of reducing the number of cross-thread invocations.
  399. /// </summary>
  400. private class NodeInfo
  401. {
  402. public NodeInfo(TreeNode node)
  403. {
  404. this.node = node;
  405. this.expanded = false;
  406. }
  407. public TreeNode Node
  408. {
  409. get { return node; }
  410. }
  411. public bool Expanded
  412. {
  413. get { return expanded; }
  414. set { expanded = value; }
  415. }
  416. private TreeNode node;
  417. private bool expanded;
  418. }
  419. #endregion
  420. #region Delegates
  421. private delegate TreeNode AddDirectoryDelegate (TreeNode parentNode, Eq2DirectoryInfo directory);
  422. private delegate void AddChildToDirectoryDelegate (Eq2DirectoryInfo directory, Eq2FileSystemInfo child);
  423. #endregion
  424. #region Properties
  425. public int FileCount
  426. {
  427. get { lock (this) return fileCount; }
  428. }
  429. public Eq2FileSystem FileSystem
  430. {
  431. get { return filesystem; }
  432. }
  433. #endregion
  434. #region Fields
  435. private IDictionary<Eq2DirectoryInfo, NodeInfo> nodeDictionary = new Dictionary<Eq2DirectoryInfo, NodeInfo>();
  436. private IList<string> temporaryFiles = new List<string>();
  437. private int fileCount;
  438. private string filename;
  439. private Thread thread;
  440. private Eq2FileSystem filesystem;
  441. private TreeView treeview;
  442. private ListView listview;
  443. private IconManager iconManager;
  444. private ExtractionManager extractionManager;
  445. #endregion
  446. }
  447. }