Program: Add crude infinite loop detection to GetRandomElementFromPool

This commit is contained in:
OatmealDome
2024-08-11 01:34:54 -04:00
parent 052cb0b45b
commit 7eebc76421

View File

@@ -1,4 +1,4 @@
using System.CommandLine;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Text.Json;
using OatmealDome.BinaryData;
@@ -323,10 +323,18 @@ void Run(InvocationContext context)
T GetRandomElementFromPool<T>(List<T> pool, Func<T, bool> validityChecker)
{
T element;
int tries = 0;
do
{
element = pool[random.Next(0, pool.Count)];
tries++;
if (tries > 10000)
{
throw new Exception("Possible infinite loop detected in GetRandomElementFromPool");
}
} while (!validityChecker(element));
pool.Remove(element);