forked from DeltaV-Station/Delta-v-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelSqlite.cs
146 lines (120 loc) · 4.65 KB
/
ModelSqlite.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
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
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Content.Server.Database
{
public sealed class SqliteServerDbContext : ServerDbContext
{
public SqliteServerDbContext(DbContextOptions<SqliteServerDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
options.ConfigureWarnings(x =>
{
x.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
#if DEBUG
// for tests
x.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning);
#endif
});
#if DEBUG
options.EnableSensitiveDataLogging();
#endif
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var ipConverter = new ValueConverter<IPAddress, string>(
v => v.ToString(),
v => IPAddress.Parse(v));
modelBuilder.Entity<Player>()
.Property(p => p.LastSeenAddress)
.HasConversion(ipConverter);
var ipMaskConverter = new ValueConverter<(IPAddress address, int mask), string>(
v => InetToString(v.address, v.mask),
v => StringToInet(v)
);
modelBuilder
.Entity<ServerBan>()
.Property(e => e.Address)
.HasColumnType("TEXT")
.HasConversion(ipMaskConverter);
modelBuilder
.Entity<ServerRoleBan>()
.Property(e => e.Address)
.HasColumnType("TEXT")
.HasConversion(ipMaskConverter);
var jsonStringConverter = new ValueConverter<JsonDocument, string>(
v => JsonDocumentToString(v),
v => StringToJsonDocument(v));
var jsonByteArrayConverter = new ValueConverter<JsonDocument?, byte[]>(
v => JsonDocumentToByteArray(v),
v => ByteArrayToJsonDocument(v));
modelBuilder.Entity<AdminLog>()
.Property(log => log.Json)
.HasConversion(jsonStringConverter);
modelBuilder.Entity<Profile>()
.Property(log => log.Markings)
.HasConversion(jsonByteArrayConverter);
}
public override int CountAdminLogs()
{
return AdminLog.Count();
}
private static string InetToString(IPAddress address, int mask) {
if (address.IsIPv4MappedToIPv6)
{
// Fix IPv6-mapped IPv4 addresses
// So that IPv4 addresses are consistent between separate-socket and dual-stack socket modes.
address = address.MapToIPv4();
mask -= 96;
}
return $"{address}/{mask}";
}
private static (IPAddress, int) StringToInet(string inet) {
var idx = inet.IndexOf('/', StringComparison.Ordinal);
return (
IPAddress.Parse(inet.AsSpan(0, idx)),
int.Parse(inet.AsSpan(idx + 1), provider: CultureInfo.InvariantCulture)
);
}
private static string JsonDocumentToString(JsonDocument document)
{
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions {Indented = false});
document.WriteTo(writer);
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}
private static JsonDocument StringToJsonDocument(string str)
{
return JsonDocument.Parse(str);
}
private static byte[] JsonDocumentToByteArray(JsonDocument? document)
{
if (document == null)
{
return Array.Empty<byte>();
}
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions {Indented = false});
document.WriteTo(writer);
writer.Flush();
return stream.ToArray();
}
private static JsonDocument ByteArrayToJsonDocument(byte[] str)
{
return JsonDocument.Parse(str);
}
}
}