Skip to content

Commit 46ac169

Browse files
committed
chore: better resolve stuff
1 parent 34f4358 commit 46ac169

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Ivy/Core/NativeJsonDiff.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.IO;
3+
using System.Reflection;
24
using System.Runtime.InteropServices;
35
using System.Text.Json.Nodes;
46

@@ -12,6 +14,53 @@ public static class NativeJsonDiff
1214
{
1315
private const string RustLib = "rustserver";
1416

17+
static NativeJsonDiff()
18+
{
19+
NativeLibrary.SetDllImportResolver(typeof(NativeJsonDiff).Assembly, ResolveDllImport);
20+
}
21+
22+
private static IntPtr ResolveDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
23+
{
24+
if (libraryName != RustLib) return IntPtr.Zero;
25+
26+
// Try default load first
27+
if (NativeLibrary.TryLoad(libraryName, assembly, searchPath, out var handle))
28+
return handle;
29+
30+
// Fallback to searching runtimes/ folder
31+
var rid = GetRuntimeIdentifier();
32+
var libFileName = GetLibraryFileName();
33+
var probePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "runtimes", rid, "native", libFileName);
34+
35+
if (File.Exists(probePath) && NativeLibrary.TryLoad(probePath, out handle))
36+
return handle;
37+
38+
return IntPtr.Zero;
39+
}
40+
41+
private static string GetRuntimeIdentifier()
42+
{
43+
var os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" :
44+
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
45+
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" : "unknown";
46+
47+
var arch = RuntimeInformation.ProcessArchitecture switch
48+
{
49+
Architecture.X64 => "x64",
50+
Architecture.Arm64 => "arm64",
51+
_ => "unknown"
52+
};
53+
54+
return $"{os}-{arch}";
55+
}
56+
57+
private static string GetLibraryFileName()
58+
{
59+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return "rustserver.dll";
60+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return "librustserver.dylib";
61+
return "librustserver.so";
62+
}
63+
1564
[DllImport(RustLib, CallingConvention = CallingConvention.Cdecl)]
1665
private static extern IntPtr rustserver_diff_trees(
1766
IntPtr old_json_ptr, int old_len,

src/Ivy/Ivy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
<!-- macOS local fallbacks, isolated to prevent MSB3021 -->
137137
<Content Include="../RustServer/target/release/librustserver.dylib" Pack="true" PackagePath="runtimes/osx-arm64/native" CopyToOutputDirectory="PreserveNewest" Link="runtimes/osx-arm64/native/librustserver.dylib" Condition="!Exists('../RustServer/artifacts/native/osx-arm64/librustserver.dylib') AND Exists('../RustServer/target/release/librustserver.dylib') AND '$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)' == 'Arm64' AND $([MSBuild]::IsOSPlatform('OSX'))" />
138+
<Content Include="../RustServer/target/release/librustserver.dylib" Pack="false" CopyToOutputDirectory="PreserveNewest" Link="librustserver.dylib" Condition="!Exists('../RustServer/artifacts/native/osx-arm64/librustserver.dylib') AND Exists('../RustServer/target/release/librustserver.dylib') AND '$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)' == 'Arm64' AND $([MSBuild]::IsOSPlatform('OSX'))" />
138139
<Content Include="../RustServer/target/release/librustserver.dylib" Pack="true" PackagePath="runtimes/osx-x64/native" CopyToOutputDirectory="PreserveNewest" Link="runtimes/osx-x64/native/librustserver.dylib" Condition="!Exists('../RustServer/artifacts/native/osx-x64/librustserver.dylib') AND Exists('../RustServer/target/release/librustserver.dylib') AND '$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)' == 'X64' AND $([MSBuild]::IsOSPlatform('OSX'))" />
139140
</ItemGroup>
140141
</Target>

0 commit comments

Comments
 (0)