From 6fffce97dc4cc827edbfbbf6f4ba5b279a8cb639 Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Fri, 3 May 2024 08:10:32 +0500 Subject: [PATCH] Link xrGame to xr_3da directly --- src/xrEngine/Engine.cpp | 4 +- src/xrEngine/Engine.h | 2 +- src/xrEngine/EngineAPI.cpp | 75 +++++++++++++------------------ src/xrEngine/EngineAPI.h | 44 +++++++----------- src/xrEngine/x_ray.cpp | 4 +- src/xrEngine/x_ray.h | 4 +- src/xrGame/CMakeLists.txt | 1 + src/xrGame/xrGame.cpp | 12 +++-- src/xrGame/xrGame.h | 29 ++++++++++++ src/xrGame/xrGame.vcxproj | 1 + src/xrGame/xrGame.vcxproj.filters | 3 ++ src/xr_3da/CMakeLists.txt | 1 + src/xr_3da/entry_point.cpp | 3 +- src/xr_3da/xr_3da.vcxproj | 3 ++ 14 files changed, 103 insertions(+), 83 deletions(-) create mode 100644 src/xrGame/xrGame.h diff --git a/src/xrEngine/Engine.cpp b/src/xrEngine/Engine.cpp index aea781905d0..277b90bba4c 100644 --- a/src/xrEngine/Engine.cpp +++ b/src/xrEngine/Engine.cpp @@ -61,7 +61,7 @@ void CheckAndSetupRenderer() extern void msCreate(pcstr name); -void CEngine::Initialize(void) +void CEngine::Initialize(GameModule* game) { ZoneScoped; #ifdef DEBUG @@ -77,7 +77,7 @@ void CEngine::Initialize(void) CheckAndSetupRenderer(); - External.Initialize(); + External.Initialize(game); Sheduler.Initialize(); } diff --git a/src/xrEngine/Engine.h b/src/xrEngine/Engine.h index fd67507acb5..753904dd7d6 100644 --- a/src/xrEngine/Engine.h +++ b/src/xrEngine/Engine.h @@ -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; diff --git a/src/xrEngine/EngineAPI.cpp b/src/xrEngine/EngineAPI.cpp index 2285a87497b..58f1b8cdc7d 100644 --- a/src/xrEngine/EngineAPI.cpp +++ b/src/xrEngine/EngineAPI.cpp @@ -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 + extern xr_vector 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 g_render_modules = +{{ + { "xrRender_R4", nullptr, nullptr }, + { "xrRender_GL", nullptr, nullptr }, +}}; + ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// @@ -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; @@ -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(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; @@ -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()); diff --git a/src/xrEngine/EngineAPI.h b/src/xrEngine/EngineAPI.h index 826238ce336..5a472088b52 100644 --- a/src/xrEngine/EngineAPI.h +++ b/src/xrEngine/EngineAPI.h @@ -6,11 +6,8 @@ #pragma once #include "xrEngine/Engine.h" -#include "xrCore/ModuleLookup.hpp" #include "xrCore/clsid.h" -#include - class XR_NOVTABLE IFactoryObject { public: @@ -39,6 +36,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: @@ -50,44 +55,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 renderers; xr_map 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(); diff --git a/src/xrEngine/x_ray.cpp b/src/xrEngine/x_ray.cpp index 7a02cf31e84..75914e8b8ae 100644 --- a/src/xrEngine/x_ray.cpp +++ b/src/xrEngine/x_ray.cpp @@ -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); @@ -319,7 +319,7 @@ CApplication::CApplication(pcstr commandLine) InitConsole(); TaskScheduler->Wait(createRendererList); - Engine.Initialize(); + Engine.Initialize(game); Device.Initialize(); Console->OnDeviceInitialize(); diff --git a/src/xrEngine/x_ray.h b/src/xrEngine/x_ray.h index 8a9651cb4b6..1ab9d457d3b 100644 --- a/src/xrEngine/x_ray.h +++ b/src/xrEngine/x_ray.h @@ -3,7 +3,7 @@ #include -#include "xrCore/Threading/Event.hpp" +#include "xrEngine/Engine.h" struct SDL_Window; struct SDL_Surface; @@ -36,7 +36,7 @@ class ENGINE_API CApplication final public: // Other - CApplication(pcstr commandLine); + CApplication(pcstr commandLine, GameModule* game); ~CApplication(); int Run(); diff --git a/src/xrGame/CMakeLists.txt b/src/xrGame/CMakeLists.txt index 2121a29d0e8..355aa83eae5 100644 --- a/src/xrGame/CMakeLists.txt +++ b/src/xrGame/CMakeLists.txt @@ -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 diff --git a/src/xrGame/xrGame.cpp b/src/xrGame/xrGame.cpp index e6ea87dd1a2..73a488827b6 100644 --- a/src/xrGame/xrGame.cpp +++ b/src/xrGame/xrGame.cpp @@ -7,6 +7,7 @@ //////////////////////////////////////////////////////////////////////////// #include "StdAfx.h" +#include "xrGame.h" #include "object_factory.h" @@ -16,6 +17,8 @@ #include "xrUICore/XML/xrUIXmlParser.h" #include "xrUICore/ui_styles.h" +xrGameModule xrGame; + void CCC_RegisterCommands(); extern float g_fTimeFactor; @@ -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 @@ -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(); @@ -77,4 +84,3 @@ XR_EXPORT void finalize_library() xr_delete(g_profiler); #endif } -} diff --git a/src/xrGame/xrGame.h b/src/xrGame/xrGame.h new file mode 100644 index 00000000000..dbc068e6612 --- /dev/null +++ b/src/xrGame/xrGame.h @@ -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; diff --git a/src/xrGame/xrGame.vcxproj b/src/xrGame/xrGame.vcxproj index 5b856169444..4117babe5e1 100644 --- a/src/xrGame/xrGame.vcxproj +++ b/src/xrGame/xrGame.vcxproj @@ -1443,6 +1443,7 @@ + diff --git a/src/xrGame/xrGame.vcxproj.filters b/src/xrGame/xrGame.vcxproj.filters index 794e6c8653e..064a21b7c5e 100644 --- a/src/xrGame/xrGame.vcxproj.filters +++ b/src/xrGame/xrGame.vcxproj.filters @@ -6501,6 +6501,9 @@ UI\Common\PDA\Tasks + + Core\Server + diff --git a/src/xr_3da/CMakeLists.txt b/src/xr_3da/CMakeLists.txt index 7fbdf10f7c1..52ecca693f9 100644 --- a/src/xr_3da/CMakeLists.txt +++ b/src/xr_3da/CMakeLists.txt @@ -21,6 +21,7 @@ target_link_libraries(xr_3da xrCore xrAPI xrEngine + xrGame ) set_target_properties(xr_3da PROPERTIES diff --git a/src/xr_3da/entry_point.cpp b/src/xr_3da/entry_point.cpp index 0d5f7e0240a..e59f3b191a1 100644 --- a/src/xr_3da/entry_point.cpp +++ b/src/xr_3da/entry_point.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "xrEngine/x_ray.h" +#include "xrGame/xrGame.h" #if defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD) || defined(XR_PLATFORM_APPLE) #include @@ -24,7 +25,7 @@ int entry_point(pcstr commandLine) if (strstr(commandLine, "-dedicated")) GEnv.isDedicatedServer = true; - CApplication app{ commandLine }; + CApplication app{ commandLine, &xrGame }; return app.Run(); } diff --git a/src/xr_3da/xr_3da.vcxproj b/src/xr_3da/xr_3da.vcxproj index fccddc4fccb..d55a87b8c5c 100644 --- a/src/xr_3da/xr_3da.vcxproj +++ b/src/xr_3da/xr_3da.vcxproj @@ -59,6 +59,9 @@ {1daec516-e52c-4a3c-a4da-ae3553e6e0f8} + + {200652a6-043e-4634-8837-87983b3bd5e0} +