UserProperties.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using Newtonsoft.Json;
  6. namespace InABox.Core
  7. {
  8. public class UserProperty : IPackable
  9. {
  10. [DoNotSerialize]
  11. public Type Type { get; set; }
  12. public object? Value { get; set; }
  13. public void Pack(FastBinaryWriter writer)
  14. {
  15. writer.Write(Type.EntityName());
  16. writer.Write(Value?.ToString() ?? "");
  17. }
  18. public void Unpack(FastBinaryReader reader)
  19. {
  20. Type = CoreUtils.GetEntity(reader.ReadString());
  21. Value = CoreUtils.ChangeType(reader.ReadString(), Type);
  22. }
  23. }
  24. public class UserPropertyDictionary : PackableDictionary<UserProperty>
  25. {
  26. public override void PackItem(FastBinaryWriter writer, UserProperty value)
  27. {
  28. value.Pack(writer);
  29. }
  30. public override UserProperty UnpackItem(FastBinaryReader reader)
  31. {
  32. var result = new UserProperty();
  33. result.Unpack(reader);
  34. return result;
  35. }
  36. }
  37. [Serializable]
  38. public class UserProperties : IPackable
  39. {
  40. public delegate void PropertyChangedEventHandler(object sender, string name, object? before, object? after);
  41. public UserProperties()
  42. {
  43. Dictionary = new UserPropertyDictionary();
  44. //_dictionary = new Dictionary<string, UserProperty>();
  45. }
  46. public UserPropertyDictionary Dictionary { get; set; }
  47. //public Type ParentType { get; set; }
  48. public object? this[string key]
  49. {
  50. get => Get(key);
  51. set => Set(key, value);
  52. }
  53. [JsonIgnore]
  54. public Dictionary<string, object?> AsDictionary
  55. {
  56. get
  57. {
  58. var result = new Dictionary<string, object?>();
  59. foreach (var key in Dictionary.Keys)
  60. result[key] = this[key];
  61. return result;
  62. }
  63. set
  64. {
  65. Clear();
  66. foreach (var key in value.Keys)
  67. this[key] = value[key];
  68. }
  69. }
  70. [JsonIgnore]
  71. public int Count => Dictionary.Count;
  72. public void Pack(FastBinaryWriter writer)
  73. {
  74. Dictionary.Pack(writer);
  75. }
  76. public void Unpack(FastBinaryReader reader)
  77. {
  78. Dictionary.Unpack(reader);
  79. }
  80. public override bool Equals(object obj)
  81. {
  82. if (!(obj is UserProperties other))
  83. return false;
  84. if (Dictionary.Count != other.Count) // Require equal count.
  85. return false;
  86. foreach (var pair in Dictionary)
  87. try
  88. {
  89. var value = other[pair.Key];
  90. if (value == null)
  91. {
  92. if (pair.Value != null)
  93. return false;
  94. }
  95. else if (!value.Equals(pair.Value))
  96. {
  97. return false;
  98. }
  99. }
  100. catch
  101. {
  102. return false;
  103. }
  104. return true;
  105. }
  106. public event PropertyChangedEventHandler? OnPropertyChanged;
  107. private object? Get(string key)
  108. {
  109. var prop = new UserProperty { Type = typeof(string), Value = "" };
  110. if (Dictionary.ContainsKey(key))
  111. prop = Dictionary[key];
  112. if (prop.Type == null)
  113. return prop.Value;
  114. if (prop.Value == null)
  115. return prop.Type == typeof(object) ? null : Activator.CreateInstance(prop.Type);
  116. if (prop.Value.GetType() == prop.Type)
  117. return prop.Value;
  118. var tc = TypeDescriptor.GetConverter(prop.Type);
  119. return tc.ConvertFrom(prop.Value);
  120. }
  121. private void Set(string key, object? obj)
  122. {
  123. var before = Dictionary.GetValueOrDefault(key);
  124. Dictionary[key] = new UserProperty { Type = obj?.GetType() ?? typeof(object), Value = obj };
  125. OnPropertyChanged?.Invoke(this, key, before, obj);
  126. }
  127. public bool ContainsKey(string key)
  128. {
  129. return Dictionary.ContainsKey(key);
  130. }
  131. public void Clear()
  132. {
  133. Dictionary.Clear();
  134. }
  135. public string[] GetKeys()
  136. {
  137. return Dictionary.Keys.ToArray();
  138. }
  139. internal void LoadFromDictionary(Dictionary<string, object?> defaultProperties)
  140. {
  141. foreach (var pair in defaultProperties)
  142. Dictionary[pair.Key] = new UserProperty { Type = pair.Value != null ? pair.Value.GetType() : typeof(object), Value = pair.Value };
  143. }
  144. }
  145. public class UserPropertiesJsonConverter : JsonConverter
  146. {
  147. public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
  148. {
  149. if (!(value is UserProperties props)) return;
  150. writer.WriteStartObject();
  151. foreach (var key in props.GetKeys())
  152. if (props[key] != null)
  153. {
  154. writer.WritePropertyName(key);
  155. writer.WriteValue(props[key] is string ? props[key] as string : props[key]);
  156. }
  157. writer.WriteEndObject();
  158. }
  159. public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
  160. {
  161. var result = new UserProperties();
  162. while (reader.TokenType != JsonToken.EndObject && reader.Read())
  163. if (reader.Value != null)
  164. {
  165. var key = reader.Value.ToString();
  166. reader.Read();
  167. result[key] = reader.Value;
  168. }
  169. return result;
  170. }
  171. public override bool CanConvert(Type objectType)
  172. {
  173. return objectType == typeof(UserProperties);
  174. }
  175. }
  176. }