0.2.0-dev Download ZIP
175 lines ·6.9 KB Raw
| 1 | using System.Globalization; |
| 2 | using System.Text.RegularExpressions; |
| 3 | using EvilEngine.Adapters.Pc; |
| 4 | using EvilEngine.Adapters.Steam; |
| 5 | using EvilEngine.Mounts; |
| 6 | |
| 7 | namespace EvilEngine.Adapters.Ksp; |
| 8 | |
| 9 | public sealed partial class KspGame : IGameAdapter |
| 10 | { |
| 11 | public const string Squad = "Squad"; |
| 12 | |
| 13 | public const string Expansions = "SquadExpansion"; |
| 14 | |
| 15 | public const string AppId = "220200"; |
| 16 | |
| 17 | private static readonly Platform Pc = PcPlatform.Platform; |
| 18 | |
| 19 | private static readonly string[] BuildIdFiles = ["buildID.txt", "buildID64.txt"]; |
| 20 | |
| 21 | private static readonly (string File, string Player)[] Players = |
| 22 | [("KSP.x86_64", "linux"), ("KSP_x64.exe", "windows"), ("KSP.app", "macos")]; |
| 23 | |
| 24 | private static readonly string[] DataFolders = ["KSP_Data", "KSP_x64_Data"]; |
| 25 | |
| 26 | private static readonly string[] PartFolders = ["Parts", "Sounds", "FX", "Resources"]; |
| 27 | |
| 28 | private static readonly string[] CoreBundles = ["squadcore.ksp", "squadcorefx.ksp"]; |
| 29 | |
| 30 | private static readonly string[] InteriorFolders = ["Spaces", "Props"]; |
| 31 | |
| 32 | public string Game => Ksp.Game; |
| 33 | |
| 34 | public IReadOnlyList<Platform> Platforms { get; } = [Pc]; |
| 35 | |
| 36 | public MountProbe? Probe(IFileSource tree) |
| 37 | { |
| 38 | if (BuildIdFile(tree) is not { } buildIdFile) |
| 39 | return null; |
| 40 | var squad = GameData.Root.Join(Squad); |
| 41 | if (!tree.Enumerate(squad, true).Any()) |
| 42 | return null; |
| 43 | if (DataFolders.FirstOrDefault(folder => tree.Exists($"{folder}/globalgamemanagers")) is not { } data) |
| 44 | return null; |
| 45 | |
| 46 | var evidence = new List<string> { $"{squad} holds the base game", $"{data} is Unity's player data" }; |
| 47 | foreach (var (file, player) in Players) |
| 48 | if (tree.Exists(file) || tree.Enumerate(file, true).Any()) |
| 49 | evidence.Add($"{file} is the {player} player build"); |
| 50 | var build = BuildIdPattern().Match(tree.ReadAllText(buildIdFile)); |
| 51 | var version = tree.Exists("readme.txt") ? VersionPattern().Match(tree.ReadAllText("readme.txt")) : Match.Empty; |
| 52 | if (!build.Success || !version.Success) |
| 53 | { |
| 54 | evidence.Add(build.Success ? "readme.txt names no version" : $"{buildIdFile} names no build id"); |
| 55 | return new MountProbe(new MountId(Game, Pc.Id, "unknown"), ProbeStatus.UnknownVariant, tree) |
| 56 | { Evidence = evidence, Externals = [Workshop.External] }; |
| 57 | } |
| 58 | |
| 59 | var number = int.Parse(build.Groups[1].Value, CultureInfo.InvariantCulture); |
| 60 | evidence.Add($"readme.txt names version {version.Groups[1].Value} and {buildIdFile} build {number}"); |
| 61 | return new MountProbe(new MountId(Game, Pc.Id, $"{version.Groups[1].Value}.{number}"), ProbeStatus.Known, tree) |
| 62 | { Evidence = evidence, Externals = [Workshop.External] }; |
| 63 | } |
| 64 | |
| 65 | public MountPlan Open(MountProbe probe, Externals externals) |
| 66 | { |
| 67 | var tree = probe.Tree; |
| 68 | var layers = new List<LayerPlan>(); |
| 69 | var parts = new List<PartPlan> |
| 70 | { |
| 71 | new("parts", |
| 72 | [ |
| 73 | .. PartFolders.SelectMany(folder => Files(tree, $"GameData/{Squad}/{folder}")), |
| 74 | .. CoreBundles.Select(bundle => VirtualPath.Parse($"GameData/{Squad}/{bundle}")).Where(tree.Exists) |
| 75 | ]), |
| 76 | new("interiors", [.. InteriorFolders.SelectMany(folder => Files(tree, $"GameData/{Squad}/{folder}"))]) |
| 77 | }; |
| 78 | var mods = new List<PartPlan>(); |
| 79 | |
| 80 | foreach (var folder in GameData.Folders(tree, GameData.Root)) |
| 81 | { |
| 82 | var path = GameData.Root.Join(folder); |
| 83 | if (folder.Equals(Expansions, StringComparison.OrdinalIgnoreCase)) |
| 84 | { |
| 85 | foreach (var pack in GameData.Folders(tree, path)) |
| 86 | { |
| 87 | var packPath = path.Join(pack); |
| 88 | layers.Add(Placed(tree, packPath, "dlc")); |
| 89 | parts.Add(new PartPlan($"dlc/{pack.ToLowerInvariant()}", Files(tree, packPath))); |
| 90 | } |
| 91 | |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | var squad = folder.Equals(Squad, StringComparison.OrdinalIgnoreCase); |
| 96 | layers.Add(Placed(tree, path, squad ? "base" : "mod")); |
| 97 | if (!squad) |
| 98 | mods.Add(new PartPlan($"mod/{folder.ToLowerInvariant()}", Files(tree, path, true))); |
| 99 | } |
| 100 | |
| 101 | var notes = new List<string>(); |
| 102 | var craft = new List<PartPlan>(); |
| 103 | if (externals[Workshop.External.Name] is { IsResolved: true } workshop) |
| 104 | foreach (var (layer, part) in WorkshopItems(workshop, notes)) |
| 105 | { |
| 106 | layers.Add(layer); |
| 107 | craft.Add(part); |
| 108 | } |
| 109 | |
| 110 | layers.Add(new LayerPlan("install", "base", new LayerSource.Tree(tree))); |
| 111 | |
| 112 | return new MountPlan(Pc.CaseRule) |
| 113 | { |
| 114 | Layers = layers, |
| 115 | Parts = [.. parts, .. mods, .. craft], |
| 116 | BuildFiles = [BuildIdFile(tree)!.Value], |
| 117 | Notes = notes |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | private static List<(LayerPlan Layer, PartPlan Part)> WorkshopItems(ResolvedExternal workshop, List<string> notes) |
| 122 | { |
| 123 | var files = workshop.Files!; |
| 124 | var items = new List<(LayerPlan, PartPlan)>(); |
| 125 | var installed = Workshop.Installed(files, AppId, notes); |
| 126 | foreach (var item in installed) |
| 127 | { |
| 128 | if (!Workshop.IsOn(item, workshop, true, "", notes)) |
| 129 | continue; |
| 130 | var folder = Workshop.Content(AppId, item.Id); |
| 131 | var inFolder = Files(files, folder); |
| 132 | if (inFolder.Length == 0) |
| 133 | { |
| 134 | notes.Add($"{item.Name} has no files in {folder}."); |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | items.Add(( |
| 139 | new LayerPlan(item.Name, "workshop", |
| 140 | new LayerSource.Tree(new PlacedSource(new SubtreeSource(files, folder), item.Name))), |
| 141 | new PartPlan(item.Name, inFolder) { Files = files, Version = item.Manifest })); |
| 142 | } |
| 143 | |
| 144 | Workshop.NoteUninstalled(installed, workshop, notes); |
| 145 | return items; |
| 146 | } |
| 147 | |
| 148 | private static LayerPlan Placed(IFileSource tree, VirtualPath path, string role) |
| 149 | { |
| 150 | return new LayerPlan(path.Value, role, |
| 151 | new LayerSource.Tree(new PlacedSource(new SubtreeSource(tree, path), path))); |
| 152 | } |
| 153 | |
| 154 | private static VirtualPath[] Files(IFileSource tree, VirtualPath folder, bool skipPluginData = false) |
| 155 | { |
| 156 | return tree.Enumerate(folder, true) |
| 157 | .Select(entry => entry.Path) |
| 158 | .Where(path => !skipPluginData || !path.Value.Split(VirtualPath.Separator) |
| 159 | .Contains("PluginData", StringComparer.OrdinalIgnoreCase)) |
| 160 | .Order() |
| 161 | .ToArray(); |
| 162 | } |
| 163 | |
| 164 | private static VirtualPath? BuildIdFile(IFileSource tree) |
| 165 | { |
| 166 | return BuildIdFiles.Select(VirtualPath.Parse).Cast<VirtualPath?>() |
| 167 | .FirstOrDefault(file => tree.Exists(file!.Value)); |
| 168 | } |
| 169 | |
| 170 | [GeneratedRegex(@"^\s*build id\s*=\s*(\d+)\s*$", RegexOptions.Multiline)] |
| 171 | private static partial Regex BuildIdPattern(); |
| 172 | |
| 173 | [GeneratedRegex(@"^\s*Version (\d+\.\d+\.\d+)\s*$", RegexOptions.Multiline)] |
| 174 | private static partial Regex VersionPattern(); |
| 175 | } |