MoonTools.NETPhysFS/SharpPhysFS/PhysFSStream.cs

121 lines
2.3 KiB
C#
Raw Normal View History

2016-01-27 18:54:18 +00:00
using System;
using System.IO;
2016-01-27 17:57:53 +00:00
namespace SharpPhysFS
2016-01-27 17:57:53 +00:00
{
public class PhysFSStream : Stream
{
IntPtr handle;
bool readOnly = false;
2016-02-08 21:25:37 +00:00
PhysFS physFS;
2016-01-27 17:57:53 +00:00
2016-02-08 21:25:37 +00:00
internal PhysFSStream(PhysFS pfs, IntPtr ptr, bool readOnly)
2016-01-27 17:57:53 +00:00
{
2016-02-08 21:25:37 +00:00
handle = ptr;
this.readOnly = readOnly;
physFS = pfs;
2016-01-27 17:57:53 +00:00
}
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanSeek
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return !readOnly;
}
}
public override void Flush()
{
2016-02-08 21:25:37 +00:00
PhysFS.LowLevel.Flush(handle, physFS);
2016-01-27 17:57:53 +00:00
}
public override long Length
{
get
{
2016-02-08 21:25:37 +00:00
return PhysFS.LowLevel.FileLength(handle, physFS);
2016-01-27 17:57:53 +00:00
}
}
public override long Position
{
get
{
2016-02-08 21:25:37 +00:00
return PhysFS.LowLevel.Tell(handle, physFS);
2016-01-27 17:57:53 +00:00
}
set
{
2016-02-08 21:25:37 +00:00
PhysFS.LowLevel.Seek(handle, (ulong)value, physFS);
2016-01-27 17:57:53 +00:00
}
}
public long Read(byte[] buffer, uint offset, uint count)
{
2016-02-08 21:25:37 +00:00
return PhysFS.LowLevel.Read(handle, buffer, 1, count, physFS);
2016-01-27 17:57:53 +00:00
}
public override int Read(byte[] buffer, int offset, int count)
{
return (int)Read(buffer, (uint)offset, (uint)count);
}
public override long Seek(long offset, SeekOrigin origin)
{
long pos = 0;
if (origin == SeekOrigin.Begin)
pos = 0;
else if (origin == SeekOrigin.Current)
2016-02-08 21:25:37 +00:00
pos = PhysFS.LowLevel.Tell(handle, physFS);
2016-01-27 17:57:53 +00:00
else
2016-02-08 21:25:37 +00:00
pos = PhysFS.LowLevel.FileLength(handle, physFS);
2016-01-27 17:57:53 +00:00
2016-02-08 21:25:37 +00:00
PhysFS.LowLevel.Seek(handle, (ulong)(pos + offset), physFS);
2016-01-27 17:57:53 +00:00
return pos + offset;
}
public long Write(byte[] buffer, uint offset, uint count)
{
2016-02-08 21:25:37 +00:00
return PhysFS.LowLevel.Write(handle, buffer, 1, count, physFS);
2016-01-27 17:57:53 +00:00
}
public override void Write(byte[] buffer, int offset, int count)
{
Write(buffer, (uint)offset, (uint)count);
}
2016-02-08 21:25:37 +00:00
public override void SetLength(long value)
2016-01-27 17:57:53 +00:00
{
2016-02-08 21:25:37 +00:00
throw new NotImplementedException();
2016-01-27 17:57:53 +00:00
}
protected override void Dispose(bool disposing)
{
2016-02-08 21:25:37 +00:00
if(disposing)
2016-01-27 17:57:53 +00:00
{
2016-02-08 21:25:37 +00:00
if (handle != IntPtr.Zero)
{
PhysFS.LowLevel.Close(handle, physFS);
handle = IntPtr.Zero;
}
2016-01-27 17:57:53 +00:00
}
base.Dispose(disposing);
}
}
}