mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 12:14:16 +01:00
Convert ObjectTable enumerator to struct
This commit is contained in:
parent
6340afb692
commit
2b2f628096
1 changed files with 3 additions and 37 deletions
|
|
@ -12,8 +12,6 @@ using Dalamud.Utility;
|
||||||
|
|
||||||
using FFXIVClientStructs.Interop;
|
using FFXIVClientStructs.Interop;
|
||||||
|
|
||||||
using Microsoft.Extensions.ObjectPool;
|
|
||||||
|
|
||||||
using CSGameObject = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject;
|
using CSGameObject = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject;
|
||||||
using CSGameObjectManager = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObjectManager;
|
using CSGameObjectManager = FFXIVClientStructs.FFXIV.Client.Game.Object.GameObjectManager;
|
||||||
|
|
||||||
|
|
@ -34,8 +32,6 @@ internal sealed partial class ObjectTable : IServiceType, IObjectTable
|
||||||
private readonly ClientState clientState;
|
private readonly ClientState clientState;
|
||||||
private readonly CachedEntry[] cachedObjectTable;
|
private readonly CachedEntry[] cachedObjectTable;
|
||||||
|
|
||||||
private readonly Enumerator?[] frameworkThreadEnumerators = new Enumerator?[4];
|
|
||||||
|
|
||||||
[ServiceManager.ServiceConstructor]
|
[ServiceManager.ServiceConstructor]
|
||||||
private unsafe ObjectTable(ClientState clientState)
|
private unsafe ObjectTable(ClientState clientState)
|
||||||
{
|
{
|
||||||
|
|
@ -47,9 +43,6 @@ internal sealed partial class ObjectTable : IServiceType, IObjectTable
|
||||||
this.cachedObjectTable = new CachedEntry[objectTableLength];
|
this.cachedObjectTable = new CachedEntry[objectTableLength];
|
||||||
for (var i = 0; i < this.cachedObjectTable.Length; i++)
|
for (var i = 0; i < this.cachedObjectTable.Length; i++)
|
||||||
this.cachedObjectTable[i] = new(nativeObjectTable.GetPointer(i));
|
this.cachedObjectTable[i] = new(nativeObjectTable.GetPointer(i));
|
||||||
|
|
||||||
for (var i = 0; i < this.frameworkThreadEnumerators.Length; i++)
|
|
||||||
this.frameworkThreadEnumerators[i] = new(this, i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|
@ -239,30 +232,14 @@ internal sealed partial class ObjectTable
|
||||||
public IEnumerator<IGameObject> GetEnumerator()
|
public IEnumerator<IGameObject> GetEnumerator()
|
||||||
{
|
{
|
||||||
ThreadSafety.AssertMainThread();
|
ThreadSafety.AssertMainThread();
|
||||||
|
return new Enumerator(this);
|
||||||
// If we're on the framework thread, see if there's an already allocated enumerator available for use.
|
|
||||||
foreach (ref var x in this.frameworkThreadEnumerators.AsSpan())
|
|
||||||
{
|
|
||||||
if (x is not null)
|
|
||||||
{
|
|
||||||
var t = x;
|
|
||||||
x = null;
|
|
||||||
t.Reset();
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// No reusable enumerator is available; allocate a new temporary one.
|
|
||||||
return new Enumerator(this, -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
|
||||||
|
|
||||||
private sealed class Enumerator(ObjectTable owner, int slotId) : IEnumerator<IGameObject>, IResettable
|
private struct Enumerator(ObjectTable owner) : IEnumerator<IGameObject>
|
||||||
{
|
{
|
||||||
private ObjectTable? owner = owner;
|
|
||||||
|
|
||||||
private int index = -1;
|
private int index = -1;
|
||||||
|
|
||||||
public IGameObject Current { get; private set; } = null!;
|
public IGameObject Current { get; private set; } = null!;
|
||||||
|
|
@ -274,7 +251,7 @@ internal sealed partial class ObjectTable
|
||||||
if (this.index == objectTableLength)
|
if (this.index == objectTableLength)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var cache = this.owner!.cachedObjectTable.AsSpan();
|
var cache = owner.cachedObjectTable.AsSpan();
|
||||||
for (this.index++; this.index < objectTableLength; this.index++)
|
for (this.index++; this.index < objectTableLength; this.index++)
|
||||||
{
|
{
|
||||||
if (cache[this.index].Update() is { } ao)
|
if (cache[this.index].Update() is { } ao)
|
||||||
|
|
@ -291,17 +268,6 @@ internal sealed partial class ObjectTable
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (this.owner is not { } o)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (slotId != -1)
|
|
||||||
o.frameworkThreadEnumerators[slotId] = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryReset()
|
|
||||||
{
|
|
||||||
this.Reset();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue