fix(codegen): Codegens bulletin boards. (#712)

This commit is contained in:
Kamron Batman 2021-08-22 08:05:03 -07:00 • committed by GitHub
parent 8b0eb1aa79
commit d9a9e90c14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 200 additions and 180 deletions

View file

@ -101,8 +101,8 @@ namespace Server.Network
{ {
var eq = msg.PostedEquip[i]; var eq = msg.PostedEquip[i];
Stream.Write((short)eq.itemID); Stream.Write((short)eq._itemID);
Stream.Write((short)eq.hue); Stream.Write((short)eq._hue);
} }
len = msg.Lines.Length; len = msg.Lines.Length;

View file

@ -1,6 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Server.Collections;
using Server.Network; using Server.Network;
namespace Server.Items namespace Server.Items
@ -25,56 +25,38 @@ namespace Server.Items
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CheckCreateTime(DateTime time) => time + ThreadCreateTime < Core.Now; public static bool CheckCreateTime(DateTime time) => time + ThreadCreateTime < Core.Now;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CheckDeletionTime(DateTime time) => time + ThreadDeletionTime < Core.Now; public static bool CheckDeletionTime(DateTime time) => time + ThreadDeletionTime < Core.Now;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CheckReplyTime(DateTime time) => time + ThreadReplyTime < Core.Now; public static bool CheckReplyTime(DateTime time) => time + ThreadReplyTime < Core.Now;
} }
[Serializable(0, false)]
[Flippable(0x1E5E, 0x1E5F)] [Flippable(0x1E5E, 0x1E5F)]
public class BulletinBoard : BaseBulletinBoard public partial class BulletinBoard : BaseBulletinBoard
{ {
[Constructible] [Constructible]
public BulletinBoard() : base(0x1E5E) public BulletinBoard() : base(0x1E5E)
{ {
} }
public BulletinBoard(Serial serial) : base(serial)
{
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(0); // version
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
}
} }
public abstract class BaseBulletinBoard : Item [Serializable(0, false)]
public abstract partial class BaseBulletinBoard : Item
{ {
[SerializableField(0)]
[SerializableFieldAttr("[CommandProperty(AccessLevel.GameMaster)]")]
private string _boardName;
public BaseBulletinBoard(int itemID) : base(itemID) public BaseBulletinBoard(int itemID) : base(itemID)
{ {
BoardName = "bulletin board"; BoardName = "bulletin board";
Movable = false; Movable = false;
} }
public BaseBulletinBoard(Serial serial) : base(serial) [AfterDeserialization(false)]
{
}
[CommandProperty(AccessLevel.GameMaster)]
public string BoardName { get; set; }
public virtual void Cleanup() public virtual void Cleanup()
{ {
var items = Items; var items = Items;
@ -86,7 +68,7 @@ namespace Server.Items
continue; continue;
} }
if (!(items[i] is BulletinMessage msg)) if (items[i] is not BulletinMessage msg || msg.Deleted)
{ {
continue; continue;
} }
@ -94,14 +76,24 @@ namespace Server.Items
if (msg.Thread == null && BulletinBoardSystem.CheckDeletionTime(msg.LastPostTime)) if (msg.Thread == null && BulletinBoardSystem.CheckDeletionTime(msg.LastPostTime))
{ {
msg.Delete(); msg.Delete();
RecurseDelete(msg); // A root-level thread has expired var queue = PooledRefQueue<Item>.Create();
var thread = msg;
do
{
BFSDelete(thread, ref queue);
if (queue.Count > 0)
{
thread = (BulletinMessage)queue.Dequeue();
}
} while (thread != null);
queue.Dispose();
} }
} }
} }
private void RecurseDelete(BulletinMessage msg) private void BFSDelete(BulletinMessage msg, ref PooledRefQueue<Item> queue)
{ {
var found = new List<Item>();
var items = Items; var items = Items;
for (var i = items.Count - 1; i >= 0; --i) for (var i = items.Count - 1; i >= 0; --i)
@ -111,7 +103,7 @@ namespace Server.Items
continue; continue;
} }
if (!(items[i] is BulletinMessage check)) if (items[i] is not BulletinMessage check || check.Deleted)
{ {
continue; continue;
} }
@ -119,24 +111,20 @@ namespace Server.Items
if (check.Thread == msg) if (check.Thread == msg)
{ {
check.Delete(); check.Delete();
found.Add(check); queue.Enqueue(check);
} }
} }
for (var i = 0; i < found.Count; ++i)
{
RecurseDelete((BulletinMessage)found[i]);
}
} }
public virtual bool GetLastPostTime(Mobile poster, bool onlyCheckRoot, ref DateTime lastPostTime) public virtual bool GetLastPostTime(Mobile poster, bool onlyCheckRoot, out DateTime lastPostTime)
{ {
lastPostTime = DateTime.MinValue;
var items = Items; var items = Items;
var wasSet = false; var wasSet = false;
for (var i = 0; i < items.Count; ++i) for (var i = 0; i < items.Count; ++i)
{ {
if (!(items[i] is BulletinMessage msg) || msg.Poster != poster) if (items[i] is not BulletinMessage msg || msg.Poster != poster)
{ {
continue; continue;
} }
@ -158,19 +146,18 @@ namespace Server.Items
public override void OnDoubleClick(Mobile from) public override void OnDoubleClick(Mobile from)
{ {
if (CheckRange(from)) if (!CheckRange(from))
{
Cleanup();
var state = from.NetState;
state.SendBBDisplayBoard(this);
state.SendContainerContent(from, this);
}
else
{ {
from.LocalOverheadMessage(MessageType.Regular, 0x3B2, 1019045); // I can't reach that. from.LocalOverheadMessage(MessageType.Regular, 0x3B2, 1019045); // I can't reach that.
return;
} }
Cleanup();
var state = from.NetState;
state.SendBBDisplayBoard(this);
state.SendContainerContent(from, this);
} }
public virtual bool CheckRange(Mobile from) => public virtual bool CheckRange(Mobile from) =>
@ -185,30 +172,5 @@ namespace Server.Items
AddItem(new BulletinMessage(from, thread, subject, lines)); AddItem(new BulletinMessage(from, thread, subject, lines));
} }
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(0); // version
writer.Write(BoardName);
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
switch (version)
{
case 0:
{
BoardName = reader.ReadString();
break;
}
}
}
} }
} }

View file

@ -112,9 +112,7 @@ namespace Server.Network
thread = thread.Thread; thread = thread.Thread;
} }
var lastPostTime = DateTime.MinValue; if (board.GetLastPostTime(from, thread == null, out var lastPostTime))
if (board.GetLastPostTime(from, thread == null, ref lastPostTime))
{ {
if (thread == null) if (thread == null)
{ {
@ -259,8 +257,8 @@ namespace Server.Network
for (var i = 0; i < equipLength; i++) for (var i = 0; i < equipLength; i++)
{ {
var eq = msg.PostedEquip[i]; var eq = msg.PostedEquip[i];
writer.Write((short)eq.itemID); writer.Write((short)eq._itemID);
writer.Write((short)eq.hue); writer.Write((short)eq._hue);
} }
writer.Write((byte)linesLength); writer.Write((byte)linesLength);

View file

@ -2,13 +2,25 @@ namespace Server.Items
{ {
public struct BulletinEquip public struct BulletinEquip
{ {
public int itemID; public int _itemID;
public int hue; public int _hue;
public BulletinEquip(int itemID, int hue) public BulletinEquip(int itemID, int hue)
{ {
this.itemID = itemID; _itemID = itemID;
this.hue = hue; _hue = hue;
}
public BulletinEquip(IGenericReader reader)
{
_itemID = reader.ReadInt();
_hue = reader.ReadInt();
}
public void Serialize(IGenericWriter writer)
{
writer.Write(_itemID);
writer.Write(_hue);
} }
} }
} }

View file

@ -4,7 +4,8 @@ using Server.Targeting;
namespace Server.Items namespace Server.Items
{ {
public class BulletinMessage : Item [Serializable(2, false)]
public partial class BulletinMessage : Item
{ {
public BulletinMessage(Mobile poster, BulletinMessage thread, string subject, string[] lines) : base(0xEB0) public BulletinMessage(Mobile poster, BulletinMessage thread, string subject, string[] lines) : base(0xEB0)
{ {
@ -35,29 +36,35 @@ namespace Server.Items
PostedEquip = list.ToArray(); PostedEquip = list.ToArray();
} }
public BulletinMessage(Serial serial) : base(serial) [SerializableField(0)]
{ private Mobile _poster;
}
public Mobile Poster { get; private set; } [SerializableField(1)]
private string _subject;
public BulletinMessage Thread { get; private set; } [SerializableField(2)]
private DateTime _time;
public string Subject { get; private set; } [SerializableField(3)]
private DateTime _lastPostTime;
public DateTime Time { get; private set; } [SerializableField(4)]
private BulletinMessage _thread;
public DateTime LastPostTime { get; set; } [SerializableField(5)]
private string _postedName;
public string PostedName { get; private set; } [SerializableField(6)]
private int _postedBody;
public int PostedBody { get; private set; } [SerializableField(7)]
private int _postedHue;
public int PostedHue { get; private set; } [SerializableField(8)]
private BulletinEquip[] _postedEquip;
public BulletinEquip[] PostedEquip { get; private set; } [SerializableField(9)]
private string[] _lines;
public string[] Lines { get; private set; }
// TODO: Memoize // TODO: Memoize
public string GetTimeAsString() => Time.ToString("MMM dd, yyyy"); public string GetTimeAsString() => Time.ToString("MMM dd, yyyy");
@ -66,95 +73,34 @@ namespace Server.Items
public override bool IsAccessibleTo(Mobile check) => false; public override bool IsAccessibleTo(Mobile check) => false;
public override void Serialize(IGenericWriter writer) private void Deserialize(IGenericReader reader, int version)
{ {
base.Serialize(writer); Poster = reader.ReadEntity<Mobile>();
Subject = reader.ReadString();
Time = reader.ReadDateTime();
LastPostTime = reader.ReadDateTime();
reader.ReadBool(); // Has thread
Thread = reader.ReadEntity<BulletinMessage>();
PostedName = reader.ReadString();
PostedBody = reader.ReadInt();
PostedHue = reader.ReadInt();
writer.Write(1); // version PostedEquip = new BulletinEquip[reader.ReadInt()];
writer.Write(Poster);
writer.Write(Subject);
writer.Write(Time);
writer.Write(LastPostTime);
writer.Write(Thread != null);
writer.Write(Thread);
writer.Write(PostedName);
writer.Write(PostedBody);
writer.Write(PostedHue);
writer.Write(PostedEquip.Length);
for (var i = 0; i < PostedEquip.Length; ++i) for (var i = 0; i < PostedEquip.Length; ++i)
{ {
writer.Write(PostedEquip[i].itemID); PostedEquip[i]._itemID = reader.ReadInt();
writer.Write(PostedEquip[i].hue); PostedEquip[i]._hue = reader.ReadInt();
} }
writer.Write(Lines.Length); Lines = new string[reader.ReadInt()];
for (var i = 0; i < Lines.Length; ++i) for (var i = 0; i < Lines.Length; ++i)
{ {
writer.Write(Lines[i]); Lines[i] = reader.ReadString();
} }
}
public override void Deserialize(IGenericReader reader) // Moved validation/cleanup to the BB itself
{
base.Deserialize(reader);
var version = reader.ReadInt();
switch (version)
{
case 1:
case 0:
{
Poster = reader.ReadEntity<Mobile>();
Subject = reader.ReadString();
Time = reader.ReadDateTime();
LastPostTime = reader.ReadDateTime();
var hasThread = reader.ReadBool();
Thread = reader.ReadEntity<BulletinMessage>();
PostedName = reader.ReadString();
PostedBody = reader.ReadInt();
PostedHue = reader.ReadInt();
PostedEquip = new BulletinEquip[reader.ReadInt()];
for (var i = 0; i < PostedEquip.Length; ++i)
{
PostedEquip[i].itemID = reader.ReadInt();
PostedEquip[i].hue = reader.ReadInt();
}
Lines = new string[reader.ReadInt()];
for (var i = 0; i < Lines.Length; ++i)
{
Lines[i] = reader.ReadString();
}
if (hasThread && Thread == null)
{
Delete();
}
if (version == 0)
{
ValidationQueue<BulletinMessage>.Add(this);
}
break;
}
}
}
public void Validate()
{
if ((Parent as BulletinBoard)?.Items.Contains(this) == false)
{
Delete();
}
} }
} }
} }

View file

@ -0,0 +1,14 @@
{
"version": 0,
"type": "Server.Items.BaseBulletinBoard",
"properties": [
{
"name": "BoardName",
"type": "string",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
}
]
}

View file

@ -0,0 +1,4 @@
{
"version": 0,
"type": "Server.Items.BulletinBoard"
}

View file

@ -0,0 +1,84 @@
{
"version": 2,
"type": "Server.Items.BulletinMessage",
"properties": [
{
"name": "Poster",
"type": "Server.Mobile",
"rule": "SerializableInterfaceMigrationRule"
},
{
"name": "Subject",
"type": "string",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
},
{
"name": "Time",
"type": "System.DateTime",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
},
{
"name": "LastPostTime",
"type": "System.DateTime",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
},
{
"name": "Thread",
"type": "Server.Items.BulletinMessage",
"rule": "SerializableInterfaceMigrationRule"
},
{
"name": "PostedName",
"type": "string",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
},
{
"name": "PostedBody",
"type": "int",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
},
{
"name": "PostedHue",
"type": "int",
"rule": "PrimitiveTypeMigrationRule",
"ruleArguments": [
""
]
},
{
"name": "PostedEquip",
"type": "Server.Items.BulletinEquip[]",
"rule": "ArrayMigrationRule",
"ruleArguments": [
"Server.Items.BulletinEquip",
"SerializationMethodSignatureMigrationRule",
""
]
},
{
"name": "Lines",
"type": "string[]",
"rule": "ArrayMigrationRule",
"ruleArguments": [
"string",
"PrimitiveTypeMigrationRule",
""
]
}
]
}