Skip to content

Commit

Permalink
update(gameevents): Listen to used ones
Browse files Browse the repository at this point in the history
  • Loading branch information
skuzzis committed Nov 21, 2024
1 parent 5dd675b commit fc100b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/engine/gameevents/gameevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,35 @@ void EventManager::Initialize()
void EventManager::RegisterGameEvents()
{
PLUGIN_PRINT("Game Events", "Loading game events...\n");
for (auto it = gameEventsRegister.begin(); it != gameEventsRegister.end(); ++it)
for(auto it = enqueueListenEvents.begin(); it != enqueueListenEvents.end(); ++it)
{
if (!g_gameEventManager->FindListener(this, it->first.c_str()))
g_gameEventManager->AddListener(this, it->first.c_str(), true);
std::string ev = *(it);
if (!g_gameEventManager->FindListener(this, ev.c_str()))
g_gameEventManager->AddListener(this, ev.c_str(), true);
}
PLUGIN_PRINT("Game Events", "Game events have been succesfully loaded.\n");
loadedGameEvents = true;
}

void EventManager::RegisterGameEventListen(std::string ev_name)
{
std::string raw_ev = "";

for(auto it = gameEventsRegister.begin(); it != gameEventsRegister.end(); ++it) {
if(it->second == ev_name) {
raw_ev = it->first;
break;
}
}

if(raw_ev == "") return;

if(!loadedGameEvents) {
if(enqueueListenEvents.find(raw_ev) == enqueueListenEvents.end()) enqueueListenEvents.insert(raw_ev);
} else {
if (!g_gameEventManager->FindListener(this, raw_ev.c_str()))
g_gameEventManager->AddListener(this, raw_ev.c_str(), true);
}
PLUGIN_PRINTF("Game Events", "%d game events have been succesfully loaded.\n", gameEventsRegister.size());
}

int EventManager::LoadEventsFromFile(const char* filePath, bool searchAll)
Expand Down
5 changes: 5 additions & 0 deletions src/engine/gameevents/gameevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
#include "../../common.h"
#include "../../entrypoint.h"
#include <utlvector.h>
#include <set>

typedef void (*FnEventListenerCallback)(IGameEvent* event);

class EventManager : public IGameEventListener2
{
private:
std::set<std::string> enqueueListenEvents;
bool loadedGameEvents = false;
public:
EventManager() = default;
~EventManager() = default;

void Initialize();
void Shutdown();
void RegisterGameEvents();
void RegisterGameEventListen(std::string ev_name);

void FireGameEvent(IGameEvent* pEvent) override;
bool OnFireEvent(IGameEvent* pEvent, bool bDontBroadcast);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/lua/LuaPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../core/scripting.h"
#include "../PluginManager.h"
#include "../../tools/crashreporter/CallStack.h"
#include "../../engine/gameevents/gameevents.h"

#include <vector>

Expand Down Expand Up @@ -230,6 +231,7 @@ void LuaPlugin::RegisterEventHandler(void* functionPtr)
void LuaPlugin::RegisterEventHandling(std::string eventName)
{
if (this->eventHandlers.find(eventName) == this->eventHandlers.end()) this->eventHandlers.insert(eventName);
eventManager->RegisterGameEventListen(eventName);
}

void LuaPlugin::UnregisterEventHandling(std::string eventName)
Expand Down

0 comments on commit fc100b1

Please sign in to comment.