Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2 #5

Merged
merged 12 commits into from
Jul 29, 2023
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ add_library(Stealthometer SHARED
"src/Stealthometer.cpp"
"src/Stealthometer.h"
"src/Stats.h" "src/StatWindow.h" "src/StatWindow.cpp" "src/FixMinMax.h"
"src/Rating.h" "src/Rating.cpp" "src/PlayStyleRating.h" "src/util.h")
"src/Rating.h" "src/Rating.cpp" "src/PlayStyleRating.h" "src/util.h" "src/Events.h" "src/EventSystem.h" "src/EventSystem.cpp" "src/Enums.h")

target_link_libraries(Stealthometer PRIVATE
ZHMModSDK
Expand Down
422 changes: 422 additions & 0 deletions src/Enums.h

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions src/EventSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "EventSystem.h"

std::unordered_set<std::string> eventNameBlacklist = {
// Map-specific Perma Shortcut Events
"Bulldog_Ladder_A_Open",
"Bulldog_Ladder_B_Open",
"Dugong_Ladder_A_Down",
"Dugong_Ladder_B_Down",
"Edgy_Ladder_A_Down",
"Gecko_Ladder_A_Down",
"Gecko_Ladder_B_Down",
"Gecko_Ladder_C_Down",
"Rat_Ladder_A_Open",
// Freelancer Objectives
"Activate_BlindGuard",
"Activate_BlindTarget",
"Activate_Camera_Caught",
"Activate_Camera_DestroyRecorder",
"Activate_DartGun_Target",
"Activate_DisguiseBlown",
"Activate_Distract_Target",
"Activate_DontTakeDamage",
"Activate_EliminationPayout",
"Activate_HideTargetBodies",
"Activate_KillGuard_Sniper",
"Activate_KillGuard_SubMachineGun",
"Activate_KillMethod_Poison",
"Activate_KillMethod_Sniper",
"Activate_KillMethod_UnSilenced_Pistol",
"Activate_LimitedDisguise",
"Activate_No_Firearms",
"Activate_No_Witnesses",
"Activate_NoCombat",
"Activate_NoMissedShots",
"Activate_NoBodyFound",
"Activate_NotSpotted",
"Activate_PacifyGuard_Explosive",
"Activate_PoisonGuard_Any",
"Activate_PoisonGuard_Syringe",
"Activate_PoisonTarget_Emetic",
"Activate_PoisonTarget_Sedative",
"Activate_SA",
"Activate_SASO",
"Activate_SilentTakedown_3",
"Activate_Timed_SilientTakedown",
"DrActivate_EliminationPayout",
// Freelancer Challenge Events
"CollectorUpdate",
"GunmasterComplete",
"GunslingerUpdate",
"LetsGoHuntingUpdate",
"OneShotOneKillUpdate",
"SprayAndPrayUpdate",
"ThisIsMyRifleUpdate",
"UpCloseAndPersonalUpdate",
// Misc. Freelancer Events
"AddAssassin_Event",
"AddLookout_Event",
"CampaignInProgress",
"CompleteEvergreenPrimaryObj",
"Evergreen_EvaluateChallenge",
"Evergreen_Mastery_Level",
"Evergreen_Merces_Data",
"Evergreen_MissionCompleted_Hot",
"Evergreen_MissionPayout",
"Evergreen_Payout_Data",
"Evergreen_Safehouse_Stash_ItemChosen",
"Evergreen_SecurityCameraDestroyed",
"Evergreen_Stash_ItemChosen",
"Evergreen_Suspect_Looks",
"EvergreenCampaignActivated",
"EvergreenExitTriggered",
"EvergreenExitTriggeredOrWounded",
"EvergreenMissionEnd",
"GearSlotsTotal",
"GearSlotsTutorialised",
"GearSlotsUsed",
"MildMissionCompleted_Africa_Event",
"MildMissionCompleted_Asia_Event",
"MildMissionCompleted_Event",
"MissionCompleted_Event",
"NoCampaignActive",
"NoTargetsLeft",
"NumberOfTargets",
"PayoutObjective_Completed",
"ScoringScreenEndState_CampaignCompleted",
"ScoringScreenEndState_CampaignCompletedBonusXP_Professional",
"ScoringScreenEndState_CampaignCompletedBonusXP_Hard",
"ScoringScreenEndState_MildCompleted",
"SetPayout",
"Setup_TargetName",
"TravelDestination",
// Misc. Events
"ChallengeCompleted",
"ContractSessionMarker",
"CpdSet",
"Hero_Health",
"LeaderboardUpdated",
"Progression_XPGain",
"SegmentClosing",
"StartCpd",
// Maybe Useful Freelancer Events
"AddSuspectGlow",
//"Dart_Hit"
//"Evergreen_ShotMissed",
"Leader_In_Meeting",
"LeaderDeadEscaping_Event",
"LeaderEscaping",
"LeaderPacifiedEscaping_Event",
"RemoveSuspectGlow",
"SupplierVisited",
"TargetPickedConfirm",
};
132 changes: 132 additions & 0 deletions src/EventSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include "json.hpp"

enum class Events;
template<Events>
struct Event;

template<Events T>
class ServerEvent
{
public:
nlohmann::json json;
typename Event<T>::EventValue Value;
std::string ContractSessionId;
std::string ContractId;
std::string Name;
double Timestamp = 0;

public:
ServerEvent(typename Event<T>::EventValue&& value) : Value(std::forward<typename Event<T>::EventValue>(value))
{ }
};

extern std::unordered_set<std::string> eventNameBlacklist;

class EventListenersBase
{
protected:
auto virtual call(const nlohmann::json& ev) const -> bool = 0;

public:
auto operator()(const nlohmann::json& ev) const -> bool {
return this->call(ev);
}

auto handle(const nlohmann::json& ev) const -> bool {
return this->call(ev);
}
};

template<Events TEvent>
class EventListeners : public EventListenersBase
{
public:
using HandlerFunc = void(const ServerEvent<TEvent>&);

EventListeners()
{ }

template<typename TFunc>
auto add(TFunc&& func) -> void {
this->handlers.emplace_back(std::forward<TFunc>(func));
}

protected:
auto call(const nlohmann::json& ev) const -> bool {
auto it = ev.find("Value");
if (it == ev.end()) return false;

ServerEvent<TEvent> serverEvent{typename Event<TEvent>::EventValue(*it)};
serverEvent.json = ev;
serverEvent.Name = ev.value("Name", "");
serverEvent.ContractId = ev.value("ContractId", "");
serverEvent.ContractSessionId = ev.value("ContractSessionId", "");
serverEvent.Timestamp = ev.value("Timestamp", 0);

for (auto& handler : this->handlers)
handler(serverEvent);

return this->handlers.size() > 0;
}

private:
std::vector<std::function<HandlerFunc>> handlers;
};

class EventSystem {
public:
template<Events TEvent>
auto listen(std::function<void(const ServerEvent<TEvent>&)> handler) {
auto eventNameIt = this->eventNames.find(TEvent);
if (eventNameIt == this->eventNames.end())
eventNameIt = this->eventNames.emplace(TEvent, Event<TEvent>::Name).first;

auto it = this->listeners.find(eventNameIt->second);
if (it == this->listeners.end())
it = this->listeners.emplace(eventNameIt->second, std::make_unique<EventListeners<TEvent>>()).first;
static_cast<EventListeners<TEvent>*>(it->second.get())->add(handler);
}

auto handle(const std::string& str, const nlohmann::json& json) -> bool {
auto listeners = this->findListeners(str);
if (listeners) return listeners->handle(json);
return false;
}

auto handle(Events ev, const nlohmann::json& json) {
auto listeners = this->findListeners(ev);
if (listeners) return listeners->handle(json);
return false;
}

auto getEventName(Events ev) -> const std::string* {
auto it = this->eventNames.find(ev);
if (it != this->eventNames.end())
return &it->second;
return nullptr;
}

private:
auto findListeners(Events ev) -> EventListenersBase* {
auto name = this->getEventName(ev);
if (name) return this->findListeners(*name);
return nullptr;
}

auto findListeners(const std::string& name) -> EventListenersBase* {
auto it = this->listeners.find(name);
if (it != this->listeners.end())
return it->second.get();
return nullptr;
}

private:
std::unordered_map<Events, std::string> eventNames;
std::unordered_map<std::string, std::unique_ptr<EventListenersBase>> listeners;
};
Loading
Loading