No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StateAnalyser.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. namespace Unity.VisualScripting
  3. {
  4. [Analyser(typeof(IState))]
  5. public class StateAnalyser<TState> : Analyser<TState, StateAnalysis>
  6. where TState : class, IState
  7. {
  8. public StateAnalyser(GraphReference reference, TState target) : base(reference, target) { }
  9. public TState state => target;
  10. [Assigns]
  11. protected virtual bool IsEntered()
  12. {
  13. using (var recursion = Recursion.New(1))
  14. {
  15. return IsEntered(state, recursion);
  16. }
  17. }
  18. [Assigns]
  19. protected virtual IEnumerable<Warning> Warnings()
  20. {
  21. if (!IsEntered())
  22. {
  23. yield return Warning.Info("State is never entered.");
  24. }
  25. }
  26. private bool IsEntered(IState state, Recursion recursion)
  27. {
  28. if (state.isStart)
  29. {
  30. return true;
  31. }
  32. if (!recursion?.TryEnter(state) ?? false)
  33. {
  34. return false;
  35. }
  36. foreach (var incomingTransition in state.incomingTransitions)
  37. {
  38. if (IsEntered(incomingTransition.source, recursion) && incomingTransition.Analysis<StateTransitionAnalysis>(context).isTraversed)
  39. {
  40. recursion?.Exit(state);
  41. return true;
  42. }
  43. }
  44. recursion?.Exit(state);
  45. return false;
  46. }
  47. }
  48. }