forked from tModLoader/tModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modernize PlayerLoader, support custom ModPlayer hooks. (tModLoader#2645
) * Modernized PlayerLoader, ModPlayer is now a GlobalType * Added PlayerLoader.AddModHook. * Partial revert, IIndexed interface, repairs. * player.(ModPlayers.array -> modPlayers) * Undid unnecessary removal. * Ordering, -trails. * Player mod hook clearing. * Make a HookList enumerator which works without Instanced<T>[] * Add HookList.Enumerate variants for arrays, spans and lists Co-authored-by: Chicken-Bones <[email protected]>
- Loading branch information
1 parent
9ad6fc8
commit a21502d
Showing
14 changed files
with
391 additions
and
301 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
patches/tModLoader/Terraria/ModLoader/Core/FilteredEnumerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Terraria.ModLoader.Core | ||
{ | ||
// split into two classes, because the span version is slow. Keep an eye on https://github.com/dotnet/runtime/issues/68797 and check disassembly periodically | ||
// or https://github.com/dotnet/runtime/issues/9977 | ||
// and my issue https://github.com/dotnet/runtime/issues/71510 | ||
public ref struct FilteredArrayEnumerator<T> | ||
{ | ||
// T[] current produces much better code-gen than `ReadOnlySpan<T>` | ||
private readonly T[] arr; | ||
private readonly int[] inds; | ||
|
||
private T current; | ||
private int i; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public FilteredArrayEnumerator(T[] arr, int[] inds) { | ||
this.arr = arr; | ||
this.inds = inds; | ||
current = default; | ||
i = 0; | ||
} | ||
|
||
public T Current => current; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool MoveNext() { | ||
if (i >= inds.Length) | ||
return false; | ||
|
||
current = arr[inds[i++]]; | ||
return true; | ||
} | ||
|
||
public FilteredArrayEnumerator<T> GetEnumerator() => this; | ||
} | ||
|
||
public ref struct FilteredSpanEnumerator<T> | ||
{ | ||
private readonly ReadOnlySpan<T> arr; | ||
private readonly int[] inds; | ||
|
||
private T current; | ||
private int i; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public FilteredSpanEnumerator(ReadOnlySpan<T> arr, int[] inds) { | ||
this.arr = arr; | ||
this.inds = inds; | ||
current = default; | ||
i = 0; | ||
} | ||
|
||
public T Current => current; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool MoveNext() { | ||
if (i >= inds.Length) | ||
return false; | ||
|
||
current = arr[inds[i++]]; | ||
return true; | ||
} | ||
|
||
public FilteredSpanEnumerator<T> GetEnumerator() => this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Terraria.ModLoader | ||
{ | ||
public interface IIndexed | ||
{ | ||
ushort Index { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.