From 61b2d3c28f0cba1019aaee8b9a50066bd0cca26c Mon Sep 17 00:00:00 2001 From: mooooooi Date: Wed, 20 Aug 2025 01:04:31 +0800 Subject: [PATCH 1/2] asmdef + LoadLib --- Jolt.Native/Debug/Jolt.Native.asmdef | 16 + Jolt.Native/Debug/Jolt.Native.asmdef.meta | 3 + Jolt.Native/NativeLibrary.cs | 310 +++++++++--------- Jolt.Native/{ => Release}/Jolt.Native.asmdef | 4 +- .../{ => Release}/Jolt.Native.asmdef.meta | 0 Jolt.Native/Release/macos-arm64.meta | 8 + .../Release/macos-arm64/libjoltc.dylib.meta | 2 + Jolt/Types/PhysicsSystem.cs | 2 +- 8 files changed, 189 insertions(+), 156 deletions(-) create mode 100644 Jolt.Native/Debug/Jolt.Native.asmdef create mode 100644 Jolt.Native/Debug/Jolt.Native.asmdef.meta rename Jolt.Native/{ => Release}/Jolt.Native.asmdef (84%) rename Jolt.Native/{ => Release}/Jolt.Native.asmdef.meta (100%) create mode 100644 Jolt.Native/Release/macos-arm64.meta create mode 100644 Jolt.Native/Release/macos-arm64/libjoltc.dylib.meta diff --git a/Jolt.Native/Debug/Jolt.Native.asmdef b/Jolt.Native/Debug/Jolt.Native.asmdef new file mode 100644 index 0000000..7a3f69d --- /dev/null +++ b/Jolt.Native/Debug/Jolt.Native.asmdef @@ -0,0 +1,16 @@ +{ + "name": "Jolt.Native.Debug", + "rootNamespace": "", + "references": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": true, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [ + "!JOLT_RELEASE" + ], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Jolt.Native/Debug/Jolt.Native.asmdef.meta b/Jolt.Native/Debug/Jolt.Native.asmdef.meta new file mode 100644 index 0000000..d70d9f9 --- /dev/null +++ b/Jolt.Native/Debug/Jolt.Native.asmdef.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 83646f093ae00e84287a6b052ac4ffa7 +timeCreated: 1704908185 diff --git a/Jolt.Native/NativeLibrary.cs b/Jolt.Native/NativeLibrary.cs index 64d1927..831d917 100644 --- a/Jolt.Native/NativeLibrary.cs +++ b/Jolt.Native/NativeLibrary.cs @@ -1,154 +1,156 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; -using UnityEngine; -using UnityEngine.Scripting; - -// Unity quietly strips unreferenced assemblies when compiling for IL2CPP. AlwaysLinkAssembly -// ensures the Jolt.Native initialization code will run even when unreferenced. - -[assembly: AlwaysLinkAssembly] - -namespace Jolt.Native -{ - public static class NativeLibrary - { - [DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "LoadLibrary")] - private static extern IntPtr LoadLibraryWindows(string path); - - [DllImport("libc", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "dlopen")] - private static extern IntPtr LoadLibraryLinux(string path, int flags); - - [DllImport("libdl", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "dlopen")] - private static extern IntPtr LoadLibraryMacOS(string path, int flags); - - private static IntPtr libptr; - - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] - public static void LoadLibrary() - { - try - { - MaybeLoadLibrary(); - } - catch (Exception ex) - { - Debug.LogError(ex); - } - } - - private static void MaybeLoadLibrary() - { - if (libptr != IntPtr.Zero) - { - return; - } - - string libname; - - if (IsWindows()) - { - libname = "windows-x64\\joltc.dll"; - } - else if (IsLinux()) - { - libname = "linux-x64/libjoltc.so"; - } - else if (IsMacOS()) - { - libname = !IsArm64() - ? "macos-x64/libjoltc.dylib" - : "macos-arm64/libjoltc.dylib"; - } - else - { - throw new Exception("Unrecognized platform, unable to load native lib."); - } - - #if UNITY_EDITOR - var paths = EditorLibraryPaths(); - #else - var paths = RuntimeLibraryPaths(); - #endif - - foreach (var path in paths) - { - if (TryLoadLibrary(Path.Combine(path, libname), out libptr)) - { - Debug.Log($"Loaded Jolt library at {path}/{libname}"); - break; - } - } - - if (libptr == IntPtr.Zero) - { - throw new Exception("Failed to load native lib."); - } - } - - private static bool TryLoadLibrary(string path, out IntPtr handle) - { - handle = IntPtr.Zero; - - if (IsWindows()) - { - handle = LoadLibraryWindows(path); - } - else if (IsLinux()) - { - handle = LoadLibraryLinux(path, 0x101); - } - else if (IsMacOS()) - { - handle = LoadLibraryMacOS(path, 0x101); - } - - return handle != IntPtr.Zero; - } - - private static bool IsWindows() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - } - - private static bool IsLinux() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); - } - - private static bool IsMacOS() - { - return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); - } - - private static bool IsArm64() - { - return RuntimeInformation.OSArchitecture == Architecture.Arm64; - } - - private static string[] EditorLibraryPaths() - { - const string package = "com.seep.jolt"; - - #if JOLT_RELEASE - const string config = "Release"; - #else - const string config = "Debug"; - #endif - - return new[] - { - Path.GetFullPath($"Packages/{package}/Jolt.Native/{config}") - }; - } - - private static string[] RuntimeLibraryPaths() - { - return new[] - { - $"{Application.dataPath}/Plugins/x86_64", - $"{Application.dataPath}/Plugins" - }; - } - } -} +// #define JOLT_RELEASE +// +// using System; +// using System.IO; +// using System.Runtime.InteropServices; +// using UnityEngine; +// using UnityEngine.Scripting; +// +// // Unity quietly strips unreferenced assemblies when compiling for IL2CPP. AlwaysLinkAssembly +// // ensures the Jolt.Native initialization code will run even when unreferenced. +// +// [assembly: AlwaysLinkAssembly] +// +// namespace Jolt.Native +// { +// public static class NativeLibrary +// { +// [DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "LoadLibrary")] +// private static extern IntPtr LoadLibraryWindows(string path); +// +// [DllImport("libc", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "dlopen")] +// private static extern IntPtr LoadLibraryLinux(string path, int flags); +// +// [DllImport("libdl", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "dlopen")] +// private static extern IntPtr LoadLibraryMacOS(string path, int flags); +// +// private static IntPtr libptr; +// +// [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] +// public static void LoadLibrary() +// { +// try +// { +// MaybeLoadLibrary(); +// } +// catch (Exception ex) +// { +// Debug.LogError(ex); +// } +// } +// +// private static void MaybeLoadLibrary() +// { +// if (libptr != IntPtr.Zero) +// { +// return; +// } +// +// string libname; +// +// if (IsWindows()) +// { +// libname = "windows-x64\\joltc.dll"; +// } +// else if (IsLinux()) +// { +// libname = "linux-x64/libjoltc.so"; +// } +// else if (IsMacOS()) +// { +// libname = !IsArm64() +// ? "macos-x64/libjoltc.dylib" +// : "macos-arm64/libjoltc.dylib"; +// } +// else +// { +// throw new Exception("Unrecognized platform, unable to load native lib."); +// } +// +// #if UNITY_EDITOR +// var paths = EditorLibraryPaths(); +// #else +// var paths = RuntimeLibraryPaths(); +// #endif +// +// foreach (var path in paths) +// { +// if (TryLoadLibrary(Path.Combine(path, libname), out libptr)) +// { +// Debug.Log($"Loaded Jolt library at {path}/{libname}"); +// break; +// } +// } +// +// if (libptr == IntPtr.Zero) +// { +// throw new Exception("Failed to load native lib."); +// } +// } +// +// private static bool TryLoadLibrary(string path, out IntPtr handle) +// { +// handle = IntPtr.Zero; +// +// if (IsWindows()) +// { +// handle = LoadLibraryWindows(path); +// } +// else if (IsLinux()) +// { +// handle = LoadLibraryLinux(path, 0x101); +// } +// else if (IsMacOS()) +// { +// handle = LoadLibraryMacOS(path, 0x101); +// } +// +// return handle != IntPtr.Zero; +// } +// +// private static bool IsWindows() +// { +// return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); +// } +// +// private static bool IsLinux() +// { +// return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); +// } +// +// private static bool IsMacOS() +// { +// return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); +// } +// +// private static bool IsArm64() +// { +// return RuntimeInformation.OSArchitecture == Architecture.Arm64; +// } +// +// private static string[] EditorLibraryPaths() +// { +// const string package = "com.seep.jolt"; +// +// #if JOLT_RELEASE +// const string config = "Release"; +// #else +// const string config = "Debug"; +// #endif +// +// return new[] +// { +// Path.GetFullPath($"Packages/{package}/Jolt.Native/{config}") +// }; +// } +// +// private static string[] RuntimeLibraryPaths() +// { +// return new[] +// { +// $"{Application.dataPath}/Plugins/x86_64", +// $"{Application.dataPath}/Plugins" +// }; +// } +// } +// } diff --git a/Jolt.Native/Jolt.Native.asmdef b/Jolt.Native/Release/Jolt.Native.asmdef similarity index 84% rename from Jolt.Native/Jolt.Native.asmdef rename to Jolt.Native/Release/Jolt.Native.asmdef index 57e22ef..f753407 100644 --- a/Jolt.Native/Jolt.Native.asmdef +++ b/Jolt.Native/Release/Jolt.Native.asmdef @@ -8,7 +8,9 @@ "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [], + "defineConstraints": [ + "JOLT_RELEASE" + ], "versionDefines": [], "noEngineReferences": false } \ No newline at end of file diff --git a/Jolt.Native/Jolt.Native.asmdef.meta b/Jolt.Native/Release/Jolt.Native.asmdef.meta similarity index 100% rename from Jolt.Native/Jolt.Native.asmdef.meta rename to Jolt.Native/Release/Jolt.Native.asmdef.meta diff --git a/Jolt.Native/Release/macos-arm64.meta b/Jolt.Native/Release/macos-arm64.meta new file mode 100644 index 0000000..7ab05c7 --- /dev/null +++ b/Jolt.Native/Release/macos-arm64.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b441895565b08ce4a8eed7c1f4baa8e7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Jolt.Native/Release/macos-arm64/libjoltc.dylib.meta b/Jolt.Native/Release/macos-arm64/libjoltc.dylib.meta new file mode 100644 index 0000000..491ff6f --- /dev/null +++ b/Jolt.Native/Release/macos-arm64/libjoltc.dylib.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0d172af96b0f71c42815de979121e081 \ No newline at end of file diff --git a/Jolt/Types/PhysicsSystem.cs b/Jolt/Types/PhysicsSystem.cs index 251dd35..fb6278c 100644 --- a/Jolt/Types/PhysicsSystem.cs +++ b/Jolt/Types/PhysicsSystem.cs @@ -47,7 +47,7 @@ public void SetBodyActivationListener(BodyActivationListener listener) /// /// The out parameter will contain the error if any. /// - public bool Update(float deltaTime, int collisionSteps, JobSystem jobSystem, out PhysicsUpdateError error) + public readonly bool Step(float deltaTime, int collisionSteps, JobSystem jobSystem, out PhysicsUpdateError error) { return (error = JPH_PhysicsSystem_Update(Handle, deltaTime, collisionSteps, jobSystem.Handle)) == PhysicsUpdateError.None; } From 1e61e113f7b24fe341a5415c6d2c22e801417d7a Mon Sep 17 00:00:00 2001 From: mooooooi Date: Wed, 20 Aug 2025 13:52:51 +0800 Subject: [PATCH 2/2] chore: remove Debug Native plugins --- Jolt.Native/Debug.meta | 8 -------- Jolt.Native/{Debug => Debug~}/Jolt.Native.asmdef | 0 .../{Debug => Debug~}/Jolt.Native.asmdef.meta | 0 Jolt.Native/{Debug => Debug~}/linux-x64.meta | 0 Jolt.Native/{Debug => Debug~}/linux-x64/libjoltc.so | Bin .../{Debug => Debug~}/linux-x64/libjoltc.so.meta | 0 Jolt.Native/{Debug => Debug~}/macos-arm64.meta | 0 .../{Debug => Debug~}/macos-arm64/libjoltc.dylib | Bin .../macos-arm64/libjoltc.dylib.meta | 0 Jolt.Native/{Debug => Debug~}/macos-x64.meta | 0 .../{Debug => Debug~}/macos-x64/libjoltc.dylib | Bin .../{Debug => Debug~}/macos-x64/libjoltc.dylib.meta | 0 Jolt.Native/{Debug => Debug~}/windows-x64.meta | 0 Jolt.Native/{Debug => Debug~}/windows-x64/joltc.dll | Bin .../{Debug => Debug~}/windows-x64/joltc.dll.meta | 0 15 files changed, 8 deletions(-) delete mode 100644 Jolt.Native/Debug.meta rename Jolt.Native/{Debug => Debug~}/Jolt.Native.asmdef (100%) rename Jolt.Native/{Debug => Debug~}/Jolt.Native.asmdef.meta (100%) rename Jolt.Native/{Debug => Debug~}/linux-x64.meta (100%) rename Jolt.Native/{Debug => Debug~}/linux-x64/libjoltc.so (100%) rename Jolt.Native/{Debug => Debug~}/linux-x64/libjoltc.so.meta (100%) rename Jolt.Native/{Debug => Debug~}/macos-arm64.meta (100%) rename Jolt.Native/{Debug => Debug~}/macos-arm64/libjoltc.dylib (100%) rename Jolt.Native/{Debug => Debug~}/macos-arm64/libjoltc.dylib.meta (100%) rename Jolt.Native/{Debug => Debug~}/macos-x64.meta (100%) rename Jolt.Native/{Debug => Debug~}/macos-x64/libjoltc.dylib (100%) rename Jolt.Native/{Debug => Debug~}/macos-x64/libjoltc.dylib.meta (100%) rename Jolt.Native/{Debug => Debug~}/windows-x64.meta (100%) rename Jolt.Native/{Debug => Debug~}/windows-x64/joltc.dll (100%) rename Jolt.Native/{Debug => Debug~}/windows-x64/joltc.dll.meta (100%) diff --git a/Jolt.Native/Debug.meta b/Jolt.Native/Debug.meta deleted file mode 100644 index 11bbc8f..0000000 --- a/Jolt.Native/Debug.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 7aeea15d47a327349ae45afbb000712c -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Jolt.Native/Debug/Jolt.Native.asmdef b/Jolt.Native/Debug~/Jolt.Native.asmdef similarity index 100% rename from Jolt.Native/Debug/Jolt.Native.asmdef rename to Jolt.Native/Debug~/Jolt.Native.asmdef diff --git a/Jolt.Native/Debug/Jolt.Native.asmdef.meta b/Jolt.Native/Debug~/Jolt.Native.asmdef.meta similarity index 100% rename from Jolt.Native/Debug/Jolt.Native.asmdef.meta rename to Jolt.Native/Debug~/Jolt.Native.asmdef.meta diff --git a/Jolt.Native/Debug/linux-x64.meta b/Jolt.Native/Debug~/linux-x64.meta similarity index 100% rename from Jolt.Native/Debug/linux-x64.meta rename to Jolt.Native/Debug~/linux-x64.meta diff --git a/Jolt.Native/Debug/linux-x64/libjoltc.so b/Jolt.Native/Debug~/linux-x64/libjoltc.so similarity index 100% rename from Jolt.Native/Debug/linux-x64/libjoltc.so rename to Jolt.Native/Debug~/linux-x64/libjoltc.so diff --git a/Jolt.Native/Debug/linux-x64/libjoltc.so.meta b/Jolt.Native/Debug~/linux-x64/libjoltc.so.meta similarity index 100% rename from Jolt.Native/Debug/linux-x64/libjoltc.so.meta rename to Jolt.Native/Debug~/linux-x64/libjoltc.so.meta diff --git a/Jolt.Native/Debug/macos-arm64.meta b/Jolt.Native/Debug~/macos-arm64.meta similarity index 100% rename from Jolt.Native/Debug/macos-arm64.meta rename to Jolt.Native/Debug~/macos-arm64.meta diff --git a/Jolt.Native/Debug/macos-arm64/libjoltc.dylib b/Jolt.Native/Debug~/macos-arm64/libjoltc.dylib similarity index 100% rename from Jolt.Native/Debug/macos-arm64/libjoltc.dylib rename to Jolt.Native/Debug~/macos-arm64/libjoltc.dylib diff --git a/Jolt.Native/Debug/macos-arm64/libjoltc.dylib.meta b/Jolt.Native/Debug~/macos-arm64/libjoltc.dylib.meta similarity index 100% rename from Jolt.Native/Debug/macos-arm64/libjoltc.dylib.meta rename to Jolt.Native/Debug~/macos-arm64/libjoltc.dylib.meta diff --git a/Jolt.Native/Debug/macos-x64.meta b/Jolt.Native/Debug~/macos-x64.meta similarity index 100% rename from Jolt.Native/Debug/macos-x64.meta rename to Jolt.Native/Debug~/macos-x64.meta diff --git a/Jolt.Native/Debug/macos-x64/libjoltc.dylib b/Jolt.Native/Debug~/macos-x64/libjoltc.dylib similarity index 100% rename from Jolt.Native/Debug/macos-x64/libjoltc.dylib rename to Jolt.Native/Debug~/macos-x64/libjoltc.dylib diff --git a/Jolt.Native/Debug/macos-x64/libjoltc.dylib.meta b/Jolt.Native/Debug~/macos-x64/libjoltc.dylib.meta similarity index 100% rename from Jolt.Native/Debug/macos-x64/libjoltc.dylib.meta rename to Jolt.Native/Debug~/macos-x64/libjoltc.dylib.meta diff --git a/Jolt.Native/Debug/windows-x64.meta b/Jolt.Native/Debug~/windows-x64.meta similarity index 100% rename from Jolt.Native/Debug/windows-x64.meta rename to Jolt.Native/Debug~/windows-x64.meta diff --git a/Jolt.Native/Debug/windows-x64/joltc.dll b/Jolt.Native/Debug~/windows-x64/joltc.dll similarity index 100% rename from Jolt.Native/Debug/windows-x64/joltc.dll rename to Jolt.Native/Debug~/windows-x64/joltc.dll diff --git a/Jolt.Native/Debug/windows-x64/joltc.dll.meta b/Jolt.Native/Debug~/windows-x64/joltc.dll.meta similarity index 100% rename from Jolt.Native/Debug/windows-x64/joltc.dll.meta rename to Jolt.Native/Debug~/windows-x64/joltc.dll.meta