|
7 | 7 |
|
8 | 8 | namespace sl |
9 | 9 | { |
| 10 | + public static class NativeWrapper |
| 11 | + { |
| 12 | + [DllImport("kernel32.dll")] |
| 13 | + private static extern IntPtr LoadLibrary(string dllToLoad); |
| 14 | + |
| 15 | + [DllImport("kernel32.dll")] |
| 16 | + private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); |
| 17 | + |
| 18 | + [DllImport("kernel32.dll")] |
| 19 | + private static extern bool FreeLibrary(IntPtr hModule); |
| 20 | + |
| 21 | + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
| 22 | + private delegate int CheckZEDPluginDelegate(int Major, int Minor); |
| 23 | + |
| 24 | + private static CheckZEDPluginDelegate _checkZEDPlugin; |
| 25 | + |
| 26 | + public static bool IsWrapperLoaded => _checkZEDPlugin != null; |
| 27 | + |
| 28 | + public static bool Init() |
| 29 | + { |
| 30 | + string DllPath = "Assets/SDK/Plugins/win64/" + sl.ZEDCommon.NameDLL; |
| 31 | + IntPtr pDll = LoadLibrary(DllPath); |
| 32 | + if (pDll == IntPtr.Zero) |
| 33 | + { |
| 34 | + Console.WriteLine("Failed to load DLL."); |
| 35 | + return false; |
| 36 | + } |
| 37 | + IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "sl_check_plugin"); |
| 38 | + if (pAddressOfFunctionToCall != IntPtr.Zero) |
| 39 | + { |
| 40 | + _checkZEDPlugin = Marshal.GetDelegateForFunctionPointer<CheckZEDPluginDelegate>(pAddressOfFunctionToCall); |
| 41 | + return true; |
| 42 | + } |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + public static int CheckPlugin() |
| 47 | + { |
| 48 | + if (_checkZEDPlugin == null) |
| 49 | + throw new InvalidOperationException("Function not loaded."); |
| 50 | + return _checkZEDPlugin(ZEDCamera.PluginVersion.Major, ZEDCamera.PluginVersion.Minor); |
| 51 | + } |
| 52 | + } |
| 53 | + |
10 | 54 | /// <summary> |
11 | 55 | /// Main interface between Unity and the ZED SDK. Primarily consists of extern calls to the ZED SDK wrapper .dll and |
12 | 56 | /// low-level logic to process data sent to/received from it. |
@@ -773,8 +817,9 @@ private static extern int dllz_get_objects_batch_data(int cameraID, int batch_in |
773 | 817 | /* |
774 | 818 | * Specific plugin functions |
775 | 819 | */ |
776 | | - [DllImport(nameDll, EntryPoint = "sl_check_plugin")] |
777 | | - private static extern int dllz_check_plugin(int major, int minor); |
| 820 | + |
| 821 | + /* [DllImport(nameDll, EntryPoint = "sl_check_plugin")] |
| 822 | + private static extern int dllz_check_plugin(int major, int minor);*/ |
778 | 823 |
|
779 | 824 | [DllImport(nameDll, EntryPoint = "sl_get_sdk_version")] |
780 | 825 | private static extern IntPtr dllz_get_sdk_version(); |
@@ -931,25 +976,19 @@ public static string GenerateUniqueID() |
931 | 976 | /// </summary> |
932 | 977 | public static bool CheckPlugin() |
933 | 978 | { |
934 | | - try |
| 979 | + if (NativeWrapper.Init()) |
935 | 980 | { |
936 | | - int res = dllz_check_plugin(PluginVersion.Major, PluginVersion.Minor); |
937 | | - if (res!= 0) |
| 981 | + int res = NativeWrapper.CheckPlugin(); |
| 982 | + if (res == 0) |
938 | 983 | { |
939 | | - //0 = installed SDK is compatible with plugin. 1 otherwise. |
940 | | - Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE)); |
941 | | - return false; |
| 984 | + pluginIsReady = true; |
| 985 | + return true; |
942 | 986 | } |
943 | 987 | } |
944 | | - catch (DllNotFoundException) //In case could not resolve the dll/.so |
945 | | - { |
946 | | - Debug.Log("DllNotFoundException"); |
947 | | - Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE)); |
948 | | - return false; |
949 | | - } |
950 | | - |
951 | | - pluginIsReady = true; |
952 | | - return true; |
| 988 | + |
| 989 | + //0 = installed SDK is compatible with plugin. 1 otherwise. |
| 990 | + Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE)); |
| 991 | + return false; |
953 | 992 | } |
954 | 993 |
|
955 | 994 | /// <summary> |
|
0 commit comments