2021-08-23 03:33:57 +00:00
|
|
|
|
using NAudio.Wave;
|
2020-12-29 16:08:21 +00:00
|
|
|
|
using Resourcer;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
2021-08-23 03:33:57 +00:00
|
|
|
|
using Dalamud.Game;
|
|
|
|
|
using Dalamud.Game.ClientState.Objects.SubKinds;
|
|
|
|
|
using Dalamud.Game.ClientState.Objects.Types;
|
2021-04-05 19:13:41 +00:00
|
|
|
|
using Dalamud.Game.Text;
|
2021-09-11 17:22:41 +00:00
|
|
|
|
using Dalamud.Logging;
|
2021-08-23 03:33:57 +00:00
|
|
|
|
using PeepingTom.Ipc;
|
2021-04-28 14:41:34 +00:00
|
|
|
|
using PeepingTom.Resources;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
|
|
|
|
namespace PeepingTom {
|
2020-12-29 16:08:21 +00:00
|
|
|
|
internal class TargetWatcher : IDisposable {
|
|
|
|
|
private PeepingTomPlugin Plugin { get; }
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private Stopwatch UpdateWatch { get; } = new();
|
|
|
|
|
private Stopwatch? SoundWatch { get; set; }
|
2020-12-29 16:08:21 +00:00
|
|
|
|
private int LastTargetAmount { get; set; }
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-12-29 16:08:21 +00:00
|
|
|
|
private Targeter[] Current { get; set; } = Array.Empty<Targeter>();
|
2020-08-08 20:23:51 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
public IReadOnlyCollection<Targeter> CurrentTargeters => this.Current;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2021-02-01 18:21:44 +00:00
|
|
|
|
private List<Targeter> Previous { get; } = new();
|
2020-12-29 16:08:21 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
public IReadOnlyCollection<Targeter> PreviousTargeters => this.Previous;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-08-08 10:05:23 +00:00
|
|
|
|
public TargetWatcher(PeepingTomPlugin plugin) {
|
2021-04-28 14:41:34 +00:00
|
|
|
|
this.Plugin = plugin;
|
2021-08-23 03:33:57 +00:00
|
|
|
|
this.UpdateWatch.Start();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
this.Plugin.Framework.Update += this.OnFrameworkUpdate;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
public void Dispose() {
|
|
|
|
|
this.Plugin.Framework.Update -= this.OnFrameworkUpdate;
|
2020-08-08 20:23:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
public void ClearPrevious() {
|
|
|
|
|
this.Previous.Clear();
|
2020-08-08 20:23:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private void OnFrameworkUpdate(Framework framework) {
|
|
|
|
|
if (this.Plugin.InPvp) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
if (this.UpdateWatch.Elapsed > TimeSpan.FromMilliseconds(this.Plugin.Config.PollFrequency)) {
|
|
|
|
|
this.Update();
|
2020-08-08 20:23:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private void Update() {
|
2021-08-23 03:33:57 +00:00
|
|
|
|
var player = this.Plugin.ClientState.LocalPlayer;
|
|
|
|
|
if (player == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-08 20:23:51 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
// get targeters and set a copy so we can release the mutex faster
|
|
|
|
|
var newCurrent = this.GetTargeting(this.Plugin.ObjectTable, player);
|
2020-08-08 20:23:51 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
foreach (var newTargeter in newCurrent.Where(t => this.Current.All(c => c.ObjectId != t.ObjectId))) {
|
2021-09-11 17:22:41 +00:00
|
|
|
|
try {
|
|
|
|
|
this.Plugin.IpcManager.SendNewTargeter(newTargeter);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
PluginLog.LogError(ex, "Failed to send IPC message");
|
|
|
|
|
}
|
2021-08-23 03:33:57 +00:00
|
|
|
|
}
|
2020-08-08 20:23:51 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
foreach (var stopped in this.Current.Where(t => newCurrent.All(c => c.ObjectId != t.ObjectId))) {
|
2021-09-11 17:22:41 +00:00
|
|
|
|
try {
|
|
|
|
|
this.Plugin.IpcManager.SendStoppedTargeting(stopped);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
PluginLog.LogError(ex, "Failed to send IPC message");
|
|
|
|
|
}
|
2020-08-08 20:23:51 +00:00
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
this.Current = newCurrent;
|
|
|
|
|
|
2020-12-29 16:08:21 +00:00
|
|
|
|
this.HandleHistory(this.Current);
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
|
|
|
|
// play sound if necessary
|
2020-08-08 10:05:23 +00:00
|
|
|
|
if (this.CanPlaySound()) {
|
2021-08-23 03:33:57 +00:00
|
|
|
|
this.SoundWatch?.Restart();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
this.PlaySound();
|
|
|
|
|
}
|
2020-12-29 16:08:21 +00:00
|
|
|
|
|
|
|
|
|
this.LastTargetAmount = this.Current.Length;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 20:23:51 +00:00
|
|
|
|
private void HandleHistory(Targeter[] targeting) {
|
2021-01-22 19:27:44 +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-12-29 16:08:21 +00:00
|
|
|
|
foreach (var targeter in targeting) {
|
2020-08-08 09:20:48 +00:00
|
|
|
|
// add the targeter to the previous list
|
2021-08-23 03:33:57 +00:00
|
|
|
|
if (this.Previous.Any(old => old.ObjectId == targeter.ObjectId)) {
|
|
|
|
|
this.Previous.RemoveAll(old => old.ObjectId == targeter.ObjectId);
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
2020-12-29 16:08:21 +00:00
|
|
|
|
|
|
|
|
|
this.Previous.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)
|
2021-08-23 03:33:57 +00:00
|
|
|
|
while (this.Previous.Count(old => targeting.All(actor => actor.ObjectId != old.ObjectId)) > this.Plugin.Config.NumHistory) {
|
2020-12-29 16:08:21 +00:00
|
|
|
|
this.Previous.RemoveAt(this.Previous.Count - 1);
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private Targeter[] GetTargeting(IEnumerable<GameObject> objects, GameObject player) {
|
|
|
|
|
return objects
|
|
|
|
|
.Where(obj => obj.TargetObjectId == player.ObjectId && obj is PlayerCharacter)
|
|
|
|
|
// .Where(obj => Marshal.ReadByte(obj.Address + ActorOffsets.PlayerCharacterTargetActorId + 4) == 0)
|
2020-12-29 16:08:21 +00:00
|
|
|
|
.Cast<PlayerCharacter>()
|
|
|
|
|
.Where(actor => this.Plugin.Config.LogParty || !InParty(actor))
|
|
|
|
|
.Where(actor => this.Plugin.Config.LogAlliance || !InAlliance(actor))
|
|
|
|
|
.Where(actor => this.Plugin.Config.LogInCombat || !InCombat(actor))
|
2021-08-23 03:33:57 +00:00
|
|
|
|
.Where(actor => this.Plugin.Config.LogSelf || actor.ObjectId != player.ObjectId)
|
2020-08-08 20:23:51 +00:00
|
|
|
|
.Select(actor => new Targeter(actor))
|
2020-08-08 09:20:48 +00:00
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private static byte GetStatus(GameObject actor) {
|
2021-01-22 19:27:44 +00:00
|
|
|
|
var statusPtr = actor.Address + 0x1980; // updated 5.4
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return Marshal.ReadByte(statusPtr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private static bool InCombat(GameObject actor) => (GetStatus(actor) & 2) > 0;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private static bool InParty(GameObject actor) => (GetStatus(actor) & 16) > 0;
|
2020-08-09 02:15:04 +00:00
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
private static bool InAlliance(GameObject actor) => (GetStatus(actor) & 32) > 0;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
|
|
|
|
|
private bool CanPlaySound() {
|
2020-12-29 16:08:21 +00:00
|
|
|
|
if (!this.Plugin.Config.PlaySoundOnTarget) {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 16:08:21 +00:00
|
|
|
|
if (this.Current.Length <= this.LastTargetAmount) {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 16:08:21 +00:00
|
|
|
|
if (!this.Plugin.Config.PlaySoundWhenClosed && !this.Plugin.Ui.Visible) {
|
2020-08-08 10:05:23 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
if (this.SoundWatch == null) {
|
|
|
|
|
this.SoundWatch = new Stopwatch();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 03:33:57 +00:00
|
|
|
|
var secs = this.SoundWatch.Elapsed.TotalSeconds;
|
2020-12-29 16:08:21 +00:00
|
|
|
|
return secs >= this.Plugin.Config.SoundCooldown;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaySound() {
|
2022-08-21 19:30:25 +00:00
|
|
|
|
var soundDevice = DirectSoundOut.Devices.FirstOrDefault(d => d.Guid == this.Plugin.Config.SoundDeviceNew);
|
|
|
|
|
if (soundDevice == null) {
|
|
|
|
|
return;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
2020-08-24 17:13:42 +00:00
|
|
|
|
|
2020-12-29 16:08:21 +00:00
|
|
|
|
new Thread(() => {
|
2020-08-24 17:13:42 +00:00
|
|
|
|
WaveStream reader;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
try {
|
2020-12-29 16:08:21 +00:00
|
|
|
|
if (this.Plugin.Config.SoundPath == null) {
|
|
|
|
|
reader = new WaveFileReader(Resource.AsStream("Resources/target.wav"));
|
2020-08-24 17:13:42 +00:00
|
|
|
|
} else {
|
2022-08-21 19:30:25 +00:00
|
|
|
|
reader = new MediaFoundationReader(this.Plugin.Config.SoundPath);
|
2020-08-24 17:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2021-04-28 14:41:34 +00:00
|
|
|
|
var error = string.Format(Language.SoundChatError, e.Message);
|
|
|
|
|
this.SendError(error);
|
2020-08-24 17:13:42 +00:00
|
|
|
|
return;
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
2020-08-24 17:13:42 +00:00
|
|
|
|
|
2021-04-15 17:05:24 +00:00
|
|
|
|
using var channel = new WaveChannel32(reader) {
|
2020-12-29 16:11:32 +00:00
|
|
|
|
Volume = this.Plugin.Config.SoundVolume,
|
2020-12-30 19:20:42 +00:00
|
|
|
|
PadWithZeroes = false,
|
2020-12-29 16:11:32 +00:00
|
|
|
|
};
|
2020-08-25 18:06:18 +00:00
|
|
|
|
|
2020-08-24 17:13:42 +00:00
|
|
|
|
using (reader) {
|
2022-08-21 19:30:25 +00:00
|
|
|
|
using var output = new DirectSoundOut(soundDevice.Guid);
|
|
|
|
|
|
2021-11-21 04:13:12 +00:00
|
|
|
|
try {
|
|
|
|
|
output.Init(channel);
|
|
|
|
|
output.Play();
|
|
|
|
|
|
|
|
|
|
while (output.PlaybackState == PlaybackState.Playing) {
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
PluginLog.LogError(ex, "Exception playing sound");
|
2020-08-24 17:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-29 16:08:21 +00:00
|
|
|
|
}).Start();
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendError(string message) {
|
2021-08-23 03:33:57 +00:00
|
|
|
|
this.Plugin.ChatGui.PrintChat(new XivChatEntry {
|
|
|
|
|
Message = $"[{this.Plugin.Name}] {message}",
|
2020-12-30 19:20:42 +00:00
|
|
|
|
Type = XivChatType.ErrorMessage,
|
|
|
|
|
});
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|
2020-08-08 20:23:51 +00:00
|
|
|
|
}
|
2020-08-08 09:20:48 +00:00
|
|
|
|
}
|