No Description
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.

JsonObject.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace UnityEditor.ShaderGraph.Serialization
  7. {
  8. [Serializable]
  9. public class JsonObject : ISerializationCallbackReceiver
  10. {
  11. public virtual int latestVersion { get; } = 0;
  12. [SerializeField]
  13. protected int m_SGVersion = 0;
  14. public virtual int sgVersion { get => m_SGVersion; protected set => m_SGVersion = value; }
  15. internal protected delegate void VersionChange(int newVersion);
  16. internal protected VersionChange onBeforeVersionChange;
  17. internal protected Action onAfterVersionChange;
  18. internal void ChangeVersion(int newVersion)
  19. {
  20. if (newVersion == sgVersion)
  21. {
  22. return;
  23. }
  24. if (newVersion < 0)
  25. {
  26. Debug.LogError("Cant downgrade past version 0");
  27. return;
  28. }
  29. if (newVersion > latestVersion)
  30. {
  31. Debug.LogError("Cant upgrade to a version >= the current latest version");
  32. return;
  33. }
  34. onBeforeVersionChange?.Invoke(newVersion);
  35. sgVersion = newVersion;
  36. onAfterVersionChange?.Invoke();
  37. }
  38. public JsonObject()
  39. {
  40. sgVersion = latestVersion;
  41. }
  42. public static readonly string emptyObjectId = Guid.Empty.ToString("N");
  43. [SerializeField]
  44. string m_Type;
  45. [SerializeField]
  46. string m_ObjectId = Guid.NewGuid().ToString("N");
  47. internal void OverrideObjectId(string namespaceUid, string newObjectId) { m_ObjectId = GenerateNamespaceUUID(namespaceUid, newObjectId).ToString("N"); }
  48. internal void OverrideObjectId(string newObjectId) { m_ObjectId = newObjectId; }
  49. public string objectId => m_ObjectId;
  50. public bool objectIdIsEmpty => m_ObjectId.Equals(emptyObjectId);
  51. void ISerializationCallbackReceiver.OnBeforeSerialize()
  52. {
  53. m_Type = $"{GetType().FullName}";
  54. OnBeforeSerialize();
  55. }
  56. public virtual T CastTo<T>() where T : JsonObject { return (T)this; }
  57. public virtual string Serialize() { return EditorJsonUtility.ToJson(this, true); }
  58. public virtual void Deserailize(string typeInfo, string jsonData) { EditorJsonUtility.FromJsonOverwrite(jsonData, this); }
  59. public virtual void OnBeforeSerialize() { }
  60. public virtual void OnBeforeDeserialize() { }
  61. public virtual void OnAfterDeserialize() { }
  62. public virtual void OnAfterDeserialize(string json) { }
  63. public virtual void OnAfterMultiDeserialize(string json) { }
  64. internal static Guid GenerateNamespaceUUID(string Namespace, string Name)
  65. {
  66. Guid namespaceGuid;
  67. if (!Guid.TryParse(Namespace, out namespaceGuid))
  68. {
  69. // Fallback namespace in case the one provided is invalid.
  70. // If an object ID was used as the namespace, this shouldn't normally be reachable.
  71. namespaceGuid = new Guid("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
  72. }
  73. return GenerateNamespaceUUID(namespaceGuid, Name);
  74. }
  75. internal static Guid GenerateNamespaceUUID(Guid Namespace, string Name)
  76. {
  77. // Generate a deterministic guid using namespace guids: RFC 4122 §4.3 version 5.
  78. void FlipByNetworkOrder(byte[] bytes)
  79. { bytes = new byte[] { bytes[3], bytes[2], bytes[1], bytes[0], bytes[5], bytes[4], bytes[7], bytes[6] }; }
  80. var namespaceBytes = Namespace.ToByteArray();
  81. FlipByNetworkOrder(namespaceBytes);
  82. var nameBytes = Encoding.UTF8.GetBytes(Name);
  83. var hash = SHA1.Create().ComputeHash(namespaceBytes.Concat(nameBytes).ToArray());
  84. byte[] newguid = new byte[16];
  85. Array.Copy(hash, newguid, 16);
  86. newguid[6] = (byte)((newguid[6] & 0x0F) | 0x80);
  87. newguid[8] = (byte)((newguid[8] & 0x3F) | 0x80);
  88. FlipByNetworkOrder(newguid);
  89. return new Guid(newguid);
  90. }
  91. }
  92. }