From 7d9a039252d960fdffa8c0854168a8a3a89dea3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20M=C4=99drek?= Date: Sat, 2 Aug 2025 14:57:43 +0000 Subject: [PATCH] add: Dictionary OnUnpaused generator (#6119) * add: Dictionary OnUnpaused generator * fix * add: test * Fix compiler warning from duplicate using --------- Co-authored-by: PJB3005 --- .../ComponentPauseGeneratorTest.cs | 44 ++++++++++++++++++- .../ComponentPauseGenerator.cs | 18 ++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Robust.Analyzers.Tests/ComponentPauseGeneratorTest.cs b/Robust.Analyzers.Tests/ComponentPauseGeneratorTest.cs index 5e1a7b3d0..ccd37c5aa 100644 --- a/Robust.Analyzers.Tests/ComponentPauseGeneratorTest.cs +++ b/Robust.Analyzers.Tests/ComponentPauseGeneratorTest.cs @@ -1,4 +1,4 @@ -extern alias SerializationGenerator; +extern alias SerializationGenerator; using System.Linq; using System.Reflection; using Microsoft.CodeAnalysis; @@ -126,6 +126,48 @@ public sealed class ComponentPauseGeneratorTest """); } + [Test] + public void TestDictionary() + { + var result = RunGenerator(""" + [AutoGenerateComponentPause] + public sealed partial class FooComponent : IComponent + { + [AutoPausedField] + public Dictionary Foo; + } + """); + + ExpectNoDiagnostics(result); + ExpectSource( + result, + """ + // + + using Robust.Shared.GameObjects; + + public partial class FooComponent + { + [RobustAutoGenerated] + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public sealed class FooComponent_AutoPauseSystem : EntitySystem + { + public override void Initialize() + { + SubscribeLocalEvent(OnEntityUnpaused); + } + + private void OnEntityUnpaused(EntityUid uid, FooComponent component, ref EntityUnpausedEvent args) + { + foreach (var key in component.Foo.Keys) + component.Foo[key] += args.PausedTime; + } + } + } + + """); + } + [Test] public void TestAutoState() { diff --git a/Robust.Serialization.Generator/ComponentPauseGenerator.cs b/Robust.Serialization.Generator/ComponentPauseGenerator.cs index 962bc5cc1..7b94e4e97 100644 --- a/Robust.Serialization.Generator/ComponentPauseGenerator.cs +++ b/Robust.Serialization.Generator/ComponentPauseGenerator.cs @@ -1,4 +1,4 @@ -using System.Collections.Immutable; +using System.Collections.Immutable; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -85,12 +85,17 @@ public sealed class ComponentPauseGenerator : IIncrementalGenerator var invalid = false; var nullable = false; + var dictionary = false; if (namedType.Name != "TimeSpan") { if (namedType is { Name: "Nullable", TypeArguments: [{Name: "TimeSpan"}] }) { nullable = true; } + else if (namedType is { Name: "Dictionary", TypeArguments: [{}, {Name: "TimeSpan"}]}) + { + dictionary = true; + } else { invalid = true; @@ -101,7 +106,7 @@ public sealed class ComponentPauseGenerator : IIncrementalGenerator if (AttributeHelper.HasAttribute(member, AutoNetworkFieldAttributeName, out var _)) dirty = true; - fieldBuilder.Add(new FieldInfo(member.Name, nullable, invalid, member.Locations[0])); + fieldBuilder.Add(new FieldInfo(member.Name, nullable, invalid, dictionary, member.Locations[0])); } return new ComponentInfo( @@ -181,6 +186,13 @@ public sealed class ComponentPauseGenerator : IIncrementalGenerator component.{field.Name} = component.{field.Name}.Value + args.PausedTime; """); } + else if (field.Dictionary) + { + builder.AppendLine($""" + foreach (var key in component.{field.Name}.Keys) + component.{field.Name}[key] += args.PausedTime; + """); + } else { builder.AppendLine($" component.{field.Name} += args.PausedTime;"); @@ -247,7 +259,7 @@ public sealed class ComponentPauseGenerator : IIncrementalGenerator bool NotComponent, Location Location); - public sealed record FieldInfo(string Name, bool Nullable, bool Invalid, Location Location); + public sealed record FieldInfo(string Name, bool Nullable, bool Invalid, bool Dictionary, Location Location); public sealed record AllFieldInfo(string Name, string ParentDisplayName, Location Location); }