Skip to content

Commit

Permalink
Merge pull request #1342 from anatawa12/broken-layer-override-support
Browse files Browse the repository at this point in the history
fix: Unpacking prefab might look like some data lost in PrefabSafeUniqueCollection
  • Loading branch information
anatawa12 authored Nov 14, 2024
2 parents 14a6e74 + 3516f81 commit 5b80521
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
### Fixed
- Error with nested merge skinned mesh `#1340`
- Broken synced Layer support `#1341`
- Unpacking prefab might look like some data lost in PrefabSafeUniqueCollection `#1342`

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,14 @@ ElementStatus.Natural or ElementStatus.Removed or ElementStatus.NewElement
}

private (int indexInModifier, SerializedProperty modifierProp) AddToAdditions(TAdditionValue value) =>
AddToModifications(value, ref _currentAdditionsSize, CurrentAdditions, _helper.WriteAdditionValue);
AddToModifications(value, ref _currentAdditionsSize, ref CurrentAdditions, _helper.WriteAdditionValue);

private (int indexInModifier, SerializedProperty modifierProp) AddToRemoves(TRemoveKey value) =>
AddToModifications(value, ref _currentRemovesSize, CurrentRemoves, _helper.WriteRemoveKey);
AddToModifications(value, ref _currentRemovesSize, ref CurrentRemoves, _helper.WriteRemoveKey);

private (int indexInModifier, SerializedProperty modifierProp) AddToModifications<T>(T value,
ref int currentModificationSize,
SerializedProperty? currentModifications,
ref SerializedProperty? currentModifications, // ref is needed to ensure InitCurrentLayer effect
Action<SerializedProperty, T> writeValue)
{
InitCurrentLayer(true);
Expand Down
11 changes: 9 additions & 2 deletions Internal/PrefabSafeSet/Runtime/PrefabSafeSetTestComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Anatawa12.AvatarOptimizer.PrefabSafeSet
{
public class PrefabSafeSetTestComponent : MonoBehaviour
public class PrefabSafeSetTestComponent : MonoBehaviour, ISerializationCallbackReceiver
{
[SerializeField] internal PrefabSafeSet<Material> materials;

Expand All @@ -11,9 +11,16 @@ public PrefabSafeSetTestComponent()
materials = new PrefabSafeSet<Material>(this);
}

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.OnValidate(this, x => x.materials);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
}
}
11 changes: 9 additions & 2 deletions Runtime/FreezeBlendShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Anatawa12.AvatarOptimizer
[AddComponentMenu("Avatar Optimizer/AAO Freeze BlendShapes")]
[DisallowMultipleComponent]
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/freeze-blendshape/")]
internal class FreezeBlendShape : EditSkinnedMeshComponent
internal class FreezeBlendShape : EditSkinnedMeshComponent, ISerializationCallbackReceiver
{
public PrefabSafeSet.PrefabSafeSet<string> shapeKeysSet;

Expand All @@ -18,9 +18,16 @@ public FreezeBlendShape()

public HashSet<string> FreezingShapeKeys => shapeKeysSet.GetAsSet();

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.shapeKeysSet);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
}
}
11 changes: 9 additions & 2 deletions Runtime/MakeChildren.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Anatawa12.AvatarOptimizer
[AddComponentMenu("Avatar Optimizer/AAO Make Children")]
[DisallowMultipleComponent]
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/make-children/")]
internal class MakeChildren : AvatarTagComponent
internal class MakeChildren : AvatarTagComponent, ISerializationCallbackReceiver
{
[NotKeyable, AAOLocalized("MakeChildren:prop:executeEarly", "MakeChildren:tooltip:executeEarly")]
public bool executeEarly;
Expand All @@ -18,9 +18,16 @@ internal MakeChildren()
children = new PrefabSafeSet.PrefabSafeSet<Transform>(this);
}

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.children);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
}
}
11 changes: 9 additions & 2 deletions Runtime/MergePhysBone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Anatawa12.AvatarOptimizer
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/merge-physbone/")]
// INetworkID is implemented to make it possible to assign networkID to this components GameObject
// Note: when MergePhysBone become a public API, we should consider removing INetworkID implementation
internal class MergePhysBone : AvatarTagComponent, VRC.SDKBase.INetworkID
internal class MergePhysBone : AvatarTagComponent, VRC.SDKBase.INetworkID, ISerializationCallbackReceiver
{
[NotKeyable]
[AAOLocalized("MergePhysBone:prop:makeParent", "MergePhysBone:tooltip:makeParent")]
Expand Down Expand Up @@ -254,10 +254,17 @@ public MergePhysBone()
componentsSet = new PrefabSafeSet.PrefabSafeSet<VRCPhysBoneBase>(this);
}

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.componentsSet);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
}

internal enum CollidersSettings
Expand Down
11 changes: 9 additions & 2 deletions Runtime/MergeSkinnedMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Anatawa12.AvatarOptimizer
[DisallowMultipleComponent]
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/merge-skinned-mesh/")]
[PublicAPI]
public sealed class MergeSkinnedMesh : EditSkinnedMeshComponent, ISourceSkinnedMeshComponent
public sealed class MergeSkinnedMesh : EditSkinnedMeshComponent, ISourceSkinnedMeshComponent, ISerializationCallbackReceiver
{
[AAOLocalized("MergeSkinnedMesh:prop:renderers")]
[SerializeField]
Expand Down Expand Up @@ -83,13 +83,20 @@ internal MergeSkinnedMesh()
doNotMergeMaterials = new PrefabSafeSet.PrefabSafeSet<Material>(this);
}

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.renderersSet);
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.staticRenderersSet);
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.doNotMergeMaterials);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}

/// <summary>
/// Initializes the MergeSkinnedMesh with the specified default behavior version.
///
Expand Down
11 changes: 9 additions & 2 deletions Runtime/RemoveMeshByBlendShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Anatawa12.AvatarOptimizer
[AllowMultipleComponent]
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/remove-mesh-by-blendshape/")]
[PublicAPI]
public sealed class RemoveMeshByBlendShape : EditSkinnedMeshComponent
public sealed class RemoveMeshByBlendShape : EditSkinnedMeshComponent, ISerializationCallbackReceiver
{
[SerializeField]
internal PrefabSafeSet.PrefabSafeSet<string> shapeKeysSet;
Expand All @@ -27,11 +27,18 @@ internal RemoveMeshByBlendShape()

internal HashSet<string> RemovingShapeKeys => shapeKeysSet.GetAsSet();

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.shapeKeysSet);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}

APIChecker _checker;

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion Runtime/RenameBlendShape.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
using System;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer
{
[AddComponentMenu("Avatar Optimizer/AAO Rename BlendShape")]
[DisallowMultipleComponent]
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/rename-blend-shape/")]
internal class RenameBlendShape : EditSkinnedMeshComponent
internal class RenameBlendShape : EditSkinnedMeshComponent, ISerializationCallbackReceiver
{
[SerializeField] internal PrefabSafeMap.PrefabSafeMap<string, string> nameMap;

public RenameBlendShape()
{
nameMap = new PrefabSafeMap.PrefabSafeMap<string, string>(this);
}

private void ValidatePSUC()
{
PrefabSafeMap.PrefabSafeMap.OnValidate(this, x => x.nameMap);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
}
}
4 changes: 3 additions & 1 deletion Test~/PrefabSafeSet/PSSTestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static GameObject CreateBasePrefab()
component.stringSet.mainSet = new[]
{ "mainSet", "addedTwiceInVariant", "removedInVariant", "addedTwiceInInstance", "removedInInstance" };
newObject = PrefabUtility.SaveAsPrefabAsset(newObject, $"Assets/test-{Guid.NewGuid()}.prefab");
Assert.True(newObject);
newObject.GetComponent<PrefabSafeSetComponent>().stringSet.IsNew = false;
return newObject;
}

Expand All @@ -31,6 +31,7 @@ private static GameObject CreateBaseVariantPrefab(GameObject basePrefab)
new[] { "addedTwiceInVariant", "addedInVariant", "addedInVariantRemovedInInstance" };
component.stringSet.prefabLayers[0].removes = new[] { "removedInVariant", "fakeRemovedInVariant" };
newObject = PrefabUtility.SaveAsPrefabAsset(newObject, $"Assets/test-{Guid.NewGuid()}.prefab");
newObject.GetComponent<PrefabSafeSetComponent>().stringSet.IsNew = false;
Assert.True(newObject);
return newObject;
}
Expand All @@ -43,6 +44,7 @@ private static GameObject CreateInstance(GameObject baseObject)
component.stringSet.prefabLayers[1].additions = new[] { "addedTwiceInInstance", "addedInInstance" };
component.stringSet.prefabLayers[1].removes = new[]
{ "removedInInstance", "addedInVariantRemovedInInstance", "fakeRemovedInInstance" };
component.stringSet.IsNew = false;
return newObject;
}

Expand Down
11 changes: 9 additions & 2 deletions Test~/Runtime/PrefabSafeSetComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Anatawa12.AvatarOptimizer.Test.Runtime
{
public class PrefabSafeSetComponent : MonoBehaviour
public class PrefabSafeSetComponent : MonoBehaviour, ISerializationCallbackReceiver
{
public PrefabSafeSet.PrefabSafeSet<string> stringSet;

Expand All @@ -11,9 +11,16 @@ public PrefabSafeSetComponent()
stringSet = new PrefabSafeSet.PrefabSafeSet<string>(this);
}

private void OnValidate()
private void ValidatePSUC()
{
PrefabSafeSet.PrefabSafeSet.OnValidate(this, x => x.stringSet);
}

private void OnValidate() => ValidatePSUC();
void ISerializationCallbackReceiver.OnBeforeSerialize() => ValidatePSUC();

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
}
}
}

0 comments on commit 5b80521

Please sign in to comment.