Skip to content
Permalink
Browse files

[Native] Explicitly require related Assembly when loading native libr…

…ary (we can't use GetCallingAssembly as it might be wrong due to optimizations)
  • Loading branch information
xen2 committed Dec 8, 2018
1 parent 4bd757d commit f4a5489254dcdf690b3890ad4fcfc256db31565d
@@ -27,7 +27,7 @@ public static class NativeInvoke

static NativeInvoke()
{
NativeLibrary.PreloadLibrary(LibraryName);
NativeLibrary.PreloadLibrary(LibraryName, typeof(NativeInvoke).Assembly);
}

/// <summary>
@@ -28,8 +28,9 @@ public static class NativeLibrary
/// Only available on Windows for now.
/// </summary>
/// <param name="libraryName">Name of the library.</param>
/// <param name="owner">Assembly whose location is related to the native library.</param>
/// <exception cref="System.InvalidOperationException">Library could not be loaded.</exception>
public static void PreloadLibrary(string libraryName)
public static void PreloadLibrary(string libraryName, Assembly owner)
{
#if XENKO_PLATFORM_WINDOWS_DESKTOP
lock (LoadedLibraries)
@@ -52,9 +53,8 @@ public static void PreloadLibrary(string libraryName)

// 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);
var callingAssembly = Assembly.GetCallingAssembly();
if (dllFolder == null)
dllFolder = Path.Combine(Path.GetDirectoryName(callingAssembly.Location), cpu);
dllFolder = Path.Combine(Path.GetDirectoryName(owner.Location), cpu);
if (!Directory.Exists(dllFolder))
dllFolder = Path.Combine(Environment.CurrentDirectory, cpu);
var libraryFilename = Path.Combine(dllFolder, libraryName);
@@ -8,7 +8,7 @@ public abstract class NativeLz4Base
{
static NativeLz4Base()
{
NativeLibrary.PreloadLibrary(NativeInvoke.LibraryName);
NativeLibrary.PreloadLibrary(NativeInvoke.LibraryName, typeof(NativeLz4Base).Assembly);
}

[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
@@ -15,7 +15,7 @@ public class AssimpAssetImporter : ModelAssetImporter
{
static AssimpAssetImporter()
{
NativeLibrary.PreloadLibrary("assimp-vc120-mt.dll");
NativeLibrary.PreloadLibrary("assimp-vc120-mt.dll", typeof(AssimpAssetImporter).Assembly);
}

// Supported file extensions for this importer
@@ -14,7 +14,7 @@ public class FbxAssetImporter : ModelAssetImporter
{
static FbxAssetImporter()
{
NativeLibrary.PreloadLibrary("libfbxsdk.dll");
NativeLibrary.PreloadLibrary("libfbxsdk.dll", typeof(FbxAssetImporter).Assembly);
}

// Supported file extensions for this importer
@@ -27,7 +27,7 @@ internal class ColliderShapeAssetCompiler : AssetCompilerBase
{
static ColliderShapeAssetCompiler()
{
NativeLibrary.PreloadLibrary("VHACD.dll");
NativeLibrary.PreloadLibrary("VHACD.dll", typeof(ColliderShapeAssetCompiler).Assembly);
}

public override IEnumerable<BuildDependencyInfo> GetInputTypes(AssetItem assetItem)
@@ -16,9 +16,9 @@ internal static class NativeInvoke
internal static void PreLoad()
{
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(Library + ".dll");
NativeLibrary.PreloadLibrary(Library + ".dll", typeof(NativeInvoke).Assembly);
#else
NativeLibrary.PreloadLibrary(Library + ".so");
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke).Assembly);
#endif
}

@@ -1,4 +1,4 @@
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System;
@@ -121,7 +121,7 @@ static BowyerWatsonTetrahedralization()
{
// TODO: Add native to Xenko.Engine?
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll");
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll", typeof(BowyerWatsonTetrahedralization).Assembly);
#endif
exactinit();
}
@@ -109,7 +109,7 @@ private static OpenTK.GameWindow TryGameWindow(int requestedWidth, int requested
#if XENKO_GRAPHICS_API_OPENGL || XENKO_GRAPHICS_API_OPENGLES
// Preload proper SDL native library (depending on CPU type)
// This is for OpenGL ES on desktop
Core.NativeLibrary.PreloadLibrary("SDL2.dll");
Core.NativeLibrary.PreloadLibrary("SDL2.dll", typeof(GameContextOpenTK).Assembly);
#endif

var gameWindow = new OpenTK.GameWindow(requestedWidth, requestedHeight, graphicMode, GameContext.ProductName, GameWindowFlags.Default, DisplayDevice.Default, versionMajor, versionMinor,
@@ -13,7 +13,7 @@ public class GameContextSDL : GameContextWindows<Window>
static GameContextSDL()
{
// Preload proper SDL native library (depending on CPU type)
Core.NativeLibrary.PreloadLibrary("SDL2.dll");
Core.NativeLibrary.PreloadLibrary("SDL2.dll", typeof(GameContextSDL).Assembly);
}

/// <inheritDoc/>
@@ -81,7 +81,7 @@ public FontManager(IDatabaseFileProviderService fileProviderService)
contentManager = new ContentManager(fileProviderService);

// Preload proper freetype native library (depending on CPU type)
Core.NativeLibrary.PreloadLibrary("freetype.dll");
Core.NativeLibrary.PreloadLibrary("freetype.dll", typeof(FontManager).Assembly);

// create a freetype library used to generate the bitmaps
freetypeLibrary = new Library();
@@ -22,9 +22,9 @@ internal static class NativeInvoke
internal static void PreLoad()
{
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(Library + ".dll");
NativeLibrary.PreloadLibrary(Library + ".dll", typeof(NativeInvoke).Assembly);
#else
NativeLibrary.PreloadLibrary(Library + ".so");
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke).Assembly);
#endif
}

@@ -16,9 +16,9 @@ internal static class NativeInvoke
internal static void PreLoad()
{
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(Library + ".dll");
NativeLibrary.PreloadLibrary(Library + ".dll", typeof(NativeInvoke).Assembly);
#else
NativeLibrary.PreloadLibrary(Library + ".so");
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke).Assembly);
#endif
}

@@ -22,7 +22,7 @@ private class PhysicsScene
static Bullet2PhysicsSystem()
{
// Preload proper libbulletc native library (depending on CPU type)
NativeLibrary.PreloadLibrary("libbulletc.dll");
NativeLibrary.PreloadLibrary("libbulletc.dll", typeof(Bullet2PhysicsSystem).Assembly);
}

public Bullet2PhysicsSystem(IServiceRegistry registry)
@@ -27,7 +27,7 @@ public abstract class PhysicsComponent : ActivableEntityComponent
static PhysicsComponent()
{
// Preload proper libbulletc native library (depending on CPU type)
NativeLibrary.PreloadLibrary("libbulletc.dll");
NativeLibrary.PreloadLibrary("libbulletc.dll", typeof(PhysicsComponent).Assembly);
}

protected PhysicsComponent()
@@ -46,7 +46,7 @@ public class EffectCompiler : EffectCompilerBase
public EffectCompiler(IVirtualFileProvider fileProvider)
{
FileProvider = fileProvider;
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll");
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll", typeof(EffectCompiler).Assembly);
SourceDirectories = new List<string>();
UrlToFilePath = new Dictionary<string, string>();
}
@@ -89,7 +89,7 @@ public override TaskOrResult<EffectBytecodeCompilerResult> Compile(ShaderMixinSo
// Note: No lock, it's probably fine if it gets called from multiple threads at the same time.
if (Platform.IsWindowsDesktop && !d3dCompilerLoaded)
{
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll");
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll", typeof(EffectCompiler).Assembly);
d3dCompilerLoaded = true;
}

@@ -77,13 +77,14 @@ public static void PreloadLibraries()
// |---- avcodec
// |---- avutil
#if XENKO_PLATFORM_WINDOWS
Core.NativeLibrary.PreloadLibrary("avutil-55");
Core.NativeLibrary.PreloadLibrary("swresample-2");
Core.NativeLibrary.PreloadLibrary("avcodec-57");
Core.NativeLibrary.PreloadLibrary("avformat-57");
Core.NativeLibrary.PreloadLibrary("swscale-4");
Core.NativeLibrary.PreloadLibrary("avfilter-6");
Core.NativeLibrary.PreloadLibrary("avdevice-57");
var assembly = typeof(FFmpegUtils).Assembly;
Core.NativeLibrary.PreloadLibrary("avutil-55", assembly);
Core.NativeLibrary.PreloadLibrary("swresample-2", assembly);
Core.NativeLibrary.PreloadLibrary("avcodec-57", assembly);
Core.NativeLibrary.PreloadLibrary("avformat-57", assembly);
Core.NativeLibrary.PreloadLibrary("swscale-4", assembly);
Core.NativeLibrary.PreloadLibrary("avfilter-6", assembly);
Core.NativeLibrary.PreloadLibrary("avdevice-57", assembly);
#else
uint version;
version = ffmpeg.avutil_version();
@@ -14,7 +14,7 @@ public static class Fove
static Fove()
{
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll");
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll", typeof(Fove).Assembly);
#endif
}

@@ -16,9 +16,9 @@ internal static class NativeInvoke
internal static void PreLoad()
{
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(Library + ".dll");
NativeLibrary.PreloadLibrary(Library + ".dll", typeof(NativeInvoke).Assembly);
#else
NativeLibrary.PreloadLibrary(Library + ".so");
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke).Assembly);
#endif
}

@@ -15,7 +15,7 @@ internal static class OculusOvr
static OculusOvr()
{
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll");
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll", typeof(OculusOvr).Assembly);
#endif
}

@@ -155,7 +155,7 @@ public void Update()

static OpenVR()
{
NativeLibrary.PreloadLibrary("openvr_api.dll");
NativeLibrary.PreloadLibrary("openvr_api.dll", typeof(OpenVR).Assembly);
}

public static bool InitDone = false;
@@ -21,12 +21,7 @@ static Module()

public static void LoadLibraries()
{
NativeLibrary.PreloadLibrary("AtitcWrapper.dll");
NativeLibrary.PreloadLibrary("DxtWrapper.dll");
NativeLibrary.PreloadLibrary("PVRTexLib.dll");
NativeLibrary.PreloadLibrary("PvrttWrapper.dll");
NativeLibrary.PreloadLibrary("FreeImage.dll");
NativeLibrary.PreloadLibrary("FreeImageNET.dll");
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(TextureTool).TypeHandle);
}
}
}
@@ -36,12 +36,13 @@ public class TextureTool : IDisposable

static TextureTool()
{
NativeLibrary.PreloadLibrary("AtitcWrapper.dll");
NativeLibrary.PreloadLibrary("DxtWrapper.dll");
NativeLibrary.PreloadLibrary("PVRTexLib.dll");
NativeLibrary.PreloadLibrary("PvrttWrapper.dll");
NativeLibrary.PreloadLibrary("FreeImage.dll");
NativeLibrary.PreloadLibrary("FreeImageNET.dll");
var assembly = typeof(TextureTool).Assembly;
NativeLibrary.PreloadLibrary("AtitcWrapper.dll", assembly);
NativeLibrary.PreloadLibrary("DxtWrapper.dll", assembly);
NativeLibrary.PreloadLibrary("PVRTexLib.dll", assembly);
NativeLibrary.PreloadLibrary("PvrttWrapper.dll", assembly);
NativeLibrary.PreloadLibrary("FreeImage.dll", assembly);
NativeLibrary.PreloadLibrary("FreeImageNET.dll", assembly);
}

/// <summary>

0 comments on commit f4a5489

Please sign in to comment.
You can’t perform that action at this time.