JsonDataTrimmer.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using AssetBank.Settings;
  4. using Newtonsoft.Json.Linq;
  5. using UnityEngine;
  6. namespace AssetBank.Editor.Tools
  7. {
  8. public static class JsonDataTrimmer
  9. {
  10. public static string Process(string rawJsonText, ProjectExporterSettings settings)
  11. {
  12. var allComponents = JArray.Parse(rawJsonText);
  13. // Pass 1: Build the definitive ruleset
  14. var rules = settings.SafeComponents.ToDictionary(r => r.Component, r => r.ExposedFields.ToHashSet());
  15. if (!settings.OverrideHardcodedDefaults)
  16. {
  17. foreach (var hardcodedRule in settings.HardcodedComponents)
  18. {
  19. if (rules.TryGetValue(hardcodedRule.Component, out var userRules))
  20. {
  21. userRules.UnionWith(hardcodedRule.ExposedFields);
  22. }
  23. else
  24. {
  25. rules[hardcodedRule.Component] = hardcodedRule.ExposedFields.ToHashSet();
  26. }
  27. }
  28. }
  29. // Pass 2: Create a fully trimmed copy of every component
  30. var trimmedComponentMap = new Dictionary<string, JObject>();
  31. foreach (var component in allComponents.Children<JObject>())
  32. {
  33. var anchorId = component["anchor_id"]?.ToString();
  34. if (string.IsNullOrEmpty(anchorId)) continue;
  35. var trimmedComponent = component.DeepClone() as JObject;
  36. var dataBlock = trimmedComponent["data"] as JObject;
  37. if (dataBlock == null || !dataBlock.HasValues)
  38. {
  39. trimmedComponentMap[anchorId] = trimmedComponent;
  40. continue;
  41. }
  42. var componentProperty = dataBlock.Properties().FirstOrDefault();
  43. if (componentProperty == null)
  44. {
  45. trimmedComponentMap[anchorId] = trimmedComponent;
  46. continue;
  47. }
  48. var componentName = componentProperty.Name;
  49. var componentJObject = componentProperty.Value as JObject;
  50. if (componentJObject == null)
  51. {
  52. trimmedComponentMap[anchorId] = trimmedComponent;
  53. continue;
  54. }
  55. if (rules.TryGetValue(componentName, out var exposedFields))
  56. {
  57. if (exposedFields.Contains("*"))
  58. {
  59. trimmedComponentMap[anchorId] = trimmedComponent;
  60. continue;
  61. }
  62. var propertiesToKeep = new HashSet<string>(exposedFields);
  63. var currentKeys = componentJObject.Properties().Select(p => p.Name).ToList();
  64. foreach (var key in currentKeys)
  65. {
  66. if (!propertiesToKeep.Contains(key))
  67. {
  68. componentJObject.Remove(key);
  69. }
  70. }
  71. }
  72. else
  73. {
  74. componentJObject.RemoveAll();
  75. }
  76. trimmedComponentMap[anchorId] = trimmedComponent;
  77. }
  78. // Pass 3: Final Assembly
  79. var finalOutput = new JArray();
  80. var embeddedIds = new HashSet<string>();
  81. // First, identify all components that will be embedded into GameObjects
  82. foreach (var originalComponent in allComponents.Children<JObject>())
  83. {
  84. if (originalComponent["data"]?["GameObject"]?["m_Component"] is JArray componentList)
  85. {
  86. foreach (var compRef in componentList.Children<JObject>())
  87. {
  88. if (compRef["component"]?["fileID"]?.ToString() is var fileID && !string.IsNullOrEmpty(fileID))
  89. {
  90. embeddedIds.Add(fileID);
  91. }
  92. }
  93. }
  94. }
  95. // Now, build the final output, preserving order
  96. foreach (var originalComponent in allComponents.Children<JObject>())
  97. {
  98. var anchorId = originalComponent["anchor_id"]?.ToString();
  99. if (string.IsNullOrEmpty(anchorId) || embeddedIds.Contains(anchorId))
  100. {
  101. // If a component has no ID, or it's a child of a GameObject, skip it.
  102. // Children will be added via their parent GameObject.
  103. continue;
  104. }
  105. // Get the trimmed version of the current component
  106. if (!trimmedComponentMap.TryGetValue(anchorId, out var trimmedComponent))
  107. {
  108. // Should not happen, but as a safeguard, add the original.
  109. finalOutput.Add(originalComponent.DeepClone());
  110. continue;
  111. }
  112. // If it's a GameObject, embed its already-trimmed children
  113. if (originalComponent["data"]?["GameObject"] is JObject originalGameObjectData)
  114. {
  115. var trimmedGameObjectData = trimmedComponent["data"]["GameObject"] as JObject;
  116. var componentList = originalGameObjectData["m_Component"] as JArray;
  117. if (trimmedGameObjectData != null && componentList != null)
  118. {
  119. var newComponentList = new JArray();
  120. var gameObjectName = originalGameObjectData["m_Name"]?.ToString() ?? "Unnamed";
  121. foreach (var compRef in componentList.Children<JObject>())
  122. {
  123. var fileID = compRef["component"]?["fileID"]?.ToString();
  124. if (string.IsNullOrEmpty(fileID)) continue;
  125. if (trimmedComponentMap.TryGetValue(fileID, out var linkedComponent))
  126. {
  127. var clonedData = linkedComponent["data"].DeepClone() as JObject;
  128. (clonedData?.Properties().FirstOrDefault()?.Value as JObject)?.Remove("m_GameObject");
  129. newComponentList.Add(clonedData);
  130. }
  131. else
  132. {
  133. Debug.LogWarning($"JsonDataTrimmer: Could not find component with anchor_id '{fileID}' referenced by GameObject '{gameObjectName}'.");
  134. }
  135. }
  136. trimmedGameObjectData["m_Component"] = newComponentList;
  137. }
  138. }
  139. finalOutput.Add(trimmedComponent);
  140. }
  141. return finalOutput.ToString(Newtonsoft.Json.Formatting.None);
  142. }
  143. }
  144. }