Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EventListener.cs
Go to the documentation of this file.
4
6
8{
11
12 internal volatile EventListener m_Next;
13
14 internal static EventListener s_Listeners;
15
17
18 private static bool s_CreatingListener;
19
20 internal static object EventListenersLock
21 {
22 get
23 {
24 if (s_EventSources == null)
25 {
27 }
28 return s_EventSources;
29 }
30 }
31
44
46
51
53 {
55 {
56 args.EventSource.AddListener((EventListener)obj);
57 });
58 }
59
60 public virtual void Dispose()
61 {
63 {
64 if (s_Listeners == null)
65 {
66 return;
67 }
68 if (this == s_Listeners)
69 {
73 return;
74 }
76 EventListener next;
77 while (true)
78 {
79 next = eventListener.m_Next;
80 if (next == null)
81 {
82 return;
83 }
84 if (next == this)
85 {
86 break;
87 }
88 eventListener = next;
89 }
90 eventListener.m_Next = next.m_Next;
92 }
93 }
94
95 public void EnableEvents(EventSource eventSource, EventLevel level)
96 {
97 EnableEvents(eventSource, level, EventKeywords.None);
98 }
99
100 public void EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword)
101 {
102 EnableEvents(eventSource, level, matchAnyKeyword, null);
103 }
104
106 {
107 if (eventSource == null)
108 {
109 throw new ArgumentNullException("eventSource");
110 }
111 eventSource.SendCommand(this, EventProviderType.None, 0, 0, EventCommand.Update, enable: true, level, matchAnyKeyword, arguments);
112 if (eventSource.GetType() == typeof(NativeRuntimeEventSource))
113 {
114 EventPipeEventDispatcher.Instance.SendCommand(this, EventCommand.Update, enable: true, level, matchAnyKeyword);
115 }
116 }
117
118 public void DisableEvents(EventSource eventSource)
119 {
120 if (eventSource == null)
121 {
122 throw new ArgumentNullException("eventSource");
123 }
124 eventSource.SendCommand(this, EventProviderType.None, 0, 0, EventCommand.Update, enable: false, EventLevel.LogAlways, EventKeywords.None, null);
125 if (eventSource.GetType() == typeof(NativeRuntimeEventSource))
126 {
127 EventPipeEventDispatcher.Instance.SendCommand(this, EventCommand.Update, enable: false, EventLevel.LogAlways, EventKeywords.None);
128 }
129 }
130
131 public static int EventSourceIndex(EventSource eventSource)
132 {
133 return eventSource.m_id;
134 }
135
136 protected internal virtual void OnEventSourceCreated(EventSource eventSource)
137 {
139 if (eventSourceCreated != null)
140 {
142 eventSourceCreatedEventArgs.EventSource = eventSource;
144 }
145 }
146
147 protected internal virtual void OnEventWritten(EventWrittenEventArgs eventData)
148 {
149 this.EventWritten?.Invoke(this, eventData);
150 }
151
153 {
155 {
156 int num = -1;
157 if (s_EventSources.Count % 64 == 63)
158 {
159 int num2 = s_EventSources.Count;
160 while (0 < num2)
161 {
162 num2--;
164 if (!weakReference.TryGetTarget(out var _))
165 {
166 num = num2;
167 weakReference.SetTarget(newEventSource);
168 break;
169 }
170 }
171 }
172 if (num < 0)
173 {
174 num = s_EventSources.Count;
176 }
177 newEventSource.m_id = num;
178 for (EventListener next = s_Listeners; next != null; next = next.m_Next)
179 {
180 newEventSource.AddListener(next);
181 }
182 }
183 }
184
185 internal static void DisposeOnShutdown()
186 {
189 {
191 {
192 if (s_EventSource.TryGetTarget(out var target))
193 {
194 list.Add(target);
195 }
196 }
197 }
198 foreach (EventSource item in list)
199 {
200 item.Dispose();
201 }
202 }
203
205 {
207 {
208 if (!s_EventSource.TryGetTarget(out var target))
209 {
210 continue;
211 }
212 if (target.m_Dispatchers.m_Listener == listenerToRemove)
213 {
214 target.m_Dispatchers = target.m_Dispatchers.m_Next;
215 continue;
216 }
217 EventDispatcher eventDispatcher = target.m_Dispatchers;
218 while (true)
219 {
220 EventDispatcher next = eventDispatcher.m_Next;
221 if (next == null)
222 {
223 break;
224 }
225 if (next.m_Listener == listenerToRemove)
226 {
227 eventDispatcher.m_Next = next.m_Next;
228 break;
229 }
230 eventDispatcher = next;
231 }
232 }
234 }
235
237 {
239 {
241 {
243 }
244 try
245 {
246 s_CreatingListener = true;
248 {
250 s_Listeners = this;
251 }
252 if (callback == null)
253 {
254 return;
255 }
258 {
259 if (weakReference.TryGetTarget(out var target))
260 {
262 eventSourceCreatedEventArgs.EventSource = target;
263 callback(this, eventSourceCreatedEventArgs);
264 }
265 }
266 }
267 finally
268 {
269 s_CreatingListener = false;
270 }
271 }
272 }
273}
void Add(TKey key, TValue value)
static ? Delegate Remove(Delegate? source, Delegate? value)
Definition Delegate.cs:463
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
virtual void OnEventSourceCreated(EventSource eventSource)
void CallBackForExistingEventSources(bool addToListenersList, EventHandler< EventSourceCreatedEventArgs > callback)
EventHandler< EventSourceCreatedEventArgs > _EventSourceCreated
void EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword, IDictionary< string, string?>? arguments)
EventHandler< EventSourceCreatedEventArgs >? EventSourceCreated
virtual void OnEventWritten(EventWrittenEventArgs eventData)
static void AddEventSource(EventSource newEventSource)
static List< WeakReference< EventSource > > s_EventSources
void EnableEvents(EventSource eventSource, EventLevel level)
void DisableEvents(EventSource eventSource)
EventHandler< EventWrittenEventArgs >? EventWritten
void EnableEvents(EventSource eventSource, EventLevel level, EventKeywords matchAnyKeyword)
static void RemoveReferencesToListenerInEventSources(EventListener listenerToRemove)
static int EventSourceIndex(EventSource eventSource)
static readonly EventPipeEventDispatcher Instance
static void SendCommand(EventSource eventSource, EventCommand command, IDictionary< string, string?>? commandArguments)
static void KeepAlive(object? obj)
Definition GC.cs:180
Definition GC.cs:8
static string EventSource_ListenerCreatedInsideCallback
Definition SR.cs:1236
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)