1
0

SurveillanceCameraSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using Content.Server.DeviceNetwork;
  2. using Content.Server.DeviceNetwork.Components;
  3. using Content.Server.DeviceNetwork.Systems;
  4. using Content.Server.Emp;
  5. using Content.Server.Power.Components;
  6. using Content.Shared.ActionBlocker;
  7. using Content.Shared.DeviceNetwork;
  8. using Content.Shared.Power;
  9. using Content.Shared.SurveillanceCamera;
  10. using Content.Shared.Verbs;
  11. using Robust.Server.GameObjects;
  12. using Robust.Shared.Player;
  13. using Robust.Shared.Prototypes;
  14. namespace Content.Server.SurveillanceCamera;
  15. public sealed class SurveillanceCameraSystem : EntitySystem
  16. {
  17. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  18. [Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
  19. [Dependency] private readonly ViewSubscriberSystem _viewSubscriberSystem = default!;
  20. [Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
  21. [Dependency] private readonly UserInterfaceSystem _userInterface = default!;
  22. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  23. // Pings a surveillance camera subnet. All cameras will always respond
  24. // with a data message if they are on the same subnet.
  25. public const string CameraPingSubnetMessage = "surveillance_camera_ping_subnet";
  26. // Pings a surveillance camera. Useful to ensure that the camera is still on
  27. // before connecting fully.
  28. public const string CameraPingMessage = "surveillance_camera_ping";
  29. // Camera heartbeat. Monitors ping this to ensure that a camera is still able to
  30. // be contacted. If this doesn't get sent after some time, the monitor will
  31. // automatically disconnect.
  32. public const string CameraHeartbeatMessage = "surveillance_camera_heartbeat";
  33. // Surveillance camera data. This generally should contain nothing
  34. // except for the subnet that this camera is on -
  35. // this is because of the fact that the PacketEvent already
  36. // contains the sender UID, and that this will always be targeted
  37. // towards the sender that pinged the camera.
  38. public const string CameraDataMessage = "surveillance_camera_data";
  39. public const string CameraConnectMessage = "surveillance_camera_connect";
  40. public const string CameraSubnetConnectMessage = "surveillance_camera_subnet_connect";
  41. public const string CameraSubnetDisconnectMessage = "surveillance_camera_subnet_disconnect";
  42. public const string CameraAddressData = "surveillance_camera_data_origin";
  43. public const string CameraNameData = "surveillance_camera_data_name";
  44. public const string CameraSubnetData = "surveillance_camera_data_subnet";
  45. public const int CameraNameLimit = 32;
  46. public override void Initialize()
  47. {
  48. SubscribeLocalEvent<SurveillanceCameraComponent, ComponentShutdown>(OnShutdown);
  49. SubscribeLocalEvent<SurveillanceCameraComponent, PowerChangedEvent>(OnPowerChanged);
  50. SubscribeLocalEvent<SurveillanceCameraComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
  51. SubscribeLocalEvent<SurveillanceCameraComponent, SurveillanceCameraSetupSetName>(OnSetName);
  52. SubscribeLocalEvent<SurveillanceCameraComponent, SurveillanceCameraSetupSetNetwork>(OnSetNetwork);
  53. SubscribeLocalEvent<SurveillanceCameraComponent, GetVerbsEvent<AlternativeVerb>>(AddVerbs);
  54. SubscribeLocalEvent<SurveillanceCameraComponent, EmpPulseEvent>(OnEmpPulse);
  55. SubscribeLocalEvent<SurveillanceCameraComponent, EmpDisabledRemoved>(OnEmpDisabledRemoved);
  56. }
  57. private void OnPacketReceived(EntityUid uid, SurveillanceCameraComponent component, DeviceNetworkPacketEvent args)
  58. {
  59. if (!component.Active)
  60. {
  61. return;
  62. }
  63. if (!TryComp(uid, out DeviceNetworkComponent? deviceNet))
  64. {
  65. return;
  66. }
  67. if (args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? command))
  68. {
  69. var payload = new NetworkPayload()
  70. {
  71. { DeviceNetworkConstants.Command, string.Empty },
  72. { CameraAddressData, deviceNet.Address },
  73. { CameraNameData, component.CameraId },
  74. { CameraSubnetData, string.Empty }
  75. };
  76. var dest = string.Empty;
  77. switch (command)
  78. {
  79. case CameraConnectMessage:
  80. if (!args.Data.TryGetValue(CameraAddressData, out dest)
  81. || string.IsNullOrEmpty(args.Address))
  82. {
  83. return;
  84. }
  85. payload[DeviceNetworkConstants.Command] = CameraConnectMessage;
  86. break;
  87. case CameraHeartbeatMessage:
  88. if (!args.Data.TryGetValue(CameraAddressData, out dest)
  89. || string.IsNullOrEmpty(args.Address))
  90. {
  91. return;
  92. }
  93. payload[DeviceNetworkConstants.Command] = CameraHeartbeatMessage;
  94. break;
  95. case CameraPingMessage:
  96. if (!args.Data.TryGetValue(CameraSubnetData, out string? subnet))
  97. {
  98. return;
  99. }
  100. dest = args.SenderAddress;
  101. payload[CameraSubnetData] = subnet;
  102. payload[DeviceNetworkConstants.Command] = CameraDataMessage;
  103. break;
  104. }
  105. _deviceNetworkSystem.QueuePacket(
  106. uid,
  107. dest,
  108. payload);
  109. }
  110. }
  111. private void AddVerbs(EntityUid uid, SurveillanceCameraComponent component, GetVerbsEvent<AlternativeVerb> verbs)
  112. {
  113. if (!_actionBlocker.CanInteract(verbs.User, uid))
  114. {
  115. return;
  116. }
  117. if (component.NameSet && component.NetworkSet)
  118. {
  119. return;
  120. }
  121. AlternativeVerb verb = new();
  122. verb.Text = Loc.GetString("surveillance-camera-setup");
  123. verb.Act = () => OpenSetupInterface(uid, verbs.User, component);
  124. verbs.Verbs.Add(verb);
  125. }
  126. private void OnPowerChanged(EntityUid camera, SurveillanceCameraComponent component, ref PowerChangedEvent args)
  127. {
  128. SetActive(camera, args.Powered, component);
  129. }
  130. private void OnShutdown(EntityUid camera, SurveillanceCameraComponent component, ComponentShutdown args)
  131. {
  132. Deactivate(camera, component);
  133. }
  134. private void OnSetName(EntityUid uid, SurveillanceCameraComponent component, SurveillanceCameraSetupSetName args)
  135. {
  136. if (args.UiKey is not SurveillanceCameraSetupUiKey key
  137. || key != SurveillanceCameraSetupUiKey.Camera
  138. || string.IsNullOrEmpty(args.Name)
  139. || args.Name.Length > CameraNameLimit)
  140. {
  141. return;
  142. }
  143. component.CameraId = args.Name;
  144. component.NameSet = true;
  145. UpdateSetupInterface(uid, component);
  146. }
  147. private void OnSetNetwork(EntityUid uid, SurveillanceCameraComponent component,
  148. SurveillanceCameraSetupSetNetwork args)
  149. {
  150. if (args.UiKey is not SurveillanceCameraSetupUiKey key
  151. || key != SurveillanceCameraSetupUiKey.Camera)
  152. {
  153. return;
  154. }
  155. if (args.Network < 0 || args.Network >= component.AvailableNetworks.Count)
  156. {
  157. return;
  158. }
  159. if (!_prototypeManager.TryIndex<DeviceFrequencyPrototype>(component.AvailableNetworks[args.Network],
  160. out var frequency))
  161. {
  162. return;
  163. }
  164. _deviceNetworkSystem.SetReceiveFrequency(uid, frequency.Frequency);
  165. component.NetworkSet = true;
  166. UpdateSetupInterface(uid, component);
  167. }
  168. private void OpenSetupInterface(EntityUid uid, EntityUid player, SurveillanceCameraComponent? camera = null)
  169. {
  170. if (!Resolve(uid, ref camera))
  171. return;
  172. if (!_userInterface.TryOpenUi(uid, SurveillanceCameraSetupUiKey.Camera, player))
  173. return;
  174. UpdateSetupInterface(uid, camera);
  175. }
  176. private void UpdateSetupInterface(EntityUid uid, SurveillanceCameraComponent? camera = null, DeviceNetworkComponent? deviceNet = null)
  177. {
  178. if (!Resolve(uid, ref camera, ref deviceNet))
  179. {
  180. return;
  181. }
  182. if (camera.NameSet && camera.NetworkSet)
  183. {
  184. _userInterface.CloseUi(uid, SurveillanceCameraSetupUiKey.Camera);
  185. return;
  186. }
  187. if (camera.AvailableNetworks.Count == 0)
  188. {
  189. if (deviceNet.ReceiveFrequencyId != null)
  190. {
  191. camera.AvailableNetworks.Add(deviceNet.ReceiveFrequencyId);
  192. }
  193. else if (!camera.NetworkSet)
  194. {
  195. _userInterface.CloseUi(uid, SurveillanceCameraSetupUiKey.Camera);
  196. return;
  197. }
  198. }
  199. var state = new SurveillanceCameraSetupBoundUiState(camera.CameraId, deviceNet.ReceiveFrequency ?? 0,
  200. camera.AvailableNetworks, camera.NameSet, camera.NetworkSet);
  201. _userInterface.SetUiState(uid, SurveillanceCameraSetupUiKey.Camera, state);
  202. }
  203. // If the camera deactivates for any reason, it must have all viewers removed,
  204. // and the relevant event broadcast to all systems.
  205. private void Deactivate(EntityUid camera, SurveillanceCameraComponent? component = null)
  206. {
  207. if (!Resolve(camera, ref component))
  208. {
  209. return;
  210. }
  211. var ev = new SurveillanceCameraDeactivateEvent(camera);
  212. RemoveActiveViewers(camera, new(component.ActiveViewers), null, component);
  213. component.Active = false;
  214. // Send a targetted event to all monitors.
  215. foreach (var monitor in component.ActiveMonitors)
  216. {
  217. RaiseLocalEvent(monitor, ev, true);
  218. }
  219. component.ActiveMonitors.Clear();
  220. // Send a local event that's broadcasted everywhere afterwards.
  221. RaiseLocalEvent(ev);
  222. UpdateVisuals(camera, component);
  223. }
  224. public void SetActive(EntityUid camera, bool setting, SurveillanceCameraComponent? component = null)
  225. {
  226. if (!Resolve(camera, ref component))
  227. {
  228. return;
  229. }
  230. if (setting)
  231. {
  232. var attemptEv = new SurveillanceCameraSetActiveAttemptEvent();
  233. RaiseLocalEvent(camera, ref attemptEv);
  234. if (attemptEv.Cancelled)
  235. return;
  236. component.Active = setting;
  237. }
  238. else
  239. {
  240. Deactivate(camera, component);
  241. }
  242. UpdateVisuals(camera, component);
  243. }
  244. public void AddActiveViewer(EntityUid camera, EntityUid player, EntityUid? monitor = null, SurveillanceCameraComponent? component = null, ActorComponent? actor = null)
  245. {
  246. if (!Resolve(camera, ref component)
  247. || !component.Active
  248. || !Resolve(player, ref actor))
  249. {
  250. return;
  251. }
  252. _viewSubscriberSystem.AddViewSubscriber(camera, actor.PlayerSession);
  253. component.ActiveViewers.Add(player);
  254. if (monitor != null)
  255. {
  256. component.ActiveMonitors.Add(monitor.Value);
  257. }
  258. UpdateVisuals(camera, component);
  259. }
  260. public void AddActiveViewers(EntityUid camera, HashSet<EntityUid> players, EntityUid? monitor = null, SurveillanceCameraComponent? component = null)
  261. {
  262. if (!Resolve(camera, ref component) || !component.Active)
  263. {
  264. return;
  265. }
  266. foreach (var player in players)
  267. {
  268. AddActiveViewer(camera, player, monitor, component);
  269. }
  270. // Add monitor without viewers
  271. if (players.Count == 0 && monitor != null)
  272. {
  273. component.ActiveMonitors.Add(monitor.Value);
  274. UpdateVisuals(camera, component);
  275. }
  276. }
  277. // Switch the set of active viewers from one camera to another.
  278. public void SwitchActiveViewers(EntityUid oldCamera, EntityUid newCamera, HashSet<EntityUid> players, EntityUid? monitor = null, SurveillanceCameraComponent? oldCameraComponent = null, SurveillanceCameraComponent? newCameraComponent = null)
  279. {
  280. if (!Resolve(oldCamera, ref oldCameraComponent)
  281. || !Resolve(newCamera, ref newCameraComponent)
  282. || !oldCameraComponent.Active
  283. || !newCameraComponent.Active)
  284. {
  285. return;
  286. }
  287. if (monitor != null)
  288. {
  289. oldCameraComponent.ActiveMonitors.Remove(monitor.Value);
  290. newCameraComponent.ActiveMonitors.Add(monitor.Value);
  291. }
  292. foreach (var player in players)
  293. {
  294. RemoveActiveViewer(oldCamera, player, null, oldCameraComponent);
  295. AddActiveViewer(newCamera, player, null, newCameraComponent);
  296. }
  297. }
  298. public void RemoveActiveViewer(EntityUid camera, EntityUid player, EntityUid? monitor = null, SurveillanceCameraComponent? component = null, ActorComponent? actor = null)
  299. {
  300. if (!Resolve(camera, ref component))
  301. return;
  302. if (Resolve(player, ref actor))
  303. _viewSubscriberSystem.RemoveViewSubscriber(camera, actor.PlayerSession);
  304. component.ActiveViewers.Remove(player);
  305. if (monitor != null)
  306. {
  307. component.ActiveMonitors.Remove(monitor.Value);
  308. }
  309. UpdateVisuals(camera, component);
  310. }
  311. public void RemoveActiveViewers(EntityUid camera, HashSet<EntityUid> players, EntityUid? monitor = null, SurveillanceCameraComponent? component = null)
  312. {
  313. if (!Resolve(camera, ref component))
  314. {
  315. return;
  316. }
  317. foreach (var player in players)
  318. {
  319. RemoveActiveViewer(camera, player, monitor, component);
  320. }
  321. // Even if not removing any viewers, remove the monitor
  322. if (players.Count == 0 && monitor != null)
  323. {
  324. component.ActiveMonitors.Remove(monitor.Value);
  325. UpdateVisuals(camera, component);
  326. }
  327. }
  328. private void UpdateVisuals(EntityUid uid, SurveillanceCameraComponent? component = null, AppearanceComponent? appearance = null)
  329. {
  330. // Don't log missing, because otherwise tests fail.
  331. if (!Resolve(uid, ref component, ref appearance, false))
  332. {
  333. return;
  334. }
  335. var key = SurveillanceCameraVisuals.Disabled;
  336. if (component.Active)
  337. {
  338. key = SurveillanceCameraVisuals.Active;
  339. }
  340. if (component.ActiveViewers.Count > 0 || component.ActiveMonitors.Count > 0)
  341. {
  342. key = SurveillanceCameraVisuals.InUse;
  343. }
  344. _appearance.SetData(uid, SurveillanceCameraVisualsKey.Key, key, appearance);
  345. }
  346. private void OnEmpPulse(EntityUid uid, SurveillanceCameraComponent component, ref EmpPulseEvent args)
  347. {
  348. if (component.Active)
  349. {
  350. args.Affected = true;
  351. args.Disabled = true;
  352. SetActive(uid, false);
  353. }
  354. }
  355. private void OnEmpDisabledRemoved(EntityUid uid, SurveillanceCameraComponent component, ref EmpDisabledRemoved args)
  356. {
  357. SetActive(uid, true);
  358. }
  359. }
  360. public sealed class OnSurveillanceCameraViewerAddEvent : EntityEventArgs
  361. {
  362. }
  363. public sealed class OnSurveillanceCameraViewerRemoveEvent : EntityEventArgs
  364. {
  365. }
  366. // What happens when a camera deactivates.
  367. public sealed class SurveillanceCameraDeactivateEvent : EntityEventArgs
  368. {
  369. public EntityUid Camera { get; }
  370. public SurveillanceCameraDeactivateEvent(EntityUid camera)
  371. {
  372. Camera = camera;
  373. }
  374. }
  375. [ByRefEvent]
  376. public record struct SurveillanceCameraSetActiveAttemptEvent(bool Cancelled);