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.

76 lines
3.3 KiB

  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using IPA.Config.Data;
  5. namespace IPA.Config
  6. {
  7. /// <summary>
  8. /// An interface for configuration providers.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// Implementers must provide a default constructor. Do not assume that <see cref="File"/> will ever be set for a given object.
  13. /// </para>
  14. /// <para>
  15. /// Implementers are expected to preserve the typing of values passed to <see cref="Store"/> when returned from <see cref="Load"/>.
  16. /// The only exceptions to this are the numeric types, <see cref="Integer"/> and <see cref="FloatingPoint"/>, since they can be coerced
  17. /// to each other with <see cref="Integer.AsFloat"/> and <see cref="FloatingPoint.AsInteger"/> respectively. The provider <i>should</i>
  18. /// however store and recover <see cref="Integer"/> with as much precision as is possible. For example, a JSON provider may decide to
  19. /// decode all numbers that have an integral value, even if they were originally <see cref="FloatingPoint"/>, as <see cref="Integer"/>.
  20. /// This is reasonable, as <see cref="Integer"/> is more precise, particularly with larger values, than <see cref="FloatingPoint"/>.
  21. /// </para>
  22. /// </remarks>
  23. public interface IConfigProvider
  24. {
  25. /// <summary>
  26. /// Gets the extension <i>without</i> a dot to use for files handled by this provider.
  27. /// </summary>
  28. /// <remarks>
  29. /// This must work immediately, and is used to generate the <see cref="FileInfo"/> used to set
  30. /// <see cref="File"/>.
  31. /// </remarks>
  32. string Extension { get; }
  33. /// <summary>
  34. /// Stores the <see cref="Value"/> given to disk in the format specified.
  35. /// </summary>
  36. /// <param name="value">the <see cref="Value"/> to store</param>
  37. /// <param name="file">the file to write to</param>
  38. void Store(Value value, FileInfo file);
  39. /// <summary>
  40. /// Loads a <see cref="Value"/> from disk in whatever format this provider provides
  41. /// and returns it.
  42. /// </summary>
  43. /// <param name="file">the file to read from</param>
  44. /// <returns>the <see cref="Value"/> loaded</returns>
  45. Value Load(FileInfo file);
  46. }
  47. /// <summary>
  48. /// A wrapper for an <see cref="IConfigProvider"/> and the <see cref="FileInfo"/> to use with it.
  49. /// </summary>
  50. public class ConfigProvider // this *should* be a struct imo, but mono doesn't seem to like that
  51. {
  52. private readonly FileInfo file;
  53. private readonly IConfigProvider provider;
  54. internal ConfigProvider(FileInfo file, IConfigProvider provider)
  55. {
  56. this.file = file; this.provider = provider;
  57. }
  58. /// <summary>
  59. /// Stores the <see cref="Value"/> given to disk in the format specified.
  60. /// </summary>
  61. /// <param name="value">the <see cref="Value"/> to store</param>
  62. public void Store(Value value) => provider.Store(value, file);
  63. /// <summary>
  64. /// Loads a <see cref="Value"/> from disk in whatever format this provider provides
  65. /// and returns it.
  66. /// </summary>
  67. /// <returns>the <see cref="Value"/> loaded</returns>
  68. public Value Load() => provider.Load(file);
  69. }
  70. }