Skip to content

Commit

Permalink
dedupe
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey authored Jun 11, 2024
1 parent 2b542fc commit 678d013
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions src/Assimp/Silk.NET.Assimp/CustomFileIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,57 +138,39 @@ private static void CloseFile(FileIO* mFileSystem, File* pFile)
sHandle.Free();
SilkMarshal.Free((nint) pFile);
}

public static Stream GetStream(File* file) =>
GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s
? throw new InvalidOperationException("Invalid UserData for File.")
: s;

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static nuint ReadFile(File* file, byte* buffer, nuint size, nuint count) =>
GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s
? throw new InvalidOperationException("Invalid UserData for File.")
: (nuint) s.Read(new Span<byte>(buffer, (int) (size * count))) / size;
GetStream(file).Read(new Span<byte>(buffer, (int) (size * count))) / size;

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static nuint WriteFile(File* file, byte* buffer, nuint size, nuint count)
{
if (GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s)
{
throw new InvalidOperationException("Invalid UserData for File.");
}

s.Write(new Span<byte>(buffer, (int) (size * count)));
GetStream(file).Write(new Span<byte>(buffer, (int) (size * count)));
return count;
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static nuint FileSize(File* file) =>
GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s
? throw new InvalidOperationException("Invalid UserData for File.")
: (nuint) s.Length;
(nuint) GetStream(file).Length;

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static nuint FileTell(File* file) =>
GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s
? throw new InvalidOperationException("Invalid UserData for File.")
: (nuint) s.Position;
(nuint) GetStream(file).Position;

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static void FileFlush(File* file)
{
if (GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s)
{
throw new InvalidOperationException("Invalid UserData for File.");
}

s.Flush();
}
=> GetStream(file).Flush();

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static Return FileSeek(File* file, nuint offset, Origin origin)
{
if (GCHandle.FromIntPtr((nint) file->UserData).Target is not Stream s)
{
throw new InvalidOperationException("Invalid UserData for File.");
}

s.Seek((long) offset, (SeekOrigin) origin);
GetStream(file).Seek((long) offset, (SeekOrigin) origin);
return Return.Success;
}

Expand Down

0 comments on commit 678d013

Please sign in to comment.