Skip to content

Commit

Permalink
Add conditional hook macros and ShipInit module
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettjoecox committed Nov 29, 2024
1 parent b38794e commit 3917e3b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
10 changes: 4 additions & 6 deletions mm/2s2h/BenGui/UIWidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ bool CVarCheckbox(const char* label, const char* cvarName, const CheckboxOptions
if (Checkbox(label, &value, options)) {
CVarSetInteger(cvarName, value);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
dirty = true;
}
return dirty;
Expand Down Expand Up @@ -344,7 +345,6 @@ bool SliderInt(const char* label, int32_t* value, int32_t min, int32_t max, cons
*value -= options.step;
if (*value < min)
*value = min;
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
dirty = true;
}
ImGui::SameLine(0, 3.0f);
Expand All @@ -353,7 +353,6 @@ bool SliderInt(const char* label, int32_t* value, int32_t min, int32_t max, cons
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
}
if (ImGui::SliderScalar(invisibleLabel, ImGuiDataType_S32, value, &min, &max, options.format, options.flags)) {
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
dirty = true;
}
if (options.showButtons) {
Expand All @@ -363,7 +362,6 @@ bool SliderInt(const char* label, int32_t* value, int32_t min, int32_t max, cons
*value += options.step;
if (*value > max)
*value = max;
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
dirty = true;
}
}
Expand All @@ -387,6 +385,7 @@ bool CVarSliderInt(const char* label, const char* cvarName, int32_t min, int32_t
if (SliderInt(label, &value, min, max, options)) {
CVarSetInteger(cvarName, value);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
dirty = true;
}
return dirty;
Expand Down Expand Up @@ -458,7 +457,6 @@ bool SliderFloat(const char* label, float* value, float min, float max, const Fl
if (Button("-", { .color = options.color, .size = Sizes::Inline }) && *value > min) {
*value -= options.step;
ClampFloat(value, min, max, options.step);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
dirty = true;
}
ImGui::SameLine(0, 3.0f);
Expand All @@ -470,7 +468,6 @@ bool SliderFloat(const char* label, float* value, float min, float max, const Fl
options.format, options.flags)) {
*value = options.isPercentage ? valueToDisplay / 100.0f : valueToDisplay;
ClampFloat(value, min, max, options.step);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
dirty = true;
}
if (options.showButtons) {
Expand All @@ -479,7 +476,6 @@ bool SliderFloat(const char* label, float* value, float min, float max, const Fl
if (Button("+", { .color = options.color, .size = Sizes::Inline }) && *value < max) {
*value += options.step;
ClampFloat(value, min, max, options.step);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
dirty = true;
}
}
Expand All @@ -503,6 +499,7 @@ bool CVarSliderFloat(const char* label, const char* cvarName, float min, float m
if (SliderFloat(label, &value, min, max, options)) {
CVarSetFloat(cvarName, value);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
dirty = true;
}
return dirty;
Expand All @@ -520,6 +517,7 @@ bool CVarColorPicker(const char* label, const char* cvarName, Color_RGBA8 defaul
color.a = (uint8_t)(colorVec.w * 255.0f);
CVarSetColor(cvarName, color);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
changed = true;
}
PopStyleCombobox();
Expand Down
4 changes: 4 additions & 0 deletions mm/2s2h/BenGui/UIWidgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <libultraship/libultraship.h>
#include <unordered_map>
#include "2s2h/ShipUtils.h"
#include "2s2h/ShipInit.hpp"

namespace UIWidgets {

Expand Down Expand Up @@ -351,6 +352,7 @@ namespace UIWidgets {
if (Combobox<T>(label, &value, comboMap, options)) {
CVarSetInteger(cvarName, value);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
dirty = true;
}
return dirty;
Expand All @@ -363,6 +365,7 @@ namespace UIWidgets {
if (Combobox<T>(label, &value, comboVector, options)) {
CVarSetInteger(cvarName, value);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
dirty = true;
}
return dirty;
Expand All @@ -375,6 +378,7 @@ namespace UIWidgets {
if (Combobox<T>(label, &value, comboArray, options)) {
CVarSetInteger(cvarName, value);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
ShipInit::Init(cvarName);
dirty = true;
}
return dirty;
Expand Down
2 changes: 2 additions & 0 deletions mm/2s2h/BenPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CrowdControl* CrowdControl::Instance;
#include "2s2h/DeveloperTools/DeveloperTools.h"
#include "2s2h/SaveManager/SaveManager.h"
#include "2s2h/ShipUtils.h"
#include "2s2h/ShipInit.hpp"

// Resource Types/Factories
#include "resource/type/Blob.h"
Expand Down Expand Up @@ -636,6 +637,7 @@ extern "C" void InitOTR() {
GameInteractor::Instance = new GameInteractor();
LoadGuiTextures();
BenGui::SetupGuiElements();
ShipInit::InitAll();
InitEnhancements();
InitDeveloperTools();
GfxPatcher_ApplyNecessaryAuthenticPatches();
Expand Down
27 changes: 27 additions & 0 deletions mm/2s2h/GameInteractor/GameInteractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,33 @@ bool GameInteractor_Should(GIVanillaBehavior flag, uint32_t result, ...);
body; \
va_end(args); \
})
#define COND_HOOK(hookType, condition, body) \
{ \
static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHook<GameInteractor::hookType>(hookId); \
hookId = 0; \
if (condition) { \
hookId = GameInteractor::Instance->RegisterGameHook<GameInteractor::hookType>(body); \
} \
}
#define COND_ID_HOOK(hookType, id, condition, body) \
{ \
static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::hookType>(hookId); \
hookId = 0; \
if (condition) { \
hookId = GameInteractor::Instance->RegisterGameHookForID<GameInteractor::hookType>(id, body); \
} \
}
#define COND_VB_SHOULD(id, condition, body) \
{ \
static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::ShouldVanillaBehavior>(hookId); \
hookId = 0; \
if (condition) { \
hookId = REGISTER_VB_SHOULD(id, body); \
} \
}

int GameInteractor_InvertControl(GIInvertType type);
uint32_t GameInteractor_Dpad(GIDpadType type, uint32_t buttonCombo);
Expand Down
41 changes: 41 additions & 0 deletions mm/2s2h/ShipInit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef SHIP_INIT_HPP
#define SHIP_INIT_HPP

#ifdef __cplusplus

#include <vector>
#include <set>

struct ShipInit {
static std::unordered_map<std::string, std::vector<std::function<void()>>>& GetAll() {
static std::unordered_map<std::string, std::vector<std::function<void()>>> shipInitFuncs;
return shipInitFuncs;
}

static void InitAll() {
ShipInit::Init("*");
}

static void Init(const std::string& path) {
auto& shipInitFuncs = ShipInit::GetAll();
for (const auto& initFunc : shipInitFuncs[path]) {
initFunc();
}
}
};

struct RegisterShipInitFunc {
RegisterShipInitFunc(std::function<void()> initFunc, const std::set<std::string>& updatePaths = {}) {
auto& shipInitFuncs = ShipInit::GetAll();

shipInitFuncs["*"].push_back(initFunc);

for (const auto& path : updatePaths) {
shipInitFuncs[path].push_back(initFunc);
}
}
};

#endif // __cplusplus

#endif // SHIP_INIT_HPP

0 comments on commit 3917e3b

Please sign in to comment.