DFLayoutLookupValue.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Core;
  5. namespace InABox.Core
  6. {
  7. public class DFLayoutLookupValue : IDFLayoutValue<Guid>
  8. {
  9. public Guid ID { get; set; }
  10. public string Text { get; set; }
  11. public Dictionary<string, object?> Values { get; set; }
  12. public DFLayoutLookupValue()
  13. {
  14. ID = Guid.Empty;
  15. Text = "";
  16. Values = new Dictionary<string, object?>();
  17. }
  18. /*public void Load(string serialized)
  19. {
  20. if (!serialized.IsNullOrWhiteSpace())
  21. {
  22. try
  23. {
  24. var value = Serialization.Deserialize<DFLayoutLookupValue>(serialized, true);
  25. ID = value?.ID ?? Guid.Empty;
  26. Text = value?.Text ?? string.Empty;
  27. Values = value?.Values ?? new Dictionary<string, object?>();
  28. }
  29. catch
  30. {
  31. if (Guid.TryParse(serialized, out var id))
  32. {
  33. ID = id;
  34. Text = id.ToString();
  35. Values = new Dictionary<string, object?>();
  36. }
  37. else
  38. {
  39. ID = Guid.Empty;
  40. Text = serialized;
  41. Values = new Dictionary<string, object?>();
  42. }
  43. }
  44. }
  45. }*/
  46. public void Serialize(DFSaveStorageEntry storage)
  47. {
  48. storage.SetValue(Text);
  49. storage.AddValue("ID", ID);
  50. var valuesExcludingID = Values.Where(x => !String.Equals(x.Key, "ID")).ToArray();
  51. foreach(var (key, value) in valuesExcludingID)
  52. {
  53. if(value is byte[] bArr)
  54. storage.AddBlobValue(key, bArr);
  55. else
  56. storage.AddValue(key, value);
  57. }
  58. }
  59. public void Deserialize(DFLoadStorageEntry storage)
  60. {
  61. Text = storage.GetValue<string>() ?? string.Empty;
  62. ID = storage.GetValue<Guid>("ID");
  63. foreach(var (key, value) in storage.SubItems())
  64. {
  65. if (!key.Equals("ID"))
  66. {
  67. Values.Add(key, value);
  68. }
  69. }
  70. }
  71. }
  72. }