Initial commit
This commit is contained in:
commit
137cb2ce13
215 changed files with 659023 additions and 0 deletions
45
src/GameCult.Caching.MessagePack/DatabaseEntryResolver.cs
Normal file
45
src/GameCult.Caching.MessagePack/DatabaseEntryResolver.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace GameCult.Caching.MessagePack
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolver that knows how to resolve:<br/>
|
||||
/// 1. Source-generated singleton formatters for DatabaseEntry subclasses<br/>
|
||||
/// 2. Generic DatabaseLink<T> formatter
|
||||
/// </summary>
|
||||
public sealed class DatabaseEntryResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly DatabaseEntryResolver Instance = new DatabaseEntryResolver();
|
||||
|
||||
private readonly ConcurrentDictionary<Type, object> _formatterCache = new();
|
||||
|
||||
private DatabaseEntryResolver() { }
|
||||
|
||||
public IMessagePackFormatter<T>? GetFormatter<T>()
|
||||
{
|
||||
// 1. Check generated resolver mapping
|
||||
var fmt = GeneratedDatabaseEntryResolvers.GetFormatter<T>();
|
||||
if (fmt != null) return fmt;
|
||||
|
||||
// 2. Provide generic DatabaseLink<T> formatter
|
||||
var t = typeof(T);
|
||||
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(DatabaseLink<>))
|
||||
{
|
||||
return (IMessagePackFormatter<T>)_formatterCache.GetOrAdd(
|
||||
t,
|
||||
static linkType =>
|
||||
{
|
||||
var valueType = linkType.GetGenericArguments()[0];
|
||||
var formatterType = typeof(DatabaseLinkFormatter<>).MakeGenericType(valueType);
|
||||
return Activator.CreateInstance(formatterType)!;
|
||||
});
|
||||
}
|
||||
|
||||
// 3. Nothing found
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/GameCult.Caching.MessagePack/DatabaseLinkFormatter.cs
Normal file
27
src/GameCult.Caching.MessagePack/DatabaseLinkFormatter.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace GameCult.Caching.MessagePack
|
||||
{
|
||||
public sealed class DatabaseLinkFormatter<T> : IMessagePackFormatter<DatabaseLink<T>> where T : DatabaseEntry
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, DatabaseLink<T>? value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null) { writer.WriteNil(); return; }
|
||||
writer.WriteArrayHeader(1);
|
||||
var fmt = options.Resolver.GetFormatterWithVerify<Guid>();
|
||||
fmt.Serialize(ref writer, value.LinkID, options);
|
||||
}
|
||||
|
||||
public DatabaseLink<T>? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.TryReadNil()) return null;
|
||||
var c = reader.ReadArrayHeader();
|
||||
if (c != 1) throw new MessagePackSerializationException($"Invalid DatabaseLink<{typeof(T).Name}> array length {c}, expected 1");
|
||||
var id = options.Resolver.GetFormatterWithVerify<Guid>().Deserialize(ref reader, options);
|
||||
var link = new DatabaseLink<T> { LinkID = id };
|
||||
return link;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Description>File-based MessagePack backing store for CultCache</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GameCult.Caching\GameCult.Caching.csproj" />
|
||||
<!-- Bring in analyzers project so its DLL is packed alongside -->
|
||||
<ProjectReference Include="..\GameCult.Caching.MessagePack.Analyzers\GameCult.Caching.MessagePack.Analyzers.csproj"
|
||||
PrivateAssets="none" />
|
||||
<PackageReference Include="MessagePack" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
// This stub ensures the project compiles before the source generator runs.
|
||||
|
||||
using MessagePack.Formatters;
|
||||
|
||||
namespace GameCult.Caching.MessagePack
|
||||
{
|
||||
static partial class GeneratedDatabaseEntryResolvers
|
||||
{
|
||||
// The generator will emit the real implementation.
|
||||
public static IMessagePackFormatter<T>? GetFormatter<T>() => null;
|
||||
}
|
||||
}
|
||||
35
src/GameCult.Caching.MessagePack/MessagePackBackingStore.cs
Normal file
35
src/GameCult.Caching.MessagePack/MessagePackBackingStore.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using MessagePack;
|
||||
|
||||
namespace GameCult.Caching.MessagePack;
|
||||
|
||||
public class SingleFileMessagePackBackingStore : SingleFileBackingStore
|
||||
{
|
||||
public SingleFileMessagePackBackingStore(string filePath) : base(filePath) { }
|
||||
|
||||
public override byte[] Serialize(DatabaseEntry[] entries)
|
||||
{
|
||||
return MessagePackSerializer.Serialize(entries);
|
||||
}
|
||||
|
||||
public override DatabaseEntry[] Deserialize(byte[] data)
|
||||
{
|
||||
return MessagePackSerializer.Deserialize<DatabaseEntry[]>(data);
|
||||
}
|
||||
}
|
||||
|
||||
public class MultiFileMessagePackBackingStore : MultiFileBackingStore
|
||||
{
|
||||
public MultiFileMessagePackBackingStore(string path) : base(path) { }
|
||||
|
||||
public override byte[] Serialize(DatabaseEntry entry)
|
||||
{
|
||||
return MessagePackSerializer.Serialize(entry);
|
||||
}
|
||||
|
||||
public override DatabaseEntry Deserialize(byte[] data)
|
||||
{
|
||||
return MessagePackSerializer.Deserialize<DatabaseEntry>(data);
|
||||
}
|
||||
|
||||
public override string Extension => "msgpack";
|
||||
}
|
||||
Loading…
Reference in a new issue