-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (90 loc) · 4.8 KB
/
Copy pathProgram.cs
File metadata and controls
97 lines (90 loc) · 4.8 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
using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RustPlusBot.Abstractions.Credentials;
using RustPlusBot.Abstractions.Events;
using RustPlusBot.Abstractions.Time;
using RustPlusBot.Discord;
using RustPlusBot.Features.Alarms;
using RustPlusBot.Features.Chat;
using RustPlusBot.Features.Commands;
using RustPlusBot.Features.Connections;
using RustPlusBot.Features.Events;
using RustPlusBot.Features.Map;
using RustPlusBot.Features.Pairing;
using RustPlusBot.Features.Players;
using RustPlusBot.Features.StorageMonitors;
using RustPlusBot.Features.Switches;
using RustPlusBot.Features.Workspace;
using RustPlusBot.Host.Credentials;
using RustPlusBot.Persistence;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddOptions<DiscordOptions>()
.Bind(builder.Configuration.GetSection("Discord"))
.Validate(static o => !string.IsNullOrWhiteSpace(o.Token), "Discord:Token is required.")
.ValidateOnStart();
var connectionString = builder.Configuration["Database:ConnectionString"] ?? "DataSource=rustplusbot.db";
builder.Services.AddDataProtection();
builder.Services.AddSingleton<IClock, SystemClock>();
builder.Services.AddSingleton<IEventBus, InMemoryEventBus>();
builder.Services.AddSingleton<ICredentialProtector, DataProtectionCredentialProtector>();
builder.Services.AddBotPersistence(connectionString);
builder.Services.AddDiscordBot();
builder.Services.AddOptions<WorkspaceOptions>()
.Bind(builder.Configuration.GetSection("Workspace"));
builder.Services.AddWorkspace();
builder.Services.AddOptions<PairingOptions>()
.Bind(builder.Configuration.GetSection("Pairing"))
.Validate(static o => o.ProbeTimeout > TimeSpan.Zero, "Pairing:ProbeTimeout must be positive.")
.Validate(static o => o.InitialRetryDelay > TimeSpan.Zero, "Pairing:InitialRetryDelay must be positive.")
.Validate(static o => o.MaxRetryDelay >= o.InitialRetryDelay,
"Pairing:MaxRetryDelay must be at least InitialRetryDelay.")
.ValidateOnStart();
builder.Services.AddPairing();
builder.Services.AddOptions<ConnectionOptions>()
.Bind(builder.Configuration.GetSection("Connections"))
.Validate(static o => o.ConnectTimeout > TimeSpan.Zero, "Connections:ConnectTimeout must be positive.")
.Validate(static o => o.InitialRetryDelay > TimeSpan.Zero, "Connections:InitialRetryDelay must be positive.")
.Validate(static o => o.MaxRetryDelay >= o.InitialRetryDelay,
"Connections:MaxRetryDelay must be at least InitialRetryDelay.")
.Validate(static o => o.HeartbeatInterval > TimeSpan.Zero, "Connections:HeartbeatInterval must be positive.")
.Validate(static o => o.HeartbeatTimeout > TimeSpan.Zero, "Connections:HeartbeatTimeout must be positive.")
.Validate(static o => o.HeartbeatTimeout < o.HeartbeatInterval,
"Connections:HeartbeatTimeout must be less than HeartbeatInterval.")
.Validate(static o => o.MarkerPollInterval > TimeSpan.Zero, "Connections:MarkerPollInterval must be positive.")
.Validate(static o => o.MarkerPollFastInterval > TimeSpan.Zero,
"Connections:MarkerPollFastInterval must be positive.")
.Validate(static o => o.RigRadius > 0f, "Connections:RigRadius must be positive.")
.Validate(static o => o.RigActiveWindow > TimeSpan.Zero, "Connections:RigActiveWindow must be positive.")
.Validate(static o => o.RigOfflineWindow > TimeSpan.Zero, "Connections:RigOfflineWindow must be positive.")
.Validate(static o => o.RigTickInterval > TimeSpan.Zero, "Connections:RigTickInterval must be positive.")
.ValidateOnStart();
builder.Services.AddConnections();
builder.Services.AddChat();
builder.Services.AddOptions<CommandOptions>()
.Bind(builder.Configuration.GetSection("Commands"))
.Validate(static o => o.Cooldown > TimeSpan.Zero, "Commands:Cooldown must be positive.")
.ValidateOnStart();
builder.Services.AddCommands();
builder.Services.AddOptions<MaintenanceOptions>()
.Bind(builder.Configuration.GetSection("Workspace"));
builder.Services.AddEvents();
builder.Services.AddPlayers();
builder.Services.AddOptions<MapOptions>()
.Bind(builder.Configuration.GetSection("Map"))
.Validate(static o => o.MapRefreshInterval > TimeSpan.Zero, "Map:MapRefreshInterval must be positive.")
.ValidateOnStart();
builder.Services.AddMap();
builder.Services.AddSwitches();
builder.Services.AddAlarms();
builder.Services.AddStorageMonitors();
var host = builder.Build();
// Apply migrations at startup using a short-lived scope (the scoped BotDbContext is disposed
// synchronously by the scope, so no await-using is needed here).
using (var scope = host.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<BotDbContext>();
await db.Database.MigrateAsync().ConfigureAwait(false);
}
await host.RunAsync().ConfigureAwait(false);