Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectManager.cs
Go to the documentation of this file.
3
5
6public class ObjectManager
7{
8 private static readonly FieldInfo s_nullableValueField = typeof(Nullable<>).GetField("value", BindingFlags.Instance | BindingFlags.NonPublic);
9
10 private DeserializationEventHandler _onDeserializationHandler;
11
12 private SerializationEventHandler _onDeserializedHandler;
13
15
16 internal object _topObject;
17
19
20 internal long _fixupCount;
21
22 internal readonly ISurrogateSelector _selector;
23
24 internal readonly StreamingContext _context;
25
26 internal object? TopObject
27 {
28 get
29 {
30 return _topObject;
31 }
32 set
33 {
35 }
36 }
37
39
41 {
42 _objects = new ObjectHolder[16];
43 _selector = selector;
44 _context = context;
45 }
46
47 private bool CanCallGetType(object obj)
48 {
49 return true;
50 }
51
53 {
54 int num = (int)(objectID & 0xFFFFF);
55 if (num >= _objects.Length)
56 {
57 return null;
58 }
61 {
62 if (objectHolder._id == objectID)
63 {
64 return objectHolder;
65 }
66 }
67 return objectHolder;
68 }
69
80
82 {
83 if (holder._id >= _objects.Length && _objects.Length != 1048576)
84 {
85 int num = 1048576;
86 if (holder._id < 524288)
87 {
88 num = _objects.Length * 2;
89 while (num <= holder._id && num < 1048576)
90 {
91 num *= 2;
92 }
93 if (num > 1048576)
94 {
95 num = 1048576;
96 }
97 }
98 ObjectHolder[] array = new ObjectHolder[num];
100 _objects = array;
101 }
102 int num2 = (int)(holder._id & 0xFFFFF);
103 ObjectHolder next = _objects[num2];
104 holder._next = next;
106 }
107
109 {
110 member = fixup._fixupInfo;
112 if (holder == null || holder.CanObjectValueChange || holder.ObjectValue == null)
113 {
114 if (bThrowIfMissing)
115 {
116 if (holder == null)
117 {
119 }
120 if (holder.IsIncompleteObjectReference)
121 {
123 }
125 }
126 return false;
127 }
128 if (!holder.CompletelyFixed && holder.ObjectValue != null && holder.ObjectValue is ValueType)
129 {
131 return false;
132 }
133 return true;
134 }
135
136 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
138 {
139 ISurrogateSelector selector = null;
140 if (holder.HasSurrogate)
141 {
143 object obj = surrogate.SetObjectData(holder.ObjectValue, holder.SerializationInfo, _context, selector);
144 if (obj != null)
145 {
146 if (!holder.CanSurrogatedObjectValueChange && obj != holder.ObjectValue)
147 {
149 }
150 holder.SetObjectValue(obj, this);
151 }
152 holder._surrogate = null;
153 holder.SetFlags();
154 }
155 else
156 {
157 CompleteISerializableObject(holder.ObjectValue, holder.SerializationInfo, _context);
158 }
161 if (holder.RequiresValueTypeFixup && holder.ValueTypeFixupPerformed)
162 {
163 DoValueTypeFixup(null, holder, holder.ObjectValue);
164 }
166 }
167
169 {
170 int num = 0;
171 try
172 {
173 object objectValue;
174 do
175 {
176 objectValue = holder.ObjectValue;
177 holder.SetObjectValue(((IObjectReference)holder.ObjectValue).GetRealObject(_context), this);
178 if (holder.ObjectValue == null)
179 {
180 holder.SetObjectValue(objectValue, this);
181 return false;
182 }
183 if (num++ == 100)
184 {
186 }
187 }
188 while (holder.ObjectValue is IObjectReference && objectValue != holder.ObjectValue);
189 }
191 {
192 return false;
193 }
196 return true;
197 }
198
200 {
201 FieldInfo[] array = new FieldInfo[4];
202 int num = 0;
203 int[] array2 = null;
204 object objectValue = holder.ObjectValue;
205 while (holder.RequiresValueTypeFixup)
206 {
207 if (num + 1 >= array.Length)
208 {
210 Array.Copy(array, array3, array.Length);
211 array = array3;
212 }
214 objectValue = holder.ObjectValue;
215 if (valueFixup.ParentField != null)
216 {
217 FieldInfo parentField = valueFixup.ParentField;
219 if (objectHolder.ObjectValue == null)
220 {
221 break;
222 }
224 if (nullableValueField != null)
225 {
227 num++;
228 }
229 array[num] = parentField;
231 num++;
232 continue;
233 }
234 holder = FindObjectHolder(valueFixup.ContainerID);
235 array2 = valueFixup.ParentIndex;
236 break;
237 }
238 if (!(holder.ObjectValue is Array) && holder.ObjectValue != null)
239 {
240 objectValue = holder.ObjectValue;
241 }
242 if (num != 0)
243 {
244 FieldInfo[] array4 = new FieldInfo[num];
245 for (int i = 0; i < num; i++)
246 {
247 FieldInfo fieldInfo = array[num - 1 - i];
249 array4[i] = ((serializationFieldInfo == null) ? fieldInfo : serializationFieldInfo.FieldInfo);
250 }
252 if (memberToFix != null)
253 {
254 memberToFix.SetValueDirect(typedReference, value);
255 }
256 else
257 {
259 }
260 }
261 else if (memberToFix != null)
262 {
264 }
265 if (array2 != null && holder.ObjectValue != null)
266 {
267 ((Array)holder.ObjectValue).SetValue(objectValue, array2);
268 }
269 return true;
270 }
271
273 {
274 if (Nullable.GetUnderlyingType(type) != null)
275 {
276 return (FieldInfo)type.GetMemberWithSameMetadataDefinitionAs(s_nullableValueField);
277 }
278 return null;
279 }
280
282 {
283 FixupHolderList missingElements = holder._missingElements;
284 object member = null;
285 ObjectHolder holder2 = null;
286 int num = 0;
287 if (holder.ObjectValue == null)
288 {
290 }
291 if (missingElements == null)
292 {
293 return;
294 }
295 if (holder.HasSurrogate || holder.HasISerializable)
296 {
298 if (serInfo == null)
299 {
301 }
302 if (missingElements != null)
303 {
304 for (int i = 0; i < missingElements._count; i++)
305 {
306 if (missingElements._values[i] != null && GetCompletionInfo(missingElements._values[i], out holder2, out member, bObjectFullyComplete))
307 {
308 object objectValue = holder2.ObjectValue;
310 {
311 serInfo.UpdateValue((string)member, objectValue, objectValue.GetType());
312 }
313 else
314 {
315 serInfo.UpdateValue((string)member, objectValue, typeof(MarshalByRefObject));
316 }
317 num++;
318 missingElements._values[i] = null;
320 {
321 holder.DecrementFixupsRemaining(this);
322 holder2.RemoveDependency(holder._id);
323 }
324 }
325 }
326 }
327 }
328 else
329 {
330 for (int j = 0; j < missingElements._count; j++)
331 {
334 {
335 continue;
336 }
337 if (holder2.TypeLoadExceptionReachable)
338 {
339 holder.TypeLoadException = holder2.TypeLoadException;
340 if (holder.Reachable)
341 {
342 throw new SerializationException(System.SR.Format(System.SR.Serialization_TypeLoadFailure, holder.TypeLoadException.TypeName));
343 }
344 }
345 if (holder.Reachable)
346 {
347 holder2.Reachable = true;
348 }
349 switch (fixupHolder._fixupType)
350 {
351 case 1:
352 if (holder.RequiresValueTypeFixup)
353 {
355 }
356 ((Array)holder.ObjectValue).SetValue(holder2.ObjectValue, (int[])member);
357 break;
358 case 2:
359 {
362 {
363 if (holder.RequiresValueTypeFixup && holder.ValueTypeFixupPerformed)
364 {
366 {
368 }
369 }
370 else
371 {
373 }
374 if (holder2.RequiresValueTypeFixup)
375 {
377 }
378 break;
379 }
381 }
382 default:
384 }
385 num++;
386 missingElements._values[j] = null;
388 {
389 holder.DecrementFixupsRemaining(this);
390 holder2.RemoveDependency(holder._id);
391 }
392 }
393 }
394 _fixupCount -= num;
395 if (missingElements._count == num)
396 {
398 }
399 }
400
402 {
403 if (holder.CanObjectValueChange)
404 {
405 return;
406 }
407 LongList dependentObjects = holder.DependentObjects;
408 if (dependentObjects == null)
409 {
410 return;
411 }
413 while (dependentObjects.MoveNext())
414 {
416 objectHolder.DecrementFixupsRemaining(this);
417 if (objectHolder.DirectlyDependentObjects == 0)
418 {
419 if (objectHolder.ObjectValue != null)
420 {
422 }
423 else
424 {
425 objectHolder.MarkForCompletionWhenAvailable();
426 }
427 }
428 }
429 }
430
431 public virtual object? GetObject(long objectID)
432 {
433 if (objectID <= 0)
434 {
436 }
438 if (objectHolder == null || objectHolder.CanObjectValueChange)
439 {
440 return null;
441 }
442 return objectHolder.ObjectValue;
443 }
444
445 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
446 public virtual void RegisterObject(object obj, long objectID)
447 {
448 RegisterObject(obj, objectID, null, 0L, null);
449 }
450
451 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
453 {
454 RegisterObject(obj, objectID, info, 0L, null);
455 }
456
457 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
459 {
461 }
462
464 {
467 }
468
469 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
470 public void RegisterObject(object obj, long objectID, SerializationInfo? info, long idOfContainingObj, MemberInfo? member, int[]? arrayIndex)
471 {
472 if (obj == null)
473 {
474 throw new ArgumentNullException("obj");
475 }
476 if (objectID <= 0)
477 {
479 }
480 if (member != null && !(member is FieldInfo))
481 {
483 }
485 if (_selector != null)
486 {
489 }
491 {
492 DeserializationEventHandler handler = ((IDeserializationCallback)obj).OnDeserialization;
493 AddOnDeserialization(handler);
494 }
495 if (arrayIndex != null)
496 {
497 arrayIndex = (int[])arrayIndex.Clone();
498 }
500 if (objectHolder == null)
501 {
504 if (objectHolder.RequiresDelayedFixup)
505 {
507 }
509 return;
510 }
511 if (objectHolder.ObjectValue != null)
512 {
514 }
515 objectHolder.UpdateData(obj, info, surrogate, idOfContainingObj, (FieldInfo)member, arrayIndex, this);
516 if (objectHolder.DirectlyDependentObjects > 0)
517 {
519 }
520 if (objectHolder.RequiresDelayedFixup)
521 {
523 }
524 if (objectHolder.CompletelyFixed)
525 {
528 }
529 if (objectHolder.TotalDependentObjects > 0)
530 {
532 }
533 else
534 {
536 }
537 }
538
539 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
541 {
542 if (obj == null)
543 {
544 throw new ArgumentNullException("obj");
545 }
546 if (!(obj is ISerializable))
547 {
549 }
550 Type type = obj.GetType();
552 try
553 {
555 }
557 {
559 }
560 deserializationConstructor.Invoke(obj, new object[2] { info, context });
561 }
562
564 {
565 ConstructorInfo[] constructors = t.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
567 {
568 ParameterInfo[] parameters = constructorInfo.GetParameters();
569 if (parameters.Length == 2 && parameters[0].ParameterType == typeof(SerializationInfo) && parameters[1].ParameterType == typeof(StreamingContext))
570 {
571 return constructorInfo;
572 }
573 }
575 }
576
577 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
578 public virtual void DoFixups()
579 {
580 int num = -1;
581 while (num != 0)
582 {
583 num = 0;
585 while (fixupEnumerator.MoveNext())
586 {
587 ObjectHolder current = fixupEnumerator.Current;
588 if (current.ObjectValue == null)
589 {
591 }
592 if (current.TotalDependentObjects == 0)
593 {
594 if (current.RequiresSerInfoFixup)
595 {
596 FixupSpecialObject(current);
597 num++;
598 }
599 else if (!current.IsIncompleteObjectReference)
600 {
601 CompleteObject(current, bObjectFullyComplete: true);
602 }
604 {
605 num++;
606 }
607 }
608 }
609 }
610 if (_fixupCount == 0L)
611 {
613 {
615 }
616 return;
617 }
618 for (int i = 0; i < _objects.Length; i++)
619 {
620 for (ObjectHolder current = _objects[i]; current != null; current = current._next)
621 {
622 if (current.TotalDependentObjects > 0)
623 {
624 CompleteObject(current, bObjectFullyComplete: true);
625 }
626 }
627 if (_fixupCount == 0L)
628 {
629 return;
630 }
631 }
633 }
634
647
648 public virtual void RecordFixup(long objectToBeFixed, MemberInfo member, long objectRequired)
649 {
650 if (objectToBeFixed <= 0 || objectRequired <= 0)
651 {
652 throw new ArgumentOutOfRangeException((objectToBeFixed <= 0) ? "objectToBeFixed" : "objectRequired", System.SR.Serialization_IdTooSmall);
653 }
654 if (member == null)
655 {
656 throw new ArgumentNullException("member");
657 }
658 if (!(member is FieldInfo))
659 {
661 }
664 }
665
666 public virtual void RecordDelayedFixup(long objectToBeFixed, string memberName, long objectRequired)
667 {
668 if (objectToBeFixed <= 0 || objectRequired <= 0)
669 {
670 throw new ArgumentOutOfRangeException((objectToBeFixed <= 0) ? "objectToBeFixed" : "objectRequired", System.SR.Serialization_IdTooSmall);
671 }
672 if (memberName == null)
673 {
674 throw new ArgumentNullException("memberName");
675 }
678 }
679
680 public virtual void RecordArrayElementFixup(long arrayToBeFixed, int index, long objectRequired)
681 {
683 }
684
685 public virtual void RecordArrayElementFixup(long arrayToBeFixed, int[] indices, long objectRequired)
686 {
687 if (arrayToBeFixed <= 0 || objectRequired <= 0)
688 {
689 throw new ArgumentOutOfRangeException((arrayToBeFixed <= 0) ? "arrayToBeFixed" : "objectRequired", System.SR.Serialization_IdTooSmall);
690 }
691 if (indices == null)
692 {
693 throw new ArgumentNullException("indices");
694 }
697 }
698
699 public virtual void RaiseDeserializationEvent()
700 {
702 _onDeserializationHandler?.Invoke(null);
703 }
704
705 internal virtual void AddOnDeserialization(DeserializationEventHandler handler)
706 {
708 }
709
710 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
716
717 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
723
724 [RequiresUnreferencedCode("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
730}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
static void SerializationSetValue(MemberInfo fi, object target, object value)
ObjectHolderListEnumerator GetFixupEnumerator()
bool GetCompletionInfo(FixupHolder fixup, [NotNullWhen(true)] out ObjectHolder holder, out object member, bool bThrowIfMissing)
DeserializationEventHandler _onDeserializationHandler
void CompleteObject(ObjectHolder holder, bool bObjectFullyComplete)
virtual void RegisterObject(object obj, long objectID)
ObjectManager(ISurrogateSelector? selector, StreamingContext context)
void RegisterObject(object obj, long objectID, SerializationInfo info)
virtual void AddOnDeserialization(DeserializationEventHandler handler)
virtual void RaiseOnDeserializedEvent(object obj)
virtual void RecordDelayedFixup(long objectToBeFixed, string memberName, long objectRequired)
void RegisterObject(object obj, long objectID, SerializationInfo? info, long idOfContainingObj, MemberInfo? member, int[]? arrayIndex)
virtual void RecordArrayElementFixup(long arrayToBeFixed, int index, long objectRequired)
ObjectHolder FindOrCreateObjectHolder(long objectID)
bool ResolveObjectReference(ObjectHolder holder)
static FieldInfo GetNullableValueField(Type type)
void RegisterFixup(FixupHolder fixup, long objectToBeFixed, long objectRequired)
void CompleteISerializableObject(object obj, SerializationInfo info, StreamingContext context)
void RegisterString(string obj, long objectID, SerializationInfo info, long idOfContainingObj, MemberInfo member)
static ConstructorInfo GetDeserializationConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type t)
bool DoValueTypeFixup(FieldInfo memberToFix, ObjectHolder holder, object value)
static readonly FieldInfo s_nullableValueField
void FixupSpecialObject(ObjectHolder holder)
void AddObjectHolder(ObjectHolder holder)
readonly ISurrogateSelector _selector
SerializationEventHandler _onDeserializedHandler
virtual void RecordFixup(long objectToBeFixed, MemberInfo member, long objectRequired)
void DoNewlyRegisteredObjectFixups(ObjectHolder holder)
ObjectHolder FindObjectHolder(long objectID)
virtual ? object GetObject(long objectID)
void RegisterObject(object obj, long objectID, SerializationInfo? info, long idOfContainingObj, MemberInfo? member)
virtual void RecordArrayElementFixup(long arrayToBeFixed, int[] indices, long objectRequired)
static SerializationEvents GetSerializationEventsForType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t)
static string Serialization_NotCyclicallyReferenceableSurrogate
Definition SR.cs:36
static string Serialization_InvalidFixupDiscovered
Definition SR.cs:42
static string Serialization_IdTooSmall
Definition SR.cs:64
static string Serialization_IORIncomplete
Definition SR.cs:32
static string Serialization_ConstructorNotFound
Definition SR.cs:58
static string Serialization_InvalidFixupType
Definition SR.cs:62
static string Serialization_ObjectNotSupplied
Definition SR.cs:34
static string Serialization_TooManyReferences
Definition SR.cs:38
static string ArgumentOutOfRange_ObjectID
Definition SR.cs:52
static string Serialization_InvalidType
Definition SR.cs:1886
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Serialization_MissingObject
Definition SR.cs:40
static string Serialization_ValueTypeFixup
Definition SR.cs:46
static string Serialization_IncorrectNumberOfFixups
Definition SR.cs:60
static string Serialization_PartialValueTypeFixup
Definition SR.cs:48
static string Serialization_NeverSeen
Definition SR.cs:30
static string Serialization_NotISer
Definition SR.cs:56
static string Serialization_UnknownMemberInfo
Definition SR.cs:22
static string Serialization_UnableToFixup
Definition SR.cs:50
static string Serialization_TypeLoadFailure
Definition SR.cs:44
static string Serialization_RegisterTwice
Definition SR.cs:54
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
ConstructorInfo[] GetConstructors()
Definition Type.cs:580
object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector? selector)
ISerializationSurrogate? GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
delegate void DeserializationEventHandler(object sender)
static ? Type GetUnderlyingType(Type nullableType)
Definition Nullable.cs:139
static unsafe TypedReference MakeTypedReference(object target, FieldInfo[] flds)
static void SetTypedReference(TypedReference target, object? value)