Skip to content

Commit 5b2ec76

Browse files
committed
Link xrGame to xr_3da directly
1 parent 655e7ab commit 5b2ec76

14 files changed

+103
-80
lines changed

src/xrEngine/Engine.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void CheckAndSetupRenderer()
6161

6262
extern void msCreate(pcstr name);
6363

64-
void CEngine::Initialize(void)
64+
void CEngine::Initialize(GameModule* game)
6565
{
6666
ZoneScoped;
6767
#ifdef DEBUG
@@ -77,7 +77,7 @@ void CEngine::Initialize(void)
7777

7878
CheckAndSetupRenderer();
7979

80-
External.Initialize();
80+
External.Initialize(game);
8181
Sheduler.Initialize();
8282
}
8383

src/xrEngine/Engine.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ENGINE_API CEngine final : public pureFrame, public IEventReceiver
3333
CSheduler Sheduler;
3434
CSoundManager Sound;
3535

36-
void Initialize();
36+
void Initialize(GameModule* game);
3737
void Destroy();
3838

3939
void OnEvent(EVENT E, u64 P1, u64 P2) override;

src/xrEngine/EngineAPI.cpp

+31-44
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,37 @@
44
//////////////////////////////////////////////////////////////////////
55

66
#include "stdafx.h"
7+
78
#include "EngineAPI.h"
89
#include "XR_IOConsole.h"
910

10-
#include "xrCore/ModuleLookup.hpp"
1111
#include "xrCore/xr_token.h"
12+
#include "xrCore/ModuleLookup.hpp"
1213
#include "xrCore/Threading/ParallelForEach.hpp"
1314

1415
#include "xrScriptEngine/ScriptExporter.hpp"
1516

17+
#include <array>
18+
1619
extern xr_vector<xr_token> VidQualityToken;
1720

1821
constexpr pcstr GET_RENDERER_MODULE_FUNC = "GetRendererModule";
1922

20-
constexpr pcstr r4_library = "xrRender_R4";
21-
constexpr pcstr gl_library = "xrRender_GL";
23+
using GetRendererModule = RendererModule*();
2224

23-
constexpr pcstr RENDER_LIBRARIES[] =
25+
struct RendererDesc
2426
{
25-
r4_library,
26-
gl_library
27+
pcstr libraryName;
28+
XRay::Module handle;
29+
RendererModule* module;
2730
};
2831

32+
std::array<RendererDesc, 2> g_render_modules =
33+
{{
34+
{ "xrRender_R4", nullptr, nullptr },
35+
{ "xrRender_GL", nullptr, nullptr },
36+
}};
37+
2938
//////////////////////////////////////////////////////////////////////
3039
// Construction/Destruction
3140
//////////////////////////////////////////////////////////////////////
@@ -99,56 +108,40 @@ void CEngineAPI::SelectRenderer()
99108
Log("Selected renderer:", selected_mode);
100109
}
101110

102-
void CEngineAPI::Initialize(void)
111+
void CEngineAPI::Initialize(GameModule* game)
103112
{
104113
ZoneScoped;
105114

106115
SelectRenderer();
107116

108-
hGame = XRay::LoadModule("xrGame");
109117
if (!CanSkipGameModuleLoading())
110118
{
111-
R_ASSERT2(hGame->IsLoaded(), "! Game DLL raised exception during loading or there is no game DLL at all");
112-
113-
pCreate = (Factory_Create*)hGame->GetProcAddress("xrFactory_Create");
119+
gameModule = game;
120+
gameModule->initialize(pCreate, pDestroy);
114121
R_ASSERT(pCreate);
115-
116-
pDestroy = (Factory_Destroy*)hGame->GetProcAddress("xrFactory_Destroy");
117122
R_ASSERT(pDestroy);
118-
119-
pInitializeGame = (InitializeGameLibraryProc)hGame->GetProcAddress("initialize_library");
120-
R_ASSERT(pInitializeGame);
121-
122-
pFinalizeGame = (FinalizeGameLibraryProc)hGame->GetProcAddress("finalize_library");
123-
R_ASSERT(pFinalizeGame);
124-
125-
pInitializeGame();
126123
}
127124

128125
CloseUnusedLibraries();
129126
}
130127

131-
void CEngineAPI::Destroy(void)
128+
void CEngineAPI::Destroy()
132129
{
133130
ZoneScoped;
134-
if (pFinalizeGame)
135-
pFinalizeGame();
136131

137-
pInitializeGame = nullptr;
138-
pFinalizeGame = nullptr;
132+
if (gameModule)
133+
gameModule->finalize();
134+
139135
pCreate = nullptr;
140136
pDestroy = nullptr;
141137

142-
hGame = nullptr;
143-
144-
renderers.clear();
145138
XRC.r_clear_compact();
146139
}
147140

148141
void CEngineAPI::CloseUnusedLibraries()
149142
{
150143
ZoneScoped;
151-
for (RendererDesc& desc : renderers)
144+
for (RendererDesc& desc : g_render_modules)
152145
{
153146
if (desc.module != selectedRenderer)
154147
desc.handle = nullptr;
@@ -162,35 +155,29 @@ void CEngineAPI::CreateRendererList()
162155

163156
ZoneScoped;
164157

165-
const auto loadLibrary = [&](pcstr library) -> bool
158+
const auto loadLibrary = [&](RendererDesc& desc) -> bool
166159
{
167-
auto handle = XRay::LoadModule(library);
160+
auto handle = XRay::LoadModule(desc.libraryName);
168161
if (!handle->IsLoaded())
169162
return false;
170163

171-
const auto getModule = (GetRendererModule)handle->GetProcAddress(GET_RENDERER_MODULE_FUNC);
164+
const auto getModule = reinterpret_cast<GetRendererModule*>(handle->GetProcAddress(GET_RENDERER_MODULE_FUNC));
172165
RendererModule* module = getModule ? getModule() : nullptr;
173166
if (!module)
174167
return false;
175168

176-
renderers.emplace_back(RendererDesc({ library, std::move(handle), module }));
169+
desc.handle = std::move(handle);
170+
desc.module = module;
177171
return true;
178172
};
179173

180174
if (GEnv.isDedicatedServer)
181175
{
182-
#if defined(XR_PLATFORM_WINDOWS)
183-
R_ASSERT2(loadLibrary(r4_library), "Dedicated server needs xrRender_R1 to work");
184-
#else
185-
R_ASSERT2(loadLibrary(gl_library), "Dedicated server needs xrRender_GL to work");
186-
#endif
176+
R_ASSERT2(loadLibrary(g_render_modules[0]), "Dedicated server needs xrRender to work");
187177
}
188178
else
189179
{
190-
for (cpcstr library : RENDER_LIBRARIES)
191-
{
192-
loadLibrary(library);
193-
}
180+
std::for_each(std::begin(g_render_modules), std::end(g_render_modules), loadLibrary);
194181
}
195182

196183
std::mutex mutex;
@@ -221,7 +208,7 @@ void CEngineAPI::CreateRendererList()
221208
}
222209
};
223210

224-
xr_parallel_for_each(renderers, obtainModes);
211+
xr_parallel_for_each(g_render_modules, obtainModes);
225212

226213
auto& modes = VidQualityToken;
227214
Msg("Available render modes[%d]:", modes.size());

src/xrEngine/EngineAPI.h

+16-25
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ using Factory_Create = IFactoryObject* __cdecl(CLASS_ID CLS_ID);
3939
using Factory_Destroy = void __cdecl(IFactoryObject* O);
4040
}
4141

42+
class XR_NOVTABLE GameModule
43+
{
44+
public:
45+
virtual ~GameModule() = default;
46+
virtual void initialize(Factory_Create*& pCreate, Factory_Destroy*& pDestroy) = 0;
47+
virtual void finalize() = 0;
48+
};
49+
4250
class XR_NOVTABLE RendererModule
4351
{
4452
public:
@@ -50,44 +58,27 @@ class XR_NOVTABLE RendererModule
5058

5159
class ENGINE_API CEngineAPI
5260
{
53-
using InitializeGameLibraryProc = void(*)();
54-
using FinalizeGameLibraryProc = void(*)();
55-
56-
using GetRendererModule = RendererModule*(*)();
57-
58-
struct RendererDesc
59-
{
60-
pcstr libraryName;
61-
XRay::Module handle;
62-
RendererModule* module;
63-
};
64-
65-
xr_vector<RendererDesc> renderers;
6661
xr_map<shared_str, RendererModule*> renderModes;
6762

63+
GameModule* gameModule;
6864
RendererModule* selectedRenderer{};
6965

70-
XRay::Module hGame;
71-
72-
InitializeGameLibraryProc pInitializeGame{};
73-
FinalizeGameLibraryProc pFinalizeGame{};
66+
void SelectRenderer();
67+
void CloseUnusedLibraries();
7468

7569
public:
7670
Factory_Create* pCreate;
7771
Factory_Destroy* pDestroy;
7872

79-
void Initialize();
80-
81-
void SelectRenderer();
82-
void CloseUnusedLibraries();
73+
public:
74+
CEngineAPI();
75+
~CEngineAPI();
8376

77+
void CreateRendererList();
78+
void Initialize(GameModule* game);
8479
void Destroy();
8580

86-
void CreateRendererList();
8781
bool CanSkipGameModuleLoading() const { return !!strstr(Core.Params, "-nogame"); }
88-
89-
CEngineAPI();
90-
~CEngineAPI();
9182
};
9283

9384
ENGINE_API bool is_enough_address_space_available();

src/xrEngine/x_ray.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void execUserScript()
219219
constexpr pcstr APPLICATION_STARTUP = "Application startup";
220220
constexpr pcstr APPLICATION_SHUTDOWN = "Application shutdown";
221221

222-
CApplication::CApplication(pcstr commandLine)
222+
CApplication::CApplication(pcstr commandLine, GameModule* game)
223223
{
224224
Threading::SetCurrentThreadName("Primary thread");
225225
FrameMarkStart(APPLICATION_STARTUP);
@@ -319,7 +319,7 @@ CApplication::CApplication(pcstr commandLine)
319319
InitConsole();
320320

321321
TaskScheduler->Wait(createRendererList);
322-
Engine.Initialize();
322+
Engine.Initialize(game);
323323
Device.Initialize();
324324

325325
Console->OnDeviceInitialize();

src/xrEngine/x_ray.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <mutex>
55

6-
#include "xrCore/Threading/Event.hpp"
6+
#include "xrEngine/Engine.h"
77

88
struct SDL_Window;
99
struct SDL_Surface;
@@ -36,7 +36,7 @@ class ENGINE_API CApplication final
3636

3737
public:
3838
// Other
39-
CApplication(pcstr commandLine);
39+
CApplication(pcstr commandLine, GameModule* game);
4040
~CApplication();
4141

4242
int Run();

src/xrGame/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ target_sources(xrGame PRIVATE
16821682
xrClientsPool.cpp
16831683
xrClientsPool.h
16841684
xrGame.cpp
1685+
xrGame.h
16851686
xrgame_dll_detach.cpp
16861687
xrGameSpy_GameSpyFuncs.cpp
16871688
xrGameSpyServer_callbacks.cpp

src/xrGame/xrGame.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
////////////////////////////////////////////////////////////////////////////
88

99
#include "StdAfx.h"
10+
#include "xrGame.h"
1011

1112
#include "object_factory.h"
1213

@@ -16,6 +17,8 @@
1617
#include "xrUICore/XML/xrUIXmlParser.h"
1718
#include "xrUICore/ui_styles.h"
1819

20+
xrGameModule xrGame;
21+
1922
void CCC_RegisterCommands();
2023

2124
extern float g_fTimeFactor;
@@ -35,11 +38,15 @@ XR_EXPORT IFactoryObject* __cdecl xrFactory_Create(CLASS_ID clsid)
3538
}
3639

3740
XR_EXPORT void __cdecl xrFactory_Destroy(IFactoryObject* O) { xr_delete(O); }
41+
}
3842

39-
XR_EXPORT void initialize_library()
43+
void xrGameModule::initialize(Factory_Create*& pCreate, Factory_Destroy*& pDestroy)
4044
{
4145
ZoneScoped;
4246

47+
pCreate = &xrFactory_Create;
48+
pDestroy = &xrFactory_Destroy;
49+
4350
g_fTimeFactor = pSettings->r_float("alife", "time_factor"); // XXX: find a better place
4451

4552
// Fill ui style token
@@ -67,7 +74,7 @@ XR_EXPORT void initialize_library()
6774
ImGui::SetCurrentContext(Device.GetImGuiContext());
6875
}
6976

70-
XR_EXPORT void finalize_library()
77+
void xrGameModule::finalize()
7178
{
7279
xr_delete(UIStyles);
7380
StringTable().Destroy();
@@ -77,4 +84,3 @@ XR_EXPORT void finalize_library()
7784
xr_delete(g_profiler);
7885
#endif
7986
}
80-
}

src/xrGame/xrGame.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "xrCore/clsid.h"
4+
#include "xrEngine/EngineAPI.h"
5+
6+
#ifdef XRAY_STATIC_BUILD
7+
# define XRGAME_API
8+
#else
9+
# ifdef XRGAME_EXPORTS
10+
# define XRGAME_API XR_EXPORT
11+
# else
12+
# define XRGAME_API XR_IMPORT
13+
# endif
14+
#endif
15+
16+
extern "C"
17+
{
18+
XRGAME_API IFactoryObject* __cdecl xrFactory_Create(CLASS_ID clsid);
19+
XRGAME_API void __cdecl xrFactory_Destroy(IFactoryObject* O);
20+
}
21+
22+
class xrGameModule final : public GameModule
23+
{
24+
public:
25+
void initialize(Factory_Create*& pCreate, Factory_Destroy*& pDestroy) override;
26+
void finalize() override;
27+
};
28+
29+
extern XRGAME_API xrGameModule xrGame;

src/xrGame/xrGame.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,7 @@
14431443
<ClInclude Include="wrapper_abstract.h" />
14441444
<ClInclude Include="wrapper_abstract_inline.h" />
14451445
<ClInclude Include="xrClientsPool.h" />
1446+
<ClInclude Include="xrGame.h" />
14461447
<ClInclude Include="xrGameSpyServer.h" />
14471448
<ClInclude Include="xrGameSpyServer_callbacks.h" />
14481449
<ClInclude Include="xrServer.h" />

src/xrGame/xrGame.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -6501,6 +6501,9 @@
65016501
<ClInclude Include="ui\UIMapFilters.h">
65026502
<Filter>UI\Common\PDA\Tasks</Filter>
65036503
</ClInclude>
6504+
<ClInclude Include="xrGame.h">
6505+
<Filter>Core\Server</Filter>
6506+
</ClInclude>
65046507
</ItemGroup>
65056508
<ItemGroup>
65066509
<ClCompile Include="damage_manager.cpp">

0 commit comments

Comments
 (0)