From 2b43ae0c6dccb89ec19a3b634b7ced3e2adf9024 Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Fri, 16 Jun 2023 21:46:59 -0700 Subject: [PATCH] Set up cross-platform ModDLL loading --- Sources/Plasma/Apps/plClient/linux/main.cpp | 2 - Sources/Plasma/Apps/plClient/main.cpp | 2 - Sources/Plasma/Apps/plClient/plClient.cpp | 55 +++++++++++++++++++ Sources/Plasma/Apps/plClient/plClient.h | 4 +- .../Apps/plClient/win32/plClient_Win.cpp | 29 ---------- Sources/Plasma/CoreLib/HeadSpin.h | 2 + 6 files changed, 60 insertions(+), 34 deletions(-) diff --git a/Sources/Plasma/Apps/plClient/linux/main.cpp b/Sources/Plasma/Apps/plClient/linux/main.cpp index d0870d7cbf..3e77fe5836 100644 --- a/Sources/Plasma/Apps/plClient/linux/main.cpp +++ b/Sources/Plasma/Apps/plClient/linux/main.cpp @@ -172,8 +172,6 @@ void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed) void plClient::IChangeResolution(int width, int height) {} void plClient::IUpdateProgressIndicator(plOperationProgress* progress) {} -void plClient::InitDLLs() {} -void plClient::ShutdownDLLs() {} void plClient::ShowClientWindow() { /* Map the window on the screen */ diff --git a/Sources/Plasma/Apps/plClient/main.cpp b/Sources/Plasma/Apps/plClient/main.cpp index 3a11817fd3..610deceddc 100644 --- a/Sources/Plasma/Apps/plClient/main.cpp +++ b/Sources/Plasma/Apps/plClient/main.cpp @@ -50,8 +50,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed) {} void plClient::IChangeResolution(int width, int height) {} void plClient::IUpdateProgressIndicator(plOperationProgress* progress) {} -void plClient::InitDLLs() {} -void plClient::ShutdownDLLs() {} void plClient::ShowClientWindow() {} void plClient::FlashWindow() {} diff --git a/Sources/Plasma/Apps/plClient/plClient.cpp b/Sources/Plasma/Apps/plClient/plClient.cpp index 9e2bd3121b..32d9e7a470 100644 --- a/Sources/Plasma/Apps/plClient/plClient.cpp +++ b/Sources/Plasma/Apps/plClient/plClient.cpp @@ -155,6 +155,9 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "pfPython/cyMisc.h" #include "pfPython/cyPythonInterface.h" +#ifdef HS_BUILD_FOR_UNIX +# include // For ModDLL loading +#endif #define MSG_LOADING_BAR @@ -370,6 +373,58 @@ bool plClient::Shutdown() return false; } +void plClient::InitDLLs() { + hsStatusMessage("Init dlls client\n"); + + std::vector dlls = plFileSystem::ListDir("ModDLL", +#if defined(HS_BUILD_FOR_WIN32) + "*.dll" +#elif defined(HS_BUILD_FOR_APPLE) + "*.dylib" +#else + "*.so" +#endif + ); + + for (auto iter = dlls.begin(); iter != dlls.end(); ++iter) + { +#ifdef HS_BUILD_FOR_WIN32 + hsLibraryHndl mod = LoadLibraryW(iter->WideString().data()); +#else + hsLibraryHndl mod = dlopen(iter->AsString().c_str(), RTLD_LAZY | RTLD_LOCAL); +#endif + + if (mod) + { +#ifdef HS_BUILD_FOR_WIN32 + pInitGlobalsFunc initGlobals = (pInitGlobalsFunc)GetProcAddress(mod, "InitGlobals"); +#else + pInitGlobalsFunc initGlobals = (pInitGlobalsFunc)dlsym(mod, "InitGlobals"); +#endif + + (*initGlobals)(hsgResMgr::ResMgr(), plFactory::GetTheFactory(), plgTimerCallbackMgr::Mgr(), + hsTimer::GetTheTimer(), plNetClientApp::GetInstance()); + fLoadedDLLs.emplace_back(mod); + } + } +} + +void plClient::ShutdownDLLs() +{ + for (hsLibraryHndl mod : fLoadedDLLs) + { +#ifdef HS_BUILD_FOR_WIN32 + BOOL ret = FreeLibrary(mod); + if (!ret) + hsStatusMessage("Failed to free lib\n"); +#else + dlclose(mod); +#endif + } + + fLoadedDLLs.clear(); +} + void plClient::InitAuxInits() { // Use another init directory specified in Command line Arg -i diff --git a/Sources/Plasma/Apps/plClient/plClient.h b/Sources/Plasma/Apps/plClient/plClient.h index dd839cb5c8..5baafffa45 100644 --- a/Sources/Plasma/Apps/plClient/plClient.h +++ b/Sources/Plasma/Apps/plClient/plClient.h @@ -178,7 +178,9 @@ class plClient : public hsKeyedObject int fNumPostLoadMsgs; float fPostLoadMsgInc; - + + std::vector fLoadedDLLs; + void ICompleteInit (); void IOnAsyncInitComplete (); void IHandlePatcherMsg (plResPatcherMsg * msg); diff --git a/Sources/Plasma/Apps/plClient/win32/plClient_Win.cpp b/Sources/Plasma/Apps/plClient/win32/plClient_Win.cpp index 0604e448c5..f0384f5d64 100644 --- a/Sources/Plasma/Apps/plClient/win32/plClient_Win.cpp +++ b/Sources/Plasma/Apps/plClient/win32/plClient_Win.cpp @@ -56,7 +56,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plProgressMgr/plProgressMgr.h" extern ITaskbarList3* gTaskbarList; -static std::vector fLoadedDLLs; void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed) { @@ -146,34 +145,6 @@ void plClient::IUpdateProgressIndicator(plOperationProgress* progress) } } -void plClient::InitDLLs() -{ - hsStatusMessage("Init dlls client\n"); - std::vector dlls = plFileSystem::ListDir("ModDLL", "*.dll"); - for (auto iter = dlls.begin(); iter != dlls.end(); ++iter) - { - HMODULE hMod = LoadLibraryW(iter->WideString().data()); - if (hMod) - { - pInitGlobalsFunc initGlobals = (pInitGlobalsFunc)GetProcAddress(hMod, "InitGlobals"); - (*initGlobals)(hsgResMgr::ResMgr(), plFactory::GetTheFactory(), plgTimerCallbackMgr::Mgr(), - hsTimer::GetTheTimer(), plNetClientApp::GetInstance()); - fLoadedDLLs.emplace_back(hMod); - } - } -} - -void plClient::ShutdownDLLs() -{ - for (HMODULE dll : fLoadedDLLs) - { - BOOL ret = FreeLibrary(dll); - if (!ret) - hsStatusMessage("Failed to free lib\n"); - } - fLoadedDLLs.clear(); -} - // Show the client window void plClient::ShowClientWindow() { diff --git a/Sources/Plasma/CoreLib/HeadSpin.h b/Sources/Plasma/CoreLib/HeadSpin.h index e7e581796a..4e64176c1c 100644 --- a/Sources/Plasma/CoreLib/HeadSpin.h +++ b/Sources/Plasma/CoreLib/HeadSpin.h @@ -71,11 +71,13 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com typedef HWND hsWindowHndl; typedef HINSTANCE hsWindowInst; typedef HINSTANCE HMODULE; + typedef HMODULE hsLibraryHndl; typedef long HRESULT; typedef void* HANDLE; #else typedef int32_t* hsWindowHndl; typedef int32_t* hsWindowInst; + typedef void* hsLibraryHndl; #endif // HS_BUILD_FOR_WIN32 //======================================