Skip to content

Commit

Permalink
Fix TMPro migration bug (lost references) in previous version
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed May 5, 2024
1 parent 722c042 commit fbcc342
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 28 deletions.
25 changes: 16 additions & 9 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/ComponentReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static T TryReplaceComponent<T>(Component oldComponent, bool copyContent)
replacer.DestroyDependents();
var newComponent = gameObject.AddComponent<T>();
if (copyContent) EditorUtility.CopySerializedIfDifferent(oldComponent, newComponent);
replacer.RestoreDependents();
replacer.RestoreDependents(newComponent);
replacer.DestroyTemporary();
return newComponent;
}
Expand Down Expand Up @@ -170,28 +170,35 @@ void DestroyDependents() {
}
}

void RestoreDependents() {
void RestoreDependents(Component newAddedComponent = null) {
var stack = new Stack<ComponentReplacer>();
stack.Push(this);
while (stack.Count > 0) {
var current = stack.Pop();
foreach (var downstream in current.downstreams) stack.Push(downstream);
Component temp = null;
if (current.prefabComponent != null) {
if (current.componentIndex == componentIndex)
temp = newAddedComponent;
else if (current.prefabComponent != null) {
PrefabUtility.RevertRemovedComponent(prefabInstance, current.prefabComponent, InteractionMode.AutomatedAction);
temp = current.componentsInGameObject[current.componentIndex];
if (temp == null) sourceGameObject.TryGetComponent(current.componentType, out temp);
}
if (temp == null) temp = sourceGameObject.AddComponent(current.componentType);
current.componentsInGameObject[current.componentIndex] = temp;
if (temp != null) EditorUtility.CopySerializedIfDifferent(current.componentsInTemporary[current.componentIndex], temp);
if (temp != null) {
var src = current.componentsInTemporary[current.componentIndex];
if (src != null && src.GetType() == temp.GetType())
EditorUtility.CopySerializedIfDifferent(current.componentsInTemporary[current.componentIndex], 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();
}
using (var so = new SerializedObject(component))
try {
var sp = so.FindProperty(path);
sp.objectReferenceValue = temp;
so.ApplyModifiedProperties();
} catch {}
}
}
}
Expand Down
32 changes: 21 additions & 11 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/TMProMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
using UnityEditor;
using TMPro;

using static UnityEngine.Object;

namespace JLChnToZ.VRC.VVMW.Editors {
public static class TMProMigratator {
static readonly Dictionary<Font, TMP_FontAsset> fontAssetMapping = new Dictionary<Font, TMP_FontAsset>();
static readonly List<MonoBehaviour> tempMonoBehaviours = new List<MonoBehaviour>();

static TMProMigratator() {
#if VRC_SDK_VRCSDK3
Expand Down Expand Up @@ -48,6 +45,7 @@ public static void LoadFontMapping() {
public static void Migrate(GameObject root) {
var migratableTypes = new Dictionary<Type, bool>();
var migratableFields = new Dictionary<Type, Dictionary<FieldInfo, FieldInfo>>();
var newCreated = new Dictionary<FieldInfo, TextMeshProUGUI>();
foreach (var monoBehaviour in root.GetComponentsInChildren<MonoBehaviour>(true)) {
var type = monoBehaviour.GetType();
if (!migratableTypes.TryGetValue(type, out var isTypeMigratable))
Expand All @@ -58,7 +56,8 @@ public static void Migrate(GameObject root) {
if (attr == null) continue;
if (mapping == null) mapping = new Dictionary<FieldInfo, FieldInfo>();
var targetField = type.GetField(attr.TMProFieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (targetField != null) mapping[field] = targetField;
if (targetField != null)
mapping[field] = targetField;
}
migratableFields[type] = mapping;
}
Expand All @@ -67,17 +66,28 @@ public static void Migrate(GameObject root) {
ComponentReplacer.CanAllReferencesReplaceWith<TextMeshProUGUI>(text))
Migrate(text);
if (mapping != null) {
newCreated.Clear();
foreach (var kv in mapping) {
var sourceField = kv.Key;
var targetField = kv.Value;
var sourceValue = sourceField.GetValue(monoBehaviour);
if (sourceValue is Text sourceText) {
var targetText = Migrate(sourceText);
sourceField.SetValue(monoBehaviour, null);
targetField.SetValue(monoBehaviour, targetText);
}
if (!(sourceValue is Text sourceText) ||
kv.Value.GetValue(monoBehaviour) != null)
continue;
var targetText = Migrate(sourceText);
if (targetText == null) continue;
newCreated[sourceField] = targetText;
}
EditorUtility.SetDirty(monoBehaviour);
if (newCreated.Count > 0)
using (var so = new SerializedObject(monoBehaviour)) {
foreach (var kv in newCreated) {
if (!mapping.TryGetValue(kv.Key, out var destField)) continue;
var prop = so.FindProperty(kv.Key.Name);
if (prop != null) prop.objectReferenceValue = null;
prop = so.FindProperty(destField.Name);
if (prop != null) prop.objectReferenceValue = kv.Value;
}
so.ApplyModifiedProperties();
}
}
}
var components = new List<Component>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ PrefabInstance:
- target: {fileID: 5272184638770894274, guid: a9c62789c705a85428df158bfee22825,
type: 3}
propertyPath: m_PresetInfoIsWorld
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 6947233848682010541, guid: a9c62789c705a85428df158bfee22825,
type: 3}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7796304162394215050}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &7796304162394215054
MonoBehaviour:
Expand Down Expand Up @@ -78,10 +79,12 @@ MeshRenderer:
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 0
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 0
m_ReflectionProbeUsage: 0
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
Expand All @@ -106,6 +109,7 @@ MeshRenderer:
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!114 &7796304162394215053
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -158,4 +162,6 @@ MonoBehaviour:
texturePropertyName: _MainTex
useSharedMaterial: 0
primaryAudioSource: {fileID: 0}
useFlickerWorkaround: 1
isAvPro: 0
blitMaterial: {fileID: 0}
3 changes: 2 additions & 1 deletion Packages/idv.jlchntoz.vvmw/Prefabs/Re-Sync Button.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 4
m_PresetInfoIsWorld: 0
m_PresetInfoIsWorld: 1
--- !u!114 &6589996199566909317
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -468,6 +468,7 @@ MonoBehaviour:
m_Script: {fileID: -1533785930, guid: 661092b4961be7145bfbe56e1e62337b, type: 3}
m_Name:
m_EditorClassIdentifier:
AllowFocusView: 1
--- !u!65 &3180401798966787971
BoxCollider:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 4
m_PresetInfoIsWorld: 1
m_PresetInfoIsWorld: 0
--- !u!114 &1053562278243965984
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -304,6 +304,7 @@ MonoBehaviour:
m_Script: {fileID: -1533785930, guid: 661092b4961be7145bfbe56e1e62337b, type: 3}
m_Name:
m_EditorClassIdentifier:
AllowFocusView: 1
--- !u!65 &1053562278243965987
BoxCollider:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 4
m_PresetInfoIsWorld: 1
m_PresetInfoIsWorld: 0
--- !u!114 &480465869703688039
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -885,7 +885,7 @@ PrefabInstance:
- target: {fileID: 1749176670469352045, guid: 09128f14656df224fab9e554c7b4099c,
type: 3}
propertyPath: m_Value
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4089793476639635851, guid: 09128f14656df224fab9e554c7b4099c,
type: 3}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit fbcc342

Please sign in to comment.