ConfigDefinition.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. namespace BepInEx.Configuration
  3. {
  4. public class ConfigDescription
  5. {
  6. public ConfigDescription(string description, Type settingType, object defaultValue)
  7. {
  8. Description = description ?? throw new ArgumentNullException(nameof(description));
  9. SettingType = settingType ?? throw new ArgumentNullException(nameof(settingType));
  10. DefaultValue = defaultValue;
  11. if(defaultValue == null && settingType.IsByRef)
  12. throw new ArgumentException("defaultValue is null while settingType is a value type");
  13. if(defaultValue != null && !settingType.IsInstanceOfType(defaultValue))
  14. throw new ArgumentException("defaultValue can not be assigned to type " + settingType.Name);
  15. }
  16. public string Description { get; }
  17. public Type SettingType { get; }
  18. public object DefaultValue { get; }
  19. //todo value range
  20. }
  21. public class ConfigDefinition : IEquatable<ConfigDefinition>
  22. {
  23. public string Section { get; }
  24. public string Key { get; }
  25. public ConfigDefinition(string section, string key)
  26. {
  27. Key = key;
  28. Section = section;
  29. }
  30. public bool Equals(ConfigDefinition other)
  31. {
  32. if (other == null) return false;
  33. return string.Equals(Key, other.Key)
  34. && string.Equals(Section, other.Section);
  35. }
  36. public override bool Equals(object obj)
  37. {
  38. if (ReferenceEquals(null, obj))
  39. return false;
  40. if (ReferenceEquals(this, obj))
  41. return true;
  42. return Equals(obj as ConfigDefinition);
  43. }
  44. public override int GetHashCode()
  45. {
  46. unchecked
  47. {
  48. int hashCode = Key != null ? Key.GetHashCode() : 0;
  49. hashCode = (hashCode * 397) ^ (Section != null ? Section.GetHashCode() : 0);
  50. return hashCode;
  51. }
  52. }
  53. public static bool operator ==(ConfigDefinition left, ConfigDefinition right)
  54. => Equals(left, right);
  55. public static bool operator !=(ConfigDefinition left, ConfigDefinition right)
  56. => !Equals(left, right);
  57. }
  58. }