SharpLibEar/Action.cs
author StephaneLenclud
Sat, 07 Jan 2017 20:21:42 +0100
changeset 277 71ba0dd622a5
parent 265 82e87f4956ea
permissions -rw-r--r--
Created Audio Manager class.
Clean up CScore audio usage.
Fixing broken audio device change handler.
Fixed various audio Dispose deadlock due to Invoke usage.
Thus now using BeginInvoke instead.
StephaneLenclud@210
     1
//
StephaneLenclud@210
     2
StephaneLenclud@210
     3
StephaneLenclud@210
     4
using System;
Stephane@212
     5
using System.Collections.Generic;
StephaneLenclud@253
     6
using System.Diagnostics;
StephaneLenclud@265
     7
using System.Linq;
StephaneLenclud@210
     8
using System.Runtime.Serialization;
StephaneLenclud@210
     9
using System.Threading;
StephaneLenclud@258
    10
using System.Threading.Tasks;
StephaneLenclud@210
    11
StephaneLenclud@210
    12
namespace SharpLib.Ear
StephaneLenclud@210
    13
{
StephaneLenclud@210
    14
    [DataContract]
StephaneLenclud@266
    15
    [AttributeObject(Id = "Action", Name = "Action Group", Description = "Use it to group other actions together.")]
StephaneLenclud@265
    16
    public class Action: Object
StephaneLenclud@210
    17
    {
StephaneLenclud@264
    18
        [DataMember]
StephaneLenclud@264
    19
        [AttributeObjectProperty
StephaneLenclud@264
    20
            (
StephaneLenclud@264
    21
            Id = "Action.Iterations",
StephaneLenclud@264
    22
            Name = "Iterations",
StephaneLenclud@264
    23
            Description = "Specifies the number of time this action should execute.",
StephaneLenclud@264
    24
            Minimum = "0",
StephaneLenclud@264
    25
            Maximum = "10000",
StephaneLenclud@264
    26
            Increment = "1"
StephaneLenclud@264
    27
            )
StephaneLenclud@264
    28
        ]
StephaneLenclud@264
    29
        public int Iterations { get; set; } = 1;
StephaneLenclud@264
    30
StephaneLenclud@264
    31
        /// <summary>
StephaneLenclud@264
    32
        /// 
StephaneLenclud@264
    33
        /// </summary>
StephaneLenclud@264
    34
        public bool Enabled
StephaneLenclud@264
    35
        {
StephaneLenclud@264
    36
            get { return Iterations > 0; }
StephaneLenclud@264
    37
        }
StephaneLenclud@264
    38
StephaneLenclud@266
    39
StephaneLenclud@266
    40
        public override bool IsValid()
StephaneLenclud@266
    41
        {            
StephaneLenclud@266
    42
            // We don't want to override this behaviour for derived classes
StephaneLenclud@266
    43
            if (GetType() == typeof(Action))
StephaneLenclud@266
    44
            {
StephaneLenclud@266
    45
                // Avoid having empty actions with no name
StephaneLenclud@266
    46
                return !string.IsNullOrEmpty(Name);
StephaneLenclud@266
    47
            }
StephaneLenclud@266
    48
StephaneLenclud@266
    49
            return base.IsValid();
StephaneLenclud@266
    50
        }
StephaneLenclud@266
    51
StephaneLenclud@266
    52
StephaneLenclud@265
    53
        /// <summary>
StephaneLenclud@265
    54
        /// Basic action just does nothing
StephaneLenclud@265
    55
        /// </summary>
StephaneLenclud@265
    56
        /// <returns></returns>
StephaneLenclud@265
    57
        protected virtual async Task DoExecute()
StephaneLenclud@265
    58
        {
StephaneLenclud@265
    59
            
StephaneLenclud@265
    60
        }
StephaneLenclud@223
    61
StephaneLenclud@231
    62
        /// <summary>
StephaneLenclud@231
    63
        /// Allows testing from generic edit dialog.
StephaneLenclud@231
    64
        /// </summary>
StephaneLenclud@231
    65
        public void Test()
StephaneLenclud@231
    66
        {
StephaneLenclud@253
    67
            Trace.WriteLine("Action test");
StephaneLenclud@231
    68
            Execute();
StephaneLenclud@231
    69
        }
StephaneLenclud@231
    70
StephaneLenclud@264
    71
        /// <summary>
StephaneLenclud@264
    72
        /// Execute our action N times.
StephaneLenclud@264
    73
        /// </summary>
StephaneLenclud@264
    74
        /// <returns></returns>
StephaneLenclud@258
    75
        public async Task Execute()
StephaneLenclud@223
    76
        {
StephaneLenclud@239
    77
            if (!IsValid())
StephaneLenclud@239
    78
            {
StephaneLenclud@264
    79
                Trace.WriteLine("EAR: Action.Execute: WARNING: Action invalid, aborting execution: " + Brief());
StephaneLenclud@239
    80
                return;
StephaneLenclud@239
    81
            }
StephaneLenclud@264
    82
StephaneLenclud@264
    83
            if (!Enabled)
StephaneLenclud@264
    84
            {
StephaneLenclud@264
    85
                Trace.WriteLine("EAR: Action.Execute: Action disabled: " + Brief());
StephaneLenclud@264
    86
                return;
StephaneLenclud@264
    87
            }
StephaneLenclud@264
    88
StephaneLenclud@264
    89
            for (int i = Iterations; i > 0; i--)
StephaneLenclud@264
    90
            {
StephaneLenclud@264
    91
                Trace.WriteLine($"EAR: Action.Execute: [{Iterations - i + 1}/{Iterations}] - {BriefBase()}");
StephaneLenclud@265
    92
                //For each iteration
StephaneLenclud@265
    93
                //We first execute ourselves
StephaneLenclud@264
    94
                await DoExecute();
StephaneLenclud@265
    95
StephaneLenclud@265
    96
                //Then our children
StephaneLenclud@265
    97
                foreach (Action a in Objects.OfType<Action>())
StephaneLenclud@265
    98
                {
StephaneLenclud@265
    99
                    await a.Execute();
StephaneLenclud@265
   100
                }
StephaneLenclud@264
   101
            }            
StephaneLenclud@223
   102
        }
StephaneLenclud@210
   103
StephaneLenclud@265
   104
        /// <summary>
StephaneLenclud@265
   105
        /// For inherited classes to override.
StephaneLenclud@265
   106
        /// </summary>
StephaneLenclud@265
   107
        /// <returns></returns>
StephaneLenclud@264
   108
        public virtual string BriefBase()
StephaneLenclud@264
   109
        {
StephaneLenclud@266
   110
            return string.IsNullOrEmpty(Name)? AttributeName : Name;
StephaneLenclud@264
   111
        }
StephaneLenclud@264
   112
StephaneLenclud@264
   113
        /// <summary>
StephaneLenclud@264
   114
        /// Dynamic object description.
StephaneLenclud@264
   115
        /// </summary>
StephaneLenclud@264
   116
        /// <returns></returns>
StephaneLenclud@264
   117
        public sealed override string Brief()
StephaneLenclud@264
   118
        {
StephaneLenclud@264
   119
            return Iterations > 1 ? $"{Iterations} x " + BriefBase():BriefBase();
StephaneLenclud@264
   120
        }
StephaneLenclud@264
   121
StephaneLenclud@264
   122
StephaneLenclud@264
   123
StephaneLenclud@264
   124
StephaneLenclud@264
   125
StephaneLenclud@264
   126
StephaneLenclud@210
   127
    }
StephaneLenclud@210
   128
StephaneLenclud@210
   129
StephaneLenclud@210
   130
}