MoonTools.NETPhysFS/NETPhysFS/PhysFSStream.cs

121 lines
2.3 KiB
C#
Raw Permalink Normal View History

2016-01-27 18:54:18 +00:00
using System;
using System.IO;
2016-01-27 17:57:53 +00:00
2020-01-18 03:16:09 +00:00
namespace MoonTools.NETPhysFS
2016-01-27 17:57:53 +00:00
{
public class PhysFSStream : Stream
{
private IntPtr handle;
private readonly bool readOnly;
private readonly 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
{
2017-05-26 11:14:23 +00:00
return PhysFS.LowLevel.FileLength(handle);
2016-01-27 17:57:53 +00:00
}
}
public override long Position
{
get
{
2017-05-26 11:14:23 +00:00
return PhysFS.LowLevel.Tell(handle);
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
}
}
2017-05-28 12:47:02 +00:00
public long Read(byte[] buffer, int offset, uint count)
2016-01-27 17:57:53 +00:00
{
2017-05-28 12:47:02 +00:00
return PhysFS.LowLevel.Read(handle, buffer, 1, count, offset);
2016-01-27 17:57:53 +00:00
}
public override int Read(byte[] buffer, int offset, int count)
{
2017-05-28 12:47:02 +00:00
return (int)Read(buffer, offset, (uint)count);
2016-01-27 17:57:53 +00:00
}
public override long Seek(long offset, SeekOrigin origin)
{
long pos;
2016-01-27 17:57:53 +00:00
if (origin == SeekOrigin.Begin)
pos = 0;
else if (origin == SeekOrigin.Current)
2017-05-26 11:14:23 +00:00
pos = PhysFS.LowLevel.Tell(handle);
2016-01-27 17:57:53 +00:00
else
2017-05-26 11:14:23 +00:00
pos = PhysFS.LowLevel.FileLength(handle);
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;
}
2020-01-18 03:16:09 +00:00
public long Write(byte[] buffer, uint count)
2016-01-27 17:57:53 +00:00
{
2017-05-26 11:14:23 +00:00
return PhysFS.LowLevel.Write(handle, buffer, 1, count);
2016-01-27 17:57:53 +00:00
}
public override void Write(byte[] buffer, int offset, int count)
{
2020-01-18 03:16:09 +00:00
Write(buffer, (uint)count);
2016-01-27 17:57:53 +00:00
}
2016-02-08 21:25:37 +00:00
public override void SetLength(long value)
2016-01-27 17:57:53 +00:00
{
2020-01-18 03:16:09 +00:00
throw new NotSupportedException();
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);
}
}
}