Skip to content
Permalink
Browse files

[Native] PreloadLibrary: Use type rather than assembly (to avoid use …

…of GetTypeInfo() everywhere)
  • Loading branch information
xen2 committed Dec 8, 2018
1 parent f4a5489 commit 23bd86129b3730ded7d7d4d62910d0c7da12e4c9
@@ -27,7 +27,7 @@ public static class NativeInvoke

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

/// <summary>
@@ -28,9 +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>
/// <param name="owner">Type whose assembly location is related to the native library (we can't use GetCallingAssembly as it might be wrong due to optimizations).</param>
/// <exception cref="System.InvalidOperationException">Library could not be loaded.</exception>
public static void PreloadLibrary(string libraryName, Assembly owner)
public static void PreloadLibrary(string libraryName, Type owner)
{
#if XENKO_PLATFORM_WINDOWS_DESKTOP
lock (LoadedLibraries)
@@ -54,7 +54,7 @@ public static void PreloadLibrary(string libraryName, Assembly owner)
// 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);
if (dllFolder == null)
dllFolder = Path.Combine(Path.GetDirectoryName(owner.Location), cpu);
dllFolder = Path.Combine(Path.GetDirectoryName(owner.GetTypeInfo().Assembly.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, typeof(NativeLz4Base).Assembly);
NativeLibrary.PreloadLibrary(NativeInvoke.LibraryName, typeof(NativeLz4Base));
}

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

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

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

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", typeof(NativeInvoke).Assembly);
NativeLibrary.PreloadLibrary(Library + ".dll", typeof(NativeInvoke));
#else
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke).Assembly);
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke));
#endif
}

@@ -121,7 +121,7 @@ static BowyerWatsonTetrahedralization()
{
// TODO: Add native to Xenko.Engine?
#if XENKO_PLATFORM_WINDOWS
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll", typeof(BowyerWatsonTetrahedralization).Assembly);
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll", typeof(BowyerWatsonTetrahedralization));
#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", typeof(GameContextOpenTK).Assembly);
Core.NativeLibrary.PreloadLibrary("SDL2.dll", typeof(GameContextOpenTK));
#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", typeof(GameContextSDL).Assembly);
Core.NativeLibrary.PreloadLibrary("SDL2.dll", typeof(GameContextSDL));
}

/// <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", typeof(FontManager).Assembly);
Core.NativeLibrary.PreloadLibrary("freetype.dll", typeof(FontManager));

// 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", typeof(NativeInvoke).Assembly);
NativeLibrary.PreloadLibrary(Library + ".dll", typeof(NativeInvoke));
#else
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke).Assembly);
NativeLibrary.PreloadLibrary(Library + ".so", typeof(NativeInvoke));
#endif
}

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

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

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", typeof(PhysicsComponent).Assembly);
NativeLibrary.PreloadLibrary("libbulletc.dll", typeof(PhysicsComponent));
}

protected PhysicsComponent()
@@ -46,7 +46,7 @@ public class EffectCompiler : EffectCompilerBase
public EffectCompiler(IVirtualFileProvider fileProvider)
{
FileProvider = fileProvider;
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll", typeof(EffectCompiler).Assembly);
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll", typeof(EffectCompiler));
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", typeof(EffectCompiler).Assembly);
NativeLibrary.PreloadLibrary("d3dcompiler_47.dll", typeof(EffectCompiler));
d3dCompilerLoaded = true;
}

@@ -77,14 +77,14 @@ public static void PreloadLibraries()
// |---- avcodec
// |---- avutil
#if XENKO_PLATFORM_WINDOWS
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);
var type = typeof(FFmpegUtils);
Core.NativeLibrary.PreloadLibrary("avutil-55", type);
Core.NativeLibrary.PreloadLibrary("swresample-2", type);
Core.NativeLibrary.PreloadLibrary("avcodec-57", type);
Core.NativeLibrary.PreloadLibrary("avformat-57", type);
Core.NativeLibrary.PreloadLibrary("swscale-4", type);
Core.NativeLibrary.PreloadLibrary("avfilter-6", type);
Core.NativeLibrary.PreloadLibrary("avdevice-57", type);
#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", typeof(Fove).Assembly);
NativeLibrary.PreloadLibrary(NativeInvoke.Library + ".dll", typeof(Fove));
#endif
}

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

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

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

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

public static bool InitDone = false;
@@ -36,13 +36,13 @@ public class TextureTool : IDisposable

static TextureTool()
{
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);
var type = typeof(TextureTool);
NativeLibrary.PreloadLibrary("AtitcWrapper.dll", type);
NativeLibrary.PreloadLibrary("DxtWrapper.dll", type);
NativeLibrary.PreloadLibrary("PVRTexLib.dll", type);
NativeLibrary.PreloadLibrary("PvrttWrapper.dll", type);
NativeLibrary.PreloadLibrary("FreeImage.dll", type);
NativeLibrary.PreloadLibrary("FreeImageNET.dll", type);
}

/// <summary>

0 comments on commit 23bd861

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