Terraria ModLoader  0.11.5
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 365 of file ConfigManager.cs.

Member Function Documentation

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

Definition at line 401 of file ConfigManager.cs.

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

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

+ Here is the call graph for this function: