Skip to content

Commit 8c37df0

Browse files
committed
Set up cross-platform ModDLL loading
1 parent c4053eb commit 8c37df0

File tree

6 files changed

+60
-34
lines changed

6 files changed

+60
-34
lines changed

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

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

173173
void plClient::IChangeResolution(int width, int height) {}
174174
void plClient::IUpdateProgressIndicator(plOperationProgress* progress) {}
175-
void plClient::InitDLLs() {}
176-
void plClient::ShutdownDLLs() {}
177175

178176
void plClient::ShowClientWindow() {
179177
/* 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

+55
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ You can contact Cyan Worlds, Inc. by email [email protected]
150150
#include "pfPython/cyMisc.h"
151151
#include "pfPython/cyPythonInterface.h"
152152

153+
#ifdef HS_BUILD_FOR_UNIX
154+
# include <dlfcn.h> // For ModDLL loading
155+
#endif
153156

154157
#define MSG_LOADING_BAR
155158

@@ -365,6 +368,58 @@ bool plClient::Shutdown()
365368
return false;
366369
}
367370

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

-29
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ You can contact Cyan Worlds, Inc. by email [email protected]
5555
#include "plProgressMgr/plProgressMgr.h"
5656

5757
extern ITaskbarList3* gTaskbarList;
58-
static std::vector<HMODULE> fLoadedDLLs;
5958

6059
void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed)
6160
{
@@ -139,34 +138,6 @@ void plClient::IUpdateProgressIndicator(plOperationProgress* progress)
139138
}
140139
}
141140

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

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)