Skip to content

Commit 6331269

Browse files
Inline GlobalContext methods directly into endpoints
1 parent 79c81e6 commit 6331269

File tree

3 files changed

+119
-125
lines changed

3 files changed

+119
-125
lines changed

Source/Endpoints.h

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,123 @@
44
#include "Hooks/PeepEventsHook.h"
55
#include "Utils/ReturnAddress.h"
66

7+
[[NOINLINE]] void finishInit(auto& hookContext)
8+
{
9+
hookContext.entityClassifier().init(hookContext);
10+
if (const auto mainMenu{hookContext.patternSearchResults().template get<MainMenuPanelPointer>()}; mainMenu && *mainMenu)
11+
hookContext.template make<PanoramaGUI>().init(hookContext.template make<PanoramaUiPanel>((*mainMenu)->uiPanel));
12+
hookContext.config().init();
13+
hookContext.config().scheduleLoad();
14+
hookContext.hooks().peepEventsHook.disable();
15+
hookContext.hooks().viewRenderHook.install();
16+
}
17+
718
int SDLHook_PeepEvents(void* events, int numevents, int action, unsigned minType, unsigned maxType) noexcept
819
{
9-
return GlobalContext::instance().peepEventsHook(events, numevents, action, minType, maxType);
20+
const auto initInProgress = !HookContext<GlobalContext>::isGlobalContextComplete();
21+
if (initInProgress)
22+
HookContext<GlobalContext>::initCompleteGlobalContextFromGameThread();
23+
24+
HookContext<GlobalContext> hookContext;
25+
26+
if (initInProgress)
27+
finishInit(hookContext);
28+
29+
return hookContext.hooks().peepEventsHook.original(events, numevents, action, minType, maxType);
30+
}
31+
32+
[[NOINLINE]] void unload(auto& hookContext) noexcept
33+
{
34+
hookContext.template make<BombTimer>().onUnload();
35+
hookContext.template make<DefusingAlert>().onUnload();
36+
hookContext.template make<PostRoundTimer>().onUnload();
37+
hookContext.template make<OutlineGlow>().onUnload();
38+
hookContext.template make<BombStatusPanel>().onUnload();
39+
hookContext.template make<InWorldPanels>().onUnload();
40+
hookContext.template make<PanoramaGUI>().onUnload();
41+
hookContext.hooks().viewRenderHook.uninstall();
42+
hookContext.template make<ClientModeHooks>().restoreGetViewmodelFov();
43+
hookContext.template make<PlayerModelGlowPreview>().onUnload();
44+
hookContext.template make<WeaponModelGlowPreview>().onUnload();
45+
hookContext.template make<NoScopeInaccuracyVis>().onUnload();
46+
hookContext.template make<BombPlantAlert>().onUnload();
47+
48+
hookContext.template make<EntitySystem>().forEachNetworkableEntityIdentity([&hookContext](const auto& entityIdentity) {
49+
auto&& baseEntity = hookContext.template make<BaseEntity>(static_cast<cs2::C_BaseEntity*>(entityIdentity.entity));
50+
const auto entityTypeInfo = baseEntity.classify();
51+
if (entityTypeInfo.template is<cs2::C_CSPlayerPawn>())
52+
hookContext.template make<ModelGlow>().onUnload()(PlayerModelGlow{hookContext}, baseEntity.template as<PlayerPawn>());
53+
else if (entityTypeInfo.template is<cs2::C_C4>())
54+
hookContext.template make<ModelGlow>().onUnload()(DroppedBombModelGlow{hookContext}, baseEntity.template as<BaseWeapon>());
55+
else if (entityTypeInfo.template is<cs2::CBaseAnimGraph>())
56+
hookContext.template make<ModelGlow>().onUnload()(DefuseKitModelGlow{hookContext}, baseEntity);
57+
else if (entityTypeInfo.template is<cs2::CPlantedC4>())
58+
hookContext.template make<ModelGlow>().onUnload()(TickingBombModelGlow{hookContext}, baseEntity.template as<PlantedC4>());
59+
else if (entityTypeInfo.isGrenadeProjectile())
60+
hookContext.template make<ModelGlow>().onUnload()(GrenadeProjectileModelGlow{hookContext}, baseEntity);
61+
else if (entityTypeInfo.isWeapon())
62+
hookContext.template make<ModelGlow>().onUnload()(WeaponModelGlow{hookContext}, baseEntity.template as<BaseWeapon>());
63+
});
1064
}
1165

1266
void ViewRenderHook_onRenderStart(cs2::CViewRender* thisptr) noexcept
1367
{
14-
const auto unloadFlag = GlobalContext::instance().onRenderStartHook(thisptr);
15-
if (unloadFlag)
16-
GlobalContext::destroyInstance();
68+
HookContext<GlobalContext> hookContext;
69+
hookContext.hooks().viewRenderHook.getOriginalOnRenderStart()(thisptr);
70+
hookContext.make<InWorldPanels>().updateState();
71+
SoundWatcher<decltype(hookContext)> soundWatcher{hookContext.soundWatcherState(), hookContext};
72+
soundWatcher.update();
73+
SoundFeatures{hookContext.soundWatcherState(), hookContext.hooks().viewRenderHook, hookContext}.runOnViewMatrixUpdate();
74+
75+
hookContext.make<NoScopeInaccuracyVis>().update();
76+
hookContext.make<RenderingHookEntityLoop>().run();
77+
hookContext.make<GlowSceneObjects>().removeUnreferencedObjects();
78+
hookContext.make<DefusingAlert>().run();
79+
hookContext.make<KillfeedPreserver>().run();
80+
hookContext.make<BombStatusPanelManager>().run();
81+
hookContext.make<InWorldPanels>().hideUnusedPanels();
82+
83+
UnloadFlag unloadFlag;
84+
hookContext.make<PanoramaGUI>().run(unloadFlag);
85+
hookContext.config().update();
86+
hookContext.config().performFileOperation();
87+
88+
if (unloadFlag) {
89+
unload(hookContext);
90+
HookContext<GlobalContext>::destroyGlobalContext();
91+
}
1792
}
1893

1994
LINUX_ONLY([[gnu::aligned(8)]]) std::uint64_t PlayerPawn_sceneObjectUpdater(cs2::C_CSPlayerPawn* playerPawn, void* unknown, bool unknownBool) noexcept
2095
{
21-
return GlobalContext::instance().playerPawnSceneObjectUpdater(playerPawn, unknown, unknownBool);
96+
HookContext<GlobalContext> hookContext;
97+
const auto originalReturnValue = hookContext.featuresStates().visualFeaturesStates.modelGlowState.originalPlayerPawnSceneObjectUpdater(playerPawn, unknown, unknownBool);
98+
99+
auto&& playerPawn_ = hookContext.make<PlayerPawn>(playerPawn);
100+
if (auto&& previewPlayer = playerPawn_.template cast<PreviewPlayer>(); !previewPlayer)
101+
hookContext.make<ModelGlow>().updateInSceneObjectUpdater()(PlayerModelGlow{hookContext}, playerPawn_, EntityTypeInfo{});
102+
else
103+
hookContext.make<PlayerModelGlowPreview>().applyPreviewPlayerModelGlow(previewPlayer);
104+
105+
return originalReturnValue;
22106
}
23107

24108
LINUX_ONLY([[gnu::aligned(8)]]) std::uint64_t Weapon_sceneObjectUpdater(cs2::C_CSWeaponBase* weapon, void* unknown, bool unknownBool) noexcept
25109
{
26-
return GlobalContext::instance().weaponSceneObjectUpdater(weapon, unknown, unknownBool);
110+
HookContext<GlobalContext> hookContext;
111+
const auto originalReturnValue = hookContext.featuresStates().visualFeaturesStates.modelGlowState.originalWeaponSceneObjectUpdater(weapon, unknown, unknownBool);
112+
if (auto&& c4 = hookContext.make<BaseWeapon>(weapon).template cast<C4>())
113+
hookContext.make<ModelGlow>().updateInSceneObjectUpdater()(DroppedBombModelGlow{hookContext}, c4.baseWeapon(), EntityTypeInfo{});
114+
else
115+
hookContext.make<ModelGlow>().updateInSceneObjectUpdater()(WeaponModelGlow{hookContext}, hookContext.make<BaseWeapon>(weapon), hookContext.make<BaseWeapon>(weapon).baseEntity().classify());
116+
return originalReturnValue;
27117
}
28118

29119
float ClientModeHook_getViewmodelFov(cs2::ClientModeCSNormal* clientMode) noexcept
30120
{
31-
return GlobalContext::instance().getViewmodelFovHook(clientMode);
121+
HookContext<GlobalContext> hookContext;
122+
const auto originalFov = hookContext.hooks().originalGetViewmodelFov(clientMode);
123+
if (auto&& viewmodelMod = hookContext.template make<ViewmodelMod>(); viewmodelMod.shouldModifyViewmodelFov())
124+
return viewmodelMod.viewmodelFov();
125+
return originalFov;
32126
}

Source/GlobalContext/GlobalContext.h

Lines changed: 3 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -72,114 +72,9 @@ class GlobalContext {
7272
return deferredCompleteContext.complete();
7373
}
7474

75-
[[nodiscard]] int peepEventsHook(void* events, int numevents, int action, unsigned minType, unsigned maxType) noexcept
75+
[[nodiscard]] bool isComplete() noexcept
7676
{
77-
const auto initInProgress = !deferredCompleteContext.isComplete();
78-
if (initInProgress)
79-
initCompleteContextFromGameThread();
80-
81-
HookContext<GlobalContext> hookContext;
82-
83-
if (initInProgress)
84-
finishInit(hookContext);
85-
86-
return hookContext.hooks().peepEventsHook.original(events, numevents, action, minType, maxType);
87-
}
88-
89-
[[nodiscard]] std::uint64_t playerPawnSceneObjectUpdater(cs2::C_CSPlayerPawn* entity, void* unknown, bool unknownBool) noexcept
90-
{
91-
HookContext<GlobalContext> hookContext;
92-
const auto originalReturnValue = hookContext.featuresStates().visualFeaturesStates.modelGlowState.originalPlayerPawnSceneObjectUpdater(entity, unknown, unknownBool);
93-
94-
auto&& playerPawn = hookContext.make<PlayerPawn>(entity);
95-
if (auto&& previewPlayer = playerPawn.template cast<PreviewPlayer>(); !previewPlayer)
96-
hookContext.make<ModelGlow>().updateInSceneObjectUpdater()(PlayerModelGlow{hookContext}, playerPawn, EntityTypeInfo{});
97-
else
98-
hookContext.make<PlayerModelGlowPreview>().applyPreviewPlayerModelGlow(previewPlayer);
99-
100-
return originalReturnValue;
101-
}
102-
103-
[[nodiscard]] std::uint64_t weaponSceneObjectUpdater(cs2::C_CSWeaponBase* weapon, void* unknown, bool unknownBool) noexcept
104-
{
105-
HookContext<GlobalContext> hookContext;
106-
const auto originalReturnValue = hookContext.featuresStates().visualFeaturesStates.modelGlowState.originalWeaponSceneObjectUpdater(weapon, unknown, unknownBool);
107-
if (auto&& c4 = hookContext.make<BaseWeapon>(weapon).template cast<C4>())
108-
hookContext.make<ModelGlow>().updateInSceneObjectUpdater()(DroppedBombModelGlow{hookContext}, c4.baseWeapon(), EntityTypeInfo{});
109-
else
110-
hookContext.make<ModelGlow>().updateInSceneObjectUpdater()(WeaponModelGlow{hookContext}, hookContext.make<BaseWeapon>(weapon), hookContext.make<BaseWeapon>(weapon).baseEntity().classify());
111-
return originalReturnValue;
112-
}
113-
114-
[[nodiscard]] float getViewmodelFovHook(cs2::ClientModeCSNormal* clientMode) noexcept
115-
{
116-
HookContext<GlobalContext> hookContext;
117-
const auto originalFov = hookContext.hooks().originalGetViewmodelFov(clientMode);
118-
if (auto&& viewmodelMod = hookContext.template make<ViewmodelMod>(); viewmodelMod.shouldModifyViewmodelFov())
119-
return viewmodelMod.viewmodelFov();
120-
return originalFov;
121-
}
122-
123-
[[nodiscard]] UnloadFlag onRenderStartHook(cs2::CViewRender* viewRender) noexcept
124-
{
125-
HookContext<GlobalContext> hookContext;
126-
fullContext().hooks.viewRenderHook.getOriginalOnRenderStart()(viewRender);
127-
hookContext.make<InWorldPanels>().updateState();
128-
SoundWatcher<decltype(hookContext)> soundWatcher{fullContext().soundWatcherState, hookContext};
129-
soundWatcher.update();
130-
SoundFeatures{hookContext.soundWatcherState(), fullContext().hooks.viewRenderHook, hookContext}.runOnViewMatrixUpdate();
131-
132-
hookContext.make<NoScopeInaccuracyVis>().update();
133-
hookContext.make<RenderingHookEntityLoop>().run();
134-
hookContext.make<GlowSceneObjects>().removeUnreferencedObjects();
135-
hookContext.make<DefusingAlert>().run();
136-
hookContext.make<KillfeedPreserver>().run();
137-
hookContext.make<BombStatusPanelManager>().run();
138-
hookContext.make<InWorldPanels>().hideUnusedPanels();
139-
140-
UnloadFlag unloadFlag;
141-
hookContext.make<PanoramaGUI>().run(unloadFlag);
142-
hookContext.config().update();
143-
hookContext.config().performFileOperation();
144-
145-
if (unloadFlag)
146-
unload(hookContext);
147-
return unloadFlag;
148-
}
149-
150-
private:
151-
[[NOINLINE]] void unload(auto& hookContext) noexcept
152-
{
153-
hookContext.template make<BombTimer>().onUnload();
154-
hookContext.template make<DefusingAlert>().onUnload();
155-
hookContext.template make<PostRoundTimer>().onUnload();
156-
hookContext.template make<OutlineGlow>().onUnload();
157-
hookContext.template make<BombStatusPanel>().onUnload();
158-
hookContext.template make<InWorldPanels>().onUnload();
159-
hookContext.template make<PanoramaGUI>().onUnload();
160-
hookContext.hooks().viewRenderHook.uninstall();
161-
hookContext.template make<ClientModeHooks>().restoreGetViewmodelFov();
162-
hookContext.template make<PlayerModelGlowPreview>().onUnload();
163-
hookContext.template make<WeaponModelGlowPreview>().onUnload();
164-
hookContext.template make<NoScopeInaccuracyVis>().onUnload();
165-
hookContext.template make<BombPlantAlert>().onUnload();
166-
167-
hookContext.template make<EntitySystem>().forEachNetworkableEntityIdentity([&hookContext](const auto& entityIdentity) {
168-
auto&& baseEntity = hookContext.template make<BaseEntity>(static_cast<cs2::C_BaseEntity*>(entityIdentity.entity));
169-
const auto entityTypeInfo = baseEntity.classify();
170-
if (entityTypeInfo.template is<cs2::C_CSPlayerPawn>())
171-
hookContext.template make<ModelGlow>().onUnload()(PlayerModelGlow{hookContext}, baseEntity.template as<PlayerPawn>());
172-
else if (entityTypeInfo.template is<cs2::C_C4>())
173-
hookContext.template make<ModelGlow>().onUnload()(DroppedBombModelGlow{hookContext}, baseEntity.template as<BaseWeapon>());
174-
else if (entityTypeInfo.template is<cs2::CBaseAnimGraph>())
175-
hookContext.template make<ModelGlow>().onUnload()(DefuseKitModelGlow{hookContext}, baseEntity);
176-
else if (entityTypeInfo.template is<cs2::CPlantedC4>())
177-
hookContext.template make<ModelGlow>().onUnload()(TickingBombModelGlow{hookContext}, baseEntity.template as<PlantedC4>());
178-
else if (entityTypeInfo.isGrenadeProjectile())
179-
hookContext.template make<ModelGlow>().onUnload()(GrenadeProjectileModelGlow{hookContext}, baseEntity);
180-
else if (entityTypeInfo.isWeapon())
181-
hookContext.template make<ModelGlow>().onUnload()(WeaponModelGlow{hookContext}, baseEntity.template as<BaseWeapon>());
182-
});
77+
return deferredCompleteContext.isComplete();
18378
}
18479

18580
[[NOINLINE]] void initCompleteContextFromGameThread() noexcept
@@ -194,17 +89,7 @@ class GlobalContext {
19489
);
19590
}
19691

197-
[[NOINLINE]] static void finishInit(auto& hookContext)
198-
{
199-
hookContext.entityClassifier().init(hookContext);
200-
if (const auto mainMenu{hookContext.patternSearchResults().template get<MainMenuPanelPointer>()}; mainMenu && *mainMenu)
201-
hookContext.template make<PanoramaGUI>().init(hookContext.template make<PanoramaUiPanel>((*mainMenu)->uiPanel));
202-
hookContext.config().init();
203-
hookContext.config().scheduleLoad();
204-
hookContext.hooks().peepEventsHook.disable();
205-
hookContext.hooks().viewRenderHook.install();
206-
}
207-
92+
private:
20893
FreeMemoryRegionList _freeRegionList;
20994
DeferredCompleteObject<PartialGlobalContext, FullGlobalContext> deferredCompleteContext;
21095

Source/HookContext/HookContext.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ struct HookContext {
3434
{
3535
}
3636

37+
[[nodiscard]] static bool isGlobalContextComplete() noexcept
38+
{
39+
return GlobalContext::instance().isComplete();
40+
}
41+
42+
static void initCompleteGlobalContextFromGameThread() noexcept
43+
{
44+
GlobalContext::instance().initCompleteContextFromGameThread();
45+
}
46+
47+
static void destroyGlobalContext() noexcept
48+
{
49+
GlobalContext::destroyInstance();
50+
}
51+
3752
[[nodiscard]] SoundWatcherState& soundWatcherState() const noexcept
3853
{
3954
return fullGlobalContext.soundWatcherState;

0 commit comments

Comments
 (0)