Skip to content

Commit

Permalink
Explicitly initialize and finalize xrGame instead of using DllMain or…
Browse files Browse the repository at this point in the history
… __attribute__((constructor)) / __attribute__((destructor)) (#804)
  • Loading branch information
Xottab-DUTY committed May 30, 2021
1 parent 47ba334 commit 810692a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 58 deletions.
20 changes: 18 additions & 2 deletions src/xrEngine/EngineAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ void CEngineAPI::Initialize(void)

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();
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -148,10 +156,18 @@ void CEngineAPI::Initialize(void)

void CEngineAPI::Destroy(void)
{
hGame = nullptr;
hTuner = nullptr;
if (pFinalizeGame)
pFinalizeGame();

pInitializeGame = nullptr;
pFinalizeGame = nullptr;
pCreate = nullptr;
pDestroy = nullptr;

hGame = nullptr;

hTuner = nullptr;

renderers.clear();
Engine.Event._destroy();
XRC.r_clear_compact();
Expand Down
6 changes: 6 additions & 0 deletions src/xrEngine/EngineAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class RendererModule

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

using GetRendererModule = RendererModule*(*)();

struct RendererDesc
Expand All @@ -73,6 +76,9 @@ class ENGINE_API CEngineAPI
XRay::Module hGame;
XRay::Module hTuner;

InitializeGameLibraryProc pInitializeGame;
FinalizeGameLibraryProc pFinalizeGame;

public:
BENCH_SEC_SCRAMBLEMEMBER1

Expand Down
84 changes: 28 additions & 56 deletions src/xrGame/xrGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,42 @@
extern void FillUIStyleToken();
extern void CleanupUIStyleToken();

extern "C" {
XR_EXPORT IFactoryObject* __cdecl xrFactory_Create(CLASS_ID clsid)
{
IFactoryObject* object = object_factory().client_object(clsid);
#ifdef DEBUG
if (!object)
return (0);
#endif
// XXX nitrocaster XRFACTORY: set clsid during factory initialization
object->GetClassId() = clsid;
return (object);
}

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

void CCC_RegisterCommands();

static void initialize_library()
extern "C"
{
// Fill ui style token
FillUIStyleToken();
// register console commands
CCC_RegisterCommands();
// keyboard binding
CCC_RegisterInput();
XR_EXPORT IFactoryObject* __cdecl xrFactory_Create(CLASS_ID clsid)
{
IFactoryObject* object = object_factory().client_object(clsid);
#ifdef DEBUG
g_profiler = xr_new<CProfiler>();
if (!object)
return (0);
#endif
gStringTable = xr_new<CStringTable>();
StringTable().Init();
}
// XXX nitrocaster XRFACTORY: set clsid during factory initialization
object->GetClassId() = clsid;
return (object);
}

static void finalize_library()
{
CleanupUIStyleToken();
xr_delete(gStringTable);
}
XR_EXPORT void __cdecl xrFactory_Destroy(IFactoryObject* O) { xr_delete(O); }

#ifdef XR_PLATFORM_WINDOWS
BOOL APIENTRY DllMain(HANDLE /*hModule*/, u32 ul_reason_for_call, LPVOID /*lpReserved*/)
{
switch (ul_reason_for_call)
XR_EXPORT void initialize_library()
{
case DLL_PROCESS_ATTACH:
initialize_library();
break;

case DLL_PROCESS_DETACH:
finalize_library();
break;
// Fill ui style token
FillUIStyleToken();
// register console commands
CCC_RegisterCommands();
// keyboard binding
CCC_RegisterInput();
#ifdef DEBUG
g_profiler = xr_new<CProfiler>();
#endif
gStringTable = xr_new<CStringTable>();
StringTable().Init();
}
return (TRUE);
}
#elif defined(XR_PLATFORM_LINUX)
__attribute__((constructor)) static void load(int /*argc*/, char** /*argv*/, char** /*envp*/)
{
initialize_library();
}

__attribute__((destructor)) static void unload()
{
finalize_library();
XR_EXPORT void finalize_library()
{
CleanupUIStyleToken();
xr_delete(gStringTable);
}
}
#else
#error Add your platform here
#endif

0 comments on commit 810692a

Please sign in to comment.