Encryption Keys for headsets (#12615)

This commit is contained in:
AlexMorgan3817
2023-01-29 00:53:08 +00:00
committed by GitHub
parent 14ba167201
commit c8b89c7008
33 changed files with 783 additions and 270 deletions

View File

@@ -1,11 +1,18 @@
using Content.Server.Chat.Systems;
using Content.Server.Radio.Components;
using Content.Server.Popups;
using Content.Server.Tools;
using Content.Shared.Tools.Components;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Inventory.Events;
using Content.Shared.Radio;
using Content.Server.Radio.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using System.Linq;
using Robust.Shared.Profiling;
namespace Content.Server.Radio.EntitySystems;
@@ -14,6 +21,10 @@ public sealed class HeadsetSystem : EntitySystem
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly RadioSystem _radio = default!;
[Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
@@ -23,6 +34,10 @@ public sealed class HeadsetSystem : EntitySystem
SubscribeLocalEvent<HeadsetComponent, GotEquippedEvent>(OnGotEquipped);
SubscribeLocalEvent<HeadsetComponent, GotUnequippedEvent>(OnGotUnequipped);
SubscribeLocalEvent<WearingHeadsetComponent, EntitySpokeEvent>(OnSpeak);
SubscribeLocalEvent<HeadsetComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<HeadsetComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<HeadsetComponent, EntInsertedIntoContainerMessage>(OnContainerInserted);
}
private void OnSpeak(EntityUid uid, WearingHeadsetComponent component, EntitySpokeEvent args)
@@ -43,10 +58,15 @@ public sealed class HeadsetSystem : EntitySystem
if (component.IsEquipped && component.Enabled)
{
EnsureComp<WearingHeadsetComponent>(args.Equipee).Headset = uid;
EnsureComp<ActiveRadioComponent>(uid).Channels.UnionWith(component.Channels);
PushRadioChannelsToOwner(uid, component, EnsureComp<ActiveRadioComponent>(uid));
}
}
private void PushRadioChannelsToOwner(EntityUid uid, HeadsetComponent component, ActiveRadioComponent activeRadio)
{
activeRadio.Channels.UnionWith(component.Channels);
}
private void OnGotUnequipped(EntityUid uid, HeadsetComponent component, GotUnequippedEvent args)
{
component.IsEquipped = false;
@@ -86,21 +106,127 @@ public sealed class HeadsetSystem : EntitySystem
{
if (!args.IsInDetailsRange)
return;
args.PushMarkup(Loc.GetString("examine-headset"));
foreach (var id in component.Channels)
if (component.KeyContainer.ContainedEntities.Count == 0)
{
if (id == "Common") continue;
var proto = _protoManager.Index<RadioChannelPrototype>(id);
args.PushMarkup(Loc.GetString("examine-headset-channel",
("color", proto.Color),
("key", proto.KeyCode),
("id", proto.LocalizedName),
("freq", proto.Frequency)));
args.PushMarkup(Loc.GetString("examine-headset-no-keys"));
return;
}
else if (component.Channels.Count > 0)
{
args.PushMarkup(Loc.GetString("examine-headset-channels-prefix"));
EncryptionKeySystem.GetChannelsExamine(component.Channels, args, _protoManager, "examine-headset-channel");
args.PushMarkup(Loc.GetString("examine-headset-chat-prefix", ("prefix", ":h")));
if (component.DefaultChannel != null)
{
var proto = _protoManager.Index<RadioChannelPrototype>(component.DefaultChannel);
args.PushMarkup(Loc.GetString("examine-headset-default-channel", ("channel", component.DefaultChannel), ("color", proto.Color)));
}
}
}
args.PushMarkup(Loc.GetString("examine-headset-chat-prefix", ("prefix", ";")));
private void OnStartup(EntityUid uid, HeadsetComponent component, ComponentStartup args)
{
component.KeyContainer = _container.EnsureContainer<Container>(uid, HeadsetComponent.KeyContainerName);
}
private bool InstallKey(HeadsetComponent component, EntityUid key, EncryptionKeyComponent keyComponent)
{
if (component.KeyContainer.Insert(key))
{
UploadChannelsFromKey(component, keyComponent);
return true;
}
return false;
}
private void UploadChannelsFromKey(HeadsetComponent component, EncryptionKeyComponent key)
{
foreach (var j in key.Channels)
component.Channels.Add(j);
}
public void RecalculateChannels(HeadsetComponent component)
{
component.Channels.Clear();
foreach (EntityUid i in component.KeyContainer.ContainedEntities)
{
if (TryComp<EncryptionKeyComponent>(i, out var key))
{
UploadChannelsFromKey(component, key);
}
}
}
private void OnInteractUsing(EntityUid uid, HeadsetComponent component, InteractUsingEvent args)
{
if (!TryComp<ContainerManagerComponent>(uid, out var storage))
return;
if(!component.IsKeysUnlocked)
{
_popupSystem.PopupEntity(Loc.GetString("headset-encryption-keys-are-locked"), uid, args.User);
return;
}
if (TryComp<EncryptionKeyComponent>(args.Used, out var key))
{
if (component.KeySlots > component.KeyContainer.ContainedEntities.Count)
{
if (InstallKey(component, args.Used, key))
{
_popupSystem.PopupEntity(Loc.GetString("headset-encryption-key-successfully-installed"), uid, args.User);
_audio.PlayPvs(component.KeyInsertionSound, args.Target);
}
}
else
{
_popupSystem.PopupEntity(Loc.GetString("headset-encryption-key-slots-already-full"), uid, args.User);
}
}
if (TryComp<ToolComponent>(args.Used, out var tool))
{
if (component.KeyContainer.ContainedEntities.Count > 0)
{
if (_toolSystem.UseTool(
args.Used, args.User, uid,
0f, 0f, new String[] { component.KeysExtractionMethod },
doAfterCompleteEvent: null, toolComponent: tool)
)
{
var contained = component.KeyContainer.ContainedEntities.ToArray<EntityUid>();
foreach (var i in contained)
component.KeyContainer.Remove(i);
component.Channels.Clear();
UpdateDefaultChannel(component);
_popupSystem.PopupEntity(Loc.GetString("headset-encryption-keys-all-extracted"), uid, args.User);
_audio.PlayPvs(component.KeyExtractionSound, args.Target);
}
}
else
{
_popupSystem.PopupEntity(Loc.GetString("headset-encryption-keys-no-keys"), uid, args.User);
}
}
}
private void UpdateDefaultChannel(HeadsetComponent component)
{
if (component.KeyContainer.ContainedEntities.Count >= 1)
component.DefaultChannel = EnsureComp<EncryptionKeyComponent>(component.KeyContainer.ContainedEntities[0])?.DefaultChannel;
else
component.DefaultChannel = null;
}
private void OnContainerInserted(EntityUid uid, HeadsetComponent component, EntInsertedIntoContainerMessage args)
{
if (args.Container.ID != HeadsetComponent.KeyContainerName)
{
return;
}
if (TryComp<EncryptionKeyComponent>(args.Entity, out var added))
{
UpdateDefaultChannel(component);
UploadChannelsFromKey(component, added);
PushRadioChannelsToOwner(uid, component, EnsureComp<ActiveRadioComponent>(uid));
}
return;
}
}