RpcSaveResult.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using InABox.Core;
  4. namespace InABox.Rpc
  5. {
  6. public class RpcSaveResult : IRpcCommandResult
  7. {
  8. public Type Type { get; set; }
  9. public Dictionary<string, object?>[] Deltas { get; set; }
  10. public RpcSaveResult()
  11. {
  12. Deltas = Array.Empty<Dictionary<string, object?>>();
  13. }
  14. public void SerializeBinary(CoreBinaryWriter writer)
  15. {
  16. writer.Write(Type.EntityName());
  17. writer.Write(Deltas.Length);
  18. foreach (var delta in Deltas)
  19. {
  20. writer.Write(delta.Count);
  21. foreach (var (key, value) in delta)
  22. {
  23. writer.Write(key);
  24. var prop = CoreUtils.GetProperty(Type, key);
  25. writer.WriteBinaryValue(prop.PropertyType, value);
  26. }
  27. }
  28. }
  29. public void DeserializeBinary(CoreBinaryReader reader)
  30. {
  31. var deltas = new List<Dictionary<string, object?>>();
  32. var typename = reader.ReadString();
  33. Type = CoreUtils.GetEntity(typename);
  34. var deltacount = reader.ReadInt32();
  35. for (int iDelta = 0; iDelta < deltacount; iDelta++)
  36. {
  37. var delta = new Dictionary<string, object?>();
  38. var keycount = reader.ReadInt32();
  39. for (int iKey = 0; iKey < keycount; iKey++)
  40. {
  41. var key = reader.ReadString();
  42. var prop = CoreUtils.GetProperty(Type, key);
  43. var value = reader.ReadBinaryValue(prop.PropertyType);
  44. delta[key] = value;
  45. }
  46. deltas.Add(delta);
  47. }
  48. Deltas = deltas.ToArray();
  49. }
  50. public string? FullDescription() => null;
  51. }
  52. }