forked from tesu/PennyPincher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PennyPincher.cs
273 lines (228 loc) · 9.85 KB
/
PennyPincher.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Dalamud.Data;
using Dalamud.Game;
using Dalamud.Game.ClientState.Keys;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.Game.Network;
using Dalamud.Game.Network.Structures;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Plugin;
using ImGuiNET;
using Lumina.Excel.GeneratedSheets;
using Num = System.Numerics;
namespace PennyPincher
{
public class PennyPincher : IDalamudPlugin
{
[PluginService] public static DalamudPluginInterface PluginInterface { get; private set; } = null!;
[PluginService] public static CommandManager CommandManager { get; private set; } = null!;
[PluginService] public static ChatGui Chat { get; private set; } = null!;
[PluginService] public static DataManager Data { get; private set; } = null!;
[PluginService] public static GameNetwork GameNetwork { get; private set; } = null!;
[PluginService] public static SigScanner SigScanner { get; private set; } = null!;
[PluginService] public static KeyState KeyState { get; private set; } = null!;
private const string commandName = "/penny";
private int configMin;
private int configMod;
private int configMultiple;
private int configDelta;
private bool configAlwaysOn;
private bool configHq;
private bool configVerbose;
private bool _config;
private Configuration configuration;
private Lumina.Excel.ExcelSheet<Item> items;
private bool newRequest;
private bool useHq;
private GetFilePointer getFilePtr;
private List<MarketBoardCurrentOfferings> _cache = new();
public PennyPincher()
{
var pluginConfig = PluginInterface.GetPluginConfig();
if (pluginConfig is Configuration)
{
configuration = (Configuration)pluginConfig;
}
else if (pluginConfig is OldConfiguration)
{
OldConfiguration oldConfig = (OldConfiguration)pluginConfig;
configuration = new Configuration
{
alwaysOn = oldConfig.alwaysOn,
delta = oldConfig.delta,
hq = oldConfig.alwaysHq,
min = oldConfig.min,
mod = oldConfig.mod,
multiple = oldConfig.multiple,
verbose = oldConfig.verbose
};
}
else
{
configuration = new Configuration();
}
LoadConfig();
items = Data.GetExcelSheet<Item>();
newRequest = false;
PluginInterface.UiBuilder.Draw += DrawWindow;
PluginInterface.UiBuilder.OpenConfigUi += OpenConfigUi;
CommandManager.AddHandler(commandName, new CommandInfo(Command)
{
HelpMessage = $"Opens the {Name} config menu",
});
GameNetwork.NetworkMessage += OnNetworkEvent;
try
{
var ptr = SigScanner.ScanText("E8 ?? ?? ?? ?? 48 85 C0 74 14 83 7B 44 00");
getFilePtr = Marshal.GetDelegateForFunctionPointer<GetFilePointer>(ptr);
}
catch (Exception e)
{
getFilePtr = null;
PluginLog.LogError(e.ToString());
}
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr GetFilePointer(byte index);
public string Name => "Penny Pincher";
public void Dispose()
{
GameNetwork.NetworkMessage -= OnNetworkEvent;
PluginInterface.UiBuilder.Draw -= DrawWindow;
PluginInterface.UiBuilder.OpenConfigUi -= OpenConfigUi;
CommandManager.RemoveHandler(commandName);
}
private void Command(string command, string arguments)
{
if (arguments == "hq")
{
configHq = !configHq;
Chat.Print("Penny Pincher HQ mode " + (configHq ? "enabled." : "disabled."));
SaveConfig();
}
else
{
_config = true;
}
}
private void OpenConfigUi()
{
_config = true;
}
private void DrawWindow()
{
if (!_config) return;
ImGui.SetNextWindowSize(new Num.Vector2(550, 270), ImGuiCond.FirstUseEver);
ImGui.Begin($"{Name} Config", ref _config);
ImGui.InputInt("Amount to undercut by", ref configDelta);
ImGui.InputInt("Minimum price to copy", ref configMin);
ImGui.InputInt("Modulo*", ref configMod);
ImGui.TextWrapped("*Subtracts an additional [<lowest price> %% <modulo>] from the price before applying the delta (no effect if modulo is 1).\nThis can be used to make the last digits of your copied prices consistent.");
ImGui.InputInt("Multiple†", ref configMultiple);
ImGui.TextWrapped("†Subtracts an additional [<lowest price> %% <multiple>] from the price after applying the delta (no effect if multiple is 1).\nThis can be used to undercut by multiples of an amount.");
ImGui.Separator();
ImGui.Checkbox($"Copy prices when opening all marketboards (instead of just retainer marketboards)", ref configAlwaysOn);
ImGui.Checkbox($"Undercut HQ prices", ref configHq);
ImGui.TextWrapped("Note that you can temporarily switch from HQ to NQ or vice versa by holding Shift when opening the marketboard");
ImGui.Checkbox($"Print chat message when prices are copied to clipboard", ref configVerbose);
ImGui.Separator();
if (ImGui.Button("Save and Close Config"))
{
SaveConfig();
_config = false;
}
ImGui.End();
}
private void LoadConfig()
{
configDelta = configuration.delta;
configMin = configuration.min;
configMod = configuration.mod;
configMultiple = configuration.multiple;
configAlwaysOn = configuration.alwaysOn;
configHq = configuration.hq;
configVerbose = configuration.verbose;
}
private void SaveConfig()
{
if (configMin < 1)
{
Chat.Print("Minimum price must be positive.");
configMin = 1;
}
if (configMod < 1)
{
Chat.Print("Modulo must be positive.");
configMod = 1;
}
if (configMultiple < 1)
{
Chat.Print("Multiple must be positive.");
configMod = 1;
}
configuration.delta = configDelta;
configuration.min = configMin;
configuration.mod = configMod;
configuration.multiple = configMultiple;
configuration.alwaysOn = configAlwaysOn;
configuration.hq = configHq;
configuration.verbose = configVerbose;
PluginInterface.SavePluginConfig(configuration);
}
private bool Retainer()
{
return (getFilePtr != null) && Marshal.ReadInt64(getFilePtr(7), 0xB0) != 0;
}
private void OnNetworkEvent(IntPtr dataPtr, ushort opCode, uint sourceActorId, uint targetActorId, NetworkMessageDirection direction)
{
if (direction != NetworkMessageDirection.ZoneDown) return;
if (!Data.IsDataReady) return;
if (opCode == Data.ServerOpCodes["MarketBoardItemRequestStart"])
{
newRequest = true;
// clear cache on new request so we can verify that we got all the data we need when we inspect the price
_cache.Clear();
var shiftHeld = KeyState[(byte)Dalamud.DrunkenToad.ModifierKey.Enum.VkShift];
useHq = shiftHeld ^ configuration.hq;
}
if (opCode != Data.ServerOpCodes["MarketBoardOfferings"] || !newRequest) return;
if (!configuration.alwaysOn && !Retainer()) return;
var listing = MarketBoardCurrentOfferings.Read(dataPtr);
// collect data for data integrity
_cache.Add(listing);
if (!IsDataValid(listing)) return;
var i = 0;
if (useHq && items.Single(j => j.RowId == listing.ItemListings[0].CatalogId).CanBeHq)
{
while (i < listing.ItemListings.Count && !listing.ItemListings[i].IsHq) i++;
if (i == listing.ItemListings.Count) return;
}
var price = listing.ItemListings[i].PricePerUnit - (listing.ItemListings[i].PricePerUnit % configuration.mod) - configuration.delta;
price -= (price % configuration.multiple);
price = Math.Max(price, configuration.min);
ImGui.SetClipboardText(price.ToString());
if (configuration.verbose)
{
Chat.Print((useHq ? "[HQ] " : string.Empty) + $"{price:n0} copied to clipboard.");
}
newRequest = false;
}
private bool IsDataValid(MarketBoardCurrentOfferings listing)
{
// handle early items / if the first request has less than 10
if (listing.ListingIndexStart == 0 && listing.ListingIndexEnd == 0)
{
return true;
}
// handle paged requests. 10 per request
var neededItems = listing.ListingIndexStart + listing.ItemListings.Count;
var actualItems = _cache.Sum(x => x.ItemListings.Count);
return (neededItems == actualItems);
}
}
}