From c5e5729bd4f9be2c9549323dd6b52e589af82bf8 Mon Sep 17 00:00:00 2001 From: Paul Ritter Date: Thu, 3 Nov 2022 02:41:12 +0100 Subject: [PATCH] removes beforeserialization hook (#12319) --- .../Gravity/GravityGeneratorVisualizer.cs | 37 +++----- .../Research/TechnologyDatabaseComponent.cs | 9 +- .../UI/ResearchConsoleBoundUserInterface.cs | 2 +- .../Components/GridAtmosphereComponent.cs | 40 +-------- .../AtmosphereSystem.GridAtmosphere.cs | 27 ++---- .../AtmosphereSystem.Processing.cs | 4 +- .../TileAtmosCollectionSerializer.cs | 89 +++++++++++++++++++ Content.Server/Atmos/TileAtmosphere.cs | 3 +- Content.Server/Lathe/LatheSystem.cs | 25 +++--- .../Research/Systems/ResearchSystem.Server.cs | 4 +- .../Systems/ResearchSystem.Technology.cs | 10 +-- Content.Shared/Alert/AlertOrderPrototype.cs | 77 ++++++++-------- .../Doors/Components/DoorComponent.cs | 33 +++---- .../SharedTechnologyDatabaseComponent.cs | 68 ++------------ 14 files changed, 199 insertions(+), 229 deletions(-) create mode 100644 Content.Server/Atmos/Serialization/TileAtmosCollectionSerializer.cs diff --git a/Content.Client/Gravity/GravityGeneratorVisualizer.cs b/Content.Client/Gravity/GravityGeneratorVisualizer.cs index f9f0bed815..6b962b9683 100644 --- a/Content.Client/Gravity/GravityGeneratorVisualizer.cs +++ b/Content.Client/Gravity/GravityGeneratorVisualizer.cs @@ -1,43 +1,32 @@ -using System; -using System.Collections.Generic; +using System.Linq; using Content.Shared.Gravity; using JetBrains.Annotations; using Robust.Client.GameObjects; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Serialization; -using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Client.Gravity { [UsedImplicitly] - public sealed class GravityGeneratorVisualizer : AppearanceVisualizer, ISerializationHooks + public sealed class GravityGeneratorVisualizer : AppearanceVisualizer { [DataField("spritemap")] - private Dictionary _rawSpriteMap = new(); - private Dictionary _spriteMap = new(); - - void ISerializationHooks.BeforeSerialization() + private Dictionary _rawSpriteMap { - _rawSpriteMap = new Dictionary(); - foreach (var (status, sprite) in _spriteMap) + get => _spriteMap.ToDictionary(x => x.Value.ToString().ToLower(), x => x.Value); + set { - _rawSpriteMap.Add(status.ToString().ToLower(), sprite); - } - } - - void ISerializationHooks.AfterDeserialization() - { - // Get Sprites for each status - foreach (var status in (GravityGeneratorStatus[]) Enum.GetValues(typeof(GravityGeneratorStatus))) - { - if (_rawSpriteMap.TryGetValue(status.ToString().ToLower(), out var sprite)) + // Get Sprites for each status + foreach (var status in (GravityGeneratorStatus[]) Enum.GetValues(typeof(GravityGeneratorStatus))) { - _spriteMap[status] = sprite; + if (value.TryGetValue(status.ToString().ToLower(), out var sprite)) + { + _spriteMap[status] = sprite; + } } } } + private Dictionary _spriteMap = new(); + [Obsolete("Subscribe to your component being initialised instead.")] public override void InitializeEntity(EntityUid entity) { diff --git a/Content.Client/Research/TechnologyDatabaseComponent.cs b/Content.Client/Research/TechnologyDatabaseComponent.cs index f69bdfdda3..e68bd4b489 100644 --- a/Content.Client/Research/TechnologyDatabaseComponent.cs +++ b/Content.Client/Research/TechnologyDatabaseComponent.cs @@ -1,8 +1,5 @@ -using System; using Content.Shared.Research.Components; using Content.Shared.Research.Prototypes; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Prototypes; namespace Content.Client.Research @@ -21,14 +18,14 @@ namespace Content.Client.Research if (curState is not TechnologyDatabaseState state) return; - Technologies.Clear(); + TechnologyIds.Clear(); var protoManager = IoCManager.Resolve(); foreach (var techID in state.Technologies) { - if (!protoManager.TryIndex(techID, out TechnologyPrototype? technology)) continue; - Technologies.Add(technology); + if (!protoManager.HasIndex(techID)) continue; + TechnologyIds.Add(techID); } OnDatabaseUpdated?.Invoke(); diff --git a/Content.Client/Research/UI/ResearchConsoleBoundUserInterface.cs b/Content.Client/Research/UI/ResearchConsoleBoundUserInterface.cs index e7333ca298..0d0b31aa3b 100644 --- a/Content.Client/Research/UI/ResearchConsoleBoundUserInterface.cs +++ b/Content.Client/Research/UI/ResearchConsoleBoundUserInterface.cs @@ -53,7 +53,7 @@ namespace Content.Client.Research.UI public bool IsTechnologyUnlocked(TechnologyPrototype technology) { - return _technologyDatabase?.IsTechnologyUnlocked(technology) ?? false; + return _technologyDatabase?.IsTechnologyUnlocked(technology.ID) ?? false; } public bool CanUnlockTechnology(TechnologyPrototype technology) diff --git a/Content.Server/Atmos/Components/GridAtmosphereComponent.cs b/Content.Server/Atmos/Components/GridAtmosphereComponent.cs index 45a1c0fcb6..f633152e32 100644 --- a/Content.Server/Atmos/Components/GridAtmosphereComponent.cs +++ b/Content.Server/Atmos/Components/GridAtmosphereComponent.cs @@ -1,7 +1,7 @@ using Content.Server.Atmos.EntitySystems; using Content.Server.Atmos.Piping.Components; +using Content.Server.Atmos.Serialization; using Content.Server.NodeContainer.NodeGroups; -using Robust.Shared.Serialization; namespace Content.Server.Atmos.Components { @@ -10,7 +10,7 @@ namespace Content.Server.Atmos.Components /// [RegisterComponent, Serializable, Access(typeof(AtmosphereSystem), typeof(GasTileOverlaySystem), typeof(AtmosDebugOverlaySystem))] - public sealed class GridAtmosphereComponent : Component, ISerializationHooks + public sealed class GridAtmosphereComponent : Component { [ViewVariables(VVAccess.ReadWrite)] public bool Simulated { get; set; } = true; @@ -24,13 +24,8 @@ namespace Content.Server.Atmos.Components [ViewVariables] public int UpdateCounter { get; set; } = 1; // DO NOT SET TO ZERO BY DEFAULT! It will break roundstart atmos... - [DataField("uniqueMixes")] - public List? UniqueMixes; - - [DataField("tiles")] - public Dictionary? TilesUniqueMixes; - [ViewVariables] + [IncludeDataField(customTypeSerializer:typeof(TileAtmosCollectionSerializer))] public readonly Dictionary Tiles = new(1000); [ViewVariables] @@ -95,34 +90,5 @@ namespace Content.Server.Atmos.Components [ViewVariables] public AtmosphereProcessingState State { get; set; } = AtmosphereProcessingState.Revalidate; - - void ISerializationHooks.BeforeSerialization() - { - var uniqueMixes = new List(); - var uniqueMixHash = new Dictionary(); - var tiles = new Dictionary(); - - foreach (var (indices, tile) in Tiles) - { - if (tile.Air == null) continue; - - if (uniqueMixHash.TryGetValue(tile.Air, out var index)) - { - tiles[indices] = index; - continue; - } - - uniqueMixes.Add(tile.Air); - var newIndex = uniqueMixes.Count - 1; - uniqueMixHash[tile.Air] = newIndex; - tiles[indices] = newIndex; - } - - if (uniqueMixes.Count == 0) uniqueMixes = null; - if (tiles.Count == 0) tiles = null; - - UniqueMixes = uniqueMixes; - TilesUniqueMixes = tiles; - } } } diff --git a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.GridAtmosphere.cs b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.GridAtmosphere.cs index df0bd23d97..5dcf40525b 100644 --- a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.GridAtmosphere.cs +++ b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.GridAtmosphere.cs @@ -2,7 +2,6 @@ using System.Linq; using Content.Server.Atmos.Components; using Content.Server.Atmos.Reactions; using Content.Shared.Atmos; -using Content.Shared.Maps; using Robust.Shared.Map; using Robust.Shared.Utility; @@ -44,29 +43,13 @@ public sealed partial class AtmosphereSystem { base.Initialize(); - gridAtmosphere.Tiles.Clear(); - if (!TryComp(uid, out IMapGridComponent? mapGrid)) return; - if (gridAtmosphere.TilesUniqueMixes != null) + foreach (var (indices, tile) in gridAtmosphere.Tiles) { - foreach (var (indices, mix) in gridAtmosphere.TilesUniqueMixes) - { - try - { - gridAtmosphere.Tiles.Add(indices, new TileAtmosphere(mapGrid.Owner, indices, - gridAtmosphere.UniqueMixes![mix].Clone())); - } - catch (ArgumentOutOfRangeException) - { - Logger.Error( - $"Error during atmos serialization! Tile at {indices} points to an unique mix ({mix}) out of range!"); - throw; - } - - gridAtmosphere.InvalidatedCoords.Add(indices); - } + gridAtmosphere.InvalidatedCoords.Add(indices); + tile.GridIndex = uid; } GridRepopulateTiles(mapGrid.Grid, gridAtmosphere); @@ -334,7 +317,7 @@ public sealed partial class AtmosphereSystem { adjacent = new TileAtmosphere(tile.GridIndex, otherIndices, GetTileMixture(null, mapUid, otherIndices), - space:IsTileSpace(null, mapUid, otherIndices, mapGridComp)); + space: IsTileSpace(null, mapUid, otherIndices, mapGridComp)); } var oppositeDirection = direction.GetOpposite(); @@ -531,7 +514,7 @@ public sealed partial class AtmosphereSystem { if (!gridAtmosphere.Tiles.ContainsKey(tile.GridIndices)) gridAtmosphere.Tiles[tile.GridIndices] = new TileAtmosphere(tile.GridUid, tile.GridIndices, - new GasMixture(volume) {Temperature = Atmospherics.T20C}); + new GasMixture(volume) { Temperature = Atmospherics.T20C }); gridAtmosphere.InvalidatedCoords.Add(tile.GridIndices); } diff --git a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Processing.cs b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Processing.cs index 60141d14c4..8d43d0130e 100644 --- a/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Processing.cs +++ b/Content.Server/Atmos/EntitySystems/AtmosphereSystem.Processing.cs @@ -3,7 +3,6 @@ using Content.Server.Atmos.Piping.Components; using Content.Server.NodeContainer.NodeGroups; using Content.Shared.Atmos; using Content.Shared.Maps; -using Robust.Shared.Map; using Robust.Shared.Physics.Components; using Robust.Shared.Timing; @@ -59,7 +58,8 @@ namespace Content.Server.Atmos.EntitySystems { if (!atmosphere.Tiles.TryGetValue(indices, out var tile)) { - tile = new TileAtmosphere(mapGrid.GridEntityId, indices, new GasMixture(volume){Temperature = Atmospherics.T20C}); + tile = new TileAtmosphere(mapGrid.GridEntityId, indices, + new GasMixture(volume) { Temperature = Atmospherics.T20C }); atmosphere.Tiles[indices] = tile; } diff --git a/Content.Server/Atmos/Serialization/TileAtmosCollectionSerializer.cs b/Content.Server/Atmos/Serialization/TileAtmosCollectionSerializer.cs new file mode 100644 index 0000000000..f85bb03ebc --- /dev/null +++ b/Content.Server/Atmos/Serialization/TileAtmosCollectionSerializer.cs @@ -0,0 +1,89 @@ +using Robust.Shared.Serialization.Manager; +using Robust.Shared.Serialization.Markdown; +using Robust.Shared.Serialization.Markdown.Mapping; +using Robust.Shared.Serialization.Markdown.Validation; +using Robust.Shared.Serialization.TypeSerializers.Interfaces; + +namespace Content.Server.Atmos.Serialization; + +public sealed class TileAtmosCollectionSerializer : ITypeSerializer, MappingDataNode> +{ + public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node, + IDependencyCollection dependencies, ISerializationContext? context = null) + { + return serializationManager.ValidateNode(node, context); + } + + public Dictionary Read(ISerializationManager serializationManager, MappingDataNode node, IDependencyCollection dependencies, + bool skipHook, ISerializationContext? context = null, Dictionary? value = default) + { + var data = serializationManager.Read(node, context, skipHook); + var tiles = new Dictionary(); + if (data.TilesUniqueMixes != null) + { + foreach (var (indices, mix) in data.TilesUniqueMixes) + { + try + { + tiles.Add(indices, new TileAtmosphere(EntityUid.Invalid, indices, + data.UniqueMixes![mix].Clone())); + } + catch (ArgumentOutOfRangeException) + { + Logger.Error( + $"Error during atmos serialization! Tile at {indices} points to an unique mix ({mix}) out of range!"); + } + } + } + + return tiles; + } + + public DataNode Write(ISerializationManager serializationManager, Dictionary value, IDependencyCollection dependencies, + bool alwaysWrite = false, ISerializationContext? context = null) + { + var uniqueMixes = new List(); + var uniqueMixHash = new Dictionary(); + var tiles = new Dictionary(); + + foreach (var (indices, tile) in value) + { + if (tile.Air == null) continue; + + if (uniqueMixHash.TryGetValue(tile.Air, out var index)) + { + tiles[indices] = index; + continue; + } + + uniqueMixes.Add(tile.Air); + var newIndex = uniqueMixes.Count - 1; + uniqueMixHash[tile.Air] = newIndex; + tiles[indices] = newIndex; + } + + if (uniqueMixes.Count == 0) uniqueMixes = null; + if (tiles.Count == 0) tiles = null; + + return serializationManager.WriteValue(new TileAtmosData + { + UniqueMixes = uniqueMixes, + TilesUniqueMixes = tiles + }, alwaysWrite, context); + } + + public Dictionary Copy(ISerializationManager serializationManager, Dictionary source, Dictionary target, bool skipHook, + ISerializationContext? context = null) + { + serializationManager.Copy(source, ref target, context, skipHook); + return target; + } + + [DataDefinition] + private struct TileAtmosData + { + [DataField("uniqueMixes")] public List? UniqueMixes; + + [DataField("tiles")] public Dictionary? TilesUniqueMixes; + } +} diff --git a/Content.Server/Atmos/TileAtmosphere.cs b/Content.Server/Atmos/TileAtmosphere.cs index 76948e1636..95ba843fb5 100644 --- a/Content.Server/Atmos/TileAtmosphere.cs +++ b/Content.Server/Atmos/TileAtmosphere.cs @@ -68,7 +68,8 @@ namespace Content.Server.Atmos public AtmosDirection LastPressureDirection; [ViewVariables] - public EntityUid GridIndex { get; } + [Access(typeof(AtmosphereSystem))] + public EntityUid GridIndex { get; set; } [ViewVariables] public TileRef? Tile => GridIndices.GetTileRef(GridIndex); diff --git a/Content.Server/Lathe/LatheSystem.cs b/Content.Server/Lathe/LatheSystem.cs index 45e6e943fd..14878721d2 100644 --- a/Content.Server/Lathe/LatheSystem.cs +++ b/Content.Server/Lathe/LatheSystem.cs @@ -1,21 +1,21 @@ using System.Diagnostics.CodeAnalysis; -using Content.Server.Lathe.Components; -using Content.Shared.Lathe; -using Content.Shared.Materials; -using Content.Shared.Research.Prototypes; -using Content.Server.Research.Components; -using Content.Server.Research; -using Content.Shared.Research.Components; -using Robust.Server.GameObjects; -using Robust.Shared.Prototypes; -using JetBrains.Annotations; using System.Linq; using Content.Server.Construction; +using Content.Server.Lathe.Components; using Content.Server.Materials; using Content.Server.Power.Components; using Content.Server.Power.EntitySystems; +using Content.Server.Research; +using Content.Server.Research.Components; using Content.Server.UserInterface; +using Content.Shared.Lathe; +using Content.Shared.Materials; +using Content.Shared.Research.Components; +using Content.Shared.Research.Prototypes; +using JetBrains.Annotations; +using Robust.Server.GameObjects; using Robust.Server.Player; +using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Server.Lathe @@ -202,8 +202,9 @@ namespace Content.Server.Lathe return; //gets all of the techs that are unlocked and also in the DynamicRecipes list - var allTechs = (from tech in component.Technologies - from recipe in tech.UnlockedRecipes + var allTechs = (from technology in from tech in component.TechnologyIds + select _proto.Index(tech) + from recipe in technology.UnlockedRecipes where latheComponent.DynamicRecipes.Contains(recipe) select recipe).ToList(); diff --git a/Content.Server/Research/Systems/ResearchSystem.Server.cs b/Content.Server/Research/Systems/ResearchSystem.Server.cs index 76251a546f..c973b0b198 100644 --- a/Content.Server/Research/Systems/ResearchSystem.Server.cs +++ b/Content.Server/Research/Systems/ResearchSystem.Server.cs @@ -1,6 +1,6 @@ using Content.Server.Power.EntitySystems; -using Content.Server.Station.Systems; using Content.Server.Research.Components; +using Content.Server.Station.Systems; using Content.Shared.Research.Prototypes; namespace Content.Server.Research; @@ -70,7 +70,7 @@ public sealed partial class ResearchSystem TechnologyDatabaseComponent? databaseComponent = null) { if (!Resolve(component.Owner, ref databaseComponent, false)) return false; - return databaseComponent.IsTechnologyUnlocked(prototype); + return databaseComponent.IsTechnologyUnlocked(prototype.ID); } public bool CanUnlockTechnology(ResearchServerComponent component, TechnologyPrototype technology, TechnologyDatabaseComponent? databaseComponent = null) diff --git a/Content.Server/Research/Systems/ResearchSystem.Technology.cs b/Content.Server/Research/Systems/ResearchSystem.Technology.cs index 99219369fc..52ebe22c2d 100644 --- a/Content.Server/Research/Systems/ResearchSystem.Technology.cs +++ b/Content.Server/Research/Systems/ResearchSystem.Technology.cs @@ -14,7 +14,7 @@ public sealed partial class ResearchSystem private void OnTechnologyGetState(EntityUid uid, TechnologyDatabaseComponent component, ref ComponentGetState args) { - args.State = new TechnologyDatabaseState(component.Technologies); + args.State = new TechnologyDatabaseState(component.TechnologyIds); } /// @@ -26,7 +26,7 @@ public sealed partial class ResearchSystem /// Whether the other database should be synced against this one too or not. public void Sync(TechnologyDatabaseComponent component, TechnologyDatabaseComponent otherDatabase, bool twoway = true) { - foreach (var tech in otherDatabase.Technologies) + foreach (var tech in otherDatabase.TechnologyIds) { if (!component.IsTechnologyUnlocked(tech)) AddTechnology(component, tech); } @@ -62,7 +62,7 @@ public sealed partial class ResearchSystem { if (!component.CanUnlockTechnology(technology)) return false; - AddTechnology(component, technology); + AddTechnology(component, technology.ID); Dirty(component); return true; } @@ -71,8 +71,8 @@ public sealed partial class ResearchSystem /// Adds a technology to the database without checking if it could be unlocked. /// /// - public void AddTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology) + public void AddTechnology(TechnologyDatabaseComponent component, string technology) { - component.Technologies.Add(technology); + component.TechnologyIds.Add(technology); } } diff --git a/Content.Shared/Alert/AlertOrderPrototype.cs b/Content.Shared/Alert/AlertOrderPrototype.cs index 8366b10138..dfef1c2cbf 100644 --- a/Content.Shared/Alert/AlertOrderPrototype.cs +++ b/Content.Shared/Alert/AlertOrderPrototype.cs @@ -1,5 +1,4 @@ using Robust.Shared.Prototypes; -using Robust.Shared.Serialization; namespace Content.Shared.Alert { @@ -8,59 +7,55 @@ namespace Content.Shared.Alert /// [Prototype("alertOrder")] [DataDefinition] - public sealed class AlertOrderPrototype : IPrototype, IComparer, ISerializationHooks + public sealed class AlertOrderPrototype : IPrototype, IComparer { [ViewVariables] [IdDataFieldAttribute] public string ID { get; } = default!; - [DataField("order")] private readonly List<(string type, string alert)> _order = new(); - - private readonly Dictionary _typeToIdx = new(); - private readonly Dictionary _categoryToIdx = new(); - - void ISerializationHooks.BeforeSerialization() + [DataField("order")] + private List<(string type, string alert)> Order { - _order.Clear(); - - var orderArray = new KeyValuePair[_typeToIdx.Count + _categoryToIdx.Count]; - - foreach (var (type, id) in _typeToIdx) + get { - orderArray[id] = new KeyValuePair("alertType", type.ToString()); - } + var res = new List<(string, string)>(_typeToIdx.Count + _categoryToIdx.Count); - foreach (var (category, id) in _categoryToIdx) - { - orderArray[id] = new KeyValuePair("category", category.ToString()); - } - - foreach (var (type, alert) in orderArray) - { - _order.Add((type, alert)); - } - } - - void ISerializationHooks.AfterDeserialization() - { - var i = 0; - - foreach (var (type, alert) in _order) - { - switch (type) + foreach (var (type, id) in _typeToIdx) { - case "alertType": - _typeToIdx[Enum.Parse(alert)] = i++; - break; - case "category": - _categoryToIdx[Enum.Parse(alert)] = i++; - break; - default: - throw new ArgumentException(); + res.Insert(id, ("alertType", type.ToString())); + } + + foreach (var (category, id) in _categoryToIdx) + { + res.Insert(id, ("category", category.ToString())); + } + + return res; + } + set + { + var i = 0; + + foreach (var (type, alert) in value) + { + switch (type) + { + case "alertType": + _typeToIdx[Enum.Parse(alert)] = i++; + break; + case "category": + _categoryToIdx[Enum.Parse(alert)] = i++; + break; + default: + throw new ArgumentException(); + } } } } + private readonly Dictionary _typeToIdx = new(); + private readonly Dictionary _categoryToIdx = new(); + private int GetOrderIndex(AlertPrototype alert) { if (_typeToIdx.TryGetValue(alert.AlertType, out var idx)) diff --git a/Content.Shared/Doors/Components/DoorComponent.cs b/Content.Shared/Doors/Components/DoorComponent.cs index a67f57731e..105a97f045 100644 --- a/Content.Shared/Doors/Components/DoorComponent.cs +++ b/Content.Shared/Doors/Components/DoorComponent.cs @@ -1,8 +1,8 @@ using Content.Shared.Damage; using Content.Shared.Tools; +using JetBrains.Annotations; using Robust.Shared.Audio; using Robust.Shared.GameStates; -using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; @@ -146,26 +146,27 @@ public sealed class DoorComponent : Component, ISerializationHooks /// Time until next state change. Because apparently might not get saved/restored. /// [DataField("SecondsUntilStateChange")] - private float? _secondsUntilStateChange; - - void ISerializationHooks.BeforeSerialization() + private float? SecondsUntilStateChange { - if (NextStateChange == null) + [UsedImplicitly] + get { - _secondsUntilStateChange = null; - return; - }; + if (NextStateChange == null) + { + return null; + } - var curTime = IoCManager.Resolve().CurTime; - _secondsUntilStateChange = (float) (NextStateChange.Value - curTime).TotalSeconds; - } + var curTime = IoCManager.Resolve().CurTime; + return (float) (NextStateChange.Value - curTime).TotalSeconds; + } + set + { + if (value == null || value.Value > 0) + return; - void ISerializationHooks.AfterDeserialization() - { - if (_secondsUntilStateChange == null || _secondsUntilStateChange.Value > 0) - return; + NextStateChange = IoCManager.Resolve().CurTime + TimeSpan.FromSeconds(value.Value); - NextStateChange = IoCManager.Resolve().CurTime + TimeSpan.FromSeconds(_secondsUntilStateChange.Value); + } } #endregion diff --git a/Content.Shared/Research/Components/SharedTechnologyDatabaseComponent.cs b/Content.Shared/Research/Components/SharedTechnologyDatabaseComponent.cs index 1c95a2c04b..30e0994417 100644 --- a/Content.Shared/Research/Components/SharedTechnologyDatabaseComponent.cs +++ b/Content.Shared/Research/Components/SharedTechnologyDatabaseComponent.cs @@ -1,77 +1,25 @@ -using System.Collections; using Content.Shared.Research.Prototypes; using Robust.Shared.GameStates; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List; namespace Content.Shared.Research.Components { [NetworkedComponent()] - public abstract class SharedTechnologyDatabaseComponent : Component, IEnumerable, ISerializationHooks + public abstract class SharedTechnologyDatabaseComponent : Component { - [DataField("technologies")] private List _technologyIds = new(); - - public List Technologies = new(); - - void ISerializationHooks.BeforeSerialization() - { - var techIds = new List(); - - foreach (var tech in Technologies) - { - techIds.Add(tech.ID); - } - - _technologyIds = techIds; - } - - void ISerializationHooks.AfterDeserialization() - { - var prototypeManager = IoCManager.Resolve(); - - foreach (var id in _technologyIds) - { - if (prototypeManager.TryIndex(id, out TechnologyPrototype? tech)) - { - Technologies.Add(tech); - } - } - } - - public IEnumerator GetEnumerator() - { - return Technologies.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - /// - /// Returns a list with the IDs of all unlocked technologies. - /// - /// A list of technology IDs - public List GetTechnologyIdList() - { - List techIds = new List(); - - foreach (var tech in Technologies) - { - techIds.Add(tech.ID); - } - - return techIds; - } + [DataField("technologies", customTypeSerializer: typeof(PrototypeIdListSerializer))] + public readonly List TechnologyIds = new(); /// /// Returns whether a technology is unlocked on this database or not. /// /// The technology to be checked /// Whether it is unlocked or not - public bool IsTechnologyUnlocked(TechnologyPrototype technology) + public bool IsTechnologyUnlocked(string technologyId) { - return Technologies.Contains(technology); + return TechnologyIds.Contains(technologyId); } /// @@ -82,7 +30,7 @@ namespace Content.Shared.Research.Components /// Whether it could be unlocked or not public bool CanUnlockTechnology(TechnologyPrototype technology) { - if (technology == null || IsTechnologyUnlocked(technology)) return false; + if (IsTechnologyUnlocked(technology.ID)) return false; var protoMan = IoCManager.Resolve(); foreach (var technologyId in technology.RequiredTechnologies) { @@ -90,7 +38,7 @@ namespace Content.Shared.Research.Components if (requiredTechnology == null) return false; - if (!IsTechnologyUnlocked(requiredTechnology)) + if (!IsTechnologyUnlocked(requiredTechnology.ID)) return false; } return true;