Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Content.Client/_DEN/Consent/EntitySystems/ConsentSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Content.Shared._DEN.Consent.EntitySystems;

namespace Content.Client._DEN.Consent.EntitySystems;

public sealed class ConsentSystem : SharedConsentSystem;
93 changes: 93 additions & 0 deletions Content.Server/_DEN/Consent/ConsentCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Content.Shared._DEN.Consent.Managers;
using Content.Shared._DEN.Consent.Prototypes;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.Prototypes;

namespace Content.Server._DEN.Consent;

[AnyCommand]
public sealed class SetConsentCommand : LocalizedCommands
{
[Dependency] private readonly IConsentManager _consentManager = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;

public override string Command { get; } = "setconsent";

public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 2 || shell.Player == null)
{
shell.WriteError(Loc.GetString("cmd-setconsent-error-args", ("usage", Help)));
return;
}

if (!_protoManager.TryIndex<ConsentTogglePrototype>(args[0], out _))
{
shell.WriteError(Loc.GetString("cmd-setconsent-error-invalid-consent", ("consentId", args[0]), ("usage", Help)));
return;
}

if (!bool.TryParse(args[1], out var newValue))
{
shell.WriteError(Loc.GetString("cmd-setconsent-error-bool", ("value", args[1]), ("usage", Help)));
return;
}

_consentManager.SetConsentToggle(shell.Player.UserId, args[0], newValue);
shell.WriteLine(Loc.GetString("cmd-setconsent-success", ("consentId", args[0]), ("value", args[1])));
}

public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length == 1)
{
var options = CompletionHelper.PrototypeIDs<ConsentTogglePrototype>();
return CompletionResult.FromOptions(options);
}

if (args.Length == 2)
{
var options = CompletionHelper.Booleans;
return CompletionResult.FromOptions(options);
}

return CompletionResult.Empty;
}
}

[AnyCommand]
public sealed class ConsentCommand : LocalizedCommands
{
[Dependency] private readonly IConsentManager _consentManager = default!;

public override string Command { get; } = "consents";

public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 1 || shell.Player == null)
return;

var consents = _consentManager.GetConsentToggles(shell.Player.UserId);
var consentsMessage = string.Join("\n -", consents);

if (consents.Count == 0)
{
shell.WriteLine(Loc.GetString("cmd-consent-no-different"));
return;
}

shell.WriteLine(Loc.GetString("cmd-consent-differences", ("differentConsents", consentsMessage)));
}

public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length == 1)
{
var options = CompletionHelper.PrototypeIDs<ConsentTogglePrototype>();
return CompletionResult.FromOptions(options);
}

return CompletionResult.Empty;
}
}
70 changes: 70 additions & 0 deletions Content.Server/_DEN/Consent/EntitySystems/ConsentSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Content.Shared._DEN.Consent.Components;
using Content.Shared._DEN.Consent.EntitySystems;
using Content.Shared._DEN.Consent.Events;
using Content.Shared._DEN.Consent.Managers;
using Robust.Server.Player;
using Robust.Shared.Network;
using Robust.Shared.Player;

namespace Content.Server._DEN.Consent.EntitySystems;

/// <summary>
/// This handles server-sided consent information.
/// </summary>
public sealed class ConsentSystem : SharedConsentSystem
{
[Dependency] private readonly IConsentManager _consentManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;

/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<PlayerAttachedEvent>(OnMindAdded);
SubscribeLocalEvent<PlayerDetachedEvent>(OnMindRemoved);

_consentManager.OnConsentUpdated += args => OnConsentUpdated(args.UserId);
_consentManager.OnConsentSet += OnConsentUpdated;
}

private void OnConsentUpdated(NetUserId userId)
{
if (!_playerManager.TryGetSessionById(userId, out var session)
|| session.AttachedEntity is not { Valid: true } attachedEntity)
return;

var consentToggles = ConsentManager.GetConsentToggles(userId);
var consentComponent = EnsureComp<ConsentComponent>(attachedEntity);

if (consentComponent.ConsentToggles == consentToggles)
return;

var ent = (attachedEntity, consentComponent);
consentComponent.ConsentToggles = consentToggles;

Dirty<ConsentComponent>(ent);
RaiseLocalEvent(new ConsentUpdatedEvent(attachedEntity));
}

private void OnMindAdded(PlayerAttachedEvent ev)
{
BuildConsentComponent(ev);
}

private void OnMindRemoved(PlayerDetachedEvent ev)
{
RemComp<ConsentComponent>(ev.Entity);
}

private void BuildConsentComponent(PlayerAttachedEvent ev)
{
var userId = ev.Player.UserId;

var consentToggles = ConsentManager.GetConsentToggles(userId);
var consentComponent = EnsureComp<ConsentComponent>(ev.Entity);
consentComponent.ConsentToggles = consentToggles;

Dirty<ConsentComponent>((ev.Entity, consentComponent));
}
}
2 changes: 2 additions & 0 deletions Content.Shared/Entry/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Content.Shared._DEN.Consent.Managers;
using Content.Shared.Humanoid.Markings;
using Content.Shared.Maps;
using Robust.Shared;
Expand Down Expand Up @@ -47,6 +48,7 @@ public override void PostInit()

InitTileDefinitions();
Dependencies.Resolve<MarkingManager>().Initialize();
Dependencies.Resolve<IConsentManager>().Initialize(); // DEN: Consent system

#if DEBUG
_configurationManager.OverrideDefault(CVars.NetFakeLagMin, 0.075f);
Expand Down
4 changes: 3 additions & 1 deletion Content.Shared/IoC/SharedContentIoC.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Shared.Humanoid.Markings;
using Content.Shared._DEN.Consent.Managers;
using Content.Shared.Humanoid.Markings;
using Content.Shared.Localizations;

namespace Content.Shared.IoC
Expand All @@ -9,6 +10,7 @@ public static void Register(IDependencyCollection deps)
{
deps.Register<MarkingManager, MarkingManager>();
deps.Register<ContentLocalizationManager, ContentLocalizationManager>();
deps.Register<IConsentManager, ConsentManager>(); // DEN: Consent System
}
}
}
15 changes: 15 additions & 0 deletions Content.Shared/_DEN/Consent/Components/ConsentComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Content.Shared._DEN.Consent.Prototypes;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;

namespace Content.Shared._DEN.Consent.Components;

/// <summary>
/// This is used for sharing a user's consent information with other clients.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ConsentComponent : Component
{
[DataField, AutoNetworkedField]
public List<ProtoId<ConsentTogglePrototype>> ConsentToggles { get; set; } = new();
}
24 changes: 24 additions & 0 deletions Content.Shared/_DEN/Consent/EntitySystems/SharedConsentSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Content.Shared._DEN.Consent.Components;
using Content.Shared._DEN.Consent.Managers;
using Content.Shared._DEN.Consent.Prototypes;
using Robust.Shared.Prototypes;

namespace Content.Shared._DEN.Consent.EntitySystems;

public abstract class SharedConsentSystem : EntitySystem
{
[Dependency] protected readonly IConsentManager ConsentManager = default!;

public bool HasConsent(EntityUid uid, ProtoId<ConsentTogglePrototype> toggle)
{
var defaultValue = ConsentManager.GetDefaultValue(toggle);

if (TryComp<ConsentComponent>(uid, out var consent)
&& consent.ConsentToggles.Contains(toggle))
return !defaultValue;

return defaultValue;
}
}

public record struct UserConsentInfo(ProtoId<ConsentTogglePrototype> ToggleId, bool ToggleValue);
54 changes: 54 additions & 0 deletions Content.Shared/_DEN/Consent/Events/ConsentEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Content.Shared._DEN.Consent.Components;
using Content.Shared._DEN.Consent.Prototypes;
using Lidgren.Network;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Content.Shared._DEN.Consent.Managers;

namespace Content.Shared._DEN.Consent.Events;

/// <summary>
/// Used to inform same-side entity systems about updated consents, for whatever reason.
/// </summary>
/// <remarks>
/// This will only run if the user in question has a valid AttachedEntity.
/// <see cref="IConsentManager"/> contains an action that always runs on update.
/// </remarks>
public sealed class ConsentUpdatedEvent(EntityUid uid) : EntityEventArgs
{
public EntityUid Entity { get; } = uid;
}

/// <summary>
/// Used to update consent settings on either the client or the server.
/// </summary>
public sealed class MsgUpdateConsent : NetMessage
{
public override MsgGroups MsgGroup => MsgGroups.Command;

public List<ProtoId<ConsentTogglePrototype>> NotDefaultConsents = [];

public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer)
{
var length = buffer.ReadVariableInt32();

NotDefaultConsents.Clear();

for (var i = 0; i < length; i++)
{
var updatedConsent = (ProtoId<ConsentTogglePrototype>) buffer.ReadString();
NotDefaultConsents.Add(updatedConsent);
}
}

public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer)
{
buffer.WriteVariableInt32(NotDefaultConsents.Count);

foreach (var consentId in NotDefaultConsents)
{
buffer.Write(consentId);
}
}
}
15 changes: 15 additions & 0 deletions Content.Shared/_DEN/Consent/Events/ConsentUpdatedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Content.Shared._DEN.Consent.Prototypes;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;

namespace Content.Shared._DEN.Consent.Events;

public sealed class ConsentUpdatedEventArgs(
NetUserId userId,
ProtoId<ConsentTogglePrototype> toggleId,
bool newValue) : EventArgs
{
public readonly NetUserId UserId = userId;
public readonly ProtoId<ConsentTogglePrototype> ToggleId = toggleId;
public readonly bool ToggleValue = newValue;
}
Loading