add: Dictionary<T, TimeSpan> OnUnpaused generator (#6119)

* add: Dictionary<T, TimeSpan> OnUnpaused generator

* fix

* add: test

* Fix compiler warning from duplicate using

---------

Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com>
This commit is contained in:
Łukasz Mędrek
2025-08-02 14:57:43 +00:00
committed by GitHub
parent 857f9a540b
commit 7d9a039252
2 changed files with 58 additions and 4 deletions

View File

@@ -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<string, TimeSpan> Foo;
}
""");
ExpectNoDiagnostics(result);
ExpectSource(
result,
"""
// <auto-generated />
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<FooComponent, EntityUnpausedEvent>(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()
{

View File

@@ -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);
}