-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetainerWatcher.cs
More file actions
264 lines (227 loc) · 9.65 KB
/
Copy pathRetainerWatcher.cs
File metadata and controls
264 lines (227 loc) · 9.65 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using Dalamud.Game.Addon.Lifecycle;
using Dalamud.Game.Addon.Lifecycle.AddonArgTypes;
using Dalamud.Game.Inventory;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game;
using Lumina.Excel.Sheets;
using Restocker.Data;
namespace Restocker;
/// <summary>
/// 「リテイナーが召喚されてマーケット出品リストが開いた瞬間」を検知し、
/// その時点のインベントリ+出品状態を <see cref="Configuration.Snapshots"/> に書き込む。
/// 受動収集(ユーザーが普通にリテイナーを開いただけで貯まる)。
/// </summary>
public sealed unsafe class RetainerWatcher : IDisposable
{
private readonly Configuration configuration;
private readonly IAddonLifecycle addonLifecycle;
private readonly IPlayerState playerState;
private readonly IObjectTable objectTable;
private readonly IDataManager dataManager;
private readonly IPluginLog log;
public RetainerWatcher(
Configuration configuration,
IAddonLifecycle addonLifecycle,
IPlayerState playerState,
IObjectTable objectTable,
IDataManager dataManager,
IPluginLog log)
{
this.configuration = configuration;
this.addonLifecycle = addonLifecycle;
this.playerState = playerState;
this.objectTable = objectTable;
this.dataManager = dataManager;
this.log = log;
addonLifecycle.RegisterListener(AddonEvent.PostRequestedUpdate, "RetainerSellList", OnSellListUpdate);
}
public void Dispose()
{
addonLifecycle.UnregisterListener(AddonEvent.PostRequestedUpdate, "RetainerSellList", OnSellListUpdate);
}
/// <summary>
/// RetainerSellList addon の PostRequestedUpdate(ゲーム側が再描画したタイミング)でスナップショットを取得。
/// PostSetup だと中身が埋まる前に発火することがあるため、Update の方が安定。
/// </summary>
private void OnSellListUpdate(AddonEvent type, AddonArgs args)
{
try
{
CaptureActiveRetainerSnapshot();
}
catch (Exception ex)
{
log.Error(ex, "[Restocker] failed to capture retainer snapshot");
}
}
public void CaptureActiveRetainerSnapshot()
{
var retainerManager = RetainerManager.Instance();
if (retainerManager == null || !retainerManager->IsReady) return;
var active = retainerManager->GetActiveRetainer();
if (active == null) return;
var characterId = playerState.ContentId;
if (characterId == 0) return;
var localPlayer = objectTable.LocalPlayer;
var snapshot = new RetainerSnapshot
{
CharacterContentId = characterId,
CharacterName = localPlayer?.Name.TextValue ?? string.Empty,
RetainerId = active->RetainerId,
RetainerName = active->NameString,
WorldId = localPlayer?.HomeWorld.RowId ?? 0,
LastRefreshedUtc = DateTime.UtcNow,
};
snapshot.Key = RetainerSnapshot.MakeKey(snapshot.CharacterContentId, snapshot.RetainerId);
snapshot.Listings = ReadListings();
snapshot.Inventory = ReadInventory();
configuration.Snapshots[snapshot.Key] = snapshot;
configuration.Save();
log.Debug($"[Restocker] snapshot updated: {snapshot.RetainerName} listings={snapshot.Listings.Count} inv={snapshot.Inventory.Count}");
}
private List<ListingEntry> ReadListings()
{
var result = new List<ListingEntry>();
var im = InventoryManager.Instance();
if (im == null) return result;
var container = im->GetInventoryContainer(InventoryType.RetainerMarket);
if (container == null || !container->IsLoaded) return result;
for (var i = 0; i < container->Size; i++)
{
var item = container->GetInventorySlot(i);
if (item == null || item->ItemId == 0) continue;
// 現在価格は InventoryManager.GetRetainerMarketPrice(slot) で直接取得
var price = (long)im->GetRetainerMarketPrice((short)i);
result.Add(new ListingEntry
{
ItemId = item->ItemId,
IsHQ = (item->Flags & InventoryItem.ItemFlags.HighQuality) != 0,
Quantity = (int)item->Quantity,
UnitPrice = price,
ListingIndex = item->Slot,
});
}
return result;
}
/// <summary>
/// 1 出品スロットあたりの最大個数。FFXIV のリテイナーマーケット側は事実上
/// 99/listing が上限なので、stack size がそれ以上の物(999 stack の crystal 等)も
/// 99 にクリップする。
/// </summary>
private static int ResolveMaxListingStack(Item row)
{
var inventoryStack = (int)row.StackSize;
if (inventoryStack <= 0) return 1;
return System.Math.Min(inventoryStack, 99);
}
private static readonly InventoryType[] RetainerInventoryTypes =
[
InventoryType.RetainerPage1, InventoryType.RetainerPage2, InventoryType.RetainerPage3,
InventoryType.RetainerPage4, InventoryType.RetainerPage5, InventoryType.RetainerPage6,
InventoryType.RetainerPage7,
];
private static readonly InventoryType[] CharacterBagTypes =
[
InventoryType.Inventory1, InventoryType.Inventory2,
InventoryType.Inventory3, InventoryType.Inventory4,
];
private static readonly InventoryType[] SaddlebagTypes =
[
InventoryType.SaddleBag1, InventoryType.SaddleBag2,
];
private static readonly InventoryType[] PremiumSaddlebagTypes =
[
InventoryType.PremiumSaddleBag1, InventoryType.PremiumSaddleBag2,
];
/// <summary>
/// キャラクター側のバッグ + サドルバッグ + プレミアムサドルを読み、
/// <see cref="Configuration.Characters"/> に保存する。
/// 召喚中リテイナーが居なくても呼べる(ベル開時など)。
/// </summary>
public void CaptureCharacterSnapshot()
{
var characterId = playerState.ContentId;
if (characterId == 0) return;
var localPlayer = objectTable.LocalPlayer;
var name = localPlayer?.Name.TextValue ?? string.Empty;
var snapshot = new CharacterSnapshot
{
CharacterContentId = characterId,
CharacterName = name,
Bag = ReadContainers(CharacterBagTypes),
Saddlebag = ReadContainers(SaddlebagTypes),
PremiumSaddlebag = ReadContainers(PremiumSaddlebagTypes),
LastRefreshedUtc = DateTime.UtcNow,
};
configuration.Characters[CharacterSnapshot.MakeKey(characterId)] = snapshot;
configuration.Save();
log.Debug($"[Restocker] character snapshot updated: bag={snapshot.Bag.Count} saddle={snapshot.Saddlebag.Count} premium={snapshot.PremiumSaddlebag.Count}");
}
private List<InventoryEntry> ReadContainers(InventoryType[] types)
{
var result = new List<InventoryEntry>();
var im = InventoryManager.Instance();
if (im == null) return result;
var itemSheet = dataManager.GetExcelSheet<Item>();
foreach (var t in types)
{
var container = im->GetInventoryContainer(t);
if (container == null || !container->IsLoaded) continue;
for (var i = 0; i < container->Size; i++)
{
var item = container->GetInventorySlot(i);
if (item == null || item->ItemId == 0) continue;
var entry = new InventoryEntry
{
ItemId = item->ItemId,
IsHQ = (item->Flags & InventoryItem.ItemFlags.HighQuality) != 0,
Quantity = (int)item->Quantity,
ContainerId = (uint)t,
SlotIndex = item->Slot,
};
if (itemSheet.TryGetRow(item->ItemId, out var row))
{
entry.MaxStackPerListing = ResolveMaxListingStack(row);
entry.IsListable = row.ItemSearchCategory.RowId != 0;
}
result.Add(entry);
}
}
return result;
}
private List<InventoryEntry> ReadInventory()
{
var result = new List<InventoryEntry>();
var im = InventoryManager.Instance();
if (im == null) return result;
var itemSheet = dataManager.GetExcelSheet<Item>();
foreach (var t in RetainerInventoryTypes)
{
var container = im->GetInventoryContainer(t);
if (container == null || !container->IsLoaded) continue;
for (var i = 0; i < container->Size; i++)
{
var item = container->GetInventorySlot(i);
if (item == null || item->ItemId == 0) continue;
var entry = new InventoryEntry
{
ItemId = item->ItemId,
IsHQ = (item->Flags & InventoryItem.ItemFlags.HighQuality) != 0,
Quantity = (int)item->Quantity,
ContainerId = (uint)t,
SlotIndex = item->Slot,
};
if (itemSheet.TryGetRow(item->ItemId, out var row))
{
entry.MaxStackPerListing = ResolveMaxListingStack(row);
// 出品可能フラグは ItemSearchCategory != 0 で近似(厳密には bind 状態など個別判定が要る)
entry.IsListable = row.ItemSearchCategory.RowId != 0;
}
result.Add(entry);
}
}
return result;
}
}