Skip to content

Commit

Permalink
Minor API adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Nov 17, 2024
1 parent dd1927c commit ccf4aea
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 90 deletions.
2 changes: 2 additions & 0 deletions Editor/API/AnimatorServices/AnimatorServicesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using VRC.SDK3.Avatars.Components;

namespace nadena.dev.ndmf.animator
{
[PublicAPI]
public sealed class AnimatorServicesContext : IExtensionContext
{
private class LayerState
Expand Down
18 changes: 7 additions & 11 deletions Editor/API/AnimatorServices/CloneContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using UnityEditor.Animations;
using UnityEngine;
using Object = UnityEngine.Object;

namespace nadena.dev.ndmf.animator
{
public class CloneContext
[PublicAPI]
public sealed class CloneContext
{
public IPlatformAnimatorBindings PlatformBindings { get; private set; }
private readonly Dictionary<object, IDisposable> _clones = new();
private readonly Dictionary<object, object> _clones = new();

private int _cloneDepth, _nextVirtualLayer, _virtualLayerBase, _maxMappedPhysLayer;
private readonly Queue<Action> _deferredCalls = new();
Expand Down Expand Up @@ -65,7 +67,7 @@ public AnimationClip MapClipOnClone(AnimationClip clip)
return clip;
}

public bool TryGetValue<T, U>(T key, out U? value) where U : IDisposable
internal bool TryGetValue<T, U>(T key, out U? value)
{
if (key == null) throw new ArgumentNullException(nameof(key));

Expand All @@ -77,13 +79,7 @@ public bool TryGetValue<T, U>(T key, out U? value) where U : IDisposable
return rv;
}

public void Add<T, U>(T key, U value) where U: IDisposable
{
if (key == null) throw new ArgumentNullException(nameof(key));
_clones.Add(key, value);
}

private U? GetOrClone<T, U>(T? key, Func<CloneContext, T, U> clone) where U : class, IDisposable
private U? GetOrClone<T, U>(T? key, Func<CloneContext, T, U> clone) where U : class
{
try
{
Expand Down Expand Up @@ -141,7 +137,7 @@ internal int AllocateSingleVirtualLayer()
public VirtualLayer? Clone(AnimatorControllerLayer? layer, int index)
{
using var _ = new ProfilerScope("Clone Animator Layer");
return GetOrClone(layer, (ctx, obj) => VirtualLayer.Clone(ctx, obj, index)!);
return GetOrClone(layer, (ctx, obj) => VirtualLayer.Clone(ctx, obj, index));
}

[return: NotNullIfNotNull("stateMachine")]
Expand Down
2 changes: 2 additions & 0 deletions Editor/API/AnimatorServices/LayerPriority.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#nullable enable

using System;
using JetBrains.Annotations;

namespace nadena.dev.ndmf.animator
{
[PublicAPI]
public struct LayerPriority : IComparable<LayerPriority>, IEquatable<LayerPriority>
{
public static LayerPriority Default = new();
Expand Down
4 changes: 3 additions & 1 deletion Editor/API/AnimatorServices/ObjectPathRemapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using System.Collections.Generic;
using JetBrains.Annotations;
using nadena.dev.ndmf.runtime;
using UnityEngine;

Expand All @@ -25,7 +26,8 @@ namespace nadena.dev.ndmf.animator
/// Note that it's possible that these paths may collide with paths that _previously_ existed, so it's still
/// recommended to use `GetVirtualPathForObject` to ensure that the path is unique.
/// </summary>
public class ObjectPathRemapper
[PublicAPI]
public sealed class ObjectPathRemapper
{
private readonly Transform _root;
private readonly Dictionary<Transform, List<string>> _objectToOriginalPaths = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace nadena.dev.ndmf.animator
{
public class VRChatPlatformAnimatorBindings : IPlatformAnimatorBindings
public sealed class VRChatPlatformAnimatorBindings : IPlatformAnimatorBindings
{
private const string SAMPLE_PATH_PACKAGE =
"Packages/com.vrchat.avatars/Samples/AV3 Demo Assets/Animation/Controllers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;
using UnityEditor.Animations;
using UnityEngine;

Expand All @@ -20,7 +21,8 @@ namespace nadena.dev.ndmf.animator
/// - AnimationClip
/// - Any state behaviors attached to the animator controller
/// </summary>
public class VirtualAnimatorController : VirtualNode, ICommitable<AnimatorController>, IDisposable
[PublicAPI]
public sealed class VirtualAnimatorController : VirtualNode, ICommitable<AnimatorController>
{
private readonly CloneContext _context;
public string Name { get; set; }
Expand All @@ -40,7 +42,12 @@ private struct LayerGroup
public List<VirtualLayer> Layers;
}

public VirtualAnimatorController(CloneContext context, string name = "")
public static VirtualAnimatorController Create(CloneContext context, string name = "(unnamed)")
{
return new VirtualAnimatorController(context, name);
}

private VirtualAnimatorController(CloneContext context, string name)
{
_context = context;
_parameters = ImmutableDictionary<string, AnimatorControllerParameter>.Empty;
Expand Down Expand Up @@ -136,11 +143,6 @@ void ICommitable<AnimatorController>.Commit(CommitContext context, AnimatorContr
obj.layers = Layers.Select(context.CommitObject).ToArray();
}

public void Dispose()
{
throw new NotImplementedException();
}

protected override IEnumerable<VirtualNode> _EnumerateChildren()
{
return Layers;
Expand Down
18 changes: 10 additions & 8 deletions Editor/API/AnimatorServices/VirtualObjects/VirtualBlendTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

namespace nadena.dev.ndmf.animator
{
public class VirtualBlendTree : VirtualMotion
[PublicAPI]
public sealed class VirtualBlendTree : VirtualMotion
{
private readonly BlendTree _tree;

Expand All @@ -23,12 +25,17 @@ public sealed class VirtualChildMotion
public float TimeScale = 1.0f;
}

private VirtualBlendTree(CloneContext context, BlendTree cloned)
public static VirtualBlendTree Create(string name = "(unnamed)")
{
return new VirtualBlendTree(null, new BlendTree { name = name });
}

private VirtualBlendTree(CloneContext? context, BlendTree cloned)
{
_tree = cloned;
_children = ImmutableList<VirtualChildMotion>.Empty;

context.DeferCall(() =>
context?.DeferCall(() =>
{
Children = _tree.children.Select(m => new VirtualChildMotion
{
Expand Down Expand Up @@ -132,11 +139,6 @@ protected override void Commit(object context, Motion obj)
}).ToArray();
}

public override void Dispose()
{
Object.DestroyImmediate(_tree);
}

protected override IEnumerable<VirtualNode> _EnumerateChildren()
{
return Children.Where(c => c.Motion != null).Select(c => c.Motion!);
Expand Down
7 changes: 1 addition & 6 deletions Editor/API/AnimatorServices/VirtualObjects/VirtualClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace nadena.dev.ndmf.animator
/// clips, and in particular provides helpers for common operations (e.g. rewriting all paths in a clip).
/// </summary>
[PublicAPI]
public class VirtualClip : VirtualMotion, IDisposable
public sealed class VirtualClip : VirtualMotion
{
private AnimationClip _clip;

Expand Down Expand Up @@ -464,10 +464,5 @@ public void SetObjectCurve(string path, Type type, string prop, ObjectReferenceK
{
SetObjectCurve(EditorCurveBinding.PPtrCurve(path, type, prop), curve);
}

public override void Dispose()
{
if (_clip != null) Object.DestroyImmediate(_clip);
}
}
}
8 changes: 1 addition & 7 deletions Editor/API/AnimatorServices/VirtualObjects/VirtualLayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand All @@ -15,7 +14,7 @@ namespace nadena.dev.ndmf.animator
/// A layer within a VirtualAnimatorController
/// </summary>
[PublicAPI]
public class VirtualLayer : VirtualNode, ICommitable<AnimatorControllerLayer>, IDisposable
public sealed class VirtualLayer : VirtualNode, ICommitable<AnimatorControllerLayer>
{
/// <summary>
/// Returns a "virtual layer index" which can be used to map to the actual layer index in the animator controller,
Expand Down Expand Up @@ -202,11 +201,6 @@ void ICommitable<AnimatorControllerLayer>.Commit(CommitContext context, Animator
)));
}

public void Dispose()
{
// no-op
}

public override string ToString()
{
return $"VirtualLayer[{VirtualLayerIndex}]: {Name}";
Expand Down
8 changes: 4 additions & 4 deletions Editor/API/AnimatorServices/VirtualObjects/VirtualMotion.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#nullable enable

using System;
using JetBrains.Annotations;
using UnityEditor.Animations;
using UnityEngine;

namespace nadena.dev.ndmf.animator
{
public abstract class VirtualMotion : VirtualNode, ICommitable<Motion>, IDisposable
[PublicAPI]
public abstract class VirtualMotion : VirtualNode, ICommitable<Motion>
{
internal VirtualMotion()
{
}

public static VirtualMotion Clone(
internal static VirtualMotion Clone(
CloneContext context,
Motion motion
)
Expand Down Expand Up @@ -42,7 +44,5 @@ void ICommitable<Motion>.Commit(CommitContext context, Motion obj)
{
Commit(context, obj);
}

public abstract void Dispose();
}
}
7 changes: 6 additions & 1 deletion Editor/API/AnimatorServices/VirtualObjects/VirtualNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

using System;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace nadena.dev.ndmf.animator
{
[ExcludeFromDocs]
/// <summary>
/// Base class for all virtual animation nodes. Contains common functionality for cache invalidation.
/// Generally, external libraries should not use this class directly.
/// </summary>
[PublicAPI]
public abstract class VirtualNode
{
private Action? _lastCacheObserver;
Expand Down
19 changes: 3 additions & 16 deletions Editor/API/AnimatorServices/VirtualObjects/VirtualState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand All @@ -13,7 +12,7 @@
namespace nadena.dev.ndmf.animator
{
[PublicAPI]
public class VirtualState : VirtualNode, ICommitable<AnimatorState>, IDisposable
public sealed class VirtualState : VirtualNode, ICommitable<AnimatorState>
{
private AnimatorState _state;

Expand All @@ -25,7 +24,7 @@ public ImmutableList<StateMachineBehaviour> Behaviours
set => _behaviours = I(value);
}

public static VirtualState Clone(
internal static VirtualState Clone(
CloneContext context,
AnimatorState state
)
Expand All @@ -39,7 +38,7 @@ AnimatorState state
return new VirtualState(context, clonedState);
}

public static VirtualState Create(CloneContext _, string name = "unnamed")
public static VirtualState Create(string name = "unnamed")
{
return new VirtualState { Name = name };
}
Expand Down Expand Up @@ -191,18 +190,6 @@ void ICommitable<AnimatorState>.Commit(CommitContext context, AnimatorState obj)
obj.behaviours = Behaviours.ToArray();
obj.transitions = Transitions.Select(t => (AnimatorStateTransition)context.CommitObject(t)).ToArray();
}

void IDisposable.Dispose()
{
foreach (var behaviour in Behaviours)
{
Object.DestroyImmediate(behaviour);
}

Behaviours = Behaviours.Clear();

if (_state != null) Object.DestroyImmediate(_state);
}

public override string ToString()
{
Expand Down
15 changes: 3 additions & 12 deletions Editor/API/AnimatorServices/VirtualObjects/VirtualStateMachine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand All @@ -13,12 +12,12 @@ namespace nadena.dev.ndmf.animator
/// <summary>
/// Represents a state machine in a virtual layer.
/// </summary>
public class VirtualStateMachine : VirtualNode, ICommitable<AnimatorStateMachine>, IDisposable
public sealed class VirtualStateMachine : VirtualNode, ICommitable<AnimatorStateMachine>
{
private readonly CloneContext _context;
private AnimatorStateMachine _stateMachine;

public static VirtualStateMachine Clone(CloneContext context, AnimatorStateMachine stateMachine)
internal static VirtualStateMachine Clone(CloneContext context, AnimatorStateMachine stateMachine)
{
if (context.TryGetValue(stateMachine, out VirtualStateMachine? clone)) return clone!;

Expand Down Expand Up @@ -243,14 +242,6 @@ public struct VirtualChildState
public Vector3 Position;
}

public void Dispose()
{
if (_stateMachine != null)
{
Object.DestroyImmediate(_stateMachine);
}
}

protected override IEnumerable<VirtualNode> _EnumerateChildren()
{
foreach (var sm in StateMachines)
Expand Down Expand Up @@ -278,7 +269,7 @@ protected override IEnumerable<VirtualNode> _EnumerateChildren()

public VirtualState AddState(string name, VirtualMotion? motion = null, Vector3? position = null)
{
var state = VirtualState.Create(_context, name);
var state = VirtualState.Create(name);

state.Motion = motion;
var childState = new VirtualChildState
Expand Down
Loading

0 comments on commit ccf4aea

Please sign in to comment.