modular-avatar/Editor/PreventStripTagObjects.cs
kaikoga 3667dc319a
chore: Wrap VRC dependencies and VRCAvatarDescriptors (#504)
* add version defines

* refactor: prefer BuildContext.ctor() overload taking GameObject

* wrap unit tests entirely with MA_VRCSDK3_AVATARS

* wrap unit tests smart with MA_VRCSDK3_AVATARS

* wrap runtime entirely with MA_VRCSDK3_AVATARS

* wrap VRC.SDKBase.IEditorOnly with MA_VRCSDK3_AVATARS

* wrap editor entirely with MA_VRCSDK3_AVATARS

* fix AvatarObjectReference.Get(Component)

* wrap editor smart with MA_VRCSDK3_AVATARS

* wrap BuildContext smart with MA_VRCSDK3_AVATARS

* wrap PluginDefinition smart with MA_VRCSDK3_AVATARS

* style: move conditional compiles one step outside
2023-11-10 15:37:56 +09:00

70 lines
2.1 KiB
C#

#if MA_VRCSDK3_AVATARS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using VRC.SDKBase;
using VRC.SDKBase.Editor.BuildPipeline;
using Object = UnityEngine.Object;
namespace nadena.dev.modular_avatar.core.editor
{
/// <summary>
/// See https://feedback.vrchat.com/sdk-bug-reports/p/ieditoronly-components-should-be-destroyed-late-in-the-build-process
/// </summary>
[InitializeOnLoad]
internal static class PreventStripTagObjects
{
static PreventStripTagObjects()
{
EditorApplication.delayCall += () =>
{
var f_callbacks = typeof(VRCBuildPipelineCallbacks).GetField("_preprocessAvatarCallbacks",
BindingFlags.Static | BindingFlags.NonPublic);
var callbacks = (List<IVRCSDKPreprocessAvatarCallback>) f_callbacks.GetValue(null);
var filteredCallbacks = callbacks.Where(c => !(c is RemoveAvatarEditorOnly)).ToList();
f_callbacks.SetValue(null, filteredCallbacks);
};
}
}
internal class ReplacementRemoveAvatarEditorOnly : IVRCSDKPreprocessAvatarCallback
{
public int callbackOrder => -1024;
public bool OnPreprocessAvatar(GameObject avatarGameObject)
{
foreach (var xform in avatarGameObject.GetComponentsInChildren<Transform>(true))
{
if (xform != null && xform.CompareTag("EditorOnly"))
{
Object.DestroyImmediate(xform.gameObject);
}
}
return true;
}
}
internal class ReplacementRemoveIEditorOnly : IVRCSDKPreprocessAvatarCallback
{
public int callbackOrder => Int32.MaxValue;
public bool OnPreprocessAvatar(GameObject avatarGameObject)
{
foreach (var component in avatarGameObject.GetComponentsInChildren<IEditorOnly>(true))
{
Object.DestroyImmediate(component as Object);
}
return true;
}
}
}
#endif