Ejemplo n.º 1
0
        /// <summary>
        /// Try to preload the library.
        /// This is useful when we want to have AnyCPU .NET and CPU-specific native code.
        /// Only available on Windows for now.
        /// </summary>
        /// <param name="libraryName">Name of the library.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static void PreloadLibrary(string libraryName)
        {
#if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
            lock (LoadedLibraries)
            {
                // If already loaded, just exit as we want to load it just once
                var libraryNameNormalized = libraryName.ToLowerInvariant();
                if (LoadedLibraries.ContainsKey(libraryNameNormalized))
                {
                    return;
                }

                string      cpu;
                SYSTEM_INFO systemInfo;
                GetNativeSystemInfo(out systemInfo);

                if (systemInfo.processorArchitecture == PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_ARM)
                {
                    cpu = "ARM";
                }
                else
                {
                    cpu = IntPtr.Size == 8 ? "x64" : "x86";
                }

                // We are trying to load the dll from a shadow path if it is already registered, otherwise we use it directly from the folder
                var dllFolder       = NativeLibraryInternal.GetShadowPathForNativeDll(libraryName) ?? Path.Combine(Path.GetDirectoryName(typeof(NativeLibrary).GetTypeInfo().Assembly.Location), cpu);
                var libraryFilename = Path.Combine(dllFolder, libraryName);
                var result          = LoadLibrary(libraryFilename);

                if (result == IntPtr.Zero)
                {
                    var envSdk = Environment.GetEnvironmentVariable("SiliconStudioXenkoDir");
                    if (envSdk != null)
                    {
                        //give a further try by using xenko env dir.. this is specially necessary when dealing with nunit tests
                        libraryFilename = Path.Combine(envSdk, "Bin\\Windows\\" + cpu, libraryName);
                        result          = LoadLibrary(libraryFilename);
                    }
                }

                if (result == IntPtr.Zero)
                {
                    throw new InvalidOperationException(string.Format("Could not load native library {0} from path [{1}] using CPU architecture {2}.", libraryName, libraryFilename, cpu));
                }

                LoadedLibraries.Add(libraryName.ToLowerInvariant(), result);
            }
#endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Try to preload the library.
        /// This is useful when we want to have AnyCPU .NET and CPU-specific native code.
        /// Only available on Windows for now.
        /// </summary>
        /// <param name="libraryName">Name of the library.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static void PreloadLibrary(string libraryName)
        {
#if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
            lock (LoadedLibraries)
            {
                // If already loaded, just exit as we want to load it just once
                var libraryNameNormalized = libraryName.ToLowerInvariant();
                if (LoadedLibraries.ContainsKey(libraryNameNormalized))
                {
                    return;
                }

                var systemInfo = new SYSTEM_INFO();
                GetNativeSystemInfo(out systemInfo);

                string cpu;
                if (systemInfo.processorArchitecture == PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_ARM)
                {
                    cpu = "ARM";
                }
                else
                {
                    cpu = IntPtr.Size == 8 ? "x64" : "x86";
                }

                // We are trying to load the dll from a shadow path if it is already registered, otherwise we use it directly from the folder
                var dllFolder       = NativeLibraryInternal.GetShadowPathForNativeDll(libraryName) ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cpu);
                var libraryFilename = Path.Combine(dllFolder, libraryName);
                var result          = LoadLibrary(libraryFilename);

                if (result == IntPtr.Zero)
                {
                    throw new InvalidOperationException(string.Format("Could not load native library {0} from path [{1}] using CPU architecture {2}.", libraryName, libraryFilename, cpu));
                }
                else
                {
                    LoadedLibraries.Add(libraryName.ToLowerInvariant(), result);
                }
            }
#endif
        }