fix: Codegens corpses (#1287)

This commit is contained in:
Kamron Batman 2022-11-28 23:08:38 -08:00 • committed by GitHub
parent c8a804d0b1
commit 0b4cd460de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1250 additions and 1339 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,11 @@
using System;
namespace Server
{
[AttributeUsage(AttributeTargets.Class)]
public class CorpseNameAttribute : Attribute
{
public CorpseNameAttribute(string name) => Name = name;
namespace Server;
public string Name { get; }
}
[AttributeUsage(AttributeTargets.Class)]
public class CorpseNameAttribute : Attribute
{
public CorpseNameAttribute(string name) => Name = name;
public string Name { get; }
}

View file

@ -17,143 +17,142 @@ using System.Buffers;
using System.IO;
using Server.Items;
namespace Server.Network
namespace Server.Network;
public static class CorpsePackets
{
public static class CorpsePackets
public static void SendCorpseEquip(this NetState ns, Mobile beholder, Corpse beheld)
{
public static void SendCorpseEquip(this NetState ns, Mobile beholder, Corpse beheld)
if (ns.CannotSendPackets())
{
if (ns.CannotSendPackets())
return;
}
var list = beheld.EquipItems;
var maxLength = 8 + (list.Count + 2) * 5;
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0x89);
writer.Seek(2, SeekOrigin.Current);
writer.Write(beheld.Serial);
for (var i = 0; i < list.Count; ++i)
{
var item = list[i];
if (!item.Deleted && beholder.CanSee(item) && item.Parent == beheld)
{
return;
writer.Write((byte)(item.Layer + 1));
writer.Write(item.Serial);
}
}
var list = beheld.EquipItems;
if (beheld.Hair?.ItemID > 0)
{
writer.Write((byte)(Layer.Hair + 1));
writer.Write(HairInfo.FakeSerial(beheld.Owner.Serial) - 2);
}
var maxLength = 8 + (list.Count + 2) * 5;
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0x89);
writer.Seek(2, SeekOrigin.Current);
if (beheld.FacialHair?.ItemID > 0)
{
writer.Write((byte)(Layer.FacialHair + 1));
writer.Write(FacialHairInfo.FakeSerial(beheld.Owner.Serial) - 2);
}
writer.Write((byte)Layer.Invalid);
writer.WritePacketLength();
ns.Send(writer.Span);
}
public static void SendCorpseContent(this NetState ns, Mobile beholder, Corpse beheld)
{
if (ns.CannotSendPackets())
{
return;
}
var list = beheld.EquipItems;
var hairItemID = beheld.Hair?.ItemID ?? 0;
var facialHairItemID = beheld.FacialHair?.ItemID ?? 0;
var count = list.Count;
if (hairItemID > 0)
{
count++;
}
if (facialHairItemID > 0)
{
count++;
}
var maxLength = 5 + count * (ns.ContainerGridLines ? 20 : 19);
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0x3C);
writer.Seek(4, SeekOrigin.Current); // Length and Count
var written = 0;
for (var i = 0; i < list.Count; ++i)
{
var child = list[i];
if (!child.Deleted && child.Parent == beheld && beholder.CanSee(child))
{
writer.Write(child.Serial);
writer.Write((ushort)child.ItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)child.Amount);
writer.Write((short)child.X);
writer.Write((short)child.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)child.Hue);
++written;
}
}
if (hairItemID > 0)
{
writer.Write(HairInfo.FakeSerial(beheld.Owner.Serial) - 2);
writer.Write((ushort)hairItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)1);
writer.Write(0); // X/Y
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)beheld.Hair!.Hue);
for (var i = 0; i < list.Count; ++i)
{
var item = list[i];
if (!item.Deleted && beholder.CanSee(item) && item.Parent == beheld)
{
writer.Write((byte)(item.Layer + 1));
writer.Write(item.Serial);
}
}
if (beheld.Hair?.ItemID > 0)
{
writer.Write((byte)(Layer.Hair + 1));
writer.Write(HairInfo.FakeSerial(beheld.Owner.Serial) - 2);
}
if (beheld.FacialHair?.ItemID > 0)
{
writer.Write((byte)(Layer.FacialHair + 1));
writer.Write(FacialHairInfo.FakeSerial(beheld.Owner.Serial) - 2);
}
writer.Write((byte)Layer.Invalid);
writer.WritePacketLength();
ns.Send(writer.Span);
++written;
}
public static void SendCorpseContent(this NetState ns, Mobile beholder, Corpse beheld)
if (facialHairItemID > 0)
{
if (ns.CannotSendPackets())
writer.Write(FacialHairInfo.FakeSerial(beheld.Owner.Serial) - 2);
writer.Write((ushort)facialHairItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)1);
writer.Write(0); // X/Y
if (ns.ContainerGridLines)
{
return;
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)beheld.FacialHair!.Hue);
var list = beheld.EquipItems;
var hairItemID = beheld.Hair?.ItemID ?? 0;
var facialHairItemID = beheld.FacialHair?.ItemID ?? 0;
var count = list.Count;
if (hairItemID > 0)
{
count++;
}
if (facialHairItemID > 0)
{
count++;
}
var maxLength = 5 + count * (ns.ContainerGridLines ? 20 : 19);
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0x3C);
writer.Seek(4, SeekOrigin.Current); // Length and Count
var written = 0;
for (var i = 0; i < list.Count; ++i)
{
var child = list[i];
if (!child.Deleted && child.Parent == beheld && beholder.CanSee(child))
{
writer.Write(child.Serial);
writer.Write((ushort)child.ItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)child.Amount);
writer.Write((short)child.X);
writer.Write((short)child.Y);
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)child.Hue);
++written;
}
}
if (hairItemID > 0)
{
writer.Write(HairInfo.FakeSerial(beheld.Owner.Serial) - 2);
writer.Write((ushort)hairItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)1);
writer.Write(0); // X/Y
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)beheld.Hair!.Hue);
++written;
}
if (facialHairItemID > 0)
{
writer.Write(FacialHairInfo.FakeSerial(beheld.Owner.Serial) - 2);
writer.Write((ushort)facialHairItemID);
writer.Write((byte)0); // signed, itemID offset
writer.Write((ushort)1);
writer.Write(0); // X/Y
if (ns.ContainerGridLines)
{
writer.Write((byte)0); // Grid Location?
}
writer.Write(beheld.Serial);
writer.Write((ushort)beheld.FacialHair!.Hue);
++written;
}
writer.Seek(1, SeekOrigin.Begin);
writer.Write((ushort)writer.BytesWritten);
writer.Write((ushort)written);
writer.Seek(0, SeekOrigin.End);
ns.Send(writer.Span);
++written;
}
writer.Seek(1, SeekOrigin.Begin);
writer.Write((ushort)writer.BytesWritten);
writer.Write((ushort)written);
writer.Seek(0, SeekOrigin.End);
ns.Send(writer.Span);
}
}

View file

@ -1,108 +1,74 @@
using System;
using ModernUO.Serialization;
namespace Server.Items
namespace Server.Items;
[SerializationGenerator(2, false)]
public partial class DecayedCorpse : Container
{
public class DecayedCorpse : Container
private static TimeSpan _defaultDecayTime = TimeSpan.FromMinutes(7.0);
[TimerDrift]
[SerializableField(0, getter: "private", setter: "private")]
private Timer _decayTimer;
[DeserializeTimerField(0)]
private void DeserializeDecayTimer(TimeSpan delay) => BeginDecay(delay);
public DecayedCorpse(string name) : base(Utility.Random(0xECA, 9))
{
private static readonly TimeSpan m_DefaultDecayTime = TimeSpan.FromMinutes(7.0);
private DateTime m_DecayTime;
private Timer m_DecayTimer;
Movable = false;
Name = name;
public DecayedCorpse(string name) : base(Utility.Random(0xECA, 9))
BeginDecay(_defaultDecayTime);
}
// Do not display (x items, y stones)
public override bool DisplaysContent => false;
public void BeginDecay(TimeSpan delay)
{
_decayTimer?.Stop();
DecayTimer = new InternalTimer(this, delay);
DecayTimer.Start();
}
public override void OnAfterDelete()
{
_decayTimer?.Stop();
_decayTimer = null;
}
// Do not display (x items, y stones)
public override bool CheckContentDisplay(Mobile from) => false;
public override void AddNameProperty(IPropertyList list)
{
list.Add(1046414, Name); // the remains of ~1_NAME~
}
public override void OnSingleClick(Mobile from)
{
LabelTo(from, 1046414, Name); // the remains of ~1_NAME~
}
private void Deserialize(IGenericReader reader, int version)
{
if (reader.ReadBool())
{
Movable = false;
Name = name;
BeginDecay(m_DefaultDecayTime);
BeginDecay(reader.ReadDeltaTime() - Core.Now);
}
}
public DecayedCorpse(Serial serial) : base(serial)
private class InternalTimer : Timer
{
private DecayedCorpse _corpse;
public InternalTimer(DecayedCorpse c, TimeSpan delay) : base(delay) => _corpse = c;
protected override void OnTick()
{
}
// Do not display (x items, y stones)
public override bool DisplaysContent => false;
public void BeginDecay(TimeSpan delay)
{
m_DecayTimer?.Stop();
m_DecayTime = Core.Now + delay;
m_DecayTimer = new InternalTimer(this, delay);
m_DecayTimer.Start();
}
public override void OnAfterDelete()
{
m_DecayTimer?.Stop();
m_DecayTimer = null;
}
// Do not display (x items, y stones)
public override bool CheckContentDisplay(Mobile from) => false;
public override void AddNameProperty(IPropertyList list)
{
list.Add(1046414, Name); // the remains of ~1_NAME~
}
public override void OnSingleClick(Mobile from)
{
LabelTo(from, 1046414, Name); // the remains of ~1_NAME~
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(1); // version
writer.Write(m_DecayTimer != null);
if (m_DecayTimer != null)
{
writer.WriteDeltaTime(m_DecayTime);
}
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
switch (version)
{
case 0:
{
BeginDecay(m_DefaultDecayTime);
break;
}
case 1:
{
if (reader.ReadBool())
{
BeginDecay(reader.ReadDeltaTime() - Core.Now);
}
break;
}
}
}
private class InternalTimer : Timer
{
private readonly DecayedCorpse m_Corpse;
public InternalTimer(DecayedCorpse c, TimeSpan delay) : base(delay) => m_Corpse = c;
protected override void OnTick()
{
m_Corpse.Delete();
}
_corpse.Delete();
}
}
}