Serialization.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.WebSockets;
  7. using System.Reflection;
  8. using System.Runtime.InteropServices.ComTypes;
  9. using System.Threading;
  10. using System.Xml.Linq;
  11. using InABox.Clients;
  12. using Newtonsoft.Json;
  13. using Newtonsoft.Json.Linq;
  14. namespace InABox.Core
  15. {
  16. public class SerialisationException : Exception
  17. {
  18. public SerialisationException(string message): base(message) { }
  19. }
  20. public interface ISerializeBinary
  21. {
  22. public void SerializeBinary(CoreBinaryWriter writer);
  23. public void DeserializeBinary(CoreBinaryReader reader);
  24. }
  25. public static class Serialization
  26. {
  27. private static JsonSerializerSettings? _serializerSettings;
  28. private static JsonSerializerSettings SerializerSettings(bool indented = true)
  29. {
  30. if (_serializerSettings == null)
  31. {
  32. _serializerSettings = new JsonSerializerSettings
  33. {
  34. DateParseHandling = DateParseHandling.DateTime,
  35. DateFormatHandling = DateFormatHandling.IsoDateFormat,
  36. DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
  37. };
  38. _serializerSettings.Converters.Add(new CoreTableJsonConverter());
  39. //serializerSettings.Converters.Add(new DateTimeJsonConverter());
  40. _serializerSettings.Converters.Add(new FilterJsonConverter());
  41. _serializerSettings.Converters.Add(new ColumnJsonConverter());
  42. _serializerSettings.Converters.Add(new SortOrderJsonConverter());
  43. _serializerSettings.Converters.Add(new UserPropertiesJsonConverter());
  44. _serializerSettings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
  45. }
  46. _serializerSettings.Formatting = indented ? Formatting.Indented : Formatting.None;
  47. return _serializerSettings;
  48. }
  49. public static string Serialize(object? o, bool indented = false)
  50. {
  51. var json = JsonConvert.SerializeObject(o, SerializerSettings(indented));
  52. return json;
  53. }
  54. public static void Serialize(object o, Stream stream, bool indented = false)
  55. {
  56. var settings = SerializerSettings(indented);
  57. using (var sw = new StreamWriter(stream))
  58. {
  59. using (JsonWriter writer = new JsonTextWriter(sw))
  60. {
  61. var serializer = JsonSerializer.Create(settings);
  62. serializer.Serialize(writer, o);
  63. }
  64. }
  65. }
  66. public static void DeserializeInto(string json, object target)
  67. {
  68. JsonConvert.PopulateObject(json, target, SerializerSettings());
  69. }
  70. [return: MaybeNull]
  71. public static T Deserialize<T>(Stream? stream, bool strict = false)
  72. {
  73. if (stream == null)
  74. return default;
  75. try
  76. {
  77. var settings = SerializerSettings();
  78. using var sr = new StreamReader(stream);
  79. using JsonReader reader = new JsonTextReader(sr);
  80. var serializer = JsonSerializer.Create(settings);
  81. return serializer.Deserialize<T>(reader);
  82. }
  83. catch (Exception e)
  84. {
  85. if (strict)
  86. throw;
  87. Logger.Send(LogType.Error, ClientFactory.UserID, $"Error in Deserialize<{typeof(T)}>(): {e.Message}");
  88. return default;
  89. }
  90. }
  91. public static object? Deserialize(Type type, Stream? stream)
  92. {
  93. if (stream == null)
  94. return null;
  95. object? result = null;
  96. var settings = SerializerSettings();
  97. using (var sr = new StreamReader(stream))
  98. {
  99. using (JsonReader reader = new JsonTextReader(sr))
  100. {
  101. var serializer = JsonSerializer.Create(settings);
  102. result = serializer.Deserialize(reader, type);
  103. }
  104. }
  105. return result;
  106. }
  107. [return: MaybeNull]
  108. public static T Deserialize<T>(JToken obj, bool strict = false)
  109. {
  110. var ret = default(T);
  111. try
  112. {
  113. var settings = SerializerSettings();
  114. var serializer = JsonSerializer.Create(settings);
  115. return obj.ToObject<T>();
  116. }
  117. catch (Exception)
  118. {
  119. if (strict)
  120. {
  121. throw;
  122. }
  123. if (typeof(T).IsArray)
  124. {
  125. ret = (T)(object)Array.CreateInstance(typeof(T).GetElementType(), 0);
  126. }
  127. else
  128. {
  129. ret = Activator.CreateInstance<T>();
  130. }
  131. }
  132. return ret;
  133. }
  134. [return: MaybeNull]
  135. public static T Deserialize<T>(string? json, bool strict = false) // where T : new()
  136. {
  137. var ret = default(T);
  138. if (string.IsNullOrWhiteSpace(json))
  139. return ret;
  140. try
  141. {
  142. var settings = SerializerSettings();
  143. //if (typeof(T).IsSubclassOf(typeof(BaseObject)))
  144. //{
  145. // ret = Activator.CreateInstance<T>();
  146. // (ret as BaseObject).SetObserving(false);
  147. // JsonConvert.PopulateObject(json, ret, settings);
  148. // (ret as BaseObject).SetObserving(true);
  149. //}
  150. //else
  151. if (typeof(T).IsArray)
  152. {
  153. ret = JsonConvert.DeserializeObject<T>(json, settings);
  154. //object o = Array.CreateInstance(typeof(T).GetElementType(), 0);
  155. //ret = (T)o;
  156. }
  157. else
  158. {
  159. ret = JsonConvert.DeserializeObject<T>(json, settings);
  160. }
  161. }
  162. catch (Exception e)
  163. {
  164. if (strict)
  165. {
  166. throw;
  167. }
  168. if (typeof(T).IsArray)
  169. {
  170. ret = (T)(object)Array.CreateInstance(typeof(T).GetElementType(), 0);
  171. }
  172. else
  173. {
  174. ret = (T)Activator.CreateInstance(typeof(T), true);
  175. }
  176. }
  177. return ret;
  178. }
  179. public static object? Deserialize(Type T, string json) // where T : new()
  180. {
  181. var ret = T.GetDefault();
  182. if (string.IsNullOrWhiteSpace(json))
  183. return ret;
  184. try
  185. {
  186. var settings = SerializerSettings();
  187. //if (typeof(T).IsSubclassOf(typeof(BaseObject)))
  188. //{
  189. // ret = Activator.CreateInstance<T>();
  190. // (ret as BaseObject).SetObserving(false);
  191. // JsonConvert.PopulateObject(json, ret, settings);
  192. // (ret as BaseObject).SetObserving(true);
  193. //}
  194. //else
  195. if (T.IsArray)
  196. {
  197. object o = Array.CreateInstance(T.GetElementType(), 0);
  198. ret = o;
  199. }
  200. else
  201. {
  202. ret = JsonConvert.DeserializeObject(json, T, settings);
  203. }
  204. }
  205. catch (Exception)
  206. {
  207. ret = Activator.CreateInstance(T, true);
  208. }
  209. return ret;
  210. }
  211. #region Binary Serialization
  212. public static byte[] WriteBinary(this ISerializeBinary obj, BinarySerializationSettings settings)
  213. {
  214. using var stream = new MemoryStream();
  215. obj.SerializeBinary(new CoreBinaryWriter(stream, settings));
  216. return stream.ToArray();
  217. }
  218. public static void WriteBinary(this ISerializeBinary obj, Stream stream, BinarySerializationSettings settings)
  219. {
  220. obj.SerializeBinary(new CoreBinaryWriter(stream, settings));
  221. }
  222. public static T ReadBinary<T>(byte[] data, BinarySerializationSettings settings)
  223. where T : ISerializeBinary, new() => (T)ReadBinary(typeof(T), data, settings);
  224. public static T ReadBinary<T>(Stream stream, BinarySerializationSettings settings)
  225. where T : ISerializeBinary, new() => (T)ReadBinary(typeof(T), stream, settings);
  226. public static object ReadBinary(Type T, byte[] data, BinarySerializationSettings settings)
  227. {
  228. using var stream = new MemoryStream(data);
  229. return ReadBinary(T, stream, settings);
  230. }
  231. public static object ReadBinary(Type T, Stream stream, BinarySerializationSettings settings)
  232. {
  233. var obj = (Activator.CreateInstance(T) as ISerializeBinary)!;
  234. obj.DeserializeBinary(new CoreBinaryReader(stream, settings));
  235. return obj;
  236. }
  237. #endregion
  238. }
  239. public class CoreBinaryReader : BinaryReader
  240. {
  241. public BinarySerializationSettings Settings { get; set; }
  242. public CoreBinaryReader(Stream stream, BinarySerializationSettings settings) : base(stream)
  243. {
  244. Settings = settings;
  245. }
  246. }
  247. public class CoreBinaryWriter : BinaryWriter
  248. {
  249. public BinarySerializationSettings Settings { get; set; }
  250. public CoreBinaryWriter(Stream stream, BinarySerializationSettings settings) : base(stream)
  251. {
  252. Settings = settings;
  253. }
  254. }
  255. /// <summary>
  256. /// A class to maintain the consistency of serialisation formats across versions.
  257. /// The design of this is such that specific versions of serialisation have different parameters set,
  258. /// and the versions are maintained as static properties. Please keep the constructor private.
  259. /// </summary>
  260. /// <remarks>
  261. /// Note that <see cref="Latest"/> should always be updated to point to the latest version.
  262. /// <br/>
  263. /// Note also that all versions should have an entry in the <see cref="ConvertVersionString(string)"/> function.
  264. /// <br/>
  265. /// Also, if you create a new format, it would probably be a good idea to add a database update script to get all
  266. /// <see cref="IPackable"/> and <see cref="ISerializeBinary"/> properties and update the version of the format.
  267. /// (Otherwise, we'd basically be nullifying all data that is currently binary serialised.)
  268. /// </remarks>
  269. public class BinarySerializationSettings
  270. {
  271. /// <summary>
  272. /// Should the Info() call return RPC and Rest Ports? This is
  273. /// To workaround a bug in RPCsockets that crash on large uploads
  274. /// </summary>
  275. /// <remarks>
  276. /// True in all serialization versions >= 1.2
  277. /// </remarks>
  278. public bool RPCClientWorkaround { get; set; }
  279. /// <summary>
  280. /// Should reference types include a flag for nullability? (Adds an extra boolean field for whether the value is null or not).
  281. /// </summary>
  282. /// <remarks>
  283. /// True in all serialisation versions >= 1.1.
  284. /// </remarks>
  285. public bool IncludeNullables { get; set; }
  286. public string Version { get; set; }
  287. public static BinarySerializationSettings Latest => V1_2;
  288. public static BinarySerializationSettings V1_0 = new BinarySerializationSettings("1.0")
  289. {
  290. IncludeNullables = false,
  291. RPCClientWorkaround = false
  292. };
  293. public static BinarySerializationSettings V1_1 = new BinarySerializationSettings("1.1")
  294. {
  295. IncludeNullables = true,
  296. RPCClientWorkaround = false
  297. };
  298. public static BinarySerializationSettings V1_2 = new BinarySerializationSettings("1.2")
  299. {
  300. IncludeNullables = true,
  301. RPCClientWorkaround = true
  302. };
  303. public static BinarySerializationSettings ConvertVersionString(string version) => version switch
  304. {
  305. "1.0" => V1_0,
  306. "1.1" => V1_1,
  307. "1.2" => V1_2,
  308. _ => V1_0
  309. };
  310. private BinarySerializationSettings(string version)
  311. {
  312. Version = version;
  313. }
  314. }
  315. public static class SerializationUtils
  316. {
  317. public static void Write(this BinaryWriter writer, Guid guid)
  318. {
  319. writer.Write(guid.ToByteArray());
  320. }
  321. public static Guid ReadGuid(this BinaryReader reader)
  322. {
  323. return new Guid(reader.ReadBytes(16));
  324. }
  325. public static void Write(this BinaryWriter writer, DateTime dateTime)
  326. {
  327. writer.Write(dateTime.Ticks);
  328. }
  329. public static DateTime ReadDateTime(this BinaryReader reader)
  330. {
  331. return new DateTime(reader.ReadInt64());
  332. }
  333. private static bool MatchType<T1>(Type t) => typeof(T1) == t;
  334. private static bool MatchType<T1,T2>(Type t) => (typeof(T1) == t) || (typeof(T2) == t);
  335. /// <summary>
  336. /// Binary serialize a bunch of different types of values. <see cref="WriteBinaryValue(CoreBinaryWriter, Type, object?)"/> and
  337. /// <see cref="ReadBinaryValue(CoreBinaryReader, Type)"/> are inverses of each other.
  338. /// </summary>
  339. /// <remarks>
  340. /// Handles <see cref="byte"/>[], <see cref="Array"/>s of serialisable values, <see cref="Enum"/>, <see cref="bool"/>, <see cref="string"/>,
  341. /// <see cref="Guid"/>, <see cref="byte"/>, <see cref="Int16"/>, <see cref="Int32"/>, <see cref="Int64"/>, <see cref="float"/>, <see cref="double"/>,
  342. /// <see cref="DateTime"/>, <see cref="TimeSpan"/>, <see cref="LoggablePropertyAttribute"/>, <see cref="IPackable"/>, <see cref="Nullable{T}"/>
  343. /// and <see cref="ISerializeBinary"/>.
  344. /// </remarks>
  345. /// <param name="writer"></param>
  346. /// <param name="type"></param>
  347. /// <param name="value"></param>
  348. /// <exception cref="Exception">If an object of <paramref name="type"/> is unable to be serialized.</exception>
  349. public static void WriteBinaryValue(this CoreBinaryWriter writer, Type type, object? value)
  350. {
  351. value ??= CoreUtils.GetDefault(type);
  352. if (value == null)
  353. {
  354. if (MatchType<string>(type))
  355. writer.Write("");
  356. else if (writer.Settings.IncludeNullables && typeof(IPackable).IsAssignableFrom(type))
  357. writer.Write(false);
  358. else if (writer.Settings.IncludeNullables && typeof(ISerializeBinary).IsAssignableFrom(type))
  359. writer.Write(false);
  360. else if (Nullable.GetUnderlyingType(type) is Type t)
  361. writer.Write(false);
  362. else if (MatchType<LoggablePropertyAttribute, object>(type))
  363. writer.Write("");
  364. else
  365. writer.Write(0);
  366. }
  367. else if (MatchType<byte[], object>(type) && value is byte[] bArray)
  368. {
  369. writer.Write(bArray.Length);
  370. writer.Write(bArray);
  371. }
  372. else if (type.IsArray && value is Array array)
  373. {
  374. var elementType = type.GetElementType();
  375. writer.Write(array.Length);
  376. foreach (var val1 in array)
  377. {
  378. WriteBinaryValue(writer, elementType, val1);
  379. }
  380. }
  381. else if (type.IsEnum && value is Enum e)
  382. {
  383. var underlyingType = type.GetEnumUnderlyingType();
  384. WriteBinaryValue(writer, underlyingType, Convert.ChangeType(e, underlyingType));
  385. }
  386. else if (MatchType<bool, object>(type) && value is bool b)
  387. {
  388. writer.Write(b);
  389. }
  390. else if (MatchType<string, object>(type) && value is string str)
  391. writer.Write(str);
  392. else if (MatchType<Guid, object>(type) && value is Guid guid)
  393. writer.Write(guid);
  394. else if (MatchType<byte, object>(type) && value is byte i8)
  395. writer.Write(i8);
  396. else if (MatchType<Int16, object>(type) && value is Int16 i16)
  397. writer.Write(i16);
  398. else if (MatchType<Int32, object>(type) && value is Int32 i32)
  399. writer.Write(i32);
  400. else if (MatchType<Int64, object>(type) && value is Int64 i64)
  401. writer.Write(i64);
  402. else if (MatchType<float, object>(type) && value is float f32)
  403. writer.Write(f32);
  404. else if (MatchType<double, object>(type) && value is double f64)
  405. writer.Write(f64);
  406. else if (MatchType<DateTime, object>(type) && value is DateTime date)
  407. writer.Write(date.Ticks);
  408. else if (MatchType<TimeSpan, object>(type) && value is TimeSpan time)
  409. writer.Write(time.Ticks);
  410. else if (MatchType<LoggablePropertyAttribute, object>(type) && value is LoggablePropertyAttribute lpa)
  411. writer.Write(lpa.Format ?? string.Empty);
  412. else if (typeof(IPackable).IsAssignableFrom(type) && value is IPackable pack)
  413. {
  414. if (writer.Settings.IncludeNullables)
  415. writer.Write(true);
  416. pack.Pack(writer);
  417. }
  418. else if (typeof(ISerializeBinary).IsAssignableFrom(type) && value is ISerializeBinary binary)
  419. {
  420. if (writer.Settings.IncludeNullables)
  421. writer.Write(true);
  422. binary.SerializeBinary(writer);
  423. }
  424. else if (Nullable.GetUnderlyingType(type) is Type t)
  425. {
  426. writer.Write(true);
  427. writer.WriteBinaryValue(t, value);
  428. }
  429. else if (value is UserProperty userprop)
  430. WriteBinaryValue(writer, userprop.Type, userprop.Value);
  431. else
  432. throw new SerialisationException($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
  433. }
  434. public static void WriteBinaryValue<T>(this CoreBinaryWriter writer, T value)
  435. => WriteBinaryValue(writer, typeof(T), value);
  436. /// <summary>
  437. /// Binary deserialize a bunch of different types of values. <see cref="WriteBinaryValue(CoreBinaryWriter, Type, object?)"/> and
  438. /// <see cref="ReadBinaryValue(CoreBinaryReader, Type)"/> are inverses of each other.
  439. /// </summary>
  440. /// <remarks>
  441. /// Handles <see cref="byte"/>[], <see cref="Array"/>s of serialisable values, <see cref="Enum"/>, <see cref="bool"/>, <see cref="string"/>,
  442. /// <see cref="Guid"/>, <see cref="byte"/>, <see cref="Int16"/>, <see cref="Int32"/>, <see cref="Int64"/>, <see cref="float"/>, <see cref="double"/>,
  443. /// <see cref="DateTime"/>, <see cref="TimeSpan"/>, <see cref="LoggablePropertyAttribute"/>, <see cref="IPackable"/>, <see cref="Nullable{T}"/>
  444. /// and <see cref="ISerializeBinary"/>.
  445. /// </remarks>
  446. /// <param name="reader"></param>
  447. /// <param name="type"></param>
  448. /// <exception cref="Exception">If an object of <paramref name="type"/> is unable to be deserialized.</exception>
  449. public static object? ReadBinaryValue(this CoreBinaryReader reader, Type type)
  450. {
  451. if (type == typeof(byte[]))
  452. {
  453. var length = reader.ReadInt32();
  454. return reader.ReadBytes(length);
  455. }
  456. else if (type.IsArray)
  457. {
  458. var length = reader.ReadInt32();
  459. var elementType = type.GetElementType();
  460. var array = Array.CreateInstance(elementType, length);
  461. for (int i = 0; i < array.Length; ++i)
  462. {
  463. array.SetValue(ReadBinaryValue(reader, elementType), i);
  464. }
  465. return array;
  466. }
  467. else if (type.IsEnum)
  468. {
  469. var val = ReadBinaryValue(reader, type.GetEnumUnderlyingType());
  470. return Enum.ToObject(type, val);
  471. }
  472. else if (type == typeof(bool))
  473. {
  474. return reader.ReadBoolean();
  475. }
  476. else if (type == typeof(string))
  477. {
  478. return reader.ReadString();
  479. }
  480. else if (type == typeof(Guid))
  481. {
  482. return reader.ReadGuid();
  483. }
  484. else if (type == typeof(byte))
  485. {
  486. return reader.ReadByte();
  487. }
  488. else if (type == typeof(Int16))
  489. {
  490. return reader.ReadInt16();
  491. }
  492. else if (type == typeof(Int32))
  493. {
  494. return reader.ReadInt32();
  495. }
  496. else if (type == typeof(Int64))
  497. {
  498. return reader.ReadInt64();
  499. }
  500. else if (type == typeof(float))
  501. {
  502. return reader.ReadSingle();
  503. }
  504. else if (type == typeof(double))
  505. {
  506. return reader.ReadDouble();
  507. }
  508. else if (type == typeof(DateTime))
  509. {
  510. return new DateTime(reader.ReadInt64());
  511. }
  512. else if (type == typeof(TimeSpan))
  513. {
  514. return new TimeSpan(reader.ReadInt64());
  515. }
  516. else if (type == typeof(LoggablePropertyAttribute))
  517. {
  518. String format = reader.ReadString();
  519. return String.IsNullOrWhiteSpace(format)
  520. ? null
  521. : new LoggablePropertyAttribute() { Format = format };
  522. }
  523. else if (typeof(IPackable).IsAssignableFrom(type))
  524. {
  525. if (!reader.Settings.IncludeNullables || reader.ReadBoolean()) // Note the short-circuit operator preventing reading a boolean.
  526. {
  527. var packable = (Activator.CreateInstance(type) as IPackable)!;
  528. packable.Unpack(reader);
  529. return packable;
  530. }
  531. else
  532. {
  533. return null;
  534. }
  535. }
  536. else if (typeof(ISerializeBinary).IsAssignableFrom(type))
  537. {
  538. if (!reader.Settings.IncludeNullables || reader.ReadBoolean()) // Note the short-circuit operator preventing reading a boolean.
  539. {
  540. var obj = (Activator.CreateInstance(type, true) as ISerializeBinary)!;
  541. obj.DeserializeBinary(reader);
  542. return obj;
  543. }
  544. else
  545. {
  546. return null;
  547. }
  548. }
  549. else if (Nullable.GetUnderlyingType(type) is Type t)
  550. {
  551. var isNull = reader.ReadBoolean();
  552. if (isNull)
  553. {
  554. return null;
  555. }
  556. else
  557. {
  558. return reader.ReadBinaryValue(t);
  559. }
  560. }
  561. else
  562. {
  563. throw new SerialisationException($"Invalid type; Target DataType is {type}");
  564. }
  565. }
  566. public static T ReadBinaryValue<T>(this CoreBinaryReader reader)
  567. {
  568. var result = ReadBinaryValue(reader, typeof(T));
  569. return (result != null ? (T)result : default)!;
  570. }
  571. private static bool IsSerializable(Type type, StandardProperty? prop)
  572. {
  573. if (prop == null)
  574. return true;
  575. if (prop.Property.GetCustomAttribute<DoNotSerialize>() != null)
  576. return false;
  577. return IsSerializable(type, prop.Parent as StandardProperty);
  578. }
  579. public static IEnumerable<IProperty> SerializableProperties(Type type) =>
  580. DatabaseSchema.Properties(type)
  581. .Where(x => !(x is StandardProperty st) || IsSerializable(type,st));
  582. private static void GetOriginalValues(BaseObject obj, string? parent, List<Tuple<Type, string, object?>> values)
  583. {
  584. parent = parent != null ? $"{parent}." : "";
  585. foreach (var (key, value) in obj.OriginalValues)
  586. {
  587. // EnclosedEntities and EntityLinks will be updated through the recursive code below,
  588. // so we should not need to serialise the entire object again..
  589. if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop
  590. && !prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity))
  591. && !prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink))
  592. )
  593. {
  594. values.Add(new Tuple<Type, string, object?>(prop.PropertyType, parent + key, value));
  595. }
  596. }
  597. var props = obj.GetType().GetProperties().Where(x =>
  598. x.GetCustomAttribute<DoNotSerialize>() == null
  599. && x.GetCustomAttribute<DoNotPersist>() == null
  600. && x.GetCustomAttribute<AggregateAttribute>() == null
  601. && x.GetCustomAttribute<FormulaAttribute>() == null
  602. && x.GetCustomAttribute<ConditionAttribute>() == null
  603. && x.GetCustomAttribute<ComplexFormulaAttribute>() == null
  604. && x.GetCustomAttribute<ChildEntityAttribute>() == null
  605. && x.CanWrite);
  606. foreach (var prop in props)
  607. {
  608. if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
  609. {
  610. if (prop.GetValue(obj) is BaseObject child)
  611. GetOriginalValues(child, parent + prop.Name, values);
  612. }
  613. else if (prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  614. {
  615. if (prop.GetValue(obj) is BaseObject child && child.HasOriginalValue("ID"))
  616. {
  617. values.Add(new Tuple<Type, string, object?>(typeof(Guid), parent + prop.Name + ".ID", child.OriginalValues["ID"]));
  618. }
  619. }
  620. }
  621. }
  622. private static void WriteOriginalValues<TObject>(this CoreBinaryWriter writer, TObject obj)
  623. where TObject : BaseObject
  624. {
  625. var originalValues = new List<Tuple<Type, string, object?>>();
  626. GetOriginalValues(obj, null, originalValues);
  627. writer.Write(originalValues.Count);
  628. foreach (var (type, key, value) in originalValues)
  629. {
  630. writer.Write(key);
  631. try
  632. {
  633. writer.WriteBinaryValue(type, value);
  634. }
  635. catch (Exception e)
  636. {
  637. }
  638. }
  639. }
  640. private static void ReadOriginalValues<TObject>(this CoreBinaryReader reader, TObject obj)
  641. where TObject : BaseObject
  642. {
  643. var nOriginalValues = reader.ReadInt32();
  644. for (int i = 0; i < nOriginalValues; ++i)
  645. {
  646. var key = reader.ReadString();
  647. if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop)
  648. {
  649. var value = reader.ReadBinaryValue(prop.PropertyType);
  650. if (prop.Parent is null)
  651. {
  652. obj.OriginalValues[prop.Name] = value;
  653. }
  654. else
  655. {
  656. if (prop.Parent.Getter()(obj) is BaseObject parent)
  657. {
  658. parent.OriginalValues[prop.Name.Split('.').Last()] = value;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. public static void WriteObject<TObject>(this CoreBinaryWriter writer, TObject entity, Type type)
  665. where TObject : BaseObject
  666. {
  667. if (!typeof(TObject).IsAssignableFrom(type))
  668. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  669. var properties = SerializableProperties(type).ToList();
  670. writer.Write(properties.Count);
  671. foreach (var property in properties)
  672. {
  673. writer.Write(property.Name);
  674. writer.WriteBinaryValue(property.PropertyType, property.Getter()(entity));
  675. }
  676. writer.WriteOriginalValues(entity);
  677. }
  678. /// <summary>
  679. /// An implementation of binary serialising a <typeparamref name="TObject"/>; this is the inverse of <see cref="ReadObject{TObject}(CoreBinaryReader)"/>.
  680. /// </summary>
  681. /// <remarks>
  682. /// Also serialises the names of properties along with the values.
  683. /// </remarks>
  684. /// <typeparam name="TObject"></typeparam>
  685. /// <param name="writer"></param>
  686. /// <param name="entity"></param>
  687. public static void WriteObject<TObject>(this CoreBinaryWriter writer, TObject entity)
  688. where TObject : BaseObject, new() => WriteObject(writer, entity, typeof(TObject));
  689. public static TObject ReadObject<TObject>(this CoreBinaryReader reader, Type type)
  690. where TObject : BaseObject
  691. {
  692. if (!typeof(TObject).IsAssignableFrom(type))
  693. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  694. var obj = (Activator.CreateInstance(type) as TObject)!;
  695. obj.SetObserving(false);
  696. var nProps = reader.ReadInt32();
  697. for (int i = 0; i < nProps; ++i)
  698. {
  699. var propName = reader.ReadString();
  700. var property = DatabaseSchema.Property(type, propName)
  701. ?? throw new SerialisationException($"Property {propName} does not exist on {type.EntityName()}");
  702. property.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
  703. }
  704. reader.ReadOriginalValues(obj);
  705. obj.SetObserving(true);
  706. return obj;
  707. }
  708. /// <summary>
  709. /// The inverse of <see cref="WriteObject{TObject}(CoreBinaryWriter, TObject)"/>.
  710. /// </summary>
  711. /// <typeparam name="TObject"></typeparam>
  712. /// <param name="reader"></param>
  713. /// <returns></returns>
  714. public static TObject ReadObject<TObject>(this CoreBinaryReader reader)
  715. where TObject : BaseObject, new() => reader.ReadObject<TObject>(typeof(TObject));
  716. /// <summary>
  717. /// An implementation of binary serialising multiple <typeparamref name="TObject"/>s;
  718. /// this is the inverse of <see cref="ReadObjects{TObject}(CoreBinaryReader)"/>.
  719. /// </summary>
  720. /// <remarks>
  721. /// Also serialises the names of properties along with the values.
  722. /// </remarks>
  723. /// <typeparam name="TObject"></typeparam>
  724. /// <param name="writer"></param>
  725. /// <param name="objects"></param>
  726. public static void WriteObjects<TObject>(this CoreBinaryWriter writer, ICollection<TObject>? objects)
  727. where TObject : BaseObject, new() => WriteObjects(writer, typeof(TObject), objects);
  728. public static void WriteObjects<TObject>(this CoreBinaryWriter writer, Type type, ICollection<TObject>? objects)
  729. where TObject : BaseObject
  730. {
  731. if (!typeof(TObject).IsAssignableFrom(type))
  732. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  733. var nObjs = objects?.Count ?? 0;
  734. writer.Write(nObjs);
  735. if (nObjs == 0)
  736. {
  737. return;
  738. }
  739. var properties = SerializableProperties(type).ToList();
  740. writer.Write(properties.Count);
  741. foreach (var property in properties)
  742. {
  743. writer.Write(property.Name);
  744. }
  745. if(objects != null)
  746. {
  747. foreach (var obj in objects)
  748. {
  749. foreach (var property in properties)
  750. {
  751. writer.WriteBinaryValue(property.PropertyType, property.Getter()(obj));
  752. }
  753. writer.WriteOriginalValues(obj);
  754. }
  755. }
  756. }
  757. /// <summary>
  758. /// The inverse of <see cref="WriteObjects{TObject}(CoreBinaryWriter, ICollection{TObject})"/>.
  759. /// </summary>
  760. /// <typeparam name="TObject"></typeparam>
  761. /// <param name="reader"></param>
  762. /// <returns></returns>
  763. public static List<TObject> ReadObjects<TObject>(this CoreBinaryReader reader)
  764. where TObject : BaseObject, new()
  765. {
  766. return ReadObjects<TObject>(reader, typeof(TObject));
  767. }
  768. public static List<TObject> ReadObjects<TObject>(this CoreBinaryReader reader, Type type) where TObject : BaseObject
  769. {
  770. if (!typeof(TObject).IsAssignableFrom(type))
  771. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  772. var objs = new List<TObject>();
  773. var properties = new List<IProperty>();
  774. var nObjs = reader.ReadInt32();
  775. if(nObjs == 0)
  776. {
  777. return objs;
  778. }
  779. var nProps = reader.ReadInt32();
  780. for (int i = 0; i < nProps; ++i)
  781. {
  782. var propertyName = reader.ReadString();
  783. var property = DatabaseSchema.Property(type, propertyName)
  784. ?? throw new SerialisationException($"Property {propertyName} does not exist on {type.EntityName()}");
  785. properties.Add(property);
  786. }
  787. for (int i = 0; i < nObjs; ++i)
  788. {
  789. var obj = (Activator.CreateInstance(type) as TObject)!;
  790. obj.SetObserving(false);
  791. foreach (var property in properties)
  792. {
  793. property.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
  794. }
  795. reader.ReadOriginalValues(obj);
  796. obj.SetObserving(true);
  797. objs.Add(obj);
  798. }
  799. return objs;
  800. }
  801. }
  802. }