Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Common.cs
Go to the documentation of this file.
6
8
9internal static class Common
10{
12
13 private static class CachedGenericDelegates<T>
14 {
15 internal static readonly Func<T> DefaultTResultFunc = () => default(T);
16
24 }
25
27
29
31
33
35
37
39 {
40 for (int num = 16; num > 0; num--)
41 {
42 if (!Thread.Yield() && predicate(stateIn, out stateOut))
43 {
44 return true;
45 }
46 }
47 stateOut = default(TStateOut);
48 return false;
49 }
50
51 internal static T UnwrapWeakReference<T>(object state) where T : class
52 {
54 if (!weakReference.TryGetTarget(out var target))
55 {
56 return null;
57 }
58 return target;
59 }
60
61 internal static int GetBlockId(IDataflowBlock block)
62 {
64 }
65
67 {
68 if (block == null)
69 {
70 return string.Empty;
71 }
72 string name = block.GetType().Name;
73 if (options == null)
74 {
75 return name;
76 }
78 try
79 {
80 return string.Format(options.NameFormat, name, blockId);
81 }
82 catch (Exception ex)
83 {
84 return ex.Message;
85 }
86 }
87
92
108
110 {
111 try
112 {
113 throw exception;
114 }
115 catch
116 {
117 return exception;
118 }
119 }
120
122 {
123 string text = messageValue as string;
124 if (text == null && messageValue != null)
125 {
126 try
127 {
128 text = messageValue.ToString();
129 }
130 catch
131 {
132 }
133 }
134 if (text == null)
135 {
136 return;
137 }
138 StoreStringIntoExceptionData(exc, "DataflowMessageValue", text);
140 {
141 return;
142 }
144 {
145 {
146 foreach (Exception innerException in ex.InnerExceptions)
147 {
148 StoreStringIntoExceptionData(innerException, "DataflowMessageValue", text);
149 }
150 return;
151 }
152 }
153 if (exc.InnerException != null)
154 {
155 StoreStringIntoExceptionData(exc.InnerException, "DataflowMessageValue", text);
156 }
157 }
158
159 private static void StoreStringIntoExceptionData(Exception exception, string key, string value)
160 {
161 try
162 {
163 IDictionary data = exception.Data;
164 if (data != null && !data.IsFixedSize && !data.IsReadOnly && data[key] == null)
165 {
166 data[key] = value;
167 }
168 }
169 catch
170 {
171 }
172 }
173
182
183 internal static void AddException([NotNull] ref List<Exception> list, Exception exception, bool unwrapInnerExceptions = false)
184 {
185 if (list == null)
186 {
187 list = new List<Exception>();
188 }
190 {
192 {
193 list.AddRange(ex.InnerExceptions);
194 }
195 else
196 {
197 list.Add(exception.InnerException);
198 }
199 }
200 else
201 {
202 list.Add(exception);
203 }
204 }
205
212
219
226
231
233 {
234 try
235 {
236 return block.Completion;
237 }
239 {
240 }
242 {
243 }
244 return null;
245 }
246
251
252 internal static bool IsValidTimeout(TimeSpan timeout)
253 {
254 long num = (long)timeout.TotalMilliseconds;
255 if (num >= -1)
256 {
257 return num <= int.MaxValue;
258 }
259 return false;
260 }
261
266
276
278 {
280 {
281 task.Start(scheduler);
282 return null;
283 }
285 }
286
288 {
289 Exception result = null;
290 try
291 {
292 task.Start(scheduler);
293 }
294 catch (Exception ex)
295 {
296 _ = task.Exception;
297 result = ex;
298 }
299 return result;
300 }
301
303 {
305 int num = 0;
307 while (postponedMessages.TryPop(out item))
308 {
309 try
310 {
311 if (item.Key.ReserveMessage(item.Value, target))
312 {
313 item.Key.ReleaseReservation(item.Value, target);
314 }
315 }
316 catch (Exception exception)
317 {
319 }
320 num++;
321 }
322 }
323
325 {
326 AggregateException ex = (sourceCompletionTask.IsFaulted ? sourceCompletionTask.Exception : null);
327 try
328 {
329 if (ex != null)
330 {
331 target.Fault(ex);
332 }
333 else
334 {
335 target.Complete();
336 }
337 }
338 catch (Exception obj)
339 {
340 if (exceptionHandler != null)
341 {
343 return;
344 }
345 throw;
346 }
347 }
348
356
358 {
359 if (sourceCompletionTask.IsCompleted)
360 {
362 }
363 else
364 {
366 }
367 }
368}
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
static ExceptionDispatchInfo Capture(Exception source)
static readonly Action< object, TargetRegistry< T >, ITargetBlock< T > > CreateUnlinkerShimAction
Definition Common.cs:17
static void ThrowAsync(Exception error)
Definition Common.cs:174
static readonly TaskCompletionSource< VoidResult > CompletedVoidResultTaskCompletionSource
Definition Common.cs:32
static bool TryKeepAliveUntil< TStateIn, TStateOut >(KeepAlivePredicate< TStateIn, TStateOut > predicate, TStateIn stateIn, [MaybeNullWhen(false)] out TStateOut stateOut)
Definition Common.cs:38
static bool IsCooperativeCancellation(Exception exception)
Definition Common.cs:88
static readonly Task< bool > CompletedTaskWithFalseResult
Definition Common.cs:30
static Exception StartTaskSafe(Task task, TaskScheduler scheduler)
Definition Common.cs:277
static Exception StartTaskSafeCore(Task task, TaskScheduler scheduler)
Definition Common.cs:287
static Task GetPotentiallyNotSupportedCompletionTask(IDataflowBlock block)
Definition Common.cs:232
static void PropagateCompletionAsContinuation(Task sourceCompletionTask, IDataflowBlock target)
Definition Common.cs:349
static TaskContinuationOptions GetContinuationOptions(TaskContinuationOptions toInclude=TaskContinuationOptions.None)
Definition Common.cs:262
static readonly Action< Exception > AsyncExceptionHandler
Definition Common.cs:36
static Task< TResult > CreateTaskFromCancellation< TResult >(CancellationToken cancellationToken)
Definition Common.cs:227
static T UnwrapWeakReference< T >(object state)
Definition Common.cs:51
static void ReleaseAllPostponedMessages< T >(ITargetBlock< T > target, QueuedMap< ISourceBlock< T >, DataflowMessageHeader > postponedMessages, ref List< Exception > exceptions)
Definition Common.cs:302
static bool IsValidTimeout(TimeSpan timeout)
Definition Common.cs:252
static void StoreDataflowMessageValueIntoExceptionData< T >(Exception exc, T messageValue, bool targetInnerExceptions=false)
Definition Common.cs:121
static Task< bool > CreateCachedBooleanTask(bool value)
Definition Common.cs:206
static void PropagateCompletionOnceCompleted(Task sourceCompletionTask, IDataflowBlock target)
Definition Common.cs:357
delegate bool KeepAlivePredicate< TStateIn, TStateOut >(TStateIn stateIn, out TStateOut stateOut)
static readonly Task< bool > CompletedTaskWithTrueResult
Definition Common.cs:28
static TaskCreationOptions GetCreationOptionsForTask(bool isReplacementReplica=false)
Definition Common.cs:267
static void PropagateCompletion(Task sourceCompletionTask, IDataflowBlock target, Action< Exception > exceptionHandler)
Definition Common.cs:324
static TaskCompletionSource< T > CreateCachedTaskCompletionSource< T >()
Definition Common.cs:213
static string GetNameForDebugger(IDataflowBlock block, DataflowBlockOptions options=null)
Definition Common.cs:66
static IDisposable CreateUnlinker< TOutput >(object outgoingLock, TargetRegistry< TOutput > targetRegistry, ITargetBlock< TOutput > targetBlock)
Definition Common.cs:247
static void AddException([NotNull] ref List< Exception > list, Exception exception, bool unwrapInnerExceptions=false)
Definition Common.cs:183
static readonly DataflowMessageHeader SingleMessageHeader
Definition Common.cs:26
static Exception InitializeStackTrace(Exception exception)
Definition Common.cs:109
static void WireCancellationToComplete(CancellationToken cancellationToken, Task completionTask, Action< object > completeAction, object completeState)
Definition Common.cs:93
static int GetBlockId(IDataflowBlock block)
Definition Common.cs:61
static Task< TResult > CreateTaskFromException< TResult >(Exception exception)
Definition Common.cs:220
static void StoreStringIntoExceptionData(Exception exception, string key, string value)
Definition Common.cs:159
static readonly TimeSpan InfiniteTimeSpan
Definition Common.cs:34
static bool QueueUserWorkItem(WaitCallback callBack)
static bool Yield()
Definition Thread.cs:412
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5