-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathTimeDilationPlugin.cs
95 lines (84 loc) · 3.43 KB
/
TimeDilationPlugin.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
using System.Globalization;
using AssettoServer.Server.Configuration;
using AssettoServer.Server.Plugin;
using AssettoServer.Server.Weather;
using AssettoServer.Shared.Services;
using AssettoServer.Utils;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace TimeDilationPlugin;
public class TimeDilationPlugin : CriticalBackgroundService, IAssettoServerAutostart
{
private readonly WeatherManager _weatherManager;
private readonly ACServerConfiguration _serverConfiguration;
private readonly TimeDilationConfiguration _configuration;
public TimeDilationPlugin(TimeDilationConfiguration configuration,
WeatherManager weatherManager,
ACServerConfiguration serverConfiguration,
IHostApplicationLifetime applicationLifetime) : base(applicationLifetime)
{
_weatherManager = weatherManager;
_serverConfiguration = serverConfiguration;
_configuration = configuration;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
return _configuration.Mode == TimeDilationMode.Time
? TimeBasedTimeDilation(stoppingToken)
: SunPositionTimeDilation(stoppingToken);
}
private async Task SunPositionTimeDilation(CancellationToken stoppingToken)
{
if (!_weatherManager.CurrentSunPosition.HasValue)
{
Log.Error("TimeDilationPlugin cannot get current sun position, aborting");
return;
}
var lookupTable = CreateSunAngleBasedLookupTable(_configuration.SunAngleLookupTable);
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
do
{
try
{
var sunAltitudeDeg = _weatherManager.CurrentSunPosition.Value.Altitude * 180.0 / Math.PI;
_serverConfiguration.Server.TimeOfDayMultiplier = (float)lookupTable.GetValue(sunAltitudeDeg);
}
catch (Exception ex)
{
Log.Error(ex, "Error during time dilation update");
}
} while (await timer.WaitForNextTickAsync(stoppingToken));
}
private async Task TimeBasedTimeDilation(CancellationToken stoppingToken)
{
var lookupTable = CreateTimeBasedLookupTable(_configuration.TimeLookupTable);
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
do
{
try
{
var liveTime = _weatherManager.CurrentDateTime.TimeOfDay.TickOfDay / 10_000_000.0;
_serverConfiguration.Server.TimeOfDayMultiplier = (float)lookupTable.GetValue(liveTime);
}
catch (Exception ex)
{
Log.Error(ex, "Error during time dilation update");
}
} while (await timer.WaitForNextTickAsync(stoppingToken));
}
private static LookupTable CreateSunAngleBasedLookupTable(List<SunAngleLUTEntry> entries)
{
return new LookupTable(entries
.Select(entry => new KeyValuePair<double, double>(entry.SunAngle, entry.TimeMult))
.ToList());
}
private static LookupTable CreateTimeBasedLookupTable(List<TimeLUTEntry> entries)
{
return new LookupTable(entries
.Select(entry =>
new KeyValuePair<double, double>(
DateTime.ParseExact(entry.Time, "H:mm", CultureInfo.InvariantCulture).TimeOfDay.TotalSeconds,
entry.TimeMult)
).ToList(), 86400);
}
}