/* * 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.Collections.Generic; using System.Linq; using System.Text; namespace xAPI4Unity.Editor.Parser.IO { /// /// Class that represents a context of xAPI definitions /// internal class Context { /// /// Name of the context /// public string Name => _meta.Name; private readonly FileMeta _meta; /// /// Activity files in the context /// public List Activities { get; } = new List(); /// /// Extension files in the context /// public IDictionary> Extensions { get; } = new Dictionary>(); /// /// Verb files in the context /// public List Verbs { get; } = new List(); public Context(FileMeta meta) { _meta = meta; } /// /// Adds an activity to the context /// /// Activity /// True, if it was successfully added, false, if not public bool AddActivity(FileMeta activity) { var added = false; if (activity != null && (added = !Activities.Contains(activity))) { Activities.Add(activity); } return added; } /// /// Adds activities to the context /// /// A list of activities /// Number of successfully added activities public int AddActivities(IEnumerable activities) => activities?.Count(AddActivity) ?? 0; /// /// Adds a verb to the context /// /// Verb /// True, if it was successfully added, false, if not public bool AddVerb(FileMeta verb) { var added = false; if (verb != null && (added = !Verbs.Contains(verb))) { Verbs.Add(verb); } return added; } /// /// Adds verbs to the context /// /// List of verbs /// Number of successfully added verbs public int AddVerbs(List verbs) => verbs?.Count(AddVerb) ?? 0; /// /// Adds an extension to the context /// /// Extension /// True, if it was successfully added, false, if not public bool AddExtension(string type, FileMeta extension) { var added = false; if (!Extensions.ContainsKey(type)) { Extensions.Add(type, new List()); } var extensionsOfType = Extensions[type]; if (extension != null && (added = !extensionsOfType.Contains(extension))) { Extensions[type].Add(extension); } return added; } /// /// Adds extensions to the context /// /// List of extension file objects /// Number of successfully added extensions public int AddExtensions(IDictionary> extensions) { var added = 0; if (extensions == null) return added; foreach (var extType in extensions) { var type = extType.Key; if (!Extensions.ContainsKey(type)) { Extensions.Add(type, new List()); } foreach (var extension in extensions[type].Where(extension => !Extensions[type].Contains(extension))) { Extensions[type].Add(extension); ++added; } } return added; } public override string ToString() { var extensions = new StringBuilder(); foreach (var ext in Extensions) { extensions.Append($"key={ext.Key},value=[{StringExt.ToString(ext.Value.ToArray())}]"); } return $"[Context name={Name}, Activities={StringExt.ToString(Activities.ToArray())}, Verbs={StringExt.ToString(Verbs.ToArray())}, Extensions=[{extensions}]]"; } } } #endif