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:
parent
bb24b330e1
commit
f58117a877
88 changed files with 1206 additions and 1483 deletions
|
|
@ -1,4 +1,17 @@
|
|||
using System.Collections.Generic;
|
||||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2024 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ContextMenu.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
namespace Server.ContextMenus;
|
||||
|
||||
|
|
@ -18,30 +31,28 @@ public class ContextMenu
|
|||
/// <seealso cref="From" />
|
||||
/// </param>
|
||||
/// <param name="target">
|
||||
/// The <see cref="Mobile" /> or <see cref="Item" /> for which this ContextMenu is on.
|
||||
/// The <see cref="Mobile" /> or <see cref="Item" /> to execute the ContextMenu on.
|
||||
/// <seealso cref="Target" />
|
||||
/// </param>
|
||||
public ContextMenu(Mobile from, IEntity target)
|
||||
/// <param name="entries">
|
||||
/// An array of <see cref="ContextMenuEntry">entries</see> contained in this ContextMenu.
|
||||
/// <seealso cref="Entries" />
|
||||
/// </param>
|
||||
public ContextMenu(Mobile from, IEntity target, ContextMenuEntry[] entries)
|
||||
{
|
||||
From = from;
|
||||
Target = target;
|
||||
Entries = entries;
|
||||
|
||||
var list = new List<ContextMenuEntry>();
|
||||
|
||||
if (target is Mobile mobile)
|
||||
for (var i = 0; i < Entries.Length; i++)
|
||||
{
|
||||
mobile.GetContextMenuEntries(from, list);
|
||||
}
|
||||
else if (target is Item item)
|
||||
{
|
||||
item.GetContextMenuEntries(from, list);
|
||||
}
|
||||
var entry = Entries[i];
|
||||
|
||||
Entries = list.ToArray();
|
||||
|
||||
for (var i = 0; i < Entries.Length; ++i)
|
||||
{
|
||||
Entries[i].Owner = this;
|
||||
if (entry.Number is < 3000000 or > 3032767)
|
||||
{
|
||||
RequiresNewPacket = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -63,20 +74,5 @@ public class ContextMenu
|
|||
/// <summary>
|
||||
/// Returns true if this ContextMenu requires packet version 2.
|
||||
/// </summary>
|
||||
public bool RequiresNewPacket
|
||||
{
|
||||
get
|
||||
{
|
||||
for (var i = 0; i < Entries.Length; ++i)
|
||||
{
|
||||
var number = Entries[i].Number;
|
||||
if (number is < 3000000 or > 3032767)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool RequiresNewPacket { get; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,18 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2024 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ContextMenuEntry.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using Server.Network;
|
||||
|
||||
namespace Server.ContextMenus;
|
||||
|
|
@ -42,11 +57,6 @@ public class ContextMenuEntry
|
|||
/// </summary>
|
||||
public CMEFlags Flags { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="ContextMenu" /> that owns this entry.
|
||||
/// </summary>
|
||||
public ContextMenu Owner { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the localization number containing the name of this entry.
|
||||
/// </summary>
|
||||
|
|
@ -76,7 +86,7 @@ public class ContextMenuEntry
|
|||
/// <summary>
|
||||
/// Overridable. Virtual event invoked when the entry is clicked.
|
||||
/// </summary>
|
||||
public virtual void OnClick()
|
||||
public virtual void OnClick(Mobile from, IEntity target)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
namespace Server.ContextMenus;
|
||||
|
||||
public class OpenBackpackEntry : ContextMenuEntry
|
||||
{
|
||||
private readonly Mobile m_Mobile;
|
||||
|
||||
public OpenBackpackEntry(Mobile m) : base(6145) => m_Mobile = m;
|
||||
|
||||
public override void OnClick()
|
||||
{
|
||||
m_Mobile.Use(m_Mobile.Backpack);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
namespace Server.ContextMenus;
|
||||
|
||||
public class PaperdollEntry : ContextMenuEntry
|
||||
{
|
||||
private readonly Mobile m_Mobile;
|
||||
|
||||
public PaperdollEntry(Mobile m) : base(6123, 18) => m_Mobile = m;
|
||||
|
||||
public override void OnClick()
|
||||
{
|
||||
if (m_Mobile.CanPaperdollBeOpenedBy(Owner.From))
|
||||
{
|
||||
m_Mobile.DisplayPaperdollTo(Owner.From);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2131,27 +2131,27 @@ public class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropertyListEnt
|
|||
|
||||
public virtual bool CanEquip(Mobile m) => m_Layer != Layer.Invalid && m.FindItemOnLayer(m_Layer) == null;
|
||||
|
||||
public virtual void GetChildContextMenuEntries(Mobile from, List<ContextMenuEntry> list, Item item)
|
||||
public virtual void GetChildContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list, Item item)
|
||||
{
|
||||
if (m_Parent is Item parentItem)
|
||||
{
|
||||
parentItem.GetChildContextMenuEntries(from, list, item);
|
||||
parentItem.GetChildContextMenuEntries(from, ref list, item);
|
||||
}
|
||||
else if (m_Parent is Mobile parentMobile)
|
||||
{
|
||||
parentMobile.GetChildContextMenuEntries(from, list, item);
|
||||
parentMobile.GetChildContextMenuEntries(from, ref list, item);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
|
||||
public virtual void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
|
||||
{
|
||||
if (m_Parent is Item item)
|
||||
{
|
||||
item.GetChildContextMenuEntries(from, list, this);
|
||||
item.GetChildContextMenuEntries(from, ref list, this);
|
||||
}
|
||||
else if (m_Parent is Mobile mobile)
|
||||
{
|
||||
mobile.GetChildContextMenuEntries(from, list, this);
|
||||
mobile.GetChildContextMenuEntries(from, ref list, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -264,7 +264,6 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
|
|||
private int m_ChangingCombatant;
|
||||
private Mobile m_Combatant;
|
||||
private TimerExecutionToken _combatTimerToken;
|
||||
private ContextMenu m_ContextMenu;
|
||||
private bool m_Criminal;
|
||||
|
||||
private MobileDelta m_DeltaFlags;
|
||||
|
|
@ -870,16 +869,6 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
|
|||
}
|
||||
}
|
||||
|
||||
public ContextMenu ContextMenu
|
||||
{
|
||||
get => m_ContextMenu;
|
||||
set
|
||||
{
|
||||
m_ContextMenu = value;
|
||||
m_NetState.SendDisplayContextMenu(m_ContextMenu);
|
||||
}
|
||||
}
|
||||
|
||||
[IgnoreDupe]
|
||||
public bool Pushing { get; set; }
|
||||
|
||||
|
|
@ -6521,26 +6510,12 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
|
|||
|
||||
public virtual bool CanPaperdollBeOpenedBy(Mobile from) => Body.IsHuman || Body.IsGhost || IsBodyMod;
|
||||
|
||||
public virtual void GetChildContextMenuEntries(Mobile from, List<ContextMenuEntry> list, Item item)
|
||||
public virtual void GetChildContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list, Item item)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
|
||||
public virtual void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
|
||||
{
|
||||
if (Deleted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (CanPaperdollBeOpenedBy(from))
|
||||
{
|
||||
list.Add(new PaperdollEntry(this));
|
||||
}
|
||||
|
||||
if (from == this && Backpack != null && CanSee(Backpack) && CheckAlive(false))
|
||||
{
|
||||
list.Add(new OpenBackpackEntry(this));
|
||||
}
|
||||
}
|
||||
|
||||
public void Internalize()
|
||||
|
|
|
|||
|
|
@ -1,7 +1,21 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2024 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: OutgoingMenuPackets.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using Server.ContextMenus;
|
||||
using Server.Menus.ItemLists;
|
||||
using Server.Menus.Questions;
|
||||
|
||||
|
|
@ -132,82 +146,4 @@ public static class OutgoingMenuPackets
|
|||
writer.WritePacketLength();
|
||||
ns.Send(writer.Span);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue