RpcSaveResult.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = DatabaseSchema.Property(Type, key);
  25. //var prop = CoreUtils.GetProperty(Type, key);
  26. writer.WriteBinaryValue(prop?.PropertyType ?? typeof(object), value);
  27. }
  28. }
  29. }
  30. public void DeserializeBinary(CoreBinaryReader reader)
  31. {
  32. var deltas = new List<Dictionary<string, object?>>();
  33. var typename = reader.ReadString();
  34. Type = CoreUtils.GetEntity(typename);
  35. var deltacount = reader.ReadInt32();
  36. for (int iDelta = 0; iDelta < deltacount; iDelta++)
  37. {
  38. var delta = new Dictionary<string, object?>();
  39. var keycount = reader.ReadInt32();
  40. for (int iKey = 0; iKey < keycount; iKey++)
  41. {
  42. var key = reader.ReadString();
  43. var prop = DatabaseSchema.Property(Type, key);
  44. //var prop = CoreUtils.GetProperty(Type, key);
  45. var value = reader.ReadBinaryValue(prop?.PropertyType ?? typeof(object));
  46. delta[key] = value;
  47. }
  48. deltas.Add(delta);
  49. }
  50. Deltas = deltas.ToArray();
  51. }
  52. public string? FullDescription() => null;
  53. }
  54. }