2022-09-09 11:40:52 +08:00
|
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2022 bd_
|
|
|
|
|
*
|
|
|
|
|
* 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:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* 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;
|
2022-08-28 04:38:52 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using VRC.SDK3.Avatars.Components;
|
2022-10-03 09:04:27 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
#endif
|
2022-08-28 04:38:52 +08:00
|
|
|
|
|
|
|
|
|
namespace net.fushizen.modular_avatar.core
|
|
|
|
|
{
|
|
|
|
|
public static class RuntimeUtil
|
|
|
|
|
{
|
2022-09-11 01:47:41 +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
|
|
|
|
|
|
|
|
|
public enum OnDemandSource
|
|
|
|
|
{
|
|
|
|
|
Awake,
|
|
|
|
|
Start
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delegate void OnDemandProcessAvatarDelegate(OnDemandSource source, AvatarTagComponent component);
|
|
|
|
|
|
|
|
|
|
public static OnDemandProcessAvatarDelegate OnDemandProcessAvatar = (_m, _c) => { };
|
|
|
|
|
|
2022-08-28 04:38:52 +08:00
|
|
|
|
[CanBeNull]
|
|
|
|
|
public static string RelativePath(GameObject root, GameObject child)
|
|
|
|
|
{
|
|
|
|
|
if (root == child) return "";
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-28 04:38:52 +08:00
|
|
|
|
List<string> pathSegments = new List<string>();
|
|
|
|
|
while (child != root && child != null)
|
|
|
|
|
{
|
|
|
|
|
pathSegments.Add(child.name);
|
2022-10-03 08:37:50 +08:00
|
|
|
|
child = child.transform.parent?.gameObject;
|
2022-08-28 04:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (child == null) return null;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-28 04:38:52 +08:00
|
|
|
|
pathSegments.Reverse();
|
|
|
|
|
return String.Join("/", pathSegments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
|
public static string AvatarRootPath(GameObject child)
|
|
|
|
|
{
|
|
|
|
|
var avatar = FindAvatarInParents(child.transform);
|
|
|
|
|
if (avatar == null) return null;
|
|
|
|
|
return RelativePath(avatar.gameObject, child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static VRCAvatarDescriptor FindAvatarInParents(Transform target)
|
|
|
|
|
{
|
|
|
|
|
while (target != null)
|
|
|
|
|
{
|
|
|
|
|
var av = target.GetComponent<VRCAvatarDescriptor>();
|
|
|
|
|
if (av != null) return av;
|
|
|
|
|
target = target.parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-08-28 10:04:53 +08:00
|
|
|
|
|
|
|
|
|
public static void MarkDirty(UnityEngine.Object obj)
|
|
|
|
|
{
|
2022-08-30 06:13:26 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(obj))
|
2022-08-28 10:04:53 +08:00
|
|
|
|
{
|
2022-08-30 06:13:26 +08:00
|
|
|
|
UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(obj);
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
UnityEditor.EditorUtility.SetDirty(obj);
|
2022-10-03 08:37:50 +08:00
|
|
|
|
#endif
|
2022-08-28 10:04:53 +08:00
|
|
|
|
}
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
private static UnityEngine.Object cachedAnimationWindowState;
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
|
|
|
|
private static readonly Type animationWindowStateType
|
2022-08-30 06:13:26 +08:00
|
|
|
|
= typeof(UnityEditor.Editor).Assembly.GetType("UnityEditorInternal.AnimationWindowState");
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
private static readonly PropertyInfo recordingProp = animationWindowStateType.GetProperty(
|
|
|
|
|
"recording",
|
|
|
|
|
BindingFlags.Instance | BindingFlags.Public
|
|
|
|
|
);
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
private static readonly PropertyInfo previewingProp = animationWindowStateType.GetProperty(
|
|
|
|
|
"previewing",
|
|
|
|
|
BindingFlags.Instance | BindingFlags.Public
|
|
|
|
|
);
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
private static readonly PropertyInfo playingProp = animationWindowStateType.GetProperty(
|
|
|
|
|
"playing",
|
|
|
|
|
BindingFlags.Instance | BindingFlags.Public
|
|
|
|
|
);
|
|
|
|
|
#endif
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
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)
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|| (bool) previewingProp.GetValue(cachedAnimationWindowState, null)
|
|
|
|
|
|| (bool) playingProp.GetValue(cachedAnimationWindowState, null);
|
2022-08-30 06:13:26 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2022-10-03 08:37:50 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
public static bool isPlaying => UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode;
|
|
|
|
|
#else
|
|
|
|
|
public static bool isPlaying => true;
|
|
|
|
|
#endif
|
2022-10-03 08:37:50 +08:00
|
|
|
|
public static void InvokeHierarchyChanged()
|
|
|
|
|
{
|
|
|
|
|
OnHierarchyChanged?.Invoke();
|
|
|
|
|
}
|
2022-08-28 04:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|