ModernUO/Projects/UOContent/Gumps/Base/NetStateGumps.cs
Kamron Batman 8282b00ca2
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.
2024-08-09 19:07:32 -07:00

87 lines
1.9 KiB
C#

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);
}
}