2024-03-19 20:02:20 +01:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
2024-06-14 17:24:38 +02:00
|
|
|
namespace ReFuel.Stb
|
2024-03-19 20:02:20 +01:00
|
|
|
{
|
2024-06-14 17:24:38 +02:00
|
|
|
public unsafe delegate int StbiReadProc(void *userdata, byte* buffer, int count);
|
|
|
|
public unsafe delegate void StbiSkipProc(void *userdata, int count);
|
|
|
|
public unsafe delegate int StbiEofProc(void *userdata);
|
|
|
|
|
2024-03-19 20:02:20 +01:00
|
|
|
public unsafe class StbiStreamWrapper : IDisposable
|
|
|
|
{
|
|
|
|
private Stream _stream;
|
|
|
|
private bool _keepOpen;
|
|
|
|
private bool _isDisposed;
|
|
|
|
|
2024-06-14 17:24:38 +02:00
|
|
|
private StbiReadProc _readCb;
|
|
|
|
private StbiSkipProc _skipCb;
|
|
|
|
private StbiEofProc _eofCb;
|
2024-03-19 20:02:20 +01:00
|
|
|
|
|
|
|
public StbiStreamWrapper(Stream stream, bool keepOpen = false)
|
|
|
|
{
|
|
|
|
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
|
|
|
|
|
|
|
_stream = stream;
|
|
|
|
_keepOpen = keepOpen;
|
2024-06-14 17:24:38 +02:00
|
|
|
|
|
|
|
_readCb = ReadCb;
|
|
|
|
_skipCb = SkipCb;
|
|
|
|
_eofCb = EofCb;
|
2024-03-19 20:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CreateCallbacks(out stbi_io_callbacks cb)
|
|
|
|
{
|
|
|
|
cb = default;
|
2024-06-14 17:24:38 +02:00
|
|
|
cb.read = Marshal.GetFunctionPointerForDelegate<StbiReadProc>(_readCb);
|
|
|
|
cb.skip = Marshal.GetFunctionPointerForDelegate<StbiSkipProc>(_skipCb);
|
|
|
|
cb.eof = Marshal.GetFunctionPointerForDelegate<StbiEofProc>(_eofCb);
|
2024-03-19 20:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private int ReadCb(void *userdata, byte* buffer, int count)
|
|
|
|
{
|
|
|
|
Span<byte> bytes = new Span<byte>(buffer, count);
|
|
|
|
return _stream.Read(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SkipCb(void *userdata, int count)
|
|
|
|
{
|
|
|
|
_stream.Seek(count, SeekOrigin.Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int EofCb(void *userdata)
|
|
|
|
{
|
|
|
|
if (!_stream.CanRead || _stream.Position == _stream.Length)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (_isDisposed) return;
|
|
|
|
|
|
|
|
if (!_keepOpen) _stream.Dispose();
|
|
|
|
|
|
|
|
_isDisposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|