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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using VRC.Udon.Common.Interfaces;
using VRC.Udon.EditorBindings;
using VRC.Udon.EditorBindings.Interfaces;
using VRC.Udon.Graph;
using VRC.Udon.Graph.Interfaces;
using VRC.Udon.UAssembly.Interfaces;
namespace VRC.Udon.Editor
{
public class UdonEditorManager : IUdonEditorInterface
{
#region Singleton
private static UdonEditorManager _instance;
public static UdonEditorManager Instance => _instance ?? (_instance = new UdonEditorManager());
#endregion
#region Build Preprocessor Class
private class UdonBuildPreprocessor : IProcessSceneWithReport
{
public int callbackOrder => 0;
public void OnProcessScene(Scene scene, BuildReport report)
{
PopulateSceneSerializedProgramAssetReferences(scene);
PopulateAssetDependenciesPrefabSerializedProgramAssetReferences(scene.path);
}
}
#endregion
#region Public Events
public event Action WantRepaint;
#endregion
#region Private Constants
private const double REFRESH_QUEUE_WAIT_PERIOD = 5.0;
#endregion
#region Private Fields
private readonly UdonEditorInterface _udonEditorInterface;
private readonly HashSet<AbstractUdonProgramSource> _programSourceRefreshQueue = new HashSet<AbstractUdonProgramSource>();
#endregion
#region Initialization
[InitializeOnLoadMethod]
private static void Initialize()
{
_instance = new UdonEditorManager();
}
#endregion
#region Constructors
private UdonEditorManager()
{
_udonEditorInterface = new UdonEditorInterface();
_udonEditorInterface.AddTypeResolver(new UdonBehaviourTypeResolver());
EditorSceneManager.sceneOpened += OnSceneOpened;
EditorSceneManager.sceneSaving += OnSceneSaving;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
#endregion
#region UdonBehaviour and ProgramSource Refresh
public void QueueAndRefreshProgram(AbstractUdonProgramSource programSource)
{
QueueProgramSourceRefresh(programSource);
RefreshQueuedProgramSources();
}
public void RefreshQueuedProgramSources()
{
foreach(AbstractUdonProgramSource programSource in _programSourceRefreshQueue)
{
if(programSource == null)
{
return;
}
try
{
programSource.RefreshProgram();
}
catch(Exception e)
{
UnityEngine.Debug.LogError($"Failed to refresh program '{programSource.name}' due to exception '{e}'.");
}
}
_programSourceRefreshQueue.Clear();
WantRepaint?.Invoke();
}
public bool IsProgramSourceRefreshQueued(AbstractUdonProgramSource programSource)
{
if(_programSourceRefreshQueue.Count <= 0)
{
return false;
}
if(!_programSourceRefreshQueue.Contains(programSource))
{
return false;
}
return true;
}
public void QueueProgramSourceRefresh(AbstractUdonProgramSource programSource)
{
if(Application.isPlaying)
{
return;
}
if(programSource == null)
{
return;
}
if(IsProgramSourceRefreshQueued(programSource))
{
return;
}
_programSourceRefreshQueue.Add(programSource);
}
public void CancelQueuedProgramSourceRefresh(AbstractUdonProgramSource programSource)
{
if(programSource == null)
{
return;
}
if(_programSourceRefreshQueue.Contains(programSource))
{
_programSourceRefreshQueue.Remove(programSource);
}
}
[MenuItem("VRChat SDK/Utilities/Re-compile All Program Sources")]
public static void RecompileAllProgramSources()
{
string[] programSourceGUIDs = AssetDatabase.FindAssets("t:AbstractUdonProgramSource");
foreach(string guid in programSourceGUIDs)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
AbstractUdonProgramSource programSource = AssetDatabase.LoadAssetAtPath<AbstractUdonProgramSource>(assetPath);
if(programSource == null)
{
continue;
}
programSource.RefreshProgram();
}
AssetDatabase.SaveAssets();
PopulateAllPrefabSerializedProgramAssetReferences();
}
[PublicAPI]
public static void PopulateAllPrefabSerializedProgramAssetReferences()
{
foreach(string prefabPath in GetAllPrefabAssetPaths())
{
PopulatePrefabSerializedProgramAssetReferences(prefabPath);
}
}
private static List<UdonBehaviour> prefabBehavioursTempList = new List<UdonBehaviour>();
[PublicAPI]
public static void PopulateAssetDependenciesPrefabSerializedProgramAssetReferences(string assetPath)
{
IEnumerable<string> prefabDependencyPaths = AssetDatabase.GetDependencies(assetPath, true)
.Where(path => path.EndsWith(".prefab"))
.Where(path => path.StartsWith("Assets"));
foreach(string prefabPath in prefabDependencyPaths)
{
if(!(AssetDatabase.LoadMainAssetAtPath(prefabPath) is GameObject prefab))
{
return;
}
prefab.GetComponentsInChildren<UdonBehaviour>(prefabBehavioursTempList);
if(prefabBehavioursTempList.Count < 1)
{
return;
}
PopulatePrefabSerializedProgramAssetReferences(prefabPath);
}
}
private static void PopulatePrefabSerializedProgramAssetReferences(string prefabPath)
{
using(EditPrefabAssetScope editScope = new EditPrefabAssetScope(prefabPath))
{
if(!editScope.IsEditable)
{
return;
}
editScope.PrefabRoot.GetComponentsInChildren(prefabBehavioursTempList);
if(prefabBehavioursTempList.Count < 1)
{
return;
}
bool dirty = false;
foreach(UdonBehaviour udonBehaviour in prefabBehavioursTempList)
{
if(PopulateSerializedProgramAssetReference(udonBehaviour))
{
dirty = true;
}
}
if(dirty)
{
editScope.MarkDirty();
}
}
}
#endregion
#region Scene Manager Callbacks
private void OnSceneOpened(Scene scene, OpenSceneMode mode)
{
RefreshQueuedProgramSources();
PopulateSceneSerializedProgramAssetReferences(scene);
}
private void OnSceneSaving(Scene scene, string _)
{
RefreshQueuedProgramSources();
PopulateSceneSerializedProgramAssetReferences(scene);
}
private static void PopulateSceneSerializedProgramAssetReferences(Scene scene)
{
if (!scene.IsValid())
{
return;
}
foreach(GameObject sceneGameObject in scene.GetRootGameObjects())
{
foreach(UdonBehaviour udonBehaviour in sceneGameObject.GetComponentsInChildren<UdonBehaviour>(true))
{
PopulateSerializedProgramAssetReference(udonBehaviour);
}
}
}
// Returns true if the serializedProgramProperty was changed, false otherwise.
private static bool PopulateSerializedProgramAssetReference(UdonBehaviour udonBehaviour)
{
SerializedObject serializedUdonBehaviour = new SerializedObject(udonBehaviour);
SerializedProperty programSourceSerializedProperty = serializedUdonBehaviour.FindProperty("programSource");
SerializedProperty serializedProgramAssetSerializedProperty = serializedUdonBehaviour.FindProperty("serializedProgramAsset");
if(!(programSourceSerializedProperty.objectReferenceValue is AbstractUdonProgramSource abstractUdonProgramSource))
{
return false;
}
if(abstractUdonProgramSource == null)
{
return false;
}
if(serializedProgramAssetSerializedProperty.objectReferenceValue == abstractUdonProgramSource.SerializedProgramAsset)
{
return false;
}
serializedProgramAssetSerializedProperty.objectReferenceValue = abstractUdonProgramSource.SerializedProgramAsset;
serializedUdonBehaviour.ApplyModifiedPropertiesWithoutUndo();
return true;
}
#endregion
#region PlayMode Callback
private void OnPlayModeStateChanged(PlayModeStateChange playModeStateChange)
{
if(playModeStateChange != PlayModeStateChange.ExitingEditMode)
{
return;
}
for(int index = 0; index < SceneManager.sceneCount; index++)
{
PopulateSceneSerializedProgramAssetReferences(SceneManager.GetSceneAt(index));
}
}
#endregion
#region IUdonEditorInterface Methods
public IUdonVM ConstructUdonVM()
{
return _udonEditorInterface.ConstructUdonVM();
}
public IUdonProgram Assemble(string assembly)
{
return _udonEditorInterface.Assemble(assembly);
}
public IUdonWrapper GetWrapper()
{
return _udonEditorInterface.GetWrapper();
}
public IUdonHeap ConstructUdonHeap()
{
return _udonEditorInterface.ConstructUdonHeap();
}
public IUdonHeap ConstructUdonHeap(uint heapSize)
{
return _udonEditorInterface.ConstructUdonHeap(heapSize);
}
public string CompileGraph(
IUdonCompilableGraph graph, INodeRegistry nodeRegistry,
out Dictionary<string, (string uid, string fullName, int index)> linkedSymbols,
out Dictionary<string, (object value, Type type)> heapDefaultValues
)
{
return _udonEditorInterface.CompileGraph(graph, nodeRegistry, out linkedSymbols, out heapDefaultValues);
}
public Type GetTypeFromTypeString(string typeString)
{
return _udonEditorInterface.GetTypeFromTypeString(typeString);
}
public void AddTypeResolver(IUAssemblyTypeResolver typeResolver)
{
_udonEditorInterface.AddTypeResolver(typeResolver);
}
public string[] DisassembleProgram(IUdonProgram program)
{
return _udonEditorInterface.DisassembleProgram(program);
}
public string DisassembleInstruction(IUdonProgram program, ref uint offset)
{
return _udonEditorInterface.DisassembleInstruction(program, ref offset);
}
public UdonNodeDefinition GetNodeDefinition(string identifier)
{
return _udonEditorInterface.GetNodeDefinition(identifier);
}
public IEnumerable<UdonNodeDefinition> GetNodeDefinitions()
{
return _udonEditorInterface.GetNodeDefinitions();
}
public Dictionary<string, INodeRegistry> GetNodeRegistries()
{
return _udonEditorInterface.GetNodeRegistries();
}
private IReadOnlyDictionary<string, ReadOnlyCollection<KeyValuePair<string, INodeRegistry>>> _topRegistries;
public IReadOnlyDictionary<string, ReadOnlyCollection<KeyValuePair<string, INodeRegistry>>> GetTopRegistries()
{
if (_topRegistries != null) return _topRegistries;
var topRegistries = new Dictionary<string, List<KeyValuePair<string, INodeRegistry>>>()
{
{"System", new List<KeyValuePair<string, INodeRegistry>>()},
{"Udon", new List<KeyValuePair<string, INodeRegistry>>()},
{"VRC", new List<KeyValuePair<string, INodeRegistry>>()},
{"UnityEngine", new List<KeyValuePair<string, INodeRegistry>>()},
};
// Go through each node registry and put it in the right parent registry
foreach (KeyValuePair<string, INodeRegistry> nodeRegistry in GetNodeRegistries())
{
if (nodeRegistry.Key.StartsWith("System"))
{
topRegistries["System"].Add(nodeRegistry);
}
else if (nodeRegistry.Key.StartsWith("Udon"))
{
topRegistries["Udon"].Add(nodeRegistry);
}
else if (nodeRegistry.Key.StartsWith("VRC"))
{
topRegistries["VRC"].Add(nodeRegistry);
}
else if (nodeRegistry.Key.StartsWith("UnityEngine"))
{
topRegistries["UnityEngine"].Add(nodeRegistry);
}
else if (nodeRegistry.Key.StartsWith("Cinemachine"))
{
topRegistries["UnityEngine"].Add(nodeRegistry);
}
else if (nodeRegistry.Key.StartsWith("TMPro"))
{
topRegistries["UnityEngine"].Add(nodeRegistry);
}
else
{
// Todo: note and handle these
UnityEngine.Debug.Log($"The Registry {nodeRegistry.Key} needs to be Added Somewhere");
}
}
// Save result as cached variable
_topRegistries = new ReadOnlyDictionary<string, ReadOnlyCollection<KeyValuePair<string, INodeRegistry>>>
(
topRegistries.ToDictionary(entry => entry.Key, entry => entry.Value.AsReadOnly())
);
// return cached version
return _topRegistries;
}
private Dictionary<string, INodeRegistry> _registryLookup;
private void CacheRegistryLookup()
{
_registryLookup = new Dictionary<string, INodeRegistry>();
foreach (KeyValuePair<string, INodeRegistry> topRegistry in GetNodeRegistries())
{
// save top-level registry. do we need to do this? probably not
_registryLookup.Add(topRegistry.Key, topRegistry.Value);
foreach (KeyValuePair<string, INodeRegistry> registry in topRegistry.Value.GetNodeRegistries())
{
_registryLookup.Add(registry.Key, registry.Value);
}
}
}
public bool TryGetRegistry(string name, out INodeRegistry registry)
{
if(_registryLookup == null)
{
CacheRegistryLookup();
}
return _registryLookup.TryGetValue(name, out registry);
}
public IEnumerable<UdonNodeDefinition> GetNodeDefinitions(string baseIdentifier)
{
return _udonEditorInterface.GetNodeDefinitions(baseIdentifier);
}
#endregion
#region Prefab Utilities
private static IEnumerable<string> GetAllPrefabAssetPaths()
{
return AssetDatabase.GetAllAssetPaths()
.Where(path => path.EndsWith(".prefab"))
.Where(path => path.StartsWith("Assets"));
}
private class EditPrefabAssetScope : IDisposable
{
private readonly string _assetPath;
private readonly GameObject _prefabRoot;
public GameObject PrefabRoot => _disposed ? null : _prefabRoot;
private readonly bool _isEditable;
public bool IsEditable => !_disposed && _isEditable;
private bool _dirty = false;
private bool _disposed;
public EditPrefabAssetScope(string assetPath)
{
_assetPath = assetPath;
_prefabRoot = PrefabUtility.LoadPrefabContents(_assetPath);
_isEditable = !PrefabUtility.IsPartOfImmutablePrefab(_prefabRoot);
}
public void MarkDirty()
{
_dirty = true;
}
public void Dispose()
{
if(_disposed)
{
return;
}
_disposed = true;
if(_dirty)
{
try
{
PrefabUtility.SaveAsPrefabAsset(_prefabRoot, _assetPath);
}
catch(Exception e)
{
UnityEngine.Debug.LogError($"Failed to save changes to prefab at '{_assetPath}' due to exception '{e}'.");
}
}
PrefabUtility.UnloadPrefabContents(_prefabRoot);
}
}
#endregion
}
}
|