using System; using System.Collections.Generic; using System.Linq; using UnityEngine.Localization.Tables; namespace UnityEngine.Localization.Metadata { /// /// Shared Metadata is data that is associated to all tables that use the same , also known as an AssetTableCollection. /// Multiple table entries can be included in the SharedTableCollectionMetadata and The tables that use each table entry are recorded. /// [Serializable] public abstract class SharedTableCollectionMetadata : IMetadata, ISerializationCallbackReceiver { [Serializable] class Item { [SerializeField] long m_KeyId; [SerializeField] List m_TableCodes = new List(); public long KeyId { get => m_KeyId; set => m_KeyId = value; } public List Tables { get => m_TableCodes; set => m_TableCodes = value; } } [SerializeField, HideInInspector] List m_Entries = new List(); /// /// Dictionary that contains all the entries Key Ids that are using this, the /// HashSet includes the country codes of the tables that are using the metadata. /// public Dictionary> EntriesLookup { get; set; } = new Dictionary>(); /// /// Are any table entries currently associated to this Metadata? /// public bool IsEmpty => EntriesLookup.Count == 0; /// /// Is the table entry associated to this Metadata? /// /// /// public bool Contains(long keyId) { return EntriesLookup.ContainsKey(keyId); } /// /// Is the table entry and culture code associated to this Metadata? /// /// /// /// public bool Contains(long keyId, string code) { return EntriesLookup.TryGetValue(keyId, out var codes) && codes.Contains(code); } /// /// Add the table entry for a specific table to the shared Metadata. /// /// The Id of the table entry. /// The table culture code. public void AddEntry(long keyId, string code) { EntriesLookup.TryGetValue(keyId, out var item); if (item == null) { item = new HashSet(); EntriesLookup[keyId] = item; } item.Add(code); } /// /// Remove the table entry for a specific table from the shared Metadata. /// /// /// public void RemoveEntry(long keyId, string code) { if (EntriesLookup.TryGetValue(keyId, out var item)) { item.Remove(code); if (item.Count == 0) EntriesLookup.Remove(keyId); } } /// /// Converts the internal dictionary into a serializable list. /// public virtual void OnBeforeSerialize() { m_Entries.Clear(); foreach (var entry in EntriesLookup) { m_Entries.Add(new Item() { KeyId = entry.Key, Tables = entry.Value.ToList() }); } } /// /// Converts the serializable list into a dictionary. /// public virtual void OnAfterDeserialize() { EntriesLookup = new Dictionary>(); foreach (var entry in m_Entries) { EntriesLookup[entry.KeyId] = new HashSet(entry.Tables); } } } }