CVarControl.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Content.Shared.Administration;
  2. using Robust.Shared.Reflection;
  3. namespace Content.Shared.CCVar.CVarAccess;
  4. /// <summary>
  5. /// Manages what admin flags can change the cvar value. With optional mins and maxes.
  6. /// </summary>
  7. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
  8. [Reflect(discoverable: true)]
  9. public sealed class CVarControl : Attribute
  10. {
  11. public AdminFlags AdminFlags { get; }
  12. public object? Min { get; }
  13. public object? Max { get; }
  14. public CVarControl(AdminFlags adminFlags, object? min = null, object? max = null, string? helpText = null)
  15. {
  16. AdminFlags = adminFlags;
  17. Min = min;
  18. Max = max;
  19. // Not actually sure if its a good idea to throw exceptions in attributes.
  20. if (min != null && max != null)
  21. {
  22. if (min.GetType() != max.GetType())
  23. {
  24. throw new ArgumentException("Min and max must be of the same type.");
  25. }
  26. }
  27. if (min == null && max != null || min != null && max == null)
  28. {
  29. throw new ArgumentException("Min and max must both be null or both be set.");
  30. }
  31. }
  32. }