forked from Caraxi/FPSPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPSPlugin.cs
167 lines (146 loc) · 6.73 KB
/
FPSPlugin.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
using Dalamud.Plugin;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Dalamud.Game.Gui.Dtr;
using Dalamud.IoC;
using Dalamud.Plugin.Services;
namespace FPSPlugin {
public class FPSPlugin : IDalamudPlugin {
public string Name => "FPS Plugin";
public FPSPluginConfig PluginConfig { get; }
private bool drawConfigWindow;
private List<float> fpsHistory;
private Stopwatch fpsHistoryInterval;
private string fpsText;
private IDtrBarEntry dtrEntry;
private float maxSeenFps;
[PluginService] public static ICommandManager CommandManager { get; set; } = null!;
[PluginService] public static IFramework Framework { get; set; } = null!;
[PluginService] public static IDalamudPluginInterface PluginInterface { get; set; } = null!;
[PluginService] public static IDtrBar DtrBar { get; set; } = null!;
[PluginService] public static IPluginLog PluginLog { get; set; } = null!;
public void Dispose() {
PluginInterface.UiBuilder.Draw -= this.BuildUI;
PluginInterface.UiBuilder.OpenConfigUi -= this.OpenConfigUi;
Framework.Update -= this.OnFrameworkUpdate;
fpsHistoryInterval?.Stop();
dtrEntry?.Remove();
RemoveCommands();
}
public FPSPlugin() {
PluginConfig = (FPSPluginConfig) PluginInterface.GetPluginConfig() ?? new FPSPluginConfig();
PluginConfig.Init(this);
fpsText = string.Empty;
fpsHistory = new List<float>();
fpsHistoryInterval = new Stopwatch();
fpsHistoryInterval.Start();
SetupCommands();
PluginInterface.UiBuilder.Draw += this.BuildUI;
PluginInterface.UiBuilder.OpenConfigUi += this.OpenConfigUi;
Framework.Update += OnFrameworkUpdate;
}
private string FormatFpsValue(float value) {
if (maxSeenFps > 1000) return PluginConfig.ShowDecimals ? $"{value,8:####0.00}" : $"{value,5:####0}";
if (maxSeenFps > 100) return PluginConfig.ShowDecimals ? $"{value,7:###0.00}" : $"{value,4:###0}";
return PluginConfig.ShowDecimals ? $"{value,6:##0.00}" : $"{value,3:##0}";
}
private unsafe void OnFrameworkUpdate(IFramework dFramework) {
var framework = FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance();
try {
if (fpsText != null) {
dtrEntry ??= DtrBar.Get("FPS Display");
dtrEntry.Shown = PluginConfig.Enable;
dtrEntry.Text = fpsText;
dtrEntry.OnClick = PluginConfig.DtrOpenSettings ? OpenConfigUi : null;
dtrEntry.Tooltip = PluginConfig.DtrTooltip && fpsHistory.Count > 0 ? $"Average:{FormatFpsValue(fpsHistory.Average())}\nMinimum:{FormatFpsValue(fpsHistory.Min())}" : null;
} else {
if (dtrEntry != null) dtrEntry.Shown = false;
}
if (fpsHistoryInterval.ElapsedMilliseconds > 1000) {
fpsHistoryInterval.Restart();
// FPS values are only updated in memory once per second.
var fps = framework->FrameRate;
var windowInactive = framework->WindowInactive;
if (fps > maxSeenFps) maxSeenFps = fps;
fpsText = string.Empty;
if (!PluginConfig.NoLabels && !PluginConfig.AlternativeFPSLabel) fpsText += "FPS:";
fpsText += $"{FormatFpsValue(fps)}";
if (!PluginConfig.NoLabels && PluginConfig.AlternativeFPSLabel) fpsText += "fps";
if (PluginConfig.ShowAverage || PluginConfig.ShowMinimum || PluginConfig.DtrTooltip) {
if (!windowInactive) fpsHistory.Add(fps);
if (fpsHistory.Count > PluginConfig.HistorySnapshotCount) {
fpsHistory.RemoveRange(0, fpsHistory.Count - PluginConfig.HistorySnapshotCount);
}
if (PluginConfig.ShowAverage && fpsHistory.Count > 0) {
fpsText += " / ";
if (!PluginConfig.NoLabels) fpsText += "Avg:";
fpsText += $"{FormatFpsValue(fpsHistory.Average())}";
}
if (PluginConfig.ShowMinimum && fpsHistory.Count > 0) {
fpsText += " / ";
if (!PluginConfig.NoLabels) fpsText += "Min:";
fpsText += $"{FormatFpsValue(fpsHistory.Min())}";
}
}
#if DEBUG
if (!string.IsNullOrEmpty(PluginConfig.TestText)) {
fpsText = PluginConfig.TestText;
}
#endif
}
} catch (Exception ex) {
PluginLog.Error(ex.Message);
}
}
public void SetupCommands() {
CommandManager.AddHandler("/pfps", new Dalamud.Game.Command.CommandInfo(OnConfigCommandHandler) {
HelpMessage = $"Open config window for {this.Name}. /pfps [show|hide|toggle|reset]",
ShowInHelp = true
});
}
private void OpenConfigUi() {
OnConfigCommandHandler(null, null);
}
public void OnConfigCommandHandler(string command, string args) {
if (args != null) {
switch (args.ToLower()) {
case "t":
case "toggle": {
PluginConfig.Enable = !PluginConfig.Enable;
break;
}
case "s":
case "show": {
PluginConfig.Enable = true;
break;
}
case "h":
case "hide": {
PluginConfig.Enable = false;
break;
}
case "r":
case "reset": {
fpsHistory.Clear();
break;
}
default: {
drawConfigWindow = true;
break;
}
}
PluginConfig.Save();
} else {
drawConfigWindow = true;
}
}
public void RemoveCommands() {
CommandManager.RemoveHandler("/pfps");
}
private void BuildUI() {
drawConfigWindow = drawConfigWindow && PluginConfig.DrawConfigUI();
}
}
}