You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.6 KiB
45 lines
1.6 KiB
10 months ago
|
public static class NodeExtensions
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Similar to <see cref="Node.GetNodeOrNull{T}"/>, but throws a
|
||
|
/// <see cref="InvalidOperationException"/> if the node at the given
|
||
|
/// path does not exist, or a <see cref="InvalidCastException"/> if
|
||
|
/// it is not of the correct type.
|
||
|
/// </summary>
|
||
|
public static T GetNodeOrThrow<T>(this Node self, NodePath path)
|
||
|
where T : class
|
||
|
{
|
||
|
var result = self.GetNodeOrNull(path);
|
||
|
if (result == null) throw new InvalidOperationException(
|
||
|
$"Node at '{path}' not found");
|
||
|
if (result is not T resultOfType) throw new InvalidCastException(
|
||
|
$"Node at '{path}' is {result.GetType()}, expected {typeof(T)}");
|
||
|
return resultOfType;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Finds the first parent of this node that is of
|
||
|
/// type <paramref name="T"/> or null if not found.
|
||
|
/// </summary>
|
||
|
public static T FindParentOrNull<T>(this Node self)
|
||
|
where T : class
|
||
|
{
|
||
|
while ((self = self.GetParent()) != null)
|
||
|
if (self is T result) return result;
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Finds the first parent of this node that is of type <paramref name="T"/>
|
||
|
/// or throws a <see cref="InvalidOperationException"/> if not found.
|
||
|
/// </summary>
|
||
|
public static T FindParentOrThrow<T>(this Node self)
|
||
|
where T : class
|
||
|
=> self.FindParentOrNull<T>() ?? throw new InvalidOperationException(
|
||
|
$"No parent node of type {typeof(T)} found in hierarchy of '{self.GetPath()}'");
|
||
|
|
||
|
/// <summary> Returns whether the current peer has authority over this node. </summary>
|
||
|
public static bool IsAuthority(this Node self)
|
||
|
=> self.Multiplayer.GetUniqueId() == self.GetMultiplayerAuthority();
|
||
|
}
|