ConfigDefinition.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. namespace BepInEx.Configuration
  3. {
  4. /// <summary>
  5. /// Section and key of a setting. Used as a unique key for identification within a <see cref="T:BepInEx.Configuration.ConfigFile" />.
  6. /// The same definition can be used in multiple config files, it will point to different settings then.
  7. /// </summary>
  8. /// <inheritdoc />
  9. public class ConfigDefinition : IEquatable<ConfigDefinition>
  10. {
  11. /// <summary>
  12. /// Group of the setting. All settings within a config file are grouped by this.
  13. /// </summary>
  14. public string Section { get; }
  15. /// <summary>
  16. /// Name of the setting.
  17. /// </summary>
  18. public string Key { get; }
  19. /// <summary>
  20. /// Create a new definition. Definitions with same section and key are equal.
  21. /// </summary>
  22. /// <param name="section">Group of the setting, case sensitive.</param>
  23. /// <param name="key">Name of the setting, case sensitive.</param>
  24. public ConfigDefinition(string section, string key)
  25. {
  26. Key = key ?? throw new ArgumentNullException(nameof(key));
  27. Section = section ?? throw new ArgumentNullException(nameof(section));
  28. }
  29. /// <inheritdoc />
  30. [Obsolete("description argument is no longer used, put it in a ConfigDescription instead")]
  31. public ConfigDefinition(string section, string key, string description) : this(section, key) { }
  32. /// <summary>
  33. /// Check if the definitions are the same.
  34. /// </summary>
  35. /// <inheritdoc />
  36. public bool Equals(ConfigDefinition other)
  37. {
  38. if (other == null) return false;
  39. return string.Equals(Key, other.Key)
  40. && string.Equals(Section, other.Section);
  41. }
  42. /// <summary>
  43. /// Check if the definitions are the same.
  44. /// </summary>
  45. public override bool Equals(object obj)
  46. {
  47. if (ReferenceEquals(null, obj))
  48. return false;
  49. if (ReferenceEquals(this, obj))
  50. return true;
  51. return Equals(obj as ConfigDefinition);
  52. }
  53. /// <inheritdoc />
  54. public override int GetHashCode()
  55. {
  56. unchecked
  57. {
  58. int hashCode = Key != null ? Key.GetHashCode() : 0;
  59. hashCode = (hashCode * 397) ^ (Section != null ? Section.GetHashCode() : 0);
  60. return hashCode;
  61. }
  62. }
  63. /// <summary>
  64. /// Check if the definitions are the same.
  65. /// </summary>
  66. public static bool operator ==(ConfigDefinition left, ConfigDefinition right)
  67. => Equals(left, right);
  68. /// <summary>
  69. /// Check if the definitions are the same.
  70. /// </summary>
  71. public static bool operator !=(ConfigDefinition left, ConfigDefinition right)
  72. => !Equals(left, right);
  73. /// <inheritdoc />
  74. public override string ToString()
  75. {
  76. return Section + "." + Key;
  77. }
  78. }
  79. }