Files
wwdpublic/Content.Client/Light/RgbLightControllerSystem.cs
SimpleStation14 b1e3b79253 Mirror: Obsolete Logger cleanup for EntitySystems (#132)
## Mirror of PR #25941: [Obsolete `Logger` cleanup for
`EntitySystem`s](https://github.com/space-wizards/space-station-14/pull/25941)
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)

###### `aafe81512258b5a80776ada1f471b58e7507ca2d`

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-09 12:19:14 UTC
PR merged by <img
src="https://avatars.githubusercontent.com/u/19864447?v=4"
width="16"/><a href="https://github.com/web-flow"> web-flow</a> at
2024-03-10 00:15:13 UTC

---

PR changed 25 files with 41 additions and 45 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? -->
> Changed almost all[^1] obsolete `Logger` calls in Content's
`EntitySystem`s to use `Log` instead. `Log` automatically selects the
system-specific `Sawmill`, so this isn't just a refactor - it makes logs
slightly more accurate (puts them in appropriate sawmill/context rather
than the root 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. Assumed this
is a desired change.
> 
> ## Technical details
> <!-- If this is a code change, summarize at high level how your new
code works. This makes it easier to review. -->
> This changes some log contexts, but generally in a desirable way:
> - For most, it put logs in `system.appropriate` `Sawmill` rather than
root.
> - For some that were forced into another `Sawmill` it changes it to a
more specific one, i.e. from `atmos` it becomes `system.gas_filter` or
`system.automatic_atmos` and `system.station` into
`system.station_jobs`.
> - For the rest it remains unchanged
> 
> **I assumed that all of the above was desirable because this seems to
be the standard convention** - I imagine that was the idea behind the
`Log` in all `EntitySystem`s coupled with `[Obsolete] Logger` methods -
**but if my assumptions are incorrect and/or there are exceptions,
please let me know and I will adjust.**
> 
> [^1]: There is only one `EntitySystem` that I didn't update -
`ExamineSystemShared`. That is because the `Logger` is in a `static`
method and refactoring that away causes a lot of cascading changes. May
do it as a separate PR to avoid overly diluting this one.
> 
> ## 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.
> -->
> Log output changes. `EntitySystem` logs now go to the expected
`Sawmill` for the system rather than hardcoded/root one.
> Any log analyzers anticipating these logs' context must be updated.


</details>

Co-authored-by: LordCarve <27449516+LordCarve@users.noreply.github.com>
2024-05-04 19:54:48 -04:00

219 lines
8.0 KiB
C#

using System.Linq;
using Content.Client.Items.Systems;
using Content.Shared.Clothing;
using Content.Shared.Hands;
using Content.Shared.Inventory.Events;
using Content.Shared.Light;
using Content.Shared.Light.Components;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Map.Components;
using Robust.Shared.Timing;
using static Robust.Client.GameObjects.SpriteComponent;
namespace Content.Client.Light
{
public sealed class RgbLightControllerSystem : SharedRgbLightControllerSystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly ItemSystem _itemSystem = default!;
[Dependency] private readonly SharedPointLightSystem _lights = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RgbLightControllerComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<RgbLightControllerComponent, ComponentShutdown>(OnComponentShutdown);
SubscribeLocalEvent<RgbLightControllerComponent, ComponentStartup>(OnComponentStart);
SubscribeLocalEvent<RgbLightControllerComponent, GotUnequippedEvent>(OnGotUnequipped);
SubscribeLocalEvent<RgbLightControllerComponent, EquipmentVisualsUpdatedEvent>(OnEquipmentVisualsUpdated);
SubscribeLocalEvent<RgbLightControllerComponent, HeldVisualsUpdatedEvent>(OnHeldVisualsUpdated);
}
private void OnComponentStart(EntityUid uid, RgbLightControllerComponent rgb, ComponentStartup args)
{
GetOriginalColors(uid, rgb);
// trigger visuals updated events
_itemSystem.VisualsChanged(uid);
}
private void OnComponentShutdown(EntityUid uid, RgbLightControllerComponent rgb, ComponentShutdown args)
{
if (LifeStage(uid) >= EntityLifeStage.Terminating)
return;
ResetOriginalColors(uid, rgb);
// and reset any in-hands or clothing sprites
_itemSystem.VisualsChanged(uid);
}
private void OnGotUnequipped(EntityUid uid, RgbLightControllerComponent rgb, GotUnequippedEvent args)
{
rgb.Holder = null;
rgb.HolderLayers = null;
}
private void OnHeldVisualsUpdated(EntityUid uid, RgbLightControllerComponent rgb, HeldVisualsUpdatedEvent args)
{
if (args.RevealedLayers.Count == 0)
{
rgb.Holder = null;
rgb.HolderLayers = null;
return;
}
rgb.Holder = args.User;
rgb.HolderLayers = new();
if (!TryComp(args.User, out SpriteComponent? sprite))
return;
foreach (var key in args.RevealedLayers)
{
if (!sprite.LayerMapTryGet(key, out var index) || sprite[index] is not Layer layer)
continue;
if (layer.ShaderPrototype == "unshaded")
rgb.HolderLayers.Add(key);
}
}
private void OnEquipmentVisualsUpdated(EntityUid uid, RgbLightControllerComponent rgb, EquipmentVisualsUpdatedEvent args)
{
rgb.Holder = args.Equipee;
rgb.HolderLayers = new();
if (!TryComp(args.Equipee, out SpriteComponent? sprite))
return;
foreach (var key in args.RevealedLayers)
{
if (!sprite.LayerMapTryGet(key, out var index) || sprite[index] is not Layer layer)
continue;
if (layer.ShaderPrototype == "unshaded")
rgb.HolderLayers.Add(key);
}
}
private void OnHandleState(EntityUid uid, RgbLightControllerComponent rgb, ref ComponentHandleState args)
{
if (args.Current is not RgbLightControllerState state)
return;
ResetOriginalColors(uid, rgb);
rgb.CycleRate = state.CycleRate;
rgb.Layers = state.Layers;
GetOriginalColors(uid, rgb);
}
private void GetOriginalColors(EntityUid uid, RgbLightControllerComponent? rgb = null, PointLightComponent? light = null, SpriteComponent? sprite = null)
{
if (!Resolve(uid, ref rgb, ref sprite, ref light))
return;
rgb.OriginalLightColor = light.Color;
rgb.OriginalLayerColors = new();
var layerCount = sprite.AllLayers.Count();
// if layers is null, get unshaded layers
if (rgb.Layers == null)
{
rgb.Layers = new();
for (var i = 0; i < layerCount; i++)
{
if (sprite[i] is Layer layer && layer.ShaderPrototype == "unshaded")
{
rgb.Layers.Add(i);
rgb.OriginalLayerColors[i] = layer.Color;
}
}
return;
}
foreach (var index in rgb.Layers.ToArray())
{
if (index < layerCount)
rgb.OriginalLayerColors[index] = sprite[index].Color;
else
{
// admeme fuck-ups or bad yaml?
Log.Warning($"RGB light attempted to use invalid sprite index {index} on entity {ToPrettyString(uid)}");
rgb.Layers.Remove(index);
}
}
}
private void ResetOriginalColors(EntityUid uid, RgbLightControllerComponent? rgb = null, PointLightComponent? light = null, SpriteComponent? sprite = null)
{
if (!Resolve(uid, ref rgb, ref sprite, ref light, false))
return;
_lights.SetColor(uid, rgb.OriginalLightColor, light);
if (rgb.Layers == null || rgb.OriginalLayerColors == null)
return;
foreach (var (layer, color) in rgb.OriginalLayerColors)
{
sprite.LayerSetColor(layer, color);
}
}
public override void FrameUpdate(float frameTime)
{
var lightQuery = EntityQueryEnumerator<RgbLightControllerComponent, PointLightComponent, SpriteComponent>();
while (lightQuery.MoveNext(out var uid, out var rgb, out var light, out var sprite))
{
var color = GetCurrentRgbColor(_gameTiming.RealTime, rgb.CreationTick.Value * _gameTiming.TickPeriod, (uid, rgb));
_lights.SetColor(uid, color, light);
if (rgb.Layers != null)
{
foreach (var index in rgb.Layers)
{
if (sprite.TryGetLayer(index, out var layer))
layer.Color = color;
}
}
// is the entity being held by someone?
if (rgb.HolderLayers == null || !TryComp(rgb.Holder, out SpriteComponent? holderSprite))
continue;
foreach (var layer in rgb.HolderLayers)
{
if (holderSprite.LayerMapTryGet(layer, out var index))
holderSprite.LayerSetColor(index, color);
}
}
var mapQuery = EntityQueryEnumerator<MapLightComponent, RgbLightControllerComponent>();
while (mapQuery.MoveNext(out var uid, out var map, out var rgb))
{
var color = GetCurrentRgbColor(_gameTiming.RealTime, rgb.CreationTick.Value * _gameTiming.TickPeriod, (uid, rgb));
map.AmbientLightColor = color;
}
}
public static Color GetCurrentRgbColor(TimeSpan curTime, TimeSpan offset, Entity<RgbLightControllerComponent> rgb)
{
return Color.FromHsv(new Vector4(
(float) (((curTime.TotalSeconds - offset.TotalSeconds) * rgb.Comp.CycleRate + Math.Abs(rgb.Owner.Id * 0.1)) % 1),
1.0f,
1.0f,
1.0f
));
}
}
}