forked from FPC-PM/NameplateSignResizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.cs
160 lines (140 loc) · 5.88 KB
/
Plugin.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
using System;
using System.Runtime.InteropServices;
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.Hooking;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Plugin;
using FFXIVClientStructs.FFXIV.Client.UI;
namespace NameplateSignResizer
{
public sealed class Plugin : IDalamudPlugin
{
public string Name => "NameplateSignResizer";
private const string configCommand = "/nsr";
private readonly int defaultXPos = 95;
private readonly int defaultYPos = 4;
[PluginService]
private DalamudPluginInterface PluginInterface { get; init; }
[PluginService]
private CommandManager CommandManager { get; init; }
private Configuration config { get; init; }
private PluginUI pluginUi { get; init; }
private Hook<SetNamePlateDelegate> setNamePlateHook;
internal PluginAddressResolver address;
[PluginService]
public SigScanner SigScanner { get; set; }
[PluginService]
public ClientState Client { get; init; }
[PluginService]
public GameGui GameGui { get; set; }
public IntPtr AddonNamePlatePtr => GameGui.GetAddonByName("NamePlate", 1);
public Plugin(
[RequiredVersion("1.0")] DalamudPluginInterface pluginInterface,
[RequiredVersion("1.0")] CommandManager commandManager)
{
this.PluginInterface = pluginInterface;
this.CommandManager = commandManager;
address = new PluginAddressResolver();
address.Setup(SigScanner);
setNamePlateHook = new Hook<SetNamePlateDelegate>(address.AddonNamePlate_SetNamePlatePtr, SetNamePlateDetour);
setNamePlateHook.Enable();
// Setup and initialize config UI.
this.config = this.PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
this.config.Initialize(this.PluginInterface);
this.pluginUi = new PluginUI(this.config);
// Add plugin commands
this.CommandManager.AddHandler(configCommand, new CommandInfo(OnConfig)
{
HelpMessage = "Open config window for Nameplate Sign Resizer"
});
// Draw UI
this.PluginInterface.UiBuilder.Draw += DrawUI;
this.PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
}
public void Dispose()
{
this.pluginUi.Dispose();
this.CommandManager.RemoveHandler(configCommand);
setNamePlateHook.Dispose();
}
/// <summary>
/// Open/close plugin settings window.
/// </summary>
/// <param name="command"></param>
/// <param name="args"></param>
private void OnConfig(string command, string args)
{
this.pluginUi.SettingsVisible = !this.pluginUi.SettingsVisible;
}
private unsafe void UpdateSign(AddonNamePlate.NamePlateObject npObject)
{
int xOffset = 0;
int yOffset = 0;
float scale = 1;
// Only apply config if plugin is enabled and nameplate is a non-pvp player nameplate.
if (config.enabled && npObject.NameplateKind == 0 && npObject.IsPvpEnemy == 0)
{
// Nameplate belongs to the local player or sync is enabled.
if (npObject.IsLocalPlayer || config.syncOthersWithSelf)
{
// Hide nameplate sign.
if (config.hideSignOnSelf)
scale = 0;
else
{
scale = config.ownSignScale;
xOffset = config.xOffset;
yOffset = config.yOffset;
}
}
else
{
if (config.hideSignOnOthers)
scale = 0;
else
{
scale = config.otherSignScale;
xOffset = config.xOffsetOthers;
yOffset = config.yOffsetOthers;
}
}
}
// Change icon size
npObject.ImageNode2->AtkResNode.SetScale(scale, scale);
// TODO: Auto position after scale change
// Offset icon position to compensate for scale change
//float posOffsetScale = scale < 1 ? scale : -scale;
npObject.ImageNode2->AtkResNode.SetPositionFloat(this.defaultXPos + xOffset, this.defaultYPos + yOffset);
}
private void DrawUI()
{
this.pluginUi.Draw();
}
private void DrawConfigUI()
{
this.pluginUi.SettingsVisible = true;
}
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)
{
var npObject = Marshal.PtrToStructure<AddonNamePlate.NamePlateObject>(namePlateObjectPtr);
this.UpdateSign(npObject);
return setNamePlateHook.Original(namePlateObjectPtr, isPrefixTitle, displayTitle, title, name, fcName, iconID);
}
}
}