forked from Yuvix25/ReHUD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReHUDHub.cs
More file actions
248 lines (213 loc) · 7.73 KB
/
Copy pathReHUDHub.cs
File metadata and controls
248 lines (213 loc) · 7.73 KB
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
using Microsoft.AspNetCore.SignalR;
using ReHUD;
using ReHUD.Interfaces;
using System.Collections.Concurrent;
using System.Data;
using System.Reflection;
namespace SignalRChat.Hubs
{
public class ReHUDHub : Hub
{
private static List<string> ConnectedIds = [];
private static Dictionary<string, List<Action<object>>> Listeners = InitializeListeners();
private static Dictionary<string, List<Action<object>>> InitializeListeners()
{
var methods = typeof(ReHUDHub).GetMethods().Select(SelectName);
var dict = new Dictionary<string, List<Action<object>>> ();
foreach (var method in methods)
{
dict.Add(method, []);
}
return dict;
}
public void Log(string level, double startTimestamp, double endTimestamp, string message) {
try {
if (startTimestamp != -1) {
startTimestamp /= 1000;
}
if (endTimestamp != -1) {
endTimestamp /= 1000;
}
LogMessage logMessage = new(startTimestamp, endTimestamp, message);
if (Startup.logger != null) {
switch (level) {
case "WARN":
Startup.logger.Warn(logMessage);
break;
case "ERROR":
Startup.logger.Error(logMessage);
break;
default:
Startup.logger.Info(logMessage);
break;
}
}
}
catch (Exception e) {
Console.WriteLine(e);
}
}
public override Task OnConnectedAsync()
{
ConnectedIds.Add(Context.ConnectionId);
Console.WriteLine($"Connections: {ConnectedIds.Count}");
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception? exception)
{
ConnectedIds.Remove(Context.ConnectionId);
Console.WriteLine($"Connections: {ConnectedIds.Count}");
return base.OnDisconnectedAsync(exception);
}
private IR3EDataService? GetR3EDataService()
{
return Context.GetHttpContext()?.RequestServices.GetService<IR3EDataService>();
}
public void SaveBestLap(int layoutId, int classId, double laptime, double[] points, double pointsPerMeter)
{
Startup.logger.InfoFormat("SaveBestLap: layoutId={0}, classId={1}, laptime={2}, points={3}, pointsPerMeter={4}", layoutId, classId, laptime, points.Length, pointsPerMeter);
var r3eDataService = GetR3EDataService();
if (r3eDataService == null) {
Startup.logger.Error("SaveBestLap: r3eDataService is null");
return;
}
r3eDataService.SaveBestLap(layoutId, classId, laptime, points, pointsPerMeter);
}
public string LoadBestLap(int layoutId, int classId)
{
Startup.logger.InfoFormat("LoadBestLap: layoutId={0}, classId={1}", layoutId, classId);
var r3eDataService = GetR3EDataService();
if (r3eDataService == null) {
Startup.logger.Error("LoadBestLap: r3eDataService is null");
return "{}";
}
return r3eDataService.LoadBestLap(layoutId, classId);
}
public void Response(string response)
{
_ = response;
}
[HubMethodName("get-port")]
public void GetPort(object input)
{
ExecuteAllListeners("get-port", input);
}
[HubMethodName("used-keys")]
public void UsedKeys(object input)
{
ExecuteAllListeners("used-keys", input);
}
[HubMethodName("load-settings")]
public void LoadSettings(object input)
{
ExecuteAllListeners("load-settings", input);
}
[HubMethodName("get-hud-layout")]
public void GetHudLayout(object input)
{
ExecuteAllListeners("get-hud-layout", input);
}
[HubMethodName("set-hud-layout")]
public void SetHudLayout(object input)
{
ExecuteAllListeners("set-hud-layout", input);
}
[HubMethodName("load-replay-preset")]
public void LoadReplayPreset(object input)
{
ExecuteAllListeners("load-replay-preset", input);
}
[HubMethodName("unload-replay-preset")]
public void UnLoadReplayPreset(object input)
{
ExecuteAllListeners("unload-replay-preset", input);
}
[HubMethodName("update-preset-name")]
public void UpdatePresetName(object input)
{
ExecuteAllListeners("update-preset-name", input);
}
[HubMethodName("update-preset-is-replay")]
public void UpdatePresetIsReplay(object input)
{
ExecuteAllListeners("update-preset-is-replay", input);
}
[HubMethodName("toggle-element")]
public void ToggleElement(object input)
{
ExecuteAllListeners("toggle-element", input);
}
[HubMethodName("reset-active-layout")]
public void ResetActiveLayout(object input)
{
ExecuteAllListeners("reset-active-layout", input);
}
[HubMethodName("request-layout-visibility")]
public void RequestLayoutVisibility(object input)
{
ExecuteAllListeners("request-layout-visibility", input);
}
[HubMethodName("check-for-updates")]
public void CheckForUpdates(object input)
{
ExecuteAllListeners("check-for-updates", input);
}
[HubMethodName("lock-overlay")]
public void LockOverlay(object input)
{
ExecuteAllListeners("lock-overlay", input);
}
[HubMethodName("set-setting")]
public void SetSetting(object input)
{
ExecuteAllListeners("set-setting", input);
}
[HubMethodName("show-log-file")]
public void ShowLogFile(object input)
{
ExecuteAllListeners("show-log-file", input);
}
[HubMethodName("new-hud-layout")]
public void NewHudLayout(object input)
{
ExecuteAllListeners("new-hud-layout", input);
}
[HubMethodName("delete-hud-layout")]
public void DeleteHudLayout(object input)
{
ExecuteAllListeners("delete-hud-layout", input);
}
[HubMethodName("restart-app")]
public void RestartApp(object input)
{
ExecuteAllListeners("restart-app", input);
}
[HubMethodName("webHudMode")]
public void WebHudMode(object input)
{
ExecuteAllListeners("webHudMode", input);
}
public static Task RegisterListeners(string channel, Action<object> action)
{
Listeners.FirstOrDefault(x => x.Key == channel).Value.Add(action);
return Task.CompletedTask;
}
private static string SelectName(MethodInfo type)
{
var attr = type.GetCustomAttribute(typeof(HubMethodNameAttribute)) as HubMethodNameAttribute;
if (attr is null)
{
return type.Name;
}
return attr.Name;
}
private void ExecuteAllListeners(string channel, object value)
{
var listeners = Listeners.FirstOrDefault(x => x.Key.Equals(channel));
foreach (var listener in listeners.Value)
{
listener(value);
}
}
}
}