Change the order of probing for loading native libraries #3033
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR modifies the order in which
NativeLibraryHelperinStride.Coretries different strategies for locating the native libraries to load, so it tries first the Stride custom ones, and defers the default .NET strategy as a fallback.PR Details
Some context. I was running the tests in
Stride.Graphics.Testsand found that all tests that made use of fonts, text rendering, etc., were being aborted (not failing) with the following error:After some investigation, I found that
NativeLibraryHelperwas trying to loadfreetype(a native dependency) using the default loading mechanism of .NET, even though that class implements strategies to find native libraries better suited to the custom nature of what Stride needs. The problem was that the tests were sometimes loading the wrong versión of some of our native dependencies.It seems the .NET default strategy (
NativeLibrary.TryLoad) was trying to look forfreetype.dllin the directory where the executable is, the directory where the P/Invoking module is, the current directory, inC:\Windows\System32, etc., and finally, in the paths in the environmentPATHvariable.So, instead of finding our version of
freetypein/runtimes/win-x64, it was loading a different (and incompatible) version it found inC:\Users\Mario\AppData\Local\Microsoft\WinGet\Packages\oschwartz10612.Poppler_Microsoft.Winget.Source_8wekyb3d8bbwe\poppler-25.07.0\Library\bin\freetype.DLL.And not only with
freetype. Instead of loading our version ofd3d3dcompiler_47.dll, it was loadingC:\WINDOWS\SYSTEM32\d3dcompiler_47.DLL(this one is not a problem because they are identical).So, I've restructured and reordered the logic a bit to first look for libraries using our custom logic, and only if none is found, fallback to the default logic. With this change, tests started running and passing as before this problem arised.
I have also:
NativeLibraryLoaderclass.Related Issue
There are several I've found: #1692 #1750
Possibly also #1700 (the incorrectly loaded library does not manifest as crashes sometimes, but as corrupt data or wrong font metrics, etc.)
Types of changes
Checklist