Terraria ModLoader  0.11.7.5
A mod to make and play Terraria mods
Terraria.ModLoader.Config.ReferenceDefaultsPreservingResolver Class Reference

Custom ContractResolver for facilitating refernce type defaults. The ShouldSerialize code enables unchanged-by-user reference type defaults to properly not serialize. The ValueProvider code helps during deserialization to not More...

+ Inheritance diagram for Terraria.ModLoader.Config.ReferenceDefaultsPreservingResolver:
+ Collaboration diagram for Terraria.ModLoader.Config.ReferenceDefaultsPreservingResolver:

Classes

class  NullToDefaultValueProvider
 
class  ValueProviderDecorator
 

Protected Member Functions

override IList< JsonProperty > CreateProperties (Type type, MemberSerialization memberSerialization)
 

Detailed Description

Custom ContractResolver for facilitating refernce type defaults. The ShouldSerialize code enables unchanged-by-user reference type defaults to properly not serialize. The ValueProvider code helps during deserialization to not

Definition at line 373 of file ConfigManager.cs.

Member Function Documentation

override IList<JsonProperty> Terraria.ModLoader.Config.ReferenceDefaultsPreservingResolver.CreateProperties ( Type  type,
MemberSerialization  memberSerialization 
)
protected

Definition at line 409 of file ConfigManager.cs.

References Terraria.ModLoader.Config.ConfigManager.ObjectEquals().

409  {
410  IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
411  if (type.IsClass) {
412  ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
413  if (ctor != null) {
414  object referenceInstance = ctor.Invoke(null);
415  foreach (JsonProperty prop in props.Where(p => p.Readable)) {
416  if (!prop.PropertyType.IsValueType) {
417  var a = type.GetMember(prop.PropertyName);
418  if (prop.Writable) {
419  if (prop.PropertyType.GetConstructor(Type.EmptyTypes) != null) {
420  // defaultValueCreator will create new instance, then get the value from a field in that object. Prevents deserialized nulls from sharing with other instances.
421  Func<object> defaultValueCreator = () => prop.ValueProvider.GetValue(ctor.Invoke(null));
422  prop.ValueProvider = new NullToDefaultValueProvider(prop.ValueProvider, defaultValueCreator);
423  }
424  else if (prop.PropertyType.IsArray) {
425  Func<object> defaultValueCreator = () => (prop.ValueProvider.GetValue(referenceInstance) as Array).Clone();
426  prop.ValueProvider = new NullToDefaultValueProvider(prop.ValueProvider, defaultValueCreator);
427  }
428  }
429  if (prop.ShouldSerialize == null)
430  prop.ShouldSerialize = instance =>
431  {
432  object val = prop.ValueProvider.GetValue(instance);
433  object refVal = prop.ValueProvider.GetValue(referenceInstance);
434  return !ConfigManager.ObjectEquals(val, refVal);
435  };
436  }
437  }
438  }
439  }
440  return props;
441  }

+ Here is the call graph for this function: