/* Yarn Spinner is licensed to you under the terms found in the file LICENSE.md. */ using System; using System.Collections.Generic; using UnityEngine; #nullable enable namespace Yarn.Unity { [Serializable] public class LineMetadata { [Serializable] class StringDictionary : SerializableDictionary { } [SerializeField] private StringDictionary _lineMetadata = new StringDictionary(); internal LineMetadata(IEnumerable lineMetadataTableEntries) { AddMetadata(lineMetadataTableEntries); } /// /// Adds any metadata if they are defined for each line. The metadata is /// internally stored as a single string with each piece of metadata /// separated by a single whitespace. /// /// IEnumerable with metadata /// entries. internal void AddMetadata(IEnumerable lineMetadataTableEntries) { foreach (var entry in lineMetadataTableEntries) { if (entry.Metadata.Length == 0) { continue; } _lineMetadata.Add(entry.ID, String.Join(" ", entry.Metadata)); } } public LineMetadata() { } public void AddMetadata(string lineID, IEnumerable metadata) { _lineMetadata.Add(lineID, string.Join(" ", metadata)); } /// /// Gets the line IDs that contain metadata. /// /// The line IDs. public IEnumerable GetLineIDs() { // The object returned doesn't allow modifications and is kept in // sync with `_lineMetadata`. return _lineMetadata.Keys; } /// /// Returns metadata for a given line ID, if any is defined. /// /// The line ID. /// An array of each piece of metadata if defined, otherwise /// returns null. public string[]? GetMetadata(string lineID) { if (_lineMetadata.TryGetValue(lineID, out var result)) { return result.Split(' '); } return null; } public string? GetShadowLineSource(string lineID) { if (_lineMetadata.TryGetValue(lineID, out var metadataString) == false) { // The line has no metadata, so it is not a shadow line. return null; } var metadata = metadataString.Split(' '); foreach (var metadataEntry in metadata) { if (metadataEntry.StartsWith("shadow:")) { // This is a shadow line. Return the line ID that it's // shadowing. return "line:" + metadataEntry.Substring("shadow:".Length); } } // The line had metadata, but it wasn't a shadow line. return null; } } }