Skip to content

Commit

Permalink
Hint fixes & pooling
Browse files Browse the repository at this point in the history
  • Loading branch information
marchellc committed Nov 23, 2024
1 parent d51303e commit 83575fd
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 86 deletions.
11 changes: 7 additions & 4 deletions LabExtended/API/Hints/HintController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using LabExtended.API.Collections.Locked;
using LabExtended.API.Enums;

using LabExtended.API.Pooling;
using LabExtended.Core;
using LabExtended.Core.Configs;
using LabExtended.Core.Ticking;
Expand Down Expand Up @@ -114,7 +114,7 @@ public static void ClearElements()
{
_elements.ForEach(x =>
{
x.SetInactive();
x.IsActive = false;

x.Whitelist.Dispose();
x.Data.Clear();
Expand Down Expand Up @@ -158,7 +158,7 @@ public static bool RemoveElement(HintElement element)
if (element is null)
throw new ArgumentNullException(nameof(element));

element.SetInactive();
element.IsActive = false;

if (!_elements.Remove(element))
return false;
Expand Down Expand Up @@ -200,7 +200,7 @@ public static bool AddElement(HintElement element)

if (_elements.Add(element))
{
element.SetActive();
element.IsActive = true;
element.OnEnabled();
return true;
}
Expand Down Expand Up @@ -351,7 +351,9 @@ private static void OnTick()
{
if (element._prevCompiled is null || element._prevCompiled != content)
{
element.Data.ForEach(x => ObjectPool<HintData>.Return(x));
element.Data.Clear();

element._prevCompiled = content;

content = content
Expand Down Expand Up @@ -481,6 +483,7 @@ private static void AppendMessages(IEnumerable<HintData> hints, HintAlign align,
builder.Append(message.Content);
}

builder.Append("</voffset>");
builder.AppendLine();
}
}
Expand Down
18 changes: 5 additions & 13 deletions LabExtended/API/Hints/HintData.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
namespace LabExtended.API.Hints
{
public struct HintData
public class HintData
{
public readonly float VerticalOffset;
public readonly string Content;
public readonly int Size;
public readonly int Id;

public HintData(string content, int size, float vOffset, int id)
{
VerticalOffset = vOffset;
Content = content;
Size = size;
Id = id;
}
public float VerticalOffset;
public string Content;
public int Size;
public int Id;
}
}
52 changes: 9 additions & 43 deletions LabExtended/API/Hints/HintElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ namespace LabExtended.API.Hints
{
public abstract class HintElement
{
public const float DefaultVerticalOffset = 0f;
public const int DefaultPixelLineSpacing = 3;

internal long _tickNum = 0;
internal string _prevCompiled = null;

internal int _pixelSpacing = 3;
internal float _vOffset = 0f;

internal bool _isGlobal = true;
internal bool _isRaw = false;

internal bool _shouldWrap = true;

internal HintAlign _align;

public int Id { get; internal set; }
public string CustomId { get; internal set; }

Expand All @@ -29,12 +22,12 @@ public abstract class HintElement

public bool IsActive { get; internal set; }

public virtual HintAlign Align => _align;
public virtual HintAlign Align { get; } = HintAlign.Center;

public virtual bool IsGlobal => _isGlobal;
public virtual bool IsRaw => _isRaw;
public virtual bool IsGlobal { get; } = true;
public virtual bool IsRaw { get; } = false;

public virtual bool ShouldWrap => _shouldWrap;
public virtual bool ShouldWrap { get; } = true;

public virtual void TickElement() { }

Expand All @@ -46,41 +39,14 @@ public virtual void OnWhitelistRemoved(ExPlayer player) { }

public virtual bool ValidateWhitelist(ExPlayer player) => false;

public virtual float GetVerticalOffset(ExPlayer player) => _vOffset;
public virtual int GetPixelSpacing(ExPlayer player) => _pixelSpacing;
public virtual int GetPixelSpacing(ExPlayer player) => DefaultPixelLineSpacing;
public virtual float GetVerticalOffset(ExPlayer player) => DefaultVerticalOffset;

public abstract string BuildContent(ExPlayer player);

internal bool CompareId(string customId)
=> !string.IsNullOrWhiteSpace(customId) && !string.IsNullOrWhiteSpace(CustomId) && customId == CustomId;

public void SetCustomId(string customId) => CustomId = customId;

public void SetActive() => IsActive = true;
public void SetInactive() => IsActive = false;
public void ToggleActive() => IsActive = !IsActive;

public void DisableWhitelist() => _isGlobal = true;
public void EnableWhitelist() => _isGlobal = false;
public void ToggleWhitelist() => _isGlobal = !_isGlobal;

public void MakeRawDisplay() => _isRaw = true;
public void MakeParsedDisplay() => _isRaw = false;
public void ToggleParsedDisplay() => _isRaw = !_isRaw;

public void EnableWrapping() => _shouldWrap = true;
public void DisableWrapping() => _shouldWrap = false;
public void ToggleWrapping() => _shouldWrap = !_shouldWrap;

public void SetVerticalOffset(float verticalOffset) => _vOffset = verticalOffset;
public void SetAlignment(HintAlign align) => _align = align;

public void SetPixelSpacing(int pixelSpacing)
{
if (pixelSpacing < 0)
throw new ArgumentOutOfRangeException(nameof(pixelSpacing));

_pixelSpacing = pixelSpacing;
}
}
}
14 changes: 12 additions & 2 deletions LabExtended/API/Pooling/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@ public static void PreConstruct(int size, Func<T> create = null)
throw new ArgumentOutOfRangeException(nameof(size));

for (int i = 0; i < size; i++)
_pool.Enqueue((create != null ? create() : Create()));
{
if (create != null)
_pool.Enqueue(create());
else
_pool.Enqueue(Create());
}
}

public static T Rent(Action<T> setup = null, Func<T> create = null)
{
if (!_pool.TryDequeue(out var item))
item = (create is null ? Create() : create());
{
if (create != null)
item = create();
else
item = Create();
}

setup.InvokeSafe(item);
return item;
Expand Down
20 changes: 10 additions & 10 deletions LabExtended/Commands/CustomCollectionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,32 @@
using LabExtended.API.Collections.Locked;

using NorthwoodLib.Pools;
using HarmonyLib;

using System.Reflection;

namespace LabExtended.Commands
{
public abstract class CustomCommand<T> : CustomCommand
where T : class, IArgumentCollection
where T : class
{
private LockedList<ArgumentCollectionMember> _members;
private Func<object[], object> _constructor;

public CustomCommand() : base() { }

public abstract T Instantiate();

public override ArgumentDefinition[] BuildArgs()
=> GenerateArgs();

public override void OnCommand(ExPlayer sender, ICommandContext ctx, ArgumentCollection args)
{
base.OnCommand(sender, ctx, args);

var buffer = ObjectPool<object[]>.Rent(null, null);
var buffer = ObjectPool<object[]>.Rent(null, () => new object[1]);

try
{
var collection = ObjectPool<T>.Rent(null, () => (T)_constructor(Array.Empty<object>()));

collection.Initialize(sender, ctx, args);
var collection = ObjectPool<T>.Rent(null, Instantiate);

for (int i = 0; i < _members.Count; i++)
{
Expand All @@ -50,7 +48,10 @@ public override void OnCommand(ExPlayer sender, ICommandContext ctx, ArgumentCol

OnCommand(sender, ctx, collection);

ObjectPool<T>.Return(collection, _ => collection.Dispose());
if (collection is IDisposable disposable)
disposable.Dispose();

ObjectPool<T>.Return(collection);
}
catch (Exception ex)
{
Expand All @@ -66,7 +67,6 @@ public virtual void OnCommand(ExPlayer sender, ICommandContext ctx, T collection
private ArgumentDefinition[] GenerateArgs()
{
_members = new LockedList<ArgumentCollectionMember>();
_constructor = FastReflection.ForConstructor(typeof(T).Constructor());

var properties = typeof(T).GetAllProperties();
var list = ListPool<ArgumentDefinition>.Shared.Rent();
Expand All @@ -87,7 +87,7 @@ private ArgumentDefinition[] GenerateArgs()
member.Definition = new ArgumentDefinition(
property.PropertyType,

attribute is null || string.IsNullOrWhiteSpace(attribute.Name) ? property.Name : attribute.Name,
attribute is null || string.IsNullOrWhiteSpace(attribute.Name) ? property.Name.SpaceByUpperCase() : attribute.Name,
attribute is null || string.IsNullOrWhiteSpace(attribute.Description) ? parser.Name : attribute.Description,

parser,
Expand Down
10 changes: 0 additions & 10 deletions LabExtended/Commands/Interfaces/IArgumentCollection.cs

This file was deleted.

34 changes: 30 additions & 4 deletions LabExtended/Utilities/HintUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LabExtended.API.Collections.Locked;
using LabExtended.API.Hints;
using LabExtended.API.Pooling;

using System.Text.RegularExpressions;

Expand Down Expand Up @@ -119,7 +120,13 @@ internal static void GetMessages(string content, LockedHashSet<HintData> message
if (messages.Count > 0)
vOffset -= (biggestPixelSize + pixelLineSpacing) / (float)PixelsPerEm;

messages.Add(new HintData(line, biggestPixelSize, vOffset, ++clock));
messages.Add(ObjectPool<HintData>.Rent(x =>
{
x.Content = line;
x.Size = biggestPixelSize;
x.VerticalOffset = vOffset;
x.Id = ++clock;
}, () => new HintData()));

line = tagEnded ? "" : $"<size={pixelSize}>";
avgCharWidth = AvgCharWidth(pixelSize);
Expand Down Expand Up @@ -148,7 +155,13 @@ internal static void GetMessages(string content, LockedHashSet<HintData> message
if (messages.Count > 0)
vOffset -= (biggestPixelSize + pixelLineSpacing) / (float)PixelsPerEm;

messages.Add(new HintData(line, biggestPixelSize, vOffset, ++clock));
messages.Add(ObjectPool<HintData>.Rent(x =>
{
x.Content = line;
x.Size = biggestPixelSize;
x.VerticalOffset = vOffset;
x.Id = ++clock;
}, () => new HintData()));
}

while (text.Length * avgCharWidth > 100f)
Expand All @@ -162,7 +175,14 @@ internal static void GetMessages(string content, LockedHashSet<HintData> message
if (messages.Count > 0)
vOffset -= (biggestPixelSize + pixelLineSpacing) / (float)PixelsPerEm;

messages.Add(new HintData(line, biggestPixelSize, vOffset, ++clock));
messages.Add(ObjectPool<HintData>.Rent(x =>
{
x.Content = line;
x.Size = biggestPixelSize;
x.VerticalOffset = vOffset;
x.Id = ++clock;
}, () => new HintData()));

text = text.Substring(cutIndex);
}

Expand All @@ -178,7 +198,13 @@ internal static void GetMessages(string content, LockedHashSet<HintData> message
if (messages.Count > 0)
vOffset -= (biggestPixelSize + pixelLineSpacing) / (float)PixelsPerEm;

messages.Add(new HintData(line, biggestPixelSize, vOffset, ++clock));
messages.Add(ObjectPool<HintData>.Rent(x =>
{
x.Content = line;
x.Size = biggestPixelSize;
x.VerticalOffset = vOffset;
x.Id = ++clock;
}, () => new HintData()));
}
}
}
Expand Down

0 comments on commit 83575fd

Please sign in to comment.