wabbajack/Wabbajack.Server/DataLayer/Mappers.cs

109 lines
3.0 KiB
C#
Raw Normal View History

2020-05-09 22:16:16 +00:00
using System;
using System.Data;
using Dapper;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs;
using Wabbajack.DTOs.CDN;
using Wabbajack.DTOs.DownloadStates;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Paths;
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Server.DataLayer;
public partial class SqlService
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
private static DTOSerializer _dtoStatic;
static SqlService()
{
SqlMapper.AddTypeHandler(new HashMapper());
SqlMapper.AddTypeHandler(new RelativePathMapper());
SqlMapper.AddTypeHandler(new JsonMapper<IDownloadState>());
SqlMapper.AddTypeHandler(new JsonMapper<FileDefinition>());
SqlMapper.AddTypeHandler(new JsonMapper<ModlistMetadata>());
SqlMapper.AddTypeHandler(new VersionMapper());
SqlMapper.AddTypeHandler(new GameMapper());
SqlMapper.AddTypeHandler(new DateTimeHandler());
}
/// <summary>
/// Needed to make sure dates are all in UTC format
/// </summary>
private class DateTimeHandler : SqlMapper.TypeHandler<DateTime>
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
public override void SetValue(IDbDataParameter parameter, DateTime value)
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
parameter.Value = value;
2020-05-12 23:19:54 +00:00
}
2021-10-23 16:51:17 +00:00
public override DateTime Parse(object value)
{
return DateTime.SpecifyKind((DateTime) value, DateTimeKind.Utc);
2020-05-09 22:16:16 +00:00
}
2021-10-23 16:51:17 +00:00
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
private class JsonMapper<T> : SqlMapper.TypeHandler<T>
{
public override void SetValue(IDbDataParameter parameter, T value)
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
parameter.Value = _dtoStatic.Serialize(value);
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public override T Parse(object value)
{
return _dtoStatic.Deserialize<T>((string) value)!;
2020-05-09 22:16:16 +00:00
}
2021-10-23 16:51:17 +00:00
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
private class RelativePathMapper : SqlMapper.TypeHandler<RelativePath>
{
public override void SetValue(IDbDataParameter parameter, RelativePath value)
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
parameter.Value = value.ToString();
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public override RelativePath Parse(object value)
{
return (RelativePath) (string) value;
2020-05-09 22:16:16 +00:00
}
2021-10-23 16:51:17 +00:00
}
private class HashMapper : SqlMapper.TypeHandler<Hash>
{
public override void SetValue(IDbDataParameter parameter, Hash value)
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
parameter.Value = (long) value;
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public override Hash Parse(object value)
{
return Hash.FromLong((long) value);
2020-05-09 22:16:16 +00:00
}
2021-10-23 16:51:17 +00:00
}
private class VersionMapper : SqlMapper.TypeHandler<Version>
{
public override void SetValue(IDbDataParameter parameter, Version value)
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
parameter.Value = value.ToString();
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public override Version Parse(object value)
{
return Version.Parse((string) value);
2020-05-09 22:16:16 +00:00
}
2021-10-23 16:51:17 +00:00
}
private class GameMapper : SqlMapper.TypeHandler<Game>
{
public override void SetValue(IDbDataParameter parameter, Game value)
2020-05-09 22:16:16 +00:00
{
2021-10-23 16:51:17 +00:00
parameter.Value = value.ToString();
}
2020-05-09 22:16:16 +00:00
2021-10-23 16:51:17 +00:00
public override Game Parse(object value)
{
return GameRegistry.GetByFuzzyName((string) value).Game;
2020-05-09 22:16:16 +00:00
}
}
2021-10-23 16:51:17 +00:00
}