Skip to content

Commit

Permalink
Merge pull request #92 from Kaffeine/loading_and_lookup
Browse files Browse the repository at this point in the history
Wrap dynamic library loading and function lookup.
  • Loading branch information
Pavel Kovalenko committed Nov 24, 2015
2 parents 7048f0a + 28acac8 commit 60dc401
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 38 deletions.
24 changes: 24 additions & 0 deletions src/xrCore/ModuleLookup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "ModuleLookup.hpp"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace XRay {
HMODULE LoadLibrary(const char *libraryFileName, bool log)
{
if (log)
Log("Loading DLL:", libraryFileName);
return ::LoadLibrary(libraryFileName);
}

void UnloadLibrary(HMODULE libraryHandle)
{
FreeLibrary(libraryHandle);
}

void *GetProcAddress(HMODULE libraryHandle, const char *procName)
{
return ::GetProcAddress(libraryHandle, procName);
}

}
9 changes: 9 additions & 0 deletions src/xrCore/ModuleLookup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "xrCore.h"

namespace XRay {
XRCORE_API HMODULE LoadLibrary(const char *libraryFileName, bool log = true);
XRCORE_API void UnloadLibrary(HMODULE libraryHandle);
XRCORE_API void *GetProcAddress(HMODULE libraryHandle, const char *procName);
}
2 changes: 2 additions & 0 deletions src/xrCore/xrCore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@
<ClCompile Include="Threading\ttapi.cpp" />
<ClCompile Include="Threading\Lock.cpp" />
<ClCompile Include="xrCore.cpp" />
<ClCompile Include="ModuleLookup.cpp" />
<ClCompile Include="xrDebugNew.cpp" />
<ClCompile Include="xrMemory.cpp" />
<ClCompile Include="xrMemory_align.cpp" />
Expand Down Expand Up @@ -508,6 +509,7 @@
<ClInclude Include="Threading\Lock.hpp" />
<ClInclude Include="vector.h" />
<ClInclude Include="xrCore.h" />
<ClInclude Include="ModuleLookup.hpp" />
<ClInclude Include="Platform.h" />
<ClInclude Include="xrDebug.h" />
<ClInclude Include="xrDebug_macros.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/xrCore/xrCore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<ClCompile Include="xrCore.cpp">
<Filter>Kernel</Filter>
</ClCompile>
<ClCompile Include="ModuleLookup.cpp">
<Filter>Kernel</Filter>
</ClCompile>
<ClCompile Include="_compressed_normal.cpp">
<Filter>Math</Filter>
</ClCompile>
Expand Down Expand Up @@ -401,6 +404,9 @@
<ClInclude Include="xrCore.h">
<Filter>Kernel</Filter>
</ClInclude>
<ClInclude Include="ModuleLookup.hpp">
<Filter>Kernel</Filter>
</ClInclude>
<ClInclude Include="Platform.h">
<Filter>Kernel</Filter>
</ClInclude>
Expand Down
8 changes: 5 additions & 3 deletions src/xrEngine/Device_Initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
#include "GameFont.h"
#include "PerformanceAlert.hpp"

#include "xrCore/ModuleLookup.hpp"

extern LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

#ifdef INGAME_EDITOR
void CRenderDevice::initialize_editor()
{
m_editor_module = LoadLibrary("xrWeatherEditor");
m_editor_module = XRay::LoadLibrary("xrWeatherEditor");
if (!m_editor_module)
{
Msg("! cannot load library \"xrWeatherEditor\"");
return;
}
m_editor_initialize = (initialize_function_ptr)GetProcAddress(m_editor_module, "initialize");
m_editor_initialize = (initialize_function_ptr)XRay::GetProcAddress(m_editor_module, "initialize");
VERIFY(m_editor_initialize);
m_editor_finalize = (finalize_function_ptr)GetProcAddress(m_editor_module, "finalize");
m_editor_finalize = (finalize_function_ptr)XRay::GetProcAddress(m_editor_module, "finalize");
VERIFY(m_editor_finalize);
m_engine = xr_new<engine_impl>();
m_editor_initialize(m_editor, m_engine);
Expand Down
53 changes: 24 additions & 29 deletions src/xrEngine/EngineAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "xrCDB/xrXRC.h"
#include "xrScriptEngine/script_engine.hpp"

#include "xrCore/ModuleLookup.hpp"

extern xr_token* vid_quality_token;

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -62,8 +64,7 @@ void CEngineAPI::InitializeNotDedicated()
if (psDeviceFlags.test(rsR4))
{
// try to initialize R4
Log("Loading DLL:", r4_name);
hRender = LoadLibrary(r4_name);
hRender = XRay::LoadLibrary(r4_name);
if (0 == hRender)
{
// try to load R1
Expand All @@ -75,8 +76,7 @@ void CEngineAPI::InitializeNotDedicated()
if (psDeviceFlags.test(rsR3))
{
// try to initialize R3
Log("Loading DLL:", r3_name);
hRender = LoadLibrary(r3_name);
hRender = XRay::LoadLibrary(r3_name);
if (0 == hRender)
{
// try to load R1
Expand All @@ -92,8 +92,7 @@ void CEngineAPI::InitializeNotDedicated()
// try to initialize R2
psDeviceFlags.set(rsR4, FALSE);
psDeviceFlags.set(rsR3, FALSE);
Log("Loading DLL:", r2_name);
hRender = LoadLibrary(r2_name);
hRender = XRay::LoadLibrary(r2_name);
if (0 == hRender)
{
// try to load R1
Expand Down Expand Up @@ -124,22 +123,20 @@ void CEngineAPI::Initialize(void)
psDeviceFlags.set(rsR2, FALSE);
renderer_value = 0; //con cmd

Log("Loading DLL:", r1_name);
hRender = LoadLibrary(r1_name);
hRender = XRay::LoadLibrary(r1_name);
if (0 == hRender) R_CHK(GetLastError());
R_ASSERT(hRender);
g_current_renderer = 1;
}
// game
{
LPCSTR g_name = "xrGame";
Log("Loading DLL:", g_name);
hGame = LoadLibrary(g_name);
hGame = XRay::LoadLibrary(g_name);
if (0 == hGame) R_CHK(GetLastError());
R_ASSERT2(hGame, "Game DLL raised exception during loading or there is no game DLL at all");
pCreate = (Factory_Create*)GetProcAddress(hGame, "xrFactory_Create");
pCreate = (Factory_Create*)XRay::GetProcAddress(hGame, "xrFactory_Create");
R_ASSERT(pCreate);
pDestroy = (Factory_Destroy*)GetProcAddress(hGame, "xrFactory_Destroy");
pDestroy = (Factory_Destroy*)XRay::GetProcAddress(hGame, "xrFactory_Destroy");
R_ASSERT(pDestroy);
}

Expand All @@ -149,22 +146,21 @@ void CEngineAPI::Initialize(void)
if (strstr(Core.Params, "-tune"))
{
LPCSTR g_name = "vTuneAPI";
Log("Loading DLL:", g_name);
hTuner = LoadLibrary(g_name);
hTuner = XRay::LoadLibrary(g_name);
if (0 == hTuner) R_CHK(GetLastError());
R_ASSERT2(hTuner, "Intel vTune is not installed");
tune_enabled = TRUE;
tune_pause = (VTPause*)GetProcAddress(hTuner, "VTPause");
tune_pause = (VTPause*)XRay::GetProcAddress(hTuner, "VTPause");
R_ASSERT(tune_pause);
tune_resume = (VTResume*)GetProcAddress(hTuner, "VTResume");
tune_resume = (VTResume*)XRay::GetProcAddress(hTuner, "VTResume");
R_ASSERT(tune_resume);
}
}

void CEngineAPI::Destroy(void)
{
if (hGame) { FreeLibrary(hGame); hGame = 0; }
if (hRender) { FreeLibrary(hRender); hRender = 0; }
if (hGame) { XRay::UnloadLibrary(hGame); hGame = 0; }
if (hRender) { XRay::UnloadLibrary(hRender); hRender = 0; }
pCreate = 0;
pDestroy = 0;
Engine.Event._destroy();
Expand Down Expand Up @@ -211,45 +207,44 @@ void CEngineAPI::CreateRendererList()
else
{
// try to initialize R2
Log("Loading DLL:", r2_name);
hRender = LoadLibrary(r2_name);
hRender = XRay::LoadLibrary(r2_name);
if (hRender)
{
bSupports_r2 = true;
SupportsAdvancedRendering* test_rendering = (SupportsAdvancedRendering*)GetProcAddress(hRender, "SupportsAdvancedRendering");
SupportsAdvancedRendering* test_rendering = (SupportsAdvancedRendering*)XRay::GetProcAddress(hRender, "SupportsAdvancedRendering");
R_ASSERT(test_rendering);
bSupports_r2_5 = test_rendering();
FreeLibrary(hRender);
XRay::UnloadLibrary(hRender);
}

// try to initialize R3
Log("Loading DLL:", r3_name);
// Hide "d3d10.dll not found" message box for XP
SetErrorMode(SEM_FAILCRITICALERRORS);
hRender = LoadLibrary(r3_name);
hRender = XRay::LoadLibrary(r3_name);
// Restore error handling
SetErrorMode(0);
if (hRender)
{
SupportsDX10Rendering* test_dx10_rendering = (SupportsDX10Rendering*)GetProcAddress(hRender, "SupportsDX10Rendering");
SupportsDX10Rendering* test_dx10_rendering = (SupportsDX10Rendering*)XRay::GetProcAddress(hRender, "SupportsDX10Rendering");
R_ASSERT(test_dx10_rendering);
bSupports_r3 = test_dx10_rendering();
FreeLibrary(hRender);
XRay::UnloadLibrary(hRender);
}

// try to initialize R4
Log("Loading DLL:", r4_name);
// Hide "d3d10.dll not found" message box for XP
SetErrorMode(SEM_FAILCRITICALERRORS);
hRender = LoadLibrary(r4_name);
hRender = XRay::LoadLibrary(r4_name);
// Restore error handling
SetErrorMode(0);
if (hRender)
{
SupportsDX11Rendering* test_dx11_rendering = (SupportsDX11Rendering*)GetProcAddress(hRender, "SupportsDX11Rendering");
SupportsDX11Rendering* test_dx11_rendering = (SupportsDX11Rendering*)XRay::GetProcAddress(hRender, "SupportsDX11Rendering");
R_ASSERT(test_dx11_rendering);
bSupports_r4 = test_dx11_rendering();
FreeLibrary(hRender);
XRay::UnloadLibrary(hRender);
}
}

Expand Down Expand Up @@ -388,4 +383,4 @@ void CEngineAPI::CreateRendererList()
}
*/
#endif //#ifndef DEDICATED_SERVER
}
}
13 changes: 7 additions & 6 deletions src/xrEngine/xr_input_xinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "xr_input_xinput.h"
#include <xinput.h>
#include "xrCore/ModuleLookup.hpp"
DXUT_GAMEPAD g_GamePads[DXUT_MAX_CONTROLLERS];
Expand All @@ -20,11 +21,11 @@ static LPXINPUTGETSTATE s_pXInputGetState = NULL;
static LPXINPUTGETCAPABILITIES s_pXInputGetCapabilities = NULL;
if( NULL == s_pXInputGetState || NULL == s_pXInputGetCapabilities )
{
HINSTANCE hInst = LoadLibrary( XINPUT_DLL );
HINSTANCE hInst = XRay::LoadLibrary( XINPUT_DLL );
if( hInst )
{
s_pXInputGetState = (LPXINPUTGETSTATE)GetProcAddress( hInst, "XInputGetState" );
s_pXInputGetCapabilities = (LPXINPUTGETCAPABILITIES)GetProcAddress( hInst, "XInputGetCapabilities" );
s_pXInputGetState = (LPXINPUTGETSTATE)XRay::GetProcAddress( hInst, "XInputGetState" );
s_pXInputGetCapabilities = (LPXINPUTGETCAPABILITIES)XRay::GetProcAddress( hInst, "XInputGetCapabilities" );
}
}
if( s_pXInputGetState == NULL )
Expand Down Expand Up @@ -114,9 +115,9 @@ void set_vibration (u16 s1, u16 s2)
static LPXINPUTSETSTATE s_pXInputSetState = NULL;
if( NULL == s_pXInputSetState )
{
HINSTANCE hInst = LoadLibrary( XINPUT_DLL );
HINSTANCE hInst = XRay::LoadLibrary( XINPUT_DLL );
if( hInst )
s_pXInputSetState = (LPXINPUTSETSTATE)GetProcAddress( hInst, "XInputSetState" );
s_pXInputSetState = (LPXINPUTSETSTATE)XRay::GetProcAddress( hInst, "XInputSetState" );
}
Expand All @@ -125,4 +126,4 @@ vibration.wLeftMotorSpeed = s1;
vibration.wRightMotorSpeed = s2;
s_pXInputSetState ( 0, &vibration );
}
*/
*/

0 comments on commit 60dc401

Please sign in to comment.