Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Holopad networking rework #34112

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
130 changes: 48 additions & 82 deletions Content.Client/Holopad/HolopadSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using System.Linq;
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;

namespace Content.Client.Holopad;

Expand All @@ -19,20 +21,19 @@ public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<HolopadHologramComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<HolopadHologramComponent, ComponentHandleState>(HandleHolopadHologramState);
SubscribeLocalEvent<HolopadHologramComponent, BeforePostShaderRenderEvent>(OnShaderRender);
SubscribeAllEvent<TypingChangedEvent>(OnTypingChanged);

SubscribeNetworkEvent<PlayerSpriteStateRequest>(OnPlayerSpriteStateRequest);
SubscribeNetworkEvent<PlayerSpriteStateMessage>(OnPlayerSpriteStateMessage);
}

private void OnComponentInit(EntityUid uid, HolopadHologramComponent component, ComponentInit ev)
private void HandleHolopadHologramState(Entity<HolopadHologramComponent> entity, ref ComponentHandleState args)
{
if (!TryComp<SpriteComponent>(uid, out var sprite))
if (args.Current is not HolopadHologramComponentState state)
return;

UpdateHologramSprite(uid);
entity.Comp.LinkedEntity = GetEntity(state.Target);

chromiumboy marked this conversation as resolved.
Show resolved Hide resolved
UpdateHologramSprite(entity, entity.Comp.LinkedEntity);
}

private void OnShaderRender(EntityUid uid, HolopadHologramComponent component, BeforePostShaderRenderEvent ev)
Expand All @@ -57,100 +58,65 @@ private void OnTypingChanged(TypingChangedEvent ev, EntitySessionEventArgs args)
RaiseNetworkEvent(netEv);
}

private void OnPlayerSpriteStateRequest(PlayerSpriteStateRequest ev)
private void UpdateHologramSprite(EntityUid hologram, EntityUid? target)
{
var targetPlayer = GetEntity(ev.TargetPlayer);
var player = _playerManager.LocalSession?.AttachedEntity;

// Ignore the request if received by a player who isn't the target
if (targetPlayer != player)
// Get required components
if (!TryComp<SpriteComponent>(hologram, out var hologramSprite) ||
!TryComp<HolopadHologramComponent>(hologram, out var holopadhologram))
return;

if (!TryComp<SpriteComponent>(player, out var playerSprite))
return;

var spriteLayerData = new List<PrototypeLayerData>();
// Remove all sprite layers
for (int i = hologramSprite.AllLayers.Count() - 1; i >= 0; i--)
hologramSprite.RemoveLayer(i);

if (playerSprite.Visible)
if (TryComp<SpriteComponent>(target, out var targetSprite))
{
// Record the RSI paths, state names and shader paramaters of all visible layers
for (int i = 0; i < playerSprite.AllLayers.Count(); i++)
// Use the target's holographic avatar (if available)
if (TryComp<HolographicAvatarComponent>(target, out var targetAvatar))
{
if (!playerSprite.TryGetLayer(i, out var layer))
continue;

if (!layer.Visible ||
string.IsNullOrEmpty(layer.ActualRsi?.Path.ToString()) ||
string.IsNullOrEmpty(layer.State.Name))
continue;

var layerDatum = new PrototypeLayerData();
layerDatum.RsiPath = layer.ActualRsi.Path.ToString();
layerDatum.State = layer.State.Name;

if (layer.CopyToShaderParameters != null)
for (int i = 0; i < targetAvatar.LayerData.Length; i++)
{
var key = (string)layer.CopyToShaderParameters.LayerKey;

if (playerSprite.LayerMapTryGet(key, out var otherLayerIdx) &&
playerSprite.TryGetLayer(otherLayerIdx, out var otherLayer) &&
otherLayer.Visible)
{
layerDatum.MapKeys = new() { key };

layerDatum.CopyToShaderParameters = new PrototypeCopyToShaderParameters()
{
LayerKey = key,
ParameterTexture = layer.CopyToShaderParameters.ParameterTexture,
ParameterUV = layer.CopyToShaderParameters.ParameterUV
};
}
var layer = targetAvatar.LayerData[i];
hologramSprite.AddLayer(targetAvatar.LayerData[i], i);
}
}

spriteLayerData.Add(layerDatum);
// Otherwise copy the target's current physical appearance
else
{
hologramSprite.CopyFrom(targetSprite);
}
}

// Return the recorded data to the server
var evResponse = new PlayerSpriteStateMessage(ev.TargetPlayer, spriteLayerData.ToArray());
RaiseNetworkEvent(evResponse);
}

private void OnPlayerSpriteStateMessage(PlayerSpriteStateMessage ev)
{
UpdateHologramSprite(GetEntity(ev.SpriteEntity), ev.SpriteLayerData);
}

private void UpdateHologramSprite(EntityUid uid, PrototypeLayerData[]? layerData = null)
{
if (!TryComp<SpriteComponent>(uid, out var hologramSprite))
return;

if (!TryComp<HolopadHologramComponent>(uid, out var holopadhologram))
return;
// There is no target, display a default sprite instead (if available)
else
{
if (string.IsNullOrEmpty(holopadhologram.RsiPath) || string.IsNullOrEmpty(holopadhologram.RsiState))
return;

for (int i = hologramSprite.AllLayers.Count() - 1; i >= 0; i--)
hologramSprite.RemoveLayer(i);
var layer = new PrototypeLayerData();
layer.RsiPath = holopadhologram.RsiPath;
layer.State = holopadhologram.RsiState;

if (layerData == null || layerData.Length == 0)
{
layerData = new PrototypeLayerData[1];
layerData[0] = new PrototypeLayerData()
{
RsiPath = holopadhologram.RsiPath,
State = holopadhologram.RsiState
};
hologramSprite.AddLayer(layer);
}

for (int i = 0; i < layerData.Length; i++)
{
var layer = layerData[i];
layer.Shader = "unshaded";
// Override specific values
hologramSprite.Color = Color.White;
hologramSprite.Offset = holopadhologram.Offset;
hologramSprite.DrawDepth = (int)DrawDepth.Mobs;
hologramSprite.NoRotation = true;
hologramSprite.DirectionOverride = Direction.South;
hologramSprite.EnableDirectionOverride = true;

hologramSprite.AddLayer(layerData[i], i);
// Remove shading from all layers (except displacement maps)
for (int i = 0; i < hologramSprite.AllLayers.Count(); i++)
{
if (hologramSprite.TryGetLayer(i, out var layer) && layer.ShaderPrototype != "DisplacedStencilDraw")
hologramSprite.LayerSetShader(i, "unshaded");
}

UpdateHologramShader(uid, hologramSprite, holopadhologram);
UpdateHologramShader(hologram, hologramSprite, holopadhologram);
}

private void UpdateHologramShader(EntityUid uid, SpriteComponent sprite, HolopadHologramComponent holopadHologram)
Expand Down
107 changes: 40 additions & 67 deletions Content.Server/Holopad/HolopadSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
using Content.Shared.Chat.TypingIndicator;
using Content.Shared.Holopad;
using Content.Shared.IdentityManagement;
using Content.Shared.Inventory;
using Content.Shared.Labels.Components;
using Content.Shared.Silicons.StationAi;
using Content.Shared.Telephone;
using Content.Shared.UserInterface;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Server.GameStates;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using System.Linq;
Expand All @@ -31,25 +34,18 @@ public sealed class HolopadSystem : SharedHolopadSystem
[Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
[Dependency] private readonly SharedStationAiSystem _stationAiSystem = default!;
[Dependency] private readonly AccessReaderSystem _accessReaderSystem = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private float _updateTimer = 1.0f;

private const float UpdateTime = 1.0f;
private const float MinTimeBetweenSyncRequests = 0.5f;
private TimeSpan _minTimeSpanBetweenSyncRequests;

private HashSet<EntityUid> _pendingRequestsForSpriteState = new();
private HashSet<EntityUid> _recentlyUpdatedHolograms = new();

public override void Initialize()
{
base.Initialize();

_minTimeSpanBetweenSyncRequests = TimeSpan.FromSeconds(MinTimeBetweenSyncRequests);

// Holopad UI and bound user interface messages
SubscribeLocalEvent<HolopadComponent, BeforeActivatableUIOpenEvent>(OnUIOpen);
SubscribeLocalEvent<HolopadComponent, HolopadStartNewCallMessage>(OnHolopadStartNewCall);
Expand All @@ -67,7 +63,8 @@ public override void Initialize()

// Networked events
SubscribeNetworkEvent<HolopadUserTypingChangedEvent>(OnTypingChanged);
SubscribeNetworkEvent<PlayerSpriteStateMessage>(OnPlayerSpriteStateMessage);
SubscribeLocalEvent<ExpandPvsEvent>(OnExpandPvs);
SubscribeLocalEvent<HolopadHologramComponent, ComponentGetState>(GetHolopadHologramState);

// Component start/shutdown events
SubscribeLocalEvent<HolopadComponent, ComponentInit>(OnHolopadInit);
Expand Down Expand Up @@ -263,16 +260,12 @@ private void OnHoloCallCommenced(Entity<HolopadComponent> source, ref TelephoneC
if (source.Comp.Hologram == null)
GenerateHologram(source);

// Receiver holopad holograms have to be generated now instead of waiting for their own event
// to fire because holographic avatars get synced immediately
if (TryComp<HolopadComponent>(args.Receiver, out var receivingHolopad) && receivingHolopad.Hologram == null)
GenerateHologram((args.Receiver, receivingHolopad));

// Re-link the user to refresh the sprite data
if (source.Comp.User != null)
{
// Re-link the user to refresh the sprite data
LinkHolopadToUser(source, source.Comp.User.Value);
}
}

private void OnHoloCallEnded(Entity<HolopadComponent> entity, ref TelephoneCallEndedEvent args)
Expand Down Expand Up @@ -318,20 +311,27 @@ private void OnTypingChanged(HolopadUserTypingChangedEvent ev, EntitySessionEven
}
}

private void OnPlayerSpriteStateMessage(PlayerSpriteStateMessage ev, EntitySessionEventArgs args)
private void OnExpandPvs(ref ExpandPvsEvent args)
{
var uid = args.SenderSession.AttachedEntity;
if (args.Entities == null)
args.Entities = new();

chromiumboy marked this conversation as resolved.
Show resolved Hide resolved
if (!Exists(uid))
return;
// Add holopad users and their held/equipped items to PVS so that they can be rendered by remote holopads
var query = AllEntityQuery<HolopadUserComponent>();
while (query.MoveNext(out var ent, out var entHolopadUser))
{
args.Entities.Add(ent);

if (!_pendingRequestsForSpriteState.Remove(uid.Value))
return;
foreach (var item in _inventorySystem.GetHandOrInventoryEntities(ent))
args.Entities.Add(item);
}
}

chromiumboy marked this conversation as resolved.
Show resolved Hide resolved
if (!TryComp<HolopadUserComponent>(uid, out var holopadUser))
return;
private void GetHolopadHologramState(Entity<HolopadHologramComponent> entity, ref ComponentGetState args)
{
var netTarget = GetNetEntity(entity.Comp.LinkedEntity);

SyncHolopadUserWithLinkedHolograms((uid.Value, holopadUser), ev.SpriteLayerData);
args.State = new HolopadHologramComponentState(netTarget);
}

#endregion
Expand Down Expand Up @@ -477,8 +477,6 @@ public override void Update(float frameTime)
}
}
}

_recentlyUpdatedHolograms.Clear();
}

public void UpdateUIState(Entity<HolopadComponent> entity, TelephoneComponent? telephone = null)
Expand Down Expand Up @@ -555,15 +553,21 @@ private void LinkHolopadToUser(Entity<HolopadComponent> entity, EntityUid user)
entity.Comp.User = (user, holopadUser);
}

if (TryComp<HolographicAvatarComponent>(user, out var avatar))
{
SyncHolopadUserWithLinkedHolograms((user, holopadUser), avatar.LayerData);
foreach (var receiver in GetLinkedHolopads(entity))
SyncHolopadHologramAppearanceWithTarget(receiver, (user, holopadUser));
}

private void SyncHolopadHologramAppearanceWithTarget(Entity<HolopadComponent> entity, Entity<HolopadUserComponent>? user)
{
var netHologram = GetNetEntity(entity.Comp.Hologram);

if (netHologram == null)
return;
}

// We have no apriori sprite data for the hologram, request
// the current appearance of the user from the client
RequestHolopadUserSpriteUpdate((user, holopadUser));
var netUser = GetNetEntity(user);

entity.Comp.Hologram!.Value.Comp.LinkedEntity = user?.Owner;
Dirty(entity.Comp.Hologram.Value);
}

private void UnlinkHolopadFromUser(Entity<HolopadComponent> entity, Entity<HolopadUserComponent>? user)
Expand All @@ -578,11 +582,7 @@ private void UnlinkHolopadFromUser(Entity<HolopadComponent> entity, Entity<Holop
if (linkedHolopad.Comp.Hologram != null)
{
_appearanceSystem.SetData(linkedHolopad.Comp.Hologram.Value.Owner, TypingIndicatorVisuals.IsTyping, false);

// Send message with no sprite data to the client
// This will set the holgram sprite to a generic icon
var ev = new PlayerSpriteStateMessage(GetNetEntity(linkedHolopad.Comp.Hologram.Value));
RaiseNetworkEvent(ev);
SyncHolopadHologramAppearanceWithTarget(linkedHolopad, null);
}
}

Expand All @@ -591,12 +591,10 @@ private void UnlinkHolopadFromUser(Entity<HolopadComponent> entity, Entity<Holop

user.Value.Comp.LinkedHolopads.Remove(entity);

if (!user.Value.Comp.LinkedHolopads.Any())
if (!user.Value.Comp.LinkedHolopads.Any() &&
user.Value.Comp.LifeStage < ComponentLifeStage.Stopping)
{
_pendingRequestsForSpriteState.Remove(user.Value);

if (user.Value.Comp.LifeStage < ComponentLifeStage.Stopping)
RemComp<HolopadUserComponent>(user.Value);
RemComp<HolopadUserComponent>(user.Value);
chromiumboy marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -616,31 +614,6 @@ private void ShutDownHolopad(Entity<HolopadComponent> entity)
Dirty(entity);
}

private void RequestHolopadUserSpriteUpdate(Entity<HolopadUserComponent> user)
{
if (!_pendingRequestsForSpriteState.Add(user))
return;

var ev = new PlayerSpriteStateRequest(GetNetEntity(user));
RaiseNetworkEvent(ev);
}

private void SyncHolopadUserWithLinkedHolograms(Entity<HolopadUserComponent> entity, PrototypeLayerData[]? spriteLayerData)
{
foreach (var linkedHolopad in entity.Comp.LinkedHolopads)
{
foreach (var receivingHolopad in GetLinkedHolopads(linkedHolopad))
{
if (receivingHolopad.Comp.Hologram == null || !_recentlyUpdatedHolograms.Add(receivingHolopad.Comp.Hologram.Value))
continue;

var netHologram = GetNetEntity(receivingHolopad.Comp.Hologram.Value);
var ev = new PlayerSpriteStateMessage(netHologram, spriteLayerData);
RaiseNetworkEvent(ev);
}
}
}

private void ActivateProjector(Entity<HolopadComponent> entity, EntityUid user)
{
if (!TryComp<TelephoneComponent>(entity, out var receiverTelephone))
Expand Down
Loading
Loading