Skip to content

Commit b172ce0

Browse files
committed
Refactor: dynamic library link via XRay::Module
1 parent ea56b09 commit b172ce0

File tree

11 files changed

+130
-133
lines changed

11 files changed

+130
-133
lines changed

src/Layers/xrRender/HW.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,13 @@ void CHW::Reset(HWND hwnd)
8989

9090
void CHW::CreateD3D()
9191
{
92-
//#ifndef DEDICATED_SERVER
93-
// LPCSTR _name = "d3d9.dll";
94-
//#else
95-
// LPCSTR _name = "xrD3D9-Null";
96-
//#endif
97-
98-
LPCSTR _name = "xrD3D9-Null";
92+
const pcstr _name = GEnv.isDedicatedServer ? "xrD3D9-Null" : "d3d9.dll";
9993

100-
#ifndef _EDITOR
101-
if (!GEnv.isDedicatedServer)
102-
#endif
103-
_name = "d3d9.dll";
94+
hD3D = std::make_unique<XRay::Module>(_name);
10495

105-
hD3D = LoadLibrary(_name);
106-
R_ASSERT2(hD3D, "Can't find 'd3d9.dll'\nPlease install latest version of DirectX before running this program");
96+
R_ASSERT2(hD3D->exist(), "Can't find 'd3d9.dll'\nPlease install latest version of DirectX before running this program");
10797
typedef IDirect3D9* WINAPI _Direct3DCreate9(UINT SDKVersion);
108-
_Direct3DCreate9* createD3D = (_Direct3DCreate9*)GetProcAddress(hD3D, "Direct3DCreate9");
98+
auto createD3D = (_Direct3DCreate9*)hD3D->getProcAddress("Direct3DCreate9");
10999
R_ASSERT(createD3D);
110100
this->pD3D = createD3D(D3D_SDK_VERSION);
111101
R_ASSERT2(this->pD3D, "Please install DirectX 9.0c");
@@ -114,7 +104,7 @@ void CHW::CreateD3D()
114104
void CHW::DestroyD3D()
115105
{
116106
_RELEASE(this->pD3D);
117-
FreeLibrary(hD3D);
107+
//hD3D->close();
118108
}
119109

120110
//////////////////////////////////////////////////////////////////////

src/Layers/xrRender/HW.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include "HWCaps.h"
10+
#include "xrCore/ModuleLookup.hpp"
1011

1112
#ifndef _MAYA_EXPORT
1213
#include "stats_manager.h"
@@ -88,7 +89,7 @@ class CHW
8889
D3D_FEATURE_LEVEL FeatureLevel;
8990
#else
9091
private:
91-
HINSTANCE hD3D;
92+
std::unique_ptr<XRay::Module> hD3D;
9293

9394
public:
9495
IDirect3D9* pD3D; // D3D

src/Layers/xrRenderPC_R4/r2_test_hw.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "stdafx.h"
2+
#include "xrCore/ModuleLookup.hpp"
23

34
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
45
{
@@ -12,20 +13,20 @@ typedef HRESULT(__stdcall* FuncPtrD3D11CreateDeviceAndSwapChain)(IDXGIAdapter* p
1213

1314
bool TestDX11Present()
1415
{
15-
HMODULE hD3D11 = LoadLibrary("d3d11.dll");
16+
const auto hD3D11 = std::make_unique<XRay::Module>("d3d11.dll");
1617

17-
if (!hD3D11)
18+
if (!hD3D11->exist())
1819
{
19-
Msg("* DX11: failed to load d3d11.dll");
20+
Log("* DX11: failed to load d3d11.dll");
2021
return false;
2122
}
2223

23-
FuncPtrD3D11CreateDeviceAndSwapChain pD3D11CreateDeviceAndSwapChain =
24-
(FuncPtrD3D11CreateDeviceAndSwapChain)GetProcAddress(hD3D11, "D3D11CreateDeviceAndSwapChain");
24+
auto pD3D11CreateDeviceAndSwapChain =
25+
(FuncPtrD3D11CreateDeviceAndSwapChain)hD3D11->getProcAddress("D3D11CreateDeviceAndSwapChain");
2526

2627
if (!pD3D11CreateDeviceAndSwapChain)
2728
{
28-
Msg("* DX11: failed to get address of D3D11CreateDeviceAndSwapChain");
29+
Log("* DX11: failed to get address of D3D11CreateDeviceAndSwapChain");
2930
return false;
3031
}
3132

@@ -38,7 +39,7 @@ bool TestDX11Present()
3839
wcex.lpszClassName = "TestDX11WindowClass";
3940
if (!RegisterClassEx(&wcex))
4041
{
41-
Msg("* DX11: failed to register window class");
42+
Log("* DX11: failed to register window class");
4243
return false;
4344
}
4445

@@ -89,8 +90,6 @@ bool TestDX11Present()
8990
if (pd3dDevice)
9091
pd3dDevice->Release();
9192

92-
FreeLibrary(hD3D11);
93-
9493
DestroyWindow(hWnd);
9594

9695
return SUCCEEDED(hr);

src/utils/xrAI/xrAI.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "utils/xrLCUtil/LevelCompilerLoggerWindow.hpp"
3030
#include "xrCore/cdecl_cast.hpp"
31+
#include "xrCore/ModuleLookup.hpp"
3132

3233
LevelCompilerLoggerWindow& Logger = LevelCompilerLoggerWindow();
3334

@@ -198,23 +199,24 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
198199
{
199200
xrDebug::Initialize(false);
200201
Core.Initialize("xrai", 0);
201-
HMODULE hFactory;
202-
LPCSTR g_name = "xrSE_Factory";
203-
Log("Loading DLL:", g_name);
204-
hFactory = LoadLibrary(g_name);
205-
if (0 == hFactory)
202+
203+
204+
constexpr pcstr g_name = "xrSE_Factory";
205+
Log("Loading DLL:", g_name);
206+
const auto hFactory = std::make_unique<XRay::Module>(g_name);
207+
208+
if (!hFactory->exist())
206209
R_CHK(GetLastError());
207-
R_ASSERT2(hFactory, "Factory DLL raised exception during loading or there is no factory DLL at all");
210+
R_ASSERT2(hFactory->exist(), "Factory DLL raised exception during loading or there is no factory DLL at all");
208211

209-
create_entity = (Factory_Create*)GetProcAddress(hFactory, "_create_entity@4");
212+
create_entity = (Factory_Create*)hFactory->getProcAddress("_create_entity@4");
210213
R_ASSERT(create_entity);
211-
destroy_entity = (Factory_Destroy*)GetProcAddress(hFactory, "_destroy_entity@4");
214+
215+
destroy_entity = (Factory_Destroy*)hFactory->getProcAddress("_destroy_entity@4");
212216
R_ASSERT(destroy_entity);
213217

214218
Startup(lpCmdLine);
215219

216-
FreeLibrary(hFactory);
217-
218220
Core._destroy();
219221

220222
return (0);

src/utils/xrLC/xrLC.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Common/FSMacros.hpp"
77
#include "utils/xrLC_Light/xrLC_GlobalData.h"
88
#include "utils/xrLCUtil/LevelCompilerLoggerWindow.hpp"
9+
#include "xrCore/ModuleLookup.hpp"
910

1011
#pragma comment(lib, "comctl32.lib")
1112
#pragma comment(lib, "d3dx9.lib")
@@ -116,12 +117,12 @@ void Startup(LPSTR lpCmdLine)
116117
if (bModifyOptions)
117118
{
118119
Logger.Phase("Project options...");
119-
HMODULE L = LoadLibrary("xrLC_Options");
120-
void* P = GetProcAddress(L, "_frmScenePropertiesRun");
121-
R_ASSERT(P);
122-
xrOptions* O = (xrOptions*)P;
123-
int R = O(&Params, version, false);
124-
FreeLibrary(L);
120+
const auto L = std::make_unique<XRay::Module>("xrLC_Options");
121+
122+
const auto O = (xrOptions*)L->getProcAddress("_frmScenePropertiesRun");
123+
R_ASSERT(O);
124+
125+
const int R = O(&Params, version, false);
125126
if (R == 2)
126127
{
127128
ExitProcess(0);

src/utils/xrSE_Factory/properties_list_helper_script.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "xrScriptEngine/script_engine.hpp"
1414
#include "script_token_list.h"
1515
#include "xrScriptEngine/ScriptExporter.hpp"
16+
#include "xrCore/ModuleLookup.hpp"
1617

1718
using namespace luabind;
1819

@@ -23,19 +24,20 @@ struct CChooseType
2324
typedef IPropHelper&(__stdcall* TPHelper)();
2425

2526
TPHelper _PHelper = nullptr;
26-
HMODULE prop_helper_module = nullptr;
27-
LPCSTR prop_helper_library = "xrEPropsB", prop_helper_func = "PHelper";
27+
std::unique_ptr<XRay::Module> prop_helper_module;
28+
constexpr pcstr prop_helper_library = "xrEPropsB", prop_helper_func = "PHelper";
2829
CScriptPropertiesListHelper* g_property_list_helper = nullptr;
2930

3031
void load_prop_helper()
3132
{
32-
prop_helper_module = LoadLibrary(prop_helper_library);
33-
if (!prop_helper_module)
33+
prop_helper_module = std::make_unique<XRay::Module>(prop_helper_library);
34+
if (!prop_helper_module->exist())
3435
{
3536
Msg("! Cannot find library %s", prop_helper_library);
3637
return;
3738
}
38-
_PHelper = (TPHelper)GetProcAddress(prop_helper_module, prop_helper_func);
39+
40+
_PHelper = (TPHelper)prop_helper_module->getProcAddress(prop_helper_func);
3941
if (!_PHelper)
4042
{
4143
Msg("! Cannot find entry point of the function %s in the library %s", prop_helper_func, prop_helper_func);

src/xrEngine/Device_Initialize.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66
#endif
77
#include "GameFont.h"
88
#include "PerformanceAlert.hpp"
9-
109
#include "xrCore/ModuleLookup.hpp"
1110

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

1413
#ifdef INGAME_EDITOR
1514
void CRenderDevice::initialize_editor()
1615
{
17-
m_editor_module = XRay::LoadLibrary("xrWeatherEditor");
18-
if (!m_editor_module)
16+
m_editor_module = std::make_unique<XRay::Module>("xrWeatherEditor");
17+
if (!m_editor_module->exist())
1918
{
2019
Msg("! cannot load library \"xrWeatherEditor\"");
2120
return;
2221
}
23-
m_editor_initialize = (initialize_function_ptr)XRay::GetProcAddress(m_editor_module, "initialize");
22+
23+
m_editor_initialize = (initialize_function_ptr)m_editor_module->getProcAddress("initialize");
2424
VERIFY(m_editor_initialize);
25-
m_editor_finalize = (finalize_function_ptr)XRay::GetProcAddress(m_editor_module, "finalize");
25+
26+
m_editor_finalize = (finalize_function_ptr)m_editor_module->getProcAddress("finalize");
2627
VERIFY(m_editor_finalize);
28+
2729
m_engine = new engine_impl();
2830
m_editor_initialize(m_editor, m_engine);
2931
VERIFY(m_editor);
32+
3033
m_hWnd = m_editor->view_handle();
3134
VERIFY(m_hWnd != INVALID_HANDLE_VALUE);
3235
}

0 commit comments

Comments
 (0)