Skip to content

Commit

Permalink
Set up cross-platform ModDLL loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Aug 4, 2023
1 parent db790ef commit 2527e76
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
2 changes: 0 additions & 2 deletions Sources/Plasma/Apps/plClient/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 0 additions & 2 deletions Sources/Plasma/Apps/plClient/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down
55 changes: 55 additions & 0 deletions Sources/Plasma/Apps/plClient/plClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dlfcn.h> // For ModDLL loading
#endif

#define MSG_LOADING_BAR

Expand Down Expand Up @@ -370,6 +373,58 @@ bool plClient::Shutdown()
return false;
}

void plClient::InitDLLs() {
hsStatusMessage("Init dlls client\n");

std::vector<plFileName> 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
Expand Down
4 changes: 3 additions & 1 deletion Sources/Plasma/Apps/plClient/plClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ class plClient : public hsKeyedObject

int fNumPostLoadMsgs;
float fPostLoadMsgInc;


std::vector<hsLibraryHndl> fLoadedDLLs;

void ICompleteInit ();
void IOnAsyncInitComplete ();
void IHandlePatcherMsg (plResPatcherMsg * msg);
Expand Down
29 changes: 0 additions & 29 deletions Sources/Plasma/Apps/plClient/win32/plClient_Win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HMODULE> fLoadedDLLs;

void plClient::IResizeNativeDisplayDevice(int width, int height, bool windowed)
{
Expand Down Expand Up @@ -146,34 +145,6 @@ void plClient::IUpdateProgressIndicator(plOperationProgress* progress)
}
}

void plClient::InitDLLs()
{
hsStatusMessage("Init dlls client\n");
std::vector<plFileName> 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()
{
Expand Down
2 changes: 2 additions & 0 deletions Sources/Plasma/CoreLib/HeadSpin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

//======================================
Expand Down

0 comments on commit 2527e76

Please sign in to comment.