modular-avatar/Runtime/RuntimeUtil.cs

192 lines
6.0 KiB
C#
Raw Normal View History

2022-09-09 11:40:52 +08:00
/*
* MIT License
2023-10-06 18:21:43 +08:00
*
2022-09-09 11:40:52 +08:00
* Copyright (c) 2022 bd_
2023-10-06 18:21:43 +08:00
*
2022-09-09 11:40:52 +08:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2023-10-06 18:21:43 +08:00
*
2022-09-09 11:40:52 +08:00
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2023-10-06 18:21:43 +08:00
*
2022-09-09 11:40:52 +08:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Linq;
2022-08-28 04:38:52 +08:00
using JetBrains.Annotations;
using UnityEngine;
#if MA_VRCSDK3_AVATARS
2022-08-28 04:38:52 +08:00
using VRC.SDK3.Avatars.Components;
#endif
#if UNITY_EDITOR
using System.Reflection;
#endif
2022-08-28 04:38:52 +08:00
2022-11-11 12:39:58 +08:00
namespace nadena.dev.modular_avatar.core
2022-08-28 04:38:52 +08:00
{
internal static class RuntimeUtil
2022-08-28 04:38:52 +08:00
{
// Initialized in Util
2022-10-20 10:42:33 +08:00
public static Action<Action> delayCall = (_) => { };
public static event Action OnHierarchyChanged;
2022-09-12 05:19:05 +08:00
internal static event Action OnMenuInvalidate;
internal static void InvalidateMenu()
{
OnMenuInvalidate?.Invoke();
}
2023-07-30 00:15:16 +08:00
internal static T GetOrAddComponent<T>(this Component self) where T : Component
{
var component = self.GetComponent<T>();
if (component == null) component = self.gameObject.AddComponent<T>();
return component;
}
internal static T GetOrAddComponent<T>(this GameObject self) where T : Component
{
var component = self.GetComponent<T>();
if (component == null) component = self.AddComponent<T>();
return component;
}
2022-08-28 04:38:52 +08:00
[CanBeNull]
public static string RelativePath(GameObject root, GameObject child)
{
return ndmf.runtime.RuntimeUtil.RelativePath(root, child);
2022-08-28 04:38:52 +08:00
}
[CanBeNull]
public static string AvatarRootPath(GameObject child)
{
return ndmf.runtime.RuntimeUtil.AvatarRootPath(child);
}
public static bool IsAvatarRoot(Transform target)
{
return ndmf.runtime.RuntimeUtil.IsAvatarRoot(target);
2022-08-28 04:38:52 +08:00
}
#if MA_VRCSDK3_AVATARS
2022-08-28 04:38:52 +08:00
public static VRCAvatarDescriptor FindAvatarInParents(Transform target)
{
while (target != null)
{
var av = target.GetComponent<VRCAvatarDescriptor>();
if (av != null) return av;
target = target.parent;
}
return null;
}
#endif
2022-08-28 10:04:53 +08:00
public static Transform FindAvatarTransformInParents(Transform target)
{
return ndmf.runtime.RuntimeUtil.FindAvatarInParents(target);
}
2022-08-28 10:04:53 +08:00
public static void MarkDirty(UnityEngine.Object obj)
{
#if UNITY_EDITOR
if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(obj))
2022-08-28 10:04:53 +08:00
{
UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(obj);
2022-08-28 10:04:53 +08:00
}
UnityEditor.EditorUtility.SetDirty(obj);
#endif
2022-08-28 10:04:53 +08:00
}
#if UNITY_EDITOR
private static UnityEngine.Object cachedAnimationWindowState;
private static readonly Type animationWindowStateType
= typeof(UnityEditor.Editor).Assembly.GetType("UnityEditorInternal.AnimationWindowState");
private static readonly PropertyInfo recordingProp = animationWindowStateType.GetProperty(
"recording",
BindingFlags.Instance | BindingFlags.Public
);
private static readonly PropertyInfo previewingProp = animationWindowStateType.GetProperty(
"previewing",
BindingFlags.Instance | BindingFlags.Public
);
private static readonly PropertyInfo playingProp = animationWindowStateType.GetProperty(
"playing",
BindingFlags.Instance | BindingFlags.Public
);
#endif
public static bool IsAnimationEditMode()
{
#if !UNITY_EDITOR
return false;
#else
if (cachedAnimationWindowState == null)
{
foreach (var obj in Resources.FindObjectsOfTypeAll(animationWindowStateType))
{
cachedAnimationWindowState = obj;
}
}
if (cachedAnimationWindowState == null) return false;
return (bool) recordingProp.GetValue(cachedAnimationWindowState, null)
|| (bool) previewingProp.GetValue(cachedAnimationWindowState, null)
|| (bool) playingProp.GetValue(cachedAnimationWindowState, null);
#endif
}
public static bool isPlaying => ndmf.runtime.RuntimeUtil.IsPlaying;
public static void InvokeHierarchyChanged()
{
OnHierarchyChanged?.Invoke();
}
#if UNITY_EDITOR
private static readonly Type addComponentWindowType = Type.GetType("UnityEditor.AddComponent.AddComponentWindow, UnityEditor");
#endif
public static bool IsResetFromInspector(int skipStackFrames = 1)
{
#if !UNITY_EDITOR
return false;
#else
var frames = new System.Diagnostics.StackTrace(skipStackFrames).GetFrames();
// Reset from component context menu
if (frames.Length == 1)
{
return true;
}
// Added from Add Component button
if (frames.Any(x => x.GetMethod().DeclaringType == addComponentWindowType))
{
return true;
}
return false;
#endif
}
2022-08-28 04:38:52 +08:00
}
2023-01-19 20:32:44 +08:00
}