BaseWireAction.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Content.Server.Power.EntitySystems;
  2. using Content.Shared.Administration.Logs;
  3. using Content.Shared.Database;
  4. using Content.Shared.Wires;
  5. namespace Content.Server.Wires;
  6. /// <summary><see cref="IWireAction" /></summary>
  7. [ImplicitDataDefinitionForInheritors]
  8. public abstract partial class BaseWireAction : IWireAction
  9. {
  10. private ISharedAdminLogManager _adminLogger = default!;
  11. /// <summary>
  12. /// The loc-string of the text that gets returned by <see cref="GetStatusLightData(Wire)"/>. Also used for admin logging.
  13. /// </summary>
  14. [DataField("name")]
  15. public abstract string Name { get; set; }
  16. /// <summary>
  17. /// Default color that gets returned by <see cref="GetStatusLightData(Wire)"/>.
  18. /// </summary>
  19. [DataField("color")]
  20. public abstract Color Color { get; set; }
  21. /// <summary>
  22. /// If true, the default behavior of <see cref="GetStatusLightData(Wire)"/> will return an off-light when the
  23. /// wire owner is not powered.
  24. /// </summary>
  25. [DataField("lightRequiresPower")]
  26. public virtual bool LightRequiresPower { get; set; } = true;
  27. public virtual StatusLightData? GetStatusLightData(Wire wire)
  28. {
  29. if (LightRequiresPower && !IsPowered(wire.Owner))
  30. return new StatusLightData(Color, StatusLightState.Off, Loc.GetString(Name));
  31. var state = GetLightState(wire);
  32. return state == null
  33. ? null
  34. : new StatusLightData(Color, state.Value, Loc.GetString(Name));
  35. }
  36. public virtual StatusLightState? GetLightState(Wire wire) => null;
  37. public IEntityManager EntityManager = default!;
  38. public WiresSystem WiresSystem = default!;
  39. // not virtual so implementors are aware that they need a nullable here
  40. public abstract object? StatusKey { get; }
  41. // ugly, but IoC doesn't work during deserialization
  42. public virtual void Initialize()
  43. {
  44. EntityManager = IoCManager.Resolve<IEntityManager>();
  45. _adminLogger = IoCManager.Resolve<ISharedAdminLogManager>();
  46. WiresSystem = EntityManager.EntitySysManager.GetEntitySystem<WiresSystem>();
  47. }
  48. public virtual bool AddWire(Wire wire, int count) => count == 1;
  49. public virtual bool Cut(EntityUid user, Wire wire) => Log(user, wire, "cut");
  50. public virtual bool Mend(EntityUid user, Wire wire) => Log(user, wire, "mended");
  51. public virtual void Pulse(EntityUid user, Wire wire) => Log(user, wire, "pulsed");
  52. private bool Log(EntityUid user, Wire wire, string verb)
  53. {
  54. var player = EntityManager.ToPrettyString(user);
  55. var owner = EntityManager.ToPrettyString(wire.Owner);
  56. var name = Loc.GetString(Name);
  57. var color = wire.Color.Name();
  58. var action = GetType().Name;
  59. // logs something like "... mended red POWR wire (PowerWireAction) in ...."
  60. _adminLogger.Add(LogType.WireHacking, LogImpact.Medium, $"{player} {verb} {color} {name} wire ({action}) in {owner}");
  61. return true;
  62. }
  63. public virtual void Update(Wire wire)
  64. {
  65. }
  66. /// <summary>
  67. /// Utility function to check if this given entity is powered.
  68. /// </summary>
  69. /// <returns>true if powered, false otherwise</returns>
  70. protected bool IsPowered(EntityUid uid)
  71. {
  72. return WiresSystem.IsPowered(uid, EntityManager);
  73. }
  74. }