Restructured for new direction.
This commit is contained in:
69
Assets/Scripts/Framework/Registries/RegistryLookupBuilder.cs
Normal file
69
Assets/Scripts/Framework/Registries/RegistryLookupBuilder.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user