ModernUO/Projects/UOContent/Engines/Spawners/SpawnerEntry.cs
Kamron Batman d0d7be8de0
fix: Fixes codegenned spawners and adds RunUO spawner import support (#1682)
> [!IMPORTANT]  
> This code change includes important fixes for all spawners after they were converted to codegen!

> [!NOTE] 
> **Developer Note**
> Usage/Description/Aliases attributes were moved to the core so they can be used by the command registration system.

### Summary
- **Fixes spawners not registering their spawns on world load**
- Changes `[GenerateSpawners` to `[ImportSpawners`
- Removes `ConvertPremiumSpawners`
- Adds Premium spawner import support to `[ImportSpawners`
- Adds RunUO XML import support: https://github.com/ruaduck/xmlspawner/blob/ServUOMaster/XmlSpawner/XmlSpawner%20Core/SpawnerExporter.cs
- Fixed an issue where not manually registering an alias meant it wasn't available as a command
2024-02-17 10:46:55 -08:00

112 lines
2.6 KiB
C#

using System.Collections.Generic;
using System.Text.Json.Serialization;
using ModernUO.Serialization;
using Server.Json;
namespace Server.Engines.Spawners;
[SerializationGenerator(1, false)]
public partial class SpawnerEntry
{
[DirtyTrackingEntity]
private BaseSpawner _parent;
[SerializableField(0)]
[SerializedJsonPropertyName("name")]
private string _spawnedName;
[SerializableField(1)]
[SerializedJsonPropertyName("probability")]
private int _spawnedProbability;
[SerializableField(2)]
[SerializedJsonPropertyName("maxCount")]
private int _spawnedMaxCount;
[SerializableField(3)]
[SerializedJsonPropertyName("properties")]
private string _properties;
[SerializableField(4)]
[SerializedJsonPropertyName("parameters")]
private string _parameters;
[SerializedJsonIgnore]
[SerializableField(5)]
private List<ISpawnable> _spawned;
public SpawnerEntry(BaseSpawner parent)
{
_parent = parent;
_spawned = new List<ISpawnable>();
}
public SpawnerEntry(
BaseSpawner parent,
string name,
int probability,
int maxcount,
string properties = null,
string parameters = null
) : this(parent)
{
SpawnedName = name;
SpawnedProbability = probability;
SpawnedMaxCount = maxcount;
Properties = properties;
Parameters = parameters;
}
private void Deserialize(IGenericReader reader, int version)
{
SpawnedName = reader.ReadString();
SpawnedProbability = reader.ReadInt();
SpawnedMaxCount = reader.ReadInt();
Properties = reader.ReadString();
Parameters = reader.ReadString();
var count = reader.ReadInt();
Spawned = new List<ISpawnable>(count);
for (var i = 0; i < count; ++i)
{
var e = reader.ReadEntity<ISpawnable>();
if (e != null)
{
Spawned.Add(e);
}
}
}
[AfterDeserialization]
private void AfterDeserialization()
{
foreach (var e in Spawned)
{
e.Spawner = _parent;
}
}
[JsonIgnore]
public EntryFlags Valid { get; set; }
[JsonIgnore]
public bool IsFull => Spawned.Count >= SpawnedMaxCount;
public void Defrag(BaseSpawner parent)
{
for (var i = 0; i < Spawned.Count; ++i)
{
var spawned = Spawned[i];
if (parent.OnDefragSpawn(spawned, false))
{
Spawned.RemoveAt(i--);
_parent.MarkDirty();
}
}
}
}