Terraria ModLoader  0.11.4
A framework for 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 363 of file ConfigManager.cs.

Member Function Documentation

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

Definition at line 399 of file ConfigManager.cs.

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

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

+ Here is the call graph for this function: