using PashaBibko.Pacore.Attributes; using System.Collections.Generic; using UnityEngine; using System; namespace Fruitomation.Game.Items { public class ItemInfoRegistry : MonoBehaviour { [Header("References")] [SerializeField] private SerializedItemInfoRegistry SerializedRegistry; private Dictionary Dictionary; private static ItemInfoRegistry Instance; private void Awake() { /* Sets as the global instance */ if (Instance is not null) { Debug.LogError("Multiple instances of ItemInfoRegistry found"); return; } LoadFromRegistry(); Instance = this; } private void OnDestroy() { if (Instance == this) { Instance = null; } } [InspectorCallable("Load Registry")] private void LoadFromRegistry() { Dictionary = new Dictionary(); foreach (ItemInfo info in SerializedRegistry.Registry) { Dictionary.Add(info.Type, info); } #if UNITY_EDITOR ItemType[] types = Enum.GetValues(typeof(ItemType)) as ItemType[]; Debug.Assert(types != null, nameof(types) + " != null"); foreach (ItemType type in types) { bool contained = Dictionary.ContainsKey(type); if (!contained) { Debug.LogWarning($"Type [{type}] is not contained in the registry"); } } #endif // UNITY_EDITOR } public static ItemInfo Get(ItemType type) => Instance.Dictionary[type]; } }