forked from NtsFranz/Spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOverlayServer.cs
More file actions
631 lines (551 loc) · 27.3 KB
/
OverlayServer.cs
File metadata and controls
631 lines (551 loc) · 27.3 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using EchoVRAPI;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
namespace Spark
{
public class OverlayServer
{
private IWebHost server;
private readonly SemaphoreSlim restartLock = new SemaphoreSlim(1, 1);
private bool isRestarting = false;
public static string StaticOverlayFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "IgniteVR", "Spark", "Overlays");
public OverlayServer()
{
// Initial start
_ = RestartServer();
DiscordOAuth.AccessCodeChanged += (code) =>
{
Console.WriteLine("Access code changed");
_ = RestartServer();
};
}
private async Task RestartServer()
{
// Prevent multiple concurrent restarts
if (!await restartLock.WaitAsync(0))
{
// If we can't get the lock immediately, it means a restart is already in progress.
// We can ignore this request as the current restart will pick up the latest state eventually,
// or simply preventing overlap is sufficient.
return;
}
try
{
isRestarting = true;
// Fetch data needed for the server
await OverlaysCustom.FetchOverlayData();
if (server != null)
{
await server.StopAsync();
server.Dispose();
server = null;
}
// Create and start the server
server = WebHost
.CreateDefaultBuilder()
.UseKestrel(x => { x.ListenAnyIP(6724); })
.ConfigureLogging((logging) => { /* Configure logging if needed, or keep silent to save perf */ })
.UseStartup<Routes>()
.Build();
await server.StartAsync();
}
catch (Exception e)
{
Logger.LogRow(Logger.LogType.Error, $"Error when restarting server: {e}");
}
finally
{
isRestarting = false;
restartLock.Release();
}
}
public void Stop()
{
if (server != null)
{
// Fire and forget stop
_ = server.StopAsync();
}
}
public class Routes
{
// Cache the resource names to avoid reflection on every request setup
private static readonly string[] ResourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddRouting();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Ensure directory exists
if (!Directory.Exists(StaticOverlayFolder))
{
Directory.CreateDirectory(StaticOverlayFolder);
}
var provider = new FileExtensionContentTypeProvider
{
Mappings = { [".yaml"] = "application/x-yaml" }
};
// Common CORS headers setup for static files
Action<StaticFileResponseContext> onPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
ctx.Context.Response.Headers.Append("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
};
// Global CORS policy - applied before static files
app.UseCors("CorsPolicy");
// Serve from AppData Overlays folder
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(StaticOverlayFolder),
RequestPath = "",
EnableDefaultFiles = true,
StaticFileOptions =
{
ServeUnknownFileTypes = true,
ContentTypeProvider = provider,
OnPrepareResponse = onPrepareResponse
}
});
// Serve from Build folder
string buildPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Overlay", "build");
if (Directory.Exists(buildPath))
{
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(buildPath),
RequestPath = "",
EnableDefaultFiles = true,
StaticFileOptions =
{
ContentTypeProvider = provider,
OnPrepareResponse = onPrepareResponse
}
});
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
SparkAPI.MapRoutes(endpoints);
EchoVRAPIPassthrough.MapRoutes(endpoints);
// API Endpoints
endpoints.MapGet("/spark_info", async context =>
{
await WriteJsonAsync(context, new Dictionary<string, object>
{
{ "version", Program.AppVersionString() },
{ "windows_store", Program.IsWindowsStore() },
{ "ess_version", Program.InstalledSpeakerSystemVersion },
});
});
endpoints.MapGet("/stats", async context =>
{
await WriteJsonAsync(context, GetStatsResponse());
});
endpoints.MapGet("/overlay_info", async context =>
{
try
{
Dictionary<string, object> response = new Dictionary<string, object>
{
["stats"] = GetStatsResponse(),
["visibility"] = new Dictionary<string, object>
{
{ "minimap", true },
{ "main_banner", true },
{ "neutral_jousts", true },
{ "defensive_jousts", true },
{ "event_log", true },
{ "playspace", true },
{ "player_speed", true },
{ "disc_speed", true },
},
["caster_prefs"] = SparkSettings.instance.casterPrefs
};
if (Program.InGame)
{
response["session"] = Program.lastJSON;
}
await WriteJsonAsync(context, response);
}
catch (Exception e)
{
Logger.LogRow(Logger.LogType.Error, e.ToString());
context.Response.StatusCode = 500;
}
});
endpoints.MapGet("/scoreboard", async context =>
{
await ServeScoreboard(context);
});
endpoints.MapGet("/disc_positions", async context =>
{
var positions = await GetDiscPositions();
await WriteJsonAsync(context, positions);
});
endpoints.MapGet("/disc_position_heatmap", async context =>
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(ReadResource("disc_position_heatmap.html"));
});
endpoints.MapGet("/get_player_speed", async context =>
{
float speed = Program.lastFrame?.GetPlayer(Program.lastFrame.client_name)?.velocity.ToVector3().Length() ?? -1;
await WriteJsonAsync(context, new { speed });
});
endpoints.MapGet("/get_disc_speed", async context =>
{
float speed = Program.lastFrame?.disc.velocity.ToVector3().Length() ?? -1;
await WriteJsonAsync(context, new { speed });
});
// Speedometer pages
MapHtmlReplacement(endpoints, "/speedometer/player", "speedometer.html", "FETCH_URL", "/get_player_speed");
MapHtmlReplacement(endpoints, "/speedometer/lone_echo_1", "speedometer.html", "FETCH_URL", "http://127.0.0.1:6723/le1/speed/");
MapHtmlReplacement(endpoints, "/speedometer/lone_echo_2", "speedometer.html", "FETCH_URL", "http://127.0.0.1:6723/le2/speed/");
MapHtmlReplacement(endpoints, "/speedometer/disc", "speedometer.html", "FETCH_URL", "/get_disc_speed");
// Map embedded wwwroot_resources
MapEmbeddedResources(endpoints);
});
}
private static async Task WriteJsonAsync(HttpContext context, object data)
{
context.Response.ContentType = "application/json";
context.Response.Headers["Access-Control-Allow-Origin"] = "*";
await context.Response.WriteAsync(JsonConvert.SerializeObject(data));
}
private static void MapHtmlReplacement(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string route, string resourceName, string placeholder, string replacement)
{
endpoints.MapGet(route, async context =>
{
string file = ReadResource(resourceName);
file = file.Replace(placeholder, replacement);
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(file);
});
}
private static void MapEmbeddedResources(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string sparkPath = Path.GetDirectoryName(assembly.Location);
foreach (string str in ResourceNames)
{
string[] pieces = str.Split('.');
if (pieces.Length > 2 && pieces?[1] == "wwwroot_resources")
{
// Logic to map resource name to URL path
List<string> folderPieces = pieces.Skip(2).SkipLast(2).Append(string.Join('.', pieces.TakeLast(2))).ToList();
if (folderPieces.Count > 1 && folderPieces[^1].Contains("min."))
{
folderPieces[^2] = string.Join('.', folderPieces.TakeLast(2));
folderPieces.RemoveAt(folderPieces.Count - 1);
}
string url;
string fileName = folderPieces[^1];
if (fileName == "index.html")
{
url = "/" + string.Join('/', folderPieces.SkipLast(1));
}
else if (fileName.EndsWith(".html"))
{
url = "/" + string.Join('/', folderPieces)[..^5];
}
else
{
url = "/" + string.Join('/', folderPieces);
}
endpoints.MapGet(url, async context =>
{
string ext = fileName.Split('.').Last();
string contentType = ext switch
{
"js" => "application/javascript",
"css" => "text/css",
"png" => "image/png",
"jpg" => "image/jpeg",
"html" => "text/html",
_ => "application/octet-stream"
};
context.Response.Headers.Add("content-type", contentType);
// Prefer loading from disk if available (for dev/modding), fall back to resource
if (sparkPath != null)
{
string filePath = Path.Combine(sparkPath, "wwwroot_resources", Path.Combine(folderPieces.ToArray()));
if (File.Exists(filePath))
{
await context.Response.SendFileAsync(filePath);
return;
}
}
// Fallback to embedded stream
using Stream stream = assembly.GetManifestResourceStream(str);
if (stream != null)
{
await stream.CopyToAsync(context.Response.Body);
}
});
}
}
}
private static async Task ServeScoreboard(HttpContext context)
{
string file = ReadResource("default_scoreboard.html");
string[] columns = { "player_name", "points", "assists", "saves", "stuns" };
var matchStats = GetMatchStats();
string overlayOrangeTeamName = "ORANGE TEAM";
string overlayBlueTeamName = "BLUE TEAM";
switch (SparkSettings.instance.overlaysTeamSource)
{
case 0:
overlayOrangeTeamName = SparkSettings.instance.overlaysManualTeamNameOrange;
overlayBlueTeamName = SparkSettings.instance.overlaysManualTeamNameBlue;
break;
case 1:
if (Program.CurrentRound != null)
{
overlayOrangeTeamName = Program.CurrentRound.teams[Team.TeamColor.orange].vrmlTeamName;
overlayBlueTeamName = Program.CurrentRound.teams[Team.TeamColor.blue].vrmlTeamName;
}
break;
}
if (string.IsNullOrWhiteSpace(overlayOrangeTeamName)) overlayOrangeTeamName = "ORANGE TEAM";
if (string.IsNullOrWhiteSpace(overlayBlueTeamName)) overlayBlueTeamName = "BLUE TEAM";
string[] teamHTMLs = new string[2];
for (int i = 0; i < 2; i++)
{
StringBuilder html = new StringBuilder();
html.Append("<thead>");
foreach (string column in columns)
{
html.Append("<th>");
if (column == "player_name")
html.Append(i == 0 ? overlayBlueTeamName : overlayOrangeTeamName);
else
html.Append(column);
html.Append("</th>");
}
html.Append("</thead><tbody>");
if (matchStats[i].Count >= 8) Logger.LogRow(Logger.LogType.Error, "8 or more players on a team.");
for (int playerIndex = 0; playerIndex < matchStats[i].Count && playerIndex < 8; playerIndex++)
{
Dictionary<string, object> player = matchStats[i][playerIndex];
html.Append("<tr>");
foreach (string column in columns)
{
html.Append("<td>");
html.Append(player.ContainsKey(column) ? player[column] : "");
html.Append("</td>");
}
html.Append("</tr>");
}
html.Append("</tbody>");
teamHTMLs[i] = html.ToString();
}
file = file.Replace("{{ BLUE_TEAM }}", teamHTMLs[0]);
file = file.Replace("{{ ORANGE_TEAM }}", teamHTMLs[1]);
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(file);
}
private static Dictionary<string, object> GetStatsResponse()
{
lock (Program.gameStateLock)
{
List<AccumulatedFrame> selectedMatches = GetPreviousRounds();
List<List<Dictionary<string, object>>> matchStats = GetMatchStats();
string overlayOrangeTeamName = "";
string overlayBlueTeamName = "";
string overlayOrangeTeamLogo = "";
string overlayBlueTeamLogo = "";
switch (SparkSettings.instance.overlaysTeamSource)
{
case 0:
overlayOrangeTeamName = SparkSettings.instance.overlaysManualTeamNameOrange;
overlayBlueTeamName = SparkSettings.instance.overlaysManualTeamNameBlue;
overlayOrangeTeamLogo = SparkSettings.instance.overlaysManualTeamLogoOrange;
overlayBlueTeamLogo = SparkSettings.instance.overlaysManualTeamLogoBlue;
break;
case 1:
var orangeTeam = selectedMatches?.LastOrDefault()?.teams[Team.TeamColor.orange];
var blueTeam = selectedMatches?.LastOrDefault()?.teams[Team.TeamColor.blue];
overlayOrangeTeamName = orangeTeam?.vrmlTeamName ?? "";
overlayBlueTeamName = blueTeam?.vrmlTeamName ?? "";
overlayOrangeTeamLogo = orangeTeam?.vrmlTeamLogo ?? "";
overlayBlueTeamLogo = blueTeam?.vrmlTeamLogo ?? "";
break;
}
return new Dictionary<string, object>
{
{
"teams", new[]
{
new Dictionary<string, object>
{
{ "vrml_team_name", selectedMatches?.LastOrDefault()?.teams[Team.TeamColor.blue].vrmlTeamName ?? "" },
{ "vrml_team_logo", selectedMatches?.LastOrDefault()?.teams[Team.TeamColor.blue].vrmlTeamLogo ?? "" },
{ "team_name", overlayBlueTeamName },
{ "team_logo", overlayBlueTeamLogo },
{ "players", matchStats?[0] }
},
new Dictionary<string, object>
{
{ "vrml_team_name", selectedMatches?.LastOrDefault()?.teams[Team.TeamColor.orange].vrmlTeamName ?? "" },
{ "vrml_team_logo", selectedMatches?.LastOrDefault()?.teams[Team.TeamColor.orange].vrmlTeamLogo ?? "" },
{ "team_name", overlayOrangeTeamName },
{ "team_logo", overlayOrangeTeamLogo },
{ "players", matchStats?[1] }
}
}
},
{
"joust_events", selectedMatches?
.SelectMany(m => m.events)
.Where(e => e.eventType is EventContainer.EventType.joust_speed or EventContainer.EventType.defensive_joust)
.Select(e => e.ToDict())
},
{ "goals", selectedMatches?.SelectMany(m => m.goals).Select(e => e.ToDict()) }
};
}
}
}
public static List<AccumulatedFrame> GetPreviousRounds()
{
List<AccumulatedFrame> selectedMatches = new List<AccumulatedFrame>();
if (Program.CurrentRound == null) return selectedMatches;
selectedMatches.Add(Program.CurrentRound);
AccumulatedFrame current = Program.CurrentRound;
// Limit the depth to prevent infinite loops or excessive memory usage
int depth = 0;
while (current.lastRound != null && depth < 20)
{
selectedMatches.Add(current.lastRound);
current = current.lastRound;
depth++;
}
selectedMatches.Reverse();
return selectedMatches;
}
public static string ReadResource(string name)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourcePath = assembly.GetManifestResourceNames().FirstOrDefault(str => str.EndsWith(name));
if (resourcePath == null) return "";
using Stream stream = assembly.GetManifestResourceStream(resourcePath);
if (stream == null) return "";
using StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
public static List<List<Dictionary<string, object>>> GetMatchStats()
{
List<AccumulatedFrame> selectedMatches = GetPreviousRounds();
// Helper to merge players efficiently
Dictionary<string, MatchPlayer> MergePlayers(Team.TeamColor color)
{
var dict = new Dictionary<string, MatchPlayer>();
var players = selectedMatches.SelectMany(m => m.players.Values).Where(p => p.TeamColor == color);
foreach (var p in players)
{
if (dict.TryGetValue(p.Name, out MatchPlayer existing))
{
dict[p.Name] += p;
}
else
{
dict[p.Name] = new MatchPlayer(p);
}
}
return dict;
}
var bluePlayers = MergePlayers(Team.TeamColor.blue);
var orangePlayers = MergePlayers(Team.TeamColor.orange);
return new List<List<Dictionary<string, object>>>
{
bluePlayers.Values.Select(p => p.ToDict()).ToList(),
orangePlayers.Values.Select(p => p.ToDict()).ToList(),
};
}
public static async Task<List<Dictionary<string, float>>> GetDiscPositions()
{
if (string.IsNullOrEmpty(SparkSettings.instance.saveFolder) || !Directory.Exists(SparkSettings.instance.saveFolder))
return new List<Dictionary<string, float>>();
// Optimize: use EnumerateFiles and don't load everything into memory
DirectoryInfo directory = new DirectoryInfo(SparkSettings.instance.saveFolder);
// Get round times to match against
var rounds = GetPreviousRounds();
if (rounds.Count == 0) return new List<Dictionary<string, float>>();
DateTime minTime = rounds.Select(m => m.matchTime).Min().ToUniversalTime().AddSeconds(-10);
// Find relevant files - prefer butter, fallback to echoreplay
// Check only recent files to save time
var filesToCheck = directory.EnumerateFiles("rec_*.butter", SearchOption.TopDirectoryOnly)
.OrderByDescending(f => f.LastWriteTime)
.Take(10) // Check last 10 files max
.ToList();
if (!filesToCheck.Any())
{
filesToCheck = directory.EnumerateFiles("rec_*.echoreplay", SearchOption.TopDirectoryOnly)
.OrderByDescending(f => f.LastWriteTime)
.Take(10)
.ToList();
}
var selectedFiles = new List<FileInfo>();
foreach (var f in filesToCheck)
{
if (f.Name.Length < 23) continue; // "rec_yyyy-MM-dd_HH-mm-ss" length check
if (DateTime.TryParseExact(f.Name.Substring(4, 19), "yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out DateTime time))
{
if (time.ToUniversalTime() > minTime)
{
selectedFiles.Add(f);
}
}
}
if (selectedFiles.Count == 0) return new List<Dictionary<string, float>>();
List<Dictionary<string, float>> positions = new List<Dictionary<string, float>>(1000);
foreach (FileInfo file in selectedFiles)
{
ReplayFileReader reader = new ReplayFileReader();
ReplayFile replayFile = await reader.LoadFileAsync(file.FullName, true);
if (replayFile == null) continue;
// Loop through every nth frame to reduce data size and processing time
const int n = 5;
for (int i = 0; i < replayFile.nframes; i += n)
{
Frame frame = replayFile.GetFrame(i);
if (frame != null && frame.game_status == "playing")
{
Vector3 pos = frame.disc.position.ToVector3();
positions.Add(new Dictionary<string, float>
{
{ "x", pos.X },
{ "y", pos.Y },
{ "z", pos.Z },
});
}
}
}
return positions;
}
}
}