コード例 #1
0
        public static T GetInteropDelegate <T>(IntPtr handler)
        {
            string functionName      = null;
            var    procAddress       = IntPtr.Zero;
            var    supportedPlatform = UMPSettings.SupportedPlatform;

            try
            {
                var attrs = typeof(T).GetCustomAttributes(typeof(InteropFunctionAttribute), false);
                if (attrs.Length == 0)
                {
                    throw new Exception("Could not find the LibVLCAttribute.");
                }

                var attr = (InteropFunctionAttribute)attrs[0];
                functionName = attr.FunctionName;
                if (_interopDelegates.ContainsKey(functionName))
                {
                    return((T)Convert.ChangeType(_interopDelegates[attr.FunctionName], typeof(T), null));
                }

                if (supportedPlatform == UMPSettings.Platforms.Win)
                {
                    procAddress = WindowsInterops.GetProcAddress(handler, attr.FunctionName);
                }
                if (supportedPlatform == UMPSettings.Platforms.Mac)
                {
                    procAddress = MacInterops.dlsym(handler, attr.FunctionName);
                }
                if (supportedPlatform == UMPSettings.Platforms.Linux)
                {
                    procAddress = LinuxInterops.dlsym(handler, attr.FunctionName);
                }

                if (procAddress == IntPtr.Zero)
                {
                    throw new Win32Exception("Can't get process address from " + handler + " library: " + Marshal.GetLastWin32Error());
                }

                var delegateForFunctionPointer = Marshal.GetDelegateForFunctionPointer(procAddress, typeof(T));
                _interopDelegates[attr.FunctionName] = delegateForFunctionPointer;
                return((T)Convert.ChangeType(delegateForFunctionPointer, typeof(T), null));
            }
            catch (Exception e)
            {
                Debug.LogError("GetMethod error: " + functionName);
                throw new MissingMethodException(string.Format("The address of the function '{0}' does not exist in " + handler + " library.", functionName), e);
            }
        }
コード例 #2
0
        public static void Unload(IntPtr handler)
        {
            var supportedPlatform = UMPSettings.SupportedPlatform;

            if (supportedPlatform == UMPSettings.Platforms.Win)
            {
                WindowsInterops.FreeLibrary(handler);
            }
            if (supportedPlatform == UMPSettings.Platforms.Mac)
            {
                MacInterops.dlclose(handler);
            }
            if (supportedPlatform == UMPSettings.Platforms.Linux)
            {
                LinuxInterops.dlclose(handler);
            }
        }
コード例 #3
0
        public static IntPtr Load(string libName, bool useExternalPath, string additionalPath)
        {
            var libHandler     = IntPtr.Zero;
            var libNameWithExt = string.Empty;

            if (string.IsNullOrEmpty(libName))
            {
                return(libHandler);
            }

            var libraryPath = UMPSettings.RuntimePlatformLibraryPath(useExternalPath);

            if (!string.IsNullOrEmpty(additionalPath))
            {
                libraryPath = additionalPath;
            }

            SetEnvironmentVariable(libraryPath);

            var libraryFiles = Directory.GetFiles(libraryPath);

            foreach (var libraryFile in libraryFiles)
            {
                if (libraryFile.EndsWith(".meta"))
                {
                    continue;
                }

                var fileName = Path.GetFileName(libraryFile);

                if (fileName.StartsWith(libName + ".") && (string.IsNullOrEmpty(libNameWithExt) || fileName.Any(char.IsDigit)))
                {
                    libNameWithExt = fileName;
                }
            }

            var supportedPlatform = UMPSettings.SupportedPlatform;

            if (supportedPlatform == UMPSettings.Platforms.Win)
            {
                libHandler = WindowsInterops.LoadLibrary(libraryPath + libNameWithExt);
            }

            if (supportedPlatform == UMPSettings.Platforms.Mac)
            {
                libHandler = MacInterops.dlopen(libraryPath + libNameWithExt, LIN_RTLD_NOW);
            }

            if (supportedPlatform == UMPSettings.Platforms.Linux)
            {
                libHandler = LinuxInterops.dlopen(libraryPath + libNameWithExt, LIN_RTLD_NOW);
            }

            if (libHandler == IntPtr.Zero)
            {
                int error = Marshal.GetLastWin32Error();
                if (supportedPlatform == UMPSettings.Platforms.Win && error == 127)
                {
                    Debug.LogError("Please, reopen your 'Unity Editor' for UMP correct work");
                }
                else
                {
                    Debug.LogError("Can't load " + libName + " library: " + Marshal.GetLastWin32Error());
                }
            }

            return(libHandler);
        }