Files
wwdpublic/Content.Server/Tabletop/TabletopSystem.Session.cs
SimpleStation14 a22601c7b5 Mirror: Obsolete Logger cleanup for EntitySystems part 2 (#237)
## Mirror of PR #26159: [Obsolete `Logger` cleanup for `EntitySystem`s
part 2](https://github.com/space-wizards/space-station-14/pull/26159)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `7d275a4b5e4188db424cc417c609dced3f9aca89`

PR opened by <img
src="https://avatars.githubusercontent.com/u/27449516?v=4"
width="16"/><a href="https://github.com/LordCarve"> LordCarve</a> at
2024-03-15 20:15:41 UTC

---

PR changed 14 files with 36 additions and 25 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> <!-- Please read these guidelines before opening your PR:
https://docs.spacestation14.io/en/getting-started/pr-guideline -->
> <!-- The text between the arrows are comments - they will not be
visible on your PR. -->
> 
> ## About the PR
> <!-- What did you change in this PR? -->
> Part 2 of 2, continuation of #25941 
> Gets rid of the remaining obsolete `Logger` method calls from all
`EntitySystem`s and uses the expected `Log` to get at the proper
sawmill.
> 
> In particular:
> 1. Make the `ExamineSystemShared`'s `InRangeUnObstructed` non-`static`
(so finally able to use instance-based `Log` rather than `Logger`) and
inject dependency to all systems that were using it as `static`.
> 2. Adjust 4 more `EntitySystem`s to use `Log` rather than `Logger`
that were missed in the previous PR.
> 
> Tested that the game runs and the affected systems direct logs to the
correct sawmill.
> 
> ## Why / Balance
> <!-- Why was it changed? Link any discussions or issues here. Please
discuss how this would affect game balance. -->
> Using Logger directly for logging is marked obsolete. Brings some
order to logs (i.e. all `EntitySystem` logs start with a preceding
`system.`).
> 
> ## Technical details
> <!-- If this is a code change, summarize at high level how your new
code works. This makes it easier to review. -->
> I'm pretty sure `ExamineSystemShared`'s `InRangeUnObstructed` not
being `static` is the intended way since it being `static` dodges the
IoC entirely. It even has dirty hacks such as getting the occluder
system inside to make it happen. I didn't fix any of that as it's beyond
the scope of this PR, but it's another thing that needs improving.
> 
> These are the changes to the log sawmills:
> `ExamineSystemShared.cs`: BEFORE: `root` -> NEW: `system.examine`
> `AtmosphereSystem.Monstermos.cs`: BEFORE: `root` -> NEW:
`system.atmosphere`
> `TypingIndicatorSystem.cs`: BEFORE: `root` -> NEW:
`system.typing_indicator`
> `PiratesRuleSystem.cs` BEFORE: `pirates` -> NEW: `system.pirates_rule`
> `TabletopSystem.Session.cs` BEFORE: `root` -> NEW: `system.tabletop`
> 
> ## Media
> <!-- 
> PRs which make ingame changes (adding clothing, items, new features,
etc) are required to have media attached that showcase the changes.
> Small fixes/refactors are exempt.
> Any media may be used in SS14 progress reports, with clear credit
given.
> 
> If you're unsure whether your PR will require media, ask a maintainer.
> 
> Check the box below to confirm that you have in fact seen this (put an
X in the brackets, like [X]):
> -->
> 
> - [X] I have added screenshots/videos to this PR showcasing its
changes ingame, **or** this PR does not require an ingame showcase
> 
> ## Breaking changes
> <!--
> List any breaking changes, including namespace, public
class/method/field changes, prototype renames; and provide instructions
for fixing them. This will be pasted in #codebase-changes.
> -->
> Some logs now fall under a different sawmill (more precise). Any
software that analyzes logs and makes assumptions on which sawmill do
these logs fall to will need to be adjusted.


</details>

---------

Signed-off-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: SimpleStation14 <Unknown>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
2024-05-28 21:18:15 -04:00

153 lines
6.6 KiB
C#

using System.Numerics;
using Content.Server.Tabletop.Components;
using Content.Shared.Tabletop.Events;
using Robust.Shared.Player;
using Robust.Shared.Utility;
namespace Content.Server.Tabletop
{
public sealed partial class TabletopSystem
{
/// <summary>
/// Ensures that a <see cref="TabletopSession"/> exists on a <see cref="TabletopGameComponent"/>.
/// Creates it and sets it up if it doesn't.
/// </summary>
/// <param name="tabletop">The tabletop game in question.</param>
/// <returns>The session for the given tabletop game.</returns>
public TabletopSession EnsureSession(TabletopGameComponent tabletop)
{
// We already have a session, return it
// TODO: if tables are connected, treat them as a single entity. This can be done by sharing the session.
if (tabletop.Session != null)
return tabletop.Session;
// We make sure that the tabletop map exists before continuing.
EnsureTabletopMap();
// Create new session.
var session = new TabletopSession(TabletopMap, GetNextTabletopPosition());
tabletop.Session = session;
// Since this is the first time opening this session, set up the game
tabletop.Setup.SetupTabletop(session, EntityManager);
Log.Info($"Created tabletop session number {tabletop} at position {session.Position}.");
return session;
}
/// <summary>
/// Cleans up a tabletop game session, deleting every entity in it.
/// </summary>
/// <param name="uid">The UID of the tabletop game entity.</param>
public void CleanupSession(EntityUid uid)
{
if (!EntityManager.TryGetComponent(uid, out TabletopGameComponent? tabletop))
return;
if (tabletop.Session is not { } session)
return;
foreach (var (player, _) in session.Players)
{
CloseSessionFor(player, uid);
}
foreach (var euid in session.Entities)
{
EntityManager.QueueDeleteEntity(euid);
}
tabletop.Session = null;
}
/// <summary>
/// Adds a player to a tabletop game session, sending a message so the tabletop window opens on their end.
/// </summary>
/// <param name="player">The player session in question.</param>
/// <param name="uid">The UID of the tabletop game entity.</param>
public void OpenSessionFor(ICommonSession player, EntityUid uid)
{
if (!EntityManager.TryGetComponent(uid, out TabletopGameComponent? tabletop) || player.AttachedEntity is not {Valid: true} attachedEntity)
return;
// Make sure we have a session, and add the player to it if not added already.
var session = EnsureSession(tabletop);
if (session.Players.ContainsKey(player))
return;
if(EntityManager.TryGetComponent(attachedEntity, out TabletopGamerComponent? gamer))
CloseSessionFor(player, gamer.Tabletop, false);
// Set the entity as an absolute GAMER.
EnsureComp<TabletopGamerComponent>(attachedEntity).Tabletop = uid;
// Create a camera for the gamer to use
var camera = CreateCamera(tabletop, player);
session.Players[player] = new TabletopSessionPlayerData { Camera = camera };
// Tell the gamer to open a viewport for the tabletop game
RaiseNetworkEvent(new TabletopPlayEvent(GetNetEntity(uid), GetNetEntity(camera), Loc.GetString(tabletop.BoardName), tabletop.Size), player.Channel);
}
/// <summary>
/// Removes a player from a tabletop game session, and sends them a message so their tabletop window is closed.
/// </summary>
/// <param name="player">The player in question.</param>
/// <param name="uid">The UID of the tabletop game entity.</param>
/// <param name="removeGamerComponent">Whether to remove the <see cref="TabletopGamerComponent"/> from the player's attached entity.</param>
public void CloseSessionFor(ICommonSession player, EntityUid uid, bool removeGamerComponent = true)
{
if (!EntityManager.TryGetComponent(uid, out TabletopGameComponent? tabletop) || tabletop.Session is not { } session)
return;
if (!session.Players.TryGetValue(player, out var data))
return;
if(removeGamerComponent && player.AttachedEntity is {} attachedEntity && EntityManager.TryGetComponent(attachedEntity, out TabletopGamerComponent? gamer))
{
// We invalidate this to prevent an infinite feedback from removing the component.
gamer.Tabletop = EntityUid.Invalid;
// You stop being a gamer.......
EntityManager.RemoveComponent<TabletopGamerComponent>(attachedEntity);
}
session.Players.Remove(player);
session.Entities.Remove(data.Camera);
// Deleting the view subscriber automatically cleans up subscriptions, no need to do anything else.
EntityManager.QueueDeleteEntity(data.Camera);
}
/// <summary>
/// A helper method that creates a camera for a specified player, in a tabletop game session.
/// </summary>
/// <param name="tabletop">The tabletop game component in question.</param>
/// <param name="player">The player in question.</param>
/// <param name="offset">An offset from the tabletop position for the camera. Zero by default.</param>
/// <returns>The UID of the camera entity.</returns>
private EntityUid CreateCamera(TabletopGameComponent tabletop, ICommonSession player, Vector2 offset = default)
{
DebugTools.AssertNotNull(tabletop.Session);
var session = tabletop.Session!;
// Spawn an empty entity at the coordinates
var camera = EntityManager.SpawnEntity(null, session.Position.Offset(offset));
// Add an eye component and disable FOV
var eyeComponent = EnsureComp<EyeComponent>(camera);
_eye.SetDrawFov(camera, false, eyeComponent);
_eye.SetZoom(camera, tabletop.CameraZoom, eyeComponent);
// Add the user to the view subscribers. If there is no player session, just skip this step
_viewSubscriberSystem.AddViewSubscriber(camera, player);
return camera;
}
}
}