Skip to content

Commit

Permalink
Migration bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Apr 20, 2024
1 parent 4a57445 commit f2f3791
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
71 changes: 44 additions & 27 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/ComponentReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

namespace JLChnToZ.VRC.VVMW {
public class ComponentReplacer {
static readonly List<Component> allComponents = new List<Component>();
static readonly Dictionary<Component, List<(Component, string)>> references = new Dictionary<Component, List<(Component, string)>>();
static readonly Dictionary<Type, Type[]> dependents = new Dictionary<Type, Type[]>();
readonly List<ComponentReplacer> downstreams = new List<ComponentReplacer>();
readonly Type componentType;
readonly HashSet<(Component, string)> references = new HashSet<(Component, string)>();
readonly GameObject sourceGameObject;
GameObject temporaryGameObject;
readonly Component[] componentsInGameObject;
Expand Down Expand Up @@ -55,44 +54,58 @@ static bool IsRequired(Type type, Type checkType) {
return false;
}

static void InitAllComponents(Scene scene) {
if (allComponents.Count != 0) return;
public static void InitAllComponents() {
#if UNITY_2022_2_OR_NEWER
int scneCount = SceneManager.loadedSceneCount;
#else
int scneCount = SceneManager.sceneCount;
#endif
var roots = new List<GameObject>();
var temp = new List<Component>();
var stack = new Stack<Transform>();
foreach (var root in scene.GetRootGameObjects())
stack.Push(root.transform);
for (int i = 0; i < scneCount; i++) {
SceneManager.GetSceneAt(i).GetRootGameObjects(roots);
foreach (var root in roots)
stack.Push(root.transform);
}
while (stack.Count > 0) {
var current = stack.Pop();
for (int i = current.childCount - 1; i >= 0; i--)
stack.Push(current.GetChild(i));
current.GetComponents(temp);
allComponents.AddRange(temp);
foreach (var c in temp) {
if (c == null || c is Transform) continue;
using (var so = new SerializedObject(c)) {
var sp = so.GetIterator();
while (sp.Next(true)) {
if (sp.propertyType != SerializedPropertyType.ObjectReference) continue;
var target = sp.objectReferenceValue as Component;
if (target == null || target == c) continue;
if (!references.TryGetValue(target, out var mapping))
references[target] = mapping = new List<(Component, string)>();
mapping.Add((c, sp.propertyPath));
}
}
}
}
}

public static ICollection<(Component, string)> GetReferencedComponents(Component component) =>
component != null && references.TryGetValue(component, out var mapping) ?
mapping : Array.Empty<(Component, string)>();

ComponentReplacer(GameObject sourceGameObject, Component[] components, int index) {
this.sourceGameObject = sourceGameObject;
componentsInGameObject = components;
componentIndex = index;
var component = components[index];
componentType = component.GetType();
InitAllComponents(sourceGameObject.scene);
foreach (var c in allComponents) {
if (c == null || c == component) continue;
if (c.gameObject == sourceGameObject)
if (IsRequired(c.GetType(), componentType)) {
int i = Array.IndexOf(componentsInGameObject, c);
if (i >= 0) downstreams.Add(new ComponentReplacer(sourceGameObject, componentsInGameObject, i));
else Debug.LogWarning($"Component {c.GetType()} is required by {componentType} but not found in the same GameObject.");
}
using (var so = new SerializedObject(c)) {
var sp = so.GetIterator();
while (sp.Next(true))
if (sp.propertyType == SerializedPropertyType.ObjectReference && sp.objectReferenceValue == component)
references.Add((c, sp.propertyPath));
}
foreach (var c in componentsInGameObject) {
if (c == null || c == component || !IsRequired(c.GetType(), componentType)) continue;
int i = Array.IndexOf(componentsInGameObject, c);
if (i >= 0) downstreams.Add(new ComponentReplacer(sourceGameObject, componentsInGameObject, i));
else Debug.LogWarning($"Component {c.GetType()} is required by {componentType} but not found in the same GameObject.");
}
Debug.Log($"Component {component.name} has {downstreams.Count} downstreams and {references.Count} references.");
}

void CloneToTemporary() {
Expand Down Expand Up @@ -133,10 +146,14 @@ void RestoreDependents() {
var temp = sourceGameObject.AddComponent(current.componentType);
current.componentsInGameObject[current.componentIndex] = temp;
if (temp != null) EditorUtility.CopySerializedIfDifferent(current.componentsInTemporary[current.componentIndex], temp);
foreach (var (component, path) in current.references) {
var sp = new SerializedObject(component).FindProperty(path);
sp.objectReferenceValue = temp;
}
if (references.TryGetValue(current.componentsInTemporary[current.componentIndex], out var mapping))
foreach (var (component, path) in mapping) {
using (var so = new SerializedObject(component)) {
var sp = so.FindProperty(path);
sp.objectReferenceValue = temp;
so.ApplyModifiedProperties();
}
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/TMProMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class TMProMigratator {
[MenuItem("Tools/VizVid/Migrate TMPro Components")]
static void MigrateSelected() {
LoadFontMapping();
ComponentReplacer.InitAllComponents();
foreach (var gameObject in Selection.gameObjects)
Migrate(gameObject);
}
Expand Down Expand Up @@ -54,7 +55,7 @@ public static void Migrate(GameObject root) {
}
if (isTypeMigratable && monoBehaviour.TryGetComponent(out Text text))
Migrate(text);
if (mapping != null)
if (mapping != null) {
foreach (var kv in mapping) {
var sourceField = kv.Key;
var targetField = kv.Value;
Expand All @@ -65,9 +66,13 @@ public static void Migrate(GameObject root) {
targetField.SetValue(monoBehaviour, targetText);
}
}
EditorUtility.SetDirty(monoBehaviour);
}
}
foreach (var text in root.GetComponentsInChildren<Text>(true)) {
var referencedComponents = ComponentReplacer.GetReferencedComponents(text);
if (referencedComponents.Count == 0) Migrate(text);
}
foreach (var text in root.GetComponentsInChildren<Text>(true))
Migrate(text);
}

public static TextMeshProUGUI Migrate(Text textComponent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace JLChnToZ.VRC.VVMW.I18N {
[UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
[AddComponentMenu("VizVid/Locales/Language Receiver")]
[DefaultExecutionOrder(1)]
[TMProMigratable]
public class LanguageReceiver : UdonSharpBehaviour {
[SerializeField, HideInInspector, BindUdonSharpEvent] LanguageManager manager;
[SerializeField] string key;
Expand Down
1 change: 1 addition & 0 deletions Packages/idv.jlchntoz.vvmw/Runtime/VVMW/ListEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace JLChnToZ.VRC.VVMW {
[AddComponentMenu("VizVid/Components/List Entry")]
[DefaultExecutionOrder(3)]
public class ListEntry : UdonSharpBehaviour {
[TMProMigratable(nameof(contentTMPro))]
[SerializeField] Text content;
[SerializeField] TextMeshProUGUI contentTMPro;
[BindEvent(nameof(Button.onClick), nameof(_OnClick))]
Expand Down
4 changes: 2 additions & 2 deletions Packages/idv.jlchntoz.vvmw/Runtime/VVMW/UIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public void _OnUIUpdate() {
break;
case 2: // Error
if (idleScreenRoot != null) idleScreenRoot.SetActive(true);
if (statusText == null) break;
if (statusText == null && statusTMPro == null) break;
if (timeContainer != null) {
SetStatusEnabled(true);
timeContainer.SetActive(false);
Expand Down Expand Up @@ -706,7 +706,7 @@ void SetText(Text text, TextMeshProUGUI tmp, string content) {
}

void SetStatusEnabled(bool enabled) {
if (timeContainer == null || statusText == null || statusTMPro == null) return;
if (timeContainer == null || (statusText == null && statusTMPro == null)) return;
timeContainer.SetActive(!enabled);
if (statusText != null) statusText.enabled = enabled;
if (statusTMPro != null) statusTMPro.enabled = enabled;
Expand Down

0 comments on commit f2f3791

Please sign in to comment.