Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2026.2.2.0</Version>
<Version>2026.4.11.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
Expand All @@ -12,7 +12,7 @@
<IsApplicationProject>$(MSBuildProjectDirectory.Contains('applications'))</IsApplicationProject>
<IsLibraryProject>$(MSBuildProjectDirectory.Contains('libraries'))</IsLibraryProject>
<NeedPackage>False</NeedPackage>
<NeedPackage Condition="'$(MSBuildProjectName)' == 'TedToolkit.Grasshopper'">True</NeedPackage>
<NeedPackage Condition="'$(MSBuildProjectName)' == 'TedToolkit.CppInteropGen'">True</NeedPackage>
<NeedRoslyn>False</NeedRoslyn>
<NeedRoslyn Condition="!$(IsTestProject) and $(IsRoslynProject)">True</NeedRoslyn>
<NeedAlmostAllFrameWorks>False</NeedAlmostAllFrameWorks>
Expand Down
24 changes: 10 additions & 14 deletions src/libraries/TedToolkit.CppInteropGen/CppObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ namespace TedToolkit.CppInteropGen;
public abstract class CppObject(bool disposed = false) : IDisposable
{
private bool _disposed = disposed;
private NativeFunctionLoader? _loader;
protected abstract string FileName { get; }

private static readonly string Rid = (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
throw new PlatformNotSupportedException("Unsupported OS platform"))
+ "-" + RuntimeInformation.OSArchitecture.ToString().ToLower();

private string FileFullPath
{
Expand All @@ -18,22 +25,11 @@ private string FileFullPath
var fullPath = Path.Combine(basePath, fileName);
if (File.Exists(fullPath)) return fullPath;

var ridPath = Path.Combine(basePath, "runtimes", GetRid(), "native", fileName);
var ridPath = Path.Combine(basePath, "runtimes", Rid, "native", fileName);
if (File.Exists(ridPath)) return ridPath;

throw new DllNotFoundException($"Native library not found: {fullPath} or {ridPath}");

static string GetRid()
{
var prefix =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
throw new PlatformNotSupportedException("Unsupported OS platform");

return prefix + "-" + RuntimeInformation.OSArchitecture.ToString().ToLower();
}


static string GetLibraryFileName(string baseName)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down Expand Up @@ -61,7 +57,7 @@ void IDisposable.Dispose()
/// Get the Method Pointer.
/// </summary>
protected IntPtr GetFunctionPointer(string name) =>
NativeFunctionLoader.GetLoader(FileFullPath).GetFunctionPointer(name);
(_loader ??= NativeFunctionLoader.GetLoader(FileFullPath)).GetFunctionPointer(name);

[DebuggerStepThrough]
protected unsafe void ThrowIfError(IntPtr errorPtr)
Expand Down