/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of xAPI4Unity. */ #if UNITY_EDITOR using System; using System.Collections.Generic; namespace xAPI4Unity.Editor { /// /// Represents a definition file containing mappings for names and descriptions. /// Facilitates conversion between name/description dictionaries and structured definitions. /// [Serializable] internal struct DefinitionFile { /// /// Dictionary mapping unique keys to names. /// public Dictionary name; /// /// Dictionary mapping unique keys to descriptions. /// public Dictionary description; /// /// Converts the name and description dictionaries into a dictionary of objects. /// /// A dictionary where each key corresponds to a instance. public Dictionary ToDefinitionList() { var dict = new Dictionary(); #if UNITY_2021_1_OR_NEWER // Use tuple deconstruction for newer Unity versions (C# 7.0 and later) foreach (var (key, value) in name) { dict.Add(key, new Definition(value, description[key])); } #else // Fallback for older Unity versions without tuple deconstruction foreach (var kvp in name) { dict.Add(kvp.Key, new Definition(kvp.Value, description[kvp.Key])); } #endif return dict; } /// /// Converts a dictionary of objects back into a . /// /// The dictionary of definitions to convert. /// A containing the converted name and description dictionaries. public static DefinitionFile FromDefinitionList(Dictionary definitionList) { var defFile = new DefinitionFile { name = new Dictionary(), description = new Dictionary() }; #if UNITY_2021_1_OR_NEWER // Use tuple deconstruction for newer Unity versions (C# 7.0 and later) foreach (var (key, value) in definitionList) { defFile.name.Add(key, value.name); defFile.description.Add(key, value.description); } #else // Fallback for older Unity versions without tuple deconstruction foreach (var kvp in definitionList) { defFile.name.Add(kvp.Key, kvp.Value.name); defFile.description.Add(kvp.Key, kvp.Value.description); } #endif return defFile; } /// /// Returns a string representation of the definition file, showing the count of names and descriptions. /// /// A string indicating the number of names and descriptions. public override string ToString() { return $"{name.Count} names, {description.Count} descriptions"; } } /// /// Represents a single definition with a name and description. /// internal struct Definition { /// /// Name of the definition. /// public string name; /// /// Description of the definition. /// public string description; /// /// Constructs a new instance of with the specified name and description. /// /// The name of the definition. /// The description of the definition. public Definition(string name, string description) { this.name = name; this.description = description; } } } #endif