/* * 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; using System.IO; using Newtonsoft.Json; using UnityEditor; using UnityEngine; using xAPI4Unity.Editor.Types; namespace xAPI4Unity.Editor { /// /// Represents the configuration file used by xAPI4Unity. /// Contains type mappings, references, and ignored contexts. /// [Serializable] public class ConfigFile { /// /// A dictionary mapping custom type names to their fully qualified definitions. /// [JsonProperty("types")] public Dictionary Types = new Dictionary(); /// /// Assembly references that are included in the configuration. /// public string[] references = Array.Empty(); /// /// A list of contexts that should be ignored. /// public string[] ignoredContexts = Array.Empty(); public Dictionary Variables = new Dictionary(); /// /// Reads and deserializes a ConfigFile instance from the specified path. /// /// The path to the configuration file to read. /// A deserialized instance. public static ConfigFile Read(string path) { var content = File.ReadAllText(path); return JsonConvert.DeserializeObject(content); } /// /// Provides a menu item in Unity to create a default 'xapi.config.json' file in the Assets folder. /// Prompts the user with a Yes/No dialog if the file already exists. /// [MenuItem("xAPI4Unity / Create 'xapi.config.json' file", false, 200)] private static void CreateAsset() { var filePath = Path.Combine(Application.dataPath, "xapi.config.json"); // Check if the file already exists if (File.Exists(filePath)) { // Prompt the user with a Yes/No dialog if (!EditorUtility.DisplayDialog("File Exists", "'xapi.config.json' already exists. Do you want to overwrite it?", "Yes", "No")) { return; // Exit if the user selects "No" } } // Create a new default ConfigFile instance var file = new ConfigFile { ignoredContexts = new[] { "360tours", "assistanceSystem", "exerciseGenerator", "gitanalysis", "ide", "lms", "media", "multitouch", "observation", "projectJupyter", "rightsEngine", "studybuddy", "tagformance", "uhfReader", "vrRfidChamber" } }; // Serialize it to JSON format var json = JsonConvert.SerializeObject(file, Formatting.Indented); // Write the JSON file to the Assets directory File.WriteAllText(filePath, json); Debug.Log($"'xapi.config.json' has been created at {filePath}"); } } } #endif