Skip to content

Commit

Permalink
Fix overlay control desktop mode key hint not updated
Browse files Browse the repository at this point in the history
Fix TMPro migration flow updating unsupported VRCUrlInputField
  • Loading branch information
JLChnToZ committed Apr 22, 2024
1 parent 8a31908 commit 33f91cc
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 36 deletions.
27 changes: 27 additions & 0 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/ComponentReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

namespace JLChnToZ.VRC.VVMW {
public class ComponentReplacer {
delegate FieldInfo GFIASTFP(SerializedProperty property, out Type type);
static readonly GFIASTFP getFieldInfoAndStaticTypeFromProperty = Delegate.CreateDelegate(
typeof(GFIASTFP), Type.GetType("UnityEditor.ScriptAttributeUtility, UnityEditor", false)?
.GetMethod("GetFieldInfoAndStaticTypeFromProperty", BindingFlags.NonPublic | BindingFlags.Static)
) as GFIASTFP;
static readonly Dictionary<Component, List<(Component, string)>> references = new Dictionary<Component, List<(Component, string)>>();
static readonly HashSet<(Type, string)> blackListedPaths = new HashSet<(Type, string)>();
static readonly Dictionary<Type, Type[]> dependents = new Dictionary<Type, Type[]>();
readonly List<ComponentReplacer> downstreams = new List<ComponentReplacer>();
readonly Type componentType;
Expand All @@ -19,6 +25,8 @@ public class ComponentReplacer {
Component[] componentsInTemporary;
readonly int componentIndex;

public static void AddToBlackList(Type type, string path) => blackListedPaths.Add((type, path));

public static T TryReplaceComponent<T>(Component oldComponent, bool copyContent) where T : Component {
if (oldComponent == null) return null;
var gameObject = oldComponent.gameObject;
Expand Down Expand Up @@ -93,6 +101,25 @@ public static void InitAllComponents() {
public static ICollection<(Component, string)> GetReferencedComponents(Component component) =>
component != null && references.TryGetValue(component, out var mapping) ?
mapping : Array.Empty<(Component, string)>();

public static bool CanAllReferencesReplaceWith<T>(Component component) =>
CanAllReferencesReplaceWith(component, typeof(T));

public static bool CanAllReferencesReplaceWith(Component component, Type replaceType) {
var referencedComponents = GetReferencedComponents(component);
if (referencedComponents.Count == 0) return true;
foreach (var (referencedBy, path) in referencedComponents) {
if (referencedBy == null) continue;
if (blackListedPaths.Contains((referencedBy.GetType(), path))) return false;
using (var so = new SerializedObject(referencedBy)) {
var sp = so.FindProperty(path);
if (sp == null) continue;
getFieldInfoAndStaticTypeFromProperty(sp, out var type);
if (type != null && !type.IsAssignableFrom(replaceType)) return false;
}
}
return true;
}

ComponentReplacer(GameObject sourceGameObject, Component[] components, int index) {
this.sourceGameObject = sourceGameObject;
Expand Down
15 changes: 12 additions & 3 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/TMProMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public static class TMProMigratator {
static readonly Dictionary<string, TMP_FontAsset> fontAssetMapping = new Dictionary<string, TMP_FontAsset>();
static readonly List<MonoBehaviour> tempMonoBehaviours = new List<MonoBehaviour>();

static TMProMigratator() {
#if VRC_SDK_VRCSDK3
ComponentReplacer.AddToBlackList(typeof(InputField), "m_Placeholder");
ComponentReplacer.AddToBlackList(typeof(TMP_InputField), "m_Placeholder");
ComponentReplacer.AddToBlackList(typeof(global::VRC.SDK3.Components.VRCUrlInputField), "m_Placeholder");
#endif
}

[MenuItem("Tools/VizVid/Migrate TMPro Components")]
static void MigrateSelected() {
LoadFontMapping();
Expand Down Expand Up @@ -53,7 +61,9 @@ public static void Migrate(GameObject root) {
}
migratableFields[type] = mapping;
}
if (isTypeMigratable && monoBehaviour.TryGetComponent(out Text text))
if (isTypeMigratable &&
monoBehaviour.TryGetComponent(out Text text) &&
ComponentReplacer.CanAllReferencesReplaceWith<TextMeshProUGUI>(text))
Migrate(text);
if (mapping != null) {
foreach (var kv in mapping) {
Expand All @@ -71,8 +81,7 @@ public static void Migrate(GameObject root) {
}
var components = new List<Component>();
foreach (var text in root.GetComponentsInChildren<Text>(true)) {
var referencedComponents = ComponentReplacer.GetReferencedComponents(text);
if (referencedComponents.Count > 0) continue;
if (!ComponentReplacer.CanAllReferencesReplaceWith<TextMeshProUGUI>(text)) continue;
text.GetComponents(components);
bool isRequired = false;
foreach (var component in components)
Expand Down
6 changes: 6 additions & 0 deletions Packages/idv.jlchntoz.vvmw/Prefabs/Overlay Control.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -1727,9 +1727,15 @@ MonoBehaviour:
desktopHintsReloadButtonKey: {fileID: 8178467626059293116}
desktopHintsVolumeUpKey: {fileID: 5045986053808943079}
desktopHintsVolumeDownKey: {fileID: 97217576214728287}
desktopHintsReloadButtonKey2: {fileID: 3670875378734813905}
desktopHintsVolumeUpKey2: {fileID: 5193618891654679399}
desktopHintsVolumeDownKey2: {fileID: 5898311090701198717}
desktopHintsReloadButtonKeyTMPro: {fileID: 0}
desktopHintsVolumeUpKeyTMPro: {fileID: 0}
desktopHintsVolumeDownKeyTMPro: {fileID: 0}
desktopHintsReloadButtonKey2TMPro: {fileID: 0}
desktopHintsVolumeUpKey2TMPro: {fileID: 0}
desktopHintsVolumeDownKey2TMPro: {fileID: 0}
--- !u!114 &329014519265849948
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit 33f91cc

Please sign in to comment.