MoonTools.NETPhysFS/SharpPhysFS/PhysFS.LowLevel.cs

100 lines
2.6 KiB
C#
Raw Normal View History

2016-02-08 21:25:37 +00:00
using System;
2017-05-28 12:47:02 +00:00
using System.Runtime.InteropServices;
2016-02-08 21:25:37 +00:00
2020-01-18 03:16:09 +00:00
namespace MoonTools.NETPhysFS
2016-02-08 21:25:37 +00:00
{
2017-05-28 12:47:02 +00:00
public sealed partial class PhysFS
2016-02-08 21:25:37 +00:00
{
internal static class LowLevel
2016-02-08 21:25:37 +00:00
{
public static IntPtr OpenWrite(string filename, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
var val = Interop.PHYSFS_openWrite(filename);
if (val == IntPtr.Zero)
2016-02-08 21:25:37 +00:00
throw new PhysFSException(physFS);
return val;
}
public static IntPtr OpenAppend(string filename, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
var val = Interop.PHYSFS_openAppend(filename);
if (val == IntPtr.Zero)
2016-02-08 21:25:37 +00:00
throw new PhysFSException(physFS);
return val;
}
public static IntPtr OpenRead(string filename, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
var val = Interop.PHYSFS_openRead(filename);
if (val == IntPtr.Zero)
2016-02-08 21:25:37 +00:00
throw new PhysFSException(physFS);
return val;
}
public static void Close(IntPtr file, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
int err = Interop.PHYSFS_close(file);
2016-02-08 21:25:37 +00:00
physFS.ThrowException(err);
}
2017-05-28 12:47:02 +00:00
public static long Read(IntPtr file, byte[] buffer, uint objSize, uint objCount, int offset)
2016-02-08 21:25:37 +00:00
{
2017-05-28 12:47:02 +00:00
if (buffer.Length < (objSize * objCount) + offset)
2016-02-08 21:25:37 +00:00
{
2017-05-28 12:47:02 +00:00
throw new InvalidOperationException("Buffer too small");
2016-02-08 21:25:37 +00:00
}
2017-05-28 12:47:02 +00:00
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
long ret = Interop.PHYSFS_read(file, IntPtr.Add(handle.AddrOfPinnedObject(), offset), 1, objCount);
handle.Free();
return ret;
2016-02-08 21:25:37 +00:00
}
2017-05-26 11:14:23 +00:00
public static long Write(IntPtr file, byte[] buffer, uint objSize, uint objCount)
2016-02-08 21:25:37 +00:00
{
unsafe
{
fixed (void* ptr = buffer)
{
2017-05-26 11:14:23 +00:00
return Interop.PHYSFS_write(file, (IntPtr)ptr, objSize, objCount);
2016-02-08 21:25:37 +00:00
}
}
}
2017-05-26 11:14:23 +00:00
public static bool EOF(IntPtr file)
2016-02-08 21:25:37 +00:00
{
2017-05-26 11:14:23 +00:00
return Interop.PHYSFS_eof(file) != 0;
2016-02-08 21:25:37 +00:00
}
2017-05-26 11:14:23 +00:00
public static long Tell(IntPtr file)
2016-02-08 21:25:37 +00:00
{
2017-05-26 11:14:23 +00:00
return Interop.PHYSFS_tell(file);
2016-02-08 21:25:37 +00:00
}
public static void Seek(IntPtr file, ulong pos, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
int err = Interop.PHYSFS_seek(file, pos);
2016-02-08 21:25:37 +00:00
physFS.ThrowException(err);
}
2017-05-26 11:14:23 +00:00
public static long FileLength(IntPtr file)
2016-02-08 21:25:37 +00:00
{
2017-05-26 11:14:23 +00:00
return Interop.PHYSFS_fileLength(file);
2016-02-08 21:25:37 +00:00
}
public static void SetBuffer(IntPtr file, ulong bufSize, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
int err = Interop.PHYSFS_setBuffer(file, bufSize);
2016-02-08 21:25:37 +00:00
physFS.ThrowException(err);
}
public static void Flush(IntPtr file, PhysFS physFS)
{
2017-05-26 11:14:23 +00:00
int err = Interop.PHYSFS_flush(file);
2016-02-08 21:25:37 +00:00
physFS.ThrowException(err);
}
}
}
}