fix: Moves ContextMenu out of core, streamlines code, fixes bugs (#1873)

## Summary
- Removes allocation of a `List<ContextMenuEntry>` every time a context menu is created.
- Moves packet/context menu creation logic out of the core
- Fixes tame entry

## BREAKING CHANGE
> [!Important]
> **Developer Note**
> ```cs
> public virtual void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
> ```
> and similar functions changed to
> ```cs
> public virtual void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
> ```
This commit is contained in:
Kamron Batman 2024-07-20 21:33:23 -07:00 • committed by GitHub
parent bb24b330e1
commit f58117a877
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
88 changed files with 1206 additions and 1483 deletions

View file

@ -4,47 +4,47 @@ namespace Server.ContextMenus
{
public class AddToPartyEntry : ContextMenuEntry
{
private readonly Mobile m_From;
private readonly Mobile m_Target;
public AddToPartyEntry(Mobile from, Mobile target) : base(0197, 12)
public AddToPartyEntry() : base(0197, 12)
{
m_From = from;
m_Target = target;
}
public override void OnClick()
public override void OnClick(Mobile from, IEntity target)
{
var p = Party.Get(m_From);
var mp = Party.Get(m_Target);
if (m_From == m_Target)
if (target is not Mobile targetMobile)
{
m_From.SendLocalizedMessage(1005439); // You cannot add yourself to a party.
return;
}
else if (p != null && p.Leader != m_From)
var p = Party.Get(from);
var mp = Party.Get(targetMobile);
if (from == targetMobile)
{
m_From.SendLocalizedMessage(1005453); // You may only add members to the party if you are the leader.
from.SendLocalizedMessage(1005439); // You cannot add yourself to a party.
}
else if (p != null && p.Leader != from)
{
from.SendLocalizedMessage(1005453); // You may only add members to the party if you are the leader.
}
else if (p != null && p.Members.Count + p.Candidates.Count >= Party.Capacity)
{
m_From.SendLocalizedMessage(1008095); // You may only have 10 in your party (this includes candidates).
from.SendLocalizedMessage(1008095); // You may only have 10 in your party (this includes candidates).
}
else if (!m_Target.Player)
else if (!targetMobile.Player)
{
m_From.SendLocalizedMessage(1005444); // The creature ignores your offer.
from.SendLocalizedMessage(1005444); // The creature ignores your offer.
}
else if (mp != null && mp == p)
{
m_From.SendLocalizedMessage(1005440); // This person is already in your party!
from.SendLocalizedMessage(1005440); // This person is already in your party!
}
else if (mp != null)
{
m_From.SendLocalizedMessage(1005441); // This person is already in a party!
from.SendLocalizedMessage(1005441); // This person is already in a party!
}
else
{
Party.Invite(m_From, m_Target);
Party.Invite(from, targetMobile);
}
}
}

View file

@ -9,11 +9,11 @@ namespace Server.ContextMenus
{
}
public override void OnClick()
public override void OnClick(Mobile from, IEntity target)
{
if (Owner.From.CheckAlive() && Owner.Target is SpellScroll scroll)
if (from.CheckAlive() && target is SpellScroll scroll)
{
Owner.From.Target = new InternalTarget(scroll);
from.Target = new InternalTarget(scroll);
}
}

View file

@ -0,0 +1,220 @@
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using Server.Collections;
using Server.Network;
namespace Server.ContextMenus;
public static class ContextMenuSystem
{
private static readonly Dictionary<Mobile, ContextMenu> _menus = [];
public static unsafe void Configure()
{
IncomingExtendedCommandPackets.RegisterExtended(0x13, true, &ContextMenuRequest);
IncomingExtendedCommandPackets.RegisterExtended(0x15, true, &ContextMenuResponse);
}
public static ContextMenu CreateContextMenu(Mobile from, IEntity target)
{
if (target?.Deleted != false)
{
return new ContextMenu(from, target, []);
}
var list = PooledRefList<ContextMenuEntry>.Create();
if (target is Mobile mobile)
{
if (mobile.CanPaperdollBeOpenedBy(from))
{
list.Add(new PaperdollEntry());
}
mobile.GetContextMenuEntries(from, ref list);
}
else if (target is Item item)
{
item.GetContextMenuEntries(from, ref list);
}
var entries = list.ToArray();
list.Dispose();
return new ContextMenu(from, target, entries);
}
public static void ContextMenuResponse(NetState state, SpanReader reader)
{
var from = state.Mobile;
if (from == null || !_menus.Remove(from, out var menu) || from != menu.From)
{
return;
}
var entity = World.FindEntity((Serial)reader.ReadUInt32());
if (entity == null || entity != menu.Target || !from.CanSee(entity))
{
return;
}
Point3D p;
if (entity is Mobile)
{
p = entity.Location;
}
else if (entity is Item item)
{
p = item.GetWorldLocation();
}
else
{
return;
}
int index = reader.ReadUInt16();
if (index >= menu.Entries.Length)
{
return;
}
var e = menu.Entries[index];
var range = e.Range;
if (range == -1)
{
range = 18;
}
if (e.Enabled && from.InRange(p, range))
{
e.OnClick(from, entity);
}
}
public static void ContextMenuRequest(NetState state, SpanReader reader)
{
var from = state.Mobile;
var target = World.FindEntity((Serial)reader.ReadUInt32());
if (from == null || target == null || from.Map != target.Map || !from.CanSee(target))
{
return;
}
var item = target as Item;
var checkLocation = item?.GetWorldLocation() ?? target.Location;
if (!(Utility.InUpdateRange(from.Location, checkLocation) && from.CheckContextMenuDisplay(target)))
{
return;
}
var c = CreateContextMenu(from, target);
if (c.Entries.Length <= 0)
{
return;
}
if (item?.RootParent is Mobile mobile && mobile != from && mobile.AccessLevel >= from.AccessLevel)
{
for (var i = 0; i < c.Entries.Length; ++i)
{
var entry = c.Entries[i];
if (!entry.NonLocalUse)
{
entry.Enabled = false;
}
}
}
_menus[from] = c;
state.SendDisplayContextMenu(c);
}
public static void SendDisplayContextMenu(this NetState ns, ContextMenu menu)
{
if (ns == null || menu == null)
{
return;
}
var newCommand = ns.NewHaven && menu.RequiresNewPacket;
var entries = menu.Entries;
var entriesLength = (byte)entries.Length;
var maxLength = 12 + entriesLength * 8;
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0xBF); // Packet ID
writer.Seek(2, SeekOrigin.Current); // Length
writer.Write((short)0x14); // Subpacket
writer.Write((short)(newCommand ? 0x02 : 0x01)); // Command
var target = menu.Target;
writer.Write(target.Serial);
writer.Write(entriesLength);
var p = target switch
{
Mobile _ => target.Location,
Item item => item.GetWorldLocation(),
_ => Point3D.Zero
};
for (var i = 0; i < entriesLength; ++i)
{
var e = entries[i];
var range = e.Range;
if (range == -1)
{
range = Core.GlobalUpdateRange;
}
var flags = e.Flags;
if (!(e.Enabled && menu.From.InRange(p, range)))
{
flags |= CMEFlags.Disabled;
}
if (newCommand)
{
writer.Write(e.Number);
writer.Write((short)i);
writer.Write((short)flags);
}
else
{
writer.Write((short)i);
writer.Write((ushort)(e.Number - 3000000));
var color = e.Color & 0xFFFF;
if (color != 0xFFFF)
{
flags |= CMEFlags.Colored;
}
writer.Write((short)flags);
if ((flags & CMEFlags.Colored) != 0)
{
writer.Write((short)color);
}
}
}
writer.WritePacketLength();
ns.Send(writer.Span);
}
}

View file

@ -4,23 +4,16 @@ namespace Server.ContextMenus
{
public class EatEntry : ContextMenuEntry
{
private readonly Food m_Food;
private readonly Mobile m_From;
public EatEntry(Mobile from, Food food) : base(6135, 1)
public EatEntry() : base(6135, 1)
{
m_From = from;
m_Food = food;
}
public override void OnClick()
public override void OnClick(Mobile from, IEntity target)
{
if (m_Food?.Deleted != false || !m_Food.Movable || !m_From.CheckAlive() || !m_Food.CheckItemUse(m_From))
if (from.CheckAlive() && target is Food { Deleted: false, Movable: true } food && food.CheckItemUse(from))
{
return;
food.Eat(from);
}
m_Food.Eat(m_From);
}
}
}

View file

@ -4,25 +4,25 @@ namespace Server.ContextMenus
{
public class EjectPlayerEntry : ContextMenuEntry
{
private readonly Mobile m_From;
private readonly Mobile m_Target;
private readonly BaseHouse m_TargetHouse;
public EjectPlayerEntry(Mobile from, Mobile target) : base(6206, 12)
public EjectPlayerEntry() : base(6206, 12)
{
m_From = from;
m_Target = target;
m_TargetHouse = BaseHouse.FindHouseAt(m_Target);
}
public override void OnClick()
public override void OnClick(Mobile from, IEntity target)
{
if (!m_From.Alive || m_TargetHouse.Deleted || !m_TargetHouse.IsFriend(m_From))
if (target is not Mobile targetMobile)
{
return;
}
m_TargetHouse.Kick(m_From, m_Target);
var house = BaseHouse.FindHouseAt(targetMobile);
if (!from.Alive || house.Deleted || !house.IsFriend(from))
{
return;
}
house.Kick(from, targetMobile);
}
}
}

View file

@ -0,0 +1,13 @@
namespace Server.ContextMenus;
public class OpenBackpackEntry : ContextMenuEntry
{
public OpenBackpackEntry() : base(6145)
{
}
public override void OnClick(Mobile from, IEntity target)
{
from.Use(from.Backpack);
}
}

View file

@ -2,24 +2,24 @@ namespace Server.ContextMenus
{
public class OpenBankEntry : ContextMenuEntry
{
private readonly Mobile m_Banker;
public OpenBankEntry(Mobile banker) : base(6105, 12) => m_Banker = banker;
public override void OnClick()
public OpenBankEntry() : base(6105, 12)
{
if (!Owner.From.CheckAlive())
}
public override void OnClick(Mobile from, IEntity target)
{
if (!from.CheckAlive() || target is not Mobile banker)
{
return;
}
if (Owner.From.Criminal)
if (from.Criminal)
{
m_Banker.Say(500378); // Thou art a criminal and cannot access thy bank box.
banker.Say(500378); // Thou art a criminal and cannot access thy bank box.
}
else
{
Owner.From.BankBox.Open();
from.BankBox.Open();
}
}
}

View file

@ -0,0 +1,16 @@
namespace Server.ContextMenus;
public class PaperdollEntry : ContextMenuEntry
{
public PaperdollEntry() : base(6123, 18)
{
}
public override void OnClick(Mobile from, IEntity target)
{
if (target is Mobile mobile && mobile.CanPaperdollBeOpenedBy(from))
{
mobile.DisplayPaperdollTo(from);
}
}
}

View file

@ -1,34 +1,25 @@
using Server.Mobiles;
using Server.Network;
namespace Server.ContextMenus
{
public class TeachEntry : ContextMenuEntry
{
private readonly Mobile m_From;
private readonly BaseCreature m_Mobile;
private readonly SkillName m_Skill;
private readonly SkillName _skill;
public TeachEntry(SkillName skill, BaseCreature m, Mobile from, bool enabled) : base(6000 + (int)skill)
public TeachEntry(SkillName skill, bool enabled) : base(6000 + (int)skill)
{
m_Skill = skill;
m_Mobile = m;
m_From = from;
if (!enabled)
{
Flags |= CMEFlags.Disabled;
}
_skill = skill;
Enabled = enabled;
}
public override void OnClick()
public override void OnClick(Mobile from, IEntity target)
{
if (!m_From.CheckAlive())
if (!from.CheckAlive() || target is not BaseCreature bc)
{
return;
}
m_Mobile.Teach(m_Skill, m_From, 0, false);
bc.Teach(_skill, from, 0, false);
}
}
}