2022-11-25 10:00:18 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-10-16 10:18:58 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using VRC.SDK3.Avatars.Components;
|
|
|
|
|
using VRC.SDK3.Avatars.ScriptableObjects;
|
2022-11-25 10:00:18 +08:00
|
|
|
|
using Object = UnityEngine.Object;
|
2022-10-16 10:18:58 +08:00
|
|
|
|
|
2022-11-11 12:39:58 +08:00
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
2022-11-28 05:51:39 +08:00
|
|
|
|
internal class MenuInstallHook
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
2022-11-25 11:22:23 +08:00
|
|
|
|
private static Texture2D _moreIcon = AssetDatabase.LoadAssetAtPath<Texture2D>(
|
|
|
|
|
"Packages/nadena.dev.modular-avatar/Runtime/Icons/Icon_More_A.png"
|
|
|
|
|
);
|
|
|
|
|
|
2022-10-16 10:18:58 +08:00
|
|
|
|
private Dictionary<VRCExpressionsMenu, VRCExpressionsMenu> _clonedMenus;
|
|
|
|
|
private Dictionary<VRCExpressionsMenu, VRCExpressionsMenu> _installTargets;
|
|
|
|
|
|
2022-11-07 11:33:48 +08:00
|
|
|
|
private VRCExpressionsMenu _rootMenu;
|
|
|
|
|
|
2022-10-16 10:18:58 +08:00
|
|
|
|
public void OnPreprocessAvatar(GameObject avatarRoot)
|
|
|
|
|
{
|
2022-11-07 11:33:48 +08:00
|
|
|
|
var menuInstallers = avatarRoot.GetComponentsInChildren<ModularAvatarMenuInstaller>(true)
|
|
|
|
|
.Where(c => c.enabled)
|
|
|
|
|
.ToArray();
|
2022-10-16 10:18:58 +08:00
|
|
|
|
if (menuInstallers.Length == 0) return;
|
|
|
|
|
|
|
|
|
|
_clonedMenus = new Dictionary<VRCExpressionsMenu, VRCExpressionsMenu>();
|
|
|
|
|
|
|
|
|
|
var avatar = avatarRoot.GetComponent<VRCAvatarDescriptor>();
|
|
|
|
|
|
2022-11-07 11:33:48 +08:00
|
|
|
|
if (avatar.expressionsMenu == null)
|
|
|
|
|
{
|
|
|
|
|
var menu = ScriptableObject.CreateInstance<VRCExpressionsMenu>();
|
|
|
|
|
AssetDatabase.CreateAsset(menu, Util.GenerateAssetPath());
|
|
|
|
|
avatar.expressionsMenu = menu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rootMenu = avatar.expressionsMenu;
|
2022-10-16 10:18:58 +08:00
|
|
|
|
avatar.expressionsMenu = CloneMenu(avatar.expressionsMenu);
|
|
|
|
|
_installTargets = new Dictionary<VRCExpressionsMenu, VRCExpressionsMenu>(_clonedMenus);
|
|
|
|
|
|
|
|
|
|
foreach (var install in menuInstallers)
|
|
|
|
|
{
|
|
|
|
|
InstallMenu(install);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstallMenu(ModularAvatarMenuInstaller installer)
|
|
|
|
|
{
|
2022-11-07 11:33:48 +08:00
|
|
|
|
if (!installer.enabled) return;
|
|
|
|
|
|
|
|
|
|
if (installer.installTargetMenu == null)
|
|
|
|
|
{
|
|
|
|
|
installer.installTargetMenu = _rootMenu;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 10:18:58 +08:00
|
|
|
|
if (installer.installTargetMenu == null || installer.menuToAppend == null) return;
|
|
|
|
|
if (!_installTargets.TryGetValue(installer.installTargetMenu, out var targetMenu)) return;
|
|
|
|
|
if (_installTargets.ContainsKey(installer.menuToAppend)) return;
|
|
|
|
|
|
2022-12-10 03:40:22 +08:00
|
|
|
|
// Clone before appending to sanitize menu icons
|
|
|
|
|
targetMenu.controls.AddRange(CloneMenu(installer.menuToAppend).controls);
|
2022-10-16 10:18:58 +08:00
|
|
|
|
|
|
|
|
|
while (targetMenu.controls.Count > VRCExpressionsMenu.MAX_CONTROLS)
|
|
|
|
|
{
|
|
|
|
|
// Split target menu
|
|
|
|
|
var newMenu = ScriptableObject.CreateInstance<VRCExpressionsMenu>();
|
|
|
|
|
AssetDatabase.CreateAsset(newMenu, Util.GenerateAssetPath());
|
|
|
|
|
var keepCount = VRCExpressionsMenu.MAX_CONTROLS - 1;
|
|
|
|
|
newMenu.controls.AddRange(targetMenu.controls.Skip(keepCount));
|
|
|
|
|
targetMenu.controls.RemoveRange(keepCount,
|
|
|
|
|
targetMenu.controls.Count - keepCount
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
targetMenu.controls.Add(new VRCExpressionsMenu.Control()
|
|
|
|
|
{
|
|
|
|
|
name = "More",
|
|
|
|
|
type = VRCExpressionsMenu.Control.ControlType.SubMenu,
|
2022-11-25 10:00:18 +08:00
|
|
|
|
subMenu = newMenu,
|
|
|
|
|
parameter = new VRCExpressionsMenu.Control.Parameter()
|
|
|
|
|
{
|
|
|
|
|
name = ""
|
|
|
|
|
},
|
|
|
|
|
subParameters = Array.Empty<VRCExpressionsMenu.Control.Parameter>(),
|
2022-11-25 11:22:23 +08:00
|
|
|
|
icon = _moreIcon,
|
|
|
|
|
labels = Array.Empty<VRCExpressionsMenu.Control.Label>()
|
2022-10-16 10:18:58 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_installTargets[installer.installTargetMenu] = newMenu;
|
|
|
|
|
targetMenu = newMenu;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private VRCExpressionsMenu CloneMenu(VRCExpressionsMenu menu)
|
|
|
|
|
{
|
|
|
|
|
if (menu == null) return null;
|
|
|
|
|
if (_clonedMenus.TryGetValue(menu, out var newMenu)) return newMenu;
|
|
|
|
|
newMenu = Object.Instantiate(menu);
|
|
|
|
|
AssetDatabase.CreateAsset(newMenu, Util.GenerateAssetPath());
|
|
|
|
|
_clonedMenus[menu] = newMenu;
|
2022-12-10 03:40:22 +08:00
|
|
|
|
|
2022-10-16 10:18:58 +08:00
|
|
|
|
foreach (var control in newMenu.controls)
|
|
|
|
|
{
|
2022-12-10 03:40:22 +08:00
|
|
|
|
if (Util.ValidateExpressionMenuIcon(control.icon) != Util.ValidateExpressionMenuIconResult.Success)
|
|
|
|
|
control.icon = null;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < control.labels.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var label = control.labels[i];
|
|
|
|
|
var labelResult = Util.ValidateExpressionMenuIcon(label.icon);
|
|
|
|
|
if (labelResult != Util.ValidateExpressionMenuIconResult.Success)
|
|
|
|
|
{
|
|
|
|
|
label.icon = null;
|
|
|
|
|
control.labels[i] = label;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 10:18:58 +08:00
|
|
|
|
if (control.type == VRCExpressionsMenu.Control.ControlType.SubMenu)
|
|
|
|
|
{
|
|
|
|
|
control.subMenu = CloneMenu(control.subMenu);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newMenu;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|