Skip to content

Commit a33bf48

Browse files
clean up the delay loading code, leave a TODO for Arek
1 parent 22a53a4 commit a33bf48

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

include/nbl/system/CSystemWin32.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#ifndef _NBL_SYSTEM_C_SYSTEM_WIN32_H_INCLUDED_
22
#define _NBL_SYSTEM_C_SYSTEM_WIN32_H_INCLUDED_
33

4-
#include "ISystem.h"
4+
5+
#include "nbl/system/ISystem.h"
6+
7+
8+
#ifdef _NBL_PLATFORM_WINDOWS_
9+
#include <delayimp.h>
510

611
namespace nbl::system
712
{
813

9-
#ifdef _NBL_PLATFORM_WINDOWS_
1014
class NBL_API2 CSystemWin32 : public ISystem
1115
{
1216
protected:
@@ -22,9 +26,30 @@ class NBL_API2 CSystemWin32 : public ISystem
2226
inline CSystemWin32() : ISystem(core::make_smart_refctd_ptr<CCaller>(this)) {}
2327

2428
SystemInfo getSystemInfo() const override;
29+
30+
template<typename PathContainer=core::vector<system::path>>
31+
static inline HRESULT delayLoadDLL(const char* dllName, const PathContainer& paths)
32+
{
33+
// load from right next to the executable (always be able to override like this)
34+
HMODULE res = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
35+
// now lets try our custom dirs
36+
for (system::path dir : paths)
37+
{
38+
const auto pathStr = (dir.make_preferred()/dllName).string();
39+
if (res = LoadLibraryExA(pathStr.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH))
40+
break;
41+
}
42+
// if still can't find, try looking for a system wide install
43+
if (!res)
44+
res = LoadLibraryExA(dllName, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
45+
if (!res)
46+
return E_FAIL;
47+
return __HrLoadAllImportsForDll(dllName);
48+
}
2549
};
26-
#endif
2750

2851
}
52+
#endif
53+
2954

3055
#endif

include/nbl/system/IApplicationFramework.h

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,41 @@
11
#ifndef _NBL_SYSTEM_I_APPLICATION_FRAMEWORK_H_INCLUDED_
22
#define _NBL_SYSTEM_I_APPLICATION_FRAMEWORK_H_INCLUDED_
33

4+
45
#include "nbl/core/declarations.h"
56

67
#include "nbl/system/declarations.h"
78
#include "nbl/system/definitions.h"
89

9-
#ifdef _NBL_PLATFORM_WINDOWS_
10-
#include <delayimp.h>
11-
#endif // _NBL_PLATFORM_WINDOWS_
1210

1311
namespace nbl::system
1412
{
1513

1614
class NBL_API IApplicationFramework
1715
{
1816
public:
19-
virtual void setSystem(core::smart_refctd_ptr<nbl::system::ISystem>&& system) = 0;
2017
IApplicationFramework(
2118
const system::path& _localInputCWD,
2219
const system::path& _localOutputCWD,
2320
const system::path& _sharedInputCWD,
2421
const system::path& _sharedOutputCWD) :
2522
localInputCWD(_localInputCWD), localOutputCWD(_localOutputCWD), sharedInputCWD(_sharedInputCWD), sharedOutputCWD(_sharedOutputCWD)
2623
{
27-
#if defined(_NBL_PLATFORM_WINDOWS_) && defined(_NBL_SHARED_BUILD_)
28-
{
29-
HMODULE res = LoadLibraryExA(_NABLA_DLL_NAME_, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
30-
if (!res)
31-
{
32-
const auto nablaBuiltDLL = (system::path(_NABLA_OUTPUT_DIR_).make_preferred() / _NABLA_DLL_NAME_).string();
33-
res = LoadLibraryExA(nablaBuiltDLL.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
34-
}
35-
if (!res)
36-
{
37-
const auto nablaInstalledDLL = (system::path(_NABLA_INSTALL_DIR_).make_preferred() / _NABLA_DLL_NAME_).string();
38-
res = LoadLibraryExA(nablaInstalledDLL.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
39-
}
40-
if (!res)
41-
res = LoadLibraryExA(_NABLA_DLL_NAME_, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
42-
HRESULT hr = __HrLoadAllImportsForDll(_NABLA_DLL_NAME_);
43-
assert(res && SUCCEEDED(hr));
44-
}
45-
#endif // _NBL_PLATFORM_WINDOWS_ && _NBL_SHARED_BUILD_
46-
47-
{
48-
constexpr std::string_view DXCOMPILER_DLL_NAME = "dxcompiler.dll";
49-
50-
HMODULE res = LoadLibraryExA(DXCOMPILER_DLL_NAME.data(), NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
51-
if (!res)
52-
{
53-
const auto dxcBuiltDLL = (system::path(_DXC_DLL_).make_preferred()).string();
54-
res = LoadLibraryExA(dxcBuiltDLL.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
55-
}
56-
if (!res)
57-
res = LoadLibraryExA(DXCOMPILER_DLL_NAME.data(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
58-
HRESULT hr = __HrLoadAllImportsForDll(DXCOMPILER_DLL_NAME.data());
59-
assert(res && SUCCEEDED(hr));
60-
}
24+
#ifdef _NBL_PLATFORM_WINDOWS_
25+
// TODO: @AnastaZIuk also provide define constants for DXC install dir!
26+
const HRESULT dxcLoad = CSystemWin32::delayLoadDLL("dxcompiler.dll",{_DXC_DLL_});
27+
assert(SUCCEEDED(dxcLoad));
28+
#ifdef _NBL_SHARED_BUILD_
29+
// if there was no DLL next to the executable, then try from the Nabla build directory
30+
// else if nothing in the build dir, then try looking for Nabla in the CURRENT BUILD'S INSTALL DIR
31+
const HRESULT nablaLoad = CSystemWin32::delayLoadDLL(_NABLA_DLL_NAME_,{_NABLA_OUTPUT_DIR_,_NABLA_INSTALL_DIR_});
32+
assert(SUCCEEDED(nablaLoad));
33+
#endif
34+
#endif
6135
}
6236

37+
virtual void setSystem(core::smart_refctd_ptr<nbl::system::ISystem>&& system) = 0;
38+
6339
void onAppInitialized()
6440
{
6541
return onAppInitialized_impl();

0 commit comments

Comments
 (0)