ConfigDefinition.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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)
  42. {
  43. Key = key ?? "";
  44. Section = section ?? "";
  45. }
  46. /// <summary>
  47. /// Check if the definitions are the same.
  48. /// </summary>
  49. /// <inheritdoc />
  50. public bool Equals(ConfigDefinition other)
  51. {
  52. if (other == null) return false;
  53. return string.Equals(Key, other.Key)
  54. && string.Equals(Section, other.Section);
  55. }
  56. /// <summary>
  57. /// Check if the definitions are the same.
  58. /// </summary>
  59. public override bool Equals(object obj)
  60. {
  61. if (ReferenceEquals(null, obj))
  62. return false;
  63. if (ReferenceEquals(this, obj))
  64. return true;
  65. return Equals(obj as ConfigDefinition);
  66. }
  67. /// <inheritdoc />
  68. public override int GetHashCode()
  69. {
  70. unchecked
  71. {
  72. int hashCode = Key != null ? Key.GetHashCode() : 0;
  73. hashCode = (hashCode * 397) ^ (Section != null ? Section.GetHashCode() : 0);
  74. return hashCode;
  75. }
  76. }
  77. /// <summary>
  78. /// Check if the definitions are the same.
  79. /// </summary>
  80. public static bool operator ==(ConfigDefinition left, ConfigDefinition right)
  81. => Equals(left, right);
  82. /// <summary>
  83. /// Check if the definitions are the same.
  84. /// </summary>
  85. public static bool operator !=(ConfigDefinition left, ConfigDefinition right)
  86. => !Equals(left, right);
  87. /// <inheritdoc />
  88. public override string ToString()
  89. {
  90. return Section + "." + Key;
  91. }
  92. }
  93. }