using Microsoft.VisualBasic.CompilerServices; using Newtonsoft.Json; using RhythmBase.Adofai.Converters; using RhythmBase.Adofai.Events; using RhythmBase.Adofai.Utils; using RhythmBase.Exceptions; using RhythmBase.Settings; namespace RhythmBase.Adofai.Components { /// /// Adofal level. /// public class ADLevel : ADTileCollection { /// /// Level settings. /// public ADSettings Settings { get; set; } /// /// Level decoration collection. /// public List Decorations { get; set; } /// /// Level file path. /// [JsonIgnore] public string Path => _path; /// /// Level directory path. /// [JsonIgnore] public string Directory => System.IO.Path.GetDirectoryName(_path); /// /// Get all the events of the level. /// public override IEnumerable Events { get { foreach (ADBaseEvent tile in base.Events) yield return tile; foreach (ADBaseEvent tile2 in Decorations) yield return tile2; } } /// /// The calculator that comes with the level. /// [JsonIgnore] public ADBeatCalculator Calculator { get; } public ADLevel() { Settings = new ADSettings(); Decorations = []; Calculator = new ADBeatCalculator(this); } public ADLevel(IEnumerable items) { Settings = new ADSettings(); Decorations = []; Calculator = new ADBeatCalculator(this); foreach (ADTile tile in items) Add(tile); } /// /// The default level within the game. /// public static ADLevel Default => []; /// /// Read from file as level. /// Use default input settings. /// Supports .rdlevel, .rdzip, .zip file extension. /// /// File path. /// An instance of a level that reads from a file. public static ADLevel Read(string filepath) => Read(filepath, new LevelReadOrWriteSettings()); /// /// Read from file as level. /// Supports .rdlevel, .rdzip, .zip file extension. /// /// File path. /// Input settings. /// An instance of a level that reads from a file. public static ADLevel Read(string filepath, LevelReadOrWriteSettings settings) { JsonSerializer LevelSerializer = new(); LevelSerializer.Converters.Add(new ADLevelConverter(filepath, settings)); string extension = System.IO.Path.GetExtension(filepath); if (extension != ".adofai") { throw new RhythmBaseException("File not supported."); } return LevelSerializer.Deserialize(new JsonTextReader(File.OpenText(filepath)))!; } internal string _path; } }