forked from SoyaX/Doorbell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alert.cs
124 lines (102 loc) · 3.75 KB
/
Alert.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.IO;
using System.Threading.Tasks;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Logging;
using NAudio.Wave;
using Newtonsoft.Json;
namespace Doorbell;
public class Alert {
public bool ChatEnabled = false;
public string ChatFormat = string.Empty;
public bool SoundEnabled = false;
public string SoundFile = string.Empty;
public float SoundVolume = 1;
[JsonIgnore] private WaveStream? audioFile;
[JsonIgnore] private WaveOutEvent? audioEvent;
public void DoAlert(PlayerObject player) {
PrintChat(player);
PlaySound();
}
public void PrintChat(PlayerObject player) {
if (!ChatEnabled) return;
var messageBuilder = new SeStringBuilder();
messageBuilder.AddText($"[{Plugin.Name}] ");
for (var i = 0; i < ChatFormat.Length; i++) {
if (ChatFormat[i] == '<') {
var tagEnd = ChatFormat.IndexOf('>', i + 1);
if (tagEnd > i) {
var tag = ChatFormat.Substring(i, tagEnd - i + 1);
switch (tag) {
case "<name>": {
messageBuilder.AddText(player.Name);
i = tagEnd;
continue;
}
case "<world>": {
messageBuilder.AddText(player.WorldName);
i = tagEnd;
continue;
}
case "<link>": {
messageBuilder.Add(new PlayerPayload(player.Name, player.World));
i = tagEnd;
continue;
}
}
}
}
messageBuilder.AddText($"{ChatFormat[i]}");
}
var chatMessage = $"[{Plugin.Name}] {ChatFormat}"
.Replace("<name>", player.Name);
var entry = new XivChatEntry() {
Message = messageBuilder.Build()
};
Plugin.Chat.Print(entry);
}
public void PlaySound() {
if (!SoundEnabled) return;
Task.Run(() => {
if (audioFile == null || audioEvent == null) SetupSound();
if (audioFile == null || audioEvent == null) return;
audioEvent.Stop();
audioFile.Position = 0;
audioEvent.Play();
});
}
public void SetupSound() {
DisposeSound();
try {
var file = SoundFile;
if (string.IsNullOrWhiteSpace(file))
file = Path.Join(Plugin.PluginInterface.AssemblyLocation.Directory!.FullName, "doorbell.wav");
if (file.IsHttpUrl()) {
audioFile = new MediaFoundationReader(file);
audioEvent = new WaveOutEvent();
audioEvent.Volume = MathF.Max(0, MathF.Min(1, SoundVolume));
audioEvent.Init(audioFile);
return;
}
if (!File.Exists(file)) {
Plugin.Log.Warning($"{file} does not exist.");
return;
}
audioFile = new AudioFileReader(file);
(audioFile as AudioFileReader)!.Volume = SoundVolume;
audioEvent = new WaveOutEvent();
audioEvent.Init(audioFile);
} catch (Exception ex) {
Plugin.Log.Error(ex, "Error initalizing sound.");
}
}
public void DisposeSound() {
audioFile?.Dispose();
audioEvent?.Dispose();
audioFile = null;
audioEvent = null;
}
}