summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Events/ManagedEvent.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/Events/ManagedEvent.cs')
-rw-r--r--src/SMAPI/Framework/Events/ManagedEvent.cs71
1 files changed, 36 insertions, 35 deletions
diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs
index dfdd7449..60e5c599 100644
--- a/src/SMAPI/Framework/Events/ManagedEvent.cs
+++ b/src/SMAPI/Framework/Events/ManagedEvent.cs
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using PerformanceCounterManager = StardewModdingAPI.Framework.PerformanceCounter.PerformanceCounterManager;
+using StardewModdingAPI.Framework.PerformanceCounter;
namespace StardewModdingAPI.Framework.Events
{
/// <summary>An event wrapper which intercepts and logs errors in handler code.</summary>
/// <typeparam name="TEventArgs">The event arguments type.</typeparam>
- internal class ManagedEvent<TEventArgs>: IManagedEvent
+ internal class ManagedEvent<TEventArgs> : IManagedEvent
{
/*********
** Fields
@@ -15,9 +15,6 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>The underlying event.</summary>
private event EventHandler<TEventArgs> Event;
- /// <summary>A human-readable name for the event.</summary>
- private readonly string EventName;
-
/// <summary>Writes messages to the log.</summary>
private readonly IMonitor Monitor;
@@ -30,8 +27,19 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>The cached invocation list.</summary>
private EventHandler<TEventArgs>[] CachedInvocationList;
- /// <summary>The performance counter manager.</summary>
- private readonly PerformanceCounterManager PerformanceCounterManager;
+ /// <summary>Tracks performance metrics.</summary>
+ private readonly PerformanceMonitor PerformanceMonitor;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>A human-readable name for the event.</summary>
+ public string EventName { get; }
+
+ /// <summary>Whether the event is typically called at least once per second.</summary>
+ public bool IsPerformanceCritical { get; }
+
/*********
** Public methods
@@ -40,19 +48,15 @@ namespace StardewModdingAPI.Framework.Events
/// <param name="eventName">A human-readable name for the event.</param>
/// <param name="monitor">Writes messages to the log.</param>
/// <param name="modRegistry">The mod registry with which to identify mods.</param>
- /// <param name="performanceCounterManager">The performance counter manager</param>
- public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry, PerformanceCounterManager performanceCounterManager)
+ /// <param name="performanceMonitor">Tracks performance metrics.</param>
+ /// <param name="isPerformanceCritical">Whether the event is typically called at least once per second.</param>
+ public ManagedEvent(string eventName, IMonitor monitor, ModRegistry modRegistry, PerformanceMonitor performanceMonitor, bool isPerformanceCritical = false)
{
this.EventName = eventName;
this.Monitor = monitor;
this.ModRegistry = modRegistry;
- this.PerformanceCounterManager = performanceCounterManager;
- }
-
- /// <summary>Gets the event name.</summary>
- public string GetName()
- {
- return this.EventName;
+ this.PerformanceMonitor = performanceMonitor;
+ this.IsPerformanceCritical = isPerformanceCritical;
}
/// <summary>Get whether anything is listening to the event.</summary>
@@ -93,22 +97,20 @@ namespace StardewModdingAPI.Framework.Events
return;
- this.PerformanceCounterManager.BeginTrackInvocation(this.EventName);
-
- foreach (EventHandler<TEventArgs> handler in this.CachedInvocationList)
+ this.PerformanceMonitor.Track(this.EventName, () =>
{
- try
- {
- this.PerformanceCounterManager.Track(this.EventName, this.GetModNameForPerformanceCounters(handler),
- () => handler.Invoke(null, args));
- }
- catch (Exception ex)
+ foreach (EventHandler<TEventArgs> handler in this.CachedInvocationList)
{
- this.LogError(handler, ex);
+ try
+ {
+ this.PerformanceMonitor.Track(this.EventName, this.GetModNameForPerformanceCounters(handler), () => handler.Invoke(null, args));
+ }
+ catch (Exception ex)
+ {
+ this.LogError(handler, ex);
+ }
}
- }
-
- this.PerformanceCounterManager.EndTrackInvocation(this.EventName);
+ });
}
/// <summary>Raise the event and notify all handlers.</summary>
@@ -139,18 +141,17 @@ namespace StardewModdingAPI.Framework.Events
/*********
** Private methods
*********/
-
+ /// <summary>Get the mod name for a given event handler to display in performance monitoring reports.</summary>
+ /// <param name="handler">The event handler.</param>
private string GetModNameForPerformanceCounters(EventHandler<TEventArgs> handler)
{
IModMetadata mod = this.GetSourceMod(handler);
-
if (mod == null)
- {
return Constants.GamePerformanceCounterName;
- }
-
- return mod.HasManifest() ? mod.Manifest.UniqueID : mod.DisplayName;
+ return mod.HasManifest()
+ ? mod.Manifest.UniqueID
+ : mod.DisplayName;
}
/// <summary>Track an event handler.</summary>