DirectoryContentsComparer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Collections;
  28. using System.Windows.Forms;
  29. using Eq2FileSystemInfo = Everquest2.IO.FileSystemInfo;
  30. using Eq2FileInfo = Everquest2.IO.FileInfo;
  31. using Eq2DirectoryInfo = Everquest2.IO.DirectoryInfo;
  32. #endregion
  33. namespace Eq2VpkTool
  34. {
  35. /// <summary>
  36. /// Compares two file system items for sorting the list view.
  37. /// Directories always compare lower than files.
  38. /// Directories with directories and files with files compare lexicographically.
  39. /// </summary>
  40. public class DirectoryContentsComparer : IComparer
  41. {
  42. public int Compare(object x, object y)
  43. {
  44. ListViewItem item1 = x as ListViewItem;
  45. ListViewItem item2 = y as ListViewItem;
  46. // The item tags could be null because some of the ListViewItem's passed to this function
  47. // are probably newly created items.
  48. if (item1.Tag == null) return 1;
  49. if (item2.Tag == null) return -1;
  50. Eq2FileSystemInfo child1 = item1.Tag as Eq2FileSystemInfo;
  51. Eq2FileSystemInfo child2 = item2.Tag as Eq2FileSystemInfo;
  52. if (child1 is Eq2DirectoryInfo)
  53. {
  54. if (child2 is Eq2DirectoryInfo)
  55. {
  56. Eq2DirectoryInfo directory1 = child1 as Eq2DirectoryInfo;
  57. Eq2DirectoryInfo directory2 = child2 as Eq2DirectoryInfo;
  58. return directory1.Name.CompareTo(directory2.Name);
  59. }
  60. else
  61. {
  62. return -1;
  63. }
  64. }
  65. else
  66. {
  67. if (child2 is Eq2DirectoryInfo)
  68. {
  69. return 1;
  70. }
  71. else
  72. {
  73. Eq2FileInfo file1 = child1 as Eq2FileInfo;
  74. Eq2FileInfo file2 = child2 as Eq2FileInfo;
  75. return file1.Name.CompareTo(file2.Name);
  76. }
  77. }
  78. }
  79. }
  80. }