Skip to content

Commit

Permalink
Refactor: dynamic library link via XRay::Module
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Nov 11, 2017
1 parent ea56b09 commit b172ce0
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 133 deletions.
20 changes: 5 additions & 15 deletions src/Layers/xrRender/HW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,13 @@ void CHW::Reset(HWND hwnd)

void CHW::CreateD3D()
{
//#ifndef DEDICATED_SERVER
// LPCSTR _name = "d3d9.dll";
//#else
// LPCSTR _name = "xrD3D9-Null";
//#endif

LPCSTR _name = "xrD3D9-Null";
const pcstr _name = GEnv.isDedicatedServer ? "xrD3D9-Null" : "d3d9.dll";

#ifndef _EDITOR
if (!GEnv.isDedicatedServer)
#endif
_name = "d3d9.dll";
hD3D = std::make_unique<XRay::Module>(_name);

hD3D = LoadLibrary(_name);
R_ASSERT2(hD3D, "Can't find 'd3d9.dll'\nPlease install latest version of DirectX before running this program");
R_ASSERT2(hD3D->exist(), "Can't find 'd3d9.dll'\nPlease install latest version of DirectX before running this program");
typedef IDirect3D9* WINAPI _Direct3DCreate9(UINT SDKVersion);
_Direct3DCreate9* createD3D = (_Direct3DCreate9*)GetProcAddress(hD3D, "Direct3DCreate9");
auto createD3D = (_Direct3DCreate9*)hD3D->getProcAddress("Direct3DCreate9");
R_ASSERT(createD3D);
this->pD3D = createD3D(D3D_SDK_VERSION);
R_ASSERT2(this->pD3D, "Please install DirectX 9.0c");
Expand All @@ -114,7 +104,7 @@ void CHW::CreateD3D()
void CHW::DestroyD3D()
{
_RELEASE(this->pD3D);
FreeLibrary(hD3D);
//hD3D->close();
}

//////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion src/Layers/xrRender/HW.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include "HWCaps.h"
#include "xrCore/ModuleLookup.hpp"

#ifndef _MAYA_EXPORT
#include "stats_manager.h"
Expand Down Expand Up @@ -88,7 +89,7 @@ class CHW
D3D_FEATURE_LEVEL FeatureLevel;
#else
private:
HINSTANCE hD3D;
std::unique_ptr<XRay::Module> hD3D;

public:
IDirect3D9* pD3D; // D3D
Expand Down
17 changes: 8 additions & 9 deletions src/Layers/xrRenderPC_R4/r2_test_hw.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "stdafx.h"
#include "xrCore/ModuleLookup.hpp"

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

bool TestDX11Present()
{
HMODULE hD3D11 = LoadLibrary("d3d11.dll");
const auto hD3D11 = std::make_unique<XRay::Module>("d3d11.dll");

if (!hD3D11)
if (!hD3D11->exist())
{
Msg("* DX11: failed to load d3d11.dll");
Log("* DX11: failed to load d3d11.dll");
return false;
}

FuncPtrD3D11CreateDeviceAndSwapChain pD3D11CreateDeviceAndSwapChain =
(FuncPtrD3D11CreateDeviceAndSwapChain)GetProcAddress(hD3D11, "D3D11CreateDeviceAndSwapChain");
auto pD3D11CreateDeviceAndSwapChain =
(FuncPtrD3D11CreateDeviceAndSwapChain)hD3D11->getProcAddress("D3D11CreateDeviceAndSwapChain");

if (!pD3D11CreateDeviceAndSwapChain)
{
Msg("* DX11: failed to get address of D3D11CreateDeviceAndSwapChain");
Log("* DX11: failed to get address of D3D11CreateDeviceAndSwapChain");
return false;
}

Expand All @@ -38,7 +39,7 @@ bool TestDX11Present()
wcex.lpszClassName = "TestDX11WindowClass";
if (!RegisterClassEx(&wcex))
{
Msg("* DX11: failed to register window class");
Log("* DX11: failed to register window class");
return false;
}

Expand Down Expand Up @@ -89,8 +90,6 @@ bool TestDX11Present()
if (pd3dDevice)
pd3dDevice->Release();

FreeLibrary(hD3D11);

DestroyWindow(hWnd);

return SUCCEEDED(hr);
Expand Down
22 changes: 12 additions & 10 deletions src/utils/xrAI/xrAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "utils/xrLCUtil/LevelCompilerLoggerWindow.hpp"
#include "xrCore/cdecl_cast.hpp"
#include "xrCore/ModuleLookup.hpp"

LevelCompilerLoggerWindow& Logger = LevelCompilerLoggerWindow();

Expand Down Expand Up @@ -198,23 +199,24 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
{
xrDebug::Initialize(false);
Core.Initialize("xrai", 0);
HMODULE hFactory;
LPCSTR g_name = "xrSE_Factory";
Log("Loading DLL:", g_name);
hFactory = LoadLibrary(g_name);
if (0 == hFactory)


constexpr pcstr g_name = "xrSE_Factory";
Log("Loading DLL:", g_name);
const auto hFactory = std::make_unique<XRay::Module>(g_name);

if (!hFactory->exist())
R_CHK(GetLastError());
R_ASSERT2(hFactory, "Factory DLL raised exception during loading or there is no factory DLL at all");
R_ASSERT2(hFactory->exist(), "Factory DLL raised exception during loading or there is no factory DLL at all");

create_entity = (Factory_Create*)GetProcAddress(hFactory, "_create_entity@4");
create_entity = (Factory_Create*)hFactory->getProcAddress("_create_entity@4");
R_ASSERT(create_entity);
destroy_entity = (Factory_Destroy*)GetProcAddress(hFactory, "_destroy_entity@4");

destroy_entity = (Factory_Destroy*)hFactory->getProcAddress("_destroy_entity@4");
R_ASSERT(destroy_entity);

Startup(lpCmdLine);

FreeLibrary(hFactory);

Core._destroy();

return (0);
Expand Down
13 changes: 7 additions & 6 deletions src/utils/xrLC/xrLC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Common/FSMacros.hpp"
#include "utils/xrLC_Light/xrLC_GlobalData.h"
#include "utils/xrLCUtil/LevelCompilerLoggerWindow.hpp"
#include "xrCore/ModuleLookup.hpp"

#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "d3dx9.lib")
Expand Down Expand Up @@ -116,12 +117,12 @@ void Startup(LPSTR lpCmdLine)
if (bModifyOptions)
{
Logger.Phase("Project options...");
HMODULE L = LoadLibrary("xrLC_Options");
void* P = GetProcAddress(L, "_frmScenePropertiesRun");
R_ASSERT(P);
xrOptions* O = (xrOptions*)P;
int R = O(&Params, version, false);
FreeLibrary(L);
const auto L = std::make_unique<XRay::Module>("xrLC_Options");

const auto O = (xrOptions*)L->getProcAddress("_frmScenePropertiesRun");
R_ASSERT(O);

const int R = O(&Params, version, false);
if (R == 2)
{
ExitProcess(0);
Expand Down
12 changes: 7 additions & 5 deletions src/utils/xrSE_Factory/properties_list_helper_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "xrScriptEngine/script_engine.hpp"
#include "script_token_list.h"
#include "xrScriptEngine/ScriptExporter.hpp"
#include "xrCore/ModuleLookup.hpp"

using namespace luabind;

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

TPHelper _PHelper = nullptr;
HMODULE prop_helper_module = nullptr;
LPCSTR prop_helper_library = "xrEPropsB", prop_helper_func = "PHelper";
std::unique_ptr<XRay::Module> prop_helper_module;
constexpr pcstr prop_helper_library = "xrEPropsB", prop_helper_func = "PHelper";
CScriptPropertiesListHelper* g_property_list_helper = nullptr;

void load_prop_helper()
{
prop_helper_module = LoadLibrary(prop_helper_library);
if (!prop_helper_module)
prop_helper_module = std::make_unique<XRay::Module>(prop_helper_library);
if (!prop_helper_module->exist())
{
Msg("! Cannot find library %s", prop_helper_library);
return;
}
_PHelper = (TPHelper)GetProcAddress(prop_helper_module, prop_helper_func);

_PHelper = (TPHelper)prop_helper_module->getProcAddress(prop_helper_func);
if (!_PHelper)
{
Msg("! Cannot find entry point of the function %s in the library %s", prop_helper_func, prop_helper_func);
Expand Down
13 changes: 8 additions & 5 deletions src/xrEngine/Device_Initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
#endif
#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 = XRay::LoadLibrary("xrWeatherEditor");
if (!m_editor_module)
m_editor_module = std::make_unique<XRay::Module>("xrWeatherEditor");
if (!m_editor_module->exist())
{
Msg("! cannot load library \"xrWeatherEditor\"");
return;
}
m_editor_initialize = (initialize_function_ptr)XRay::GetProcAddress(m_editor_module, "initialize");

m_editor_initialize = (initialize_function_ptr)m_editor_module->getProcAddress("initialize");
VERIFY(m_editor_initialize);
m_editor_finalize = (finalize_function_ptr)XRay::GetProcAddress(m_editor_module, "finalize");

m_editor_finalize = (finalize_function_ptr)m_editor_module->getProcAddress("finalize");
VERIFY(m_editor_finalize);

m_engine = new engine_impl();
m_editor_initialize(m_editor, m_engine);
VERIFY(m_editor);

m_hWnd = m_editor->view_handle();
VERIFY(m_hWnd != INVALID_HANDLE_VALUE);
}
Expand Down
Loading

0 comments on commit b172ce0

Please sign in to comment.