/* * 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.IO; namespace xAPI4Unity.Editor.Parser.IO { /// /// Struct that represents a file or directory /// internal class FileMeta { /// /// Name of the file /// public string Name; /// /// Path of the file /// public string Path; /// /// Content of the file as a string /// public string FileContent { get; set; } /// /// Content of the directory as a list of FileMetas /// public List DirContent { get; set; } /// /// True, if the FileMeta is a directory, false if it's a file /// public bool IsDirectory => DirContent != null; public FileMeta() { } /// /// Creates a FileMeta /// /// Name of the file /// Path of the file /// Content of the file public FileMeta(string name, string path, string content = "") { Name = name; Path = path; FileContent = content; } /// /// Creates a FileMeta /// /// Name of the file /// Path of the file /// List of files in the directory public FileMeta(string name, string path, List files) { Name = name; Path = path; DirContent = files; } public void AppendToDirContent(FileMeta fileMeta) { if (DirContent == null) DirContent = new List(); DirContent.Add(fileMeta); } public void Save(string destination) { if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); Path = destination; if (IsDirectory) { DirContent.ForEach(d => d.Save(destination)); return; } var filePath = System.IO.Path.Combine(destination, Name); using (var fs = new StreamWriter(filePath)) { fs.Write(FileContent); } } /// /// [File: Name={Name}, Path={Path}, Content={Content}] /// public override string ToString() => $"[File: Name=\"{Name}\", Path=\"{Path}\", Content=\"{(IsDirectory ? DirContent.ToString() : FileContent)}\"]"; } } #endif