FileStream.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 System.Diagnostics;
  28. using ICSharpCode.SharpZipLib;
  29. using ICSharpCode.SharpZipLib.Zip.Compression;
  30. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
  31. using Sys = System.IO;
  32. #endregion
  33. namespace Everquest2.IO
  34. {
  35. public class FileStream : Sys.Stream
  36. {
  37. public FileStream(FileSystem fileSystem, string path, Sys.FileMode mode) : this(fileSystem, path, mode, Sys.FileAccess.Read)
  38. {
  39. }
  40. public FileStream(FileSystem fileSystem, string path, Sys.FileMode mode, Sys.FileAccess access)
  41. {
  42. #region Preconditions
  43. if (fileSystem == null) throw new ArgumentNullException("fileSystem");
  44. if (path == null) throw new ArgumentNullException("path");
  45. string directory = Sys.Path.GetDirectoryName(path);
  46. if (!fileSystem.DirectoryExists(directory)) throw new Sys.DirectoryNotFoundException("Directory '" + directory + "' not found in this file system.");
  47. if (mode != Sys.FileMode.Open) throw new NotSupportedException("Only 'open' mode is supported.");
  48. if (access != Sys.FileAccess.Read) throw new NotSupportedException("Write access is not supported. Please specify read-only access.");
  49. #endregion
  50. FileInfo file = fileSystem.GetFileInfo(path);
  51. if (file == null)
  52. {
  53. if (mode == Sys.FileMode.Open) throw new Sys.FileNotFoundException("File '" + path + "' not found in this file system.", path);
  54. }
  55. Initialize(fileSystem.BasePath + file.VpkFile, file.Offset, mode, access);
  56. }
  57. internal FileStream(string vpkPath, int offset, Sys.FileMode mode, Sys.FileAccess access)
  58. {
  59. Initialize(vpkPath, offset, mode, access);
  60. }
  61. private void Initialize(string vpkPath, int offset, Sys.FileMode mode, Sys.FileAccess access)
  62. {
  63. try
  64. {
  65. using (Sys.FileStream baseStream = new Sys.FileStream(vpkPath, mode, access))
  66. {
  67. InflaterInputStream inflaterStream = new InflaterInputStream(baseStream);
  68. Sys.BinaryReader reader = new Sys.BinaryReader(baseStream);
  69. baseStream.Seek(offset, Sys.SeekOrigin.Begin);
  70. byte[] rawInt = new byte[4];
  71. Sys.MemoryStream intStream = new Sys.MemoryStream(rawInt);
  72. Sys.BinaryReader intReader = new Sys.BinaryReader(intStream);
  73. byte[] rawName;
  74. Sys.MemoryStream nameStream = new Sys.MemoryStream();
  75. Sys.BinaryReader nameReader = new Sys.BinaryReader(nameStream, System.Text.Encoding.ASCII);
  76. int deflatedDataSize = reader.ReadInt32();
  77. try { inflaterStream.Read(rawInt, 0, 4); }
  78. catch (ZipException e) { throw new Sys.InvalidDataException("Error uncompressing filename length. The compressed data is invalid.", e); }
  79. intStream.Seek(0, Sys.SeekOrigin.Begin);
  80. int fileNameLength = intReader.ReadInt32();
  81. try { inflaterStream.Read(rawInt, 0, 4); }
  82. catch (ZipException e) { throw new Sys.InvalidDataException("Error uncompressing file size. The compressed data is invalid.", e); }
  83. intStream.Seek(0, Sys.SeekOrigin.Begin);
  84. int fileSize = intReader.ReadInt32();
  85. rawName = new byte[fileNameLength];
  86. try { inflaterStream.Read(rawName, 0, fileNameLength); }
  87. catch (ZipException e) { throw new Sys.InvalidDataException("Error uncompressing filename. The compressed data is invalid.", e); }
  88. nameStream.Seek(0, Sys.SeekOrigin.Begin);
  89. nameStream.Write(rawName, 0, fileNameLength);
  90. nameStream.Seek(0, Sys.SeekOrigin.Begin);
  91. filename = new string(nameReader.ReadChars(fileNameLength - 1));
  92. byte[] fileData = new byte[fileSize];
  93. try
  94. {
  95. int bytesRead = 0;
  96. while (inflaterStream.Available == 1 && bytesRead < fileSize)
  97. {
  98. bytesRead += inflaterStream.Read(fileData, bytesRead, (int)fileSize - bytesRead);
  99. }
  100. }
  101. catch (ZipException e)
  102. {
  103. throw new Sys.InvalidDataException("Error uncompressing file data. The compressed data is invalid.", e);
  104. }
  105. bool writable = (access & Sys.FileAccess.Write) != 0;
  106. dataStream = new Sys.MemoryStream(fileData, writable);
  107. }
  108. }
  109. catch (System.Security.SecurityException e)
  110. {
  111. throw new System.Security.SecurityException("No permission to open '" + vpkPath + "' with " + mode + " mode and " + access + " access.", e);
  112. }
  113. }
  114. public override bool CanRead
  115. {
  116. get { return dataStream.CanRead; }
  117. }
  118. public override bool CanSeek
  119. {
  120. get { return dataStream.CanSeek; }
  121. }
  122. public override bool CanWrite
  123. {
  124. get { return dataStream.CanWrite; }
  125. }
  126. public override long Length
  127. {
  128. get { return dataStream.Length; }
  129. }
  130. public override long Position
  131. {
  132. get { return dataStream.Position; }
  133. set { dataStream.Position = value; }
  134. }
  135. public string Name
  136. {
  137. get { return filename; }
  138. }
  139. public override void Close()
  140. {
  141. dataStream.Close();
  142. }
  143. public override void Flush()
  144. {
  145. dataStream.Flush();
  146. }
  147. public override long Seek(long offset, Sys.SeekOrigin origin)
  148. {
  149. return dataStream.Seek(offset, origin);
  150. }
  151. public override int Read(byte[] buffer, int offset, int count)
  152. {
  153. return dataStream.Read(buffer, offset, count);
  154. }
  155. public override int ReadByte()
  156. {
  157. return dataStream.ReadByte();
  158. }
  159. public override void Write(byte[] buffer, int offset, int count)
  160. {
  161. dataStream.Write(buffer, offset, count);
  162. }
  163. public override void SetLength(long value)
  164. {
  165. dataStream.SetLength(value);
  166. }
  167. #region Fields
  168. Sys.MemoryStream dataStream;
  169. string filename;
  170. #endregion
  171. }
  172. }
  173. /* EOF */