-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneHierarchyParser.cs
More file actions
253 lines (212 loc) · 8.48 KB
/
SceneHierarchyParser.cs
File metadata and controls
253 lines (212 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using YamlDotNet.RepresentationModel;
namespace UnityProjectAnalyzer;
class SceneHierarchyParser
{
private readonly string _projectPath;
private readonly string _outputPath;
public SceneHierarchyParser(string projectPath, string outputPath)
{
_projectPath = projectPath;
_outputPath = outputPath;
}
public void ParseAllScenes()
{
string assetsPath = Path.Combine(_projectPath, "Assets");
if (!Directory.Exists(assetsPath))
{
Console.WriteLine($"Warning: Assets folder not found at '{assetsPath}'");
return;
}
var sceneFiles = Directory.GetFiles(assetsPath, "*.unity", SearchOption.AllDirectories);
// Parse scenes in parallel for better performance
Parallel.ForEach(sceneFiles, sceneFile =>
{
ParseScene(sceneFile);
});
}
private void ParseScene(string sceneFilePath)
{
var fileName = Path.GetFileName(sceneFilePath);
var outputFileName = $"{fileName}.dump";
var outputFilePath = Path.Combine(_outputPath, outputFileName);
Console.WriteLine($"Parsing scene: {fileName}");
var gameObjects = new Dictionary<string, GameObject>();
var transforms = new Dictionary<string, Transform>();
var rootTransformIds = new List<string>();
using (var reader = new StreamReader(sceneFilePath))
{
var yaml = new YamlStream();
yaml.Load(reader);
foreach (var document in yaml.Documents)
{
if (!(document.RootNode is YamlMappingNode root))
continue;
var anchor = document.RootNode.Anchor.Value;
var fileId = string.IsNullOrEmpty(anchor) ? "" : anchor;
// Identify by structure, not by type ID
if (root.Children.ContainsKey(new YamlScalarNode("GameObject")))
{
var gameObject = ParseGameObject(fileId, root);
if (gameObject != null)
gameObjects[fileId] = gameObject;
}
else if (root.Children.ContainsKey(new YamlScalarNode("Transform")))
{
var transform = ParseTransform(fileId, root);
if (transform != null)
transforms[fileId] = transform;
}
else if (root.Children.ContainsKey(new YamlScalarNode("SceneRoots")))
{
rootTransformIds = ParseSceneRoots(root);
}
}
}
// Build the hierarchy
var hierarchy = new List<string>();
foreach (var rootTransformId in rootTransformIds)
{
BuildHierarchy(rootTransformId, transforms, gameObjects, 0, hierarchy);
}
// Write output
File.WriteAllLines(outputFilePath, hierarchy);
Console.WriteLine($" Written to: {outputFileName}");
}
private GameObject? ParseGameObject(string fileId, YamlMappingNode root)
{
var gameObjectKey = new YamlScalarNode("GameObject");
if (!root.Children.ContainsKey(gameObjectKey))
return null;
var goNode = root.Children[gameObjectKey] as YamlMappingNode;
if (goNode == null)
return null;
var nameKey = new YamlScalarNode("m_Name");
if (!goNode.Children.ContainsKey(nameKey))
return null;
var name = ((YamlScalarNode)goNode.Children[nameKey]).Value ?? "";
return new GameObject { FileId = fileId, Name = name };
}
private Transform? ParseTransform(string fileId, YamlMappingNode root)
{
var transformKey = new YamlScalarNode("Transform");
if (!root.Children.ContainsKey(transformKey))
return null;
var transformNode = root.Children[transformKey] as YamlMappingNode;
if (transformNode == null)
return null;
var transform = new Transform { FileId = fileId };
// Get GameObject reference
var gameObjectKey = new YamlScalarNode("m_GameObject");
if (transformNode.Children.ContainsKey(gameObjectKey))
{
var goNode = transformNode.Children[gameObjectKey] as YamlMappingNode;
if (goNode != null)
{
var fileIdKey = new YamlScalarNode("fileID");
if (goNode.Children.ContainsKey(fileIdKey))
{
transform.GameObjectFileId = ((YamlScalarNode)goNode.Children[fileIdKey]).Value ?? "";
}
}
}
// Get parent reference
var fatherKey = new YamlScalarNode("m_Father");
if (transformNode.Children.ContainsKey(fatherKey))
{
var fatherNode = transformNode.Children[fatherKey] as YamlMappingNode;
if (fatherNode != null)
{
var fileIdKey = new YamlScalarNode("fileID");
if (fatherNode.Children.ContainsKey(fileIdKey))
{
var fatherId = ((YamlScalarNode)fatherNode.Children[fileIdKey]).Value ?? "";
if (fatherId != "0")
{
transform.ParentFileId = fatherId;
}
}
}
}
// Get children
var childrenKey = new YamlScalarNode("m_Children");
if (transformNode.Children.ContainsKey(childrenKey))
{
var childrenNode = transformNode.Children[childrenKey] as YamlSequenceNode;
if (childrenNode != null)
{
foreach (var childItem in childrenNode.Children)
{
if (childItem is YamlMappingNode childMapping)
{
var fileIdKey = new YamlScalarNode("fileID");
if (childMapping.Children.ContainsKey(fileIdKey))
{
var childId = ((YamlScalarNode)childMapping.Children[fileIdKey]).Value ?? "";
transform.ChildrenFileIds.Add(childId);
}
}
}
}
}
return transform;
}
private List<string> ParseSceneRoots(YamlMappingNode root)
{
var rootIds = new List<string>();
var sceneRootsKey = new YamlScalarNode("SceneRoots");
if (!root.Children.ContainsKey(sceneRootsKey))
return rootIds;
var sceneRootsNode = root.Children[sceneRootsKey] as YamlMappingNode;
if (sceneRootsNode == null)
return rootIds;
var rootsKey = new YamlScalarNode("m_Roots");
if (!sceneRootsNode.Children.ContainsKey(rootsKey))
return rootIds;
var rootsSequence = sceneRootsNode.Children[rootsKey] as YamlSequenceNode;
if (rootsSequence == null)
return rootIds;
foreach (var rootItem in rootsSequence.Children)
{
if (rootItem is YamlMappingNode rootMapping)
{
var fileIdKey = new YamlScalarNode("fileID");
if (rootMapping.Children.ContainsKey(fileIdKey))
{
var rootFileId = ((YamlScalarNode)rootMapping.Children[fileIdKey]).Value ?? "";
rootIds.Add(rootFileId);
}
}
}
return rootIds;
}
private void BuildHierarchy(string transformId, Dictionary<string, Transform> transforms,
Dictionary<string, GameObject> gameObjects, int depth,
List<string> hierarchy)
{
if (!transforms.TryGetValue(transformId, out var transform))
return;
if (!gameObjects.TryGetValue(transform.GameObjectFileId, out var gameObject))
return;
// Build indentation
string prefix = depth == 0 ? "" : new string('-', depth * 2);
string line = $"{prefix}{gameObject.Name}";
hierarchy.Add(line);
// Recursively process children
foreach (var childId in transform.ChildrenFileIds)
{
BuildHierarchy(childId, transforms, gameObjects, depth + 1, hierarchy);
}
}
}
class GameObject
{
public string FileId { get; set; } = "";
public string Name { get; set; } = "";
}
class Transform
{
public string FileId { get; set; } = "";
public string GameObjectFileId { get; set; } = "";
public string? ParentFileId { get; set; }
public List<string> ChildrenFileIds { get; set; } = new();
}