1+ // Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
2+
3+ #include " DiscordRpcPrivatePCH.h"
4+ #include " IPluginManager.h"
5+ #include " ModuleManager.h"
6+
7+ #define LOCTEXT_NAMESPACE " FDiscordRpcModule"
8+
9+ void FDiscordRpcModule::StartupModule ()
10+ {
11+ #if !PLATFORM_LINUX
12+ #if defined(DISCORD_DYNAMIC_LIB)
13+ // Get the base directory of this plugin
14+ FString BaseDir = IPluginManager::Get ().FindPlugin (" DiscordRpc" )->GetBaseDir ();
15+ const FString SDKDir = FPaths::Combine (*BaseDir, TEXT (" Source" ), TEXT (" ThirdParty" ), TEXT (" DiscordRpcLibrary" ));
16+ #if PLATFORM_WINDOWS
17+ const FString LibName = TEXT (" discord-rpc" );
18+ const FString LibDir = FPaths::Combine (*SDKDir, TEXT (" Win64" ));
19+ if (!LoadDependency (LibDir, LibName, DiscordRpcLibraryHandle)) {
20+ FMessageDialog::Open (EAppMsgType::Ok, LOCTEXT (LOCTEXT_NAMESPACE, " Failed to load DiscordRpc plugin. Plug-in will not be functional." ));
21+ FreeDependency (DiscordRpcLibraryHandle);
22+ }
23+ #elif PLATFORM_MAC
24+ const FString LibName = TEXT (" libdiscord-rpc" );
25+ const FString LibDir = FPaths::Combine (*SDKDir, TEXT (" Mac" ));
26+ if (!LoadDependency (LibDir, LibName, DiscordRpcLibraryHandle)) {
27+ FMessageDialog::Open (EAppMsgType::Ok, LOCTEXT (LOCTEXT_NAMESPACE, " Failed to load DiscordRpc plugin. Plug-in will not be functional." ));
28+ FreeDependency (DiscordRpcLibraryHandle);
29+ }
30+ #endif
31+ #endif
32+ #endif
33+ }
34+
35+ void FDiscordRpcModule::ShutdownModule ()
36+ {
37+ // Free the dll handle
38+ #if !PLATFORM_LINUX
39+ #if defined(DISCORD_DYNAMIC_LIB)
40+ FreeDependency (DiscordRpcLibraryHandle);
41+ #endif
42+ #endif
43+ }
44+
45+ bool FDiscordRpcModule::LoadDependency (const FString& Dir, const FString& Name, void *& Handle)
46+ {
47+ FString Lib = Name + TEXT (" ." ) + FPlatformProcess::GetModuleExtension ();
48+ FString Path = Dir.IsEmpty () ? *Lib : FPaths::Combine (*Dir, *Lib);
49+
50+ Handle = FPlatformProcess::GetDllHandle (*Path);
51+
52+ if (Handle == nullptr )
53+ {
54+ return false ;
55+ }
56+
57+ return true ;
58+ }
59+
60+ void FDiscordRpcModule::FreeDependency (void *& Handle)
61+ {
62+ if (Handle != nullptr )
63+ {
64+ FPlatformProcess::FreeDllHandle (Handle);
65+ Handle = nullptr ;
66+ }
67+ }
68+
69+ #undef LOCTEXT_NAMESPACE
70+
71+ IMPLEMENT_MODULE (FDiscordRpcModule, DiscordRpc)
0 commit comments