1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4  * This file is part of OpenRA, which is free software. It is made
5  * available to you under the terms of the GNU General Public License
6  * as published by the Free Software Foundation, either version 3 of
7  * the License, or (at your option) any later version. For more
8  * information, see COPYING.
9  */
10 #endregion
11 
12 using System.Diagnostics;
13 using System.Linq;
14 using OpenRA.Activities;
15 using OpenRA.Support;
16 
17 namespace OpenRA.Traits
18 {
19 	public static class ActivityUtils
20 	{
RunActivity(Actor self, Activity act)21 		public static Activity RunActivity(Actor self, Activity act)
22 		{
23 			// PERF: If there are no activities we can bail straight away and save ourselves a call to
24 			// Stopwatch.GetTimestamp.
25 			if (act == null)
26 				return act;
27 
28 			// PERF: This is a hot path and must run with minimal added overhead.
29 			// Calling Stopwatch.GetTimestamp is a bit expensive, so we enumerate manually to allow us to call it only
30 			// once per iteration in the normal case.
31 			// See also: DoTimed
32 			var longTickThresholdInStopwatchTicks = PerfTimer.LongTickThresholdInStopwatchTicks;
33 			var start = Stopwatch.GetTimestamp();
34 			while (act != null)
35 			{
36 				var prev = act;
37 				act = act.TickOuter(self);
38 				var current = Stopwatch.GetTimestamp();
39 				if (current - start > longTickThresholdInStopwatchTicks)
40 				{
41 					PerfTimer.LogLongTick(start, current, "Activity", prev);
42 					start = Stopwatch.GetTimestamp();
43 				}
44 				else
45 					start = current;
46 
47 				if (act == prev)
48 					break;
49 			}
50 
51 			return act;
52 		}
53 	}
54 }
55