ConfigDefinition.cs 3.1 KB

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