SceneDataProcessor.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using AssetBank.Editor.SchemaConverter.Data.Input;
  5. using Newtonsoft.Json.Linq;
  6. namespace AssetBank.Editor.SchemaConverter.Processors
  7. {
  8. /// <summary>
  9. /// Handles the logic for extracting scene-level settings data.
  10. /// </summary>
  11. public static class SceneDataProcessor
  12. {
  13. /// <summary>
  14. /// Filters for scene-level nodes and extracts their data.
  15. /// </summary>
  16. /// <param name="allNodes">The complete list of original nodes from the JSON.</param>
  17. /// <param name="consumedAnchorIds">A set of all anchor_ids that have already been processed as part of the hierarchy or as prefabs.</param>
  18. /// <returns>A JObject containing all scene-level data, keyed by the component name.</returns>
  19. public static JObject Process(IReadOnlyList<OriginalNode> allNodes, HashSet<string> consumedAnchorIds)
  20. {
  21. var sceneData = new JObject();
  22. // A node is a scene setting if it was not consumed by the hierarchy/prefab processors.
  23. var sceneNodes = allNodes.Where(node => !consumedAnchorIds.Contains(node.anchor_id));
  24. foreach (var node in sceneNodes)
  25. {
  26. if (node.data is not { HasValues: true }) continue;
  27. // Take the first property from the 'data' object (e.g., "LightmapSettings").
  28. var settingProperty = node.data.Properties().FirstOrDefault();
  29. if (settingProperty != null)
  30. {
  31. try
  32. {
  33. // Add it to our scene_data object.
  34. sceneData.Add(settingProperty);
  35. }
  36. catch (ArgumentException)
  37. {
  38. // Provide a detailed error message upon failure.
  39. var existingKeys = string.Join(", ", sceneData.Properties().Select(p => p.Name));
  40. throw new ArgumentException($"Duplicate key error in SceneDataProcessor. Attempted to add key '{settingProperty.Name}' (from anchor_id: {node.anchor_id}) but a property with that name already exists. Existing keys: [{existingKeys}]");
  41. }
  42. }
  43. }
  44. return sceneData;
  45. }
  46. }
  47. }