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

@ -1,5 +1,5 @@
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Mobiles;
using Server.Multis;
@ -41,10 +41,10 @@ public abstract class BaseContainer : Container
return base.CheckItemUse(from, item);
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
SetSecureLevelEntry.AddTo(from, this, list);
base.GetContextMenuEntries(from, ref list);
SetSecureLevelEntry.AddTo(from, this, ref list);
}
public override bool TryDropItem(Mobile from, Item dropped, bool sendFullMessage)

View file

@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.Craft;
using Server.Network;
namespace Server.Items;
@ -27,18 +26,18 @@ public partial class SalvageBag : Bag
public override int LabelNumber => 1079931; // Salvage Bag
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
if (from.Alive)
{
var inBackpack = IsChildOf(from.Backpack);
var resmeltables = inBackpack && Resmeltables();
var scissorables = inBackpack && Scissorables();
list.Add(new SalvageIngotsEntry(this, resmeltables));
list.Add(new SalvageClothEntry(this, scissorables));
list.Add(new SalvageAllEntry(this, resmeltables && scissorables));
list.Add(new SalvageIngotsEntry(resmeltables));
list.Add(new SalvageClothEntry(scissorables));
list.Add(new SalvageAllEntry(resmeltables && scissorables));
}
}
@ -287,90 +286,39 @@ public partial class SalvageBag : Bag
private class SalvageAllEntry : ContextMenuEntry
{
private readonly SalvageBag m_Bag;
public SalvageAllEntry(bool enabled) : base(6276) => Enabled = enabled;
public SalvageAllEntry(SalvageBag bag, bool enabled) : base(6276)
public override void OnClick(Mobile from, IEntity target)
{
m_Bag = bag;
if (!enabled)
if (from.CheckAlive() && target is SalvageBag { Deleted: false } bag)
{
Flags |= CMEFlags.Disabled;
}
}
public override void OnClick()
{
if (m_Bag.Deleted)
{
return;
}
var from = Owner.From;
if (from.CheckAlive())
{
m_Bag.SalvageAll(from);
bag.SalvageAll(from);
}
}
}
private class SalvageIngotsEntry : ContextMenuEntry
{
private readonly SalvageBag m_Bag;
public SalvageIngotsEntry(bool enabled) : base(6277) => Enabled = enabled;
public SalvageIngotsEntry(SalvageBag bag, bool enabled) : base(6277)
public override void OnClick(Mobile from, IEntity target)
{
m_Bag = bag;
if (!enabled)
if (from.CheckAlive() && target is SalvageBag { Deleted: false } bag)
{
Flags |= CMEFlags.Disabled;
}
}
public override void OnClick()
{
if (m_Bag.Deleted)
{
return;
}
var from = Owner.From;
if (from.CheckAlive())
{
m_Bag.SalvageIngots(from);
bag.SalvageIngots(from);
}
}
}
private class SalvageClothEntry : ContextMenuEntry
{
private readonly SalvageBag m_Bag;
public SalvageClothEntry(bool enabled) : base(6278) => Enabled = enabled;
public SalvageClothEntry(SalvageBag bag, bool enabled) : base(6278)
public override void OnClick(Mobile from, IEntity target)
{
m_Bag = bag;
if (!enabled)
if (from.CheckAlive() && target is SalvageBag { Deleted: false } bag)
{
Flags |= CMEFlags.Disabled;
}
}
public override void OnClick()
{
if (m_Bag.Deleted)
{
return;
}
var from = Owner.From;
if (from.CheckAlive())
{
m_Bag.SalvageCloth(from);
bag.SalvageCloth(from);
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using ModernUO.Serialization;
using Server.Collections;
using Server.ContextMenus;
using Server.Engines.PartySystem;
using Server.Gumps;
@ -415,13 +416,13 @@ public partial class TreasureMapChest : LockableContainer
base.OnAfterDelete();
}
public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
public override void GetContextMenuEntries(Mobile from, ref PooledRefList<ContextMenuEntry> list)
{
base.GetContextMenuEntries(from, list);
base.GetContextMenuEntries(from, ref list);
if (from.Alive)
{
list.Add(new RemoveEntry(from, this));
list.Add(new RemoveEntry(from == _owner));
}
}
@ -487,25 +488,16 @@ public partial class TreasureMapChest : LockableContainer
private class RemoveEntry : ContextMenuEntry
{
private readonly TreasureMapChest _chest;
private readonly Mobile _from;
public RemoveEntry(bool enabled) : base(6149, 3) => Enabled = enabled;
public RemoveEntry(Mobile from, TreasureMapChest chest) : base(6149, 3)
public override void OnClick(Mobile from, IEntity target)
{
_from = from;
_chest = chest;
Enabled = from == chest._owner;
}
public override void OnClick()
{
if (_chest.Deleted || _from != _chest._owner || !_from.CheckAlive())
if (!from.CheckAlive() || target is not TreasureMapChest chest || chest.Deleted || from != chest._owner)
{
return;
}
_chest.BeginRemove(_from);
chest.BeginRemove(from);
}
}
}