Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializationHelper.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using UnityEditor.ShaderGraph.Serialization;
  6. using UnityEngine;
  7. namespace UnityEditor.Graphing
  8. {
  9. static class SerializationHelper
  10. {
  11. [Serializable]
  12. public struct TypeSerializationInfo
  13. {
  14. [SerializeField]
  15. public string fullName;
  16. public bool IsValid()
  17. {
  18. return !string.IsNullOrEmpty(fullName);
  19. }
  20. }
  21. [Serializable]
  22. public struct JSONSerializedElement
  23. {
  24. [SerializeField]
  25. public TypeSerializationInfo typeInfo;
  26. [SerializeField]
  27. public string JSONnodeData;
  28. }
  29. public static JSONSerializedElement nullElement
  30. {
  31. get
  32. {
  33. return new JSONSerializedElement();
  34. }
  35. }
  36. public static TypeSerializationInfo GetTypeSerializableAsString(Type type)
  37. {
  38. return new TypeSerializationInfo
  39. {
  40. fullName = type.FullName
  41. };
  42. }
  43. static Type GetTypeFromSerializedString(TypeSerializationInfo typeInfo)
  44. {
  45. if (!typeInfo.IsValid())
  46. return null;
  47. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  48. foreach (var assembly in assemblies)
  49. {
  50. var type = assembly.GetType(typeInfo.fullName);
  51. if (type != null)
  52. return type;
  53. }
  54. return null;
  55. }
  56. public static JSONSerializedElement Serialize<T>(T item)
  57. {
  58. if (item is JsonObject jsonObject)
  59. return new JSONSerializedElement() { JSONnodeData = jsonObject.Serialize() };
  60. if (item == null)
  61. throw new ArgumentNullException("item", "Can not serialize null element");
  62. //check if unknownnode type - if so, return saved metadata
  63. //unknown node type will need onbeforeserialize to set guid and edges and all the things
  64. var typeInfo = GetTypeSerializableAsString(item.GetType());
  65. var data = JsonUtility.ToJson(item, true);
  66. if (string.IsNullOrEmpty(data))
  67. throw new ArgumentException(string.Format("Can not serialize {0}", item));
  68. return new JSONSerializedElement
  69. {
  70. typeInfo = typeInfo,
  71. JSONnodeData = data
  72. };
  73. }
  74. static TypeSerializationInfo DoTypeRemap(TypeSerializationInfo info, Dictionary<TypeSerializationInfo, TypeSerializationInfo> remapper)
  75. {
  76. TypeSerializationInfo foundInfo;
  77. if (remapper.TryGetValue(info, out foundInfo))
  78. return foundInfo;
  79. return info;
  80. }
  81. public static T Deserialize<T>(JSONSerializedElement item, Dictionary<TypeSerializationInfo, TypeSerializationInfo> remapper, params object[] constructorArgs) where T : class
  82. {
  83. T instance;
  84. if (typeof(T) == typeof(JsonObject) || typeof(T).IsSubclassOf(typeof(JsonObject)))
  85. {
  86. try
  87. {
  88. var culture = CultureInfo.CurrentCulture;
  89. var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
  90. instance = Activator.CreateInstance(typeof(T), flags, null, constructorArgs, culture) as T;
  91. }
  92. catch (Exception e)
  93. {
  94. throw new Exception(string.Format("Could not construct instance of: {0}", typeof(T)), e);
  95. }
  96. MultiJson.Deserialize(instance as JsonObject, item.JSONnodeData);
  97. return instance;
  98. }
  99. if (!item.typeInfo.IsValid() || string.IsNullOrEmpty(item.JSONnodeData))
  100. throw new ArgumentException(string.Format("Can not deserialize {0}, it is invalid", item));
  101. TypeSerializationInfo info = item.typeInfo;
  102. info.fullName = info.fullName.Replace("UnityEngine.MaterialGraph", "UnityEditor.ShaderGraph");
  103. info.fullName = info.fullName.Replace("UnityEngine.Graphing", "UnityEditor.Graphing");
  104. if (remapper != null)
  105. info = DoTypeRemap(info, remapper);
  106. var type = GetTypeFromSerializedString(info);
  107. //if type is null but T is an abstract material node, instead we create an unknowntype node
  108. if (type == null)
  109. {
  110. throw new ArgumentException(string.Format("Can not deserialize ({0}), type is invalid", info.fullName));
  111. }
  112. try
  113. {
  114. var culture = CultureInfo.CurrentCulture;
  115. var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
  116. instance = Activator.CreateInstance(type, flags, null, constructorArgs, culture) as T;
  117. }
  118. catch (Exception e)
  119. {
  120. throw new Exception(string.Format("Could not construct instance of: {0}", type), e);
  121. }
  122. if (instance != null)
  123. {
  124. JsonUtility.FromJsonOverwrite(item.JSONnodeData, instance);
  125. return instance;
  126. }
  127. Debug.Log("UhOh");
  128. return null;
  129. }
  130. public static List<JSONSerializedElement> Serialize<T>(IEnumerable<T> list)
  131. {
  132. var result = new List<JSONSerializedElement>();
  133. if (list == null)
  134. return result;
  135. foreach (var element in list)
  136. {
  137. try
  138. {
  139. result.Add(Serialize(element));
  140. }
  141. catch (Exception e)
  142. {
  143. Debug.LogException(e);
  144. }
  145. }
  146. return result;
  147. }
  148. public static List<T> Deserialize<T>(IEnumerable<JSONSerializedElement> list, Dictionary<TypeSerializationInfo, TypeSerializationInfo> remapper, params object[] constructorArgs) where T : class
  149. {
  150. var result = new List<T>();
  151. if (list == null)
  152. return result;
  153. foreach (var element in list)
  154. {
  155. try
  156. {
  157. result.Add(Deserialize<T>(element, remapper));
  158. }
  159. catch (Exception e)
  160. {
  161. Debug.LogException(e);
  162. Debug.LogError(element.JSONnodeData);
  163. }
  164. }
  165. return result;
  166. }
  167. }
  168. }