diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 55918aca3..b7431c305 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog]. ### Added ### Changed +- **BREAKING** Removed Prefab Safe List `#95` ### Deprecated diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ca9eafb4..116230c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog]. ### Added ### Changed +- **BREAKING** Removed Prefab Safe List `#95` ### Deprecated diff --git a/Editor/Migration/Migration.cs b/Editor/Migration/Migration.cs index 832fbd4c7..d169aab82 100644 --- a/Editor/Migration/Migration.cs +++ b/Editor/Migration/Migration.cs @@ -270,11 +270,23 @@ private static bool MigrateComponent(AvatarTagComponent component, int forceVers { #pragma warning disable CS0618 case 1: + if (serialized.targetObject.GetType() == typeof(RemoveMeshInBox)) + { + // format for RemoveMeshInBox is reverted to v1 in v3 so skip. + versionProp.intValue = 3; + modified = true; + goto case 3; + } MigrateV1ToV2(nestCount, serialized); versionProp.intValue = 2; modified = true; goto case 2; case 2: + MigrateV2ToV3(nestCount, serialized); + versionProp.intValue = 2; + modified = true; + goto case 3; + case 3: #pragma warning restore CS0618 // it's current version: save serialized.ApplyModifiedProperties(); @@ -362,9 +374,78 @@ private static void MigrateV1ToV2(int nestCount, SerializedObject serialized) case 4: { // RemoveMeshInBox - MigrateList(serialized.FindProperty(nameof(RemoveMeshInBox.boxes)), - serialized.FindProperty(nameof(RemoveMeshInBox.boxList)), - nestCount); + throw new Exception("V1 to V2 migration for RemoveMeshInBox has been removed"); + } + } + } + + private static readonly TypeId MigrateV2ToV3Types = new TypeId(new [] + { + typeof(RemoveMeshInBox), + }); + + [Obsolete("migration process")] + private static void MigrateV2ToV3(int nestCount, SerializedObject serialized) + { + switch (MigrateV2ToV3Types.GetType(serialized.targetObject.GetType())) + { + case 1: + { + // RemoveMeshInBox + SerializedProperty[] GetContainers() + { + var psList = serialized.FindProperty(nameof(RemoveMeshInBox.boxList)); + // Container[] + var firstLayer = + psList.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.firstLayer)); + // Layer[] + var prefabLayers = + psList.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.prefabLayers)); + // Container[][] + var layerArrays = new SerializedProperty[prefabLayers.arraySize + 1]; + + layerArrays[0] = firstLayer; + + for (var i = 0; i < prefabLayers.arraySize; ++i) + layerArrays[i + 1] = prefabLayers.GetArrayElementAtIndex(i) + .FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.Layer.elements)); + + var count = layerArrays.Sum(x => x.arraySize); + var result = new SerializedProperty[count]; + + var k = 0; + foreach (var ary in layerArrays) + for (var j = 0; j < ary.arraySize; ++j) + result[k++] = ary.GetArrayElementAtIndex(j); + + return result; + } + + var containers = GetContainers(); + var array = serialized.FindProperty(nameof(RemoveMeshInBox.boxes)); + if (array.arraySize != containers.Length) + array.arraySize = containers.Length; + + for (var i = 0; i < containers.Length; i++) + { + var container = containers[i]; + var removed = + container.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.Container.removed)); + var value = container.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.Container + .value)); + var field = array.GetArrayElementAtIndex(i); + + field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.size)).vector3Value = + removed.boolValue + ? Vector3.zero + : value.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.size)).vector3Value; + + field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.center)).vector3Value = + field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.center)).vector3Value; + + field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.rotation)).quaternionValue = + field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.rotation)).quaternionValue; + } break; } } @@ -410,35 +491,6 @@ private static void MigrateSet( Assert.IsTrue(valuesSet.SetEquals(renderersSet.Values)); } - private static void MigrateList(SerializedProperty arrayProperty, SerializedProperty listProperty, - int nestCount) - { - var list = PrefabSafeList.EditorUtil.Create(listProperty, nestCount); - int index = 0; - foreach (var element in list.Elements) - { - if (!element.Contains) continue; - if (index == arrayProperty.arraySize) - element.RemovedProperty.boolValue = true; - else - { - CopySerializedPropertyValue.Copy( - arrayProperty.GetArrayElementAtIndex(index), - element.ValueProperty); - index++; - } - } - - while (index < arrayProperty.arraySize) - { - var element = list.AddElement(); - CopySerializedPropertyValue.Copy( - arrayProperty.GetArrayElementAtIndex(index), - element.ValueProperty); - index++; - } - } - private readonly struct TypeId { [CanBeNull] private readonly Dictionary _map; diff --git a/Editor/Processors/SkinnedMeshes/RemoveMeshInBoxProcessor.cs b/Editor/Processors/SkinnedMeshes/RemoveMeshInBoxProcessor.cs index 5d2ee559c..ff84c13e1 100644 --- a/Editor/Processors/SkinnedMeshes/RemoveMeshInBoxProcessor.cs +++ b/Editor/Processors/SkinnedMeshes/RemoveMeshInBoxProcessor.cs @@ -19,7 +19,7 @@ public override void Process(OptimizerSession session, MeshInfo2 target, MeshInf foreach (var vertex in target.Vertices) { var actualPosition = vertex.ComputeActualPosition(target, Target.transform.worldToLocalMatrix); - if (Component.boxList.GetAsList().Any(x => x.ContainsVertex(actualPosition))) + if (Component.boxes.Any(x => x.ContainsVertex(actualPosition))) inBoxVertices.Add(vertex); } diff --git a/Editor/RemoveMeshInBoxEditor.cs b/Editor/RemoveMeshInBoxEditor.cs index 34a2772dc..fbf32ab66 100644 --- a/Editor/RemoveMeshInBoxEditor.cs +++ b/Editor/RemoveMeshInBoxEditor.cs @@ -1,5 +1,6 @@ +using System; using System.Collections.Generic; -using Anatawa12.AvatarOptimizer.PrefabSafeList; +using JetBrains.Annotations; using UnityEditor; using UnityEngine; @@ -8,34 +9,48 @@ namespace Anatawa12.AvatarOptimizer [CustomEditor(typeof(RemoveMeshInBox))] internal class RemoveMeshInBoxEditor : AvatarTagComponentEditorBase { - private ListEditor _boxList; + private SerializedProperty _boxes; + private string _editingBoxPropPath; + private readonly Dictionary _eulerAngles = + new Dictionary(); private void OnEnable() { - var nestCount = PrefabSafeListUtil.PrefabNestCount(serializedObject.targetObject); - _boxList = new ListEditor(serializedObject.FindProperty("boxList"), nestCount); + _boxes = serializedObject.FindProperty(nameof(RemoveMeshInBox.boxes)); } protected override void OnInspectorGUIInner() { - var rect = EditorGUILayout.GetControlRect(true, _boxList.GetPropertyHeight()); - _boxList.OnGUI(rect); + // size prop + _boxes.isExpanded = true; + using (new BoundingBoxEditor.EditorScope(this)) + EditorGUILayout.PropertyField(_boxes); serializedObject.ApplyModifiedProperties(); } - private class ListEditor : EditorBase + [CustomPropertyDrawer(typeof(RemoveMeshInBox.BoundingBox))] + class BoundingBoxEditor : PropertyDrawer { - public string EditingBoxPropPath; + [CanBeNull] private static RemoveMeshInBoxEditor _upstreamEditor; - private readonly Dictionary _eulerAngle = - new Dictionary(); - - public ListEditor(SerializedProperty property, int nestCount) : base(property, nestCount) + public readonly struct EditorScope : IDisposable { + private readonly RemoveMeshInBoxEditor _oldEditor; + + public EditorScope(RemoveMeshInBoxEditor editor) + { + _oldEditor = _upstreamEditor; + _upstreamEditor = editor; + } + + public void Dispose() + { + _upstreamEditor = _oldEditor; + } } - protected override float FieldHeight(SerializedProperty serializedProperty) + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return EditorGUIUtility.singleLineHeight // header + EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight // center @@ -45,40 +60,36 @@ protected override float FieldHeight(SerializedProperty serializedProperty) ; } - protected override bool Field(Rect position, GUIContent label, SerializedProperty boxProp) + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { - var removeElement = false; - position.height = EditorGUIUtility.singleLineHeight; - var prevWidth = position.width; - var prevX = position.x; - position.width -= EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; EditorGUI.LabelField(position, label); - position.x += position.width + EditorGUIUtility.standardVerticalSpacing; - position.width = EditorGUIUtility.singleLineHeight; - - if (GUI.Button(position, EditorStatics.RemoveButton)) - removeElement = true; - - position.width = prevWidth; - position.x = prevX; position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; - var centerProp = boxProp.FindPropertyRelative("center"); - var sizeProp = boxProp.FindPropertyRelative("size"); - var rotationProp = boxProp.FindPropertyRelative("rotation"); + var centerProp = property.FindPropertyRelative("center"); + var sizeProp = property.FindPropertyRelative("size"); + var rotationProp = property.FindPropertyRelative("rotation"); using (new EditorGUI.IndentLevelScope()) { using (new GUILayout.HorizontalScope()) { - var editingCurrent = EditingBoxPropPath == boxProp.propertyPath; - if (GUI.Button(position, editingCurrent ? "Finish Editing Box" : "Edit This Box")) + if (_upstreamEditor) + { + var editingCurrent = _upstreamEditor._editingBoxPropPath == property.propertyPath; + if (GUI.Button(position, editingCurrent ? "Finish Editing Box" : "Edit This Box")) + { + _upstreamEditor._editingBoxPropPath = editingCurrent ? null : property.propertyPath; + SceneView.RepaintAll(); // to show/hide gizmo + } + } + else { - EditingBoxPropPath = editingCurrent ? null : boxProp.propertyPath; - SceneView.RepaintAll(); // to show/hide gizmo + using (new EditorGUI.DisabledScope(true)) + GUI.Button(position, "Cannot edit in this context"); } + position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; } @@ -86,11 +97,13 @@ protected override bool Field(Rect position, GUIContent label, SerializedPropert position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; EditorGUI.PropertyField(position, sizeProp); position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; - if (!_eulerAngle.TryGetValue(boxProp.propertyPath, out var eulerCache) - || eulerCache.value != rotationProp.quaternionValue) + + if (!_upstreamEditor || + !_upstreamEditor._eulerAngles.TryGetValue(property.propertyPath, out var eulerCache) || + eulerCache.value != rotationProp.quaternionValue) { - _eulerAngle[boxProp.propertyPath] = eulerCache = (rotationProp.quaternionValue, - rotationProp.quaternionValue.eulerAngles); + eulerCache = (value: rotationProp.quaternionValue, + euler: rotationProp.quaternionValue.eulerAngles); } // rotation in euler @@ -102,37 +115,22 @@ protected override bool Field(Rect position, GUIContent label, SerializedPropert { var quot = Quaternion.Euler(euler); rotationProp.quaternionValue = quot; - _eulerAngle[boxProp.propertyPath] = (quot, euler); + eulerCache = (quot, euler); } + + if (_upstreamEditor) + _upstreamEditor._eulerAngles[property.propertyPath] = eulerCache; + EditorGUI.EndProperty(); position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; } - - return removeElement; - } - - protected override void InitializeNewElement(IElement element) - { - var box = element.ValueProperty; - box.FindPropertyRelative("center").vector3Value = Vector3.zero; - box.FindPropertyRelative("size").vector3Value = Vector3.one; - box.FindPropertyRelative("rotation").quaternionValue = Quaternion.identity; - EditingBoxPropPath = element.ValueProperty.propertyPath; - } - - static class EditorStatics - { - public static readonly GUIContent RemoveButton = new GUIContent("x") - { - tooltip = "Remove Element from the list." - }; } } private void OnSceneGUI() { - if (_boxList.EditingBoxPropPath == null) return; - var box = serializedObject.FindProperty(_boxList.EditingBoxPropPath); + if (_editingBoxPropPath == null) return; + var box = serializedObject.FindProperty(_editingBoxPropPath); if (box == null) return; var centerProp = box.FindPropertyRelative("center"); @@ -195,7 +193,7 @@ public static void DrawGizmoActive(RemoveMeshInBox script, GizmoType gizmoType) Handles.matrix = script.transform.localToWorldMatrix; Handles.color = Color.red; - foreach (var boundingBox in script.boxList.GetAsList()) + foreach (var boundingBox in script.boxes) { var halfSize = boundingBox.size / 2; var x = boundingBox.rotation * new Vector3(halfSize.x, 0, 0); diff --git a/Editor/com.anatawa12.avatar-optimizer.editor.asmdef b/Editor/com.anatawa12.avatar-optimizer.editor.asmdef index ca39d04d1..8840ed95d 100644 --- a/Editor/com.anatawa12.avatar-optimizer.editor.asmdef +++ b/Editor/com.anatawa12.avatar-optimizer.editor.asmdef @@ -6,9 +6,7 @@ "GUID:f69eeb3e25674f4a9bd20e6d7e69e0e6", "GUID:2633ab9fa94544a69517fc9a1bc143c9", "GUID:b9880ca0b6584453a2627bd3c038759f", - "GUID:8542dbf824204440a818dbc2377cb4d6", - "GUID:e3dc35e4a606c438485f5fb532cb875d", - "GUID:c85616d79bd2a41109c251ab53d14c20" + "GUID:8542dbf824204440a818dbc2377cb4d6" ], "includePlatforms": [ "Editor" diff --git a/Internal/PrefabSafeList.meta b/Internal/PrefabSafeList.meta deleted file mode 100644 index 5b0834e6b..000000000 --- a/Internal/PrefabSafeList.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 62b5deccaf1604d7da5bee6977e054d9 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Internal/PrefabSafeList/Editor.meta b/Internal/PrefabSafeList/Editor.meta deleted file mode 100644 index 45c87c44f..000000000 --- a/Internal/PrefabSafeList/Editor.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: b0249c7c67a8c47adb220315f1490418 -timeCreated: 1676295508 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/EditorUtil.PrefabModification.cs b/Internal/PrefabSafeList/Editor/EditorUtil.PrefabModification.cs deleted file mode 100644 index 6dabc2a3e..000000000 --- a/Internal/PrefabSafeList/Editor/EditorUtil.PrefabModification.cs +++ /dev/null @@ -1,306 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using JetBrains.Annotations; -using UnityEditor; -using UnityEngine.Assertions; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - public abstract partial class EditorUtil - { - private struct ArraySizeCheck - { - public int Size { get; private set; } - [CanBeNull] private readonly SerializedProperty _prop; - - public ArraySizeCheck([CanBeNull] SerializedProperty prop) - { - _prop = prop; - Size = 0; - } - - public bool Changed => Size != (_prop?.intValue ?? 0); - - public void Update() => Size = (_prop?.intValue ?? 0); - } - - private sealed class PrefabModification : EditorUtil - { - private readonly List _elements; - private readonly SerializedProperty _rootProperty; - - private readonly SerializedProperty _firstLayerProp; - [ItemCanBeNull] private readonly SerializedProperty[] _layerElementsProps; - - private readonly int _nestCount; - private readonly SerializedProperty _prefabLayers; - - // upstream change check - private ArraySizeCheck _prefabLayersCheck; - private ArraySizeCheck _firstLayerCheck; - private readonly ArraySizeCheck[] _layerChecks; - - public PrefabModification(SerializedProperty property, int nestCount) - { - _elements = new List(); - _rootProperty = property; - - ClearNonLayerModifications(property, nestCount); - - _firstLayerProp = property.FindPropertyRelative(nameof(Names.firstLayer)); - _firstLayerCheck = new ArraySizeCheck(_firstLayerProp.FindPropertyRelative("Array.size")); - _layerChecks = new ArraySizeCheck[nestCount]; - _layerElementsProps = new SerializedProperty[nestCount]; - - _nestCount = nestCount; - - // apply modifications until previous one - _prefabLayers = property.FindPropertyRelative(nameof(Names.prefabLayers)); - _prefabLayersCheck = new ArraySizeCheck(_prefabLayers.FindPropertyRelative("Array.size")); - InitCurrentLayer(); - } - - private void ClearNonLayerModifications(SerializedProperty property, int nestCount) - { - // TODO - } - - public override IReadOnlyList Elements - { - get - { - Initialize(); - return _elements; - } - } - - public override int ElementsCount - { - get - { - Initialize(); - return _elements.Count; - } - } - - /// - /// initialize or update cache info if needed - /// - public void Initialize() - { - if (_prefabLayersCheck.Changed) - InitCurrentLayer(); - if (_firstLayerCheck.Changed || _layerChecks.Any(x => x.Changed)) - { - DoInitialize(); - } - } - - private void InitCurrentLayer(bool force = false) - { - if (_prefabLayers.arraySize < _nestCount && force) - _prefabLayers.arraySize = _nestCount; - - if (_prefabLayersCheck.Size < _prefabLayers.arraySize) - { - for (var i = _prefabLayersCheck.Size; i < _prefabLayers.arraySize; i++) - { - var elements = _prefabLayers.GetArrayElementAtIndex(i) - .FindPropertyRelative(nameof(Names.Layer.elements)); - - _layerElementsProps[i] = elements; - _layerChecks[i] = new ArraySizeCheck(elements.FindPropertyRelative("Array.size")); - } - } - else if (_prefabLayersCheck.Size > _prefabLayers.arraySize) - { - for (var i = _prefabLayers.arraySize; i < _prefabLayersCheck.Size; i++) - { - _layerElementsProps[i] = null; - _layerChecks[i] = new ArraySizeCheck(null); - } - } - _prefabLayersCheck.Update(); - - DoInitialize(); - } - - /// - /// initialize or update cache info - /// - public void DoInitialize() - { - var offset = 0; - - DoInitializeLayer(offset, _firstLayerCheck.Size, _firstLayerProp, false); - offset += _firstLayerProp.arraySize; - _firstLayerCheck.Update(); - - for (var i = 0; i < _layerElementsProps.Length; i++) - { - var layerElementsProp = _layerElementsProps[i]; - if (layerElementsProp == null) continue; - DoInitializeLayer(offset, _layerChecks[i].Size, layerElementsProp, - i == _layerElementsProps.Length - 1); - offset += layerElementsProp.arraySize; - _layerChecks[i].Update(); - } - } - - void DoInitializeLayer(int offset, int prevSize, SerializedProperty layerElements, bool original) - { -#if UNITY_ASSERTIONS - for (int i = 0, j = offset; i < prevSize && i < layerElements.arraySize && j < _elements.Count; i++, j++) - { - Assert.IsTrue(SerializedProperty.EqualContents(_elements[j].ContainerProp, layerElements.GetArrayElementAtIndex(i))); - } -#endif - if (prevSize < layerElements.arraySize) - { - // prev size is too small: add more - var addingElements = Enumerable.Range(prevSize, layerElements.arraySize - prevSize) - .Select(index => - new ElementImpl(this, layerElements.GetArrayElementAtIndex(index), index, original)) - .ToArray(); - - _elements.InsertRange(offset + prevSize, addingElements); - } - else if (prevSize > layerElements.arraySize) - { - // too big: remove elements - _elements.RemoveRange(offset + layerElements.arraySize, - prevSize - layerElements.arraySize); - } - } - - public override IElement AddElement() - { - InitCurrentLayer(true); - var layerIndex = _layerElementsProps.Length - 1; - var lastElementsProp = _layerElementsProps[layerIndex]; - Debug.Assert(lastElementsProp != null, nameof(lastElementsProp) + " != null"); - var addedElement = AddArrayElement(lastElementsProp); - var element = new ElementImpl(this, addedElement, lastElementsProp.arraySize - 1, true); - _elements.Add(element); - _layerChecks[layerIndex].Update(); - return element; - } - - public override void Clear() - { - Initialize(); - for (var i = _elements.Count - 1; i >= 0; i--) - _elements[i].Remove(); - } - - private class ElementImpl : IElement - { - public EditorUtil Container => _container; - private readonly PrefabModification _container; - internal readonly SerializedProperty ContainerProp; - - private PropStatus _status; - private readonly int _index; - - public ElementStatus Status - { - get - { - switch (_status) - { - case PropStatus.Inherited: - return RemovedProperty.boolValue ? ElementStatus.RemovedUpper : ElementStatus.AddedUpper; - case PropStatus.Original: - return ElementStatus.NewElement; - case PropStatus.Invalid: - return ElementStatus.Invalid; - default: - throw new ArgumentOutOfRangeException(); - } - } - } - - public SerializedProperty ValueProperty { get; } - public SerializedProperty RemovedProperty { get; } - - public bool Contains - { - get - { - switch (Status) - { - case ElementStatus.AddedUpper: - case ElementStatus.NewElement: - return true; - case ElementStatus.Invalid: - case ElementStatus.RemovedUpper: - return false; - default: - throw new ArgumentOutOfRangeException(); - } - } - } - - public ElementImpl(PrefabModification container, SerializedProperty containerProp, int index, - bool original) - { - _container = container; - ContainerProp = containerProp; - _index = index; - _status = original ? PropStatus.Original : PropStatus.Inherited; - - ValueProperty = ContainerProp.FindPropertyRelative(nameof(Names.Container.value)); - RemovedProperty = ContainerProp.FindPropertyRelative(nameof(Names.Container.removed)); - } - - private enum PropStatus - { - Inherited, - Original, - Invalid, - } - - public void Add() - { - switch (_status) - { - case PropStatus.Inherited: - RemovedProperty.boolValue = false; - break; - case PropStatus.Original: - // original means exists. - break; - case PropStatus.Invalid: - throw new InvalidOperationException(ElementErrMsg.AddOnInvalid); - default: - throw new ArgumentOutOfRangeException(); - } - } - - public void Remove() - { - switch (_status) - { - case PropStatus.Inherited: - RemovedProperty.boolValue = true; - break; - case PropStatus.Original: - var path = ContainerProp.propertyPath; - path = path.Substring(0, path.LastIndexOf(".Array.data[", StringComparison.Ordinal)); - var arrayProp = ContainerProp.serializedObject.FindProperty(path); - arrayProp.DeleteArrayElementAtIndex(_index); - _status = PropStatus.Invalid; - break; - case PropStatus.Invalid: - // invalid means removed - break; - default: - throw new ArgumentOutOfRangeException(); - } - } - } - } - } -} diff --git a/Internal/PrefabSafeList/Editor/EditorUtil.PrefabModification.cs.meta b/Internal/PrefabSafeList/Editor/EditorUtil.PrefabModification.cs.meta deleted file mode 100644 index 353468b10..000000000 --- a/Internal/PrefabSafeList/Editor/EditorUtil.PrefabModification.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 9e5399ca0d8614fa1b9bb998fc0b6ab7 -timeCreated: 1677672644 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/EditorUtil.Root.cs b/Internal/PrefabSafeList/Editor/EditorUtil.Root.cs deleted file mode 100644 index 4def9d086..000000000 --- a/Internal/PrefabSafeList/Editor/EditorUtil.Root.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using JetBrains.Annotations; -using UnityEditor; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - public abstract partial class EditorUtil - { - private sealed class Root : EditorUtil - { - private List _elements; - [NotNull] private readonly SerializedProperty _firstLayer; - public override int Count => _firstLayer.arraySize; - - public Root(SerializedProperty property) - { - _firstLayer = property.FindPropertyRelative(nameof(Names.firstLayer)) - ?? throw new ArgumentException(nameof(Names.firstLayer) + "not found", nameof(property)); - } - - public override IReadOnlyList Elements - { - get - { - Initialize(); - return _elements; - } - } - - private void Initialize() - { - if (_elements?.Count != _firstLayer.arraySize) - _elements = new ArrayPropertyEnumerable(_firstLayer) - .Select((x, i) => new ElementImpl(this, x, i)) - .ToList(); - } - - public override int ElementsCount => _firstLayer.arraySize; - - public override void Clear() => _firstLayer.arraySize = 0; - - public override IElement AddElement() - { - Initialize(); - var addedElement = AddArrayElement(_firstLayer); - var element = new ElementImpl(this, addedElement, _firstLayer.arraySize - 1); - _elements.Add(element); - return element; - } - - private class ElementImpl : IElement - { - public EditorUtil Container => _container; - public ElementStatus Status => Contains ? ElementStatus.NewElement : ElementStatus.Invalid; - public SerializedProperty RemovedProperty => null; - public bool Contains => _index >= 0; - - public SerializedProperty ValueProperty => - _containerProp?.FindPropertyRelative(nameof(Names.Container.value)); - - private SerializedProperty _containerProp; - private readonly Root _container; - private int _index; - - public ElementImpl(Root container, SerializedProperty prop, int index) - { - _container = container; - _index = index; - _containerProp = prop; - } - - public void Add() - { - if (Contains) return; - throw new InvalidOperationException(ElementErrMsg.AddOnInvalid); - } - - public void Remove() - { - if (!Contains) return; - _container._firstLayer.DeleteArrayElementAtIndex(_index); - _index = -1; - _containerProp = null; - _container._elements.Remove(this); - } - } - } - } -} diff --git a/Internal/PrefabSafeList/Editor/EditorUtil.Root.cs.meta b/Internal/PrefabSafeList/Editor/EditorUtil.Root.cs.meta deleted file mode 100644 index 55489d14a..000000000 --- a/Internal/PrefabSafeList/Editor/EditorUtil.Root.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 34edae155e4ce4a5d81387cba9670cc4 -timeCreated: 1677672557 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/EditorUtil.cs b/Internal/PrefabSafeList/Editor/EditorUtil.cs deleted file mode 100644 index 2e0683c63..000000000 --- a/Internal/PrefabSafeList/Editor/EditorUtil.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using JetBrains.Annotations; -using UnityEditor; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - /// - /// Utility to edit PrefabSafeList in CustomEditor with SerializedProperty - /// - public abstract partial class EditorUtil - { - public abstract IReadOnlyList Elements { get; } - public abstract int ElementsCount { get; } - public virtual int Count => Elements.Count(x => x.Contains); - - public static EditorUtil Create(SerializedProperty property, int nestCount) - { - if (nestCount == 0) - return new Root(property); - return new PrefabModification(property, nestCount); - } - - private EditorUtil() - { - } - - public abstract void Clear(); - - public abstract IElement AddElement(); - - private static SerializedProperty AddArrayElement([NotNull] SerializedProperty array) - { - array.arraySize += 1; - return array.GetArrayElementAtIndex(array.arraySize - 1); - } - } - - internal static class ElementErrMsg - { - internal const string AddOnInvalid = "Add is called for Invalid element"; - } - - public interface IElement - { - EditorUtil Container { get; } - ElementStatus Status { get; } - SerializedProperty ValueProperty { get; } - SerializedProperty RemovedProperty { get; } - bool Contains { get; } - void Add(); - void Remove(); - } - - public enum ElementStatus - { - Invalid = -1, - - AddedUpper, - RemovedUpper, - NewElement, - } -} diff --git a/Internal/PrefabSafeList/Editor/EditorUtil.cs.meta b/Internal/PrefabSafeList/Editor/EditorUtil.cs.meta deleted file mode 100644 index e2c981b9c..000000000 --- a/Internal/PrefabSafeList/Editor/EditorUtil.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 4332c2a2e321742859eac52cbf3acc43 -timeCreated: 1677668779 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/OnBeforeSerializeImpl.cs b/Internal/PrefabSafeList/Editor/OnBeforeSerializeImpl.cs deleted file mode 100644 index 10180fae3..000000000 --- a/Internal/PrefabSafeList/Editor/OnBeforeSerializeImpl.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using JetBrains.Annotations; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - [UsedImplicitly] // used by reflection - internal static class OnBeforeSerializeImpl - where TLayer : PrefabLayer, new() - where TContainer : ValueContainer, new() - { - [UsedImplicitly] // used by reflection - public static void Impl(PrefabSafeList self) - { - // fakeSlot must not be modified, - if (self.OuterObject == null) return; - - // match prefabLayers count. - var nestCount = PrefabSafeListUtil.PrefabNestCount(self.OuterObject); - - if (self.prefabLayers.Length < nestCount) - { - // https://github.com/anatawa12/AvatarOptimizer/issues/52 - // to avoid unnecessary modifications, resize is not performed later. - } - else if (self.prefabLayers.Length > nestCount) - ApplyModificationsToLatestLayer(self, nestCount); - - RemoveCheckCheck(self, nestCount); - } - - private static void RemoveCheckCheck(PrefabSafeList self, int nestCount) - { - if (nestCount == 0) - { - self.firstLayer = self.GetAsList().Select(x => new TContainer { value = x }).ToArray(); - } - else - { - if (nestCount < self.prefabLayers.Length) - { - var currentLayer = self.prefabLayers[nestCount - 1] ?? - (self.prefabLayers[nestCount - 1] = new TLayer()); - - var list = new List(); - currentLayer.ApplyTo(list); - currentLayer.elements = list.Select(x => new TContainer { value = x }).ToArray(); - } - } - } - - private static void ApplyModificationsToLatestLayer(PrefabSafeList self, int nestCount) - { - // after apply modifications?: apply to latest layer - if (nestCount == 0) - { - self.firstLayer = self.GetAsList().Select(x => new TContainer { value = x }).ToArray(); - self.prefabLayers = Array.Empty(); - } - else - { - // nestCount is not zero: apply to current layer - - var list = new List(); - foreach (var layer in self.prefabLayers.Skip(nestCount - 1)) - layer.ApplyTo(list); - - var currentLayer = self.prefabLayers[nestCount - 1] ?? (self.prefabLayers[nestCount - 1] = new TLayer()); - currentLayer.elements = list.Select(x => new TContainer { value = x }).ToArray(); - // resize layers array. - self.prefabLayers = PrefabSafeListRuntimeUtil.ResizeArray(self.prefabLayers, nestCount); - } - } - } -} diff --git a/Internal/PrefabSafeList/Editor/OnBeforeSerializeImpl.cs.meta b/Internal/PrefabSafeList/Editor/OnBeforeSerializeImpl.cs.meta deleted file mode 100644 index ca049a951..000000000 --- a/Internal/PrefabSafeList/Editor/OnBeforeSerializeImpl.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 94d9fa02d91744ae49dc20e33bccd406 -timeCreated: 1677667998 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/PrefabSafeListEditor.cs b/Internal/PrefabSafeList/Editor/PrefabSafeListEditor.cs deleted file mode 100644 index 6f7df54e3..000000000 --- a/Internal/PrefabSafeList/Editor/PrefabSafeListEditor.cs +++ /dev/null @@ -1,373 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using JetBrains.Annotations; -using UnityEditor; -using UnityEngine; -using Debug = System.Diagnostics.Debug; -using Object = UnityEngine.Object; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - /// - /// because there are many generic arguments, nameof is too long. so I use this class - /// - internal class Names : PrefabSafeList - { - internal class Container : ValueContainer - { - } - - internal class Layer : PrefabLayer - { - } - - protected Names(Object outerObject) : base(outerObject) - { - } - } - -#if true - internal static class EditorStatics - { - public static readonly GUIContent MultiEditingNotSupported = new GUIContent("Multi editing not supported"); - public static readonly GUIContent UnsupportedType = new GUIContent("Element type is not supported"); - public static readonly GUIContent AddNotSupported = new GUIContent("Add Not Supported"); - - public static readonly GUIContent ToAdd = new GUIContent("Element to add") - { - tooltip = "Drag & Drop value to here to add element to this set." - }; - - public static readonly GUIContent RemoveButton = new GUIContent("x") - { - tooltip = "Remove Element from the list." - }; - - public static readonly GUIContent Removed = new GUIContent("(Removed)"); - public static readonly GUIContent AddElement = new GUIContent("AddElement"); - } - - [CustomPropertyDrawer(typeof(PrefabSafeList<,,>), true)] - internal class ObjectsEditor : PropertyDrawer - { - private int _nestCountCache = -1; - - private int GetNestCount(Object obj) => - _nestCountCache != -1 ? _nestCountCache : _nestCountCache = PrefabSafeListUtil.PrefabNestCount(obj); - - private readonly Dictionary _caches = - new Dictionary(); - - [CanBeNull] - private Editor GetCache(SerializedProperty property) - { - if (!_caches.TryGetValue(property.propertyPath, out var cached)) - { - _caches[property.propertyPath] = cached = - new Editor(property, GetNestCount(property.serializedObject.targetObject)); - } - - return cached; - } - - private class Editor : EditorBase - { - public Editor(SerializedProperty property, int nestCount) : base(property, nestCount) - { - } - } - - public override float GetPropertyHeight(SerializedProperty property, GUIContent label) - { - if (property.serializedObject.isEditingMultipleObjects || !property.isExpanded) - return EditorGUIUtility.singleLineHeight; - var cache = GetCache(property); - if (cache == null) - return EditorGUIUtility.singleLineHeight; - - return cache.GetPropertyHeight() + EditorGUIUtility.singleLineHeight + - EditorGUIUtility.standardVerticalSpacing; - } - - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) - { - if (property.serializedObject.isEditingMultipleObjects) - { - EditorGUI.LabelField(position, label, EditorStatics.MultiEditingNotSupported); - return; - } - - var cache = GetCache(property); - if (cache == null) - { - EditorGUI.LabelField(position, label, EditorStatics.UnsupportedType); - return; - } - - position.height = EditorGUIUtility.singleLineHeight; - - property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, label); - - if (property.isExpanded) - { - position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; - cache.OnGUI(position); - } - } - } - - public abstract class EditorBase - { - public readonly EditorUtil EditorUtil; - - public EditorBase(SerializedProperty property, int nestCount) - { - if (property.serializedObject.isEditingMultipleObjects) - throw new ArgumentException("multi editing not supported", nameof(property)); - EditorUtil = EditorUtil.Create(property, nestCount); - } - - public float GetPropertyHeight() => - EditorUtil.Elements.Sum(x => FieldHeightOf(x) + EditorGUIUtility.standardVerticalSpacing) - + GetAddRegionSize(); // add region - - private float FieldHeightOf(IElement element) => - !element.Contains ? EditorGUIUtility.singleLineHeight : FieldHeight(element.ValueProperty); - - // position is - public void OnGUI(Rect position) - { - var elementI = 0; - var newLabel = new GUIContent(""); - - // to avoid changes in for loop - Action action = null; - - var indentLevel = EditorGUI.indentLevel; - EditorGUI.indentLevel += 1; - - foreach (var element in EditorUtil.Elements) - { - newLabel.text = $"Element {elementI++}"; - if (!element.Contains) - { - position.height = EditorGUIUtility.singleLineHeight; - EditorGUI.BeginProperty(position, newLabel, element.RemovedProperty); - EditorGUI.LabelField(position, newLabel, EditorStatics.Removed); - EditorGUI.EndProperty(); - // TODO: restore in context menu - } - else - { - position.height = FieldHeight(element.ValueProperty); - EditorGUI.BeginProperty(position, newLabel, element.ValueProperty); - if (Field(position, newLabel, element.ValueProperty.Copy())) - action = element.Remove; - EditorGUI.EndProperty(); - } - - position.y += position.height + EditorGUIUtility.standardVerticalSpacing; - } - - action?.Invoke(); - - position.height = EditorGUIUtility.singleLineHeight; - - OnGUIAddRegion(position); - EditorGUI.indentLevel = indentLevel; - } - - protected virtual float GetAddRegionSize() => EditorGUIUtility.singleLineHeight; - - protected virtual void OnGUIAddRegion(Rect position) - { - if (GUI.Button(position, EditorStatics.AddElement)) - { - InitializeNewElement(EditorUtil.AddElement()); - } - } - - protected virtual void InitializeNewElement(IElement element) - { - } - - protected virtual float FieldHeight(SerializedProperty serializedProperty) - { - if (HasVisibleChildFields(serializedProperty) && Reflection.HasPropertyDrawer(serializedProperty)) - return EditorGUI.GetPropertyHeight(serializedProperty) - + EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; // remove button - else - return EditorGUI.GetPropertyHeight(serializedProperty); - } - - /// - /// - /// - /// - /// - /// - /// True if removing the value is requested - protected virtual bool Field(Rect position, GUIContent label, SerializedProperty serializedProperty) - { - var removeElement = false; - if (HasVisibleChildFields(serializedProperty)) - { - if (Reflection.HasPropertyDrawer(serializedProperty)) - { - var prevHeight = position.height; - position.height = EditorGUIUtility.singleLineHeight; - - if (GUI.Button(position, EditorStatics.RemoveButton)) - removeElement = true; - - position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; - position.height = prevHeight - EditorGUIUtility.standardVerticalSpacing + - EditorGUIUtility.singleLineHeight; - - EditorGUI.PropertyField(position, serializedProperty, label); - } - else - { - removeElement = FieldForFieldWithoutPropertyDrawer(position, label, serializedProperty); - } - } - else - { - position.width -= EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; - EditorGUI.PropertyField(position, serializedProperty, label); - position.x += position.width + EditorGUIUtility.standardVerticalSpacing; - position.width = EditorGUIUtility.singleLineHeight; - - if (GUI.Button(position, EditorStatics.RemoveButton)) - removeElement = true; - } - - return removeElement; - } - - internal static bool HasVisibleChildFields(SerializedProperty property) - { - switch (property.propertyType) - { - case SerializedPropertyType.Vector2: - case SerializedPropertyType.Vector3: - case SerializedPropertyType.Rect: - case SerializedPropertyType.Bounds: - case SerializedPropertyType.Vector2Int: - case SerializedPropertyType.Vector3Int: - case SerializedPropertyType.RectInt: - case SerializedPropertyType.BoundsInt: - return false; - default: - return property.hasVisibleChildren; - } - } - - // The PropertyDrawer for property without propertyDrawer - private static bool FieldForFieldWithoutPropertyDrawer(Rect position, GUIContent label, - SerializedProperty property) - { - var doRemove = false; - - var enabled = GUI.enabled; - var indentLevel = EditorGUI.indentLevel; - var num = indentLevel - property.depth; - - var serializedProperty = property.Copy(); - position.height = EditorGUI.GetPropertyHeight(serializedProperty.propertyType, label); - EditorGUI.indentLevel = serializedProperty.depth + num; - - bool enterChildren; - { - var prevWidth = position.width; - position.width -= EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; - using (new EditorGUI.DisabledScope(!property.editable)) - { - var style = DragAndDrop.activeControlID == -10 ? EditorStyles.foldoutPreDrop : EditorStyles.foldout; - enterChildren = EditorGUI.Foldout(position, property.isExpanded, label, true, style); - } - - if (enterChildren != property.isExpanded) - { - if (Event.current.alt) - SetExpandedRecurse(property, enterChildren); - else - property.isExpanded = enterChildren; - } - - position.x += position.width + EditorGUIUtility.standardVerticalSpacing; - position.width = EditorGUIUtility.singleLineHeight; - - if (GUI.Button(position, EditorStatics.RemoveButton)) - doRemove = true; - - position.width = prevWidth; - position.x -= prevWidth - - (EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight); - } - - position.y += position.height + EditorGUIUtility.standardVerticalSpacing; - - if (enterChildren) - { - var endProperty = serializedProperty.GetEndProperty(); - while (serializedProperty.NextVisible(enterChildren) && - !SerializedProperty.EqualContents(serializedProperty, endProperty)) - { - EditorGUI.indentLevel = serializedProperty.depth + num; - position.height = EditorGUI.GetPropertyHeight(serializedProperty, null, false); - EditorGUI.BeginChangeCheck(); - enterChildren = EditorGUI.PropertyField(position, serializedProperty, null, false) && - HasVisibleChildFields(serializedProperty); - if (EditorGUI.EndChangeCheck()) - break; - position.y += position.height + EditorGUIUtility.standardVerticalSpacing; - } - } - - GUI.enabled = enabled; - EditorGUI.indentLevel = indentLevel; - - return doRemove; - } - - private static void SetExpandedRecurse(SerializedProperty property, bool expanded) - { - var serializedProperty = property.Copy(); - serializedProperty.isExpanded = expanded; - var depth = serializedProperty.depth; - while (serializedProperty.NextVisible(true) && serializedProperty.depth > depth) - if (serializedProperty.hasVisibleChildren) - serializedProperty.isExpanded = expanded; - } - - private static class Reflection - { - static Reflection() - { - GetHandler = typeof(EditorGUI).Assembly - .GetType("UnityEditor.ScriptAttributeUtility") - .GetMethod("GetHandler", BindingFlags.Static | BindingFlags.NonPublic, null, - new[] { typeof(SerializedProperty) }, null); - var property = typeof(EditorGUI).Assembly - .GetType("UnityEditor.PropertyHandler") - .GetProperty("hasPropertyDrawer", - BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - Debug.Assert(property != null, nameof(property) + " != null"); - GetHasPropertyDrawer = property.GetMethod; - } - - private static readonly MethodInfo GetHandler; - private static readonly MethodInfo GetHasPropertyDrawer; - - public static bool HasPropertyDrawer(SerializedProperty prop) - { - return (bool)GetHasPropertyDrawer.Invoke(GetHandler.Invoke(null, new object[]{prop}), - Array.Empty()); - } - } - } -#endif -} diff --git a/Internal/PrefabSafeList/Editor/PrefabSafeListEditor.cs.meta b/Internal/PrefabSafeList/Editor/PrefabSafeListEditor.cs.meta deleted file mode 100644 index a837f5058..000000000 --- a/Internal/PrefabSafeList/Editor/PrefabSafeListEditor.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: bf7f038b3f8a74197b04ed0daf6e019d -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Internal/PrefabSafeList/Editor/Utils.cs b/Internal/PrefabSafeList/Editor/Utils.cs deleted file mode 100644 index 7631a9df0..000000000 --- a/Internal/PrefabSafeList/Editor/Utils.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using UnityEditor; -using Object = UnityEngine.Object; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - public static class PrefabSafeListUtil - { - public static int PrefabNestCount(Object instance) - { - var nestCount = 0; - while ((bool)(instance = PrefabUtility.GetCorrespondingObjectFromSource(instance))) - nestCount++; - - return nestCount; - } - - internal static bool IsNotNull(T arg) - { - if (arg == null) return false; - if (typeof(Object).IsAssignableFrom(typeof(T))) - return (Object)(object)arg; - return true; - } - } - - internal readonly struct ArrayPropertyEnumerable : IEnumerable - { - private readonly SerializedProperty _property; - private readonly int _begin; - private readonly int _end; - - public ArrayPropertyEnumerable(SerializedProperty property) - { - _property = property; - _begin = 0; - _end = property.arraySize; - } - - private ArrayPropertyEnumerable(SerializedProperty property, int begin, int end) - { - _property = property; - _begin = begin; - _end = end; - } - - public ArrayPropertyEnumerable Take(int count) => - new ArrayPropertyEnumerable(_property, _begin, Math.Min(_end, _begin + count)); - - public Enumerator GetEnumerator() => new Enumerator(this); - - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - public struct Enumerator : IEnumerator - { - private readonly SerializedProperty _property; - private int _index; - private int _size; - - public Enumerator(ArrayPropertyEnumerable enumerable) - { - _property = enumerable._property; - _index = enumerable._begin - 1; - _size = enumerable._end; - } - - public SerializedProperty Current => _property.GetArrayElementAtIndex(_index); - SerializedProperty IEnumerator.Current => Current; - object IEnumerator.Current => Current; - - public bool MoveNext() - { - _index++; - return _index < _size; - } - - public void Reset() => throw new NotSupportedException(); - - public void Dispose() - { - } - } - } -} diff --git a/Internal/PrefabSafeList/Editor/Utils.cs.meta b/Internal/PrefabSafeList/Editor/Utils.cs.meta deleted file mode 100644 index 2332d4eb7..000000000 --- a/Internal/PrefabSafeList/Editor/Utils.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 1b0d542aeed484dcd9d7ff3bbe715bf7 -timeCreated: 1677668127 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/asssembly-info.cs b/Internal/PrefabSafeList/Editor/asssembly-info.cs deleted file mode 100644 index e86dff41d..000000000 --- a/Internal/PrefabSafeList/Editor/asssembly-info.cs +++ /dev/null @@ -1,3 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly:InternalsVisibleTo("com.anatawa12.avatar-optimizer.test")] diff --git a/Internal/PrefabSafeList/Editor/asssembly-info.cs.meta b/Internal/PrefabSafeList/Editor/asssembly-info.cs.meta deleted file mode 100644 index 19ceee564..000000000 --- a/Internal/PrefabSafeList/Editor/asssembly-info.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: ad0f5d39df5d48e9be9bf8ad5f8baddd -timeCreated: 1677831964 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor.asmdef b/Internal/PrefabSafeList/Editor/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor.asmdef deleted file mode 100644 index 0acba0f95..000000000 --- a/Internal/PrefabSafeList/Editor/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor.asmdef +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor", - "references": [ - "GUID:c85616d79bd2a41109c251ab53d14c20" - ], - "includePlatforms": [ - "Editor" - ], - "excludePlatforms": [], - "allowUnsafeCode": false, - "overrideReferences": true, - "precompiledReferences": [], - "autoReferenced": false, - "defineConstraints": [], - "versionDefines": [], - "noEngineReferences": false -} \ No newline at end of file diff --git a/Internal/PrefabSafeList/Editor/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor.asmdef.meta b/Internal/PrefabSafeList/Editor/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor.asmdef.meta deleted file mode 100644 index 17367d866..000000000 --- a/Internal/PrefabSafeList/Editor/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor.asmdef.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: e3dc35e4a606c438485f5fb532cb875d -timeCreated: 1677662234 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime.meta b/Internal/PrefabSafeList/Runtime.meta deleted file mode 100644 index 320f12ce9..000000000 --- a/Internal/PrefabSafeList/Runtime.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 0d405fe0c7c9e48a59663514dde71261 -timeCreated: 1676295743 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/PrefabSafeList.cs b/Internal/PrefabSafeList/Runtime/PrefabSafeList.cs deleted file mode 100644 index f117d9bf9..000000000 --- a/Internal/PrefabSafeList/Runtime/PrefabSafeList.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using JetBrains.Annotations; -using UnityEngine; -using Object = UnityEngine.Object; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - internal static class PrefabSafeListRuntimeUtil - { - public static T[] ResizeArray(T[] source, int size) where T: new() - { - var result = new T[size]; - Array.Copy(source, result, Math.Min(size, source.Length)); - for (var i = source.Length; i < result.Length; i++) - result[i] = new T(); - return result; - } - -#if UNITY_EDITOR - private static readonly Type OnBeforeSerializeImplType; - - static PrefabSafeListRuntimeUtil() - { - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - OnBeforeSerializeImplType = - assembly.GetType("Anatawa12.AvatarOptimizer.PrefabSafeList.OnBeforeSerializeImpl`3"); - if (OnBeforeSerializeImplType != null) return; - } - } - - public static MethodInfo GetOnBeforeSerializeCallbackMethod( - Type tType, Type tLayerType, Type tContainerType, - Type setType) - { - var implType = OnBeforeSerializeImplType.MakeGenericType(tType, tLayerType, tContainerType); - return implType.GetMethod("Impl", BindingFlags.Public | BindingFlags.Static, null, new[] { setType }, null); - } -#endif - } - - - /// - /// The serializable class to express list. - /// using array will make prefab modifications too big so I made this class - /// - /// Element Type - /// Layer Type - /// Container Type - [Serializable] - public class PrefabSafeList : ISerializationCallbackReceiver - where TLayer : PrefabLayer, new() - where TContainer : ValueContainer, new() - { - [SerializeField] internal TContainer[] firstLayer = Array.Empty(); - [SerializeField] internal TLayer[] prefabLayers = Array.Empty(); - -#if UNITY_EDITOR - internal readonly Object OuterObject; - private static MethodInfo _onBeforeSerializeCallback = PrefabSafeListRuntimeUtil - .GetOnBeforeSerializeCallbackMethod(typeof(T), typeof(TLayer), typeof(TContainer), - typeof(PrefabSafeList)); -#endif - - protected PrefabSafeList(Object outerObject) - { -#if UNITY_EDITOR - if (!outerObject) throw new ArgumentNullException(nameof(outerObject)); - OuterObject = outerObject; -#endif - } - - public List GetAsList() - { - var result = new List(); - foreach (var element in firstLayer) - element.ApplyTo(result); - foreach (var layer in prefabLayers) - layer.ApplyTo(result); - return result; - } - - public void OnBeforeSerialize() - { -#if UNITY_EDITOR - _onBeforeSerializeCallback.Invoke(null, new object[] {this}); -#endif - } - - public void OnAfterDeserialize() - { - // there's nothing to do after deserialization. - } - } - - [Serializable] - public class PrefabLayer where TContainer : ValueContainer - { - // if some value is in both removes and additions, the values should be added - [SerializeField] internal TContainer[] elements = Array.Empty(); - - public void ApplyTo([NotNull] List list) - { - foreach (var element in elements) - element.ApplyTo(list); - } - } - - [Serializable] - public class ValueContainer - { - // if some value is in both removes and additions, the values should be added - [SerializeField] internal T value; - [SerializeField] internal bool removed; - - public void ApplyTo([NotNull] List list) - { - if (!removed) list.Add(value); - } - } - - internal readonly struct ListSet - { - [NotNull] private readonly List _list; - [NotNull] private readonly HashSet _set; - public ListSet(T[] initialize) - { - _list = new List(initialize); - _set = new HashSet(initialize); - } - - public void AddRange(IEnumerable values) - { - foreach (var value in values) - if (value != null && _set.Add(value)) - _list?.Add(value); - } - - public void RemoveRange(IEnumerable values) - { - foreach (var value in values) - if (value != null && _set.Remove(value)) - _list?.Remove(value); - } - - public T[] ToArray() => _list.ToArray(); - } -} diff --git a/Internal/PrefabSafeList/Runtime/PrefabSafeList.cs.meta b/Internal/PrefabSafeList/Runtime/PrefabSafeList.cs.meta deleted file mode 100644 index 72bf5106a..000000000 --- a/Internal/PrefabSafeList/Runtime/PrefabSafeList.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: be2947ea4faff4b2b947314c63fdc7c3 -timeCreated: 1676198844 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/PrefabSafeListTestComponent.cs b/Internal/PrefabSafeList/Runtime/PrefabSafeListTestComponent.cs deleted file mode 100644 index b4fb45052..000000000 --- a/Internal/PrefabSafeList/Runtime/PrefabSafeListTestComponent.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using UnityEngine; -using Object = UnityEngine.Object; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - public class PrefabSafeListTestComponent : MonoBehaviour - { - [SerializeField] internal MaterialsSet materials; - [SerializeField] internal ComplexSet complex; - - public PrefabSafeListTestComponent() - { - materials = new MaterialsSet(this); - } - - [Serializable] - public class Complex - { - public string value; - public Vector3 zero; - } - - [Serializable] - internal class MaterialsSet : PrefabSafeList.PrefabSafeList - { - public MaterialsSet(Object outerObject) : base(outerObject) - { - } - } - - [Serializable] - internal class MaterialsLayer : PrefabSafeList.PrefabLayer - { - } - - [Serializable] - internal class MaterialContainer : PrefabSafeList.ValueContainer - { - } - - [Serializable] - internal class ComplexSet : PrefabSafeList.PrefabSafeList - { - public ComplexSet(Object outerObject) : base(outerObject) - { - } - - [Serializable] - internal class Layer : PrefabSafeList.PrefabLayer - { - } - - [Serializable] - internal class Container : PrefabSafeList.ValueContainer - { - } - } - - } -} diff --git a/Internal/PrefabSafeList/Runtime/PrefabSafeListTestComponent.cs.meta b/Internal/PrefabSafeList/Runtime/PrefabSafeListTestComponent.cs.meta deleted file mode 100644 index d176e9e29..000000000 --- a/Internal/PrefabSafeList/Runtime/PrefabSafeListTestComponent.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: dc43081261ac84f418516360d38f754b -timeCreated: 1676216044 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/assembly-info.cs b/Internal/PrefabSafeList/Runtime/assembly-info.cs deleted file mode 100644 index d840d606a..000000000 --- a/Internal/PrefabSafeList/Runtime/assembly-info.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly:InternalsVisibleTo("com.anatawa12.avatar-optimizer.internal.prefab-safe-list.editor")] -[assembly:InternalsVisibleTo("com.anatawa12.avatar-optimizer.test")] diff --git a/Internal/PrefabSafeList/Runtime/assembly-info.cs.meta b/Internal/PrefabSafeList/Runtime/assembly-info.cs.meta deleted file mode 100644 index 753162166..000000000 --- a/Internal/PrefabSafeList/Runtime/assembly-info.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: bc3cd84bf52664a5eae266ac39b5be93 -timeCreated: 1677663513 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.asmdef b/Internal/PrefabSafeList/Runtime/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.asmdef deleted file mode 100644 index 505453025..000000000 --- a/Internal/PrefabSafeList/Runtime/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.asmdef +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "com.anatawa12.avatar-optimizer.internal.prefab-safe-list", - "references": [], - "includePlatforms": [], - "excludePlatforms": [], - "allowUnsafeCode": false, - "overrideReferences": true, - "precompiledReferences": [ - "VRC.Dynamics.dll" - ], - "autoReferenced": false, - "defineConstraints": [], - "versionDefines": [], - "noEngineReferences": false -} \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.asmdef.meta b/Internal/PrefabSafeList/Runtime/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.asmdef.meta deleted file mode 100644 index da663e701..000000000 --- a/Internal/PrefabSafeList/Runtime/com.anatawa12.avatar-optimizer.internal.prefab-safe-list.asmdef.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: c85616d79bd2a41109c251ab53d14c20 -timeCreated: 1677662209 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/generate.ts b/Internal/PrefabSafeList/Runtime/generate.ts deleted file mode 100644 index 528faa6a7..000000000 --- a/Internal/PrefabSafeList/Runtime/generate.ts +++ /dev/null @@ -1,33 +0,0 @@ - -function generate(type: string) { - console.log(` [Serializable]`) - console.log(` public class ${type}Set : PrefabSafeList<${type}, ${type}Set.Layer, ${type}Set.Container>`) - console.log(` {`) - console.log(` public ${type}Set(Object outerObject) : base(outerObject)`) - console.log(` {`) - console.log(` }`) - console.log(` [Serializable]`) - console.log(` public class Layer : PrefabLayer<${type}, Container>{}`) - console.log(` [Serializable]`) - console.log(` public class Container : ValueContainer<${type}>{}`) - console.log(` }`) -} - -console.log("// generated by generate.ts") -console.log("// running generate.ts via deno will output this file.") -console.log(`using System;`) -console.log(`using System.Collections.Generic;`) -console.log(`using System.Reflection;`) -console.log(`using JetBrains.Annotations;`) -console.log(`using UnityEngine;`) -console.log(`using VRC.Dynamics;`) -console.log(`using Object = UnityEngine.Object;`) -console.log(``) -console.log(`namespace Anatawa12.AvatarOptimizer.PrefabSafeList`) -console.log(`{`) -generate("SkinnedMeshRenderer") -generate("MeshRenderer") -generate("Material") -generate("String") -generate("VRCPhysBoneBase") -console.log(`}`) diff --git a/Internal/PrefabSafeList/Runtime/generate.ts.meta b/Internal/PrefabSafeList/Runtime/generate.ts.meta deleted file mode 100644 index 273674d69..000000000 --- a/Internal/PrefabSafeList/Runtime/generate.ts.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: cb8a01500b1cb417aaf90e13602c927e -timeCreated: 1676436270 \ No newline at end of file diff --git a/Internal/PrefabSafeList/Runtime/generated.cs b/Internal/PrefabSafeList/Runtime/generated.cs deleted file mode 100644 index df864350e..000000000 --- a/Internal/PrefabSafeList/Runtime/generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -// generated by generate.ts -// running generate.ts via deno will output this file. -using System; -using System.Collections.Generic; -using System.Reflection; -using JetBrains.Annotations; -using UnityEngine; -using VRC.Dynamics; -using Object = UnityEngine.Object; - -namespace Anatawa12.AvatarOptimizer.PrefabSafeList -{ - [Serializable] - public class SkinnedMeshRendererSet : PrefabSafeList - { - public SkinnedMeshRendererSet(Object outerObject) : base(outerObject) - { - } - [Serializable] - public class Layer : PrefabLayer{} - [Serializable] - public class Container : ValueContainer{} - } - [Serializable] - public class MeshRendererSet : PrefabSafeList - { - public MeshRendererSet(Object outerObject) : base(outerObject) - { - } - [Serializable] - public class Layer : PrefabLayer{} - [Serializable] - public class Container : ValueContainer{} - } - [Serializable] - public class MaterialSet : PrefabSafeList - { - public MaterialSet(Object outerObject) : base(outerObject) - { - } - [Serializable] - public class Layer : PrefabLayer{} - [Serializable] - public class Container : ValueContainer{} - } - [Serializable] - public class StringSet : PrefabSafeList - { - public StringSet(Object outerObject) : base(outerObject) - { - } - [Serializable] - public class Layer : PrefabLayer{} - [Serializable] - public class Container : ValueContainer{} - } - [Serializable] - public class VRCPhysBoneBaseSet : PrefabSafeList - { - public VRCPhysBoneBaseSet(Object outerObject) : base(outerObject) - { - } - [Serializable] - public class Layer : PrefabLayer{} - [Serializable] - public class Container : ValueContainer{} - } -} diff --git a/Internal/PrefabSafeList/Runtime/generated.cs.meta b/Internal/PrefabSafeList/Runtime/generated.cs.meta deleted file mode 100644 index 0ac57cb8d..000000000 --- a/Internal/PrefabSafeList/Runtime/generated.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9931db97ce7844b00afb0c1845cbb9bb -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/RemoveMeshInBox.cs b/Runtime/RemoveMeshInBox.cs index 1e1f8648c..524006841 100644 --- a/Runtime/RemoveMeshInBox.cs +++ b/Runtime/RemoveMeshInBox.cs @@ -1,8 +1,6 @@ using System; -using Anatawa12.AvatarOptimizer.PrefabSafeList; using CustomLocalization4EditorExtension; using UnityEngine; -using Object = UnityEngine.Object; namespace Anatawa12.AvatarOptimizer { @@ -11,15 +9,10 @@ namespace Anatawa12.AvatarOptimizer [DisallowMultipleComponent] internal class RemoveMeshInBox : EditSkinnedMeshComponent { - [Obsolete("legacy v1", true)] public BoundingBox[] boxes = Array.Empty(); - public BoundingBoxList boxList; - - public RemoveMeshInBox() - { - boxList = new BoundingBoxList(this); - } + [Obsolete("legacy v2", true)] + public BoundingBoxList boxList = new BoundingBoxList(); [Serializable] public class BoundingBox @@ -42,15 +35,22 @@ public bool ContainsVertex(Vector3 point) } [Serializable] - public class BoundingBoxList : PrefabSafeList + public class BoundingBoxList { + public Container[] firstLayer = Array.Empty(); + public Layer[] prefabLayers = Array.Empty(); + [Serializable] - public class Layer : PrefabLayer {} - [Serializable] - public class Container : ValueContainer {} + public class Layer + { + public Container[] elements = Array.Empty(); + } - public BoundingBoxList(Object outerObject) : base(outerObject) + [Serializable] + public class Container { + public BoundingBox value; + public bool removed; } } } diff --git a/Runtime/com.anatawa12.avatar-optimizer.runtime.asmdef b/Runtime/com.anatawa12.avatar-optimizer.runtime.asmdef index 880adf965..3ddef73d9 100644 --- a/Runtime/com.anatawa12.avatar-optimizer.runtime.asmdef +++ b/Runtime/com.anatawa12.avatar-optimizer.runtime.asmdef @@ -2,8 +2,7 @@ "name": "com.anatawa12.avatar-optimizer.runtime", "references": [ "GUID:8264e72376854221affe9980c91c2fff", - "GUID:8542dbf824204440a818dbc2377cb4d6", - "GUID:c85616d79bd2a41109c251ab53d14c20" + "GUID:8542dbf824204440a818dbc2377cb4d6" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Test~/Runtime/com.anatawa12.avatar-optimizer.test.runtime.asmdef b/Test~/Runtime/com.anatawa12.avatar-optimizer.test.runtime.asmdef index 28d9fda20..5d014b4cd 100644 --- a/Test~/Runtime/com.anatawa12.avatar-optimizer.test.runtime.asmdef +++ b/Test~/Runtime/com.anatawa12.avatar-optimizer.test.runtime.asmdef @@ -2,7 +2,6 @@ "name": "com.anatawa12.avatar-optimizer.test.runtime", "references": [ "GUID:f69eeb3e25674f4a9bd20e6d7e69e0e6", - "GUID:c85616d79bd2a41109c251ab53d14c20", "GUID:8542dbf824204440a818dbc2377cb4d6" ], "includePlatforms": [], diff --git a/Test~/com.anatawa12.avatar-optimizer.test.asmdef b/Test~/com.anatawa12.avatar-optimizer.test.asmdef index 33ceb357d..a0e7d93eb 100644 --- a/Test~/com.anatawa12.avatar-optimizer.test.asmdef +++ b/Test~/com.anatawa12.avatar-optimizer.test.asmdef @@ -4,8 +4,6 @@ "GUID:aeaad65d10ca4136b1d0ee8d9bef1709", "GUID:7e6b618db39145b8a5eb4790d78c2972", "GUID:f69eeb3e25674f4a9bd20e6d7e69e0e6", - "GUID:c85616d79bd2a41109c251ab53d14c20", - "GUID:e3dc35e4a606c438485f5fb532cb875d", "GUID:8542dbf824204440a818dbc2377cb4d6", "GUID:b9880ca0b6584453a2627bd3c038759f", "GUID:27619889b8ba8c24980f49ee34dbb44a",