Skip to content

Commit 58a4a5f

Browse files
committed
Set up cross-platform ModDLL loading
1 parent 20986f4 commit 58a4a5f

File tree

7 files changed

+61
-37
lines changed

7 files changed

+61
-37
lines changed

Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm

-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ @interface AppDelegate : NSWindowController <NSApplicationDelegate,
120120
}
121121
void plClient::IChangeResolution(int width, int height) {}
122122
void plClient::IUpdateProgressIndicator(plOperationProgress* progress) {}
123-
void plClient::InitDLLs() {}
124-
void plClient::ShutdownDLLs() {}
125123
void plClient::ShowClientWindow() {}
126124
void plClient::FlashWindow()
127125
{

Sources/Plasma/Apps/plClient/linux/main.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed)
176176

177177
void plClient::IChangeResolution(int width, int height) {}
178178
void plClient::IUpdateProgressIndicator(plOperationProgress* progress) {}
179-
void plClient::InitDLLs() {}
180-
void plClient::ShutdownDLLs() {}
181179

182180
void plClient::ShowClientWindow() {
183181
/* Map the window on the screen */

Sources/Plasma/Apps/plClient/main.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ You can contact Cyan Worlds, Inc. by email [email protected]
5050
void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed) {}
5151
void plClient::IChangeResolution(int width, int height) {}
5252
void plClient::IUpdateProgressIndicator(plOperationProgress* progress) {}
53-
void plClient::InitDLLs() {}
54-
void plClient::ShutdownDLLs() {}
5553
void plClient::ShowClientWindow() {}
5654
void plClient::FlashWindow() {}
5755

Sources/Plasma/Apps/plClient/plClient.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
6262

6363
#include "pnDispatch/plDispatch.h"
6464
#include "pnDispatch/plDispatchLogBase.h"
65+
#include "pnFactory/plFactory.h"
6566
#include "pnKeyedObject/plFixedKey.h"
6667
#include "pnKeyedObject/plKey.h"
6768
#include "pnMessage/plAudioSysMsg.h"
@@ -155,6 +156,9 @@ You can contact Cyan Worlds, Inc. by email [email protected]
155156
#include "pfPython/cyMisc.h"
156157
#include "pfPython/cyPythonInterface.h"
157158

159+
#ifdef HS_BUILD_FOR_UNIX
160+
# include <dlfcn.h> // For ModDLL loading
161+
#endif
158162

159163
#define MSG_LOADING_BAR
160164

@@ -370,6 +374,58 @@ bool plClient::Shutdown()
370374
return false;
371375
}
372376

377+
void plClient::InitDLLs() {
378+
hsStatusMessage("Init dlls client\n");
379+
380+
std::vector<plFileName> dlls = plFileSystem::ListDir("ModDLL",
381+
#if defined(HS_BUILD_FOR_WIN32)
382+
"*.dll"
383+
#elif defined(HS_BUILD_FOR_APPLE)
384+
"*.dylib"
385+
#else
386+
"*.so"
387+
#endif
388+
);
389+
390+
for (auto iter = dlls.begin(); iter != dlls.end(); ++iter)
391+
{
392+
#ifdef HS_BUILD_FOR_WIN32
393+
hsLibraryHndl mod = LoadLibraryW(iter->WideString().data());
394+
#else
395+
hsLibraryHndl mod = dlopen(iter->AsString().c_str(), RTLD_LAZY | RTLD_LOCAL);
396+
#endif
397+
398+
if (mod)
399+
{
400+
#ifdef HS_BUILD_FOR_WIN32
401+
pInitGlobalsFunc initGlobals = (pInitGlobalsFunc)GetProcAddress(mod, "InitGlobals");
402+
#else
403+
pInitGlobalsFunc initGlobals = (pInitGlobalsFunc)dlsym(mod, "InitGlobals");
404+
#endif
405+
406+
(*initGlobals)(hsgResMgr::ResMgr(), plFactory::GetTheFactory(), plgTimerCallbackMgr::Mgr(),
407+
hsTimer::GetTheTimer(), plNetClientApp::GetInstance());
408+
fLoadedDLLs.emplace_back(mod);
409+
}
410+
}
411+
}
412+
413+
void plClient::ShutdownDLLs()
414+
{
415+
for (hsLibraryHndl mod : fLoadedDLLs)
416+
{
417+
#ifdef HS_BUILD_FOR_WIN32
418+
BOOL ret = FreeLibrary(mod);
419+
if (!ret)
420+
hsStatusMessage("Failed to free lib\n");
421+
#else
422+
dlclose(mod);
423+
#endif
424+
}
425+
426+
fLoadedDLLs.clear();
427+
}
428+
373429
void plClient::InitAuxInits()
374430
{
375431
// Use another init directory specified in Command line Arg -i

Sources/Plasma/Apps/plClient/plClient.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ class plClient : public hsKeyedObject
178178

179179
int fNumPostLoadMsgs;
180180
float fPostLoadMsgInc;
181-
181+
182+
std::vector<hsLibraryHndl> fLoadedDLLs;
183+
182184
void ICompleteInit ();
183185
void IOnAsyncInitComplete ();
184186
void IHandlePatcherMsg (plResPatcherMsg * msg);

Sources/Plasma/Apps/plClient/win32/plClient_Win.cpp

-30
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ You can contact Cyan Worlds, Inc. by email [email protected]
5151
#include "plClient.h"
5252
#include "plWinDpi/plWinDpi.h"
5353

54-
#include "pnFactory/plFactory.h"
5554
#include "pnNetCommon/plNetApp.h"
5655
#include "plProgressMgr/plProgressMgr.h"
5756

5857
extern ITaskbarList3* gTaskbarList;
59-
static std::vector<HMODULE> fLoadedDLLs;
6058

6159
void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed)
6260
{
@@ -146,34 +144,6 @@ void plClient::IUpdateProgressIndicator(plOperationProgress* progress)
146144
}
147145
}
148146

149-
void plClient::InitDLLs()
150-
{
151-
hsStatusMessage("Init dlls client\n");
152-
std::vector<plFileName> dlls = plFileSystem::ListDir("ModDLL", "*.dll");
153-
for (auto iter = dlls.begin(); iter != dlls.end(); ++iter)
154-
{
155-
HMODULE hMod = LoadLibraryW(iter->WideString().data());
156-
if (hMod)
157-
{
158-
pInitGlobalsFunc initGlobals = (pInitGlobalsFunc)GetProcAddress(hMod, "InitGlobals");
159-
(*initGlobals)(hsgResMgr::ResMgr(), plFactory::GetTheFactory(), plgTimerCallbackMgr::Mgr(),
160-
hsTimer::GetTheTimer(), plNetClientApp::GetInstance());
161-
fLoadedDLLs.emplace_back(hMod);
162-
}
163-
}
164-
}
165-
166-
void plClient::ShutdownDLLs()
167-
{
168-
for (HMODULE dll : fLoadedDLLs)
169-
{
170-
BOOL ret = FreeLibrary(dll);
171-
if (!ret)
172-
hsStatusMessage("Failed to free lib\n");
173-
}
174-
fLoadedDLLs.clear();
175-
}
176-
177147
// Show the client window
178148
void plClient::ShowClientWindow()
179149
{

Sources/Plasma/CoreLib/HeadSpin.h

+2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ You can contact Cyan Worlds, Inc. by email [email protected]
7171
typedef HWND hsWindowHndl;
7272
typedef HINSTANCE hsWindowInst;
7373
typedef HINSTANCE HMODULE;
74+
typedef HMODULE hsLibraryHndl;
7475
typedef long HRESULT;
7576
typedef void* HANDLE;
7677
#else
7778
typedef int32_t* hsWindowHndl;
7879
typedef int32_t* hsWindowInst;
80+
typedef void* hsLibraryHndl;
7981
#endif // HS_BUILD_FOR_WIN32
8082

8183
//======================================

0 commit comments

Comments
 (0)