forked from Haplo064/JobIcons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobIconsPlugin.cs
321 lines (265 loc) · 12 KB
/
JobIconsPlugin.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
using Dalamud.Game.Command;
using Dalamud.Hooking;
using Dalamud.Plugin;
using System;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Dalamud.Data;
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.Gui;
using Dalamud.Logging;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.Text.SeStringHandling;
namespace JobIcons
{
public class JobIconsPlugin : IDalamudPlugin
{
public string Name => "JobIcons";
private const string Command1 = "/jicons";
private const string Command2 = "/jobicons";
internal DalamudPluginInterface Interface;
internal JobIconsConfiguration Configuration;
public CommandManager CommandManager;
public Framework Framework;
public ChatGui ChatGui;
public SigScanner SigScanner;
public ClientState ClientState;
public ObjectTable ObjectTable;
public static DataManager DataManager;
public GameGui GameGui;
public Dalamud.Game.ClientState.Conditions.Condition Condition;
internal PluginAddressResolver Address;
internal JobIconsGui PluginGui;
private Hook<SetNamePlateDelegate> SetNamePlateHook;
private IntPtr EmptySeStringPtr;
private readonly OrderedDictionary LastKnownJobID = new OrderedDictionary();
private readonly IntPtr[] JobStr = new IntPtr[Enum.GetValues(typeof(Job)).Length];
//private readonly OrderedDictionary LastKnownJobID = new OrderedDictionary();
public JobIconsPlugin(DalamudPluginInterface pluginInterface,
//BuddyList buddies,
ChatGui chat,
//ChatHandlers chatHandlers,
ClientState clientState,
CommandManager commands,
Condition condition,
DataManager data,
//FateTable fates,
//FlyTextGui flyText,
Framework framework,
GameGui gameGui,
//GameNetwork gameNetwork,
//JobGauges gauges,
//KeyState keyState,
//LibcFunction libcFunction,
ObjectTable objects,
//PartyFinderGui pfGui,
//PartyList party,
//SeStringManager seStringManager,
SigScanner sigScanner,
TargetManager targets
//ToastGui toasts
)
{
Condition = condition;
DataManager = data;
ObjectTable = objects;
ClientState = clientState;
SigScanner = sigScanner;
ChatGui = chat;
Framework = framework;
CommandManager = commands;
Interface = pluginInterface;
GameGui = gameGui;
//Configuration.Init(this);
Configuration = Interface.GetPluginConfig() as JobIconsConfiguration ?? new JobIconsConfiguration();
//var testStr = XivApi.StringToSeStringPtr("TEst");
//PluginLog.LogInformation(XivApi.GetSeStringFromPtr(testStr).ToString());
//InitJobStr();
Address = new PluginAddressResolver();
Address.Setup(SigScanner);
XivApi.Initialize(this,Address);
IconSet.Initialize(this);
SetNamePlateHook = new Hook<SetNamePlateDelegate>(Address.AddonNamePlate_SetNamePlatePtr, SetNamePlateDetour);
SetNamePlateHook.Enable();
EmptySeStringPtr = XivApi.StringToSeStringPtr("");
InitJobStr();
var commandInfo = new CommandInfo(CommandHandler)
{
HelpMessage = "Opens Job Icons config.",
ShowInHelp = true
};
CommandManager.AddHandler(Command1, commandInfo);
CommandManager.AddHandler(Command2, commandInfo);
Task.Run(() => FixNamePlates(FixNonPlayerCharacterNamePlatesTokenSource.Token));
PluginGui = new JobIconsGui(this);
}
private void InitJobStr()
{
for (var index = 0; index < Enum.GetValues(typeof(Job)).Length; index++)
{
var jobName = ((Job)index).GetName();
JobStr[index] = XivApi.StringToSeStringPtr($"[{jobName}]");
//PluginLog.LogInformation(JobStr[index].ToString("X"));
//PluginLog.LogInformation(XivApi.GetSeStringFromPtr(JobStr[index]).ToString());
}
}
private void DisposeJobStr()
{
foreach (var seStrPtr in JobStr)
{
Marshal.FreeHGlobal(seStrPtr);
}
}
internal void SaveConfiguration() => Interface.SavePluginConfig(Configuration);
public void Dispose()
{
CommandManager.RemoveHandler(Command1);
CommandManager.RemoveHandler(Command2);
PluginGui.Dispose();
SetNamePlateHook.Disable();
SetNamePlateHook.Dispose();
XivApi.DisposeInstance();
FixNonPlayerCharacterNamePlatesTokenSource.Cancel();
Marshal.FreeHGlobal(EmptySeStringPtr);
DisposeJobStr();
}
private void CommandHandler(string command, string arguments) => PluginGui.ToggleConfigWindow();
#region fix non-pc nameplates
private readonly CancellationTokenSource FixNonPlayerCharacterNamePlatesTokenSource = new CancellationTokenSource();
private void FixNamePlates(CancellationToken token)
{
try
{
while (!token.IsCancellationRequested)
{
FixNamePlates();
Task.Delay(100, token).Wait(token);
}
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
PluginLog.Error(ex, "Non-PC Updater loop has crashed");
}
}
private void FixNamePlates()
{
var addon = XivApi.GetSafeAddonNamePlate();
for (int i = 0; i < 50; i++)
{
var npObject = addon.GetNamePlateObject(i);
if (npObject == null || !npObject.IsVisible)
continue;
var npInfo = npObject.NamePlateInfo;
if (npInfo == null)
continue;
var actorID = npInfo.Data.ObjectID.ObjectID;
if (actorID == 0xE0000000)
continue;
var isPC = npInfo.IsPlayerCharacter();
var isLocalPlayer = npObject.IsLocalPlayer;
var isPartyMember = npInfo.IsPartyMember();
var isAllianceMember = npInfo.IsAllianceMember();
var updateLocalPlayer = Configuration.SelfIcon && isLocalPlayer;
var updatePartyMember = Configuration.PartyIcons && isPartyMember;
var updateAllianceMember = Configuration.AllianceIcons && isAllianceMember;
var updateEveryoneElse = Configuration.EveryoneElseIcons && !isLocalPlayer && !isPartyMember && !isAllianceMember;
if (!isPC || !(updateLocalPlayer || updatePartyMember || updateAllianceMember || updateEveryoneElse))
npObject.SetIconScale(1);
}
}
#endregion
internal IntPtr SetNamePlateDetour(IntPtr namePlateObjectPtr, bool isPrefixTitle, bool displayTitle, IntPtr title, IntPtr name, IntPtr fcName, int iconID)
{
try
{
return SetNamePlate(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
}
catch (Exception ex)
{
PluginLog.Error(ex, $"SetNamePlateDetour encountered a critical error");
}
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
}
internal IntPtr SetNamePlate(IntPtr namePlateObjectPtr, bool isPrefixTitle, bool displayTitle, IntPtr title, IntPtr name, IntPtr fcName, int iconID)
{
if (!Configuration.Enabled)
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
var npObject = new XivApi.SafeNamePlateObject(namePlateObjectPtr);
if (npObject == null)
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
var npInfo = npObject.NamePlateInfo;
if (npInfo == null)
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
var actorID = npInfo.Data.ObjectID.ObjectID;
if (actorID == 0xE0000000)
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
if (!npObject.IsPlayer) // Only PlayerCharacters can have icons
{
npObject.SetIconScale(1);
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
}
var jobID = npInfo.GetJobID();
if (jobID < 1 || jobID >= Enum.GetValues(typeof(Job)).Length)
{
// This may not necessarily be needed anymore, but better safe than sorry.
var cache = LastKnownJobID[(object)actorID];
if (cache == null)
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
else
jobID = (uint)cache;
}
// Cache this actor's job
LastKnownJobID[(object)actorID] = jobID;
// Prune the pool a little.
while (LastKnownJobID.Count > 500)
LastKnownJobID.RemoveAt(0);
var isLocalPlayer = npObject.IsLocalPlayer;
var isPartyMember = npInfo.IsPartyMember();
var isAllianceMember = npInfo.IsAllianceMember();
var updateLocalPlayer = Configuration.SelfIcon && isLocalPlayer;
var updatePartyMember = Configuration.PartyIcons && isPartyMember;
var updateAllianceMember = Configuration.AllianceIcons && isAllianceMember;
var updateEveryoneElse = Configuration.EveryoneElseIcons && !isLocalPlayer && !isPartyMember && !isAllianceMember;
if (updateLocalPlayer || updatePartyMember || updateAllianceMember || updateEveryoneElse)
{
if (Configuration.ShowIcon)
{
var iconSet = Configuration.GetIconSet(jobID);
var scale = Configuration.Scale * iconSet.ScaleMultiplier;
if (iconID != 061545 && iconID != 061503/* && iconID != 061523*/)
iconID = iconSet.GetIconID(jobID);
npObject.SetIconScale(scale);
}
if (!Configuration.ShowName)
name = EmptySeStringPtr;
else
{
if (Configuration.JobName)
{
name = JobStr[jobID];
}
}
if (!Configuration.ShowTitle)
title = EmptySeStringPtr;
if (!Configuration.ShowFcName)
fcName = EmptySeStringPtr;
var result = SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
if (Configuration.LocationAdjust)
{
npObject.SetIconPosition(Configuration.XAdjust, Configuration.YAdjust);
}
return result;
}
else
{
npObject.SetIconScale(1);
return SetNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
}
}
}
}