-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACTOBSPlugin.cs
301 lines (276 loc) · 11.1 KB
/
ACTOBSPlugin.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Advanced_Combat_Tracker;
using OBSWebsocketDotNet;
using OBSWebsocketDotNet.Communication;
namespace ACTOBSPlugin
{
public class ACTOBSPlugin : IActPluginV1
{
private OBSWebsocket obs;
private LogLineEventDelegate LogLineDel;
private CombatToggleEventDelegate OnCombatEndDel;
private readonly PluginConfig config = new PluginConfig();
// We work around concurrency issues here by just using `ToList` on these collections whenever iterating over them
// Because changes to the structure are only initialized on init and on the UI thread, we don't need to lock
private readonly List<Regex> startRecordingRegexes = new List<Regex>();
private readonly List<Regex> stopRecordingRegexes = new List<Regex>();
private ConfigPanel configPanel;
private string lastVidFile = "";
private bool isConnected = false;
private bool isRecording = false;
private string GetPluginDirectory()
{
var plugin = ActGlobals.oFormActMain.ActPlugins.Where(x => x.pluginObj == this).FirstOrDefault();
if (plugin != null)
{
return Path.GetDirectoryName(plugin.pluginFile.FullName);
}
else
{
throw new Exception("Could not find ourselves in the plugin list!");
}
}
private void TryConnect()
{
if (config.Enabled)
{
try
{
obs.ConnectAsync(config.IPPort, config.Password);
}
catch (Exception ex)
{
ActGlobals.oFormActMain.BeginInvoke((MethodInvoker)delegate
{
MessageBox.Show("Connect failed : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
});
}
}
}
private void Disconnect()
{
obs.Disconnect();
}
private void Obs_Disconnected(object sender, ObsDisconnectionInfo e)
{
isConnected = false;
UpdateStatus();
Task.Delay(5000).ContinueWith(_ => {
TryConnect();
});
}
private void Obs_Connected(object sender, EventArgs e)
{
isConnected = true;
UpdateStatus();
}
public void DeInitPlugin()
{
obs.Connected -= Obs_Connected;
obs.Disconnected -= Obs_Disconnected;
Disconnect();
ActGlobals.oFormActMain.OnCombatEnd -= OnCombatEndDel;
ActGlobals.oFormActMain.BeforeLogLineRead -= LogLineDel;
config.Save();
}
private void RebuildRegexes(bool start, bool stop)
{
if (start)
{
startRecordingRegexes.Clear();
foreach (var txtRegex in config.StartRecording)
{
try
{
startRecordingRegexes.Add(new Regex(txtRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled));
}
catch (Exception e)
{
ActGlobals.oFormActMain.WriteExceptionLog(e, "Exception parsing regex " + txtRegex);
}
}
}
if (stop)
{
stopRecordingRegexes.Clear();
foreach (var txtRegex in config.StopRecording)
{
try
{
stopRecordingRegexes.Add(new Regex(txtRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled));
}
catch (Exception e)
{
ActGlobals.oFormActMain.WriteExceptionLog(e, "Exception parsing regex " + txtRegex);
}
}
}
}
public void InitPlugin(TabPage pluginScreenSpace, Label pluginStatusText)
{
var dir = GetPluginDirectory();
var autoLoadAssemblies = new string[] {
"Newtonsoft.Json.dll",
"System.Reactive.dll",
"System.Threading.Channels.dll",
"System.Threading.Tasks.Extensions.dll",
"Websocket.Client.dll",
"obs-websocket-dotnet.dll"
};
var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assemblyFilename in autoLoadAssemblies)
{
if (currentAssemblies.Any(a => {
if (a.IsDynamic) return false;
var manifest = a.ManifestModule;
if (manifest.ScopeName.Equals(assemblyFilename)) return true;
return false;
}))
{
continue;
}
AppDomain.CurrentDomain.Load(File.ReadAllBytes(Path.Combine(dir, assemblyFilename)));
}
pluginStatusText.Text = "Loading PluginConfig";
config.Load();
RebuildRegexes(true, true);
config.ConfigChanged += (_, args) => {
RebuildRegexes(args.StartRecordingChanged, args.StopRecordingChanged);
if (args.EnabledChanged)
{
if (config.Enabled)
{
TryConnect();
}
else
{
Disconnect();
}
}
};
pluginStatusText.Text = "Creating ConfigPanel";
pluginScreenSpace.Text = "ACT OBS Plugin";
configPanel = new ConfigPanel(config);
pluginScreenSpace.Controls.Add(configPanel);
pluginStatusText.Text = "In InitPlugin()";
PrivateInit();
}
private void PrivateInit()
{
obs = new OBSWebsocket();
obs.Connected += Obs_Connected;
obs.Disconnected += Obs_Disconnected;
TryConnect();
LogLineDel = (bool isImport, LogLineEventArgs logInfo) =>
{
try
{
var line = logInfo.originalLogLine;
if (obs.IsConnected)
{
if (!obs.GetRecordStatus().IsRecording)
{
foreach (var re in startRecordingRegexes)
{
if (re.IsMatch(line))
{
isRecording = true;
obs.StartRecord();
UpdateStatus();
return;
}
}
}
else
{
foreach (var re in stopRecordingRegexes)
{
if (re.IsMatch(line))
{
isRecording = false;
// Get this info before calling `StopRecord` because it could change due to delay in stopping recording process
var currentEnc = ActGlobals.oFormActMain.ActiveZone.ActiveEncounter;
var currentZone = ActGlobals.oFormActMain.ActiveZone.ZoneName;
var vidFile = obs.StopRecord();
if (config.AutoRename)
{
Task.Delay(5000).ContinueWith(_ =>
{
var encTitle = currentEnc.Title;
var baseFilename = Path.GetFileNameWithoutExtension(vidFile);
var zoneEnc = string.Join("_", (currentZone + "_" + encTitle).Split(Path.GetInvalidFileNameChars(), StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
var extension = Path.GetExtension(vidFile);
var newFileName = baseFilename + "_" + zoneEnc + extension;
var renamedFile = Path.Combine(
Path.GetDirectoryName(vidFile),
newFileName
);
File.Move(vidFile, renamedFile);
lastVidFile = newFileName;
UpdateStatus();
});
}
else
{
UpdateStatus();
}
return;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
};
OnCombatEndDel = (_, encounterInfo) =>
{
return;
};
ActGlobals.oFormActMain.OnCombatEnd += OnCombatEndDel;
ActGlobals.oFormActMain.BeforeLogLineRead += LogLineDel;
}
private void UpdateStatus()
{
var status = "";
if (isConnected)
{
status += "Connected, ";
if (isRecording)
{
status += "Recording";
}
else
{
status += "Not Recording";
}
}
else
{
status += "Disconnected, recording status unknown";
}
if (ActGlobals.oFormActMain.InvokeRequired)
{
ActGlobals.oFormActMain.Invoke((Action)(() => {
configPanel.SetStatus(status);
configPanel.SetLastFile(lastVidFile);
}));
}
else
{
configPanel.SetStatus(status);
configPanel.SetLastFile(lastVidFile);
}
}
}
}