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.

MinimalGraphData.cs 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. using UnityEditor.Graphing;
  7. using UnityEditor.ShaderGraph.Legacy;
  8. using UnityEditor.ShaderGraph.Serialization;
  9. using UnityEngine;
  10. namespace UnityEditor.ShaderGraph
  11. {
  12. /// <summary>
  13. /// Minimal version of <see cref="GraphData"/> used for gathering dependencies. This allows us to not deserialize
  14. /// all the nodes, ports, edges, groups etc., which is important as we cannot share data between
  15. /// <see cref="ShaderSubGraphImporter.GatherDependenciesFromSourceFile"/> and
  16. /// <see cref="ShaderSubGraphImporter.OnImportAsset"/>. The latter must always import fully, but for the former we
  17. /// want to avoid the extra GC pressure.
  18. /// </summary>
  19. [Serializable]
  20. class MinimalGraphData
  21. {
  22. static Dictionary<string, Type> s_MinimalTypeMap = CreateMinimalTypeMap();
  23. static Dictionary<string, Type> CreateMinimalTypeMap()
  24. {
  25. var types = new Dictionary<string, Type>();
  26. foreach (var type in TypeCache.GetTypesWithAttribute<HasDependenciesAttribute>())
  27. {
  28. var dependencyAttribute = (HasDependenciesAttribute)type.GetCustomAttributes(typeof(HasDependenciesAttribute), false)[0];
  29. if (!typeof(IHasDependencies).IsAssignableFrom(dependencyAttribute.minimalType))
  30. {
  31. Debug.LogError($"{type} must implement {typeof(IHasDependencies)} to be used in {typeof(HasDependenciesAttribute)}");
  32. continue;
  33. }
  34. types.Add(type.FullName, dependencyAttribute.minimalType);
  35. var formerNameAttributes = (FormerNameAttribute[])type.GetCustomAttributes(typeof(FormerNameAttribute), false);
  36. foreach (var formerNameAttribute in formerNameAttributes)
  37. {
  38. types.Add(formerNameAttribute.fullName, dependencyAttribute.minimalType);
  39. }
  40. }
  41. return types;
  42. }
  43. [SerializeField]
  44. List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();
  45. // gather all asset dependencies declared by nodes in the given (shadergraph or shadersubgraph) asset
  46. // by reading the source file from disk, and doing a minimal parse
  47. // returns true if it successfully gathered the dependencies, false if there was an error
  48. public static bool GatherMinimalDependenciesFromFile(string assetPath, AssetCollection assetCollection)
  49. {
  50. var textGraph = FileUtilities.SafeReadAllText(assetPath);
  51. // if we can't read the file, no dependencies can be gathered
  52. if (string.IsNullOrEmpty(textGraph))
  53. return false;
  54. var entries = MultiJsonInternal.Parse(textGraph);
  55. if (string.IsNullOrWhiteSpace(entries[0].type))
  56. {
  57. var minimalGraphData = JsonUtility.FromJson<MinimalGraphData>(textGraph);
  58. entries.Clear();
  59. foreach (var node in minimalGraphData.m_SerializableNodes)
  60. {
  61. entries.Add(new MultiJsonEntry(node.typeInfo.fullName, null, node.JSONnodeData));
  62. AbstractMaterialNode0 amn = new AbstractMaterialNode0();
  63. JsonUtility.FromJsonOverwrite(node.JSONnodeData, amn);
  64. foreach (var slot in amn.m_SerializableSlots)
  65. {
  66. entries.Add(new MultiJsonEntry(slot.typeInfo.fullName, null, slot.JSONnodeData));
  67. }
  68. }
  69. }
  70. foreach (var entry in entries)
  71. {
  72. if (s_MinimalTypeMap.TryGetValue(entry.type, out var minimalType))
  73. {
  74. var instance = (IHasDependencies)Activator.CreateInstance(minimalType);
  75. JsonUtility.FromJsonOverwrite(entry.json, instance);
  76. instance.GetSourceAssetDependencies(assetCollection);
  77. }
  78. }
  79. return true;
  80. }
  81. }
  82. }