Files
tbd-station-14/Content.Server.Database/ModelSqlite.cs
Pieter-Jan Briers a6c9c36b68 Dependency update / fixes / skrungle bungle (#23745)
* Give .props files 2-space indents.

* Move to Central Package Management.

Allows us to store NuGet package versions all in one place. Yay!

* Update NuGet packages and fix code for changes.

Notable:

Changes to ILVerify.
Npgsql doesn't need hacks for inet anymore, now we need hacks to make the old code work with this new reality.
NUnit's analyzers are already complaining and I didn't even update it to 4.x yet.
TerraFX changed to GetLastSystemError so error handling had to be changed.
Buncha APIs have more NRT annotations.

* Remove dotnet-eng NuGet package source.

I genuinely don't know what this was for, and Central Package Management starts throwing warnings about it, so YEET.

* Remove Robust.Physics project.

Never used.

* Remove erroneous NVorbis reference.

Should be VorbisPizza and otherwise wasn't used.

* Sandbox fixes

* Remove unused unit test package references.

Castle.Core and NUnit.ConsoleRunner.

* Update NUnit to 4.0.1

This requires replacing all the old assertion methods because they removed them 🥲

* Oh so that's what dotnet-eng was used for. Yeah ok that makes sense.

* Add Robust.Analyzers.Test

* Update submodule

* commit to re-run CI
2024-01-12 23:22:01 +01:00

148 lines
4.7 KiB
C#

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;
using NpgsqlTypes;
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<NpgsqlInet, string>(
v => InetToString(v.Address, v.Netmask),
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 NpgsqlInet StringToInet(string inet) {
var idx = inet.IndexOf('/', StringComparison.Ordinal);
return new NpgsqlInet(
IPAddress.Parse(inet.AsSpan(0, idx)),
byte.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);
}
}
}