using System.Text; using EvilEngine.Conversion; using EvilEngine.Formats; using EvilEngine.Formats.Records; using EvilEngine.Mounts; namespace EvilEngine.Adapters.Overgrowth; public sealed class OvergrowthSkyRecords : IConverter { public static readonly VirtualPath Levels = "Data/Levels"; public string Id => "overgrowth.skies"; public int Version => 1; public ContentKind Produces => ContentKind.Record; public bool Accepts(ConversionContext context) { return context.Id.Name == SkyRecords.Set; } public void Convert(ConversionContext context, Stream output) { var skies = new List(); var named = new HashSet(); foreach (var entry in context.List(Levels, true)) { if (!string.Equals(entry.Path.Extension, "xml", StringComparison.OrdinalIgnoreCase)) continue; if (OvergrowthLevelSky.Read(context.Read(entry.Path)) is null) continue; var level = entry.Path.WithoutExtension.Value[(Levels.Value.Length + 1)..]; ContentId id; try { id = ContentId.Create(context.Id.Namespace, level); } catch (ArgumentException) { context.Warn($"{entry.Path} can't be named as a sky record."); continue; } if (!named.Add(id)) continue; var folder = level.Split('/'); skies.Add(new SkyRecord(id, context.Reference(ContentKind.Sky, $"{entry.Path.WithoutExtension.Value}/{OvergrowthLevelSky.Part}")) { Title = folder[^1], Category = folder.Length > 1 ? folder[0] : SkyRecords.MapsCategory }); } output.Adopt(Encoding.UTF8.GetBytes(RecordText.Write(RecordSet.Empty with { Skies = skies }))); } }