Skip to content

Commit

Permalink
Fix copy paste controls crashing with stackoverflow
Browse files Browse the repository at this point in the history
  • Loading branch information
acidbubbles committed Aug 28, 2023
1 parent 288dd7e commit 3a43608
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 113 deletions.
34 changes: 17 additions & 17 deletions src/AtomAnimations/Animations/AtomAnimationClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,62 +754,62 @@ public void Leave()

#region Add/Remove Targets

public IAtomAnimationTarget Add(IAtomAnimationTarget target)
public IAtomAnimationTarget AddAny(IAtomAnimationTarget target)
{
if (target is FreeControllerV3AnimationTarget)
return Add((FreeControllerV3AnimationTarget)target);
return AddController((FreeControllerV3AnimationTarget)target);
if (target is JSONStorableFloatAnimationTarget)
return Add((JSONStorableFloatAnimationTarget)target);
return AddFloatParam((JSONStorableFloatAnimationTarget)target);
if (target is TriggersTrackAnimationTarget)
return Add((TriggersTrackAnimationTarget)target);
return AddTriggers((TriggersTrackAnimationTarget)target);
throw new NotSupportedException($"Cannot add unknown target type {target}");
}

public FreeControllerV3AnimationTarget Add(FreeControllerV3Ref controllerRef, bool targetsPosition, bool targetsRotation)
public FreeControllerV3AnimationTarget AddController(FreeControllerV3Ref controllerRef, bool targetsPosition, bool targetsRotation)
{
if (targetControllers.Any(c => c.TargetsSameAs(controllerRef, targetsPosition, targetsRotation))) return null;
return Add(new FreeControllerV3AnimationTarget(controllerRef, targetsPosition, targetsRotation));
return AddController(new FreeControllerV3AnimationTarget(controllerRef, targetsPosition, targetsRotation));
}

public JSONStorableFloatAnimationTarget Add(JSONStorableFloatRef floatRef)
public JSONStorableFloatAnimationTarget AddFloatParam(JSONStorableFloatRef floatRef)
{
if (targetFloatParams.Any(t => t.animatableRef == floatRef)) return null;
floatRef.EnsureAvailable();
return Add(new JSONStorableFloatAnimationTarget(floatRef));
return AddFloatParam(new JSONStorableFloatAnimationTarget(floatRef));
}

public TriggersTrackAnimationTarget Add(TriggersTrackRef triggersRef)
public TriggersTrackAnimationTarget AddTriggers(TriggersTrackRef triggersRef)
{
if (targetTriggers.Any(t => t.animatableRef == triggersRef)) return null;
if (triggersRef.animationLayerQualifiedId != animationLayerQualifiedId) throw new InvalidOperationException("Triggers track is not owned by the current layer");
return Add(new TriggersTrackAnimationTarget(triggersRef, _logger));
return AddTriggers(new TriggersTrackAnimationTarget(triggersRef, _logger));
}

public IAtomAnimationTarget Add(AnimatableRefBase animatableRef)
public IAtomAnimationTarget AddAny(AnimatableRefBase animatableRef)
{
if (animatableRef is FreeControllerV3Ref)
return Add((FreeControllerV3Ref)animatableRef);
return AddController((FreeControllerV3Ref)animatableRef, true, true);
if (animatableRef is JSONStorableFloatRef)
return Add((JSONStorableFloatRef)animatableRef);
return AddFloatParam((JSONStorableFloatRef)animatableRef);
if (animatableRef is TriggersTrackRef)
return Add((TriggersTrackRef)animatableRef);
return AddTriggers((TriggersTrackRef)animatableRef);
throw new NotSupportedException($"Cannot add unknown animatableRef type {animatableRef}");
}

public FreeControllerV3AnimationTarget Add(FreeControllerV3AnimationTarget target)
public FreeControllerV3AnimationTarget AddController(FreeControllerV3AnimationTarget target)
{
if (targetControllers.Any(target.TargetsSameAs)) return null;
foreach (var curve in target.curves) { curve.loop = _loop; }
return Add(targetControllers, new FreeControllerV3AnimationTarget.Comparer(), target);
}

public JSONStorableFloatAnimationTarget Add(JSONStorableFloatAnimationTarget target)
public JSONStorableFloatAnimationTarget AddFloatParam(JSONStorableFloatAnimationTarget target)
{
target.value.loop = _loop;
return Add(targetFloatParams, new JSONStorableFloatAnimationTarget.Comparer(), target);
}

public TriggersTrackAnimationTarget Add(TriggersTrackAnimationTarget target)
public TriggersTrackAnimationTarget AddTriggers(TriggersTrackAnimationTarget target)
{
return Add(targetTriggers, new TriggersTrackAnimationTarget.Comparer(), target);
}
Expand Down
2 changes: 1 addition & 1 deletion src/AtomAnimations/Editing/AtomAnimationEditContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ private void AddTargetIfMissing<TEntry, TRef, TSnapshot>(List<TEntry> entries, F
{
continue;
}
var added = clip.Add(animatableRefToAdd);
var added = clip.AddAny(animatableRefToAdd);
if (added == null)
{
SuperController.LogError($"Timeline: Cannot paste {animatableRef.GetFullName()}, invalid add state.");
Expand Down
14 changes: 7 additions & 7 deletions src/AtomAnimations/Operations/AddAnimationOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public CreatedAnimation AddAnimation(AtomAnimationClip source, string animationN
foreach (var origTarget in source.targetFloatParams)
{
if (!origTarget.animatableRef.EnsureAvailable(false)) continue;
var newTarget = clip.Add(new JSONStorableFloatAnimationTarget(origTarget));
var newTarget = clip.AddFloatParam(new JSONStorableFloatAnimationTarget(origTarget));
newTarget.group = origTarget.group;
newTarget.value.keys = new List<BezierKeyframe>(origTarget.value.keys);
newTarget.dirty = true;
}

foreach (var origTarget in source.targetTriggers)
{
var newTarget = clip.Add(new TriggersTrackAnimationTarget(origTarget.animatableRef, _animation.logger));
var newTarget = clip.AddTriggers(new TriggersTrackAnimationTarget(origTarget.animatableRef, _animation.logger));
newTarget.group = origTarget.group;
foreach (var origTrigger in origTarget.triggersMap)
{
Expand All @@ -101,7 +101,7 @@ public CreatedAnimation AddAnimation(AtomAnimationClip source, string animationN
foreach (var origTarget in source.targetFloatParams)
{
if (!origTarget.animatableRef.EnsureAvailable(false)) continue;
var newTarget = clip.Add(origTarget.animatableRef);
var newTarget = clip.AddFloatParam(origTarget.animatableRef);
newTarget.group = origTarget.group;
newTarget.SetKeyframeToCurrent(0f);
newTarget.SetKeyframeToCurrent(clip.animationLength);
Expand All @@ -112,7 +112,7 @@ public CreatedAnimation AddAnimation(AtomAnimationClip source, string animationN
var newTarget = new TriggersTrackAnimationTarget(origTarget.animatableRef, _animation.logger);
newTarget.group = origTarget.group;
newTarget.AddEdgeFramesIfMissing(clip.animationLength);
clip.Add(newTarget);
clip.AddTriggers(newTarget);
}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ private CreatedAnimation AddTransitionAnimation(AtomAnimationClip source)
foreach (var origTarget in source.targetFloatParams)
{
if (!origTarget.animatableRef.EnsureAvailable(false)) continue;
var newTarget = clip.Add(origTarget.animatableRef);
var newTarget = clip.AddFloatParam(origTarget.animatableRef);
newTarget.SetCurveSnapshot(0f, origTarget.GetCurveSnapshot(source.animationLength));
newTarget.SetCurveSnapshot(clip.animationLength, next.targetFloatParams.First(t => t.TargetsSameAs(origTarget)).GetCurveSnapshot(0f));
}
Expand All @@ -172,7 +172,7 @@ private CreatedAnimation AddTransitionAnimation(AtomAnimationClip source)
{
var newTarget = new TriggersTrackAnimationTarget(origTarget.animatableRef, _animation.logger);
newTarget.AddEdgeFramesIfMissing(clip.animationLength);
clip.Add(newTarget);
clip.AddTriggers(newTarget);
}

source.nextAnimationName = clip.animationName;
Expand All @@ -181,7 +181,7 @@ private CreatedAnimation AddTransitionAnimation(AtomAnimationClip source)

private static FreeControllerV3AnimationTarget CopyTarget(AtomAnimationClip clip, FreeControllerV3AnimationTarget origTarget)
{
var newTarget = clip.Add(origTarget.animatableRef, origTarget.targetsPosition, origTarget.targetsRotation);
var newTarget = clip.AddController(origTarget.animatableRef, origTarget.targetsPosition, origTarget.targetsRotation);
newTarget.SetParent(origTarget.parentAtomId, origTarget.parentRigidbodyId);
newTarget.weight = origTarget.weight;
newTarget.targetsPosition = origTarget.targetsPosition;
Expand Down
2 changes: 1 addition & 1 deletion src/AtomAnimations/Operations/LayersOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<AtomAnimationClip> SplitLayer(List<IAtomAnimationTarget> targets, st
foreach (var t in sourceClip.GetAllTargets().Where(t => targets.Any(t.TargetsSameAs)).ToList())
{
sourceClip.Remove(t);
newClip.Add(t);
newClip.AddAny(t);
}
created.Add(newClip);
}
Expand Down
3 changes: 1 addition & 2 deletions src/AtomAnimations/Operations/RecordOperations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
Expand Down
2 changes: 1 addition & 1 deletion src/AtomAnimations/Operations/TargetsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public FreeControllerV3AnimationTarget Add(FreeControllerV3 fc)
if (target != null) return target;
foreach (var clip in _animation.index.ByLayerQualified(_clip.animationLayerQualifiedId))
{
var t = clip.Add(_animation.animatables.GetOrCreateController(fc, fc.containingAtom == _containingAtom), true, true);
var t = clip.AddController(_animation.animatables.GetOrCreateController(fc, fc.containingAtom == _containingAtom), true, true);
if (t == null) continue;
t.SetKeyframeToCurrent(0f);
t.SetKeyframeToCurrent(clip.animationLength);
Expand Down
6 changes: 3 additions & 3 deletions src/AtomAnimations/Serialization/AtomAnimationSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private void DeserializeClip(AtomAnimationClip clip, JSONClass clipJSON, Animata
continue;
}

var target = clip.Add(controllerRef, DeserializeBool(controllerJSON["TargetsPosition"], true), DeserializeBool(controllerJSON["TargetsRotation"], true));
var target = clip.AddController(controllerRef, DeserializeBool(controllerJSON["TargetsPosition"], true), DeserializeBool(controllerJSON["TargetsRotation"], true));
if (target == null)
{
SuperController.LogError(
Expand Down Expand Up @@ -262,7 +262,7 @@ private void DeserializeClip(AtomAnimationClip clip, JSONClass clipJSON, Animata
paramJSON.HasKey("Min") ? (float?)paramJSON["Min"].AsFloat : null,
paramJSON.HasKey("Max") ? (float?)paramJSON["Max"].AsFloat : null
);
var target = clip.Add(floatParamRef);
var target = clip.AddFloatParam(floatParamRef);
if (target == null)
{
SuperController.LogError(
Expand All @@ -288,7 +288,7 @@ private void DeserializeClip(AtomAnimationClip clip, JSONClass clipJSON, Animata
var triggerTrackRef = targetsRegistry.GetOrCreateTriggerTrack(clip.animationLayerQualifiedId, triggerTrackName);
//NOTE: We are cheating here, the saved setting is on each track but the animatable itself will have the setting
triggerTrackRef.live = triggerLive;
var target = clip.Add(triggerTrackRef);
var target = clip.AddTriggers(triggerTrackRef);
if (target == null)
{
target = clip.targetTriggers.FirstOrDefault(t => t.name == triggerTrackName);
Expand Down
6 changes: 3 additions & 3 deletions src/UI/Screens/AddRemoveTargetsScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void AddTrack(TriggersTrackRef track)
if (clip.targetTriggers.Any(t => t.TargetsSameAs(track))) continue;
var target = new TriggersTrackAnimationTarget(track, animation.logger);
target.AddEdgeFramesIfMissing(clip.animationLength);
clip.Add(target);
clip.AddTriggers(target);
}
}

Expand Down Expand Up @@ -446,7 +446,7 @@ private void AddAnimatedController()

foreach (var clip in currentLayer)
{
var added = clip.Add(animation.animatables.GetOrCreateController(controller, atom == plugin.containingAtom), targetsPosition == null, targetsRotation == null);
var added = clip.AddController(animation.animatables.GetOrCreateController(controller, atom == plugin.containingAtom), targetsPosition == null, targetsRotation == null);
if (added == null) continue;

var controllerPose = clip.pose?.GetControllerPose(controller.name);
Expand Down Expand Up @@ -520,7 +520,7 @@ private bool AddFloatParam(JSONStorable storable, JSONStorableFloat jsf)
foreach (var clip in currentLayer)
{
var storableFloat = animation.animatables.GetOrCreateStorableFloat(storable, jsf, storable.containingAtom == plugin.containingAtom);
var added = clip.Add(storableFloat);
var added = clip.AddFloatParam(storableFloat);
if (added == null) continue;

added.SetKeyframe(0f, jsf.val);
Expand Down
4 changes: 2 additions & 2 deletions src/UI/Screens/AddSegmentScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void CreateTransitionSegment()
{
foreach (var target in layer.targetControllers)
{
var added = clip.Add(target.animatableRef, target.targetsPosition, target.targetsRotation);
var added = clip.AddController(target.animatableRef, target.targetsPosition, target.targetsRotation);
if (added != null)
{
var snapshot = target.GetCurveSnapshot(layer.animationLength);
Expand All @@ -194,7 +194,7 @@ private void CreateTransitionSegment()
}
foreach (var target in layer.targetFloatParams)
{
var added =clip.Add(target.animatableRef);
var added =clip.AddFloatParam(target.animatableRef);
if (added != null)
{
var snapshot = target.GetCurveSnapshot(layer.animationLength);
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Screens/ControllerTargetSettingsScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void InitSplitPosRotUI()
if (target == null) continue;
var pos = target;
pos.targetsRotation = false;
var rot = clip.Add(pos.animatableRef, false, true);
var rot = clip.AddController(pos.animatableRef, false, true);
if (rot == null) throw new NullReferenceException("Could not add rotation controller");
rot.rotation.rotX.keys = new List<BezierKeyframe>(pos.rotation.rotX.keys);
rot.rotation.rotY.keys = new List<BezierKeyframe>(pos.rotation.rotY.keys);
Expand Down
Loading

0 comments on commit 3a43608

Please sign in to comment.