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

Add attempt event for sessionstate #5237

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Robust.Server/GameStates/PvsSystem.Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal void ComputeSessionState(PvsSession session)
else
GetAllEntityStates(session);

_playerManager.GetPlayerStates(session.FromTick, session.PlayerStates);
_playerManager.GetPlayerStates(session.Session, session.FromTick, session.PlayerStates);

// lastAck varies with each client based on lag and such, we can't just make 1 global state and send it to everyone

Expand Down
10 changes: 7 additions & 3 deletions Robust.Server/Player/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,19 @@ private void HandlePlayerListReq(MsgPlayerListReq message)
session.ConnectedTime = DateTime.UtcNow;
SetStatus(session, SessionStatus.Connected);

var list = new List<SessionState>();
var list = new List<SessionState>(players.Length);
var ev = new GetSessionStateAttempt(session);
_entityManager.EventBus.RaiseEvent(EventSource.Local, ref ev);

foreach (var client in players)
{
var info = new SessionState
{
UserId = client.UserId,
Name = client.Name,
Name = ev.Cancelled ? string.Empty : client.Name,
Status = client.Status,
Ping = client.Channel!.Ping
Ping = client.Channel.Ping,
ControlledEntity = EntManager.GetNetEntity(client.AttachedEntity),
};
list.Add(info);
}
Expand Down
11 changes: 11 additions & 0 deletions Robust.Shared/GameStates/SessionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.ViewVariables;

#nullable disable
Expand Down Expand Up @@ -39,4 +40,14 @@ public SessionState Clone()
};
}
}

/// <summary>
/// Event raised to determine whether the session can see all session state data.
/// </summary>
[ByRefEvent]
public record struct GetSessionStateAttempt(ICommonSession Session)
{
public ICommonSession Session = Session;
public bool Cancelled;
}
}
2 changes: 1 addition & 1 deletion Robust.Shared/Player/ISharedPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface ISharedPlayerManager
bool HasPlayerData(NetUserId userId);

IEnumerable<SessionData> GetAllPlayerData();
void GetPlayerStates(GameTick fromTick, List<SessionState> states);
void GetPlayerStates(ICommonSession? session, GameTick fromTick, List<SessionState> states);
void UpdateState(ICommonSession commonSession);

void RemoveSession(ICommonSession session, bool removeData = false);
Expand Down
25 changes: 23 additions & 2 deletions Robust.Shared/Player/SharedPlayerManager.State.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;

Expand All @@ -12,16 +13,36 @@ public void Dirty()
LastStateUpdate = Timing.CurTick;
}

public void GetPlayerStates(GameTick fromTick, List<SessionState> states)
public void GetPlayerStates(ICommonSession? session, GameTick fromTick, List<SessionState> states)
{
states.Clear();
if (LastStateUpdate < fromTick)
return;

states.EnsureCapacity(InternalSessions.Count);
var ev = new GetSessionStateAttempt(session);

if (session == null)
{
ev.Cancelled = true;
}
else
{
EntManager.EventBus.RaiseEvent(EventSource.Local, ref ev);
}

foreach (var player in InternalSessions.Values)
{
states.Add(player.State);
if (ev.Cancelled)
{
var copy = player.State.Clone();
copy.Name = string.Empty;
states.Add(copy);
}
else
{
states.Add(player.State);
}
}
}

Expand Down
Loading