From 283eb49f1707a3b121637aa8ce50972c7d112065 Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Wed, 12 Aug 2020 01:17:37 +0200 Subject: [PATCH] feat: add OnLogin and OnLogout events to ClientState --- Dalamud/Game/ClientState/ClientState.cs | 24 +++++++++++++++++++++++- Dalamud/Game/ClientState/Condition.cs | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Dalamud/Game/ClientState/ClientState.cs b/Dalamud/Game/ClientState/ClientState.cs index 8745931b8..c29e129a9 100644 --- a/Dalamud/Game/ClientState/ClientState.cs +++ b/Dalamud/Game/ClientState/ClientState.cs @@ -148,8 +148,30 @@ namespace Dalamud.Game.ClientState this.Actors.Dispose(); } + private bool lastConditionNone = true; + + /// + /// Event that fires when a character is logging in. + /// + public event EventHandler OnLogin; + + /// + /// Event that fires when a character is logging out. + /// + public event EventHandler OnLogout; + private void FrameworkOnOnUpdateEvent(Framework framework) { - // ignored + if (this.Condition.Any() && this.lastConditionNone == true) { + Log.Debug("Is login"); + this.lastConditionNone = false; + OnLogin?.Invoke(this, null); + } + + if (!this.Condition.Any() && this.lastConditionNone == false) { + Log.Debug("Is logout"); + this.lastConditionNone = true; + OnLogout?.Invoke(this, null); + } } } } diff --git a/Dalamud/Game/ClientState/Condition.cs b/Dalamud/Game/ClientState/Condition.cs index c886c6143..a1fc9226c 100644 --- a/Dalamud/Game/ClientState/Condition.cs +++ b/Dalamud/Game/ClientState/Condition.cs @@ -38,5 +38,24 @@ namespace Dalamud.Game.ClientState return *( bool* )( this.conditionArrayBase + idx ); } } + + public bool Any() { + var didAny = false; + + for (var i = 0; i < MaxConditionEntries; i++) + { + var typedCondition = (ConditionFlag)i; + var cond = this[typedCondition]; + + if (!cond) + { + continue; + } + + didAny = true; + } + + return didAny; + } } }