extras/PhysFS.NET/PhysFSFileStream.cs
author Ryan C. Gordon <icculus@icculus.org>
Wed, 03 Oct 2018 22:45:05 -0400
branchstable-1.0
changeset 1648 93b4c3bc6b9d
parent 532 748f518a6d76
permissions -rwxr-xr-x
Fixed Win10's GetUserProfileDirectory() bug in stable-1.0 branch.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
532
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     1
/* PhysFSFileStream.cs - (c)2003 Gregory S. Read */
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     2
using System;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     3
using System.Collections;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     4
using System.IO;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     5
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     6
namespace PhysFS_NET
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     7
{
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     8
   public enum PhysFSFileMode {Read, Write, Append};
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
     9
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    10
   // Our exception class we'll use for throwing all PhysFS API related exception
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    11
   public class PhysFSException : IOException
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    12
   {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    13
      public PhysFSException(string Message) : base(Message) {}
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    14
      public PhysFSException() : base(PhysFS_DLL.PHYSFS_getLastError()) {}
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    15
   }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    16
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    17
   public unsafe class PhysFSFileStream : Stream
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    18
   {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    19
      // ***Public properties***
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    20
      public override bool CanRead
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    21
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    22
         get
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    23
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    24
            // Reading is supported
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    25
            return true;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    26
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    27
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    28
      
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    29
      public override bool CanSeek
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    30
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    31
         get
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    32
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    33
            // Seek is supported
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    34
            return true;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    35
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    36
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    37
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    38
      public override bool CanWrite
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    39
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    40
         get
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    41
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    42
            // Writing is supported
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    43
            return true;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    44
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    45
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    46
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    47
      public override long Length
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    48
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    49
         get
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    50
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    51
            long TempLength;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    52
            TempLength = PhysFS_DLL.PHYSFS_fileLength(pHandle);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    53
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    54
            // If call returned an error, throw an exception
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    55
            if(TempLength == -1)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    56
               throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    57
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    58
            return TempLength;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    59
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    60
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    61
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    62
      public override long Position
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    63
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    64
         get
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    65
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    66
            long TempPosition;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    67
            TempPosition = PhysFS_DLL.PHYSFS_tell(pHandle);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    68
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    69
            // If call returned an error, throw an exception
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    70
            if(TempPosition == -1)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    71
               throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    72
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    73
            return TempPosition;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    74
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    75
         set
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    76
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    77
            // Seek from beginning of file using the position value
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    78
            Seek(value, SeekOrigin.Begin);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    79
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    80
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    81
      
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    82
      // ***Public methods***
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    83
      public PhysFSFileStream(string FileName, PhysFSFileMode FileMode, ulong BufferSize)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    84
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    85
         // Open the specified file with the appropriate file access
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    86
         switch(FileMode)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    87
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    88
            case PhysFSFileMode.Read:
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    89
               pHandle = PhysFS_DLL.PHYSFS_openRead(FileName);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    90
               break;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    91
            case PhysFSFileMode.Write:
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    92
               pHandle = PhysFS_DLL.PHYSFS_openWrite(FileName);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    93
               break;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    94
            case PhysFSFileMode.Append:
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    95
               pHandle = PhysFS_DLL.PHYSFS_openAppend(FileName);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    96
               break;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    97
            default:
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    98
               throw new PhysFSException("Invalid FileMode specified");
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
    99
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   100
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   101
         // If handle is null, an error occured, so raise an exception
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   102
         //!!! Does object get created if exception is thrown?
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   103
         if(pHandle == null)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   104
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   105
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   106
         // Set buffer size, raise an exception if an error occured
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   107
         if(PhysFS_DLL.PHYSFS_setBuffer(pHandle, BufferSize) == 0)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   108
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   109
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   110
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   111
      // This constructor sets the buffer size to 0 if not specified
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   112
      public PhysFSFileStream(string FileName, PhysFSFileMode FileMode) : this(FileName, FileMode, 0) {}
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   113
		
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   114
      ~PhysFSFileStream()
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   115
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   116
         // Don't close the handle if they've specifically closed it already
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   117
         if(!Closed)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   118
            Close();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   119
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   120
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   121
      public override void Flush()
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   122
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   123
         if(PhysFS_DLL.PHYSFS_flush(pHandle) == 0)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   124
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   125
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   126
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   127
      public override int Read(byte[] buffer, int offset, int count)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   128
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   129
         long RetValue;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   130
   
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   131
         fixed(byte *pbytes = &buffer[offset])
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   132
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   133
            // Read into our allocated pointer
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   134
            RetValue = PhysFS_DLL.PHYSFS_read(pHandle, pbytes, sizeof(byte), (uint)count);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   135
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   136
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   137
         if(RetValue == -1)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   138
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   139
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   140
         // Return number of bytes read
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   141
         // Note: This cast should be safe since we are only reading 'count' items, which
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   142
         // is of type 'int'.
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   143
         return (int)RetValue;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   144
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   145
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   146
      public override void Write(byte[] buffer, int offset, int count)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   147
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   148
         long RetValue;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   149
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   150
         fixed(byte* pbytes = &buffer[offset])
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   151
         {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   152
            // Write buffer
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   153
            RetValue = PhysFS_DLL.PHYSFS_write(pHandle, pbytes, sizeof(byte), (uint)count);
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   154
         }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   155
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   156
         if(RetValue == -1)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   157
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   158
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   159
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   160
      public override long Seek(long offset, SeekOrigin origin)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   161
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   162
         // Only seeking from beginning is supported by PhysFS API
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   163
         if(origin != SeekOrigin.Begin)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   164
            throw new PhysFSException("Only seek origin of \"Begin\" is supported");
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   165
         
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   166
         // Seek to specified offset, raise an exception if error occured
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   167
         if(PhysFS_DLL.PHYSFS_seek(pHandle, (ulong)offset) == 0)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   168
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   169
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   170
         // Since we always seek from beginning, the offset is always
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   171
         //  the absolute position.
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   172
         return offset;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   173
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   174
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   175
      public override void SetLength(long value)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   176
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   177
         throw new NotSupportedException("SetLength method not supported in PhysFSFileStream objects.");
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   178
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   179
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   180
      public override void Close()
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   181
      {
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   182
         // Close the handle
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   183
         if(PhysFS_DLL.PHYSFS_close(pHandle) == 0)
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   184
            throw new PhysFSException();
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   185
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   186
         // File has been closed.  Rock.
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   187
         Closed = true;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   188
      }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   189
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   190
      // ***Private variables***
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   191
      private void *pHandle;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   192
      private bool Closed = false;
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   193
   }
748f518a6d76 -First version of PhysFS.NET.
Gregory S. Read <zeph@clutteredmind.org>
parents:
diff changeset
   194
}