From 678d0139496e4f2d4ab56e6852a7cd7f7f363829 Mon Sep 17 00:00:00 2001 From: Dylan Perks <11160611+Perksey@users.noreply.github.com> Date: Mon, 10 Jun 2024 22:33:05 -0500 Subject: [PATCH] dedupe --- src/Assimp/Silk.NET.Assimp/CustomFileIO.cs | 40 ++++++---------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/src/Assimp/Silk.NET.Assimp/CustomFileIO.cs b/src/Assimp/Silk.NET.Assimp/CustomFileIO.cs index cdc9e3b273..4f8d78eaab 100644 --- a/src/Assimp/Silk.NET.Assimp/CustomFileIO.cs +++ b/src/Assimp/Silk.NET.Assimp/CustomFileIO.cs @@ -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(buffer, (int) (size * count))) / size; + GetStream(file).Read(new Span(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(buffer, (int) (size * count))); + GetStream(file).Write(new Span(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; }