Skip to content

Commit e52e3e0

Browse files
committed
Add WorldElementExtensions with extensions to get the allocating user
1 parent e133fe0 commit e52e3e0

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using FrooxEngine;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace MonkeyLoader.Resonite
5+
{
6+
/// <summary>
7+
/// Contains extensions for <see cref="IWorldElement">world elements</see>.
8+
/// </summary>
9+
public static class WorldElementExtensions
10+
{
11+
/// <summary>
12+
/// Gets the allocating <see cref="User"/> of this <see cref="IWorldElement">world element</see>.
13+
/// </summary>
14+
/// <remarks>
15+
/// Implementation is the same as the Allocating User ProtoFlux node.
16+
/// </remarks>
17+
/// <param name="element">The world element to get the allocating <see cref="User"/> of.</param>
18+
/// <returns>This <see cref="IWorldElement">world element</see>'s allocating <see cref="User"/> if valid; otherwise, <see langword="null"/>.</returns>
19+
public static User? GetAllocatingUser(this IWorldElement? element)
20+
{
21+
if (element!.FilterWorldElement() is null)
22+
return null;
23+
24+
element!.ReferenceID.ExtractIDs(out var position, out var allocationId);
25+
var user = element.World.GetUserByAllocationID(allocationId);
26+
27+
if (user is null || position < user.AllocationIDStart)
28+
return null;
29+
30+
return user;
31+
}
32+
33+
/// <summary>
34+
/// Tries to get the allocating <see cref="User"/> of this <see cref="IWorldElement">world element</see>.
35+
/// </summary>
36+
/// <remarks>
37+
/// Implementation is the same as the Allocating User ProtoFlux node.
38+
/// </remarks>
39+
/// <param name="element">The world element to get the allocating <see cref="User"/> of.</param>
40+
/// <param name="user">This <see cref="IWorldElement">world element</see>'s allocating <see cref="User"/> if valid; otherwise, <see langword="null"/>.</param>
41+
/// <returns><see langword="true"/> if this <see cref="IWorldElement">world element</see> has a valid allocating <see cref="User"/>; otherwise, <see langword="false"/>.</returns>
42+
public static bool TryGetAllocatingUser(this IWorldElement? element, [NotNullWhen(true)] out User? user)
43+
{
44+
user = element.GetAllocatingUser();
45+
return user is not null;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)