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