-
Notifications
You must be signed in to change notification settings - Fork 16
/
Plugin.cs
325 lines (256 loc) · 12.1 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
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
322
323
324
325
#undef DEBUG
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Dalamud.Game.ClientState.Actors;
using Dalamud.Game.ClientState.Structs;
using Dalamud.Plugin;
using Dalamud.Hooking;
using OopsAllLalafells.Attributes;
namespace OopsAllLalafells {
public class Plugin : IDalamudPlugin {
private const uint CHARA_WINDOW_ACTOR_ID = 0xE0000000;
private const int OFFSET_RENDER_TOGGLE = 0x104;
private static readonly short[,] RACE_STARTER_GEAR_ID_MAP = {
{84, 85}, // Hyur
{86, 87}, // Elezen
{92, 93}, // Lalafell
{88, 89}, // Miqo
{90, 91}, // Roe
{257, 258}, // Au Ra
{597, -1}, // Hrothgar
{-1, 581}, // Viera
};
private static readonly short[] RACE_STARTER_GEAR_IDS;
public string Name => "Oops, All Lalafells!";
private DalamudPluginInterface pluginInterface;
public Configuration config { get; private set; }
private bool unsavedConfigChanges = false;
private PluginUI ui;
public bool SettingsVisible = false;
private PluginCommandManager<Plugin> commandManager;
private delegate IntPtr CharacterIsMounted(IntPtr actor);
private delegate IntPtr CharacterInitialize(IntPtr actorPtr, IntPtr customizeDataPtr);
private delegate IntPtr FlagSlotUpdate(IntPtr actorPtr, uint slot, IntPtr equipData);
private Hook<CharacterIsMounted> charaMountedHook;
private Hook<CharacterInitialize> charaInitHook;
private Hook<FlagSlotUpdate> flagSlotUpdateHook;
private IntPtr lastActor;
private bool lastWasPlayer;
private bool lastWasModified;
private Race lastPlayerRace;
private byte lastPlayerGender;
// This sucks, but here we are
static Plugin() {
var list = new List<short>();
foreach (short id in RACE_STARTER_GEAR_ID_MAP) {
if (id != -1) {
list.Add(id);
}
}
RACE_STARTER_GEAR_IDS = list.ToArray();
}
public void Initialize(DalamudPluginInterface pluginInterface) {
this.pluginInterface = pluginInterface;
this.config = (Configuration)this.pluginInterface.GetPluginConfig() ?? new Configuration();
this.config.Initialize(pluginInterface);
this.ui = new PluginUI(this);
this.pluginInterface.UiBuilder.OnBuildUi += this.ui.Draw;
this.pluginInterface.UiBuilder.OnOpenConfigUi += OpenSettingsMenu;
this.commandManager = new PluginCommandManager<Plugin>(this, this.pluginInterface);
var charaMountedAddr =
this.pluginInterface.TargetModuleScanner.ScanText("48 83 EC 28 48 8B 01 FF 50 18 83 F8 08 0F 94 C0");
PluginLog.Log($"Found IsMounted address: {charaMountedAddr.ToInt64():X}");
this.charaMountedHook ??=
new Hook<CharacterIsMounted>(charaMountedAddr, new CharacterIsMounted(CharacterIsMountedDetour));
this.charaMountedHook.Enable();
var charaInitAddr = this.pluginInterface.TargetModuleScanner.ScanText(
"48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 74 24 ?? 57 48 83 EC 30 48 8B F9 48 8B EA 48 81 C1 ?? ?? ?? ?? E8 ?? ?? ?? ??");
PluginLog.Log($"Found Initialize address: {charaInitAddr.ToInt64():X}");
this.charaInitHook ??=
new Hook<CharacterInitialize>(charaInitAddr, new CharacterInitialize(CharacterInitializeDetour));
this.charaInitHook.Enable();
var flagSlotUpdateAddr =
this.pluginInterface.TargetModuleScanner.ScanText(
"48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 8B DA 49 8B F0 48 8B F9 83 FA 0A");
PluginLog.Log($"Found FlagSlotUpdate address: {flagSlotUpdateAddr.ToInt64():X}");
this.flagSlotUpdateHook ??=
new Hook<FlagSlotUpdate>(flagSlotUpdateAddr, new FlagSlotUpdate(FlagSlotUpdateDetour));
this.flagSlotUpdateHook.Enable();
// Trigger an initial refresh of all players
RefreshAllPlayers();
}
private IntPtr CharacterIsMountedDetour(IntPtr actorPtr) {
if (Marshal.ReadByte(actorPtr + ActorOffsets.ObjectKind) == (byte)ObjectKind.Player) {
lastActor = actorPtr;
lastWasPlayer = true;
} else {
lastWasPlayer = false;
}
return charaMountedHook.Original(actorPtr);
}
private IntPtr CharacterInitializeDetour(IntPtr drawObjectBase, IntPtr customizeDataPtr) {
if (lastWasPlayer) {
lastWasModified = false;
var actor = Marshal.PtrToStructure<Actor>(lastActor);
if ((uint)actor.ActorId != CHARA_WINDOW_ACTOR_ID && this.pluginInterface.ClientState.LocalPlayer != null) {
if (actor.ActorId == this.pluginInterface.ClientState.LocalPlayer.ActorId) {
if (this.config.SelfChange) {
this.ChangeRace(customizeDataPtr, this.config.SelfRace);
}
} else {
if (this.config.OtherChange) {
this.ChangeRace(customizeDataPtr, this.config.OtherRace);
}
}
}
}
return charaInitHook.Original(drawObjectBase, customizeDataPtr);
}
private void ChangeRace(IntPtr customizeDataPtr, Race targetRace) {
var customData = Marshal.PtrToStructure<CharaCustomizeData>(customizeDataPtr);
if (customData.Race != targetRace) {
// Modify the race/tribe accordingly
customData.Race = targetRace;
customData.Tribe = (byte)((byte) customData.Race * 2 - customData.Tribe % 2);
// Special-case Hrothgar/Viera gender to prevent fuckery
customData.Gender = targetRace switch {
Race.HROTHGAR => 0, // Force male for Hrothgar
Race.VIERA => 1, // Force female for Viera
_ => customData.Gender
};
// TODO: Re-evaluate these for valid race-specific values? (These are Lalafell values)
// Constrain face type to 0-3 so we don't decapitate the character
customData.FaceType %= 4;
// Constrain body type to 0-1 so we don't crash the game
customData.ModelType %= 2;
// Hrothgar have a limited number of lip colors?
customData.LipColor = targetRace switch {
Race.HROTHGAR => (byte) (customData.LipColor % 5 + 1),
_ => customData.LipColor
};
customData.HairStyle = (byte) (customData.HairStyle % RaceMappings.RaceHairs[targetRace] + 1);
Marshal.StructureToPtr(customData, customizeDataPtr, true);
// Record the new race/gender for equip model mapping, and mark the equip as dirty
lastPlayerRace = customData.Race;
lastPlayerGender = customData.Gender;
lastWasModified = true;
}
}
private IntPtr FlagSlotUpdateDetour(IntPtr actorPtr, uint slot, IntPtr equipDataPtr) {
if (lastWasPlayer && lastWasModified) {
var equipData = Marshal.PtrToStructure<EquipData>(equipDataPtr);
// TODO: Handle gender-locked gear for Viera/Hrothgar
equipData = MapRacialEquipModels(lastPlayerRace, lastPlayerGender, equipData);
Marshal.StructureToPtr(equipData, equipDataPtr, true);
}
return flagSlotUpdateHook.Original(actorPtr, slot, equipDataPtr);
}
public bool SaveConfig() {
if (this.unsavedConfigChanges) {
this.config.Save();
this.unsavedConfigChanges = false;
this.RefreshAllPlayers();
return true;
}
return false;
}
public void ToggleOtherRace(bool changeRace) {
if (this.config.OtherChange == changeRace) {
return;
}
PluginLog.Log($"OtherRace toggled to {changeRace}, refreshing players");
this.config.OtherChange = changeRace;
unsavedConfigChanges = true;
}
public void UpdateOtherRace(Race race) {
if (this.config.OtherRace == race) {
return;
}
PluginLog.Log($"OtherRace changed to {race}, refreshing players");
this.config.OtherRace = race;
unsavedConfigChanges = true;
}
public void ToggleSelfRace(bool changeRace) {
if (this.config.SelfChange == changeRace) {
return;
}
PluginLog.Log($"SelfRace toggled to {changeRace}, refreshing players");
this.config.SelfChange = changeRace;
unsavedConfigChanges = true;
}
public void UpdateSelfRace(Race race) {
if (this.config.SelfRace == race) {
return;
}
PluginLog.Log($"SelfRace changed to {race}, refreshing players");
this.config.SelfRace = race;
unsavedConfigChanges = true;
}
public void RefreshAllPlayers() {
var localPlayer = this.pluginInterface.ClientState.LocalPlayer;
if (localPlayer == null) {
return;
}
for (var i = 0; i < this.pluginInterface.ClientState.Actors.Length; i++) {
var actor = this.pluginInterface.ClientState.Actors[i];
if (actor != null
&& actor.ObjectKind == ObjectKind.Player) {
RerenderActor(actor);
}
}
}
private async void RerenderActor(Dalamud.Game.ClientState.Actors.Types.Actor actor) {
try {
var addrRenderToggle = actor.Address + OFFSET_RENDER_TOGGLE;
// Trigger a rerender
Marshal.WriteInt32(addrRenderToggle, 2);
await Task.Delay(100);
Marshal.WriteInt32(addrRenderToggle, 0);
} catch (Exception ex) {
PluginLog.LogError(ex.ToString());
}
}
private EquipData MapRacialEquipModels(Race race, int gender, EquipData eq) {
if (Array.IndexOf(RACE_STARTER_GEAR_IDS, eq.model) > -1) {
PluginLog.Log($"Modified {eq.model}, {eq.variant}");
PluginLog.Log($"Race {race}, index {(byte) (race - 1)}, gender {gender}");
eq.model = RACE_STARTER_GEAR_ID_MAP[(byte) race - 1, gender];
eq.variant = 1;
PluginLog.Log($"New {eq.model}, {eq.variant}");
}
return eq;
}
[Command("/poal")]
[HelpMessage("Opens the Oops, All Lalafells! settings menu.")]
public void OpenSettingsMenuCommand(string command, string args) {
OpenSettingsMenu(command, args);
}
private void OpenSettingsMenu(object a, object b) {
this.SettingsVisible = true;
}
#region IDisposable Support
protected virtual void Dispose(bool disposing) {
if (!disposing) return;
this.commandManager.Dispose();
this.pluginInterface.UiBuilder.OnOpenConfigUi -= OpenSettingsMenu;
this.pluginInterface.UiBuilder.OnBuildUi -= this.ui.Draw;
this.SaveConfig();
this.charaMountedHook.Disable();
this.charaInitHook.Disable();
this.flagSlotUpdateHook.Disable();
this.charaMountedHook.Dispose();
this.charaInitHook.Dispose();
this.flagSlotUpdateHook.Dispose();
// Refresh all players again
RefreshAllPlayers();
this.pluginInterface.Dispose();
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}