From: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Date: Tue, 16 Jan 2024 23:02:26 +0000 (-0500) Subject: try and purify cryosleeper code (#24165) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=e145a8f6963f1a61f49894c09047a0212c90931e;p=space-station-14.git try and purify cryosleeper code (#24165) --- diff --git a/Content.Server/Bed/Cryostorage/CryostorageSystem.cs b/Content.Server/Bed/Cryostorage/CryostorageSystem.cs index 799bb82eff..f82f835d87 100644 --- a/Content.Server/Bed/Cryostorage/CryostorageSystem.cs +++ b/Content.Server/Bed/Cryostorage/CryostorageSystem.cs @@ -71,13 +71,13 @@ public sealed class CryostorageSystem : SharedCryostorageSystem private void OnRemoveItemBuiMessage(Entity ent, ref CryostorageRemoveItemBuiMessage args) { - var comp = ent.Comp; + var (_, comp) = ent; if (args.Session.AttachedEntity is not { } attachedEntity) return; - var cryoContained = GetEntity(args.Entity); + var cryoContained = GetEntity(args.StoredEntity); - if (!comp.StoredPlayers.Contains(cryoContained)) + if (!comp.StoredPlayers.Contains(cryoContained) || !IsInPausedMap(cryoContained)) return; if (!HasComp(attachedEntity)) @@ -134,6 +134,7 @@ public sealed class CryostorageSystem : SharedCryostorageSystem if (comp.GracePeriodEndTime != null) comp.GracePeriodEndTime = Timing.CurTime + cryostorageComponent.NoMindGracePeriod; + comp.AllowReEnteringBody = false; comp.UserId = args.Mind.Comp.UserId; } @@ -147,9 +148,7 @@ public sealed class CryostorageSystem : SharedCryostorageSystem if (args.NewStatus is SessionStatus.Disconnected or SessionStatus.Zombie) { - if (CryoSleepRejoiningEnabled) - containedComponent.StoredWhileDisconnected = true; - + containedComponent.AllowReEnteringBody = true; var delay = CompOrNull(containedComponent.Cryostorage)?.NoMindGracePeriod ?? TimeSpan.Zero; containedComponent.GracePeriodEndTime = Timing.CurTime + delay; containedComponent.UserId = args.Session.UserId; @@ -196,15 +195,17 @@ public sealed class CryostorageSystem : SharedCryostorageSystem return; } - if (!comp.StoredWhileDisconnected && - userId != null && - Mind.TryGetMind(userId.Value, out var mind) && - mind.Value.Comp.Session?.AttachedEntity == ent) + if (!CryoSleepRejoiningEnabled || !comp.AllowReEnteringBody) { - _gameTicker.OnGhostAttempt(mind.Value, false); + if (userId != null && Mind.TryGetMind(userId.Value, out var mind)) + { + _gameTicker.OnGhostAttempt(mind.Value, false); + } } + comp.AllowReEnteringBody = false; _transform.SetParent(ent, PausedMap.Value); cryostorageComponent.StoredPlayers.Add(ent); + Dirty(ent, comp); UpdateCryostorageUIState((cryostorageEnt.Value, cryostorageComponent)); AdminLog.Add(LogType.Action, LogImpact.High, $"{ToPrettyString(ent):player} was entered into cryostorage inside of {ToPrettyString(cryostorageEnt.Value)}"); } @@ -212,13 +213,13 @@ public sealed class CryostorageSystem : SharedCryostorageSystem private void HandleCryostorageReconnection(Entity entity) { var (uid, comp) = entity; - if (!CryoSleepRejoiningEnabled || !comp.StoredWhileDisconnected) + if (!CryoSleepRejoiningEnabled || !IsInPausedMap(uid)) return; // how did you destroy these? they're indestructible. if (comp.Cryostorage is not { } cryostorage || TerminatingOrDeleted(cryostorage) || - !TryComp(comp.Cryostorage, out var cryostorageComponent)) + !TryComp(cryostorage, out var cryostorageComponent)) { QueueDel(entity); return; @@ -234,8 +235,7 @@ public sealed class CryostorageSystem : SharedCryostorageSystem } comp.GracePeriodEndTime = null; - comp.StoredWhileDisconnected = false; - cryostorageComponent.StoredPlayers.Remove(entity); + cryostorageComponent.StoredPlayers.Remove(uid); AdminLog.Add(LogType.Action, LogImpact.High, $"{ToPrettyString(entity):player} re-entered the game from cryostorage {ToPrettyString(cryostorage)}"); UpdateCryostorageUIState((cryostorage, cryostorageComponent)); } @@ -295,7 +295,7 @@ public sealed class CryostorageSystem : SharedCryostorageSystem var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out var containedComp)) { - if (containedComp.GracePeriodEndTime == null || containedComp.StoredWhileDisconnected) + if (containedComp.GracePeriodEndTime == null) continue; if (Timing.CurTime < containedComp.GracePeriodEndTime) diff --git a/Content.Shared/Bed/Cryostorage/CryostorageComponent.cs b/Content.Shared/Bed/Cryostorage/CryostorageComponent.cs index c7aa00c300..48f451d6bb 100644 --- a/Content.Shared/Bed/Cryostorage/CryostorageComponent.cs +++ b/Content.Shared/Bed/Cryostorage/CryostorageComponent.cs @@ -9,6 +9,7 @@ namespace Content.Shared.Bed.Cryostorage; /// will delete their body and redistribute their items. /// [RegisterComponent, NetworkedComponent] +[AutoGenerateComponentState] public sealed partial class CryostorageComponent : Component { [DataField, ViewVariables(VVAccess.ReadWrite)] @@ -18,18 +19,21 @@ public sealed partial class CryostorageComponent : Component /// How long a player can remain inside Cryostorage before automatically being taken care of, given that they have no mind. /// [DataField, ViewVariables(VVAccess.ReadWrite)] + [AutoNetworkedField] public TimeSpan NoMindGracePeriod = TimeSpan.FromSeconds(30f); /// /// How long a player can remain inside Cryostorage before automatically being taken care of. /// [DataField, ViewVariables(VVAccess.ReadWrite)] + [AutoNetworkedField] public TimeSpan GracePeriod = TimeSpan.FromMinutes(5f); /// /// A list of players who have actively entered cryostorage. /// [DataField] + [AutoNetworkedField] public List StoredPlayers = new(); /// @@ -83,7 +87,7 @@ public sealed class CryostorageBuiState : BoundUserInterfaceState [Serializable, NetSerializable] public sealed class CryostorageRemoveItemBuiMessage : BoundUserInterfaceMessage { - public NetEntity Entity; + public NetEntity StoredEntity; public string Key; @@ -95,9 +99,9 @@ public sealed class CryostorageRemoveItemBuiMessage : BoundUserInterfaceMessage Inventory } - public CryostorageRemoveItemBuiMessage(NetEntity entity, string key, RemovalType type) + public CryostorageRemoveItemBuiMessage(NetEntity storedEntity, string key, RemovalType type) { - Entity = entity; + StoredEntity = storedEntity; Key = key; Type = type; } diff --git a/Content.Shared/Bed/Cryostorage/CryostorageContainedComponent.cs b/Content.Shared/Bed/Cryostorage/CryostorageContainedComponent.cs index 42a11aabe2..5ab639bd3c 100644 --- a/Content.Shared/Bed/Cryostorage/CryostorageContainedComponent.cs +++ b/Content.Shared/Bed/Cryostorage/CryostorageContainedComponent.cs @@ -12,10 +12,11 @@ namespace Content.Shared.Bed.Cryostorage; public sealed partial class CryostorageContainedComponent : Component { /// - /// Whether or not this entity is being stored on another map or is just chilling in a container + /// If true, the player's mind won't be removed from their body when they are moved into cryosleep + /// allowing them to rejoin later. /// - [DataField, AutoNetworkedField] - public bool StoredWhileDisconnected; + [DataField] + public bool AllowReEnteringBody; /// /// The time at which the cryostorage grace period ends. diff --git a/Content.Shared/Bed/Cryostorage/SharedCryostorageSystem.cs b/Content.Shared/Bed/Cryostorage/SharedCryostorageSystem.cs index e781433783..14a76ee45c 100644 --- a/Content.Shared/Bed/Cryostorage/SharedCryostorageSystem.cs +++ b/Content.Shared/Bed/Cryostorage/SharedCryostorageSystem.cs @@ -42,7 +42,7 @@ public abstract class SharedCryostorageSystem : EntitySystem SubscribeLocalEvent(OnRoundRestart); - _configuration.OnValueChanged(CCVars.GameCryoSleepRejoining, OnCvarChanged); + _configuration.OnValueChanged(CCVars.GameCryoSleepRejoining, OnCvarChanged, true); } public override void Shutdown() @@ -132,8 +132,8 @@ public abstract class SharedCryostorageSystem : EntitySystem private void OnRemovedContained(Entity ent, ref EntGotRemovedFromContainerMessage args) { - var (_, comp) = ent; - if (!comp.StoredWhileDisconnected) + var (uid, comp) = ent; + if (!IsInPausedMap(uid)) RemCompDeferred(ent, comp); } @@ -176,4 +176,12 @@ public abstract class SharedCryostorageSystem : EntitySystem _mapManager.SetMapPaused(map, true); PausedMap = _mapManager.GetMapEntityId(map); } + + public bool IsInPausedMap(Entity entity) + { + var (_, comp) = entity; + comp ??= Transform(entity); + + return comp.MapUid != null && comp.MapUid == PausedMap; + } }