Skip to content

Commit

Permalink
Simplify module handle usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jan 28, 2018
1 parent 7502e1d commit c099b90
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Layers/xrRender/HW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void CHW::CreateD3D()
{
const pcstr _name = GEnv.isDedicatedServer ? "xrD3D9-Null" : "d3d9.dll";

hD3D = std::make_unique<XRay::Module>(_name);
hD3D = XRay::LoadModule(_name);

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);
Expand Down
2 changes: 1 addition & 1 deletion src/Layers/xrRender/HW.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class CHW
D3D_FEATURE_LEVEL FeatureLevel;
#else
private:
std::unique_ptr<XRay::Module> hD3D;
XRay::Module hD3D;

public:
IDirect3D9* pD3D; // D3D
Expand Down
2 changes: 1 addition & 1 deletion src/Layers/xrRenderPC_R4/r2_test_hw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef HRESULT(__stdcall* FuncPtrD3D11CreateDeviceAndSwapChain)(IDXGIAdapter* p

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

if (!hD3D11->exist())
{
Expand Down
2 changes: 1 addition & 1 deletion src/utils/xrAI/xrAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void execute(LPSTR cmd)
char* no_separator_check = strstr(cmd, "-no_separator_check");
clear_temp_folder();

const auto hFactory = std::make_unique<XRay::Module>("xrSE_Factory");
const auto hFactory = XRay::LoadModule("xrSE_Factory");

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

Expand Down
2 changes: 1 addition & 1 deletion src/utils/xrLC/xrLC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void Startup(LPSTR lpCmdLine)
if (bModifyOptions)
{
Logger.Phase("Project options...");
const auto L = std::make_unique<XRay::Module>("xrLC_Options");
const auto L = XRay::LoadModule("xrLC_Options");

const auto O = (xrOptions*)L->getProcAddress("_frmScenePropertiesRun");
R_ASSERT(O);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/xrSE_Factory/properties_list_helper_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ struct CChooseType
{
};

typedef IPropHelper&(__stdcall* TPHelper)();
using TPHelper = IPropHelper&(__stdcall* )();

TPHelper _PHelper = nullptr;
std::unique_ptr<XRay::Module> prop_helper_module;
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 = std::make_unique<XRay::Module>(prop_helper_library);
prop_helper_module = XRay::LoadModule(prop_helper_library);
if (!prop_helper_module->exist())
{
Msg("! Cannot find library %s", prop_helper_library);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/xrSE_Factory/xrSE_Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
extern CSE_Abstract* F_entity_Create(LPCSTR section);

extern CScriptPropertiesListHelper* g_property_list_helper;
extern std::unique_ptr<XRay::Module> prop_helper_module;
extern XRay::Module prop_helper_module;

extern "C" {
FACTORY_API IServerEntity* __stdcall create_entity(LPCSTR section) { return F_entity_Create(section); }
Expand Down
18 changes: 9 additions & 9 deletions src/xrCore/ModuleLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace XRay
{
Module::Module(const bool dontUnload) : handle(nullptr), dontUnload(dontUnload) {}
ModuleHandle::ModuleHandle(const bool dontUnload) : handle(nullptr), dontUnload(dontUnload) {}

Module::Module(pcstr moduleName, bool dontUnload /*= false*/) : handle(nullptr), dontUnload(dontUnload)
ModuleHandle::ModuleHandle(pcstr moduleName, bool dontUnload /*= false*/) : handle(nullptr), dontUnload(dontUnload)
{
open(moduleName);
}

Module::~Module()
ModuleHandle::~ModuleHandle()
{
close();
}

void* Module::open(pcstr moduleName)
void* ModuleHandle::open(pcstr moduleName)
{
if (exist())
close();
Expand All @@ -31,7 +31,7 @@ void* Module::open(pcstr moduleName)
return handle;
}

void Module::close()
void ModuleHandle::close()
{
if (dontUnload)
return;
Expand All @@ -40,18 +40,18 @@ void Module::close()
handle = nullptr;
}

bool Module::exist() const
bool ModuleHandle::exist() const
{
return handle != nullptr;
}

void* Module::operator()() const
void* ModuleHandle::operator()() const
{
return handle;
}

void* Module::getProcAddress(pcstr procName) const
void* ModuleHandle::getProcAddress(pcstr procName) const
{
return ::GetProcAddress(static_cast<HMODULE>(handle), procName);
return GetProcAddress(static_cast<HMODULE>(handle), procName);
}
}
21 changes: 17 additions & 4 deletions src/xrCore/ModuleLookup.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once
#include <memory>

namespace XRay
{
class XRCORE_API Module
class XRCORE_API ModuleHandle
{
void* handle;
bool dontUnload;

public:
Module(const bool dontUnload = false);
Module(pcstr moduleName, bool dontUnload = false);
~Module();
ModuleHandle(const bool dontUnload = false);
ModuleHandle(pcstr moduleName, bool dontUnload = false);
~ModuleHandle();

void* open(pcstr moduleName);
void close();
Expand All @@ -21,4 +22,16 @@ class XRCORE_API Module

void* getProcAddress(pcstr procName) const;
};

using Module = std::unique_ptr<ModuleHandle>;

inline auto LoadModule(bool dontUnload = false)
{
return std::make_unique<ModuleHandle>(dontUnload);
}

inline auto LoadModule(pcstr moduleName, bool dontUnload = false)
{
return std::make_unique<ModuleHandle>(moduleName, dontUnload);
}
}
2 changes: 1 addition & 1 deletion src/xrEngine/Device_Initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar

void CRenderDevice::initialize_editor()
{
m_editor_module = std::make_unique<XRay::Module>("xrWeatherEditor");
m_editor_module = XRay::LoadModule("xrWeatherEditor");
if (!m_editor_module->exist())
return;

Expand Down
12 changes: 6 additions & 6 deletions src/xrEngine/EngineAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void CEngineAPI::Initialize(void)
{
InitializeRenderers();

hGame = std::make_unique<XRay::Module>("xrGame");
hGame = XRay::LoadModule("xrGame");
R_ASSERT2(hGame, "Game DLL raised exception during loading or there is no game DLL at all");

pCreate = (Factory_Create*)hGame->getProcAddress("xrFactory_Create");
Expand All @@ -156,7 +156,7 @@ void CEngineAPI::Initialize(void)
tune_enabled = false;
if (strstr(Core.Params, "-tune"))
{
hTuner = std::make_unique<XRay::Module>("vTuneAPI");
hTuner = XRay::LoadModule("vTuneAPI");
tune_pause = (VTPause*)hTuner->getProcAddress("VTPause");
tune_resume = (VTResume*)hTuner->getProcAddress("VTResume");

Expand Down Expand Up @@ -188,7 +188,7 @@ void CEngineAPI::Destroy(void)

void CEngineAPI::CreateRendererList()
{
hRenderR1 = std::make_unique<XRay::Module>("xrRender_R1");
hRenderR1 = XRay::LoadModule("xrRender_R1");

xr_vector<xr_token> modes;
if (GEnv.isDedicatedServer)
Expand All @@ -206,9 +206,9 @@ void CEngineAPI::CreateRendererList()
// Hide "d3d10.dll not found" message box for XP
SetErrorMode(SEM_FAILCRITICALERRORS);

hRenderR2 = std::make_unique<XRay::Module>("xrRender_R2");
hRenderR3 = std::make_unique<XRay::Module>("xrRender_R3");
hRenderR4 = std::make_unique<XRay::Module>("xrRender_R4");
hRenderR2 = XRay::LoadModule("xrRender_R2");
hRenderR3 = XRay::LoadModule("xrRender_R3");
hRenderR4 = XRay::LoadModule("xrRender_R4");

// Restore error handling
SetErrorMode(0);
Expand Down
20 changes: 10 additions & 10 deletions src/xrEngine/EngineAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ class ENGINE_API FactoryObjectBase : public virtual IFactoryObject

// Class creation/destroying interface
extern "C" {
typedef DLL_API IFactoryObject* __cdecl Factory_Create(CLASS_ID CLS_ID);
typedef DLL_API void __cdecl Factory_Destroy(IFactoryObject* O);
using Factory_Create = DLL_API IFactoryObject* __cdecl(CLASS_ID CLS_ID);
using Factory_Destroy = DLL_API void __cdecl(IFactoryObject* O);
};

// Tuning interface
extern "C" {
typedef void __cdecl VTPause(void);
typedef void __cdecl VTResume(void);
using VTPause = void __cdecl(void);
using VTResume = void __cdecl(void);
};

class ENGINE_API CEngineAPI
{
std::unique_ptr<XRay::Module> hGame;
std::unique_ptr<XRay::Module> hTuner;
std::unique_ptr<XRay::Module> hRenderR1;
std::unique_ptr<XRay::Module> hRenderR2;
std::unique_ptr<XRay::Module> hRenderR3;
std::unique_ptr<XRay::Module> hRenderR4;
XRay::Module hGame;
XRay::Module hTuner;
XRay::Module hRenderR1;
XRay::Module hRenderR2;
XRay::Module hRenderR3;
XRay::Module hRenderR4;

public:
BENCH_SEC_SCRAMBLEMEMBER1
Expand Down
2 changes: 1 addition & 1 deletion src/xrEngine/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class ENGINE_API CRenderDevice : public CRenderDeviceBase
using initialize_function_ptr = XRay::Editor::initialize_function_ptr;
using finalize_function_ptr = XRay::Editor::finalize_function_ptr;

std::unique_ptr<XRay::Module> m_editor_module;
XRay::Module m_editor_module;
initialize_function_ptr m_editor_initialize;
finalize_function_ptr m_editor_finalize;
XRay::Editor::ide_base* m_editor;
Expand Down
2 changes: 1 addition & 1 deletion src/xrScriptEngine/script_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ void CScriptEngine::initialize_lua_studio(lua_State* state, cs::lua_studio::worl
world = 0;
u32 const old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS);

const auto s_script_debugger_module = std::make_unique<XRay::Module>(CS_LUA_STUDIO_BACKEND_FILE_NAME);
const auto s_script_debugger_module = XRay::LoadModule(CS_LUA_STUDIO_BACKEND_FILE_NAME);
SetErrorMode(old_error_mode);
if (!s_script_debugger_module->exist())
{
Expand Down

0 comments on commit c099b90

Please sign in to comment.