using System;
using System.Collections.Generic;
using UnityEngine.Localization.Metadata;
using UnityEngine.Serialization;
namespace UnityEngine.Localization.Tables
{
///
/// Abstract base class for all tables.
/// Contains common functionality.
///
public abstract class LocalizationTable : ScriptableObject, IMetadataCollection, IComparable
{
[SerializeField]
LocaleIdentifier m_LocaleId;
[FormerlySerializedAs("m_KeyDatabase")]
[SerializeField, HideInInspector]
SharedTableData m_SharedData;
[SerializeField]
MetadataCollection m_Metadata = new MetadataCollection();
[SerializeField]
List m_TableData = new List();
///
/// The locale this asset table supports.
///
public LocaleIdentifier LocaleIdentifier
{
get => m_LocaleId;
set => m_LocaleId = value;
}
///
/// The name of this asset table collection.
///
public string TableCollectionName
{
get
{
VerifySharedTableDataIsNotNull();
return SharedData.TableCollectionName;
}
}
///
/// Data shared across all tables.
///
public SharedTableData SharedData
{
get => m_SharedData;
set => m_SharedData = value;
}
///
/// All entries stored within the table.
///
internal List TableData
{
get => m_TableData;
}
///
/// Table Metadata.
///
public IList MetadataEntries
{
get => m_Metadata.MetadataEntries;
}
///
/// Returns the first Metadata item from of type TObject.
///
///
///
public TObject GetMetadata() where TObject : IMetadata
{
return m_Metadata.GetMetadata();
}
///
/// Populates the list with all Metadata from that is of type TObject.
///
///
///
public void GetMetadatas(IList foundItems) where TObject : IMetadata
{
m_Metadata.GetMetadatas(foundItems);
}
///
/// Returns all Metadata from that is of type TObject.
///
///
///
public IList GetMetadatas() where TObject : IMetadata
{
return m_Metadata.GetMetadatas();
}
///
/// Add an entry to .
///
///
public void AddMetadata(IMetadata md)
{
m_Metadata.AddMetadata(md);
}
///
/// Remove an entry from .
///
///
///
public bool RemoveMetadata(IMetadata md)
{
return m_Metadata.RemoveMetadata(md);
}
///
/// Checks if the Metadata is contained within .
///
///
///
public bool Contains(IMetadata md)
{
return m_Metadata.Contains(md);
}
///
/// Create an empty entry in the table at the specified entry.
///
///
public abstract void CreateEmpty(TableEntryReference entryReference);
///
/// Returns the key with the matching name from the , if one exists.
///
/// The key to search for.
/// Should a new key be added if one can not be found?
/// Thrown if the is null.
/// The found key or null if one could not be found.
protected long FindKeyId(string key, bool addKey)
{
VerifySharedTableDataIsNotNull();
return SharedData.GetId(key, addKey);
}
void VerifySharedTableDataIsNotNull()
{
if (SharedData == null)
throw new NullReferenceException($"The Table \"{name}\" does not have a {nameof(SharedTableData)}.");
}
///
/// Returns a string representation of the table in the format "{TableCollectionName}({LocaleIdentifier})".
///
///
public override string ToString() => $"{TableCollectionName}({LocaleIdentifier})";
///
/// Compare to another LocalizationTable.
/// Performs a comparison against the property.
///
///
///
public int CompareTo(LocalizationTable other)
{
if (other == null)
return 1;
return LocaleIdentifier.CompareTo(other.LocaleIdentifier);
}
}
}