|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +using Xamarin.Android.Tools; |
| 6 | +using Xamarin.Tools.Zip; |
| 7 | + |
| 8 | +namespace Microsoft.Android.AppTools.Assemblies; |
| 9 | + |
| 10 | +class AssemblyStoreExplorer |
| 11 | +{ |
| 12 | + readonly AssemblyStoreReader reader; |
| 13 | + |
| 14 | + public string StorePath { get; } |
| 15 | + public AndroidTargetArch? TargetArch { get; } |
| 16 | + public uint AssemblyCount { get; } |
| 17 | + public uint IndexEntryCount { get; } |
| 18 | + public IList<AssemblyStoreItem>? Assemblies { get; } |
| 19 | + public IDictionary<string, AssemblyStoreItem>? AssembliesByName { get; } |
| 20 | + public bool Is64Bit { get; } |
| 21 | + |
| 22 | + protected AssemblyStoreExplorer (Stream storeStream, string path) |
| 23 | + { |
| 24 | + StorePath = path; |
| 25 | + var storeReader = AssemblyStoreReader.Create (storeStream, path); |
| 26 | + if (storeReader == null) { |
| 27 | + storeStream.Dispose (); |
| 28 | + throw new NotSupportedException ($"Format of assembly store '{path}' is unsupported"); |
| 29 | + } |
| 30 | + |
| 31 | + reader = storeReader; |
| 32 | + TargetArch = reader.TargetArch; |
| 33 | + AssemblyCount = reader.AssemblyCount; |
| 34 | + IndexEntryCount = reader.IndexEntryCount; |
| 35 | + Assemblies = reader.Assemblies; |
| 36 | + Is64Bit = reader.Is64Bit; |
| 37 | + |
| 38 | + if (Assemblies == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + var dict = new Dictionary<string, AssemblyStoreItem> (StringComparer.Ordinal); |
| 43 | + foreach (AssemblyStoreItem item in Assemblies) { |
| 44 | + dict.Add (item.Name, item); |
| 45 | + } |
| 46 | + AssembliesByName = dict.AsReadOnly (); |
| 47 | + } |
| 48 | + |
| 49 | + protected AssemblyStoreExplorer (FileInfo storeInfo) |
| 50 | + : this (storeInfo.OpenRead (), storeInfo.FullName) |
| 51 | + {} |
| 52 | + |
| 53 | + public static IList<AssemblyStoreExplorer>? Open (ILogger log, string inputFile, FileFormat format, FileInfo info) |
| 54 | + { |
| 55 | + switch (format) { |
| 56 | + case FileFormat.Unknown: |
| 57 | + log.Debug ($"File '{inputFile}' has an unknown format."); |
| 58 | + return null; |
| 59 | + |
| 60 | + case FileFormat.Zip: |
| 61 | + log.Debug ($"File '{inputFile}' is a ZIP archive, but not an Android one."); |
| 62 | + return null; |
| 63 | + |
| 64 | + case FileFormat.AssemblyStore: |
| 65 | + case FileFormat.ELF: |
| 66 | + return new List<AssemblyStoreExplorer> { new AssemblyStoreExplorer (info)}; |
| 67 | + |
| 68 | + case FileFormat.Aab: |
| 69 | + return OpenAab (log, info); |
| 70 | + |
| 71 | + case FileFormat.AabBase: |
| 72 | + return OpenAabBase (log, info); |
| 73 | + |
| 74 | + case FileFormat.Apk: |
| 75 | + return OpenApk (log, info); |
| 76 | + |
| 77 | + default: |
| 78 | + log.Debug ($"File '{inputFile}' has an unsupported format '{format}'"); |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + static IList<AssemblyStoreExplorer>? OpenAab (ILogger log, FileInfo fi) |
| 84 | + { |
| 85 | + return OpenCommon ( |
| 86 | + log, |
| 87 | + fi, |
| 88 | + new List<IList<string>> { |
| 89 | + StoreReader_V2.AabPaths, |
| 90 | + } |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + static IList<AssemblyStoreExplorer>? OpenAabBase (ILogger log, FileInfo fi) |
| 95 | + { |
| 96 | + return OpenCommon ( |
| 97 | + log, |
| 98 | + fi, |
| 99 | + new List<IList<string>> { |
| 100 | + StoreReader_V2.AabBasePaths, |
| 101 | + } |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + static IList<AssemblyStoreExplorer>? OpenApk (ILogger log, FileInfo fi) |
| 106 | + { |
| 107 | + return OpenCommon ( |
| 108 | + log, |
| 109 | + fi, |
| 110 | + new List<IList<string>> { |
| 111 | + StoreReader_V2.ApkPaths, |
| 112 | + } |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + static IList<AssemblyStoreExplorer>? OpenCommon (ILogger log, FileInfo fi, List<IList<string>> pathLists) |
| 117 | + { |
| 118 | + using var zip = ZipArchive.Open (fi.FullName, FileMode.Open); |
| 119 | + IList<AssemblyStoreExplorer>? explorers; |
| 120 | + bool pathsFound; |
| 121 | + |
| 122 | + foreach (IList<string> paths in pathLists) { |
| 123 | + (explorers, pathsFound) = TryLoad (log, fi, zip, paths); |
| 124 | + if (pathsFound) { |
| 125 | + return explorers; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + log.Debug ("Unable to find any blob entries"); |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + static (IList<AssemblyStoreExplorer>? explorers, bool pathsFound) TryLoad (ILogger log, FileInfo fi, ZipArchive zip, IList<string> paths) |
| 134 | + { |
| 135 | + var ret = new List<AssemblyStoreExplorer> (); |
| 136 | + |
| 137 | + foreach (string path in paths) { |
| 138 | + if (!zip.ContainsEntry (path)) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + ZipEntry entry = zip.ReadEntry (path); |
| 143 | + var stream = new MemoryStream (); |
| 144 | + entry.Extract (stream); |
| 145 | + ret.Add (new AssemblyStoreExplorer (stream, $"{fi.FullName}!{path}")); |
| 146 | + } |
| 147 | + |
| 148 | + if (ret.Count == 0) { |
| 149 | + return (null, false); |
| 150 | + } |
| 151 | + |
| 152 | + return (ret, true); |
| 153 | + } |
| 154 | + |
| 155 | + public Stream? ReadImageData (AssemblyStoreItem item, bool uncompressIfNeeded = false) |
| 156 | + { |
| 157 | + return reader.ReadEntryImageData (item, uncompressIfNeeded); |
| 158 | + } |
| 159 | + |
| 160 | + string EnsureCorrectAssemblyName (string assemblyName) |
| 161 | + { |
| 162 | + assemblyName = Path.GetFileName (assemblyName); |
| 163 | + if (reader.NeedsExtensionInName) { |
| 164 | + if (!assemblyName.EndsWith (".dll", StringComparison.OrdinalIgnoreCase)) { |
| 165 | + return $"{assemblyName}.dll"; |
| 166 | + } |
| 167 | + } else { |
| 168 | + if (assemblyName.EndsWith (".dll", StringComparison.OrdinalIgnoreCase)) { |
| 169 | + return Path.GetFileNameWithoutExtension (assemblyName); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return assemblyName; |
| 174 | + } |
| 175 | + |
| 176 | + public IList<AssemblyStoreItem>? Find (string assemblyName, AndroidTargetArch? targetArch = null) |
| 177 | + { |
| 178 | + if (Assemblies == null) { |
| 179 | + return null; |
| 180 | + } |
| 181 | + |
| 182 | + assemblyName = EnsureCorrectAssemblyName (assemblyName); |
| 183 | + var items = new List<AssemblyStoreItem> (); |
| 184 | + foreach (AssemblyStoreItem item in Assemblies) { |
| 185 | + if (String.CompareOrdinal (assemblyName, item.Name) != 0) { |
| 186 | + continue; |
| 187 | + } |
| 188 | + |
| 189 | + if (targetArch != null && item.TargetArch != targetArch) { |
| 190 | + continue; |
| 191 | + } |
| 192 | + |
| 193 | + items.Add (item); |
| 194 | + } |
| 195 | + |
| 196 | + if (items.Count == 0) { |
| 197 | + return null; |
| 198 | + } |
| 199 | + |
| 200 | + return items; |
| 201 | + } |
| 202 | + |
| 203 | + public bool Contains (string assemblyName, AndroidTargetArch? targetArch = null) |
| 204 | + { |
| 205 | + IList<AssemblyStoreItem>? items = Find (assemblyName, targetArch); |
| 206 | + if (items == null || items.Count == 0) { |
| 207 | + return false; |
| 208 | + } |
| 209 | + |
| 210 | + return true; |
| 211 | + } |
| 212 | +} |
0 commit comments