diff --git a/src/dotnet/RagePhoto.cs b/src/dotnet/RagePhoto.cs index f5cf6bc..dea1a33 100644 --- a/src/dotnet/RagePhoto.cs +++ b/src/dotnet/RagePhoto.cs @@ -10,72 +10,75 @@ namespace Syping.RagePhoto { private readonly IntPtr _instance; private const String _library = "libragephoto"; - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_open(); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_clear(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_close(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_load(IntPtr instance, Byte[] data, UIntPtr size); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_loadfile(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String filename); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern Int32 ragephoto_error(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_getphotodata(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_getphotodesc(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern UInt32 ragephoto_getphotoformat(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_getphotojpeg(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_getphotojson(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_getphotoheader(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern UInt64 ragephoto_getphotosign(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern UInt32 ragephoto_getphotosize(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_getphototitle(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern UIntPtr ragephoto_getsavesize(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern UIntPtr ragephoto_getsavesizef(IntPtr instance, UInt32 photoFormat); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_save(IntPtr instance, [Out] Byte[] data); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_savef(IntPtr instance, [Out] Byte[] data, UInt32 photoFormat); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_savefile(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String filename); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_savefilef(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String filename, UInt32 photoFormat); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setbufferdefault(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setbufferoffsets(IntPtr instance); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] + [return: MarshalAs(UnmanagedType.I1)] + private static extern bool ragephoto_setphotodatac(IntPtr instance, IntPtr data); + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setphotodesc(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String description, UInt32 bufferSize); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setphotoformat(IntPtr instance, UInt32 photoFormat); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool ragephoto_setphotojpeg(IntPtr instance, Byte[] jpeg, UInt32 size, UInt32 bufferSize); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setphotojson(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String json, UInt32 bufferSize); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setphotoheader2(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String header, UInt32 headerSum, UInt32 headerSum2); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern void ragephoto_setphototitle(IntPtr instance, [MarshalAs(UnmanagedType.LPUTF8Str)] String title, UInt32 bufferSize); - [DllImport(_library)] + [DllImport(_library, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ragephoto_version(); public Photo() { @@ -89,6 +92,19 @@ namespace Syping.RagePhoto { } } + public Photo(Photo photo) { + try { + _instance = ragephoto_open(); + if (_instance == IntPtr.Zero) + throw new RagePhotoException(this, "Failed to initialize libragephoto"); + } + catch (Exception exception) { + throw new RagePhotoException(this, "Failed to initialize libragephoto", exception); + } + if (!ragephoto_setphotodatac(_instance, ragephoto_getphotodata(photo._instance))) + throw new RagePhotoException(this, string.Format("Failed to copy Photo: {0}", Error)); + } + ~Photo() { Dispose(false); } @@ -110,21 +126,15 @@ namespace Syping.RagePhoto { } public void Load(Byte[] data) { - bool result = ragephoto_load(_instance, data, (UIntPtr)data.LongLength); - if (!result) + if (!ragephoto_load(_instance, data, (UIntPtr)data.LongLength)) throw new RagePhotoException(this, string.Format("Failed to load Photo: {0}", Error)); } public void LoadFile(String path) { - bool result = ragephoto_loadfile(_instance, path); - if (!result) + if (!ragephoto_loadfile(_instance, path)) throw new RagePhotoException(this, string.Format("Failed to load Photo: {0}", Error)); } - public RagePhotoData Data { - get => Marshal.PtrToStructure(ragephoto_getphotodata(_instance)); - } - public String Description { get => PtrToStringUTF8(ragephoto_getphotodesc(_instance)); set => ragephoto_setphotodesc(_instance, value, (UInt32)DefaultSize.DEFAULT_DESCBUFFER); @@ -188,29 +198,25 @@ namespace Syping.RagePhoto { public Byte[] Save() { Byte[] photo = new Byte[Environment.Is64BitProcess ? (UInt64)GetSaveSize() : (UInt32)GetSaveSize()]; - bool result = ragephoto_save(_instance, photo); - if (!result) + if (!ragephoto_save(_instance, photo)) throw new RagePhotoException(this, string.Format("Failed to save Photo: {0}", Error)); return photo; } public Byte[] Save(PhotoFormat photoFormat) { Byte[] photo = new Byte[Environment.Is64BitProcess ? (UInt64)GetSaveSize(photoFormat) : (UInt32)GetSaveSize(photoFormat)]; - bool result = ragephoto_savef(_instance, photo, (UInt32)photoFormat); - if (!result) + if (!ragephoto_savef(_instance, photo, (UInt32)photoFormat)) throw new RagePhotoException(this, string.Format("Failed to save Photo: {0}", Error)); return photo; } public void SaveFile(String path) { - bool result = ragephoto_savefile(_instance, path); - if (!result) + if (!ragephoto_savefile(_instance, path)) throw new RagePhotoException(this, string.Format("Failed to save Photo: {0}", Error)); } public void SaveFile(String path, PhotoFormat photoFormat) { - bool result = ragephoto_savefilef(_instance, path, (UInt32)photoFormat); - if (!result) + if (!ragephoto_savefilef(_instance, path, (UInt32)photoFormat)) throw new RagePhotoException(this, string.Format("Failed to save Photo: {0}", Error)); } @@ -254,14 +260,12 @@ namespace Syping.RagePhoto { bufferSize = size; break; } - bool result = ragephoto_setphotojpeg(_instance, jpeg, size, bufferSize); - if (!result) + if (!ragephoto_setphotojpeg(_instance, jpeg, size, bufferSize)) throw new RagePhotoException(this, string.Format("Failed to set Jpeg: {0}", Error)); } public void SetJpeg(Byte[] jpeg, UInt32 bufferSize) { - bool result = ragephoto_setphotojpeg(_instance, jpeg, (UInt32)jpeg.Length, bufferSize); - if (!result) + if (!ragephoto_setphotojpeg(_instance, jpeg, (UInt32)jpeg.Length, bufferSize)) throw new RagePhotoException(this, string.Format("Failed to set Jpeg: {0}", Error)); } diff --git a/src/dotnet/RagePhotoData.cs b/src/dotnet/RagePhotoData.cs deleted file mode 100644 index 1a164a5..0000000 --- a/src/dotnet/RagePhotoData.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace Syping.RagePhoto { - - [StructLayout(LayoutKind.Sequential)] - public readonly struct RagePhotoData { - readonly IntPtr jpeg; - readonly IntPtr description; - readonly IntPtr json; - readonly IntPtr header; - readonly IntPtr title; - readonly Int32 error; - readonly UInt32 descBuffer; - readonly UInt32 descOffset; - readonly UInt32 endOfFile; - readonly UInt32 headerSum; - readonly UInt32 headerSum2; - readonly UInt32 jpegBuffer; - readonly UInt32 jpegSize; - readonly UInt32 jsonBuffer; - readonly UInt32 jsonOffset; - readonly UInt32 photoFormat; - readonly UInt32 titlBuffer; - readonly UInt32 titlOffset; - } -} diff --git a/src/dotnet/Syping.RagePhoto.Core.csproj b/src/dotnet/Syping.RagePhoto.Core.csproj index b085894..7ccc6c7 100644 --- a/src/dotnet/Syping.RagePhoto.Core.csproj +++ b/src/dotnet/Syping.RagePhoto.Core.csproj @@ -4,9 +4,9 @@ netstandard2.1;net47 Syping.RagePhoto.Core Syping.RagePhoto - 0.6.1.4 - 0.6.1.4 - 0.6.1.4 + 0.6.1.5 + 0.6.1.5 + 0.6.1.5 Syping Copyright © 2025 Syping Open Source RAGE Photo Parser for GTA V and RDR 2