Skip to content

Commit

Permalink
Link xrGame to xr_3da directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed May 4, 2024
1 parent 655e7ab commit 5b2ec76
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 80 deletions.
4 changes: 2 additions & 2 deletions src/xrEngine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void CheckAndSetupRenderer()

extern void msCreate(pcstr name);

void CEngine::Initialize(void)
void CEngine::Initialize(GameModule* game)
{
ZoneScoped;
#ifdef DEBUG
Expand All @@ -77,7 +77,7 @@ void CEngine::Initialize(void)

CheckAndSetupRenderer();

External.Initialize();
External.Initialize(game);
Sheduler.Initialize();
}

Expand Down
2 changes: 1 addition & 1 deletion src/xrEngine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ENGINE_API CEngine final : public pureFrame, public IEventReceiver
CSheduler Sheduler;
CSoundManager Sound;

void Initialize();
void Initialize(GameModule* game);
void Destroy();

void OnEvent(EVENT E, u64 P1, u64 P2) override;
Expand Down
75 changes: 31 additions & 44 deletions src/xrEngine/EngineAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "EngineAPI.h"
#include "XR_IOConsole.h"

#include "xrCore/ModuleLookup.hpp"
#include "xrCore/xr_token.h"
#include "xrCore/ModuleLookup.hpp"
#include "xrCore/Threading/ParallelForEach.hpp"

#include "xrScriptEngine/ScriptExporter.hpp"

#include <array>

extern xr_vector<xr_token> VidQualityToken;

constexpr pcstr GET_RENDERER_MODULE_FUNC = "GetRendererModule";

constexpr pcstr r4_library = "xrRender_R4";
constexpr pcstr gl_library = "xrRender_GL";
using GetRendererModule = RendererModule*();

constexpr pcstr RENDER_LIBRARIES[] =
struct RendererDesc
{
r4_library,
gl_library
pcstr libraryName;
XRay::Module handle;
RendererModule* module;
};

std::array<RendererDesc, 2> g_render_modules =
{{
{ "xrRender_R4", nullptr, nullptr },
{ "xrRender_GL", nullptr, nullptr },
}};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -99,56 +108,40 @@ void CEngineAPI::SelectRenderer()
Log("Selected renderer:", selected_mode);
}

void CEngineAPI::Initialize(void)
void CEngineAPI::Initialize(GameModule* game)
{
ZoneScoped;

SelectRenderer();

hGame = XRay::LoadModule("xrGame");
if (!CanSkipGameModuleLoading())
{
R_ASSERT2(hGame->IsLoaded(), "! Game DLL raised exception during loading or there is no game DLL at all");

pCreate = (Factory_Create*)hGame->GetProcAddress("xrFactory_Create");
gameModule = game;
gameModule->initialize(pCreate, pDestroy);
R_ASSERT(pCreate);

pDestroy = (Factory_Destroy*)hGame->GetProcAddress("xrFactory_Destroy");
R_ASSERT(pDestroy);

pInitializeGame = (InitializeGameLibraryProc)hGame->GetProcAddress("initialize_library");
R_ASSERT(pInitializeGame);

pFinalizeGame = (FinalizeGameLibraryProc)hGame->GetProcAddress("finalize_library");
R_ASSERT(pFinalizeGame);

pInitializeGame();
}

CloseUnusedLibraries();
}

void CEngineAPI::Destroy(void)
void CEngineAPI::Destroy()
{
ZoneScoped;
if (pFinalizeGame)
pFinalizeGame();

pInitializeGame = nullptr;
pFinalizeGame = nullptr;
if (gameModule)
gameModule->finalize();

pCreate = nullptr;
pDestroy = nullptr;

hGame = nullptr;

renderers.clear();
XRC.r_clear_compact();
}

void CEngineAPI::CloseUnusedLibraries()
{
ZoneScoped;
for (RendererDesc& desc : renderers)
for (RendererDesc& desc : g_render_modules)
{
if (desc.module != selectedRenderer)
desc.handle = nullptr;
Expand All @@ -162,35 +155,29 @@ void CEngineAPI::CreateRendererList()

ZoneScoped;

const auto loadLibrary = [&](pcstr library) -> bool
const auto loadLibrary = [&](RendererDesc& desc) -> bool
{
auto handle = XRay::LoadModule(library);
auto handle = XRay::LoadModule(desc.libraryName);
if (!handle->IsLoaded())
return false;

const auto getModule = (GetRendererModule)handle->GetProcAddress(GET_RENDERER_MODULE_FUNC);
const auto getModule = reinterpret_cast<GetRendererModule*>(handle->GetProcAddress(GET_RENDERER_MODULE_FUNC));
RendererModule* module = getModule ? getModule() : nullptr;
if (!module)
return false;

renderers.emplace_back(RendererDesc({ library, std::move(handle), module }));
desc.handle = std::move(handle);
desc.module = module;
return true;
};

if (GEnv.isDedicatedServer)
{
#if defined(XR_PLATFORM_WINDOWS)
R_ASSERT2(loadLibrary(r4_library), "Dedicated server needs xrRender_R1 to work");
#else
R_ASSERT2(loadLibrary(gl_library), "Dedicated server needs xrRender_GL to work");
#endif
R_ASSERT2(loadLibrary(g_render_modules[0]), "Dedicated server needs xrRender to work");
}
else
{
for (cpcstr library : RENDER_LIBRARIES)
{
loadLibrary(library);
}
std::for_each(std::begin(g_render_modules), std::end(g_render_modules), loadLibrary);
}

std::mutex mutex;
Expand Down Expand Up @@ -221,7 +208,7 @@ void CEngineAPI::CreateRendererList()
}
};

xr_parallel_for_each(renderers, obtainModes);
xr_parallel_for_each(g_render_modules, obtainModes);

auto& modes = VidQualityToken;
Msg("Available render modes[%d]:", modes.size());
Expand Down
41 changes: 16 additions & 25 deletions src/xrEngine/EngineAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ using Factory_Create = IFactoryObject* __cdecl(CLASS_ID CLS_ID);
using Factory_Destroy = void __cdecl(IFactoryObject* O);
}

class XR_NOVTABLE GameModule
{
public:
virtual ~GameModule() = default;
virtual void initialize(Factory_Create*& pCreate, Factory_Destroy*& pDestroy) = 0;
virtual void finalize() = 0;
};

class XR_NOVTABLE RendererModule
{
public:
Expand All @@ -50,44 +58,27 @@ class XR_NOVTABLE RendererModule

class ENGINE_API CEngineAPI
{
using InitializeGameLibraryProc = void(*)();
using FinalizeGameLibraryProc = void(*)();

using GetRendererModule = RendererModule*(*)();

struct RendererDesc
{
pcstr libraryName;
XRay::Module handle;
RendererModule* module;
};

xr_vector<RendererDesc> renderers;
xr_map<shared_str, RendererModule*> renderModes;

GameModule* gameModule;
RendererModule* selectedRenderer{};

XRay::Module hGame;

InitializeGameLibraryProc pInitializeGame{};
FinalizeGameLibraryProc pFinalizeGame{};
void SelectRenderer();
void CloseUnusedLibraries();

public:
Factory_Create* pCreate;
Factory_Destroy* pDestroy;

void Initialize();

void SelectRenderer();
void CloseUnusedLibraries();
public:
CEngineAPI();
~CEngineAPI();

void CreateRendererList();
void Initialize(GameModule* game);
void Destroy();

void CreateRendererList();
bool CanSkipGameModuleLoading() const { return !!strstr(Core.Params, "-nogame"); }

CEngineAPI();
~CEngineAPI();
};

ENGINE_API bool is_enough_address_space_available();
Expand Down
4 changes: 2 additions & 2 deletions src/xrEngine/x_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void execUserScript()
constexpr pcstr APPLICATION_STARTUP = "Application startup";
constexpr pcstr APPLICATION_SHUTDOWN = "Application shutdown";

CApplication::CApplication(pcstr commandLine)
CApplication::CApplication(pcstr commandLine, GameModule* game)
{
Threading::SetCurrentThreadName("Primary thread");
FrameMarkStart(APPLICATION_STARTUP);
Expand Down Expand Up @@ -319,7 +319,7 @@ CApplication::CApplication(pcstr commandLine)
InitConsole();

TaskScheduler->Wait(createRendererList);
Engine.Initialize();
Engine.Initialize(game);
Device.Initialize();

Console->OnDeviceInitialize();
Expand Down
4 changes: 2 additions & 2 deletions src/xrEngine/x_ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <mutex>

#include "xrCore/Threading/Event.hpp"
#include "xrEngine/Engine.h"

struct SDL_Window;
struct SDL_Surface;
Expand Down Expand Up @@ -36,7 +36,7 @@ class ENGINE_API CApplication final

public:
// Other
CApplication(pcstr commandLine);
CApplication(pcstr commandLine, GameModule* game);
~CApplication();

int Run();
Expand Down
1 change: 1 addition & 0 deletions src/xrGame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ target_sources(xrGame PRIVATE
xrClientsPool.cpp
xrClientsPool.h
xrGame.cpp
xrGame.h
xrgame_dll_detach.cpp
xrGameSpy_GameSpyFuncs.cpp
xrGameSpyServer_callbacks.cpp
Expand Down
12 changes: 9 additions & 3 deletions src/xrGame/xrGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "xrGame.h"

#include "object_factory.h"

Expand All @@ -16,6 +17,8 @@
#include "xrUICore/XML/xrUIXmlParser.h"
#include "xrUICore/ui_styles.h"

xrGameModule xrGame;

void CCC_RegisterCommands();

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

XR_EXPORT void __cdecl xrFactory_Destroy(IFactoryObject* O) { xr_delete(O); }
}

XR_EXPORT void initialize_library()
void xrGameModule::initialize(Factory_Create*& pCreate, Factory_Destroy*& pDestroy)
{
ZoneScoped;

pCreate = &xrFactory_Create;
pDestroy = &xrFactory_Destroy;

g_fTimeFactor = pSettings->r_float("alife", "time_factor"); // XXX: find a better place

// Fill ui style token
Expand Down Expand Up @@ -67,7 +74,7 @@ XR_EXPORT void initialize_library()
ImGui::SetCurrentContext(Device.GetImGuiContext());
}

XR_EXPORT void finalize_library()
void xrGameModule::finalize()
{
xr_delete(UIStyles);
StringTable().Destroy();
Expand All @@ -77,4 +84,3 @@ XR_EXPORT void finalize_library()
xr_delete(g_profiler);
#endif
}
}
29 changes: 29 additions & 0 deletions src/xrGame/xrGame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "xrCore/clsid.h"
#include "xrEngine/EngineAPI.h"

#ifdef XRAY_STATIC_BUILD
# define XRGAME_API
#else
# ifdef XRGAME_EXPORTS
# define XRGAME_API XR_EXPORT
# else
# define XRGAME_API XR_IMPORT
# endif
#endif

extern "C"
{
XRGAME_API IFactoryObject* __cdecl xrFactory_Create(CLASS_ID clsid);
XRGAME_API void __cdecl xrFactory_Destroy(IFactoryObject* O);
}

class xrGameModule final : public GameModule
{
public:
void initialize(Factory_Create*& pCreate, Factory_Destroy*& pDestroy) override;
void finalize() override;
};

extern XRGAME_API xrGameModule xrGame;
1 change: 1 addition & 0 deletions src/xrGame/xrGame.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,7 @@
<ClInclude Include="wrapper_abstract.h" />
<ClInclude Include="wrapper_abstract_inline.h" />
<ClInclude Include="xrClientsPool.h" />
<ClInclude Include="xrGame.h" />
<ClInclude Include="xrGameSpyServer.h" />
<ClInclude Include="xrGameSpyServer_callbacks.h" />
<ClInclude Include="xrServer.h" />
Expand Down
3 changes: 3 additions & 0 deletions src/xrGame/xrGame.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -6501,6 +6501,9 @@
<ClInclude Include="ui\UIMapFilters.h">
<Filter>UI\Common\PDA\Tasks</Filter>
</ClInclude>
<ClInclude Include="xrGame.h">
<Filter>Core\Server</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="damage_manager.cpp">
Expand Down
Loading

0 comments on commit 5b2ec76

Please sign in to comment.