Files

70 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace BriarQueen.Framework.Registries
{
internal static class RegistryLookupBuilder
{
public static void AddEntries<TKey, TEntry, TValue>(
Dictionary<TKey, TValue> lookup,
IEnumerable<TEntry> entries,
UnityEngine.Object context,
string registryName,
string category,
string keyLabel,
Func<TEntry, TKey> keySelector,
Func<TEntry, TValue> valueSelector,
Func<TEntry, bool> isKeyValid = null,
Func<TEntry, bool> isEntryValid = null,
string invalidEntryReason = null)
where TEntry : UnityEngine.Object
{
if (lookup == null)
throw new ArgumentNullException(nameof(lookup));
if (entries == null)
return;
foreach (var entry in entries)
{
if (!entry)
continue;
if (isKeyValid != null && !isKeyValid(entry))
{
Debug.LogWarning(
$"[{registryName}] Skipping {category} entry '{entry.name}' because {keyLabel} is invalid.",
context);
continue;
}
if (isEntryValid != null && !isEntryValid(entry))
{
Debug.LogWarning(
$"[{registryName}] Skipping {category} entry '{entry.name}' because {invalidEntryReason}.",
context);
continue;
}
var key = keySelector(entry);
if (lookup.ContainsKey(key))
{
Debug.LogError(
$"[{registryName}] Duplicate {keyLabel} detected: '{key}' from entry '{entry.name}'.",
context);
continue;
}
lookup.Add(key, valueSelector(entry));
}
}
public static bool HasNonEmptyKey(string key)
{
return !string.IsNullOrWhiteSpace(key);
}
}
}