Skip to content

Commit 792b482

Browse files
committed
OSX support (not tested)
1 parent 0cf92aa commit 792b482

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/DllManipulator.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public class DllManipulator : MonoBehaviour
2828
dllPathPattern = "{assets}/Plugins/__{name}.dll",
2929
#elif UNITY_STANDALONE_LINUX
3030
dllPathPattern = "{assets}/Plugins/__{name}.so",
31+
#elif UNITY_STANDALONE_OSX
32+
dllPathPattern = "{assets}/Plugins/__{name}.dylib",
3133
#endif
3234
loadingMode = DllLoadingMode.Lazy,
33-
linuxDlopenFlags = LinuxDlopenFlags.Lazy,
35+
unixDlopenFlags = UnixDlopenFlags.Lazy,
3436
threadSafe = false,
3537
mockAllNativeFunctions = false,
3638
mockCallsInAllTypes = false,
@@ -582,7 +584,9 @@ private static IntPtr SysLoadDll(string filepath)
582584
#if UNITY_STANDALONE_WIN
583585
return PInvokes.Windows_LoadLibrary(filepath);
584586
#elif UNITY_STANDALONE_LINUX
585-
return PInvokes.Linux_dlopen(filepath, (int)_options.linuxDlopenFlags);
587+
return PInvokes.Linux_dlopen(filepath, (int)_options.unixDlopenFlags);
588+
#elif UNITY_STANDALONE_OSX
589+
return PInvokes.Osx_dlopen(filepath, (int)_options.unixDlopenFlags);
586590
#endif
587591
}
588592

@@ -592,6 +596,8 @@ private static bool SysUnloadDll(IntPtr libHandle)
592596
return PInvokes.Windows_FreeLibrary(libHandle);
593597
#elif UNITY_STANDALONE_LINUX
594598
return PInvokes.Linux_dlclose(libHandle) == 0;
599+
#elif UNITY_STANDALONE_OSX
600+
return PInvokes.Osx_dlclose(libHandle) == 0;
595601
#endif
596602
}
597603

@@ -601,6 +607,8 @@ private static IntPtr SysGetDllProcAddress(IntPtr libHandle, string symbol)
601607
return PInvokes.Windows_GetProcAddress(libHandle, symbol);
602608
#elif UNITY_STANDALONE_LINUX
603609
return PInvokes.Linux_dlsym(libHandle, symbol);
610+
#elif UNITY_STANDALONE_OSX
611+
return PInvokes.Osx_dlsym(libHandle, symbol);
604612
#endif
605613
}
606614
}
@@ -610,7 +618,7 @@ public class DllManipulatorOptions
610618
{
611619
public string dllPathPattern;
612620
public DllLoadingMode loadingMode;
613-
public LinuxDlopenFlags linuxDlopenFlags;
621+
public UnixDlopenFlags unixDlopenFlags;
614622
public bool threadSafe;
615623
public bool mockAllNativeFunctions;
616624
public bool mockCallsInAllTypes;
@@ -622,7 +630,7 @@ public enum DllLoadingMode
622630
Preload
623631
}
624632

625-
public enum LinuxDlopenFlags : int
633+
public enum UnixDlopenFlags : int
626634
{
627635
Lazy = 0x00001,
628636
Now = 0x00002,

src/Editor/DllManipulatorEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class DllManipulatorEditor : Editor
1717
"Specifies how DLLs and functions will be loaded.\n\n" +
1818
"Lazy - All DLLs and functions are loaded as they're first called. This allows them to be easily unloaded and loaded within game execution.\n\n" +
1919
"Preloaded - Slight performance benefit over Lazy mode. All DLLs and functions are loaded at startup (OnEnable()). In mid-execution it's safest to manipulate DLLs if game is paused.");
20-
private readonly GUIContent LINUX_DLOPEN_FLAGS_GUI_CONTENT = new GUIContent("dlopen flags",
21-
"Flags used in dlopen() P/Invoke on Linux systems. Has minor meaning unless library is large.");
20+
private readonly GUIContent UNIX_DLOPEN_FLAGS_GUI_CONTENT = new GUIContent("dlopen flags",
21+
"Flags used in dlopen() P/Invoke on Linux and OSX systems. Has minor meaning unless library is large.");
2222
private readonly GUIContent THREAD_SAFE_GUI_CONTENT = new GUIContent("Thread safe",
2323
"Ensures synchronization required for native calls from any other than Unity main thread. Overhead might be few times higher, with uncontended locks.\n\n" +
2424
"Only in Preloaded mode.");
@@ -165,8 +165,8 @@ private void DrawOptions(DllManipulatorOptions options)
165165
}
166166
options.loadingMode = (DllLoadingMode)EditorGUILayout.EnumPopup(DLL_LOADING_MODE_GUI_CONTENT, options.loadingMode);
167167

168-
#if UNITY_STANDALONE_LINUX
169-
options.linuxDlopenFlags = (LinuxDlopenFlags)EditorGUILayout.EnumPopup(LINUX_DLOPEN_FLAGS_GUI_CONTENT, options.linuxDlopenFlags);
168+
#if UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX
169+
options.unixDlopenFlags = (UnixDlopenFlags)EditorGUILayout.EnumPopup(UNIX_DLOPEN_FLAGS_GUI_CONTENT, options.unixDlopenFlags);
170170
#endif
171171

172172
guiEnabledStack.Push(GUI.enabled);

src/PInvokes.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace DllManipulator.Internal
55
{
66
internal static class PInvokes
77
{
8-
#if UNITY_STANDALONE_WIN //UNITY_EDITOR_WIN
8+
#if UNITY_STANDALONE_WIN
99
[DisableMocking]
1010
[DllImport("kernel32", EntryPoint = "LoadLibrary")]
1111
public static extern IntPtr Windows_LoadLibrary(string lpFileName);
@@ -30,6 +30,18 @@ internal static class PInvokes
3030
[DisableMocking]
3131
[DllImport("libdl.so", EntryPoint = "dlclose")]
3232
public static extern int Linux_dlclose(IntPtr handle);
33+
#elif UNITY_STANDALONE_OSX
34+
[DisableMocking]
35+
[DllImport("libdl.dylib", EntryPoint = "dlopen")]
36+
public static extern IntPtr Osx_dlopen(string filename, int flags);
37+
38+
[DisableMocking]
39+
[DllImport("libdl.dylib", EntryPoint = "dlsym")]
40+
public static extern IntPtr Osx_dlsym(IntPtr handle, string symbol);
41+
42+
[DisableMocking]
43+
[DllImport("libdl.dylib", EntryPoint = "dlclose")]
44+
public static extern int Osx_dlclose(IntPtr handle);
3345
#endif
3446
}
3547
}

0 commit comments

Comments
 (0)