// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2026 Kybernetik // using System; using System.Collections.Generic; using UnityEngine; namespace Animancer { /// /// A system which protects the /// from checking the same object multiple times. /// /// https://kybernetik.com.au/animancer/api/Animancer/AnimationGathererRecursionGuard /// public struct AnimationGathererRecursionGuard : IDisposable { /************************************************************************************************************************/ /// The maximum number of recursive fields to check through before stopping. public static int MaxFieldDepth = 7; /************************************************************************************************************************/ /// Types which will be skipped when attempting to gather animations from an unknown object type. public static readonly HashSet DontGatherFrom = new(); /************************************************************************************************************************/ private static readonly HashSet ObjectsChecked = new(); private static int _GuardCount; /************************************************************************************************************************/ /// Call this with a using statement before calling . public static AnimationGathererRecursionGuard Begin() { _GuardCount++; return default; } /************************************************************************************************************************/ /// Ends a block started by . public readonly void Dispose() { _GuardCount--; if (_GuardCount == 0) ObjectsChecked.Clear(); } /************************************************************************************************************************/ /// Stores the specified object and returns true if it wasn't already stored. public static bool HasCheckedObject(object obj) { if (_GuardCount <= 0) Debug.LogError( $"{nameof(AnimationGathererRecursionGuard)} is being used without {nameof(Begin)} being caled"); return !ObjectsChecked.Add(obj); } /************************************************************************************************************************/ } }