Skip to content

Commit 1290af0

Browse files
committed
core: remove dependency on RTTI from dynamic_component_cast, add IsGameProcess checks
1 parent 5a3eb40 commit 1290af0

File tree

9 files changed

+91
-17
lines changed

9 files changed

+91
-17
lines changed

code/client/citicore/ComponentLoader.h

+18-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "EventCore.h"
1212
#include <unordered_map>
1313

14+
#include <boost/type_index.hpp>
15+
1416
//
1517
// An identifier for a Fx component. These are formatted like `category:subcategory:...[version]`.
1618
//
@@ -77,6 +79,16 @@ class EXPORTED_TYPE Component : public fwRefCountable
7779
{
7880
return true;
7981
}
82+
83+
virtual inline bool IsA(uint32_t type)
84+
{
85+
return false;
86+
}
87+
88+
virtual inline void* As(uint32_t type)
89+
{
90+
return nullptr;
91+
}
8092
};
8193

8294
class EXPORTED_TYPE RunnableComponent : public Component
@@ -231,12 +243,12 @@ std::queue<TListEntry> SortDependencyList(const std::vector<TListEntry>& list)
231243
template<typename T, typename TOther>
232244
inline T dynamic_component_cast(TOther value)
233245
{
234-
try
235-
{
236-
return dynamic_cast<T>(value);
237-
}
238-
catch (std::bad_typeid)
246+
auto type = HashString(boost::typeindex::type_id<std::remove_pointer_t<T>>().pretty_name().substr(6).c_str());
247+
248+
if (value->IsA(type))
239249
{
240-
return T();
250+
return reinterpret_cast<T>(value->As(type));
241251
}
252+
253+
return T();
242254
}

code/client/citicore/ResumeComponent.h

+21-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ class LifeCycleComponentBase : public TBaseComponent, public LifeCycleComponent
2323
{
2424
// empty
2525
}
26+
27+
virtual bool IsA(uint32_t type) override
28+
{
29+
if (type == HashString("LifeCycleComponent"))
30+
{
31+
return true;
32+
}
33+
34+
return TBaseComponent::IsA(type);
35+
}
36+
37+
virtual void* As(uint32_t type) override
38+
{
39+
if (type == HashString("LifeCycleComponent"))
40+
{
41+
return static_cast<LifeCycleComponent*>(this);
42+
}
43+
44+
return TBaseComponent::As(type);
45+
}
2646
};
2747

2848
void Component_RunPreInit();
@@ -31,13 +51,8 @@ template<typename TBaseComponent>
3151
class LifeCyclePreInitComponentBase : public LifeCycleComponentBase<TBaseComponent>
3252
{
3353
public:
34-
virtual void PreResumeGame() override
35-
{
36-
OnResumeGame();
37-
}
38-
3954
virtual void PreInitGame() override
4055
{
4156
Component_RunPreInit();
4257
}
43-
};
58+
};

code/client/citicore/ToolComponentHelpers.h

+20
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,25 @@ class ToolComponentBase : public TBaseComponent, public ToolComponent
138138

139139
return nullptr;
140140
}
141+
142+
virtual bool IsA(uint32_t type) override
143+
{
144+
if (type == HashString("ToolComponent"))
145+
{
146+
return true;
147+
}
148+
149+
return TBaseComponent::IsA(type);
150+
}
151+
152+
virtual void* As(uint32_t type) override
153+
{
154+
if (type == HashString("ToolComponent"))
155+
{
156+
return static_cast<ToolComponent*>(this);
157+
}
158+
159+
return TBaseComponent::As(type);
160+
}
141161
};
142162

code/client/citicore/om/OMComponent.h

+20
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,24 @@ class OMComponentBase : public TBaseComponent, public OMComponent
162162

163163
return retval;
164164
}
165+
166+
virtual bool IsA(uint32_t type) override
167+
{
168+
if (type == HashString("OMComponent"))
169+
{
170+
return true;
171+
}
172+
173+
return TBaseComponent::IsA(type);
174+
}
175+
176+
virtual void* As(uint32_t type) override
177+
{
178+
if (type == HashString("OMComponent"))
179+
{
180+
return static_cast<OMComponent*>(this);
181+
}
182+
183+
return TBaseComponent::As(type);
184+
}
165185
};

code/client/launcher/CitizenGame.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ void CitizenGame::Launch(const std::wstring& gamePath, bool isMainGame)
411411
static HostSharedData<CfxState> initState("CfxInitState");
412412

413413
// prevent accidental duplicate instances
414-
if (isMainGame && !initState->IsMasterProcess())
414+
if (isMainGame && !initState->IsMasterProcess() && !initState->IsGameProcess())
415415
{
416416
return;
417417
}

code/client/shared/CfxState.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
struct CfxState
66
{
77
int initialPid;
8+
int gamePid;
89

910
bool inJobObject;
1011
bool running;
@@ -15,6 +16,7 @@ struct CfxState
1516
{
1617
running = true;
1718
initialPid = GetCurrentProcessId();
19+
gamePid = 0;
1820

1921
// get the init path
2022
wchar_t modulePath[512];
@@ -38,4 +40,9 @@ struct CfxState
3840
{
3941
return (initialPid == GetCurrentProcessId());
4042
}
41-
};
43+
44+
inline bool IsGameProcess()
45+
{
46+
return (gamePid == GetCurrentProcessId());
47+
}
48+
};

code/components/devcon/src/DevConServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static InitFunction initFunction([]()
191191

192192
console::CoreAddPrintListener(DevConPrintListener);
193193

194-
if (!hostData->IsMasterProcess())
194+
if (!hostData->IsMasterProcess() && !hostData->IsGameProcess())
195195
{
196196
return;
197197
}

code/components/glue/src/ConnectToNative.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void Component_RunPreInit()
448448

449449
static HostSharedData<CfxState> hostData("CfxInitState");
450450

451-
if (hostData->IsMasterProcess())
451+
if (hostData->IsMasterProcess() || hostData->IsGameProcess())
452452
{
453453
rage::OnInitFunctionStart.Connect([](rage::InitFunctionType type)
454454
{

code/components/steam/src/SteamComponent.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void SteamComponent::Initialize()
102102
// launch the presence dummy dummy if needed
103103
static HostSharedData<CfxState> hostData("CfxInitState");
104104

105-
if (hostData->IsMasterProcess())
105+
if (hostData->IsMasterProcess() || hostData->IsGameProcess())
106106
{
107107
SetEnvironmentVariable(L"SteamAppId", L"218");
108108

0 commit comments

Comments
 (0)