-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
QoLBar.cs
330 lines (277 loc) · 10.9 KB
/
QoLBar.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
326
327
328
329
330
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Dalamud.Utility;
// Disclaimer: I have no idea what I'm doing.
namespace QoLBar;
public class QoLBar : IDalamudPlugin
{
public static QoLBar Plugin { get; private set; }
public static Configuration Config { get; private set; }
public PluginUI ui;
private bool pluginReady = false;
public static TextureDictionary TextureDictionary => Config.UseHRIcons ? textureDictionaryHR : textureDictionaryLR;
public static readonly TextureDictionary textureDictionaryLR = new(false, false);
public static readonly TextureDictionary textureDictionaryHR = new(true, false);
public static readonly TextureDictionary textureDictionaryGSLR = new(false, true);
public static readonly TextureDictionary textureDictionaryGSHR = new(true, true);
public const float DefaultFontSize = 17;
public const float MaxFontSize = 64;
public static IFontHandle Font { get; private set; }
public QoLBar(IDalamudPluginInterface pluginInterface)
{
Plugin = this;
DalamudApi.Initialize(this, pluginInterface);
Config = (Configuration)DalamudApi.PluginInterface.GetPluginConfig() ?? new();
Config.Initialize();
Config.TryBackup(); // Backup on version change
DalamudApi.Framework.Update += Update;
ui = new PluginUI();
DalamudApi.PluginInterface.UiBuilder.OpenConfigUi += ToggleConfig;
DalamudApi.PluginInterface.UiBuilder.Draw += Draw;
SetupFont();
CheckHideOptOuts();
ReadyPlugin();
}
public void ReadyPlugin()
{
try
{
IPC.Initialize();
var iconPath = Config.GetPluginIconPath();
textureDictionaryLR.AddUserIcons(iconPath);
textureDictionaryHR.AddUserIcons(iconPath);
TextureDictionary.AddExtraTextures(textureDictionaryLR, textureDictionaryHR);
TextureDictionary.AddExtraTextures(textureDictionaryGSLR, textureDictionaryGSHR);
IconBrowserUI.BuildCache(false);
Game.Initialize();
ConditionManager.Initialize();
pluginReady = true;
IPC.InitializedProvider.SendMessage();
}
catch (Exception e)
{
DalamudApi.LogError($"Failed loading QoLBar!\n{e}");
}
}
public void Reload()
{
Config = (Configuration)DalamudApi.PluginInterface.GetPluginConfig() ?? new();
Config.Initialize();
Config.UpdateVersion();
Config.Save();
ui.Reload();
CheckHideOptOuts();
}
public void ToggleConfig() => ui.ToggleConfig();
[Command("/qolbar")]
[HelpMessage("Open the configuration menu.")]
public void ToggleConfig(string command, string argument) => ToggleConfig();
[Command("/qolicons")]
[HelpMessage("Open the icon browser.")]
public void ToggleIconBrowser(string command = null, string argument = null) => IconBrowserUI.ToggleIconBrowser();
[Command("/qolvisible")]
[HelpMessage("Hide or reveal a bar using its name or index. Usage: /qolvisible [on|off|toggle] <bar>")]
private void OnQoLVisible(string command, string argument)
{
var reg = Regex.Match(argument, @"^(\w+) (.+)");
if (reg.Success)
{
var subcommand = reg.Groups[1].Value.ToLower();
var bar = reg.Groups[2].Value;
var useID = int.TryParse(bar, out var id);
switch (subcommand)
{
case "on":
case "reveal":
case "r":
if (useID)
ui.SetBarHidden(id - 1, false, false);
else
ui.SetBarHidden(bar, false, false);
break;
case "off":
case "hide":
case "h":
if (useID)
ui.SetBarHidden(id - 1, false, true);
else
ui.SetBarHidden(bar, false, true);
break;
case "toggle":
case "t":
if (useID)
ui.SetBarHidden(id - 1, true);
else
ui.SetBarHidden(bar, true);
break;
default:
PrintError("Invalid subcommand.");
break;
}
}
else
PrintError("Usage: /qolvisible [on|off|toggle] <bar>");
}
[Command("/performance")]
[HelpMessage("Starts playing an instrument.")]
public void OnPerformance(string command, string argument)
{
if (!byte.TryParse(argument, out var b)
&& DalamudApi.DataManager.GetExcelSheet<Lumina.Excel.Sheets.Perform>().FirstOrDefault(r
=> argument?.Equals(r.Instrument.ExtractText(), StringComparison.CurrentCultureIgnoreCase) ?? false) is { RowId: > 0 } r)
b = (byte)r.RowId;
if (b == 0)
PrintError("Invalid instrument.");
else
Game.StartPerformance(b);
}
public static bool HasPlugin(string name) => DalamudApi.PluginInterface.InstalledPlugins.Any(p => p.IsLoaded && p.InternalName == name);
public static bool IsLoggedIn() => ConditionManager.CheckCondition("l");
public static float RunTime => (float)DalamudApi.PluginInterface.LoadTimeDelta.TotalSeconds;
public static long FrameCount => (long)DalamudApi.PluginInterface.UiBuilder.FrameCount;
private void Update(IFramework framework)
{
if (!pluginReady) return;
Config.DoTimedBackup();
Game.ReadyCommand();
Keybind.Run();
Keybind.SetupHotkeys(ui.bars);
ConditionManager.UpdateCache();
}
private void Draw()
{
if (_addUserIcons)
AddUserIcons(ref _addUserIcons);
if (!pluginReady) return;
Config.DrawUpdateWindow();
ui.Draw();
}
public static void SetupFont()
{
Font?.Dispose();
Font = DalamudApi.PluginInterface.UiBuilder.FontAtlas.NewDelegateFontHandle(buildToolkit =>
{
buildToolkit.OnPreBuild(tk =>
{
var config = new SafeFontConfig { SizePx = Math.Min(Math.Max(Config.FontSize, 1), MaxFontSize) };
var font = tk.AddDalamudAssetFont(DalamudAsset.NotoSansJpMedium, config);
config.MergeFont = font;
tk.AddGameSymbol(config);
tk.SetFontScaleMode(font, FontScaleMode.UndoGlobalScale);
});
});
}
public void CheckHideOptOuts()
{
//pluginInterface.UiBuilder.DisableAutomaticUiHide = false;
DalamudApi.PluginInterface.UiBuilder.DisableUserUiHide = Config.OptOutGameUIOffHide;
DalamudApi.PluginInterface.UiBuilder.DisableCutsceneUiHide = Config.OptOutCutsceneHide;
DalamudApi.PluginInterface.UiBuilder.DisableGposeUiHide = Config.OptOutGPoseHide;
}
public static Dictionary<int, string> GetUserIcons() => TextureDictionary.GetUserIcons();
// TODO: .
private bool _addUserIcons = false;
private bool _iconsLR = false;
private bool _iconsHR = false;
private void AddUserIcons(ref bool b)
{
if (!_iconsLR && !_iconsHR)
{
_iconsLR = true;
_iconsHR = true;
}
var iconPath = Config.GetPluginIconPath();
if (_iconsLR)
_iconsLR = !textureDictionaryLR.AddUserIcons(iconPath);
if (_iconsHR)
_iconsHR = !textureDictionaryHR.AddUserIcons(iconPath);
if (!(b = _iconsLR || _iconsHR))
IconBrowserUI.BuildCache(false);
}
public void AddUserIcons() => _addUserIcons = true;
public static void CleanTextures(bool disposing)
{
if (disposing)
{
textureDictionaryLR.Dispose();
textureDictionaryHR.Dispose();
textureDictionaryGSLR.Dispose();
textureDictionaryGSHR.Dispose();
}
else
{
textureDictionaryLR.TryEmpty();
textureDictionaryHR.TryEmpty();
textureDictionaryGSLR.TryEmpty();
textureDictionaryGSHR.TryEmpty();
}
}
public static void PrintEcho(string message) => DalamudApi.ChatGui.Print($"[QoL Bar] {message}");
public static void PrintError(string message) => DalamudApi.ChatGui.PrintError($"[QoL Bar] {message}");
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
IPC.DisposedProvider.SendMessage();
IPC.Dispose();
Config.Save();
Config.SaveTempConfig();
DalamudApi.Framework.Update -= Update;
DalamudApi.PluginInterface.UiBuilder.OpenConfigUi -= ToggleConfig;
DalamudApi.PluginInterface.UiBuilder.Draw -= Draw;
DalamudApi.Dispose();
ui.Dispose();
Game.Dispose();
CleanTextures(true);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
public static class Extensions
{
public static T2 GetDefaultValue<T, T2>(this T _, Expression<Func<T, T2>> expression)
{
if (((MemberExpression)expression.Body).Member.GetCustomAttribute(typeof(DefaultValueAttribute)) is DefaultValueAttribute attribute)
return (T2)attribute.Value;
else
return default;
}
public static byte[] GetGrayscaleImageData(this Lumina.Data.Files.TexFile tex)
{
var rgba = tex.GetRgbaImageData();
var pixels = rgba.Length / 4;
var newData = new byte[rgba.Length];
for (int i = 0; i < pixels; i++)
{
var pixel = i * 4;
var alpha = rgba[pixel + 3];
if (alpha > 0)
{
var avg = (byte)(0.2125f * rgba[pixel] + 0.7154f * rgba[pixel + 1] + 0.0721f * rgba[pixel + 2]);
newData[pixel] = avg;
newData[pixel + 1] = avg;
newData[pixel + 2] = avg;
}
newData[pixel + 3] = alpha;
}
return newData;
}
public static object Cast(this Type Type, object data)
{
var DataParam = Expression.Parameter(typeof(object), "data");
var Body = Expression.Block(Expression.Convert(Expression.Convert(DataParam, data.GetType()), Type));
var Run = Expression.Lambda(Body, DataParam).Compile();
var ret = Run.DynamicInvoke(data);
return ret;
}
}