Plasma Engine  2.0
Loading...
Searching...
No Matches
Plugin_Win.h
1
2#include <Foundation/FoundationInternal.h>
3PL_FOUNDATION_INTERNAL_HEADER
4
5#if PL_ENABLED(PL_PLATFORM_WINDOWS)
6
7# include <Foundation/Basics/Platform/Win/IncludeWindows.h>
8# include <Foundation/Configuration/Plugin.h>
9# include <Foundation/IO/OSFile.h>
10# include <Foundation/Logging/Log.h>
11# include <Foundation/Strings/StringBuilder.h>
12
13using plPluginModule = HMODULE;
14
15bool plPlugin::PlatformNeedsPluginCopy()
16{
17 return true;
18}
19
20void plPlugin::GetPluginPaths(plStringView sPluginName, plStringBuilder& ref_sOriginalFile, plStringBuilder& ref_sCopiedFile, plUInt8 uiFileCopyNumber)
21{
22 ref_sOriginalFile = plOSFile::GetApplicationDirectory();
23 ref_sOriginalFile.AppendPath(sPluginName);
24 ref_sOriginalFile.Append(".dll");
25
26 ref_sCopiedFile = plOSFile::GetApplicationDirectory();
27 ref_sCopiedFile.AppendPath(sPluginName);
28
29 if (!plOSFile::ExistsFile(ref_sOriginalFile))
30 {
31 ref_sOriginalFile = plOSFile::GetCurrentWorkingDirectory();
32 ref_sOriginalFile.AppendPath(sPluginName);
33 ref_sOriginalFile.Append(".dll");
34
35 ref_sCopiedFile = plOSFile::GetCurrentWorkingDirectory();
36 ref_sCopiedFile.AppendPath(sPluginName);
37 }
38
39 if (uiFileCopyNumber > 0)
40 ref_sCopiedFile.AppendFormat("{0}", uiFileCopyNumber);
41
42 ref_sCopiedFile.Append(".loaded");
43}
44
45plResult UnloadPluginModule(plPluginModule& ref_pModule, plStringView sPluginFile)
46{
47 // reset last error code
48 SetLastError(ERROR_SUCCESS);
49
50 if (FreeLibrary(ref_pModule) == FALSE)
51 {
52 plLog::Error("Could not unload plugin '{0}'. Error-Code {1}", sPluginFile, plArgErrorCode(GetLastError()));
53 return PL_FAILURE;
54 }
55
56 ref_pModule = nullptr;
57 return PL_SUCCESS;
58}
59
60plResult LoadPluginModule(plStringView sFileToLoad, plPluginModule& ref_pModule, plStringView sPluginFile)
61{
62 // reset last error code
63 SetLastError(ERROR_SUCCESS);
64
65# if PL_ENABLED(PL_PLATFORM_WINDOWS_UWP)
66 plStringBuilder relativePath = sFileToLoad;
67 PL_SUCCEED_OR_RETURN(relativePath.MakeRelativeTo(plOSFile::GetApplicationDirectory()));
68 ref_pModule = LoadPackagedLibrary(plStringWChar(relativePath).GetData(), 0);
69# else
70 ref_pModule = LoadLibraryW(plStringWChar(sFileToLoad).GetData());
71# endif
72
73 if (ref_pModule == nullptr)
74 {
75 const DWORD err = GetLastError();
76 plLog::Error("Could not load plugin '{0}'. Error-Code {1}", sPluginFile, plArgErrorCode(err));
77
78 if (err == 126)
79 {
80 plLog::Error("Please Note: This means that the plugin exists, but a DLL dependency of the plugin is missing. You probably need to copy 3rd "
81 "party DLLs next to the plugin.");
82 }
83
84 return PL_FAILURE;
85 }
86
87 return PL_SUCCESS;
88}
89
90#else
91# error "This file should not have been included."
92#endif
static void Error(plLogInterface *pInterface, const plFormatString &string)
An error that needs to be fixed as soon as possible.
Definition Log.cpp:375
static plStringView GetApplicationDirectory()
Returns the path to the directory in which the application binary is located.
Definition OSFile.cpp:331
static bool ExistsFile(plStringView sFile)
Checks whether the given file exists.
Definition OSFile.cpp:229
static const plString GetCurrentWorkingDirectory()
Returns the processes current working directory (CWD).
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
void Append(plUInt32 uiChar)
Appends a single Utf32 character.
Definition StringBuilder_inl.h:94
plResult MakeRelativeTo(plStringView sAbsolutePathToMakeThisRelativeTo)
Converts this path into a relative path to the path with the awesome variable name 'szAbsolutePathToM...
Definition StringBuilder.cpp:1000
void AppendFormat(const plFormatString &string)
Appends a formatted string. Uses '{}' formatting placeholders, see plFormatString for details.
Definition StringBuilder.cpp:1251
void AppendPath(plStringView sPath1, plStringView sPath2={}, plStringView sPath3={}, plStringView sPath4={})
Appends several path pieces. Makes sure they are always properly separated by a slash.
Definition StringBuilder.cpp:866
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A very simple string class that should only be used to temporarily convert text to the OSes native wc...
Definition StringConversion.h:20
Converts a windows HRESULT into an error code and a human-readable error message. Pass in GetLastErro...
Definition FormatStringArgs.h:140
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54