From 7cfa58117dc1306efcd21b565d97e661944dffac Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Sat, 28 Dec 2024 01:31:13 -0600 Subject: [PATCH] Moved spawner to OnGameUpdate hook --- src/engine/fox_game.c | 13 ++++++------ src/port/hooks/impl/EventSystem.cpp | 31 ++++++++++++++++++++++------- src/port/hooks/impl/EventSystem.h | 14 ++++++++++--- src/port/hooks/list/EngineEvent.h | 5 ++++- src/port/mods/PortEnhancements.c | 13 +++++++++++- 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/engine/fox_game.c b/src/engine/fox_game.c index f982c97e..58295050 100644 --- a/src/engine/fox_game.c +++ b/src/engine/fox_game.c @@ -4,6 +4,7 @@ #include "assets/ast_logo.h" #include "mods.h" #include "port/interpolation/FrameInterpolation.h" +#include "port/hooks/list/EngineEvent.h" f32 gNextVsViewScale; f32 gVsViewScale; @@ -346,6 +347,9 @@ void Game_Update(void) { u8 partialFill; u8 soundMode; + // @port: @event: Call GAME_UPDATE_EVENT_PRE + EventSystem_CallEvent(GAME_UPDATE_EVENT_PRE, NULL); + Game_SetGameState(); if (gGameStandby) { Game_InitStandbyDL(&gUnkDisp1); @@ -600,12 +604,9 @@ void Game_Update(void) { gFillScreenRed, gFillScreenGreen, gFillScreenBlue, gFillScreenAlpha); } Audio_dummy_80016A50(); -#if MODS_RAM_MOD == 1 - RamMod_Update(); -#endif - if(CVarGetInteger("gSpawnerMod", 0) == 1){ - Spawner(); - } + + // @port: @event: Call GAME_UPDATE_EVENT_POST + EventSystem_CallEvent(GAME_UPDATE_EVENT_POST, NULL); } } diff --git a/src/port/hooks/impl/EventSystem.cpp b/src/port/hooks/impl/EventSystem.cpp index 44c58ec0..f2ce527c 100644 --- a/src/port/hooks/impl/EventSystem.cpp +++ b/src/port/hooks/impl/EventSystem.cpp @@ -3,12 +3,25 @@ EventSystem* EventSystem::Instance = new EventSystem(); -size_t EventSystem::RegisterListener(EventID id, EventPriority priority, EventCallback callback) { - if (std::find(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), callback) != this->mEventListeners[id].end()) { +ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, EventPriority priority) { + if(std::find_if(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), [callback](EventListener listener) { + return listener.function == callback; + }) != this->mEventListeners[id].end()) { throw std::runtime_error("Listener already registered"); } - this->mEventListeners[id].push_back(callback); + this->mEventListeners[id].push_back({ priority, callback }); + + // Sort by priority + std::sort(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), [](EventListener a, EventListener b) { + return a.priority < b.priority; + }); + + return this->mEventListeners[id].size() - 1; +} + +void EventSystem::UnregisterListener(EventID ev, ListenerID id) { + this->mEventListeners[ev].erase(this->mEventListeners[ev].begin() + id); } void EventSystem::CallEvent(EventID id, IEvent* event) { @@ -16,13 +29,17 @@ void EventSystem::CallEvent(EventID id, IEvent* event) { return; } - for (auto& callback : this->mEventListeners[id]) { - callback(event); + for (auto& listener : this->mEventListeners[id]) { + listener.function(event); } } -extern "C" size_t EventSystem_RegisterListener(EventID id, EventPriority priority, EventCallback callback) { - return EventSystem::Instance->RegisterListener(id, priority, callback); +extern "C" size_t EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority) { + return EventSystem::Instance->RegisterListener(id, callback, priority); +} + +extern "C" void EventSystem_UnregisterListener(EventID ev, size_t id) { + EventSystem::Instance->UnregisterListener(ev, id); } extern "C" void EventSystem_CallEvent(EventID id, void* event) { diff --git a/src/port/hooks/impl/EventSystem.h b/src/port/hooks/impl/EventSystem.h index df35545d..d7f47b73 100644 --- a/src/port/hooks/impl/EventSystem.h +++ b/src/port/hooks/impl/EventSystem.h @@ -5,6 +5,7 @@ #include typedef uint16_t EventID; +typedef uint32_t ListenerID; typedef enum { EVENT_TYPE_PRE, @@ -23,6 +24,11 @@ typedef struct { typedef void (*EventCallback)(IEvent*); +typedef struct { + EventPriority priority; + EventCallback function; +} EventListener; + // ID Type // 000000000000000 0 #define EVENT_ID(id, type) ((id << 1) | type) @@ -34,12 +40,14 @@ typedef void (*EventCallback)(IEvent*); class EventSystem { public: static EventSystem* Instance; - size_t RegisterListener(EventID id, EventPriority priority, EventCallback callback); + ListenerID RegisterListener(EventID id, EventCallback callback, EventPriority priority = EVENT_PRIORITY_NORMAL); + void UnregisterListener(EventID ev, ListenerID id); void CallEvent(EventID id, IEvent* event); private: - std::array, 0xFFFF> mEventListeners; + std::array, 0xFFFF> mEventListeners; }; #else -extern size_t EventSystem_RegisterListener(EventID id, EventPriority priority, EventCallback callback); +extern ListenerID EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority); +extern void EventSystem_UnregisterListener(EventID ev, ListenerID id); extern void EventSystem_CallEvent(EventID id, void* event); #endif \ No newline at end of file diff --git a/src/port/hooks/list/EngineEvent.h b/src/port/hooks/list/EngineEvent.h index 8491edc9..87e883f8 100644 --- a/src/port/hooks/list/EngineEvent.h +++ b/src/port/hooks/list/EngineEvent.h @@ -3,4 +3,7 @@ #include "port/hooks/impl/EventSystem.h" #define DISPLAY_UPDATE_EVENT_PRE EVENT_ID(1, EVENT_TYPE_PRE) -#define DISPLAY_UPDATE_EVENT_POST EVENT_ID(1, EVENT_TYPE_POST) \ No newline at end of file +#define DISPLAY_UPDATE_EVENT_POST EVENT_ID(1, EVENT_TYPE_POST) + +#define GAME_UPDATE_EVENT_PRE EVENT_ID(2, EVENT_TYPE_PRE) +#define GAME_UPDATE_EVENT_POST EVENT_ID(2, EVENT_TYPE_POST) \ No newline at end of file diff --git a/src/port/mods/PortEnhancements.c b/src/port/mods/PortEnhancements.c index 6175e027..638514cb 100644 --- a/src/port/mods/PortEnhancements.c +++ b/src/port/mods/PortEnhancements.c @@ -2,6 +2,7 @@ #include "port/hooks/list/EngineEvent.h" #include "global.h" #include "hit64.h" +#include "mods.h" void OnDisplayUpdatePre(IEvent* event) { #if DEBUG_BOSS_KILLER == 1 @@ -145,9 +146,19 @@ void OnDisplayUpdatePre(IEvent* event) { #endif } +void OnGameUpdatePost(IEvent* event) { +#if MODS_RAM_MOD == 1 + RamMod_Update(); +#endif + if(CVarGetInteger("gSpawnerMod", 0) == 1){ + Spawner(); + } +} + void PortEnhancements_Init() { // Register event listeners - EventSystem_RegisterListener(DISPLAY_UPDATE_EVENT_PRE, EVENT_PRIORITY_NORMAL, OnDisplayUpdatePre); + EventSystem_RegisterListener(DISPLAY_UPDATE_EVENT_PRE, OnDisplayUpdatePre, EVENT_PRIORITY_NORMAL); + EventSystem_RegisterListener(GAME_UPDATE_EVENT_POST, OnGameUpdatePost, EVENT_PRIORITY_NORMAL); } void PortEnhancements_Exit() {