From 49d09bb44fb63a59b3f4b3f58cf2e5789fd2909c Mon Sep 17 00:00:00 2001
From: XtraCube <72575280+XtraCube@users.noreply.github.com>
Date: Fri, 28 Feb 2025 06:24:26 -0500
Subject: [PATCH] use NotNullWhen attribute
---
MiraAPI/GameEnd/GameOverManager.cs | 3 +-
MiraAPI/Modifiers/ModifierComponent.cs | 9 +++--
MiraAPI/Modifiers/ModifierExtensions.cs | 52 +++++++++++++++++++++++++
MiraAPI/Networking/CustomGameOverRpc.cs | 2 +-
4 files changed, 60 insertions(+), 6 deletions(-)
diff --git a/MiraAPI/GameEnd/GameOverManager.cs b/MiraAPI/GameEnd/GameOverManager.cs
index c25aa37..6144d8a 100644
--- a/MiraAPI/GameEnd/GameOverManager.cs
+++ b/MiraAPI/GameEnd/GameOverManager.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using Reactor.Utilities;
namespace MiraAPI.GameEnd;
@@ -51,7 +52,7 @@ public static class GameOverManager
/// ID of the custom game over.
/// The created instance of the custom game over.
/// An instance of the custom game over.
- public static bool TryGetGameOver(int id, out CustomGameOver? customGameOver)
+ public static bool TryGetGameOver(int id, [NotNullWhen(true)] out CustomGameOver? customGameOver)
{
if (GameOverTypes.TryGetValue(id, out var gameOverType))
{
diff --git a/MiraAPI/Modifiers/ModifierComponent.cs b/MiraAPI/Modifiers/ModifierComponent.cs
index 1930c2a..8c7f6f2 100644
--- a/MiraAPI/Modifiers/ModifierComponent.cs
+++ b/MiraAPI/Modifiers/ModifierComponent.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
+using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
@@ -168,7 +169,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
/// The Type of the Modifier.
/// True if the modifier was found, false otherwise.
[HideFromIl2Cpp]
- public bool TryGetModifier(out T? modifier, Func? predicate = null) where T : BaseModifier
+ public bool TryGetModifier([NotNullWhen(true)] out T? modifier, Func? predicate = null) where T : BaseModifier
{
modifier = GetModifier(predicate);
return modifier != null;
@@ -182,7 +183,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
/// The predicate to check the modifier by.
/// True if the modifier was found, false otherwise.
[HideFromIl2Cpp]
- public bool TryGetModifier(Type type, out BaseModifier? modifier, Func? predicate = null)
+ public bool TryGetModifier(Type type, [NotNullWhen(true)] out BaseModifier? modifier, Func? predicate = null)
{
modifier = GetModifier(type, predicate);
return modifier != null;
@@ -196,7 +197,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
/// The predicate to check the modifier by.
/// True if the modifier was found, false otherwise.
[HideFromIl2Cpp]
- public bool TryGetModifier(uint id, out BaseModifier? modifier, Func? predicate = null)
+ public bool TryGetModifier(uint id, [NotNullWhen(true)] out BaseModifier? modifier, Func? predicate = null)
{
modifier = GetModifier(id, predicate);
return modifier != null;
@@ -209,7 +210,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
/// The modifier or null.
/// True if the modifier was found, false otherwise.
[HideFromIl2Cpp]
- public bool TryGetModifier(Guid modifierGuid, out BaseModifier? modifier)
+ public bool TryGetModifier(Guid modifierGuid, [NotNullWhen(true)] out BaseModifier? modifier)
{
modifier = GetModifier(modifierGuid);
return modifier != null;
diff --git a/MiraAPI/Modifiers/ModifierExtensions.cs b/MiraAPI/Modifiers/ModifierExtensions.cs
index 5d05d69..06d9f06 100644
--- a/MiraAPI/Modifiers/ModifierExtensions.cs
+++ b/MiraAPI/Modifiers/ModifierExtensions.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using MiraAPI.Networking;
using MiraAPI.Networking.Modifiers;
using Reactor.Networking.Attributes;
@@ -190,6 +191,57 @@ public static class ModifierExtensions
return player.GetModifierComponent().HasModifier(uniqueId);
}
+ ///
+ /// Tries to get a modifier by its type.
+ ///
+ /// The PlayerControl instance.
+ /// The modifier or null.
+ /// The predicate to check the modifier by.
+ /// The Type of the Modifier.
+ /// True if the modifier was found, false otherwise.
+ public static bool TryGetModifier(this PlayerControl player, [NotNullWhen(true)] out T? modifier, Func? predicate = null) where T : BaseModifier
+ {
+ return player.GetModifierComponent().TryGetModifier(out modifier, predicate);
+ }
+
+ ///
+ /// Tries to get a modifier by its type.
+ ///
+ /// The PlayerControl instance.
+ /// The modifier type.
+ /// The modifier or null.
+ /// The predicate to check the modifier by.
+ /// True if the modifier was found, false otherwise.
+ public static bool TryGetModifier(this PlayerControl player, Type type, [NotNullWhen(true)] out BaseModifier? modifier, Func? predicate = null)
+ {
+ return player.GetModifierComponent().TryGetModifier(type, out modifier, predicate);
+ }
+
+ ///
+ /// Tries to get a modifier by its type ID.
+ ///
+ /// The PlayerControl instance.
+ /// The modifier type ID.
+ /// The modifier or null.
+ /// The predicate to check the modifier by.
+ /// True if the modifier was found, false otherwise.
+ public static bool TryGetModifier(this PlayerControl player, uint id, [NotNullWhen(true)] out BaseModifier? modifier, Func? predicate = null)
+ {
+ return player.GetModifierComponent().TryGetModifier(id, out modifier, predicate);
+ }
+
+ ///
+ /// Tries to get a modifier by its unique ID.
+ ///
+ /// The PlayerControl instance.
+ /// The modifier unique ID.
+ /// The modifier or null.
+ /// True if the modifier was found, false otherwise.
+ public static bool TryGetModifier(this PlayerControl player, Guid modifierGuid, [NotNullWhen(true)] out BaseModifier? modifier)
+ {
+ return player.GetModifierComponent().TryGetModifier(modifierGuid, out modifier);
+ }
+
///
/// Gets a specific modifier from the player.
///
diff --git a/MiraAPI/Networking/CustomGameOverRpc.cs b/MiraAPI/Networking/CustomGameOverRpc.cs
index c269483..978b992 100644
--- a/MiraAPI/Networking/CustomGameOverRpc.cs
+++ b/MiraAPI/Networking/CustomGameOverRpc.cs
@@ -48,7 +48,7 @@ public class CustomGameOverRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<
{
if (GameOverManager.TryGetGameOver(data.Reason, out var gameOver))
{
- if (!gameOver!.VerifyCondition(innerNetObject))
+ if (!gameOver.VerifyCondition(innerNetObject))
{
Logger.Info($"Game over condition not met for {gameOver.GetType().Name}");
return;