-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.cs
More file actions
243 lines (212 loc) · 8.26 KB
/
Copy pathPlugin.cs
File metadata and controls
243 lines (212 loc) · 8.26 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using Dalamud.Game.Command;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin;
using GilDelta.Events;
using GilDelta.Events.Rules;
using GilDelta.Localization;
using GilDelta.Wallet;
using GilDelta.Windows;
using GilDelta.Windows.Dashboard;
using GilDelta.Windows.Widget;
namespace GilDelta;
public sealed class Plugin : IDalamudPlugin
{
public string Name => "GilDelta";
private readonly Configuration _config;
private readonly EventStore _store;
private readonly EventLog _log;
private readonly Inferrer _inferrer;
private readonly WindowSystem _windowSystem = new("GilDelta");
private readonly List<Wallet.WalletDiff> _recentDiffs = new();
private readonly AddonStateTracker _addonState = new();
private static readonly TimeSpan AddonRecencyWindow = TimeSpan.FromSeconds(2);
private WalletReader? _reader;
private WalletWatcher? _watcher;
private WidgetWindow? _widget;
private DashboardWindow? _dashboard;
private ConfigWindow? _configWindow;
public Plugin(IDalamudPluginInterface pi)
{
// Dalamud SDK 15: Inject is non-generic and takes an instance whose
// [PluginService] members (static or instance) it fills via reflection.
// The Service instance is throwaway because all properties are static.
pi.Inject(new Service());
_config = Service.PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
Strings.SetLanguage(ResolveLanguage(_config.Language));
_inferrer = new Inferrer(new IInferenceRule[]
{
new PairedTransferRule(),
new SubmarineReturnRule(),
new RetainerSaleRule(),
new NpcShopBuyRule(),
new NpcShopSellRule(),
new MarketBoardBuyRule(),
new RepairRule(),
new MiscRule(),
});
_store = new EventStore(EventStorePathFor(Service.PlayerState.ContentId));
_log = new EventLog();
_log.LoadFromStore(_store);
// Sample addon state every Framework tick. Subscribe BEFORE the
// WalletWatcher so the addon snapshot is fresh whenever a diff fires.
Service.Framework.Update += SampleAddonState;
_reader = new WalletReader();
_watcher = new WalletWatcher(Service.Framework, _reader);
_watcher.OnDiff += HandleDiff;
var renderers = new IWidgetRenderer[]
{
new MinimalRenderer(),
new CompactRenderer(),
new TrendRenderer(),
new TileRenderer(),
};
_widget = new WidgetWindow(_config, renderers, BuildWidgetContext);
_windowSystem.AddWindow(_widget);
var tabs = new IDashboardTab[]
{
new TimelineTab(),
new ChartTab(),
new BreakdownTab(),
new HeatmapTab(),
};
_dashboard = new DashboardWindow(_config, tabs, BuildDashboardContext);
_windowSystem.AddWindow(_dashboard);
_configWindow = new ConfigWindow(_config);
_windowSystem.AddWindow(_configWindow);
Service.ClientState.Login += OnLogin;
Service.ClientState.Logout += OnLogout;
Service.CommandManager.AddHandler("/gildelta", new CommandInfo(OnCommand)
{
HelpMessage = "Open GilDelta dashboard. /gildelta config opens settings.",
});
Service.CommandManager.AddHandler("/gd", new CommandInfo(OnCommand)
{
HelpMessage = "Alias for /gildelta.",
ShowInHelp = false,
});
Service.PluginInterface.UiBuilder.Draw += DrawUi;
Service.PluginInterface.UiBuilder.OpenConfigUi += OnOpenConfigUi;
Service.PluginInterface.UiBuilder.OpenMainUi += OnOpenMainUi;
}
private string EventStorePathFor(ulong contentId)
{
var dir = Path.Combine(Service.PluginInterface.GetPluginConfigDirectory(), "events");
Directory.CreateDirectory(dir);
return Path.Combine(dir, $"{contentId}.jsonl");
}
private static Language ResolveLanguage(int code)
{
if (code >= 0 && code <= 5) return (Language)code;
return Service.ClientState.ClientLanguage switch
{
Dalamud.Game.ClientLanguage.Japanese => Language.Japanese,
Dalamud.Game.ClientLanguage.German => Language.German,
Dalamud.Game.ClientLanguage.French => Language.French,
_ => Language.English,
};
}
private void OnLogin()
{
try
{
var newStore = new EventStore(EventStorePathFor(Service.PlayerState.ContentId));
_log.Clear();
_log.LoadFromStore(newStore);
}
catch (Exception e)
{
Service.Log.Warning(e, "OnLogin handler failed");
}
}
private void OnLogout(int type, int code)
{
Service.Log.Information($"GilDelta: logout type={type} code={code}");
}
private void OnCommand(string command, string args)
{
if (args.Trim().Equals("config", StringComparison.OrdinalIgnoreCase))
{
if (_configWindow is not null) _configWindow.IsOpen = !_configWindow.IsOpen;
}
else
{
if (_dashboard is not null) _dashboard.IsOpen = !_dashboard.IsOpen;
}
}
private void SampleAddonState(Dalamud.Plugin.Services.IFramework _)
{
try
{
_addonState.Tick(AddonProbe.OpenAddons(Service.GameGui));
}
catch
{
// GetAddonByName can throw if the game is mid-teardown; skip the tick.
}
}
private void HandleDiff(Wallet.WalletDiff diff)
{
// Use the rolling 2-second window of recently-seen addons so the rules
// still match even when the Shop / Teleport / Repair addon was closed
// by the time WalletWatcher detected the diff on the next tick.
var openAddons = _addonState.RecentlyOpen(AddonRecencyWindow);
var ctx = new GameContext(openAddons, _recentDiffs.ToArray(), DateTimeOffset.Now);
_recentDiffs.Add(diff);
if (_recentDiffs.Count > 32) _recentDiffs.RemoveAt(0);
var ev = _inferrer.Classify(diff, ctx);
// Diagnostic breadcrumb so misclassifications can be debugged from /xllog.
Service.Log.Information(
"Diff {Kind}({Id}) {Delta:+#,0;-#,0;0} -> {Cat}; recentAddons=[{Addons}]",
diff.Id.Kind, diff.Id.Identifier, diff.Delta, ev.Category,
string.Join(",", openAddons));
try
{
_store.Append(ev);
_log.Add(ev);
}
catch (Exception e)
{
Service.Log.Warning(e, "EventStore.Append failed");
}
}
private WidgetContext? BuildWidgetContext()
{
var snapshot = _watcher?.LastSnapshot;
if (snapshot is null || snapshot.Count == 0) return null;
return new WidgetContext(
wallets: snapshot,
recentEvents: _log.All,
now: DateTimeOffset.Now,
theme: Theme.MidnightCoin.Instance);
}
private DashboardContext? BuildDashboardContext()
{
var snapshot = _watcher?.LastSnapshot;
if (snapshot is null) return null;
return new DashboardContext(
wallets: snapshot,
events: _log.All,
now: DateTimeOffset.Now,
theme: Theme.MidnightCoin.Instance);
}
private void DrawUi() => _windowSystem.Draw();
private void OnOpenConfigUi() { if (_configWindow is not null) _configWindow.IsOpen = true; }
private void OnOpenMainUi() { if (_dashboard is not null) _dashboard.IsOpen = true; }
public void Dispose()
{
_watcher?.Dispose();
Service.Framework.Update -= SampleAddonState;
Service.ClientState.Login -= OnLogin;
Service.ClientState.Logout -= OnLogout;
Service.CommandManager.RemoveHandler("/gildelta");
Service.CommandManager.RemoveHandler("/gd");
Service.PluginInterface.UiBuilder.Draw -= DrawUi;
Service.PluginInterface.UiBuilder.OpenConfigUi -= OnOpenConfigUi;
Service.PluginInterface.UiBuilder.OpenMainUi -= OnOpenMainUi;
_windowSystem.RemoveAllWindows();
Service.PluginInterface.SavePluginConfig(_config);
}
}