FileInfo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #region License information
  2. // ----------------------------------------------------------------------------
  3. //
  4. // libeq2 - A library for analyzing the Everquest II File Format
  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 Sys = System.IO;
  28. #endregion
  29. namespace Everquest2.IO
  30. {
  31. public class FileInfo : FileSystemInfo
  32. {
  33. internal FileInfo(FileSystem fileSystem, string fileName, long size, string vpkFile, int offset)
  34. {
  35. #region Preconditions
  36. if (fileName == null) throw new ArgumentNullException("fileName");
  37. #endregion
  38. this.fileSystem = fileSystem;
  39. this.fileName = fileName;
  40. this.size = size;
  41. this.vpkFile = vpkFile;
  42. this.offset = offset;
  43. }
  44. public override bool Exists
  45. {
  46. get { return fileSystem.FileExists(FullName); }
  47. }
  48. public override string FullName
  49. {
  50. get { return fileName; }
  51. }
  52. public override string Name
  53. {
  54. get
  55. {
  56. string name = FullName;
  57. int nameStart = name.LastIndexOfAny(FileSystem.directorySeparators, name.Length - 1);
  58. return nameStart == -1 ? name : name.Substring(nameStart + 1);
  59. }
  60. }
  61. public DirectoryInfo Directory
  62. {
  63. get
  64. {
  65. int index = fileName.LastIndexOfAny(FileSystem.directorySeparators);
  66. string directoryName = index == -1 ? string.Empty : fileName.Substring(0, index);
  67. DirectoryInfo directory = fileSystem.GetDirectoryInfo(directoryName);
  68. if (directory == null) throw new Sys.DirectoryNotFoundException("The directory '" + directoryName + "' was not found.");
  69. return directory;
  70. }
  71. }
  72. public string DirectoryName
  73. {
  74. get
  75. {
  76. string name = FullName;
  77. int nameEnd = name.LastIndexOfAny(FileSystem.directorySeparators, name.Length - 1);
  78. return nameEnd == -1 ? string.Empty : name.Substring(0, nameEnd);
  79. }
  80. }
  81. public long Length
  82. {
  83. get
  84. {
  85. if (!Exists) throw new System.IO.FileNotFoundException("File '" + FullName + "' not found", FullName);
  86. return size;
  87. }
  88. }
  89. /// <summary>
  90. /// Gets the relative path to the VPK file that contains this file.
  91. /// </summary>
  92. /// <value>Relative path to the VPK file.</value>
  93. internal string VpkFile
  94. {
  95. get { return vpkFile; }
  96. }
  97. /// <summary>
  98. /// Gets the offset to this file's packed block in its VPK container.
  99. /// </summary>
  100. /// <value>Offset to this file's packed block.</value>
  101. internal int Offset
  102. {
  103. get { return offset; }
  104. }
  105. public FileStream OpenRead()
  106. {
  107. return Open(Sys.FileMode.Open, Sys.FileAccess.Read);
  108. }
  109. public FileStream OpenWrite()
  110. {
  111. return Open(Sys.FileMode.Open, Sys.FileAccess.Write);
  112. }
  113. public FileStream Open(Sys.FileMode mode)
  114. {
  115. return Open(mode, Sys.FileAccess.Read);
  116. }
  117. public FileStream Open(Sys.FileMode mode, Sys.FileAccess access)
  118. {
  119. return new FileStream(fileSystem.BasePath + VpkFile, Offset, mode, access);
  120. }
  121. #region Fields
  122. private FileSystem fileSystem;
  123. private string fileName;
  124. private long size;
  125. private string vpkFile;
  126. private int offset;
  127. #endregion
  128. }
  129. }
  130. /* EOF */