feat: Moves gumps out of the core (#1916)
> [!Important] > **Developer Note** > This code change will **completely move gumps out of the core** ### Summary - Adds `GetGumps()` convenience which exposes methods to Find/Close/Send multiple gumps. This helper is a performance improvement by eliminating the Dictionary<Player, List> lookup for gumps.
This commit is contained in:
parent
40d99f6d1c
commit
8282b00ca2
246 changed files with 1722 additions and 1383 deletions
87
Projects/UOContent/Gumps/Base/NetStateGumps.cs
Normal file
87
Projects/UOContent/Gumps/Base/NetStateGumps.cs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
using Server.Network;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Gumps.Base;
|
||||
|
||||
public readonly ref struct NetStateGumps
|
||||
{
|
||||
private readonly List<BaseGump> _gumps;
|
||||
private readonly NetState _state;
|
||||
|
||||
public NetStateGumps(List<BaseGump> gumps, NetState state)
|
||||
{
|
||||
_gumps = gumps;
|
||||
_state = state;
|
||||
}
|
||||
|
||||
public bool Close<T>() where T : BaseGump
|
||||
{
|
||||
if (_state == null || _gumps == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _gumps.Count; i++)
|
||||
{
|
||||
if (_gumps[i] is T tGump)
|
||||
{
|
||||
_state.SendCloseGump(tGump.TypeID, 0);
|
||||
tGump.OnServerClose(_state);
|
||||
|
||||
_gumps.RemoveAt(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public T Find<T>() where T : BaseGump
|
||||
{
|
||||
if (_state == null || _gumps == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _gumps.Count; i++)
|
||||
{
|
||||
if (_gumps[i] is T tGump)
|
||||
{
|
||||
return tGump;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool Has<T>() where T : BaseGump => Find<T>() != null;
|
||||
|
||||
public void Send(BaseGump gump, bool singleton = false)
|
||||
{
|
||||
if (_state.CannotSendPackets()) // Cannot send packets handles _state null check
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (singleton || gump.Singleton)
|
||||
{
|
||||
for (int i = 0; i < _gumps.Count; i++)
|
||||
{
|
||||
BaseGump old = _gumps[i];
|
||||
|
||||
if (old.TypeID == gump.TypeID)
|
||||
{
|
||||
_state.SendCloseGump(old.TypeID, 0);
|
||||
old.OnServerClose(_state);
|
||||
|
||||
_gumps[i] = gump;
|
||||
gump.SendTo(_state);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_gumps.Add(gump);
|
||||
gump.SendTo(_state);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue