MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Dll.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14#include "CoreMinimal.h"
15#include "Mcro/Modules.h"
16#include "Interfaces/IPluginManager.h"
17
18#include "Mcro/Concepts.h"
19
20namespace Mcro::Dll
21{
22 using namespace Mcro::Concepts;
23 using namespace Mcro::Modules;
24
25 /** @brief RAII wrapper around PushDllDirectory / PopDllDirectory */
26 struct MCRO_API FScopedSearchPath
27 {
28 FScopedSearchPath(FString const& path);
30
31 private:
32 FString Path;
33 };
34
35 /** @brief RAII wrapper around GetDllHandle / FreeDllHandle */
36 struct MCRO_API FScopedDll
37 {
38 FScopedDll(const TCHAR* fileName);
40
41 private:
42 void* Handle;
43 };
44
45 /** @brief Handle multiple DLL files in one set and an optional base path for them */
46 struct MCRO_API FScopedDllSet
47 {
49
50 /**
51 * @param pushPath The absolute path to be pushed for the basis of finding given DLL's
52 * @param dllFiles The list of DLL file names
53 */
54 template <CConvertibleTo<const TCHAR*>... DllFiles>
55 FScopedDllSet(FString const& pushPath, DllFiles... dllFiles)
56 {
57 FScopedSearchPath pathContext(pushPath);
58 (Dlls.Emplace(dllFiles), ...);
59 }
60
61 /**
62 * @param plugin The plugin this set of DLL's will be relative to
63 * @param pushPath The relative path to the plugin base where to find given DLL's
64 * @param dllFiles The list of DLL file names
65 */
66 template <CConvertibleTo<const TCHAR*>... DllFiles>
67 FScopedDllSet(TSharedPtr<IPlugin> plugin, FString const& pushPath, DllFiles... dllFiles)
68 {
69 ASSERT_CRASH(plugin);
70 FString absPushPath = plugin->GetBaseDir() / pushPath;
71 FScopedSearchPath pathContext(absPushPath);
72 (Dlls.Emplace(dllFiles), ...);
73 }
74
75 private:
76
77 TArray<FScopedDll> Dlls;
78 };
79
80 /**
81 * @brief List DLL's which is used by a specific module and its owning plugin.
82 *
83 * Goes well with `McroBuild.ModuleRuleExtensions.UseRuntimeDependencies` or
84 * `McroBuild.ModuleRuleExtensions.PrepareRuntimeDependencies` like so:
85 * @code
86 * TModuleBoundDlls<FMyAwesomeModule> GMyLibraryDlls {MYLIBRARY_DLL_PATH, MYLIBRARY_DLL_FILES};
87 * @endcode
88 * Where `MyLibrary` is an external module type importing a third-party library with this snippet contained in its
89 * rules:
90 * @code
91 * this.PrepareRuntimeDependencies(this.ModulePath() / "lib" / "x64");
92 * @endcode
93 */
94 template <CObservableModule M>
95 struct TModuleBoundDlls : TModuleBoundObject<M, FScopedDllSet>
96 {
98
99 template <CConvertibleTo<const TCHAR*>... DllFiles>
100 TModuleBoundDlls(const TCHAR* pushPath, DllFiles... dllFiles)
102 {
103 [pushPath, dllFiles...]
104 {
105 auto thisPlugin = IPluginManager::Get().GetModuleOwnerPlugin(*InferModuleName<M>());
106 return new FScopedDllSet(thisPlugin, pushPath, dllFiles...);
107 }
108 })
109 {}
110 };
111}
#define ASSERT_CRASH(condition,...)
Use this instead of check macro if the checked expression shouldn't be ignored in shipping builds....
This header exists because STL headers in Android doesn't define STL concepts (other than same_as whi...
Namespace for utilities handling Unreal modules.
Definition Modules.h:25
FString InferModuleName()
Infer the module name from an input type. This exists because there's a very reliable naming conventi...
Definition Modules.h:42
Handle multiple DLL files in one set and an optional base path for them.
Definition Dll.h:47
FScopedDllSet(TSharedPtr< IPlugin > plugin, FString const &pushPath, DllFiles... dllFiles)
Definition Dll.h:67
FScopedDllSet(FString const &pushPath, DllFiles... dllFiles)
Definition Dll.h:55
RAII wrapper around GetDllHandle / FreeDllHandle.
Definition Dll.h:37
FScopedDll(const TCHAR *fileName)
RAII wrapper around PushDllDirectory / PopDllDirectory.
Definition Dll.h:27
FScopedSearchPath(FString const &path)
List DLL's which is used by a specific module and its owning plugin.
Definition Dll.h:96
TModuleBoundDlls(const TCHAR *pushPath, DllFiles... dllFiles)
Definition Dll.h:100
A wrapper around a given object which lifespan is bound to given module.
Definition Modules.h:231