0.2.0-dev Download ZIP
239 lines ·9.6 KB Raw
| 1 | using EvilEngine.Conversion; |
| 2 | using EvilEngine.Mounts; |
| 3 | |
| 4 | namespace EvilEngine.Host; |
| 5 | |
| 6 | public enum PointerStatus |
| 7 | { |
| 8 | Mounted, |
| 9 | Disabled, |
| 10 | Offline, |
| 11 | Unrecognised, |
| 12 | Changed, |
| 13 | Duplicate, |
| 14 | Failed, |
| 15 | Hidden |
| 16 | } |
| 17 | |
| 18 | public sealed record PointerReport(StoredPointer Pointer, PointerStatus Status) |
| 19 | { |
| 20 | public IReadOnlyList<ProbeResult> Probes { get; init; } = []; |
| 21 | |
| 22 | public IReadOnlyList<Mount> Mounts { get; init; } = []; |
| 23 | |
| 24 | public string? Detail { get; init; } |
| 25 | } |
| 26 | |
| 27 | public sealed record InstallOffer(FoundInstall Install, IReadOnlyList<MountId> Ids); |
| 28 | |
| 29 | public sealed class EngineHost |
| 30 | { |
| 31 | private readonly Dictionary<string, (MountId Id, string Path)> _mountedFrom = new(StringComparer.Ordinal); |
| 32 | |
| 33 | public EngineHost(IConversionCache? cache = null, DiskConversionCache? disk = null) |
| 34 | { |
| 35 | Mounts = new MountRegistry(); |
| 36 | Conversion = new ConversionPipeline(cache ?? new MemoryConversionCache(), disk); |
| 37 | Adapters = new AdapterHost(Mounts, Conversion); |
| 38 | Plugins = new PluginHost(Mounts, Conversion); |
| 39 | } |
| 40 | |
| 41 | public MountRegistry Mounts { get; } |
| 42 | |
| 43 | public ConversionPipeline Conversion { get; } |
| 44 | |
| 45 | public AdapterHost Adapters { get; } |
| 46 | |
| 47 | public PluginHost Plugins { get; } |
| 48 | |
| 49 | public FeatureFlags Features { get; set; } = FeatureFlags.All; |
| 50 | |
| 51 | public event Action<StoredPointer, IReadOnlyList<ProbeResult>>? PointerProbed; |
| 52 | |
| 53 | public event Action<PointerReport>? PointerReported; |
| 54 | |
| 55 | public IReadOnlyList<PointerReport> OpenPointers(IReadOnlyList<StoredPointer> pointers) |
| 56 | { |
| 57 | var reports = new PointerReport?[pointers.Count]; |
| 58 | |
| 59 | void Settle(int index, PointerReport report) |
| 60 | { |
| 61 | reports[index] = report; |
| 62 | PointerReported?.Invoke(report); |
| 63 | } |
| 64 | |
| 65 | var pending = new List<(int Index, IReadOnlyList<ProbeResult> Probes)>(); |
| 66 | for (var i = 0; i < pointers.Count; i++) |
| 67 | { |
| 68 | var stored = pointers[i]; |
| 69 | var pointer = stored.Pointer; |
| 70 | if (pointer.LastProbed.Count > 0 && pointer.LastProbed.All(id => !Features.Game(id.Game))) |
| 71 | { |
| 72 | Settle(i, new PointerReport(stored, PointerStatus.Hidden) |
| 73 | { |
| 74 | Detail = |
| 75 | $"{string.Join(", ", pointer.LastProbed.Select(id => id.Game).Distinct())} isn't switched on in this build." |
| 76 | }); |
| 77 | } |
| 78 | else if (!pointer.Enabled) |
| 79 | { |
| 80 | Settle(i, new PointerReport(stored, PointerStatus.Disabled)); |
| 81 | } |
| 82 | else if (!pointer.IsOnline) |
| 83 | { |
| 84 | Settle(i, new PointerReport(stored, PointerStatus.Offline) |
| 85 | { Detail = $"{pointer.Path} isn't there." }); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | var found = Mounts.Probe(pointer.Path).GroupBy(probe => probe.Id).Select(group => group.First()) |
| 90 | .ToArray(); |
| 91 | var probes = found.Where(probe => Features.Game(probe.Id.Game)).ToArray(); |
| 92 | foreach (var hidden in found.Except(probes)) |
| 93 | Mounts.Discard(hidden); |
| 94 | var last = pointer.LastProbed.Where(id => Features.Game(id.Game)).ToHashSet(); |
| 95 | PointerProbed?.Invoke(stored, probes); |
| 96 | if (probes.Length == 0 && found.Length > 0) |
| 97 | Settle(i, new PointerReport(stored, PointerStatus.Hidden) |
| 98 | { |
| 99 | Detail = |
| 100 | $"{string.Join(", ", found.Select(probe => probe.Id.Game).Distinct())} isn't switched on in this build." |
| 101 | }); |
| 102 | else if (probes.Length == 0) |
| 103 | Settle(i, new PointerReport(stored, PointerStatus.Unrecognised) |
| 104 | { Detail = "No installed adapter recognises it." }); |
| 105 | else if (last.Count > 0 && !last.SetEquals(probes.Select(probe => probe.Id))) |
| 106 | Settle(i, new PointerReport(stored, PointerStatus.Changed) |
| 107 | { |
| 108 | Probes = probes, |
| 109 | Detail = |
| 110 | $"It was {string.Join(", ", pointer.LastProbed)} and is now {string.Join(", ", probes.Select(probe => probe.Id))}." |
| 111 | }); |
| 112 | else |
| 113 | pending.Add((i, probes)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | var owners = new Dictionary<string, (int Index, MountId Id)>(StringComparer.Ordinal); |
| 118 | foreach (var (index, probes) in pending) |
| 119 | foreach (var probe in probes) |
| 120 | if (!_mountedFrom.ContainsKey(probe.Id.Game) && owners.TryAdd(probe.Id.Game, (index, probe.Id))) |
| 121 | Mounts.Offer(probe, pointers[index].Pointer.Externals, pointers[index].Pointer.KeyFile); |
| 122 | foreach (var (index, probes) in pending) |
| 123 | { |
| 124 | var stored = pointers[index]; |
| 125 | if (probes.FirstOrDefault(probe => _mountedFrom.ContainsKey(probe.Id.Game)) is { } earlier) |
| 126 | { |
| 127 | var (id, path) = _mountedFrom[earlier.Id.Game]; |
| 128 | Settle(index, new PointerReport(stored, PointerStatus.Duplicate) |
| 129 | { |
| 130 | Probes = probes, |
| 131 | Detail = $"{earlier.Id.Game} is already mounted as {id} from {path}." |
| 132 | }); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | if (probes.FirstOrDefault(probe => owners[probe.Id.Game].Index != index) is { } duplicate) |
| 137 | { |
| 138 | var owner = owners[duplicate.Id.Game]; |
| 139 | Settle(index, new PointerReport(stored, PointerStatus.Duplicate) |
| 140 | { |
| 141 | Probes = probes, |
| 142 | Detail = |
| 143 | $"{duplicate.Id.Game} is already mounted as {owner.Id} from {pointers[owner.Index].Pointer.Path}." |
| 144 | }); |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | try |
| 149 | { |
| 150 | var opened = probes |
| 151 | .Select(probe => Mounts.Open(probe, stored.Pointer.Externals, stored.Pointer.KeyFile)).ToArray(); |
| 152 | foreach (var mount in opened) |
| 153 | { |
| 154 | Conversion.AddNamespace(ContentNamespace.For(mount)); |
| 155 | _mountedFrom[mount.Id.Game] = (mount.Id, stored.Pointer.Path); |
| 156 | } |
| 157 | |
| 158 | Settle(index, new PointerReport(stored, PointerStatus.Mounted) { Probes = probes, Mounts = opened }); |
| 159 | } |
| 160 | catch (Exception error) when (error is MountException or InvalidOperationException) |
| 161 | { |
| 162 | Settle(index, new PointerReport(stored, PointerStatus.Failed) |
| 163 | { Probes = probes, Detail = error.Message }); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | foreach (var report in reports) |
| 168 | foreach (var probe in report!.Probes) |
| 169 | Mounts.Discard(probe); |
| 170 | return reports.Select(report => report!).ToArray(); |
| 171 | } |
| 172 | |
| 173 | public void Close(PointerReport report) |
| 174 | { |
| 175 | var closing = report.Mounts.ToHashSet(); |
| 176 | if (Mounts.Mounts.Where(mount => !closing.Contains(mount) && mount.Dependencies.Any(closing.Contains)) |
| 177 | .Select(mount => mount.Id).ToArray() is { Length: > 0 } needing) |
| 178 | throw new MountException( |
| 179 | $"{string.Join(", ", closing.Select(mount => mount.Id))} is still needed by {string.Join(", ", needing)}."); |
| 180 | |
| 181 | var externals = new List<Mount>(); |
| 182 | while (closing.Count > 0) |
| 183 | { |
| 184 | var mount = closing.First(candidate => !closing.Any(other => other.Dependencies.Contains(candidate))); |
| 185 | Mounts.Close(mount); |
| 186 | Conversion.RemoveNamespace(ContentNamespace.For(mount).Name); |
| 187 | _mountedFrom.Remove(mount.Id.Game); |
| 188 | closing.Remove(mount); |
| 189 | externals.AddRange(mount.Externals.Select(external => external.Mount).OfType<Mount>()); |
| 190 | } |
| 191 | |
| 192 | foreach (var external in externals.Distinct()) |
| 193 | if (Mounts.Mounts.Contains(external) && !_mountedFrom.ContainsKey(external.Id.Game) && |
| 194 | !Mounts.Mounts.Any(mount => mount.Dependencies.Contains(external) || |
| 195 | mount.Externals.Any(other => other.Mount == external))) |
| 196 | Mounts.Close(external); |
| 197 | } |
| 198 | |
| 199 | public IReadOnlyList<MountId> Identify(string path) |
| 200 | { |
| 201 | var probes = Mounts.Probe(path); |
| 202 | foreach (var probe in probes) |
| 203 | Mounts.Discard(probe); |
| 204 | return probes.Select(probe => probe.Id).Where(id => Features.Game(id.Game)).Distinct().ToArray(); |
| 205 | } |
| 206 | |
| 207 | public IReadOnlyList<InstallOffer> FindOffers(IEnumerable<StoredPointer> pointers) |
| 208 | { |
| 209 | var held = pointers.Select(pointer => Normal(pointer.Pointer.Path)).ToHashSet(StringComparer.Ordinal); |
| 210 | var offers = new List<InstallOffer>(); |
| 211 | foreach (var install in Mounts.FindInstalls()) |
| 212 | { |
| 213 | if (held.Contains(Normal(install.Path))) |
| 214 | continue; |
| 215 | var ids = Identify(install.Path); |
| 216 | if (ids.Count > 0 && ids.All(id => !_mountedFrom.ContainsKey(id.Game))) |
| 217 | offers.Add(new InstallOffer(install, ids)); |
| 218 | } |
| 219 | |
| 220 | return offers; |
| 221 | } |
| 222 | |
| 223 | public static string Normal(string path) |
| 224 | { |
| 225 | var full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(path)); |
| 226 | var root = Path.GetPathRoot(full) ?? ""; |
| 227 | var real = root; |
| 228 | foreach (var name in full[root.Length..] |
| 229 | .Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)) |
| 230 | { |
| 231 | real = Path.Combine(real, name); |
| 232 | FileSystemInfo entry = Directory.Exists(real) ? new DirectoryInfo(real) : new FileInfo(real); |
| 233 | if (entry.LinkTarget is not null && entry.ResolveLinkTarget(true) is { } target) |
| 234 | real = Path.TrimEndingDirectorySeparator(target.FullName); |
| 235 | } |
| 236 | |
| 237 | return real; |
| 238 | } |
| 239 | } |