forked from eq2reapp/ActWhatRune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhatRune.cs
454 lines (406 loc) · 17.8 KB
/
WhatRune.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Advanced_Combat_Tracker;
using System.Text;
namespace ACT_Plugin
{
/*
* See https://github.com/EQAditu/AdvancedCombatTracker/wiki/Plugin-Creation-Tips
*/
public class WhatRune : IActPluginV1
{
#if DEBUG
public static bool DEBUG = true;
#else
public static bool DEBUG = false;
#endif
public const int PLUGIN_ID = 91;
private const int DELAY_INIT_SECONDS = 2 * 1000;
private const string MACRO_FILENAME = "whatrune.txt";
public const string RUNES_DEFS = "https://raw.githubusercontent.com/eq2reapp/ActWhatRune/main/runes.txt";
public const string HELP_PAGE = "https://github.com/eq2reapp/ActWhatRune/wiki/Help";
public static string CONSIDER_TOKEN = "You consider";
public static Regex REGEX_CONSIDER = new Regex(@"^(\\#[ABCDEF0-9]{6})?" + CONSIDER_TOKEN);
public static string RUNE_TOKEN = "'rune ";
public static Regex REGEX_RUNE = new Regex(@"^Unknown command: " + RUNE_TOKEN);
private WhatRuneSettings _settings = null;
private TabPage _pluginScreenSpace = null;
private Label _pluginStatusText = null;
private Button _btnFetchDefs = null;
private TextBox _textBoxDefinitions = null;
private TextBox _textBoxMacro = null;
private TextBox _textBoxLogs = null;
private List<String> Logs = new List<string>();
private ConcurrentDictionary<string, string> Runes = new ConcurrentDictionary<string, string>();
private Timer _timerFetchRunes = new Timer();
public WhatRune()
{
_timerFetchRunes.Tick += _timerFetchRunes_Tick;
}
public void DeInitPlugin()
{
Log("Deinitializing plugin");
ActGlobals.oFormActMain.OnLogLineRead -= oFormActMain_OnLogLineRead;
ActGlobals.oFormActMain.UpdateCheckClicked -= OFormActMain_UpdateCheckClicked;
_pluginStatusText.Text = "Plugin stopped";
}
public void InitPlugin(TabPage pluginScreenSpace, Label pluginStatusText)
{
_settings = new WhatRuneSettings();
_pluginScreenSpace = pluginScreenSpace;
_pluginStatusText = pluginStatusText;
InitUI();
Log("Initializing plugin");
Log("Version = " + GetType().Assembly.GetName().Version);
ShowLog();
// Kick off a timer to do additional initialization
_timerFetchRunes.Interval = DELAY_INIT_SECONDS;
if (DEBUG)
{
_timerFetchRunes.Interval = 100;
}
_timerFetchRunes.Start();
try
{
// Update pattern for file download
// See: https://gist.github.com/EQAditu/4d6e3a1945fed2199f235fedc1e3ec56#Act_Plugin_Update.cs
ActGlobals.oFormActMain.UpdateCheckClicked += OFormActMain_UpdateCheckClicked;
if (ActGlobals.oFormActMain.GetAutomaticUpdatesAllowed())
{
new System.Threading.Thread(new System.Threading.ThreadStart(OFormActMain_UpdateCheckClicked)) { IsBackground = true }.Start();
}
}
catch (Exception ex)
{
Log("Error updating: " + ex.Message);
}
ActGlobals.oFormActMain.OnLogLineRead += oFormActMain_OnLogLineRead;
}
private void InitUI()
{
_pluginStatusText.Text = "Plugin started";
_pluginScreenSpace.Text = "WhatRune";
Panel pnlControls = new Panel();
pnlControls.Dock = DockStyle.Top;
int y = 5;
Label lblSource = new Label()
{
Text = "Custom definitions URL (leave empty to use default):",
AutoSize = true,
Top = y
};
pnlControls.Controls.Add(lblSource);
y = lblSource.Bottom;
_textBoxDefinitions = new TextBox()
{
Width = 400,
Top = y,
Left = 3,
Text = _settings.DefinitionsSource
};
_textBoxDefinitions.TextChanged -= _textBoxDefinitions_TextChanged;
_textBoxDefinitions.TextChanged += _textBoxDefinitions_TextChanged;
pnlControls.Controls.Add(_textBoxDefinitions);
y = _textBoxDefinitions.Bottom + 5;
Label lblMacro = new Label()
{
Text = "Macro contents (rune info will replace \"$R\"):",
AutoSize = true,
Top = y
};
pnlControls.Controls.Add(lblMacro);
y = lblMacro.Bottom;
_textBoxMacro = new TextBox()
{
ScrollBars = ScrollBars.Both,
Multiline = true,
Height = 75,
Width = 400,
Top = y,
Left = 3,
Text = _settings.MacroCommands
};
_textBoxMacro.TextChanged -= _textBoxMacro_TextChanged;
_textBoxMacro.TextChanged += _textBoxMacro_TextChanged;
pnlControls.Controls.Add(_textBoxMacro);
y = _textBoxMacro.Bottom + 5;
_btnFetchDefs = new Button()
{
Text = "Refresh Definitions",
AutoSize = true,
Top = y
};
_btnFetchDefs.Click -= _btnFetchDefs_Click;
_btnFetchDefs.Click += _btnFetchDefs_Click;
pnlControls.Controls.Add(_btnFetchDefs);
Button btnShowHelp = new Button()
{
Text = "Show Help",
AutoSize = true,
Top = y,
Left = _btnFetchDefs.Right + 5
};
btnShowHelp.Click -= _btnFetchDefs_Click;
btnShowHelp.Click += BtnShowHelp_Click;
pnlControls.Controls.Add(btnShowHelp);
y = _btnFetchDefs.Bottom + 5;
pnlControls.Height = y;
_textBoxLogs = new TextBox()
{
Dock = DockStyle.Fill,
ScrollBars = ScrollBars.Both,
Multiline = true,
ReadOnly = true
};
_pluginScreenSpace.Text = "WhatRune";
_pluginScreenSpace.Controls.Add(_textBoxLogs);
_pluginScreenSpace.Controls.Add(pnlControls);
}
private void BtnShowHelp_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(HELP_PAGE);
}
private void _textBoxDefinitions_TextChanged(object sender, EventArgs e)
{
_settings.DefinitionsSource = _textBoxDefinitions.Text;
_settings.SaveSettings();
}
private void _textBoxMacro_TextChanged(object sender, EventArgs e)
{
_settings.MacroCommands = _textBoxMacro.Text;
_settings.SaveSettings();
}
public void Log(string message)
{
// Need to lock the collection since both the parsing and UI threads can write to it
lock (Logs)
{
Logs.Add(String.Format("[{0}] {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
}
}
public void ShowLog()
{
// Update the UI with a thread-safe invocation
if (_pluginScreenSpace.InvokeRequired)
{
_pluginScreenSpace.Invoke(new Action(() => ShowLog()));
}
else
{
lock (Logs)
{
_textBoxLogs.Lines = Logs.ToArray();
}
_textBoxLogs.SelectionStart = _textBoxLogs.Text.Length;
_textBoxLogs.ScrollToCaret();
}
}
private async Task FetchRuneDefs()
{
Log("Fetching rune definitions...");
Runes.Clear();
try
{
string runeDefs = "";
using (HttpClient client = new HttpClient())
{
if (!string.IsNullOrEmpty(_settings.DefinitionsSource))
{
try
{
if (_settings.DefinitionsSource.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
{
runeDefs = await client.GetStringAsync(_settings.DefinitionsSource);
}
else
{
runeDefs = File.ReadAllText(_settings.DefinitionsSource);
}
}
catch (Exception fetchEx)
{
Log(fetchEx.Message);
Log($"Failed to get defs from {_settings.DefinitionsSource}, attempting fallback");
}
}
if (string.IsNullOrEmpty(runeDefs))
{
runeDefs = await client.GetStringAsync(RUNES_DEFS);
}
}
Log("Got rune data");
foreach (string line in runeDefs.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
{
if (!line.StartsWith("#"))
{
string[] defParts = line.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (defParts.Length > 0)
{
// The part of the string on the left of "=" can be a comma separated list of aliases,
// or in multi-name encounters the complete set of mob names
string[] mobNames = defParts[0].Trim().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string rune = "No specific rune";
if (defParts.Length >= 2)
{
// Let's make this part future-proof. We'll separate info bits using a token: ";"
string info = defParts[1];
string[] infoParts = info.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// The first part is what rune to wear
if (infoParts.Length >= 1)
{
rune = infoParts[0];
}
}
// If we get duplicate mobs, only add the first one
foreach (string mobName in mobNames)
{
string key = mobName.ToLower();
if (!Runes.ContainsKey(key))
{
Runes.TryAdd(key, rune);
}
}
}
}
}
Log($"Found {Runes.Count} definition(s)");
}
catch (Exception ex)
{
Log("Unable to get rune data: " + ex.Message);
}
ShowLog();
}
private void _btnFetchDefs_Click(object sender, EventArgs e)
{
_timerFetchRunes.Start();
}
private async void _timerFetchRunes_Tick(object sender, EventArgs e)
{
// Kill the timer, this is a singular event
_timerFetchRunes.Stop();
await FetchRuneDefs();
}
private void OFormActMain_UpdateCheckClicked()
{
if (PLUGIN_ID < int.MaxValue)
{
// This ID must be the same ID used on ACT's website.
int pluginId = PLUGIN_ID;
string pluginName = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;
try
{
Version localVersion = GetType().Assembly.GetName().Version;
Version remoteVersion = new Version(ActGlobals.oFormActMain.PluginGetRemoteVersion(pluginId).TrimStart(new char[] { 'v' }));
if (remoteVersion > localVersion)
{
DialogResult result = MessageBox.Show(
$"There is an updated version of the {pluginName} plugin. Update it now?\n\n(If there is an update to ACT, you should click No and update ACT first.)",
"New Version", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
FileInfo updatedFile = ActGlobals.oFormActMain.PluginDownload(pluginId);
ActPluginData pluginData = ActGlobals.oFormActMain.PluginGetSelfData(this);
pluginData.pluginFile.Delete();
updatedFile.MoveTo(pluginData.pluginFile.FullName);
// You can choose to simply restart the plugin, if the plugin can properly clean-up in DeInit
// and has no external assemblies that update.
ThreadInvokes.CheckboxSetChecked(ActGlobals.oFormActMain, pluginData.cbEnabled, false);
Application.DoEvents();
ThreadInvokes.CheckboxSetChecked(ActGlobals.oFormActMain, pluginData.cbEnabled, true);
}
}
}
catch (Exception ex)
{
ActGlobals.oFormActMain.WriteExceptionLog(ex, $"Plugin Update Check - {pluginName}");
}
}
}
// This is called each time ACT detects a new log line
private void oFormActMain_OnLogLineRead(bool isImport, LogLineEventArgs logInfo)
{
// Scan for consider messages, eg:
// \#FFFF40You consider High Shikari Olyxa... It looks tough -and it's a LOT tougher than it looks. Better have a lot of backup for this one!
// or, /rune MobName
// Unknown command: 'rune Mayong'
try
{
string mobName = null;
string logLine = logInfo.logLine;
int startPos = logLine.IndexOf("]");
if (startPos >= 0)
{
logLine = logLine.Substring(startPos + 2);
if (REGEX_CONSIDER.IsMatch(logLine))
{
startPos = logLine.IndexOf(CONSIDER_TOKEN) + CONSIDER_TOKEN.Length + 1;
int endPos = logLine.IndexOf("...", startPos);
if (endPos >= 0)
{
mobName = logLine.Substring(startPos, (endPos - startPos));
}
}
else if (REGEX_RUNE.IsMatch(logLine))
{
startPos = logLine.IndexOf(RUNE_TOKEN) + RUNE_TOKEN.Length;
int endPos = logLine.LastIndexOf("'");
if (endPos >= 0)
{
mobName = logLine.Substring(startPos, (endPos - startPos));
}
}
}
if (!string.IsNullOrEmpty(mobName))
{
Log("Considering " + mobName);
string runeInfo = "Unknown rune";
string key = mobName.ToLower();
if (Runes.ContainsKey(key))
{
runeInfo = Runes[key];
}
Log(" " + runeInfo);
ActGlobals.oFormActMain.TTS(runeInfo);
WriteMacroFile(runeInfo);
ShowLog();
}
}
catch { } // Black hole...
}
protected void WriteMacroFile(string runeInfo)
{
StringBuilder fileLines = new StringBuilder();
foreach (string line in _settings.MacroCommands.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
string fileLine = line.Trim();
if (!string.IsNullOrEmpty(fileLine))
{
if (line.StartsWith("/"))
{
fileLine = line.Substring(1);
}
fileLine = fileLine.Replace("$R", runeInfo);
fileLines.AppendLine(fileLine);
}
}
try
{
ActGlobals.oFormActMain.SendToMacroFile(MACRO_FILENAME, fileLines.ToString(), "");
Log("Wrote to macro file: " + Path.Combine(ActGlobals.oFormActMain.GameMacroFolder, MACRO_FILENAME));
}
catch (Exception ex)
{
Log("Failed to write macro file: " + Path.Combine(ActGlobals.oFormActMain.GameMacroFolder, MACRO_FILENAME));
Log(ex.Message);
}
}
}
}