Skip to content

Commit

Permalink
Enable nullable warnings for the animation API
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Nov 17, 2024
1 parent a8c04fb commit dd1927c
Show file tree
Hide file tree
Showing 19 changed files with 261 additions and 196 deletions.
4 changes: 3 additions & 1 deletion Editor/API/AnimatorServices/AnimationIndex.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
Expand Down
48 changes: 26 additions & 22 deletions Editor/API/AnimatorServices/AnimatorServicesContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
#nullable enable

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
Expand All @@ -9,14 +11,20 @@ public sealed class AnimatorServicesContext : IExtensionContext
{
private class LayerState
{
internal RuntimeAnimatorController originalController;
internal VirtualAnimatorController virtualController;
}
internal readonly RuntimeAnimatorController? OriginalController;
internal VirtualAnimatorController? VirtualController;

private CloneContext _cloneContext;
public LayerState(RuntimeAnimatorController? originalController)
{
OriginalController = originalController;
}
}

private readonly Dictionary<object, LayerState> _layerStates = new();

private IPlatformAnimatorBindings _platformBindings;
// initialized on activate
private IPlatformAnimatorBindings? _platformBindings;
private CloneContext? _cloneContext;

public void OnActivate(BuildContext context)
{
Expand All @@ -33,20 +41,16 @@ public void OnActivate(BuildContext context)

_cloneContext = new CloneContext(_platformBindings);

foreach (var (type, controller, isDefault) in _platformBindings.GetInnateControllers(root))
foreach (var (type, controller, _) in _platformBindings.GetInnateControllers(root))
{
_layerStates[type] = new LayerState
{
originalController = controller,
virtualController = null
};
_layerStates[type] = new LayerState(controller);

// TEMP
// TEMP - force all layers to be processed
_ = this[type];
}
}

public VirtualAnimatorController this[object key]
public VirtualAnimatorController? this[object key]
{
get
{
Expand All @@ -55,16 +59,16 @@ public VirtualAnimatorController this[object key]
return null;
}

if (state.virtualController == null)
if (state.VirtualController == null)
{
state.virtualController = _cloneContext.Clone(state.originalController);
state.VirtualController = _cloneContext!.Clone(state.OriginalController);
}

return state.virtualController;
return state.VirtualController;
}
set => _layerStates[key] = new LayerState
set => _layerStates[key] = new LayerState(null)
{
virtualController = value
VirtualController = value
};
}

Expand All @@ -74,13 +78,13 @@ public void OnDeactivate(BuildContext context)

var commitContext = new CommitContext();

var controllers = _layerStates.Where(kvp => kvp.Value.virtualController != null)
var controllers = _layerStates.Where(kvp => kvp.Value.VirtualController != null)
.ToDictionary(
k => k.Key,
v => (RuntimeAnimatorController)commitContext.CommitObject(v.Value.virtualController)
v => (RuntimeAnimatorController)commitContext.CommitObject(v.Value.VirtualController!)
);

_platformBindings.CommitInnateControllers(root, controllers);
_platformBindings!.CommitInnateControllers(root, controllers);
}
}
}
48 changes: 31 additions & 17 deletions Editor/API/AnimatorServices/CloneContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using UnityEditor.Animations;
using UnityEngine;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -61,30 +64,33 @@ public AnimationClip MapClipOnClone(AnimationClip clip)

return clip;
}
public bool TryGetValue<T, U>(T key, out U value) where U: IDisposable

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

var rv = _clones.TryGetValue(key, out var tmp);

if (rv) value = (U)tmp;
if (rv) value = (U?)tmp;
else value = default;

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, IDisposable
{
try
{
_cloneDepth++;

if (key == null || (key is Object obj && obj == null)) return null;
if (TryGetValue(key, out U value)) return value;
if (TryGetValue(key, out U? value)) return value;
value = clone(this, key);
_clones[key] = value;
return value;
Expand Down Expand Up @@ -124,49 +130,57 @@ internal int AllocateSingleVirtualLayer()
return _nextVirtualLayer++;
}

public VirtualAnimatorController Clone(RuntimeAnimatorController controller)
[return: NotNullIfNotNull("controller")]
public VirtualAnimatorController? Clone(RuntimeAnimatorController? controller)
{
using var _ = new ProfilerScope("Clone Animator Controller", controller);
return GetOrClone(controller, VirtualAnimatorController.Clone);
}

public VirtualLayer Clone(AnimatorControllerLayer layer, int index)

[return: NotNullIfNotNull("layer")]
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)!);
}

public VirtualStateMachine Clone(AnimatorStateMachine stateMachine)

[return: NotNullIfNotNull("stateMachine")]
public VirtualStateMachine? Clone(AnimatorStateMachine? stateMachine)
{
using var _ = new ProfilerScope("Clone State Machine", stateMachine);
return GetOrClone(stateMachine, VirtualStateMachine.Clone);
}

public VirtualStateTransition Clone(AnimatorStateTransition transition)
[return: NotNullIfNotNull("transition")]
public VirtualStateTransition? Clone(AnimatorStateTransition? transition)
{
using var _ = new ProfilerScope("Clone State Transition", transition);
return GetOrClone(transition, VirtualStateTransition.Clone);
}

public VirtualTransition Clone(AnimatorTransition transition)
[return: NotNullIfNotNull("transition")]
public VirtualTransition? Clone(AnimatorTransition? transition)
{
using var _ = new ProfilerScope("Clone Transition", transition);
return GetOrClone(transition, VirtualTransition.Clone);
}

public VirtualState Clone(AnimatorState state)
[return: NotNullIfNotNull("state")]
public VirtualState? Clone(AnimatorState? state)
{
using var _ = new ProfilerScope("Clone State", state);
return GetOrClone(state, VirtualState.Clone);
}

public VirtualMotion Clone(Motion m)
[return: NotNullIfNotNull("m")]
public VirtualMotion? Clone(Motion? m)
{
using var _ = new ProfilerScope("Clone Motion", m);
return GetOrClone(m, VirtualMotion.Clone);
}

public VirtualClip Clone(AnimationClip clip)
[return: NotNullIfNotNull("clip")]
public VirtualClip? Clone(AnimationClip? clip)
{
using var _ = new ProfilerScope("Clone Clip", clip);
return GetOrClone(clip, VirtualClip.Clone);
Expand Down
4 changes: 3 additions & 1 deletion Editor/API/AnimatorServices/ECBComparator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using UnityEditor;

Expand Down
9 changes: 7 additions & 2 deletions Editor/API/AnimatorServices/ICommitable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
#nullable enable

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using UnityEngine;

namespace nadena.dev.ndmf.animator
Expand All @@ -16,6 +19,7 @@ internal interface ICommitable<T>
/// Fills in all fields of the destination unity object. This may recurse back into the CommitContext.
/// </summary>
/// <param name="context"></param>
/// <param name="obj">Object returned from Prepare</param>
void Commit(CommitContext context, T obj);
}

Expand All @@ -25,7 +29,8 @@ internal class CommitContext
private readonly Dictionary<int, VirtualLayer> _virtIndexToVirtLayer = new();
private readonly Dictionary<VirtualLayer, int> _virtLayerToPhysIndex = new();

internal R CommitObject<R>(ICommitable<R> obj) where R : class
[return: NotNullIfNotNull("obj")]
internal R? CommitObject<R>(ICommitable<R>? obj) where R : class
{
if (obj == null) return null;
if (_commitCache.TryGetValue(obj, out var result)) return (R)result;
Expand Down
23 changes: 15 additions & 8 deletions Editor/API/AnimatorServices/LayerPriority.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using System;
#nullable enable

using System;

namespace nadena.dev.ndmf.animator
{
public struct LayerPriority : IComparable<LayerPriority>
public struct LayerPriority : IComparable<LayerPriority>, IEquatable<LayerPriority>
{
public static LayerPriority Default = new();
internal int Priority;

private readonly int _priority;

public LayerPriority(int priority)
{
Priority = priority;
_priority = priority;
}

public int CompareTo(LayerPriority other)
{
if (Priority != other.Priority) return Priority.CompareTo(other.Priority);
if (_priority != other._priority) return _priority.CompareTo(other._priority);

return 0;
}
Expand Down Expand Up @@ -52,12 +54,17 @@ public int CompareTo(LayerPriority other)

public override bool Equals(object obj)
{
return obj is LayerPriority other && this == other;
return obj is LayerPriority other && Equals(other);
}

public override int GetHashCode()
{
return Priority.GetHashCode();
return _priority;
}

public bool Equals(LayerPriority other)
{
return _priority == other._priority;
}
}
}
16 changes: 11 additions & 5 deletions Editor/API/AnimatorServices/ObjectPathRemapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
#nullable enable

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

Expand Down Expand Up @@ -53,9 +55,13 @@ private void UpdateCache()

foreach (var kvp in _objectToOriginalPaths)
{
var virtualPath = GetVirtualPathForObject(kvp.Key);

if (virtualPath == null) continue;

foreach (var path in kvp.Value)
{
_originalToMappedPath[path] = GetVirtualPathForObject(kvp.Key);
_originalToMappedPath[path] = virtualPath;
}
}
}
Expand All @@ -70,18 +76,18 @@ public void RecordObjectTree(Transform subtree)
}
}

public GameObject GetObjectForPath(string path)
public GameObject? GetObjectForPath(string path)
{
var xform = _pathToObject.GetValueOrDefault(path);
return xform ? xform.gameObject : null;
}

public string GetVirtualPathForObject(GameObject obj)
public string? GetVirtualPathForObject(GameObject obj)
{
return GetVirtualPathForObject(obj.transform);
}

public string GetVirtualPathForObject(Transform t)
public string? GetVirtualPathForObject(Transform t)
{
if (_objectToOriginalPaths.TryGetValue(t, out var paths))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -17,14 +18,14 @@ namespace nadena.dev.ndmf.animator
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal class SyncedLayerOverrideAccess
{
public static readonly Func<AnimatorControllerLayer, IEnumerable<KeyValuePair<AnimatorState, Motion>>>
public static readonly Func<AnimatorControllerLayer, IEnumerable<KeyValuePair<AnimatorState, Motion>>?>
ExtractStateMotionPairs;

public static readonly Action<AnimatorControllerLayer, IEnumerable<KeyValuePair<AnimatorState, Motion>>>
SetStateMotionPairs;

public static readonly
Func<AnimatorControllerLayer, IEnumerable<KeyValuePair<AnimatorState, ScriptableObject[]>>>
Func<AnimatorControllerLayer, IEnumerable<KeyValuePair<AnimatorState, ScriptableObject[]>>?>
ExtractStateBehaviourPairs;

public static readonly
Expand Down
Loading

0 comments on commit dd1927c

Please sign in to comment.