-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
MetricsService.cs
89 lines (71 loc) · 3.04 KB
/
MetricsService.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using ImGuiNET;
using Newtonsoft.Json;
using SimpleTweaksPlugin.TweakSystem;
using SimpleTweaksPlugin.Utility;
namespace SimpleTweaksPlugin;
public static unsafe class MetricsService {
private static SimpleTweaksPlugin Plugin => SimpleTweaksPlugin.Plugin;
private static SimpleTweaksPluginConfig Config => Plugin.PluginConfig;
public static void ReportMetrics(bool allowFirstUse = false) {
if (Config.AnalyticsOptOut) return;
var identifier = Config.MetricsIdentifier;
if (string.IsNullOrEmpty(identifier) || identifier.Length != 64) {
if (!allowFirstUse) return;
try {
var idStr = Guid.NewGuid().ToString();
var result = SHA256.HashData(Encoding.UTF8.GetBytes(idStr));
Config.MetricsIdentifier = identifier = BitConverter.ToString(result).Replace("-", "");
Config.Save();
} catch (Exception ex) {
SimpleTweaksPlugin.Plugin.Error(ex, "Error reporting metrics.");
return;
}
}
if (string.IsNullOrEmpty(identifier) || identifier.Length != 64) return;
var payload = new MetricsPayload {
Identifier = identifier,
Version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty,
};
void ParseTweakList(IEnumerable<BaseTweak> tweaks) {
foreach (var t in tweaks) {
if (t.TweakProvider is CustomTweakProvider) continue;
#if DEBUG
if (ImGui.GetIO().KeyShift) {
if (t is not SubTweakManager { AlwaysEnabled: true }) payload.TweakNames.Add(t.Key, t.Name);
}
#endif
if (t is SubTweakManager stm) {
if (t.Enabled || stm.AlwaysEnabled) {
if (!stm.AlwaysEnabled) {
payload.EnabledTweaks.Add(t.Key);
}
ParseTweakList(stm.GetTweakList());
}
continue;
}
if (!t.Enabled) continue;
payload.EnabledTweaks.Add(t.Key);
}
}
ParseTweakList(Plugin.Tweaks);
var json = JsonConvert.SerializeObject(payload, Formatting.None, new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.None
});
Common.HttpClient.PostAsync("https://metrics.simpletweaks.app/report", new StringContent(json)).ConfigureAwait(false);
}
public class MetricsPayload {
public string Identifier;
public string Version;
public List<string> EnabledTweaks = new();
#if DEBUG
public bool ShouldSerializeTweakNames() => TweakNames.Count > 0;
public Dictionary<string, string> TweakNames = new();
#endif
}
}