0.2.0-dev Download ZIP
194 lines ·7.9 KB Raw
| 1 | using System.Text; |
| 2 | using EvilEngine.Formats; |
| 3 | using EvilEngine.Formats.Records; |
| 4 | using EvilEngine.Mounts; |
| 5 | |
| 6 | namespace EvilEngine.Conversion; |
| 7 | |
| 8 | public sealed record SoundtrackFolder(VirtualPath Folder, bool Tagged = false); |
| 9 | |
| 10 | public sealed record SoundtrackGame(string Title, string? Store = null); |
| 11 | |
| 12 | public sealed class SoundtrackRecords( |
| 13 | IReadOnlyList<SoundtrackFolder> folders, |
| 14 | Func<string, SoundtrackGame?> game, |
| 15 | IReadOnlyList<MusicList>? lists = null) : IConverter |
| 16 | { |
| 17 | private static readonly string[] CoverNames = ["cover", "folder", "front", "album", "albumart", "albumartlarge"]; |
| 18 | private static readonly string[] ImageExtensions = ["png", "jpg", "jpeg"]; |
| 19 | |
| 20 | private readonly Dictionary<string, MusicList> listed = |
| 21 | (lists ?? []).ToDictionary(list => list.Game, StringComparer.Ordinal); |
| 22 | |
| 23 | public string Id => "soundtracks"; |
| 24 | |
| 25 | public int Version => 2; |
| 26 | |
| 27 | public ContentKind Produces => ContentKind.Record; |
| 28 | |
| 29 | public bool Accepts(ConversionContext context) |
| 30 | { |
| 31 | return context.Id.Name == AlbumRecords.Set; |
| 32 | } |
| 33 | |
| 34 | public void Convert(ConversionContext context, Stream output) |
| 35 | { |
| 36 | var albums = |
| 37 | new Dictionary<(string Game, string Title), List<(string Relative, VirtualPath Path, bool Tagged)>>(); |
| 38 | var stores = new Dictionary<string, string?>(StringComparer.Ordinal); |
| 39 | var images = new List<VirtualPath>(); |
| 40 | var listedFiles = new Dictionary<string, Dictionary<string, VirtualPath>>(StringComparer.Ordinal); |
| 41 | foreach (var folder in folders) |
| 42 | foreach (var entry in context.List(folder.Folder, true)) |
| 43 | { |
| 44 | if (ImageExtensions.Contains(entry.Path.Extension, StringComparer.OrdinalIgnoreCase)) |
| 45 | images.Add(entry.Path); |
| 46 | if (!MusicFileConverter.IsMusic(entry.Path) || |
| 47 | !entry.Path.TryRelativeTo(folder.Folder, CaseRule.Insensitive, out var relative) || |
| 48 | context.Copies(entry.Path).FirstOrDefault() is not { } layer || game(layer) is not { } owner) |
| 49 | continue; |
| 50 | var title = owner.Title; |
| 51 | stores.TryAdd(title, owner.Store); |
| 52 | if (listed.ContainsKey(title)) |
| 53 | { |
| 54 | if (!listedFiles.TryGetValue(title, out var found)) |
| 55 | listedFiles[title] = found = new Dictionary<string, VirtualPath>(StringComparer.OrdinalIgnoreCase); |
| 56 | found.TryAdd(entry.Path.Value, entry.Path); |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | var slash = relative.Value.IndexOf(VirtualPath.Separator); |
| 61 | var sub = slash < 0 ? null : relative.Value[..slash]; |
| 62 | var album = (folder.Tagged, sub) switch |
| 63 | { |
| 64 | (_, null) => title, |
| 65 | (true, _) => sub, |
| 66 | _ => $"{title} ({sub})" |
| 67 | }; |
| 68 | if (!albums.TryGetValue((title, album), out var tracks)) |
| 69 | albums[(title, album)] = tracks = []; |
| 70 | tracks.Add((relative.Value, entry.Path, folder.Tagged)); |
| 71 | } |
| 72 | |
| 73 | var records = albums.Select(pair => new AlbumRecord( |
| 74 | ContentId.Create(context.Id.Namespace, $"{Slug(pair.Key.Game)}/{Slug(pair.Key.Title)}"), pair.Key.Title, |
| 75 | pair.Key.Game) |
| 76 | { |
| 77 | Cover = Cover(context, images, pair.Value[0].Path.Parent), |
| 78 | Store = stores[pair.Key.Game], |
| 79 | Tracks = pair.Value.OrderBy(track => track.Relative, NaturalOrder.Instance) |
| 80 | .Select(track => new AlbumTrack(ContentId.Create(context.Id.Namespace, track.Path.Value)) |
| 81 | { |
| 82 | Title = track.Tagged ? null : Path.GetFileNameWithoutExtension(track.Path.Name) |
| 83 | }).ToArray() |
| 84 | }).Concat(listedFiles.SelectMany(pair => |
| 85 | Listed(context, images, listed[pair.Key], pair.Value, stores[pair.Key]))) |
| 86 | .ToArray(); |
| 87 | output.Write(Encoding.UTF8.GetBytes(RecordText.Write(RecordSet.Empty with { Albums = records }))); |
| 88 | } |
| 89 | |
| 90 | private static IEnumerable<AlbumRecord> Listed(ConversionContext context, List<VirtualPath> images, MusicList list, |
| 91 | Dictionary<string, VirtualPath> found, string? store) |
| 92 | { |
| 93 | foreach (var album in list.Albums) |
| 94 | { |
| 95 | var tracks = album.Tracks |
| 96 | .Select((track, at) => (Track: track, Number: at + 1, |
| 97 | Path: found.TryGetValue(track.File.Value, out var path) ? path : (VirtualPath?)null)) |
| 98 | .Where(track => track.Path is not null) |
| 99 | .ToArray(); |
| 100 | if (tracks.Length == 0) |
| 101 | continue; |
| 102 | yield return new AlbumRecord( |
| 103 | ContentId.Create(context.Id.Namespace, $"{Slug(list.Game)}/{Slug(album.Title)}"), album.Title, |
| 104 | list.Game) |
| 105 | { |
| 106 | Cover = Cover(context, images, tracks[0].Path!.Value.Parent), |
| 107 | Store = store, |
| 108 | Tracks = tracks.Select(track => |
| 109 | new AlbumTrack(ContentId.Create(context.Id.Namespace, track.Path!.Value.Value)) |
| 110 | { |
| 111 | Title = track.Track.Title, |
| 112 | Number = track.Number |
| 113 | }).ToArray() |
| 114 | }; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private static ContentId? Cover(ConversionContext context, List<VirtualPath> images, VirtualPath folder) |
| 119 | { |
| 120 | VirtualPath[] here = |
| 121 | [ |
| 122 | .. images.Where(image => image.Parent.Equals(folder, CaseRule.Insensitive)) |
| 123 | .OrderBy(image => image.Value, StringComparer.OrdinalIgnoreCase) |
| 124 | ]; |
| 125 | VirtualPath? cover = null; |
| 126 | foreach (var name in CoverNames) |
| 127 | if (cover is null) |
| 128 | foreach (var image in here) |
| 129 | if (image.WithoutExtension.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) |
| 130 | { |
| 131 | cover = image; |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | if (cover is null && here.Length == 1) |
| 136 | cover = here[0]; |
| 137 | return cover is { } found |
| 138 | ? context.Reference(ContentKind.Texture, |
| 139 | ContentId.Create(context.Id.Namespace, found.WithoutExtension.Value)) |
| 140 | : null; |
| 141 | } |
| 142 | |
| 143 | public static string Slug(string text) |
| 144 | { |
| 145 | var slug = new StringBuilder(); |
| 146 | foreach (var c in text.ToLowerInvariant()) |
| 147 | if (char.IsAsciiLetterOrDigit(c)) |
| 148 | slug.Append(c); |
| 149 | else if (slug.Length > 0 && slug[^1] != '-') |
| 150 | slug.Append('-'); |
| 151 | return slug.ToString().TrimEnd('-'); |
| 152 | } |
| 153 | |
| 154 | private sealed class NaturalOrder : IComparer<string> |
| 155 | { |
| 156 | public static readonly NaturalOrder Instance = new(); |
| 157 | |
| 158 | public int Compare(string? a, string? b) |
| 159 | { |
| 160 | if (a is null || b is null) |
| 161 | return string.CompareOrdinal(a, b); |
| 162 | int i = 0, j = 0; |
| 163 | while (i < a.Length && j < b.Length) |
| 164 | { |
| 165 | if (char.IsAsciiDigit(a[i]) && char.IsAsciiDigit(b[j])) |
| 166 | { |
| 167 | var startA = i; |
| 168 | var startB = j; |
| 169 | while (i < a.Length && char.IsAsciiDigit(a[i])) |
| 170 | i++; |
| 171 | while (j < b.Length && char.IsAsciiDigit(b[j])) |
| 172 | j++; |
| 173 | var numberA = a[startA..i].TrimStart('0'); |
| 174 | var numberB = b[startB..j].TrimStart('0'); |
| 175 | var order = numberA.Length != numberB.Length |
| 176 | ? numberA.Length.CompareTo(numberB.Length) |
| 177 | : string.CompareOrdinal(numberA, numberB); |
| 178 | if (order != 0) |
| 179 | return order; |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | var letters = char.ToLowerInvariant(a[i]).CompareTo(char.ToLowerInvariant(b[j])); |
| 184 | if (letters != 0) |
| 185 | return letters; |
| 186 | i++; |
| 187 | j++; |
| 188 | } |
| 189 | |
| 190 | var rest = (a.Length - i).CompareTo(b.Length - j); |
| 191 | return rest != 0 ? rest : string.CompareOrdinal(a, b); |
| 192 | } |
| 193 | } |
| 194 | } |