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.

105 lines
6.3 KiB

  1. using IPA.Config.Stores.Attributes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace IPA.Config.Stores
  9. {
  10. /// <summary>
  11. /// A class providing an extension for <see cref="Config"/> to make it easy to use generated
  12. /// config stores.
  13. /// </summary>
  14. public static class GeneratedStore
  15. {
  16. /// <summary>
  17. /// The name of the assembly that internals must be visible to to allow internal protection.
  18. /// </summary>
  19. public const string AssemblyVisibilityTarget = GeneratedStoreImpl.GeneratedAssemblyName;
  20. /// <summary>
  21. /// Creates a generated <see cref="IConfigStore"/> of type <typeparamref name="T"/>, registers it to
  22. /// the <see cref="Config"/> object, and returns it. This also forces a synchronous config load via
  23. /// <see cref="Config.LoadSync"/> if <paramref name="loadSync"/> is <see langword="true"/>.
  24. /// </summary>
  25. /// <remarks>
  26. /// <para>
  27. /// <typeparamref name="T"/> must be a public non-<see langword="sealed"/> class.
  28. /// It can also be internal, but in that case, then your assembly must have the following attribute
  29. /// to allow the generated code to reference it.
  30. /// <code lang="csharp">
  31. /// [assembly: InternalsVisibleTo(IPA.Config.Stores.GeneratedStore.AssemblyVisibilityTarget)]
  32. /// </code>
  33. /// </para>
  34. /// <para>
  35. /// Only fields and properties that are public or protected will be considered, and only properties
  36. /// where both the getter and setter are public or protected are considered. Any fields or properties
  37. /// with an <see cref="IgnoreAttribute"/> applied to them are also ignored. Having properties be <see langword="virtual"/> is not strictly
  38. /// necessary, however it allows the generated type to keep track of changes and lock around them so that the config will auto-save.
  39. /// </para>
  40. /// <para>
  41. /// All of the attributes in the <see cref="Attributes"/> namespace are handled as described by them.
  42. /// </para>
  43. /// <para>
  44. /// If the <typeparamref name="T"/> declares a public or protected, <see langword="virtual"/>
  45. /// method <c>Changed()</c>, then that method may be called to artificially signal to the runtime that the content of the object
  46. /// has changed. That method will also be called after the write locks are released when a property is set anywhere in the owning
  47. /// tree. This will only be called on the outermost generated object of the config structure, even if the change being signaled
  48. /// is somewhere deep into the tree.
  49. /// </para>
  50. /// <para>
  51. /// Similarly, <typeparamref name="T"/> can declare a public or protected, <see langword="virtual"/>
  52. /// method <c>OnReload()</c>, which will be called on the filesystem reader thread after the object has been repopulated with new data
  53. /// values. It will be called <i>after</i> the write lock for this object is released. This will only be called on the outermost generated
  54. /// object of the config structure.
  55. /// </para>
  56. /// <para>
  57. /// Similarly, <typeparamref name="T"/> can declare a public or protected, <see langword="virtual"/>
  58. /// method <c>CopyFrom(ConfigType)</c> (the first parameter is the type it is defined on), which may be called to copy the properties from
  59. /// another object of its type easily, and more importantly, as only one change. Its body will be executed after the values have been copied.
  60. /// </para>
  61. /// <para>
  62. /// Similarly, <typeparamref name="T"/> can declare a public or protected, <see langword="virtual"/>
  63. /// method <c>ChangeTransaction()</c> returning <see cref="IDisposable"/>, which may be called to get an object representing a transactional
  64. /// change. This may be used to change a lot of properties at once without triggering a save multiple times. Ideally, this is used in a
  65. /// <see langword="using"/> block or declaration. The <see cref="IDisposable"/> returned from your implementation will have its
  66. /// <see cref="IDisposable.Dispose"/> called <i>after</i> <c>Changed()</c> is called, but <i>before</i> the write lock is released.
  67. /// Unless you have a very good reason to use the nested <see cref="IDisposable"/>, avoid it.
  68. /// </para>
  69. /// <para>
  70. /// If <typeparamref name="T"/> is marked with <see cref="NotifyPropertyChangesAttribute"/>, the resulting object will implement
  71. /// <see cref="INotifyPropertyChanged"/>. Similarly, if <typeparamref name="T"/> implements <see cref="INotifyPropertyChanged"/>,
  72. /// the resulting object will implement it and notify it too.
  73. /// </para>
  74. /// </remarks>
  75. /// <typeparam name="T">the type to wrap</typeparam>
  76. /// <param name="cfg">the <see cref="Config"/> to register to</param>
  77. /// <param name="loadSync">whether to synchronously load the content, or trigger an async load</param>
  78. /// <returns>a generated instance of <typeparamref name="T"/> as a special <see cref="IConfigStore"/></returns>
  79. public static T Generated<T>(this Config cfg, bool loadSync = true) where T : class
  80. {
  81. var ret = GeneratedStoreImpl.Create<T>();
  82. cfg.SetStore(ret as IConfigStore);
  83. if (loadSync)
  84. cfg.LoadSync();
  85. else
  86. cfg.LoadAsync();
  87. return ret;
  88. }
  89. /// <summary>
  90. /// Creates a generated store outside of the context of the config system.
  91. /// </summary>
  92. /// <remarks>
  93. /// See <see cref="Generated{T}(Config, bool)"/> for more information about how it behaves.
  94. /// </remarks>
  95. /// <typeparam name="T">the type to wrap</typeparam>
  96. /// <returns>a generated instance of <typeparamref name="T"/> implementing functionality described by <see cref="Generated{T}(Config, bool)"/></returns>
  97. /// <seealso cref="Generated{T}(Config, bool)"/>
  98. public static T Create<T>() where T : class
  99. => GeneratedStoreImpl.Create<T>();
  100. }
  101. }