MinimalGraphData.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEditor.Graphing;
  6. using UnityEngine;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. /// <summary>
  10. /// Minimal version of <see cref="GraphData"/> used for gathering dependencies. This allows us to not deserialize
  11. /// all the nodes, ports, edges, groups etc., which is important as we cannot share data between
  12. /// <see cref="ShaderSubGraphImporter.GatherDependenciesFromSourceFile"/> and
  13. /// <see cref="ShaderSubGraphImporter.OnImportAsset"/>. The latter must always import fully, but for the former we
  14. /// want to avoid the extra GC pressure.
  15. /// </summary>
  16. [Serializable]
  17. class MinimalGraphData : ISerializationCallbackReceiver
  18. {
  19. static Dictionary<string, Type> s_MinimalTypeMap = CreateMinimalTypeMap();
  20. static Dictionary<string, Type> CreateMinimalTypeMap()
  21. {
  22. var types = new Dictionary<string, Type>();
  23. foreach (var nodeType in TypeCache.GetTypesWithAttribute<HasDependenciesAttribute>())
  24. {
  25. var dependencyAttribute = (HasDependenciesAttribute)nodeType.GetCustomAttributes(typeof(HasDependenciesAttribute), false)[0];
  26. if (!typeof(IHasDependencies).IsAssignableFrom(dependencyAttribute.minimalType))
  27. {
  28. Debug.LogError($"{nodeType} must implement {typeof(IHasDependencies)} to be used in {typeof(HasDependenciesAttribute)}");
  29. continue;
  30. }
  31. types.Add(nodeType.FullName, dependencyAttribute.minimalType);
  32. var formerNameAttributes = (FormerNameAttribute[])nodeType.GetCustomAttributes(typeof(FormerNameAttribute), false);
  33. foreach (var formerNameAttribute in formerNameAttributes)
  34. {
  35. types.Add(formerNameAttribute.fullName, dependencyAttribute.minimalType);
  36. }
  37. }
  38. return types;
  39. }
  40. [SerializeField]
  41. List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();
  42. public List<string> dependencies { get; set; }
  43. public void OnAfterDeserialize()
  44. {
  45. foreach (var element in m_SerializableNodes)
  46. {
  47. if (s_MinimalTypeMap.TryGetValue(element.typeInfo.fullName, out var minimalType))
  48. {
  49. var instance = (IHasDependencies)Activator.CreateInstance(minimalType);
  50. JsonUtility.FromJsonOverwrite(element.JSONnodeData, instance);
  51. instance.GetSourceAssetDependencies(dependencies);
  52. }
  53. }
  54. }
  55. public void OnBeforeSerialize()
  56. {
  57. }
  58. public static string[] GetDependencyPaths(string assetPath)
  59. {
  60. var dependencies = new List<string>();
  61. GetDependencyPaths(assetPath, dependencies);
  62. return dependencies.ToArray();
  63. }
  64. public static void GetDependencyPaths(string assetPath, List<string> dependencies)
  65. {
  66. var textGraph = File.ReadAllText(assetPath, Encoding.UTF8);
  67. var minimalGraphData = new MinimalGraphData { dependencies = dependencies };
  68. JsonUtility.FromJsonOverwrite(textGraph, minimalGraphData);
  69. }
  70. }
  71. }