This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
@@ -31,6 +32,11 @@ public interface IScriptSet : IEnumerable<ScriptContainer>
|
||||
/// </summary>
|
||||
ScriptContainer? Get(StringKey scriptKey);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a script from the set using its type.
|
||||
/// </summary>
|
||||
bool TryGet<T>([NotNullWhen(true)] out T? script) where T : Script;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a script from the set using its type.
|
||||
/// </summary>
|
||||
@@ -144,6 +150,20 @@ public class ScriptSet : IScriptSet
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer? Get(StringKey scriptKey) => _scripts.FirstOrDefault(s => s.Script?.Name == scriptKey);
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGet<T>([NotNullWhen(true)] out T? script) where T : Script
|
||||
{
|
||||
var scriptName = ScriptUtils.ResolveName<T>();
|
||||
var container = _scripts.FirstOrDefault(sc => sc.Script?.Name == scriptName);
|
||||
if (container?.Script is not T s)
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
script = s;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public T? Get<T>() where T : Script => Get(ScriptUtils.ResolveName<T>())?.Script as T;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user