Skip to content
0.2.0-dev Download ZIP
57 lines ·1.9 KB Raw
1using System.Text;
2using EvilEngine.Conversion;
3using EvilEngine.Formats;
4using EvilEngine.Formats.Records;
5using EvilEngine.Mounts;
6
7namespace EvilEngine.Adapters.Overgrowth;
8
9public sealed class OvergrowthSkyRecords : IConverter
10{
11 public static readonly VirtualPath Levels = "Data/Levels";
12
13 public string Id => "overgrowth.skies";
14
15 public int Version => 1;
16
17 public ContentKind Produces => ContentKind.Record;
18
19 public bool Accepts(ConversionContext context)
20 {
21 return context.Id.Name == SkyRecords.Set;
22 }
23
24 public void Convert(ConversionContext context, Stream output)
25 {
26 var skies = new List<SkyRecord>();
27 var named = new HashSet<ContentId>();
28 foreach (var entry in context.List(Levels, true))
29 {
30 if (!string.Equals(entry.Path.Extension, "xml", StringComparison.OrdinalIgnoreCase))
31 continue;
32 if (OvergrowthLevelSky.Read(context.Read(entry.Path)) is null)
33 continue;
34 var level = entry.Path.WithoutExtension.Value[(Levels.Value.Length + 1)..];
35 ContentId id;
36 try
37 {
38 id = ContentId.Create(context.Id.Namespace, level);
39 }
40 catch (ArgumentException)
41 {
42 context.Warn($"{entry.Path} can't be named as a sky record.");
43 continue;
44 }
45
46 if (!named.Add(id))
47 continue;
48 var folder = level.Split('/');
49 skies.Add(new SkyRecord(id,
50 context.Reference(ContentKind.Sky,
51 $"{entry.Path.WithoutExtension.Value}/{OvergrowthLevelSky.Part}"))
52 { Title = folder[^1], Category = folder.Length > 1 ? folder[0] : SkyRecords.MapsCategory });
53 }
54
55 output.Adopt(Encoding.UTF8.GetBytes(RecordText.Write(RecordSet.Empty with { Skies = skies })));
56 }
57}