| | | 1 | | using MemoryPack; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | |
| | | 6 | | namespace Chronicler; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Serializes <see cref="IRecordable"/> state graphs to and from MemoryPack through the chronicler API. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class MemoryPackRecordSerializer |
| | | 12 | | { |
| | 1 | 13 | | private static readonly byte[] EmptyRecordBytes = MemoryPackSerializer.Serialize(new MemoryPackRecordEnvelope()); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Serializes the current state of a recordable instance into MemoryPack bytes. |
| | | 17 | | /// </summary> |
| | | 18 | | public static byte[] Serialize(IRecordable target) |
| | 6 | 19 | | => Serialize(target, context: null); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Serializes the current state of a recordable instance into MemoryPack bytes. |
| | | 23 | | /// </summary> |
| | | 24 | | public static byte[] Serialize(IRecordable target, ChronicleContext? context) |
| | 29 | 25 | | { |
| | 29 | 26 | | if (target == null) |
| | 1 | 27 | | throw new ArgumentNullException(nameof(target)); |
| | | 28 | | |
| | 28 | 29 | | context ??= new ChronicleContext(); |
| | | 30 | | |
| | 28 | 31 | | var chronicler = new MemoryPackRecordWriter(context); |
| | 28 | 32 | | target.RecordData(chronicler); |
| | 27 | 33 | | return chronicler.ToArray(); |
| | 27 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Loads MemoryPack state into an existing recordable instance. |
| | | 38 | | /// </summary> |
| | | 39 | | public static void Populate(IRecordable target, byte[] data) |
| | 3 | 40 | | => Populate(target, data, context: null); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Loads MemoryPack state into an existing recordable instance. |
| | | 44 | | /// </summary> |
| | | 45 | | public static void Populate(IRecordable target, byte[] data, ChronicleContext? context) |
| | 24 | 46 | | { |
| | 24 | 47 | | if (data == null) |
| | 1 | 48 | | throw new ArgumentNullException(nameof(data)); |
| | | 49 | | |
| | 23 | 50 | | Populate(target, data.AsSpan(), context); |
| | 17 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Loads MemoryPack state into an existing recordable instance. |
| | | 55 | | /// </summary> |
| | | 56 | | public static void Populate(IRecordable target, ReadOnlySpan<byte> data) |
| | 2 | 57 | | => Populate(target, data, context: null); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Loads MemoryPack state into an existing recordable instance. |
| | | 61 | | /// </summary> |
| | | 62 | | public static void Populate(IRecordable target, ReadOnlySpan<byte> data, ChronicleContext? context) |
| | 25 | 63 | | { |
| | 25 | 64 | | if (target == null) |
| | 1 | 65 | | throw new ArgumentNullException(nameof(target)); |
| | 24 | 66 | | if (data.IsEmpty) |
| | 1 | 67 | | throw new ArgumentException("Serialized bytes must not be empty.", nameof(data)); |
| | | 68 | | |
| | 23 | 69 | | context ??= new ChronicleContext(); |
| | | 70 | | |
| | 23 | 71 | | var chronicler = new MemoryPackRecordReader(data, context); |
| | 23 | 72 | | target.RecordData(chronicler); |
| | 20 | 73 | | context.ResolveDeferredLinks(); |
| | 18 | 74 | | } |
| | | 75 | | |
| | | 76 | | private sealed class MemoryPackRecordWriter : IChronicler |
| | | 77 | | { |
| | 43 | 78 | | private readonly SwiftDictionary<string, byte[]?> _entries = new(8, StringComparer.Ordinal); |
| | | 79 | | |
| | 43 | 80 | | public MemoryPackRecordWriter(ChronicleContext context) |
| | 43 | 81 | | { |
| | 43 | 82 | | Context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 43 | 83 | | } |
| | | 84 | | |
| | 27 | 85 | | public ChronicleContext Context { get; } |
| | | 86 | | |
| | 16 | 87 | | public SerializationMode Mode => SerializationMode.Saving; |
| | | 88 | | |
| | | 89 | | public void LookValue<T>(ref T value, string name, T? defaultValue = default) |
| | 43 | 90 | | { |
| | 43 | 91 | | if (value is null || EqualityComparer<T>.Default.Equals(value, defaultValue!)) |
| | 9 | 92 | | return; |
| | | 93 | | |
| | 34 | 94 | | _entries[name] = MemoryPackSerializer.Serialize(value); |
| | 43 | 95 | | } |
| | | 96 | | |
| | | 97 | | public void LookDeep<T>(ref T value, string name) where T : class, IRecordable |
| | 10 | 98 | | { |
| | 10 | 99 | | if (value == null) |
| | 1 | 100 | | { |
| | 1 | 101 | | _entries[name] = null; |
| | 1 | 102 | | return; |
| | | 103 | | } |
| | | 104 | | |
| | 9 | 105 | | var nested = new MemoryPackRecordWriter(Context); |
| | 9 | 106 | | value.RecordData(nested); |
| | 9 | 107 | | _entries[name] = nested.ToArray(); |
| | 10 | 108 | | } |
| | | 109 | | |
| | | 110 | | public void LookDeepStruct<T>(ref T value, string name) where T : struct, IRecordable |
| | 4 | 111 | | { |
| | 4 | 112 | | var nested = new MemoryPackRecordWriter(Context); |
| | 4 | 113 | | value.RecordData(nested); |
| | 4 | 114 | | _entries[name] = nested.ToArray(); |
| | 4 | 115 | | } |
| | | 116 | | |
| | | 117 | | public void LookNullableDeep<T>(ref T? value, string name) where T : struct, IRecordable |
| | 4 | 118 | | { |
| | 4 | 119 | | if (!value.HasValue) |
| | 2 | 120 | | return; |
| | | 121 | | |
| | 2 | 122 | | T nestedValue = value.Value; |
| | 2 | 123 | | var nested = new MemoryPackRecordWriter(Context); |
| | 2 | 124 | | nestedValue.RecordData(nested); |
| | 2 | 125 | | _entries[name] = nested.ToArray(); |
| | 4 | 126 | | } |
| | | 127 | | |
| | | 128 | | public void LookLink<T>( |
| | | 129 | | ref T value, |
| | | 130 | | string name, |
| | | 131 | | string? slot = null, |
| | | 132 | | RecordLinkResolveMode resolveMode = RecordLinkResolveMode.Immediate, |
| | | 133 | | Action<T>? assignLoadedValue = null) |
| | 12 | 134 | | { |
| | 12 | 135 | | string? id = null; |
| | 12 | 136 | | if (value is not null |
| | 12 | 137 | | && !Context.Links.TryGetReferenceId(value, out id, slot)) |
| | 1 | 138 | | { |
| | 1 | 139 | | throw new InvalidOperationException( |
| | 1 | 140 | | $"Unable to save link '{name}' of type {typeof(T).Name} because no stable id could be produced{Forma |
| | | 141 | | } |
| | | 142 | | |
| | 11 | 143 | | _entries[name] = MemoryPackSerializer.Serialize(id); |
| | 11 | 144 | | } |
| | | 145 | | |
| | | 146 | | public byte[] ToArray() |
| | 42 | 147 | | { |
| | 42 | 148 | | return MemoryPackSerializer.Serialize(new MemoryPackRecordEnvelope() |
| | 42 | 149 | | { |
| | 42 | 150 | | Entries = _entries |
| | 42 | 151 | | }); |
| | 42 | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | private sealed class MemoryPackRecordReader : IChronicler |
| | | 156 | | { |
| | | 157 | | private readonly SwiftDictionary<string, byte[]?> _entries; |
| | | 158 | | |
| | 38 | 159 | | public MemoryPackRecordReader(ReadOnlySpan<byte> data, ChronicleContext context) |
| | 38 | 160 | | { |
| | 38 | 161 | | MemoryPackRecordEnvelope? envelope = MemoryPackSerializer.Deserialize<MemoryPackRecordEnvelope>(data); |
| | 38 | 162 | | _entries = envelope?.Entries ?? new SwiftDictionary<string, byte[]?>(8, StringComparer.Ordinal); |
| | 38 | 163 | | Context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 38 | 164 | | } |
| | | 165 | | |
| | 26 | 166 | | public ChronicleContext Context { get; } |
| | | 167 | | |
| | 12 | 168 | | public SerializationMode Mode => SerializationMode.Loading; |
| | | 169 | | |
| | | 170 | | public void LookValue<T>(ref T value, string name, T? defaultValue = default) |
| | 40 | 171 | | { |
| | 40 | 172 | | if (!_entries.TryGetValue(name, out byte[]? entry) |
| | 40 | 173 | | || entry == null) |
| | 20 | 174 | | { |
| | 20 | 175 | | value = defaultValue!; |
| | 20 | 176 | | return; |
| | | 177 | | } |
| | | 178 | | |
| | 20 | 179 | | T? loadedValue = MemoryPackSerializer.Deserialize<T>(entry); |
| | 20 | 180 | | if (loadedValue is null) |
| | 1 | 181 | | { |
| | 1 | 182 | | value = defaultValue!; |
| | 1 | 183 | | return; |
| | | 184 | | } |
| | | 185 | | |
| | 19 | 186 | | value = loadedValue; |
| | 40 | 187 | | } |
| | | 188 | | |
| | | 189 | | public void LookDeep<T>(ref T value, string name) where T : class, IRecordable |
| | 9 | 190 | | { |
| | 9 | 191 | | if (!_entries.TryGetValue(name, out byte[]? entry) |
| | 9 | 192 | | || entry == null) |
| | 2 | 193 | | return; |
| | | 194 | | |
| | 7 | 195 | | if (value == null) |
| | 1 | 196 | | throw new InvalidOperationException( |
| | 1 | 197 | | $"Unable to load '{name}' because {typeof(T).Name} must already be instantiated for a deep chronicle |
| | | 198 | | |
| | 6 | 199 | | var nested = new MemoryPackRecordReader(entry, Context); |
| | 6 | 200 | | value.RecordData(nested); |
| | 8 | 201 | | } |
| | | 202 | | |
| | | 203 | | public void LookDeepStruct<T>(ref T value, string name) where T : struct, IRecordable |
| | 4 | 204 | | { |
| | 4 | 205 | | value = CreateDefaultDeepStruct<T>(); |
| | | 206 | | |
| | 4 | 207 | | if (!_entries.TryGetValue(name, out byte[]? entry) |
| | 4 | 208 | | || entry == null) |
| | 1 | 209 | | return; |
| | | 210 | | |
| | 3 | 211 | | var nested = new MemoryPackRecordReader(entry, Context); |
| | 3 | 212 | | value.RecordData(nested); |
| | 4 | 213 | | } |
| | | 214 | | |
| | | 215 | | public void LookNullableDeep<T>(ref T? value, string name) where T : struct, IRecordable |
| | 4 | 216 | | { |
| | 4 | 217 | | if (!_entries.TryGetValue(name, out byte[]? entry) |
| | 4 | 218 | | || entry == null) |
| | 3 | 219 | | { |
| | 3 | 220 | | value = null; |
| | 3 | 221 | | return; |
| | | 222 | | } |
| | | 223 | | |
| | 1 | 224 | | T nestedValue = CreateDefaultDeepStruct<T>(); |
| | 1 | 225 | | var nested = new MemoryPackRecordReader(entry, Context); |
| | 1 | 226 | | nestedValue.RecordData(nested); |
| | 1 | 227 | | value = nestedValue; |
| | 4 | 228 | | } |
| | | 229 | | |
| | | 230 | | public void LookLink<T>( |
| | | 231 | | ref T value, |
| | | 232 | | string name, |
| | | 233 | | string? slot = null, |
| | | 234 | | RecordLinkResolveMode resolveMode = RecordLinkResolveMode.Immediate, |
| | | 235 | | Action<T>? assignLoadedValue = null) |
| | 10 | 236 | | { |
| | 10 | 237 | | if (!_entries.TryGetValue(name, out byte[]? entry) |
| | 10 | 238 | | || entry == null) |
| | 1 | 239 | | { |
| | 1 | 240 | | value = default!; |
| | 1 | 241 | | return; |
| | | 242 | | } |
| | | 243 | | |
| | 9 | 244 | | string? id = MemoryPackSerializer.Deserialize<string>(entry); |
| | 9 | 245 | | if (id == null) |
| | 1 | 246 | | { |
| | 1 | 247 | | value = default!; |
| | 1 | 248 | | return; |
| | | 249 | | } |
| | | 250 | | |
| | 8 | 251 | | if (resolveMode == RecordLinkResolveMode.Deferred) |
| | 5 | 252 | | { |
| | 5 | 253 | | if (assignLoadedValue == null) |
| | 1 | 254 | | throw new InvalidOperationException( |
| | 1 | 255 | | $"Deferred link '{name}' of type {typeof(T).Name} requires an assignment callback."); |
| | | 256 | | |
| | | 257 | | T? deferredValue; |
| | 4 | 258 | | if (Context.Links.TryResolve(id, out deferredValue, slot)) |
| | 1 | 259 | | { |
| | 1 | 260 | | value = deferredValue!; |
| | 1 | 261 | | assignLoadedValue(deferredValue!); |
| | 1 | 262 | | return; |
| | | 263 | | } |
| | | 264 | | |
| | 3 | 265 | | Context.QueueDeferredLink(name, id, slot, assignLoadedValue); |
| | 3 | 266 | | value = default!; |
| | 3 | 267 | | return; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | T? resolvedValue; |
| | 3 | 271 | | if (!Context.Links.TryResolve(id, out resolvedValue, slot)) |
| | 1 | 272 | | { |
| | 1 | 273 | | throw new InvalidOperationException( |
| | 1 | 274 | | $"Unable to load link '{name}' of type {typeof(T).Name} with id '{id}'{FormatSlot(slot)}."); |
| | | 275 | | } |
| | | 276 | | |
| | 2 | 277 | | value = resolvedValue!; |
| | 2 | 278 | | assignLoadedValue?.Invoke(resolvedValue!); |
| | 8 | 279 | | } |
| | | 280 | | |
| | | 281 | | private T CreateDefaultDeepStruct<T>() where T : struct, IRecordable |
| | 5 | 282 | | { |
| | 5 | 283 | | T defaultValue = new(); |
| | 5 | 284 | | var nested = new MemoryPackRecordReader(EmptyRecordBytes, Context); |
| | 5 | 285 | | defaultValue.RecordData(nested); |
| | 5 | 286 | | return defaultValue; |
| | 5 | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | private static string FormatSlot(string? slot) |
| | 2 | 291 | | { |
| | 2 | 292 | | return string.IsNullOrEmpty(slot) |
| | 2 | 293 | | ? string.Empty |
| | 2 | 294 | | : $" in slot '{slot}'"; |
| | 2 | 295 | | } |
| | | 296 | | } |