2020-08-08 09:20:48 +00:00
|
|
|
|
using Dalamud.Game.Chat;
|
|
|
|
|
using Dalamud.Game.Chat.SeStringHandling;
|
|
|
|
|
using Dalamud.Game.Chat.SeStringHandling.Payloads;
|
|
|
|
|
using Dalamud.Game.ClientState.Actors.Types;
|
|
|
|
|
using Dalamud.Game.Internal;
|
|
|
|
|
using Dalamud.Plugin;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Media;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace PeepingTom {
|
|
|
|
|
class TargetWatcher : IDisposable {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
private readonly PeepingTomPlugin plugin;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-08-08 22:29:32 +00:00
|
|
|
|
private Stopwatch watch = null;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
private int lastTargetAmount = 0;
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private volatile bool stop = false;
|
|
|
|
|
private volatile bool needsUpdate = true;
|
|
|
|
|
private Thread thread;
|
|
|
|
|
|
|
|
|
|
private readonly object dataMutex = new object();
|
|
|
|
|
private TargetThreadData data;
|
|
|
|
|
|
2020-08-08 10:21:35 +00:00
|
|
|
|
private readonly Mutex currentMutex = new Mutex();
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private Targeter[] current = Array.Empty<Targeter>();
|
|
|
|
|
public IReadOnlyCollection<Targeter> CurrentTargeters {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
get {
|
|
|
|
|
this.currentMutex.WaitOne();
|
2020-08-08 20:23:51 +00:00
|
|
|
|
Targeter[] current = this.current.ToArray();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
this.currentMutex.ReleaseMutex();
|
|
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 10:21:35 +00:00
|
|
|
|
private readonly Mutex previousMutex = new Mutex();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
private readonly List<Targeter> previousTargeters = new List<Targeter>();
|
|
|
|
|
public IReadOnlyCollection<Targeter> PreviousTargeters {
|
|
|
|
|
get {
|
|
|
|
|
this.previousMutex.WaitOne();
|
|
|
|
|
Targeter[] previous = this.previousTargeters.ToArray();
|
|
|
|
|
this.previousMutex.ReleaseMutex();
|
|
|
|
|
return previous;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 10:05:23 +00:00
|
|
|
|
public TargetWatcher(PeepingTomPlugin plugin) {
|
|
|
|
|
this.plugin = plugin ?? throw new ArgumentNullException(nameof(plugin), "PeepingTomPlugin cannot be null");
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 10:21:57 +00:00
|
|
|
|
public void ClearPrevious() {
|
|
|
|
|
this.previousMutex.WaitOne();
|
|
|
|
|
this.previousTargeters.Clear();
|
|
|
|
|
this.previousMutex.ReleaseMutex();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
public void StartThread() {
|
|
|
|
|
this.thread = new Thread(new ThreadStart(() => {
|
|
|
|
|
while (!this.stop) {
|
|
|
|
|
this.Update();
|
|
|
|
|
this.needsUpdate = true;
|
|
|
|
|
Thread.Sleep(this.plugin.Config.PollFrequency);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
this.thread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WaitStopThread() {
|
|
|
|
|
this.stop = true;
|
|
|
|
|
this.thread?.Join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "delegate")]
|
2020-08-08 09:20:48 +00:00
|
|
|
|
public void OnFrameworkUpdate(Framework framework) {
|
2020-08-08 20:23:51 +00:00
|
|
|
|
if (!this.needsUpdate) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
lock (this.dataMutex) {
|
|
|
|
|
this.data = new TargetThreadData(this.plugin.Interface);
|
|
|
|
|
}
|
|
|
|
|
this.needsUpdate = false;
|
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private void Update() {
|
|
|
|
|
lock (this.dataMutex) {
|
|
|
|
|
if (this.data == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
PlayerCharacter player = this.data.localPlayer;
|
|
|
|
|
if (player == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// block until lease
|
|
|
|
|
this.currentMutex.WaitOne();
|
|
|
|
|
|
|
|
|
|
// get targeters and set a copy so we can release the mutex faster
|
|
|
|
|
Targeter[] current = this.GetTargeting(this.data.actors, player);
|
|
|
|
|
this.current = (Targeter[])current.Clone();
|
|
|
|
|
|
|
|
|
|
// release
|
|
|
|
|
this.currentMutex.ReleaseMutex();
|
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
|
|
|
|
this.HandleHistory(current);
|
|
|
|
|
|
|
|
|
|
// play sound if necessary
|
2020-08-08 10:05:23 +00:00
|
|
|
|
if (this.CanPlaySound()) {
|
2020-08-08 22:29:32 +00:00
|
|
|
|
this.watch.Restart();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
this.PlaySound();
|
|
|
|
|
}
|
|
|
|
|
this.lastTargetAmount = this.current.Length;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private void HandleHistory(Targeter[] targeting) {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
if (!this.plugin.Config.KeepHistory || (!this.plugin.Config.HistoryWhenClosed && !this.plugin.Ui.Visible)) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 10:21:44 +00:00
|
|
|
|
this.previousMutex.WaitOne();
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
foreach (Targeter targeter in targeting) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
// add the targeter to the previous list
|
|
|
|
|
if (this.previousTargeters.Any(old => old.ActorId == targeter.ActorId)) {
|
|
|
|
|
this.previousTargeters.RemoveAll(old => old.ActorId == targeter.ActorId);
|
|
|
|
|
}
|
2020-08-08 20:23:51 +00:00
|
|
|
|
this.previousTargeters.Insert(0, targeter);
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only keep the configured number of previous targeters (ignoring ones that are currently targeting)
|
2020-08-08 10:05:23 +00:00
|
|
|
|
while (this.previousTargeters.Where(old => targeting.All(actor => actor.ActorId != old.ActorId)).Count() > this.plugin.Config.NumHistory) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
this.previousTargeters.RemoveAt(this.previousTargeters.Count - 1);
|
|
|
|
|
}
|
2020-08-08 10:21:44 +00:00
|
|
|
|
|
|
|
|
|
this.previousMutex.ReleaseMutex();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private Targeter[] GetTargeting(Actor[] actors, Actor player) {
|
|
|
|
|
return actors
|
2020-08-08 09:20:48 +00:00
|
|
|
|
.Where(actor => actor.TargetActorID == player.ActorId && actor is PlayerCharacter)
|
|
|
|
|
.Select(actor => actor as PlayerCharacter)
|
2020-08-11 12:13:19 +00:00
|
|
|
|
.Where(actor => this.plugin.Config.LogParty || !InParty(actor))
|
|
|
|
|
.Where(actor => this.plugin.Config.LogAlliance || !InAlliance(actor))
|
|
|
|
|
.Where(actor => this.plugin.Config.LogInCombat || !InCombat(actor))
|
2020-08-08 10:05:23 +00:00
|
|
|
|
.Where(actor => this.plugin.Config.LogSelf || actor.ActorId != player.ActorId)
|
2020-08-08 20:23:51 +00:00
|
|
|
|
.Select(actor => new Targeter(actor))
|
2020-08-08 09:20:48 +00:00
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 12:13:19 +00:00
|
|
|
|
private static byte GetStatus(Actor actor) {
|
|
|
|
|
IntPtr statusPtr = actor.Address + 0x1906; // updated 5.3
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return Marshal.ReadByte(statusPtr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-11 12:13:19 +00:00
|
|
|
|
private static bool InCombat(Actor actor) => (GetStatus(actor) & 2) > 0;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-08-11 12:13:19 +00:00
|
|
|
|
private static bool InParty(Actor actor) => (GetStatus(actor) & 16) > 0;
|
2020-08-09 02:15:04 +00:00
|
|
|
|
|
2020-08-11 12:13:19 +00:00
|
|
|
|
private static bool InAlliance(Actor actor) => (GetStatus(actor) & 32) > 0;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
|
|
|
|
private bool CanPlaySound() {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
if (!this.plugin.Config.PlaySoundOnTarget) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.current.Length <= this.lastTargetAmount) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.plugin.Config.PlaySoundWhenClosed && !this.plugin.Ui.Visible) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 22:29:32 +00:00
|
|
|
|
if (this.watch == null) {
|
|
|
|
|
this.watch = new Stopwatch();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 22:29:32 +00:00
|
|
|
|
double secs = this.watch.Elapsed.TotalSeconds;
|
2020-08-08 10:05:23 +00:00
|
|
|
|
return secs >= this.plugin.Config.SoundCooldown;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaySound() {
|
|
|
|
|
SoundPlayer player;
|
2020-08-08 10:05:23 +00:00
|
|
|
|
if (this.plugin.Config.SoundPath == null) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
player = new SoundPlayer(Properties.Resources.Target);
|
|
|
|
|
} else {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
player = new SoundPlayer(this.plugin.Config.SoundPath);
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
using (player) {
|
|
|
|
|
try {
|
|
|
|
|
player.Play();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
this.SendError($"Could not play sound: {e.Message}");
|
|
|
|
|
} catch (InvalidOperationException e) {
|
|
|
|
|
this.SendError($"Could not play sound: {e.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendError(string message) {
|
|
|
|
|
Payload[] payloads = { new TextPayload($"[Who's Looking] {message}") };
|
2020-08-08 10:05:23 +00:00
|
|
|
|
this.plugin.Interface.Framework.Gui.Chat.PrintChat(new XivChatEntry {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
MessageBytes = new SeString(payloads).Encode(),
|
|
|
|
|
Type = XivChatType.ErrorMessage
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
this.currentMutex.Dispose();
|
|
|
|
|
this.previousMutex.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 20:23:51 +00:00
|
|
|
|
|
|
|
|
|
class TargetThreadData {
|
|
|
|
|
public PlayerCharacter localPlayer;
|
|
|
|
|
public Actor[] actors;
|
|
|
|
|
|
|
|
|
|
public TargetThreadData(DalamudPluginInterface pi) {
|
|
|
|
|
this.localPlayer = pi.ClientState.LocalPlayer;
|
|
|
|
|
this.actors = pi.ClientState.Actors.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|