using System.Collections.Generic; using BriarQueen.Framework.Managers.Player.Data; using UnityEngine; namespace BriarQueen.Framework.Registries { [CreateAssetMenu(menuName = "Briar Queen/Registries/Codex Registry", fileName = "Codex Registry")] public class CodexRegistry : ScriptableObject { [Header("Codex Entries")] [SerializeField] private List _bookEntries = new(); [SerializeField] private List _puzzleClues = new(); [SerializeField] private List _photoEntries = new(); private Dictionary _entryLookup; public IReadOnlyList BookEntries => _bookEntries; public IReadOnlyList PuzzleClues => _puzzleClues; public IReadOnlyList PhotoEntries => _photoEntries; public void EnsureInitialized() { if (_entryLookup != null) return; RebuildLookup(); } public void RebuildLookup() { _entryLookup = new Dictionary(); AddEntries(_bookEntries, "Book Entries"); AddEntries(_puzzleClues, "Puzzle Clues"); AddEntries(_photoEntries, "Photo Entries"); } private void AddEntries(List entries, string category) { if (entries == null) return; foreach (var entry in entries) { if (entry == null) continue; if (string.IsNullOrWhiteSpace(entry.UniqueID)) { Debug.LogWarning( $"[CodexRegistry] Skipping {category} entry '{entry.name}' because UniqueID is empty.", this); continue; } if (_entryLookup.ContainsKey(entry.UniqueID)) { Debug.LogError( $"[CodexRegistry] Duplicate UniqueID detected: '{entry.UniqueID}' from entry '{entry.name}'.", this); continue; } _entryLookup.Add(entry.UniqueID, entry); } } public bool TryGetEntry(string entryID, out CodexEntrySo entry) { EnsureInitialized(); if (string.IsNullOrWhiteSpace(entryID)) { entry = null; return false; } return _entryLookup.TryGetValue(entryID, out entry); } public CodexEntrySo FindEntryByID(string entryID) { EnsureInitialized(); return string.IsNullOrWhiteSpace(entryID) ? null : _entryLookup.GetValueOrDefault(entryID); } public IEnumerable GetAllEntries() { EnsureInitialized(); return _entryLookup.Values; } public IEnumerable GetAllKeys() { EnsureInitialized(); return _entryLookup.Keys; } public IEnumerable GetBookEntries() { return _bookEntries; } public IEnumerable GetPuzzleClues() { return _puzzleClues; } public IEnumerable GetPhotoEntries() { return _photoEntries; } #if UNITY_EDITOR public void SetDiscoveredEntries( List bookEntries, List puzzleClues, List photoEntries) { _bookEntries = bookEntries ?? new List(); _puzzleClues = puzzleClues ?? new List(); _photoEntries = photoEntries ?? new List(); RebuildLookup(); } #endif } }