|
7 | 7 |
|
8 | 8 | namespace MonkeyLoader.Events |
9 | 9 | { |
| 10 | + /// <summary> |
| 11 | + /// Represents the abstract base for the concrete dispatcher types. |
| 12 | + /// </summary> |
| 13 | + /// <typeparam name="TSource">The type of the event sources.</typeparam> |
| 14 | + /// <typeparam name="THandler">The type of the event handlers.</typeparam> |
10 | 15 | internal abstract class EventDispatcherBase<TSource, THandler> : IEventDispatcher |
11 | 16 | where THandler : class, IPrioritizable |
12 | 17 | { |
13 | 18 | protected readonly AnyMap eventDispatchers = new(); |
14 | 19 | protected readonly PrioritySortedCollection<THandler> handlers = []; |
15 | 20 | protected readonly Dictionary<Mod, Dictionary<Type, HashSet<TSource>>> sourcesByMod = []; |
| 21 | + |
16 | 22 | private readonly Dictionary<Mod, HashSet<THandler>> _handlersByMod = []; |
17 | 23 | private readonly EventManager _manager; |
18 | 24 | private readonly MethodInfo _removeSourceMethod; |
| 25 | + |
19 | 26 | protected Logger Logger => _manager.Logger; |
20 | 27 |
|
| 28 | + /// <summary> |
| 29 | + /// Creates a new dispatcher with the given details. |
| 30 | + /// </summary> |
| 31 | + /// <param name="manager">The manager that this dispatcher belongs to.</param> |
| 32 | + /// <param name="removeSourceMethod">The generic <c>RemoveSource</c> implementation of the concrete dispatcher.</param> |
21 | 33 | protected EventDispatcherBase(EventManager manager, MethodInfo removeSourceMethod) |
22 | 34 | { |
23 | 35 | _manager = manager; |
24 | 36 | _removeSourceMethod = removeSourceMethod; |
25 | 37 | } |
26 | 38 |
|
| 39 | + /// <summary> |
| 40 | + /// Adds the <paramref name="handler"/> from the given <paramref name="mod"/> to this dispatcher. |
| 41 | + /// </summary> |
| 42 | + /// <param name="mod">The mod that the <paramref name="handler"/> comes from.</param> |
| 43 | + /// <param name="handler">The <typeparamref name="THandler"/> to add.</param> |
| 44 | + /// <returns><c>true</c> if the handler was newly added; otherwise, <c>false</c>.</returns> |
27 | 45 | public bool AddHandler(Mod mod, THandler handler) |
28 | 46 | { |
29 | 47 | if (_handlersByMod.GetOrCreateValue(mod).Add(handler)) |
30 | 48 | { |
31 | 49 | handlers.Add(handler); |
| 50 | + Logger.Debug(() => $"Added handler [{handler.GetType().CompactDescription()}] to event source [{typeof(TSource).CompactDescription()}] for mod: {mod}!"); |
| 51 | + |
32 | 52 | return true; |
33 | 53 | } |
34 | 54 |
|
| 55 | + Logger.Warn(() => $"Tried to add duplicate handler [{handler.GetType().CompactDescription()}] to event source [{typeof(TSource).CompactDescription()}] for mod: {mod}!"); |
| 56 | + |
35 | 57 | return false; |
36 | 58 | } |
37 | 59 |
|
| 60 | + /// <summary> |
| 61 | + /// Removes the <typeparamref name="THandler"/> from the given <paramref name="mod"/> from this dispatcher. |
| 62 | + /// </summary> |
| 63 | + /// <param name="mod">The mod that the <paramref name="handler"/> comes from.</param> |
| 64 | + /// <param name="handler">The <typeparamref name="THandler"/> to remove.</param> |
| 65 | + /// <returns><c>true</c> if the handler was found and removed; otherwise, <c>false</c>.</returns> |
38 | 66 | public bool RemoveHandler(Mod mod, THandler handler) |
39 | 67 | { |
40 | 68 | if (_handlersByMod.TryGetValue(mod, out var modHandlers)) |
41 | 69 | { |
42 | | - modHandlers.Remove(handler); |
43 | | - handlers.Remove(handler); |
| 70 | + if (modHandlers.Remove(handler)) |
| 71 | + { |
| 72 | + handlers.Remove(handler); |
| 73 | + Logger.Debug(() => $"Removed handler [{handler.GetType().CompactDescription()}] from event source [{typeof(TSource).CompactDescription()}] for mod: {mod}!"); |
44 | 74 |
|
45 | | - return true; |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + Logger.Warn(() => $"Tried to remove missing handler [{handler.GetType().CompactDescription()}] from event source [{typeof(TSource).CompactDescription()}] for missing mod: {mod}!"); |
| 79 | + |
| 80 | + return false; |
46 | 81 | } |
47 | 82 |
|
| 83 | + Logger.Warn(() => $"Tried to remove handler [{handler.GetType().CompactDescription()}] from event source [{typeof(TSource).CompactDescription()}] for missing mod: {mod}!"); |
| 84 | + |
48 | 85 | return false; |
49 | 86 | } |
50 | 87 |
|
| 88 | + /// <summary> |
| 89 | + /// Removes all <typeparamref name="TSource"/>s and <typeparamref name="THandler"/>s |
| 90 | + /// of the given <paramref name="mod"/> from this dispatcher. |
| 91 | + /// </summary> |
| 92 | + /// <param name="mod">The mod that the sources and handlers come from.</param> |
51 | 93 | public void UnregisterMod(Mod mod) |
52 | 94 | { |
| 95 | + Logger.Debug(() => $"Removing all {typeof(TSource).CompactDescription()} sources of mod: {mod}!"); |
| 96 | + |
53 | 97 | if (sourcesByMod.TryGetValue(mod, out var modSourcesByType)) |
54 | 98 | { |
| 99 | + List<Action> sourceRemovals = []; |
| 100 | + |
55 | 101 | foreach (var typeModSources in modSourcesByType) |
56 | 102 | { |
| 103 | + Logger.Debug(() => $"Removing sources of event type: {typeModSources.Key.CompactDescription()}!"); |
| 104 | + |
57 | 105 | var removeSource = _removeSourceMethod.MakeGenericMethod(typeModSources.Key); |
58 | 106 |
|
59 | 107 | foreach (var source in typeModSources.Value) |
60 | | - removeSource.Invoke(this, [mod, source]); |
| 108 | + { |
| 109 | + Logger.Trace(() => $"Removing concrete source: {source!.GetType().CompactDescription()}!"); |
| 110 | + |
| 111 | + sourceRemovals.Add(() => removeSource.Invoke(this, [mod, source])); |
| 112 | + } |
61 | 113 | } |
| 114 | + |
| 115 | + foreach (var removeSource in sourceRemovals) |
| 116 | + removeSource(); |
62 | 117 | } |
63 | 118 |
|
64 | 119 | sourcesByMod.Remove(mod); |
65 | 120 | _handlersByMod.Remove(mod); |
66 | 121 | } |
67 | 122 |
|
| 123 | + /// <summary> |
| 124 | + /// Adds the <paramref name="source"/> for the <paramref name="eventType"/> |
| 125 | + /// from the given <paramref name="mod"/> to this dispatcher. |
| 126 | + /// </summary> |
| 127 | + /// <param name="mod">The mod that the <paramref name="source"/> comes from.</param> |
| 128 | + /// <param name="eventType">The type of the event that the source is for.</param> |
| 129 | + /// <param name="source">The <typeparamref name="TSource"/> to add.</param> |
| 130 | + /// <returns><c>true</c> if the source was newly added; otherwise, <c>false</c>.</returns> |
68 | 131 | protected bool AddSource(Mod mod, Type eventType, TSource source) |
69 | | - => sourcesByMod.GetOrCreateValue(mod).GetOrCreateValue(eventType).Add(source); |
| 132 | + { |
| 133 | + if (sourcesByMod.GetOrCreateValue(mod).GetOrCreateValue(eventType).Add(source)) |
| 134 | + { |
| 135 | + Logger.Debug(() => $"Added source [{source?.GetType().CompactDescription()}] for event [{eventType.CompactDescription()}] for mod: {mod}!"); |
70 | 136 |
|
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + Logger.Warn(() => $"Tried to add duplicate source [{source?.GetType().CompactDescription()}] for event [{eventType.CompactDescription()}] for mod: {mod}!"); |
| 141 | + |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Removes the <paramref name="source"/> for the <paramref name="eventType"/> |
| 147 | + /// from the given <paramref name="mod"/> from this dispatcher. |
| 148 | + /// </summary> |
| 149 | + /// <param name="mod">The mod that the <paramref name="source"/> comes from.</param> |
| 150 | + /// <param name="eventType">The type of the event that the source is for.</param> |
| 151 | + /// <param name="source">The <typeparamref name="TSource"/> to remove.</param> |
| 152 | + /// <returns><c>true</c> if the source was found and removed; otherwise, <c>false</c>.</returns> |
71 | 153 | protected bool RemoveSource(Mod mod, Type eventType, TSource source) |
72 | 154 | { |
73 | 155 | if (sourcesByMod.TryGetValue(mod, out var modSourcesByType)) |
74 | 156 | { |
75 | 157 | if (modSourcesByType.TryGetValue(eventType, out var modTypeSources)) |
76 | | - modTypeSources.Remove(source); |
77 | | - return true; |
| 158 | + { |
| 159 | + if (modTypeSources.Remove(source)) |
| 160 | + { |
| 161 | + Logger.Debug(() => $"Removed source [{source!.GetType().CompactDescription()}] of event [{eventType.CompactDescription()}] for mod: {mod}!"); |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + Logger.Warn(() => $"Tried to remove missing source [{source!.GetType().CompactDescription()}] of event [{eventType.CompactDescription()}] for mod: {mod}!"); |
| 167 | + |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + Logger.Warn(() => $"Tried to remove source [{source!.GetType().CompactDescription()}] of missing event [{eventType.CompactDescription()}] for mod: {mod}!"); |
| 172 | + |
| 173 | + return false; |
78 | 174 | } |
79 | 175 |
|
| 176 | + Logger.Warn(() => $"Tried to remove source [{source!.GetType().CompactDescription()}] of event [{eventType.CompactDescription()}] for missing mod: {mod}!"); |
| 177 | + |
80 | 178 | return false; |
81 | 179 | } |
82 | 180 | } |
|
0 commit comments