ConfigurationSystem.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Content.Shared.Configurable;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.Tools.Components;
  4. using Content.Shared.Tools.Systems;
  5. using Robust.Server.GameObjects;
  6. using Robust.Shared.Containers;
  7. using Robust.Shared.Player;
  8. using static Content.Shared.Configurable.ConfigurationComponent;
  9. namespace Content.Server.Configurable;
  10. public sealed class ConfigurationSystem : EntitySystem
  11. {
  12. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  13. [Dependency] private readonly SharedToolSystem _toolSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<ConfigurationComponent, ConfigurationUpdatedMessage>(OnUpdate);
  18. SubscribeLocalEvent<ConfigurationComponent, ComponentStartup>(OnStartup);
  19. SubscribeLocalEvent<ConfigurationComponent, InteractUsingEvent>(OnInteractUsing);
  20. SubscribeLocalEvent<ConfigurationComponent, ContainerIsInsertingAttemptEvent>(OnInsert);
  21. }
  22. private void OnInteractUsing(EntityUid uid, ConfigurationComponent component, InteractUsingEvent args)
  23. {
  24. // TODO use activatable ui system
  25. if (args.Handled)
  26. return;
  27. if (!_toolSystem.HasQuality(args.Used, component.QualityNeeded))
  28. return;
  29. args.Handled = _uiSystem.TryOpenUi(uid, ConfigurationUiKey.Key, args.User);
  30. }
  31. private void OnStartup(EntityUid uid, ConfigurationComponent component, ComponentStartup args)
  32. {
  33. UpdateUi(uid, component);
  34. }
  35. private void UpdateUi(EntityUid uid, ConfigurationComponent component)
  36. {
  37. if (_uiSystem.HasUi(uid, ConfigurationUiKey.Key))
  38. _uiSystem.SetUiState(uid, ConfigurationUiKey.Key, new ConfigurationBoundUserInterfaceState(component.Config));
  39. }
  40. private void OnUpdate(EntityUid uid, ConfigurationComponent component, ConfigurationUpdatedMessage args)
  41. {
  42. foreach (var key in component.Config.Keys)
  43. {
  44. var value = args.Config.GetValueOrDefault(key);
  45. if (string.IsNullOrWhiteSpace(value) || component.Validation != null && !component.Validation.IsMatch(value))
  46. continue;
  47. component.Config[key] = value;
  48. }
  49. UpdateUi(uid, component);
  50. var updatedEvent = new ConfigurationUpdatedEvent(component);
  51. RaiseLocalEvent(uid, updatedEvent, false);
  52. // TODO support float (spinbox) and enum (drop-down) configurations
  53. // TODO support verbs.
  54. }
  55. private void OnInsert(EntityUid uid, ConfigurationComponent component, ContainerIsInsertingAttemptEvent args)
  56. {
  57. if (!_toolSystem.HasQuality(args.EntityUid, component.QualityNeeded))
  58. return;
  59. args.Cancel();
  60. }
  61. /// <summary>
  62. /// Sent when configuration values got changes
  63. /// </summary>
  64. public sealed class ConfigurationUpdatedEvent : EntityEventArgs
  65. {
  66. public ConfigurationComponent Configuration;
  67. public ConfigurationUpdatedEvent(ConfigurationComponent configuration)
  68. {
  69. Configuration = configuration;
  70. }
  71. }
  72. }