Esempio n. 1
0
        private static T LoadInteropType <T>(IntPtr library, IPlatformLoader loader)
            where T : new()
        {
            var result = new T();

            LoadDelegates(result, library, loader);
            return(result);
        }
Esempio n. 2
0
 public static T CurrentPlatform <T>(this IPlatformLoader <T> platform)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         return(platform.Windows());
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     {
         return(platform.macOS());
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         return(platform.Linux());
     }
     else
     {
         throw new NotSupportedException("Unknown platform.");
     }
 }
Esempio n. 3
0
        static Interop()
        {
            IPathResolver   pathResolver = null;
            IPlatformLoader loader       = null;

            if (Host.GetExportedSymbol != null)
            {
                // We are loading exported functions from the currently running executable.
                pathResolver = new Host.Loader();
                loader       = new Host.Loader();
            }
            else
            {
                pathResolver = new DynamicLinkLibraryPathResolver();
                loader       = PlatformLoaderBase.SelectPlatformLoader();
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // This custom path resolver attempts to do a DllImport to get the path that .NET decides.
                    // It may load a special dll from a NuGet package.
                    pathResolver = new WindowsDllImportLibraryPathResolver(pathResolver);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    pathResolver = new MacDllImportLibraryPathResolver(pathResolver);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    pathResolver = new LinuxDllImportLibraryPathResolver(pathResolver);
                }
            }

            var result = pathResolver.Resolve("QmlNet");

            if (!result.IsSuccess)
            {
                throw new Exception("Unable to find the native Qml.Net library." +
                                    " Try calling \"RuntimeManager.DiscoverOrDownloadSuitableQtRuntime();\" in Program.Main()");;
            }

            var library = loader.LoadLibrary(result.Path);

            if (library == IntPtr.Zero)
            {
                throw new Exception("Unable to load native Qml.Net library." +
                                    " Try calling \"RuntimeManager.DiscoverOrDownloadSuitableQtRuntime();\" in Program.Main()");;
            }

            Callbacks             = LoadInteropType <CallbacksInterop>(library, loader);
            NetTypeInfo           = LoadInteropType <NetTypeInfoInterop>(library, loader);
            NetJsValue            = LoadInteropType <NetJsValueInterop>(library, loader);
            NetMethodInfo         = LoadInteropType <NetMethodInfoInterop>(library, loader);
            NetPropertyInfo       = LoadInteropType <NetPropertyInfoInterop>(library, loader);
            NetTypeManager        = LoadInteropType <NetTypeManagerInterop>(library, loader);
            QCoreApplication      = LoadInteropType <QCoreApplicationInterop>(library, loader);
            QQmlApplicationEngine = LoadInteropType <QQmlApplicationEngineInterop>(library, loader);
            NetVariant            = LoadInteropType <NetVariantInterop>(library, loader);
            NetReference          = LoadInteropType <NetReferenceInterop>(library, loader);
            NetVariantList        = LoadInteropType <NetVariantListInterop>(library, loader);
            NetTestHelper         = LoadInteropType <NetTestHelperInterop>(library, loader);
            NetSignalInfo         = LoadInteropType <NetSignalInfoInterop>(library, loader);
            QResource             = LoadInteropType <QResourceInterop>(library, loader);
            NetDelegate           = LoadInteropType <NetDelegateInterop>(library, loader);
            QQuickStyle           = LoadInteropType <QQuickStyleInterop>(library, loader);
            QtInterop             = LoadInteropType <QtInterop>(library, loader);
            Utilities             = LoadInteropType <UtilitiesInterop>(library, loader);
            QtWebEngine           = LoadInteropType <QtWebEngineInterop>(library, loader);
            QTest      = LoadInteropType <QTestInterop>(library, loader);
            NetQObject = LoadInteropType <NetQObjectInterop>(library, loader);
            NetQObjectSignalConnection = LoadInteropType <NetQObjectSignalConnectionInterop>(library, loader);

            // RuntimeManager.ConfigureRuntimeDirectory may set these environment variables.
            // However, they only really work when called with Qt.PutEnv.
            Qt.PutEnv("QT_PLUGIN_PATH", Environment.GetEnvironmentVariable("QT_PLUGIN_PATH"));
            Qt.PutEnv("QML2_IMPORT_PATH", Environment.GetEnvironmentVariable("QML2_IMPORT_PATH"));

            var cb = DefaultCallbacks.Callbacks();

            Callbacks.RegisterCallbacks(ref cb);
        }
Esempio n. 4
0
        static Interop()
        {
            string pluginsDirectory = null;
            string qmlDirectory     = null;
            string libDirectory     = null;

            IPathResolver   pathResolver = null;
            IPlatformLoader loader       = null;

            if (Host.GetExportedSymbol != null)
            {
                // We are loading exported functions from the currently running executable.
                pathResolver = new Host.Loader();
                loader       = new Host.Loader();
            }
            else
            {
                pathResolver = new DynamicLinkLibraryPathResolver();
                loader       = PlatformLoaderBase.SelectPlatformLoader();
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // This custom path resolver attempts to do a DllImport to get the path that .NET decides.
                    // It may load a special dll from a NuGet package.
                    pathResolver = new WindowsDllImportLibraryPathResolver(pathResolver);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    pathResolver = new MacDllImportLibraryPathResolver(pathResolver);
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    pathResolver = new LinuxDllImportLibraryPathResolver(pathResolver);
                }

                var resolveResult = pathResolver.Resolve("QmlNet");

                if (resolveResult.IsSuccess)
                {
                    libDirectory = Path.GetDirectoryName(resolveResult.Path);
                    if (!string.IsNullOrEmpty(libDirectory))
                    {
                        // If this library has a plugins/qml directory below it, set it.
                        var potentialPlugisDirectory = Path.Combine(libDirectory, "plugins");
                        if (Directory.Exists(potentialPlugisDirectory))
                        {
                            pluginsDirectory = potentialPlugisDirectory;
                        }

                        var potentialQmlDirectory = Path.Combine(libDirectory, "qml");
                        if (Directory.Exists(potentialQmlDirectory))
                        {
                            qmlDirectory = potentialQmlDirectory;
                        }
                    }
                }

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (!string.IsNullOrEmpty(libDirectory) && Directory.Exists(libDirectory))
                    {
                        // Even though we opened up the native dll correctly, we need to add
                        // the folder to the path. The reason is because QML plugins aren't
                        // in the same directory and have trouble finding dependencies
                        // that are within our lib folder.
                        Environment.SetEnvironmentVariable(
                            "PATH",
                            Environment.GetEnvironmentVariable("PATH") + $";{libDirectory}");
                    }
                }
            }

            var result = pathResolver.Resolve("QmlNet");

            if (!result.IsSuccess)
            {
                throw new Exception("Couldn't find the native Qml.Net library.");
            }

            var library = loader.LoadLibrary(result.Path);

            Callbacks             = LoadInteropType <CallbacksInterop>(library, loader);
            NetTypeInfo           = LoadInteropType <NetTypeInfoInterop>(library, loader);
            NetJsValue            = LoadInteropType <NetJsValueInterop>(library, loader);
            NetMethodInfo         = LoadInteropType <NetMethodInfoInterop>(library, loader);
            NetPropertyInfo       = LoadInteropType <NetPropertyInfoInterop>(library, loader);
            NetTypeManager        = LoadInteropType <NetTypeManagerInterop>(library, loader);
            QGuiApplication       = LoadInteropType <QGuiApplicationInterop>(library, loader);
            QQmlApplicationEngine = LoadInteropType <QQmlApplicationEngineInterop>(library, loader);
            NetVariant            = LoadInteropType <NetVariantInterop>(library, loader);
            NetReference          = LoadInteropType <NetReferenceInterop>(library, loader);
            NetVariantList        = LoadInteropType <NetVariantListInterop>(library, loader);
            NetTestHelper         = LoadInteropType <NetTestHelperInterop>(library, loader);
            NetSignalInfo         = LoadInteropType <NetSignalInfoInterop>(library, loader);
            QResource             = LoadInteropType <QResourceInterop>(library, loader);
            NetDelegate           = LoadInteropType <NetDelegateInterop>(library, loader);
            QQuickStyle           = LoadInteropType <QQuickStyleInterop>(library, loader);
            QtInterop             = LoadInteropType <QtInterop>(library, loader);
            Utilities             = LoadInteropType <UtilitiesInterop>(library, loader);
            QtWebEngine           = LoadInteropType <QtWebEngineInterop>(library, loader);

            if (!string.IsNullOrEmpty(pluginsDirectory))
            {
                Qt.PutEnv("QT_PLUGIN_PATH", pluginsDirectory);
            }
            if (!string.IsNullOrEmpty(qmlDirectory))
            {
                Qt.PutEnv("QML2_IMPORT_PATH", qmlDirectory);
            }

            var cb = DefaultCallbacks.Callbacks();

            Callbacks.RegisterCallbacks(ref cb);
        }
Esempio n. 5
0
 /// <summary>
 /// Initializes static members of the <see cref="NativeLibraryBase"/> class.
 /// </summary>
 static NativeLibraryBase()
 {
     PlatformLoader = PlatformLoaderBase.SelectPlatformLoader();
 }