Skip to content

Commit 31556fc

Browse files
madebrrm5248
authored andcommitted
Introduce SharedObject abstraction around win32's LoadLibrary api
1 parent 3b87221 commit 31556fc

5 files changed

Lines changed: 118 additions & 22 deletions

File tree

Code/Commando/WINMAIN.CPP

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#include "ServerSettings.h"
9696
#include "openw3d.h"
9797
#include "ini.h"
98+
#include "soutil.h"
9899

99100
#ifdef _DEBUG
100101
#include <crtdbg.h>
@@ -258,9 +259,10 @@ static bool Graphics_Settings_Trouble_Shooting()
258259
return true;
259260
}
260261

261-
typedef IDirect3D9* (WINAPI *Direct3DCreate8Type) (UINT SDKVersion);
262-
static Direct3DCreate8Type Direct3DCreate8Ptr = NULL;
263-
static HMODULE D3D8Lib = NULL;
262+
typedef IDirect3D9* (WINAPI *Direct3DCreate9Type) (UINT SDKVersion);
263+
static Direct3DCreate9Type Direct3DCreate9Ptr = NULL;
264+
extern const char *Get_D3D9_Object_Name();
265+
static SharedObject *D3D9Lib = nullptr;
264266

265267
static bool Video_Card_Driver_Check()
266268
{
@@ -277,22 +279,24 @@ static bool Video_Card_Driver_Check()
277279
// Init D3D
278280
Init_D3D_To_WW3_Conversion();
279281

280-
D3D8Lib = LoadLibraryA("D3D9.DLL");
282+
D3D9Lib = SharedObject::LoadObject(Get_D3D9_Object_Name());
281283

282-
if (D3D8Lib != NULL) {
283-
Direct3DCreate8Ptr = (Direct3DCreate8Type) GetProcAddress(D3D8Lib, "Direct3DCreate9");
284-
if (Direct3DCreate8Ptr) {
285-
d3d=Direct3DCreate8Ptr(D3D_SDK_VERSION); // TODO: handle failure cases...
284+
if (D3D9Lib != NULL) {
285+
Direct3DCreate9Ptr = reinterpret_cast<Direct3DCreate9Type>(D3D9Lib->LoadFunction("Direct3DCreate9"));
286+
if (Direct3DCreate9Ptr) {
287+
d3d=Direct3DCreate9Ptr(D3D_SDK_VERSION); // TODO: handle failure cases...
286288
if (!d3d) {
287-
FreeLibrary(D3D8Lib);
289+
delete D3D9Lib;
290+
D3D9Lib = nullptr;
288291
return true;
289292
}
290293
} else {
291-
FreeLibrary(D3D8Lib);
292-
return(true);
294+
delete D3D9Lib;
295+
D3D9Lib = nullptr;
296+
return true;
293297
}
294298
} else {
295-
return(true);
299+
return true;
296300
}
297301

298302
// Select device. If there is already a device selected in the registry, use it.
@@ -325,7 +329,7 @@ static bool Video_Card_Driver_Check()
325329
D3DDEVTYPE_HAL,
326330
&tmp_caps))) {
327331
d3d->Release();
328-
FreeLibrary(D3D8Lib);
332+
delete D3D9Lib;
329333
return true;
330334
}
331335

@@ -335,7 +339,8 @@ static bool Video_Card_Driver_Check()
335339
0/*D3DENUM_WHQL_LEVEL*/,
336340
&adapter_id))) {
337341
d3d->Release();
338-
FreeLibrary(D3D8Lib);
342+
delete D3D9Lib;
343+
D3D9Lib = nullptr;
339344
return true;
340345
}
341346

@@ -345,7 +350,8 @@ static bool Video_Card_Driver_Check()
345350
DX8Caps::DriverVersionStatusType status=caps.Get_Driver_Version_Status();
346351

347352
d3d->Release();
348-
FreeLibrary(D3D8Lib);
353+
delete D3D9Lib;
354+
D3D9Lib = nullptr;
349355

350356
switch (status) {
351357
default:

Code/ww3d2/dx8wrapper.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "bound.h"
7878
#include "ini.h"
7979
#include "openw3d.h"
80+
#include "soutil.h"
8081

8182
const int DEFAULT_RESOLUTION_WIDTH = 800;
8283
const int DEFAULT_RESOLUTION_HEIGHT = 600;
@@ -170,14 +171,28 @@ static DynamicVectorClass<RenderDeviceDescClass> _RenderDeviceDescriptionTable;
170171

171172
typedef IDirect3D9* (WINAPI *Direct3DCreate8Type) (UINT SDKVersion);
172173
Direct3DCreate8Type Direct3DCreate8Ptr = NULL;
173-
HINSTANCE D3D8Lib = NULL;
174+
SharedObject *D3D9Lib = nullptr;
174175

175176
/***********************************************************************************
176177
**
177178
** DX8Wrapper Implementation
178179
**
179180
***********************************************************************************/
180181

182+
const char *Get_D3D9_Object_Name()
183+
{
184+
// FIXME: support overriding object using config (args, ini, register, envvar)
185+
#ifdef _WIN32
186+
return "d3d9.dll";
187+
#elif defined (__ELF__)
188+
return "libdxvk_d3d9.so.0";
189+
#elif defined (__MACH__)
190+
return "libdxvk_d3d9.0.dylib";
191+
#else
192+
static_assert(false, "Unknown d3d9 name");
193+
#endif
194+
}
195+
181196
void Log_DX8_ErrorCode(HRESULT res)
182197
{
183198
const char *error_string = DXGetErrorStringA(res);
@@ -240,11 +255,11 @@ bool DX8Wrapper::Init(void * hwnd, bool lite)
240255
Invalidate_Cached_Render_States();
241256

242257
if (!lite) {
243-
D3D8Lib = LoadLibraryA("D3D9.DLL");
258+
D3D9Lib = SharedObject::LoadObject(Get_D3D9_Object_Name());
244259

245-
if (D3D8Lib == NULL) return false;
260+
if (D3D9Lib == NULL) return false;
246261

247-
Direct3DCreate8Ptr = (Direct3DCreate8Type) GetProcAddress(D3D8Lib, "Direct3DCreate9");
262+
Direct3DCreate8Ptr = reinterpret_cast<Direct3DCreate8Type>(D3D9Lib->LoadFunction("Direct3DCreate9"));
248263
if (Direct3DCreate8Ptr) {
249264

250265
/*
@@ -311,9 +326,9 @@ void DX8Wrapper::Shutdown(void)
311326
_RenderDeviceShortNameTable.Delete_All();
312327
_RenderDeviceDescriptionTable.Delete_All();
313328

314-
if (D3D8Lib) {
315-
FreeLibrary(D3D8Lib);
316-
D3D8Lib = NULL;
329+
if (D3D9Lib) {
330+
delete D3D9Lib;
331+
D3D9Lib = NULL;
317332
}
318333

319334
IsInitted = false;

Code/wwutil/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ set(WWUTIL_SRC
88
ProcessManager.h
99
pathutil.cpp
1010
pathutil.h
11+
soutil.cpp
12+
soutil.h
1113
$<$<BOOL:${WIN32}>:stackdump-windows.cpp>
1214
$<$<BOOL:${LINUX}>:stackdump-linux.cpp>
1315
stackdump.h

Code/wwutil/soutil.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "soutil.h"
2+
3+
#ifdef _WIN32
4+
#include <windows.h>
5+
#else
6+
#include <dlfcn.h>
7+
#endif
8+
9+
SharedObject *SharedObject::LoadObject(const char *path)
10+
{
11+
SoType so;
12+
#ifdef _WIN32
13+
so = LoadLibraryA(path);
14+
#else
15+
so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
16+
#endif
17+
if (so == nullptr) {
18+
return nullptr;
19+
}
20+
auto result = new SharedObject(so);
21+
if (result == nullptr) {
22+
#ifdef _WIN32
23+
FreeLibrary(so);
24+
#else
25+
dlclose(so);
26+
#endif
27+
}
28+
return result;
29+
}
30+
31+
SharedObject::~SharedObject()
32+
{
33+
#ifdef _WIN32
34+
FreeLibrary(m_so);
35+
#else
36+
dlclose(m_so);
37+
#endif
38+
}
39+
40+
SharedObject::FunctionPointer SharedObject::LoadFunction(const char *name) const
41+
{
42+
#ifdef _WIN32
43+
return reinterpret_cast<FunctionPointer>(GetProcAddress(m_so, name));
44+
#else
45+
return reinterpret_cast<FunctionPointer>(dlsym(m_so, name));
46+
#endif
47+
}

Code/wwutil/soutil.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#ifdef _WIN32
4+
#include <windows.h>
5+
#endif
6+
7+
class SharedObject {
8+
public:
9+
using FunctionPointer = void (*)();
10+
11+
~SharedObject();
12+
13+
static SharedObject *LoadObject(const char *path);
14+
15+
FunctionPointer LoadFunction(const char *name) const;
16+
protected:
17+
#ifdef _WIN32
18+
using SoType = HMODULE;
19+
#else
20+
using SoType = void *;
21+
#endif
22+
SharedObject(SoType so) : m_so(so) {}
23+
SharedObject(SharedObject &) = delete;
24+
25+
SoType m_so;
26+
};

0 commit comments

Comments
 (0)