You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IPA.Config.Stores.Attributes
  8. {
  9. /// <summary>
  10. /// Indicates that the generated subclass of the attribute's target should implement <see cref="INotifyPropertyChanged"/>.
  11. /// If the type this is applied to already inherits it, this is implied.
  12. /// </summary>
  13. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
  14. public sealed class NotifyPropertyChangesAttribute : Attribute { }
  15. /// <summary>
  16. /// Causes a field or property in an object being wrapped by <see cref="GeneratedStore.Generated{T}(Config, bool)"/> to be
  17. /// ignored during serialization and deserialization.
  18. /// </summary>
  19. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  20. public sealed class IgnoreAttribute : Attribute { }
  21. /// <summary>
  22. /// Indicates that a field or property in an object being wrapped by <see cref="GeneratedStore.Generated{T}(Config, bool)"/>
  23. /// that would otherwise be nullable (i.e. a reference type or a <see cref="Nullable{T}"/> type) should never be null, and the
  24. /// member will be ignored if the deserialized value is <see langword="null"/>.
  25. /// </summary>
  26. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  27. public sealed class NonNullableAttribute : Attribute { }
  28. /// <summary>
  29. /// Indicates that a given field or property in an object being wrapped by <see cref="GeneratedStore.Generated{T}(Config, bool)"/>
  30. /// should be serialized and deserialized using the provided converter instead of the default mechanism.
  31. /// </summary>
  32. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  33. public sealed class UseConverterAttribute : Attribute
  34. {
  35. /// <summary>
  36. /// Gets the type of the converter to use.
  37. /// </summary>
  38. public Type ConverterType { get; }
  39. /// <summary>
  40. /// Gets the target type of the converter if it is avaliable at instantiation time, otherwise
  41. /// <see langword="null"/>.
  42. /// </summary>
  43. public Type ConverterTargetType { get; }
  44. /// <summary>
  45. /// Gets whether or not this converter is a generic <see cref="ValueConverter{T}"/>.
  46. /// </summary>
  47. public bool IsGenericConverter => ConverterTargetType != null;
  48. /// <summary>
  49. /// Creates a new <see cref="UseConverterAttribute"/> with a given <see cref="ConverterType"/>.
  50. /// </summary>
  51. /// <param name="converterType">the type to assign to <see cref="ConverterType"/></param>
  52. public UseConverterAttribute(Type converterType)
  53. {
  54. ConverterType = converterType;
  55. var baseT = ConverterType.BaseType;
  56. while (baseT != null && baseT != typeof(object) &&
  57. (!baseT.IsGenericType || baseT.GetGenericTypeDefinition() != typeof(ValueConverter<>)))
  58. baseT = baseT.BaseType;
  59. if (baseT == typeof(object)) ConverterTargetType = null;
  60. else ConverterTargetType = baseT.GetGenericArguments()[0];
  61. var implInterface = ConverterType.GetInterfaces().Contains(typeof(IValueConverter));
  62. if (ConverterTargetType == null && !implInterface) throw new ArgumentException("Type is not a value converter!");
  63. }
  64. }
  65. /// <summary>
  66. /// Specifies a name for the serialized field or property in an object being wrapped by
  67. /// <see cref="GeneratedStore.Generated{T}(Config, bool)"/> that is different from the member name itself.
  68. /// </summary>
  69. /// <example>
  70. /// <para>
  71. /// When serializing the following object, we might get the JSON that follows.
  72. /// <code>
  73. /// public class PluginConfig
  74. /// {
  75. /// public virtual bool BooleanField { get; set; } = true;
  76. /// }
  77. /// </code>
  78. /// <code>
  79. /// {
  80. /// "BooleanField": true
  81. /// }
  82. /// </code>
  83. /// </para>
  84. /// <para>
  85. /// However, if we were to add a <see cref="SerializedNameAttribute"/> to that field, we would get the following.
  86. /// <code>
  87. /// public class PluginConfig
  88. /// {
  89. /// [SerializedName("bool")]
  90. /// public virtual bool BooleanField { get; set; } = true;
  91. /// }
  92. /// </code>
  93. /// <code>
  94. /// {
  95. /// "bool": true
  96. /// }
  97. /// </code>
  98. /// </para>
  99. /// </example>
  100. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  101. public sealed class SerializedNameAttribute : Attribute
  102. {
  103. /// <summary>
  104. /// Gets the name to replace the member name with.
  105. /// </summary>
  106. public string Name { get; private set; }
  107. /// <summary>
  108. /// Creates a new <see cref="SerializedNameAttribute"/> with the given <see cref="Name"/>.
  109. /// </summary>
  110. /// <param name="name">the value to assign to <see cref="Name"/></param>
  111. public SerializedNameAttribute(string name)
  112. {
  113. Name = name;
  114. }
  115. }
  116. }