Skip to content

Commit

Permalink
Start replacing custom library loading mechanism with LibraryImport
Browse files Browse the repository at this point in the history
  • Loading branch information
Saalvage committed Nov 29, 2024
1 parent 830653e commit 2ea6481
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
26 changes: 13 additions & 13 deletions AssimpNet/AssimpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ public Scene ImportFile(string file)
/// <exception cref="AssimpException">Thrown if there was a general error in importing the model.</exception>
/// <exception cref="System.IO.FileNotFoundException">Thrown if the file could not be located.</exception>
/// <exception cref="System.ObjectDisposedException">Thrown if the context has already been disposed of.</exception>
public Scene ImportFile(string file, PostProcessSteps postProcessFlags)
public unsafe Scene ImportFile(string file, PostProcessSteps postProcessFlags)
{
CheckDisposed();

IntPtr ptr = IntPtr.Zero;
AiScene* ptr = null;
IntPtr fileIO = IntPtr.Zero;

//Only do file checks if not using a custom IOSystem
Expand All @@ -259,24 +259,24 @@ public Scene ImportFile(string file, PostProcessSteps postProcessFlags)

try
{
ptr = AssimpLibrary.Instance.ImportFile(file, PostProcessSteps.None, fileIO, m_propStore);
ptr = AssimpLibrary.aiImportFileExWithProperties(file, PostProcessSteps.None, (AiFileIO*)fileIO, m_propStore);

if(ptr == IntPtr.Zero)
if(ptr == null)
throw new AssimpException("Error importing file: " + AssimpLibrary.Instance.GetErrorString());

TransformScene(ptr);
TransformScene(new(ptr));

ptr = ApplyPostProcessing(ptr, postProcessFlags);
ptr = (AiScene*)ApplyPostProcessing(new(ptr), postProcessFlags);

return Scene.FromUnmanagedScene(ptr);
return Scene.FromUnmanagedScene(new(ptr));
}
finally
{
CleanupImport();

if(ptr != IntPtr.Zero)
if(ptr != null)
{
AssimpLibrary.Instance.ReleaseImport(ptr);
AssimpLibrary.Instance.ReleaseImport(new(ptr));
}
}
}
Expand Down Expand Up @@ -444,7 +444,7 @@ public bool ConvertFromFileToFile(string inputFilename, string outputFilename, s
/// <exception cref="AssimpException">Thrown if there was a general error in importing the model.</exception>
/// <exception cref="System.IO.FileNotFoundException">Thrown if the file could not be located.</exception>
/// <exception cref="System.ObjectDisposedException">Thrown if the context has already been disposed of.</exception>
public bool ConvertFromFileToFile(string inputFilename, PostProcessSteps importProcessSteps, string outputFilename, string exportFormatId, PostProcessSteps exportProcessSteps)
public unsafe bool ConvertFromFileToFile(string inputFilename, PostProcessSteps importProcessSteps, string outputFilename, string exportFormatId, PostProcessSteps exportProcessSteps)
{
CheckDisposed();

Expand All @@ -468,7 +468,7 @@ public bool ConvertFromFileToFile(string inputFilename, PostProcessSteps importP

try
{
ptr = AssimpLibrary.Instance.ImportFile(inputFilename, PostProcessSteps.None, fileIO, m_propStore);
ptr = (IntPtr)AssimpLibrary.aiImportFileExWithProperties(inputFilename, PostProcessSteps.None, (AiFileIO*)fileIO, m_propStore);

if(ptr == IntPtr.Zero)
throw new AssimpException("Error importing file: " + AssimpLibrary.Instance.GetErrorString());
Expand Down Expand Up @@ -534,7 +534,7 @@ public ExportDataBlob ConvertFromFileToBlob(string inputFilename, string exportF
/// <exception cref="AssimpException">Thrown if there was a general error in importing the model.</exception>
/// <exception cref="System.IO.FileNotFoundException">Thrown if the file could not be located.</exception>
/// <exception cref="System.ObjectDisposedException">Thrown if the context has already been disposed of.</exception>
public ExportDataBlob ConvertFromFileToBlob(string inputFilename, PostProcessSteps importProcessSteps, string exportFormatId, PostProcessSteps exportProcessSteps)
public unsafe ExportDataBlob ConvertFromFileToBlob(string inputFilename, PostProcessSteps importProcessSteps, string exportFormatId, PostProcessSteps exportProcessSteps)
{
CheckDisposed();

Expand All @@ -558,7 +558,7 @@ public ExportDataBlob ConvertFromFileToBlob(string inputFilename, PostProcessSte

try
{
ptr = AssimpLibrary.Instance.ImportFile(inputFilename, PostProcessSteps.None, fileIO, m_propStore);
ptr = (IntPtr)AssimpLibrary.aiImportFileExWithProperties(inputFilename, PostProcessSteps.None, (AiFileIO*)fileIO, m_propStore);

if(ptr == IntPtr.Zero)
throw new AssimpException("Error importing file: " + AssimpLibrary.Instance.GetErrorString());
Expand Down
22 changes: 6 additions & 16 deletions AssimpNet/Unmanaged/AssimpLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Assimp.Unmanaged
/// <summary>
/// Singleton that governs access to the unmanaged Assimp library functions.
/// </summary>
public sealed class AssimpLibrary : UnmanagedLibrary
public sealed partial class AssimpLibrary : UnmanagedLibrary
{
private static readonly object s_sync = new object();

Expand Down Expand Up @@ -83,9 +83,9 @@ private static AssimpLibrary CreateInstance()
/// <param name="flags">Post process flags specifying what steps are to be run after the import.</param>
/// <param name="propStore">Property store containing config name-values, may be null.</param>
/// <returns>Pointer to the unmanaged data structure.</returns>
public IntPtr ImportFile(string file, PostProcessSteps flags, IntPtr propStore)
public static unsafe IntPtr ImportFile(string file, PostProcessSteps flags, IntPtr propStore)
{
return ImportFile(file, flags, IntPtr.Zero, propStore);
return (IntPtr)aiImportFileExWithProperties(file, flags, null, propStore);
}

/// <summary>
Expand All @@ -97,14 +97,8 @@ public IntPtr ImportFile(string file, PostProcessSteps flags, IntPtr propStore)
/// any associated file the loader needs to open, passing NULL uses the default implementation.</param>
/// <param name="propStore">Property store containing config name-values, may be null.</param>
/// <returns>Pointer to the unmanaged data structure.</returns>
public IntPtr ImportFile(string file, PostProcessSteps flags, IntPtr fileIO, IntPtr propStore)
{
LoadIfNotLoaded();

Functions.aiImportFileExWithProperties func = GetFunction<Functions.aiImportFileExWithProperties>(FunctionNames.aiImportFileExWithProperties);

return func(file, (uint) flags, fileIO, propStore);
}
[LibraryImport("assimp", StringMarshalling = StringMarshalling.Utf8)]
public static unsafe partial AiScene* aiImportFileExWithProperties(string file, PostProcessSteps flags, AiFileIO* fileIO, IntPtr propStore);

/// <summary>
/// Imports a scene from a stream. This uses the "aiImportFileFromMemory" function. The stream can be from anyplace,
Expand Down Expand Up @@ -1076,7 +1070,6 @@ internal static class FunctionNames

public const string aiImportFile = "aiImportFile";
public const string aiImportFileEx = "aiImportFileEx";
public const string aiImportFileExWithProperties = "aiImportFileExWithProperties";
public const string aiImportFileFromMemory = "aiImportFileFromMemory";
public const string aiImportFileFromMemoryWithProperties = "aiImportFileFromMemoryWithProperties";
public const string aiReleaseImport = "aiReleaseImport";
Expand Down Expand Up @@ -1176,7 +1169,7 @@ internal static class FunctionNames
/// <summary>
/// Defines all of the delegates that represent the unmanaged assimp functions.
/// </summary>
internal static class Functions
internal static partial class Functions
{

#region Import Delegates
Expand All @@ -1187,9 +1180,6 @@ internal static class Functions
[UnmanagedFunctionPointer(CallingConvention.Cdecl), UnmanagedFunctionName(FunctionNames.aiImportFileEx)]
public delegate IntPtr aiImportFileEx([In, MarshalAs(UnmanagedType.LPUTF8Str)] string file, uint flags, IntPtr fileIO);

[UnmanagedFunctionPointer(CallingConvention.Cdecl), UnmanagedFunctionName(FunctionNames.aiImportFileExWithProperties)]
public delegate IntPtr aiImportFileExWithProperties([In, MarshalAs(UnmanagedType.LPUTF8Str)] string file, uint flag, IntPtr fileIO, IntPtr propStore);

[UnmanagedFunctionPointer(CallingConvention.Cdecl), UnmanagedFunctionName(FunctionNames.aiImportFileFromMemory)]
public delegate IntPtr aiImportFileFromMemory(byte[] buffer, uint bufferLength, uint flags, [In, MarshalAs(UnmanagedType.LPUTF8Str)] string formatHint);

Expand Down

0 comments on commit 2ea6481

Please sign in to comment.