ModernUO/Projects/UOContent/Engines/Spawners/Spawner.cs
Kamron Batman 6b36afa77f feat(spawners): add SpawnerJsonSerializer + typed JSON binding for Spawner
Implements Task 2 of the spawner STJ migration:
- SpawnerJsonSerializer: assembly discovery of [JsonDiscoverableType]-marked
  BaseSpawner subclasses, STJ polymorphism on $type, property pruning to
  [JsonPropertyName]-only, and OnDeserialized→OnAfterJsonDeserialize() hook.
- BaseSpawner.Json.cs: shadow JSON properties for all BaseSpawner fields,
  OnAfterJsonDeserialize() virtual method that applies deserialized state.
- Spawner.Json.cs: JsonSpawnBounds shadow property, override of
  OnAfterJsonDeserialize() to apply spawnBounds after base.
- Spawner.cs: [JsonDiscoverableType] + [JsonConstructor] on parameterless ctor.
- TestServerInitializer: SpawnerJsonSerializer.Configure() wired in.
- SpawnerRoundTripTests: 2 tests (round-trip core fields, omits domain defaults).

Old DynamicJson ctors and ToJson() left in place; removal is Task 9.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-25 10:27:55 -07:00

93 lines
2.6 KiB
C#

using System;
using System.Text.Json;
using ModernUO.Serialization;
using Server.Json;
namespace Server.Engines.Spawners;
[SerializationGenerator(1)]
[JsonDiscoverableType]
public partial class Spawner : BaseSpawner
{
/// <summary>
/// When true, enables proactive spiral scanning to find valid spawn positions.
/// Only relevant when SpawnPositionMode is Automatic or Enabled.
/// </summary>
[SerializableFieldSaveFlag(0)]
private bool ShouldSerializeUseSpiralScan() => _useSpiralScan;
[SerializableField(0)]
[SerializedCommandProperty(AccessLevel.Developer)]
private bool _useSpiralScan;
[SerializableFieldSaveFlag(1)]
private bool ShouldSerializeSpawnBounds() => _spawnBounds != default;
[SerializableProperty(1)]
[CommandProperty(AccessLevel.Developer)]
public override Rectangle3D SpawnBounds
{
get => _spawnBounds;
set
{
_spawnBounds = value;
InvalidateProperties();
this.MarkDirty();
}
}
[Constructible(AccessLevel.Developer)]
[System.Text.Json.Serialization.JsonConstructor]
public Spawner()
{
}
[Constructible(AccessLevel.Developer)]
public Spawner(string spawnedName) : base(spawnedName)
{
}
[Constructible(AccessLevel.Developer)]
public Spawner(
int amount,
TimeSpan minDelay,
TimeSpan maxDelay,
int team = 0,
Rectangle3D spawnBounds = default,
params ReadOnlySpan<string> spawnedNames
) : base(amount, minDelay, maxDelay, team, spawnBounds, spawnedNames)
{
}
public Spawner(DynamicJson json, JsonSerializerOptions options) : base(json, options)
{
// Read spawnBounds (not in BaseSpawner to allow RegionSpawner to skip it)
if (json.GetProperty("spawnBounds", options, out Rectangle3D spawnBounds))
{
SpawnBounds = spawnBounds;
}
}
public override void ToJson(DynamicJson json, JsonSerializerOptions options)
{
base.ToJson(json, options);
if (SpawnBounds != default)
{
json.SetProperty("spawnBounds", options, SpawnBounds);
}
}
public override Region Region => Region.Find(Location, Map);
protected override bool SupportsSpiralScan => _useSpiralScan;
protected override Rectangle3D GetBoundsForSpawnAttempt() => SpawnBounds;
protected override ReadOnlySpan<Rectangle3D> GetAllSpawnBounds() => new(ref _spawnBounds);
private void MigrateFrom(V0Content content)
{
// V0 had no fields in Spawner, new v1 field _useSpiralScan defaults to false
}
}