Port over ShipInit from 2Ship (#4756)

* Port over ShipInit and transform Moon Jump as example

* Clean up moon jump

* Updated moon jump structure & cvar defines
This commit is contained in:
aMannus 2025-01-05 10:48:49 +01:00 committed by GitHub
parent f0b02d6c7e
commit f8fa4416de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 108 additions and 29 deletions

View File

@ -0,0 +1,26 @@
#include <libultraship/bridge.h>
#include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h"
#include "soh/ShipInit.hpp"
extern "C" {
#include "macros.h"
extern PlayState* gPlayState;
}
#define CVAR_MOON_JUMP_NAME "gCheats.MoonJumpOnL"
#define CVAR_MOON_JUMP_DEFAULT 0
#define CVAR_MOON_JUMP_VALUE CVarGetInteger(CVAR_MOON_JUMP_NAME, CVAR_MOON_JUMP_DEFAULT)
void OnPlayerUpdateMoonJump() {
Player* player = GET_PLAYER(gPlayState);
if (player != nullptr && CHECK_BTN_ANY(gPlayState->state.input[0].cur.button, BTN_L)) {
player->actor.velocity.y = 6.34375f;
}
}
void RegisterMoonJump() {
COND_HOOK(OnPlayerUpdate, CVAR_MOON_JUMP_VALUE, OnPlayerUpdateMoonJump);
}
static RegisterShipInitFunc initFunc(RegisterMoonJump, { CVAR_MOON_JUMP_NAME });

View File

@ -592,15 +592,43 @@ struct HookInfo {
#define GET_CURRENT_REGISTERING_INFO(type) HookRegisteringInfo{}
#endif
#define REGISTER_VB_SHOULD(flag, body) \
#define REGISTER_VB_SHOULD(flag, body) \
GameInteractor::Instance->RegisterGameHookForID<GameInteractor::OnVanillaBehavior>( \
flag, [](GIVanillaBehavior _, bool* should, va_list _originalArgs) { \
va_list args; \
va_copy(args, _originalArgs); \
body; \
va_end(args); \
flag, [](GIVanillaBehavior _, bool* should, va_list _originalArgs) { \
va_list args; \
va_copy(args, _originalArgs); \
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); \
} \
}
class GameInteractor {
public:
static GameInteractor* Instance;

View File

@ -146,21 +146,6 @@ void RegisterInfiniteNayrusLove() {
});
}
void RegisterMoonJumpOnL() {
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameFrameUpdate>([]() {
if (!GameInteractor::IsSaveLoaded(true)) return;
if (CVarGetInteger(CVAR_CHEAT("MoonJumpOnL"), 0) != 0) {
Player* player = GET_PLAYER(gPlayState);
if (CHECK_BTN_ANY(gPlayState->state.input[0].cur.button, BTN_L)) {
player->actor.velocity.y = 6.34375f;
}
}
});
}
void RegisterInfiniteISG() {
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameFrameUpdate>([]() {
if (!GameInteractor::IsSaveLoaded(true)) return;
@ -1473,7 +1458,6 @@ void InitMods() {
RegisterInfiniteAmmo();
RegisterInfiniteMagic();
RegisterInfiniteNayrusLove();
RegisterMoonJumpOnL();
RegisterInfiniteISG();
RegisterEzQPA();
RegisterUnrestrictedItems();

View File

@ -127,6 +127,7 @@ Sail* Sail::Instance;
#include "soh/resource/importer/BackgroundFactory.h"
#include "soh/config/ConfigUpdaters.h"
#include "soh/ShipInit.hpp"
extern "C" {
#include "src/overlays/actors/ovl_En_Dns/z_en_dns.h"
@ -1150,6 +1151,7 @@ extern "C" void InitOTR() {
conf->RunVersionUpdates();
SohGui::SetupGuiElements();
ShipInit::InitAll();
AudioCollection::Instance = new AudioCollection();
ActorDB::Instance = new ActorDB();
#ifdef __APPLE__

43
soh/soh/ShipInit.hpp Normal file
View File

@ -0,0 +1,43 @@
#ifndef SHIP_INIT_HPP
#define SHIP_INIT_HPP
#ifdef __cplusplus
#include <vector>
#include <set>
#include <unordered_map>
#include <functional>
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

View File

@ -239,6 +239,7 @@ namespace UIWidgets {
if (CustomCheckbox(text, &val, disabled, disabledGraphic)) {
CVarSetInteger(cvarName, val);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
ShipInit::Init(cvarName);
changed = true;
}
@ -298,6 +299,7 @@ namespace UIWidgets {
selected = i;
changed = true;
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
ShipInit::Init(cvarName);
}
}
}

View File

@ -1,10 +1,3 @@
//
// UIWidgets.hpp
// soh
//
// Created by David Chavez on 25.08.22.
//
#ifndef UIWidgets_hpp
#define UIWidgets_hpp
@ -17,6 +10,7 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <imgui.h>
#include "soh/ShipInit.hpp"
namespace UIWidgets {