-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApiProvider.cs
193 lines (163 loc) · 8.91 KB
/
ApiProvider.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Threading;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Plugin.Ipc;
namespace SimpleHeels;
public static class ApiProvider {
private const int ApiVersionMajor = 2;
private const int ApiVersionMinor = 2;
public const string ApiVersionIdentifier = "SimpleHeels.ApiVersion";
public const string GetLocalPlayerIdentifier = "SimpleHeels.GetLocalPlayer";
public const string LocalChangedIdentifier = "SimpleHeels.LocalChanged";
public const string RegisterPlayerIdentifier = "SimpleHeels.RegisterPlayer";
public const string UnregisterPlayerIdentifier = "SimpleHeels.UnregisterPlayer";
public const string CreateTagIdentifier = "SimpleHeels.SetTag";
public const string GetTagIdentifier = "SimpleHeels.GetTag";
public const string RemoveTagIdentifier = "SimpleHeels.RemoveTag";
public static bool IsSerializing = false;
private static ICallGateProvider<(int, int)>? _apiVersion;
private static ICallGateProvider<string>? _getLocalPlayer;
private static ICallGateProvider<string, object?>? _localChanged;
private static ICallGateProvider<int, string, object?>? _registerPlayer;
private static ICallGateProvider<int, object?>? _unregisterPlayer;
private static ICallGateProvider<int, string, string, object?>? _setTag;
private static ICallGateProvider<int, string, string?>? _getTag;
private static ICallGateProvider<int, string, object?>? _removeTag;
private static IpcCharacterConfig? _lastReported;
private static Vector3? _lastReportedOffset;
private static float? _lastReportedRotation;
private static PitchRoll? _lastReportedPitchRoll;
private static Plugin? _plugin;
public static readonly Stopwatch TimeSinceLastReport = Stopwatch.StartNew();
private static CancellationTokenSource? _tokenSource;
public static string LastReportedData { get; private set; } = string.Empty;
public static void Init(Plugin plugin) {
_plugin = plugin;
var pluginInterface = PluginService.PluginInterface;
_apiVersion = pluginInterface.GetIpcProvider<(int, int)>(ApiVersionIdentifier);
_apiVersion.RegisterFunc(() => (ApiVersionMajor, ApiVersionMinor));
_getLocalPlayer = pluginInterface.GetIpcProvider<string>(GetLocalPlayerIdentifier);
_localChanged = pluginInterface.GetIpcProvider<string, object?>(LocalChangedIdentifier);
_registerPlayer = pluginInterface.GetIpcProvider<int, string, object?>(RegisterPlayerIdentifier);
_unregisterPlayer = pluginInterface.GetIpcProvider<int, object?>(UnregisterPlayerIdentifier);
_setTag = pluginInterface.GetIpcProvider<int, string, string, object?>(CreateTagIdentifier);
_getTag = pluginInterface.GetIpcProvider<int, string, string?>(GetTagIdentifier);
_removeTag = pluginInterface.GetIpcProvider<int, string, object?>(RemoveTagIdentifier);
_apiVersion.RegisterFunc(() => (ApiVersionMajor, ApiVersionMinor));
_registerPlayer.RegisterAction((gameObjectIndex, data) => {
var gameObject = gameObjectIndex >= 0 && gameObjectIndex < PluginService.Objects.Length ? PluginService.Objects[gameObjectIndex] : null;
if (gameObject is not IPlayerCharacter playerCharacter) return;
if (string.IsNullOrWhiteSpace(data)) {
Plugin.IpcAssignedData.Remove(playerCharacter.EntityId);
return;
}
var assigned = IpcCharacterConfig.FromString(data);
if (assigned == null) return;
Plugin.IpcAssignedData.Remove(playerCharacter.EntityId);
Plugin.IpcAssignedData.Add(playerCharacter.EntityId, assigned);
Plugin.RequestUpdateAll();
});
_unregisterPlayer.RegisterAction(gameObjectIndex => {
var gameObject = gameObjectIndex >= 0 && gameObjectIndex < PluginService.Objects.Length ? PluginService.Objects[gameObjectIndex] : null;
if (gameObject is not IPlayerCharacter playerCharacter) return;
Plugin.IpcAssignedData.Remove(playerCharacter.EntityId);
Plugin.RequestUpdateAll();
});
_getLocalPlayer.RegisterFunc(() => {
var player = PluginService.ClientState.LocalPlayer;
if (player == null) return string.Empty;
return new IpcCharacterConfig(_plugin, player).ToString();
});
_setTag.RegisterAction((gameObjectIndex, tag, value) => {
var gameObject = gameObjectIndex >= 0 && gameObjectIndex < PluginService.Objects.Length ? PluginService.Objects[gameObjectIndex] : null;
if (gameObject is not IPlayerCharacter playerCharacter) return;
if (!Plugin.Tags.TryGetValue(gameObject.EntityId, out var tagDict)) {
tagDict = new Dictionary<string, string>();
Plugin.Tags.Add(playerCharacter.EntityId, tagDict);
}
if (!tagDict.TryAdd(tag, value)) {
tagDict[tag] = value;
}
if (gameObject.ObjectIndex == 0) OnChanged();
});
_getTag.RegisterFunc((gameObjectIndex, tag) => {
var gameObject = gameObjectIndex >= 0 && gameObjectIndex < PluginService.Objects.Length ? PluginService.Objects[gameObjectIndex] : null;
if (gameObject is not IPlayerCharacter playerCharacter) return null;
return !Plugin.Tags.TryGetValue(playerCharacter.EntityId, out var tagDict) ? null : tagDict.GetValueOrDefault(tag);
});
_removeTag.RegisterAction((gameObjectIndex, tag) => {
var gameObject = gameObjectIndex >= 0 && gameObjectIndex < PluginService.Objects.Length ? PluginService.Objects[gameObjectIndex] : null;
if (gameObject is not IPlayerCharacter playerCharacter) return;
if (!Plugin.Tags.TryGetValue(gameObject.EntityId, out var tagDict)) return;
tagDict.Remove(tag);
if (tagDict.Count == 0) Plugin.Tags.Remove(playerCharacter.EntityId);
if (gameObject.ObjectIndex == 0) OnChanged();
});
}
private static void OnChanged() {
using (PerformanceMonitors.Run("Generate IPC Message")) {
PluginService.Log.Debug("Reporting to IPC");
TimeSinceLastReport.Restart();
var gameObject = PluginService.ClientState.LocalPlayer;
if (gameObject == null) return;
if (gameObject.ObjectIndex != 0) return;
if (_plugin == null) return;
var data = new IpcCharacterConfig(_plugin, gameObject);
if (data.Equals(_lastReported)) return;
var json = data.ToString();
_lastReported = data;
var tokenSource = _tokenSource;
tokenSource?.Cancel();
_tokenSource = new CancellationTokenSource();
PluginService.Framework.RunOnTick(() => {
using (PerformanceMonitors.Run("Send IPC Message")) {
LastReportedData = json;
_localChanged?.SendMessage(json);
}
}, cancellationToken: _tokenSource.Token, delay: TimeSpan.FromMilliseconds(250));
}
}
internal static void DeInit() {
_tokenSource?.Cancel();
_apiVersion?.UnregisterFunc();
_getLocalPlayer?.UnregisterFunc();
_registerPlayer?.UnregisterAction();
_unregisterPlayer?.UnregisterAction();
_setTag?.UnregisterAction();
_getTag?.UnregisterFunc();
_removeTag?.UnregisterAction();
_localChanged = null;
_apiVersion = null;
_getLocalPlayer = null;
_registerPlayer = null;
_unregisterPlayer = null;
_getTag = null;
_removeTag = null;
_setTag = null;
}
internal static void UpdateLocal(Vector3 offset, float rotation, PitchRoll pitchRoll) {
if (_lastReportedOffset == null || Vector3.Distance(_lastReportedOffset.Value, offset) > Constants.FloatDelta ||
_lastReportedRotation == null || MathF.Abs(_lastReportedRotation.Value - rotation) > Constants.FloatDelta ||
_lastReportedPitchRoll == null || !PitchRoll.Equal(_lastReportedPitchRoll, pitchRoll)) {
_lastReportedOffset = offset;
_lastReportedRotation = rotation;
_lastReportedPitchRoll = pitchRoll;
OnChanged();
}
}
internal static void ForceUpdateLocal() {
_lastReportedOffset = null;
_lastReportedRotation = null;
_lastReportedPitchRoll = null;
_lastReported = null;
OnChanged();
}
internal static void UpdateMinion(Vector3 pos, float rotation) {
if (_lastReported?.MinionPosition == null || Vector3.Distance(new Vector3(_lastReported.MinionPosition.X, _lastReported.MinionPosition.Y, _lastReported.MinionPosition.Z), pos) > Constants.FloatDelta || MathF.Abs(_lastReported.MinionPosition.R - rotation) > Constants.FloatDelta) {
OnChanged();
}
}
}