feat: Adds tidy option for serialization. Codegens ballotbox. Fixes pooled timer leaking (#681)
* Fixes pooled timer leaking * Fixes `[dumptimers` command so it outputs properly, adds spacing, and stacktraces * Adds `[Tidy]` for serializing Lists. This will remove deleted entities during world save before serializing the list. * Adds helpers for managing Lists/Sets/Dictionaries ### New API ```cs // Creates the list if it is null, then adds Utility.Add(ref list, value); Utility.Add(ref set, value); Utility.Add(ref dict, key, value); // Nulls the variable if the count is zero Utility.Remove(ref list, value); Utility.Remove(ref set, value); Utility.Remove(ref dict, key); // Marks entity as dirty in addition to doing the action entity.Add(list, value); // Marks entity as dirty, and will create list if it doesn't exist entity.Add(ref list, value); // Marks entity as dirty in addition to doing the action entity.Remove(list, value); // Marks entity as dirty, and will null the list count is zero entity.Remove(ref list, value); ``` ### Updates to [dumptimers <img width="825" alt="Screen Shot 2021-08-14 at 2 55 10 AM" src="https://user-images.githubusercontent.com/3953314/129442449-ccf7fe14-29d6-4f3f-9366-c8eb7b9828a7.png">
This commit is contained in:
parent
84cbd52a2a
commit
360143478a
53 changed files with 627 additions and 464 deletions
|
|
@ -7,7 +7,8 @@ using Server.Prompts;
|
|||
|
||||
namespace Server.Items
|
||||
{
|
||||
public class BallotBox : AddonComponent
|
||||
[Serializable(0)]
|
||||
public partial class BallotBox : AddonComponent
|
||||
{
|
||||
public static readonly int MaxTopicLines = 6;
|
||||
|
||||
|
|
@ -19,17 +20,18 @@ namespace Server.Items
|
|||
No = new List<Mobile>();
|
||||
}
|
||||
|
||||
public BallotBox(Serial serial) : base(serial)
|
||||
{
|
||||
}
|
||||
|
||||
public override int LabelNumber => 1041006; // a ballot box
|
||||
|
||||
public string[] Topic { get; private set; }
|
||||
[SerializableField(0, setter: "private")]
|
||||
private string[] _topic;
|
||||
|
||||
public List<Mobile> Yes { get; private set; }
|
||||
[Tidy]
|
||||
[SerializableField(1, setter: "private")]
|
||||
private List<Mobile> _yes;
|
||||
|
||||
public List<Mobile> No { get; private set; }
|
||||
[Tidy]
|
||||
[SerializableField(2, setter: "private")]
|
||||
private List<Mobile> _no;
|
||||
|
||||
public void ClearTopic()
|
||||
{
|
||||
|
|
@ -56,8 +58,11 @@ namespace Server.Items
|
|||
|
||||
public void ClearVotes()
|
||||
{
|
||||
Yes.Clear();
|
||||
No.Clear();
|
||||
if (Yes.Count > 0 || No.Count > 0)
|
||||
{
|
||||
this.Clear(_yes);
|
||||
this.Clear(_no);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsOwner(Mobile from)
|
||||
|
|
@ -92,43 +97,6 @@ namespace Server.Items
|
|||
}
|
||||
}
|
||||
|
||||
public override void Serialize(IGenericWriter writer)
|
||||
{
|
||||
base.Serialize(writer);
|
||||
|
||||
writer.WriteEncodedInt(0); // version
|
||||
|
||||
writer.WriteEncodedInt(Topic.Length);
|
||||
|
||||
for (var i = 0; i < Topic.Length; i++)
|
||||
{
|
||||
writer.Write(Topic[i]);
|
||||
}
|
||||
|
||||
Yes.Tidy();
|
||||
writer.Write(Yes);
|
||||
|
||||
No.Tidy();
|
||||
writer.Write(No);
|
||||
}
|
||||
|
||||
public override void Deserialize(IGenericReader reader)
|
||||
{
|
||||
base.Deserialize(reader);
|
||||
|
||||
var version = reader.ReadEncodedInt();
|
||||
|
||||
Topic = new string[reader.ReadEncodedInt()];
|
||||
|
||||
for (var i = 0; i < Topic.Length; i++)
|
||||
{
|
||||
Topic[i] = reader.ReadString();
|
||||
}
|
||||
|
||||
Yes = reader.ReadEntityList<Mobile>();
|
||||
No = reader.ReadEntityList<Mobile>();
|
||||
}
|
||||
|
||||
private class InternalGump : Gump
|
||||
{
|
||||
private readonly BallotBox m_Box;
|
||||
|
|
@ -259,7 +227,7 @@ namespace Server.Items
|
|||
}
|
||||
else
|
||||
{
|
||||
m_Box.Yes.Add(from);
|
||||
m_Box.Add(m_Box._yes, from);
|
||||
from.SendLocalizedMessage(500373); // Your vote has been registered.
|
||||
}
|
||||
}
|
||||
|
|
@ -276,7 +244,7 @@ namespace Server.Items
|
|||
}
|
||||
else
|
||||
{
|
||||
m_Box.No.Add(from);
|
||||
m_Box.Add(m_Box._no, from);
|
||||
from.SendLocalizedMessage(500373); // Your vote has been registered.
|
||||
}
|
||||
}
|
||||
|
|
@ -344,61 +312,25 @@ namespace Server.Items
|
|||
}
|
||||
}
|
||||
|
||||
public class BallotBoxAddon : BaseAddon
|
||||
[Serializable(0)]
|
||||
public partial class BallotBoxAddon : BaseAddon
|
||||
{
|
||||
public BallotBoxAddon()
|
||||
{
|
||||
AddComponent(new BallotBox(), 0, 0, 0);
|
||||
}
|
||||
|
||||
public BallotBoxAddon(Serial serial) : base(serial)
|
||||
{
|
||||
}
|
||||
|
||||
public override BaseAddonDeed Deed => new BallotBoxDeed();
|
||||
|
||||
public override void Serialize(IGenericWriter writer)
|
||||
{
|
||||
base.Serialize(writer);
|
||||
|
||||
writer.WriteEncodedInt(0); // version
|
||||
}
|
||||
|
||||
public override void Deserialize(IGenericReader reader)
|
||||
{
|
||||
base.Deserialize(reader);
|
||||
|
||||
var version = reader.ReadEncodedInt();
|
||||
}
|
||||
}
|
||||
|
||||
public class BallotBoxDeed : BaseAddonDeed
|
||||
[Serializable(0)]
|
||||
public partial class BallotBoxDeed : BaseAddonDeed
|
||||
{
|
||||
[Constructible]
|
||||
public BallotBoxDeed()
|
||||
{
|
||||
}
|
||||
|
||||
public BallotBoxDeed(Serial serial) : base(serial)
|
||||
{
|
||||
}
|
||||
|
||||
public override BaseAddon Addon => new BallotBoxAddon();
|
||||
|
||||
public override int LabelNumber => 1044327; // ballot box
|
||||
|
||||
public override void Serialize(IGenericWriter writer)
|
||||
{
|
||||
base.Serialize(writer);
|
||||
|
||||
writer.WriteEncodedInt(0); // version
|
||||
}
|
||||
|
||||
public override void Deserialize(IGenericReader reader)
|
||||
{
|
||||
base.Deserialize(reader);
|
||||
|
||||
var version = reader.ReadEncodedInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ namespace Server.Items
|
|||
Hue = CraftResources.GetHue(m_Resource);
|
||||
|
||||
InvalidateProperties();
|
||||
((ISerializable)this).MarkDirty();
|
||||
this.MarkDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace Server.Items
|
|||
protected FillableContent m_Content;
|
||||
|
||||
protected DateTime m_NextRespawnTime;
|
||||
protected TimerExecutionToken _respawnTimerToken;
|
||||
protected Timer _respawnTimer;
|
||||
|
||||
public FillableContainer(int itemID) : base(itemID) => Movable = false;
|
||||
|
||||
|
|
@ -96,7 +96,8 @@ namespace Server.Items
|
|||
{
|
||||
base.OnAfterDelete();
|
||||
|
||||
_respawnTimerToken.Cancel();
|
||||
_respawnTimer?.Stop();
|
||||
_respawnTimer = null;
|
||||
}
|
||||
|
||||
public int GetItemsCount()
|
||||
|
|
@ -118,24 +119,26 @@ namespace Server.Items
|
|||
|
||||
if (canSpawn)
|
||||
{
|
||||
if (!_respawnTimerToken.Running)
|
||||
if (_respawnTimer?.Running != true)
|
||||
{
|
||||
var mins = Utility.RandomMinMax(MinRespawnMinutes, MaxRespawnMinutes);
|
||||
var delay = TimeSpan.FromMinutes(mins);
|
||||
|
||||
m_NextRespawnTime = Core.Now + delay;
|
||||
Timer.StartTimer(delay, Respawn, out _respawnTimerToken);
|
||||
_respawnTimer = Timer.DelayCall(delay, Respawn);
|
||||
}
|
||||
}
|
||||
else if (_respawnTimerToken.Running)
|
||||
else
|
||||
{
|
||||
_respawnTimerToken.Cancel();
|
||||
_respawnTimer?.Stop();
|
||||
_respawnTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Respawn()
|
||||
{
|
||||
_respawnTimerToken.Cancel();
|
||||
_respawnTimer?.Stop();
|
||||
_respawnTimer = null;
|
||||
|
||||
if (m_Content == null || Deleted)
|
||||
{
|
||||
|
|
@ -217,7 +220,7 @@ namespace Server.Items
|
|||
{
|
||||
var subItem = list[j];
|
||||
|
||||
if (!(subItem is Container) && subItem.StackWith(null, item, false))
|
||||
if (subItem is not Container && subItem.StackWith(null, item, false))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
|
@ -238,7 +241,7 @@ namespace Server.Items
|
|||
|
||||
writer.Write((int)ContentType);
|
||||
|
||||
if (_respawnTimerToken.Running)
|
||||
if (_respawnTimer?.Running == true)
|
||||
{
|
||||
writer.Write(true);
|
||||
writer.WriteDeltaTime(m_NextRespawnTime);
|
||||
|
|
@ -269,7 +272,7 @@ namespace Server.Items
|
|||
m_NextRespawnTime = reader.ReadDeltaTime();
|
||||
|
||||
var delay = m_NextRespawnTime - Core.Now;
|
||||
Timer.StartTimer(delay, Respawn, out _respawnTimerToken);
|
||||
Timer.DelayCall(delay, Respawn);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -275,7 +275,7 @@ namespace Server.Items
|
|||
}
|
||||
}
|
||||
|
||||
Timer.StartTimer(TimeSpan.FromMinutes(1.0), TimeSpan.FromMinutes(1.0), CheckEnd_OnTick);
|
||||
Timer.DelayCall(TimeSpan.FromMinutes(1.0), TimeSpan.FromMinutes(1.0), CheckEnd_OnTick);
|
||||
}
|
||||
|
||||
public bool ValidLocation() =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue