ConfigDefinition.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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;
  27. Section = section;
  28. }
  29. /// <summary>
  30. /// Check if the definitions are the same.
  31. /// </summary>
  32. /// <inheritdoc />
  33. public bool Equals(ConfigDefinition other)
  34. {
  35. if (other == null) return false;
  36. return string.Equals(Key, other.Key)
  37. && string.Equals(Section, other.Section);
  38. }
  39. /// <summary>
  40. /// Check if the definitions are the same.
  41. /// </summary>
  42. public override bool Equals(object obj)
  43. {
  44. if (ReferenceEquals(null, obj))
  45. return false;
  46. if (ReferenceEquals(this, obj))
  47. return true;
  48. return Equals(obj as ConfigDefinition);
  49. }
  50. /// <inheritdoc />
  51. public override int GetHashCode()
  52. {
  53. unchecked
  54. {
  55. int hashCode = Key != null ? Key.GetHashCode() : 0;
  56. hashCode = (hashCode * 397) ^ (Section != null ? Section.GetHashCode() : 0);
  57. return hashCode;
  58. }
  59. }
  60. /// <summary>
  61. /// Check if the definitions are the same.
  62. /// </summary>
  63. public static bool operator ==(ConfigDefinition left, ConfigDefinition right)
  64. => Equals(left, right);
  65. /// <summary>
  66. /// Check if the definitions are the same.
  67. /// </summary>
  68. public static bool operator !=(ConfigDefinition left, ConfigDefinition right)
  69. => !Equals(left, right);
  70. /// <inheritdoc />
  71. public override string ToString()
  72. {
  73. return Section + " / " + Key;
  74. }
  75. }
  76. }