vrchat-community / UdonSharp

A compiler for compiling C# to Udon assembly
https://udonsharp.docs.vrchat.com
MIT License
470 stars 50 forks source link

Nested prefab upgrade fixes #82

Closed MerlinVR closed 1 year ago

MerlinVR commented 1 year ago

Adds handling for upgrading nested prefabs from U# 0.x.

Does this by first building a directed acyclic graph out of any prefabs with UdonSharpBehaviours. The DAG is organized where 'parent' nodes are prefabs that do not contain any nested prefabs, but may be contained in other prefabs. The children nodes from those parents are then prefabs that nest the parent prefabs along with any prefab variants of the parents. This allows a topological ordering that visits prefabs in an order that will always visit prefabs that other prefabs nest before the nesting prefabs are visited.

After we have a topological ordering of prefabs, we visit it in reverse order and apply prefab modifications to the serialized string and object reference array fields on all UdonBehaviours that are U# behaviours. This ensures that the following upgrade passes do not get partial/leaked data from their 'parent' prefabs already being upgraded.

We next pass over the topological order and do phase 1 of upgrading prefabs which adds the U# behaviour proxies. This is currently different from the old process in that we now open the prefabs in a temp scene to edit them via PrefabUtility.LoadPrefabContents() because I haven't verified that it's safe to modify nested prefabs using the old method where we just load them directly.

Then we do the same phase 2 upgrade where we pass over all the prefabs again, but now it's in topological order, and we copy the data from the UdonBehaviours to their UdonSharpBehaviour proxies. Finally, calling ApplyProperyModifications on the proxy gets Unity to find the deltas from the 'child' prefabs so you get the proper deltas on the U# proxy.