Skip to content

Commit

Permalink
Move Dynamic library code out of util in to dynamic library file
Browse files Browse the repository at this point in the history
  • Loading branch information
project64 committed Apr 21, 2021
1 parent cdb7cdc commit 3a038b2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Source/Common/Common.vcxproj
Expand Up @@ -39,6 +39,7 @@
<ItemGroup>
<ClCompile Include="CriticalSection.cpp" />
<ClCompile Include="DateTime.cpp" />
<ClCompile Include="DynamicLibrary.cpp" />
<ClCompile Include="File.cpp" />
<ClCompile Include="HighResTimeStamp.cpp" />
<ClCompile Include="IniFile.cpp" />
Expand All @@ -58,6 +59,7 @@
<ItemGroup>
<ClInclude Include="CriticalSection.h" />
<ClInclude Include="DateTime.h" />
<ClInclude Include="DynamicLibrary.h" />
<ClInclude Include="File.h" />
<ClInclude Include="HighResTimeStamp.h" />
<ClInclude Include="IniFile.h" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Common/Common.vcxproj.filters
Expand Up @@ -62,6 +62,9 @@
<ClCompile Include="Random.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DynamicLibrary.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CriticalSection.h">
Expand Down Expand Up @@ -121,5 +124,8 @@
<ClInclude Include="Random.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DynamicLibrary.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
51 changes: 51 additions & 0 deletions Source/Common/DynamicLibrary.cpp
@@ -0,0 +1,51 @@
#include "DynamicLibrary.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#endif

DynLibHandle DynamicLibraryOpen(const char *pccLibraryPath, bool ShowErrors)
{
if (pccLibraryPath == nullptr)
{
return nullptr;
}
#ifdef _WIN32
UINT LastErrorMode = SetErrorMode(ShowErrors ? 0 : SEM_FAILCRITICALERRORS);
DynLibHandle Lib = (DynLibHandle)LoadLibraryA(pccLibraryPath);
SetErrorMode(LastErrorMode);
#else
pjutil::DynLibHandle Lib = (pjutil::DynLibHandle)dlopen(pccLibraryPath, RTLD_NOW);
#endif
return Lib;
}

void DynamicLibraryClose(DynLibHandle Lib)
{
if (Lib != nullptr)
{
#ifdef _WIN32
FreeLibrary((HMODULE)Lib);
#else
dlclose(Lib);
#endif
}
}

void * DynamicLibraryGetProc(DynLibHandle Lib, const char * ProcedureName)
{
if (ProcedureName == nullptr)
{
return nullptr;
}

#ifdef _WIN32
return GetProcAddress((HMODULE)Lib, ProcedureName);
#else
return dlsym(Lib, ProcedureName);
#endif
}

7 changes: 7 additions & 0 deletions Source/Common/DynamicLibrary.h
@@ -0,0 +1,7 @@
#pragma once

typedef void * DynLibHandle;

DynLibHandle DynamicLibraryOpen(const char * LibraryPath, bool ShowErrors = true);
void DynamicLibraryClose(DynLibHandle LibHandle);
void * DynamicLibraryGetProc(DynLibHandle LibHandle, const char * ProcedureName);
44 changes: 1 addition & 43 deletions Source/Common/Util.cpp
Expand Up @@ -5,51 +5,9 @@
#include <windows.h>
#include <Tlhelp32.h>
#else
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <time.h>
#endif

pjutil::DynLibHandle pjutil::DynLibOpen(const char *pccLibraryPath, bool ShowErrors)
{
if (pccLibraryPath == nullptr)
{
return nullptr;
}
#ifdef _WIN32
UINT LastErrorMode = SetErrorMode(ShowErrors ? 0 : SEM_FAILCRITICALERRORS);
pjutil::DynLibHandle lib = (pjutil::DynLibHandle)LoadLibraryA(pccLibraryPath);
SetErrorMode(LastErrorMode);
#else
pjutil::DynLibHandle lib = (pjutil::DynLibHandle)dlopen(pccLibraryPath, RTLD_NOW);
#endif
return lib;
}

void * pjutil::DynLibGetProc(pjutil::DynLibHandle LibHandle, const char * ProcedureName)
{
if (ProcedureName == nullptr)
return nullptr;

#ifdef _WIN32
return GetProcAddress((HMODULE)LibHandle, ProcedureName);
#else
return dlsym(LibHandle, ProcedureName);
#endif
}

void pjutil::DynLibClose(pjutil::DynLibHandle LibHandle)
{
if (LibHandle != nullptr)
{
#ifdef _WIN32
FreeLibrary((HMODULE)LibHandle);
#else
dlclose(LibHandle);
#endif
}
}

void pjutil::Sleep(uint32_t timeout)
{
#ifdef _WIN32
Expand Down
5 changes: 0 additions & 5 deletions Source/Common/Util.h
Expand Up @@ -4,11 +4,6 @@
class pjutil
{
public:
typedef void * DynLibHandle;

static DynLibHandle DynLibOpen(const char *pccLibraryPath, bool ShowErrors = true);
static void * DynLibGetProc(DynLibHandle LibHandle, const char * ProcedureName);
static void DynLibClose(DynLibHandle LibHandle);
static void Sleep(uint32_t timeout);
static bool TerminatedExistingExe();

Expand Down
2 changes: 1 addition & 1 deletion Source/Project64-core/Plugins/GFXPlugin.cpp
Expand Up @@ -263,7 +263,7 @@ void CGfxPlugin::UnloadPluginDetails(void)
WriteTrace(TraceGFXPlugin, TraceDebug, "start");
if (m_LibHandle != nullptr)
{
pjutil::DynLibClose(m_LibHandle);
DynamicLibraryClose(m_LibHandle);
m_LibHandle = nullptr;
}
memset(&m_GFXDebug, 0, sizeof(m_GFXDebug));
Expand Down
4 changes: 2 additions & 2 deletions Source/Project64-core/Plugins/PluginBase.cpp
Expand Up @@ -38,7 +38,7 @@ bool CPlugin::Load(const char * FileName)

// Try to load the plugin DLL
//Try to load the DLL library
m_LibHandle = pjutil::DynLibOpen(FileName, HaveDebugger());
m_LibHandle = DynamicLibraryOpen(FileName, HaveDebugger());
WriteTrace(PluginTraceType(), TraceDebug, "Loaded: %s LibHandle: %X", FileName, m_LibHandle);

if (m_LibHandle == nullptr)
Expand Down Expand Up @@ -238,7 +238,7 @@ void CPlugin::UnloadPlugin()
}
if (m_LibHandle != nullptr)
{
pjutil::DynLibClose(m_LibHandle);
DynamicLibraryClose(m_LibHandle);
m_LibHandle = nullptr;
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Project64-core/Plugins/PluginBase.h
Expand Up @@ -3,7 +3,7 @@
#include <Project64-core/Settings/DebugSettings.h>
#include <Project64-core/TraceModulesProject64.h>
#include <Project64-core/Plugins/Plugin.h>
#include <Common/Util.h>
#include <Common/DynamicLibrary.h>

#if defined(_WIN32)
#define CALL __cdecl
Expand Down Expand Up @@ -53,14 +53,14 @@ class CPlugin :
void(CALL *SetSettingNotificationInfo)(PLUGIN_SETTINGS_NOTIFICATION *);
void(CALL *SetPluginNotification)(PLUGIN_NOTIFICATION *);

pjutil::DynLibHandle m_LibHandle;
DynLibHandle m_LibHandle;
bool m_Initialized, m_RomOpen;
PLUGIN_INFO m_PluginInfo;

// Loads a function pointer from the currently loaded DLL
void _LoadFunctionVoid(const char * szFunctionName, void ** functionPointer)
{
*functionPointer = pjutil::DynLibGetProc(m_LibHandle, szFunctionName);
*functionPointer = DynamicLibraryGetProc(m_LibHandle, szFunctionName);
}

// Simple wrapper around _LoadFunction() to avoid having to specify the same two arguments
Expand Down

0 comments on commit 3a038b2

Please sign in to comment.