Skip to content

Commit

Permalink
Separate GUI state from implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Aug 23, 2024
1 parent 19d4a4d commit 7a486f1
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 33 deletions.
18 changes: 13 additions & 5 deletions Source/GlobalContext/FullGlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <Hud/BombStatus/BombStatusPanelUnloadHandler.h>
#include <MemorySearch/PatternFinder.h>
#include <UI/Panorama/PanoramaGUI.h>
#include <UI/Panorama/PanoramaGuiState.h>
#include <UI/Panorama/PanoramaGuiUnloadHandler.h>
#include <Platform/DynamicLibrary.h>
#include <Platform/VmtFinder.h>
#include <Vmt/VmtLengthCalculator.h>
Expand Down Expand Up @@ -56,7 +58,7 @@ struct FullGlobalContext {
{
hooks.viewRenderHook.getOriginalOnRenderStart()(thisptr);

HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, inWorldPanelContainerState, featuresStates};
HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, inWorldPanelContainerState, panoramaGuiState, featuresStates};
SoundWatcher soundWatcher{featureHelpers.soundWatcherState, dependencies};
soundWatcher.update();
features(dependencies).soundFeatures().runOnViewMatrixUpdate();
Expand All @@ -66,22 +68,28 @@ struct FullGlobalContext {
playerInformationThroughWalls.hideUnusedPanels();
}

[[nodiscard]] PeepEventsHookResult onPeepEventsHook() noexcept
[[nodiscard]] PeepEventsHookResult onPeepEventsHook(bool fullContextJustInitialized) noexcept
{
HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, inWorldPanelContainerState, featuresStates};
HookDependencies dependencies{_gameDependencies, featureHelpers, bombStatusPanelState, inWorldPanelContainerState, panoramaGuiState, featuresStates};

if (fullContextJustInitialized) {
if (const auto mainMenu{_gameDependencies.mainMenu}; mainMenu && *mainMenu)
dependencies.make<PanoramaGUI>().init(PanoramaUiPanel{PanoramaUiPanelContext{dependencies, (*mainMenu)->uiPanel}});
}

features(dependencies).hudFeatures().defusingAlert().run();
features(dependencies).hudFeatures().killfeedPreserver().run();
BombStatusPanelManager{BombStatusPanelManagerContext{dependencies}}.run();

UnloadFlag unloadFlag;
panoramaGUI.run(dependencies, features(dependencies), unloadFlag);
dependencies.make<PanoramaGUI>().run(features(dependencies), unloadFlag);
hooks.update();

if (unloadFlag) {
FeaturesUnloadHandler{dependencies, featuresStates}.handleUnload();
BombStatusPanelUnloadHandler{dependencies}.handleUnload();
InWorldPanelContainerUnloadHandler{dependencies}.handleUnload();
PanoramaGuiUnloadHandler{dependencies}.handleUnload();
hooks.forceUninstall();
}

Expand All @@ -104,7 +112,7 @@ struct FullGlobalContext {
FeatureHelpers featureHelpers;
public:
FeaturesStates featuresStates;
PanoramaGUI panoramaGUI;
PanoramaGuiState panoramaGuiState;
BombStatusPanelState bombStatusPanelState;
InWorldPanelContainerState inWorldPanelContainerState;
};
11 changes: 4 additions & 7 deletions Source/GlobalContext/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ class GlobalContext {

[[nodiscard]] PeepEventsHookResult peepEventsHook() noexcept
{
initializeCompleteContextFromGameThread();
return fullContext().onPeepEventsHook();
return fullContext().onPeepEventsHook(initializeCompleteContextFromGameThread());
}

private:
void initializeCompleteContextFromGameThread() noexcept
bool initializeCompleteContextFromGameThread() noexcept
{
if (deferredCompleteContext.isComplete())
return;
return false;

const auto partialContext = deferredCompleteContext.partial();

Expand All @@ -73,9 +72,7 @@ class GlobalContext {
MemoryPatterns{partialContext.patternFinders}
);

HookDependencies hookDependencies{fullContext().gameDependencies(), fullContext().getFeatureHelpers(), fullContext().bombStatusPanelState, fullContext().inWorldPanelContainerState, fullContext().featuresStates};
if (const auto mainMenu{fullContext().gameDependencies().mainMenu}; mainMenu && *mainMenu)
fullContext().panoramaGUI.init(PanoramaUiPanel{PanoramaUiPanelContext{hookDependencies, (*mainMenu)->uiPanel}});
return true;
}

FreeMemoryRegionList _freeRegionList;
Expand Down
10 changes: 9 additions & 1 deletion Source/HookDependencies/HookDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

struct BombStatusPanelState;
struct FeaturesStates;
struct PanoramaGuiState;

struct HookDependencies {
HookDependencies(GameDependencies& gameDependencies, FeatureHelpers& featureHelpers, BombStatusPanelState& bombStatusPanelState, InWorldPanelContainerState& inWorldPanelContainerState, FeaturesStates& featuresStates) noexcept
HookDependencies(GameDependencies& gameDependencies, FeatureHelpers& featureHelpers, BombStatusPanelState& bombStatusPanelState, InWorldPanelContainerState& inWorldPanelContainerState, PanoramaGuiState& panoramaGuiState, FeaturesStates& featuresStates) noexcept
: _gameDependencies{gameDependencies}
, featureHelpers{featureHelpers}
, _bombStatusPanelState{bombStatusPanelState}
, _inWorldPanelContainerState{inWorldPanelContainerState}
, _panoramaGuiState{panoramaGuiState}
, _featuresStates{featuresStates}
{
if (gameDependencies.worldToProjectionMatrix)
Expand Down Expand Up @@ -83,6 +85,11 @@ struct HookDependencies {
return _inWorldPanelContainerState;
}

[[nodiscard]] PanoramaGuiState& panoramaGuiState() const noexcept
{
return _panoramaGuiState;
}

[[nodiscard]] FeaturesStates& featuresStates() const noexcept
{
return _featuresStates;
Expand Down Expand Up @@ -195,6 +202,7 @@ struct HookDependencies {
FeatureHelpers& featureHelpers;
BombStatusPanelState& _bombStatusPanelState;
InWorldPanelContainerState& _inWorldPanelContainerState;
PanoramaGuiState& _panoramaGuiState;
FeaturesStates& _featuresStates;

const cs2::CConcreteEntityList* entityList;
Expand Down
2 changes: 2 additions & 0 deletions Source/Osiris.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@
<ClInclude Include="SDL\SdlFunctions.h" />
<ClInclude Include="UI\Panorama\PanoramaCommandDispatcher.h" />
<ClInclude Include="UI\Panorama\PanoramaGUI.h" />
<ClInclude Include="UI\Panorama\PanoramaGuiState.h" />
<ClInclude Include="UI\Panorama\PanoramaGuiUnloadHandler.h" />
<ClInclude Include="UI\Panorama\SetCommandHandler.h" />
<ClInclude Include="Utils\Align.h" />
<ClInclude Include="Utils\BitFlags.h" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Osiris.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,12 @@
<ClInclude Include="Features\Hud\BombTimer\BombTimerPanelParams.h">
<Filter>Features\Hud\BombTimer</Filter>
</ClInclude>
<ClInclude Include="UI\Panorama\PanoramaGuiState.h">
<Filter>UI\Panorama</Filter>
</ClInclude>
<ClInclude Include="UI\Panorama\PanoramaGuiUnloadHandler.h">
<Filter>UI\Panorama</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="UI\Panorama\CreateGUI.js">
Expand Down
37 changes: 17 additions & 20 deletions Source/UI/Panorama/PanoramaGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@

#include "PanoramaCommandDispatcher.h"

template <typename HookContext>
class PanoramaGUI {
public:
explicit PanoramaGUI(HookContext& hookContext) noexcept
: hookContext{hookContext}
{
}

void init(auto&& mainMenu) noexcept
{
if (!mainMenu)
Expand All @@ -20,7 +26,7 @@ class PanoramaGUI {

const auto settings = mainMenu.findChildInLayoutFile("JsSettings");
if (settings)
settingsPanelPtr.handle = settings.getHandle();
state().settingsPanelHandle = settings.getHandle();

PanoramaUiEngine::runScript(settings, reinterpret_cast<const char*>(
#include "CreateGUI.js"
Expand All @@ -46,27 +52,15 @@ class PanoramaGUI {
)", "", 0);

if (const auto guiButtonPanel = mainMenu.findChildInLayoutFile("OsirisOpenMenuButton"))
guiButtonPointer.handle = guiButtonPanel.getHandle();
state().guiButtonHandle = guiButtonPanel.getHandle();

if (const auto guiPanel = mainMenu.findChildInLayoutFile("OsirisMenuTab"))
guiPanelPointer.handle = guiPanel.getHandle();
state().guiPanelHandle = guiPanel.getHandle();
}

~PanoramaGUI() noexcept
void run(Features features, UnloadFlag& unloadFlag) const noexcept
{
if (guiButtonPointer.getHandle().isValid())
PanoramaUiEngine::onDeletePanel(guiButtonPointer.getHandle());

if (guiPanelPointer.getHandle().isValid())
PanoramaUiEngine::onDeletePanel(guiPanelPointer.getHandle());

if (const auto settingsPanel = settingsPanelPtr.get())
PanoramaUiEngine::runScript(settingsPanel, "delete $.Osiris", "", 0);
}

void run(HookDependencies& hookDependencies, Features features, UnloadFlag& unloadFlag) const noexcept
{
const auto guiPanel = PanoramaUiPanel{PanoramaUiPanelContext{hookDependencies, guiPanelPointer.get()}};
auto&& guiPanel = hookContext.panels().getPanelFromHandle(state().guiPanelHandle);
if (!guiPanel)
return;

Expand All @@ -77,7 +71,10 @@ class PanoramaGUI {
}

private:
PanoramaPanelPointer guiPanelPointer;
PanoramaPanelPointer guiButtonPointer;
PanoramaPanelPointer settingsPanelPtr;
[[nodiscard]] auto& state() const noexcept
{
return hookContext.panoramaGuiState();
}

HookContext& hookContext;
};
9 changes: 9 additions & 0 deletions Source/UI/Panorama/PanoramaGuiState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <CS2/Panorama/PanelHandle.h>

struct PanoramaGuiState {
cs2::PanelHandle guiPanelHandle;
cs2::PanelHandle guiButtonHandle;
cs2::PanelHandle settingsPanelHandle;
};
28 changes: 28 additions & 0 deletions Source/UI/Panorama/PanoramaGuiUnloadHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <GameClasses/PanoramaUiEngine.h>

template <typename HookContext>
struct PanoramaGuiUnloadHandler {
explicit PanoramaGuiUnloadHandler(HookContext& hookContext) noexcept
: hookContext{hookContext}
{
}

void handleUnload() const noexcept
{
hookContext.panels().deletePanelByHandle(state().guiButtonHandle);
hookContext.panels().deletePanelByHandle(state().guiPanelHandle);

if (auto&& settingsPanel = hookContext.panels().getPanelFromHandle(state().settingsPanelHandle))
PanoramaUiEngine::runScript(settingsPanel, "delete $.Osiris", "", 0);
}

private:
[[nodiscard]] auto& state() const noexcept
{
return hookContext.panoramaGuiState();
}

HookContext& hookContext;
};

0 comments on commit 7a486f1

Please sign in to comment.