|
| 1 | +using AssetsTools.NET.Extra; |
| 2 | +using SharpCompress.Archives; |
| 3 | +using SharpCompress.Readers; |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Net; |
| 9 | +using ThunderKit.Common; |
| 10 | +using UnityEngine; |
| 11 | + |
| 12 | +namespace ThunderKit.Core.Utilities |
| 13 | +{ |
| 14 | + internal static class ClassDataManager |
| 15 | + { |
| 16 | + const string TpkDownloadUrl = |
| 17 | + "https://nightly.link/AssetRipper/Tpk/workflows/type_tree_tpk/master/uncompressed_file.zip"; |
| 18 | + |
| 19 | + static readonly string CacheDir = Path.Combine("Library", "ThunderKit"); |
| 20 | + static readonly string CachedTpkPath = Path.Combine("Library", "ThunderKit", "classdata.tpk"); |
| 21 | + // Marker recording the last time a download attempt failed to yield support |
| 22 | + // for the current Unity version. Throttles re-downloads (see RetryThrottle). |
| 23 | + static readonly string MetadataPath = Path.Combine("Library", "ThunderKit", "classdata.tpk.json"); |
| 24 | + static readonly TimeSpan RetryThrottle = TimeSpan.FromDays(1); |
| 25 | + |
| 26 | + internal enum ClassDataStatus |
| 27 | + { |
| 28 | + CacheSupported, |
| 29 | + DownloadedSupported, |
| 30 | + Throttled, |
| 31 | + UnsupportedAfterDownload, |
| 32 | + DownloadFailed, |
| 33 | + } |
| 34 | + |
| 35 | + [Serializable] |
| 36 | + class TpkMetadata |
| 37 | + { |
| 38 | + public string lastAttemptUtc; |
| 39 | + } |
| 40 | + |
| 41 | + public static string GetClassDataPath() |
| 42 | + { |
| 43 | + var unityVersion = Application.unityVersion; |
| 44 | + var cacheSupports = SupportsVersion(CachedTpkPath, unityVersion); |
| 45 | + var throttled = IsThrottledNow(DateTime.UtcNow); |
| 46 | + |
| 47 | + var status = PlanAcquisition( |
| 48 | + cacheSupports, |
| 49 | + throttled, |
| 50 | + tryDownload: TryDownloadTpk, |
| 51 | + cacheSupportsAfterDownload: () => SupportsVersion(CachedTpkPath, unityVersion)); |
| 52 | + |
| 53 | + switch (status) |
| 54 | + { |
| 55 | + case ClassDataStatus.CacheSupported: |
| 56 | + case ClassDataStatus.DownloadedSupported: |
| 57 | + ClearAttemptMarker(); |
| 58 | + return CachedTpkPath; |
| 59 | + |
| 60 | + case ClassDataStatus.UnsupportedAfterDownload: |
| 61 | + SafeDelete(CachedTpkPath); |
| 62 | + WriteAttemptMarker(DateTime.UtcNow); |
| 63 | + ReportUnsupported(unityVersion); |
| 64 | + return null; |
| 65 | + |
| 66 | + case ClassDataStatus.Throttled: |
| 67 | + ReportUnsupported(unityVersion); |
| 68 | + return null; |
| 69 | + |
| 70 | + case ClassDataStatus.DownloadFailed: |
| 71 | + WriteAttemptMarker(DateTime.UtcNow); |
| 72 | + Debug.LogError($"[ThunderKit] Could not download class data and no cached classdata.tpk supports Unity {unityVersion}."); |
| 73 | + return null; |
| 74 | + |
| 75 | + default: |
| 76 | + return null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + internal static ClassDataStatus PlanAcquisition(bool cacheSupports, bool throttled, |
| 81 | + Func<bool> tryDownload, Func<bool> cacheSupportsAfterDownload) |
| 82 | + { |
| 83 | + if (cacheSupports) |
| 84 | + return ClassDataStatus.CacheSupported; |
| 85 | + |
| 86 | + if (throttled) |
| 87 | + return ClassDataStatus.Throttled; |
| 88 | + |
| 89 | + if (!tryDownload()) |
| 90 | + return ClassDataStatus.DownloadFailed; |
| 91 | + |
| 92 | + return cacheSupportsAfterDownload() |
| 93 | + ? ClassDataStatus.DownloadedSupported |
| 94 | + : ClassDataStatus.UnsupportedAfterDownload; |
| 95 | + } |
| 96 | + |
| 97 | + internal static bool SupportsVersion(string tpkPath, string unityVersion) |
| 98 | + { |
| 99 | + if (!File.Exists(tpkPath)) |
| 100 | + return false; |
| 101 | + if (!TryParseUnityVersion(unityVersion, out var major, out var minor, out var patch)) |
| 102 | + return false; |
| 103 | + |
| 104 | + try |
| 105 | + { |
| 106 | + var manager = new AssetsManager(); |
| 107 | + var package = manager.LoadClassPackage(tpkPath); |
| 108 | + var versions = package?.TpkTypeTree?.Versions; |
| 109 | + if (versions == null) |
| 110 | + return false; |
| 111 | + |
| 112 | + return versions.Any(v => v.major == major && v.minor == minor && v.patch == patch); |
| 113 | + } |
| 114 | + catch (Exception e) |
| 115 | + { |
| 116 | + Debug.LogWarning($"[ThunderKit] Failed to inspect classdata.tpk versions: {e.Message}"); |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + internal static bool TryParseUnityVersion(string unityVersion, out int major, out int minor, out int patch) |
| 122 | + { |
| 123 | + major = minor = patch = 0; |
| 124 | + if (string.IsNullOrEmpty(unityVersion)) |
| 125 | + return false; |
| 126 | + |
| 127 | + var parts = unityVersion.Split('.'); |
| 128 | + if (parts.Length < 3) |
| 129 | + return false; |
| 130 | + |
| 131 | + if (!int.TryParse(parts[0], out major)) |
| 132 | + return false; |
| 133 | + if (!int.TryParse(parts[1], out minor)) |
| 134 | + return false; |
| 135 | + |
| 136 | + var patchDigits = new string(parts[2].TakeWhile(char.IsDigit).ToArray()); |
| 137 | + return int.TryParse(patchDigits, out patch); |
| 138 | + } |
| 139 | + |
| 140 | + static bool IsThrottledNow(DateTime nowUtc) |
| 141 | + { |
| 142 | + if (!File.Exists(MetadataPath)) |
| 143 | + return false; |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + if (!TryReadLastAttemptUtc(File.ReadAllText(MetadataPath), out var lastAttemptUtc)) |
| 148 | + return false; |
| 149 | + |
| 150 | + return IsThrottled(lastAttemptUtc, nowUtc, RetryThrottle); |
| 151 | + } |
| 152 | + catch |
| 153 | + { |
| 154 | + return false; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + internal static bool IsThrottled(DateTime lastAttemptUtc, DateTime nowUtc, TimeSpan window) |
| 159 | + { |
| 160 | + return (nowUtc - lastAttemptUtc) < window; |
| 161 | + } |
| 162 | + |
| 163 | + internal static bool TryReadLastAttemptUtc(string json, out DateTime lastAttemptUtc) |
| 164 | + { |
| 165 | + lastAttemptUtc = default; |
| 166 | + try |
| 167 | + { |
| 168 | + var metadata = JsonUtility.FromJson<TpkMetadata>(json); |
| 169 | + if (metadata == null || string.IsNullOrEmpty(metadata.lastAttemptUtc)) |
| 170 | + return false; |
| 171 | + |
| 172 | + lastAttemptUtc = DateTime.Parse(metadata.lastAttemptUtc, CultureInfo.InvariantCulture, |
| 173 | + DateTimeStyles.RoundtripKind).ToUniversalTime(); |
| 174 | + return true; |
| 175 | + } |
| 176 | + catch |
| 177 | + { |
| 178 | + return false; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + static bool TryDownloadTpk() |
| 183 | + { |
| 184 | + try |
| 185 | + { |
| 186 | + Debug.LogWarning("[ThunderKit] Downloading tpk archive"); |
| 187 | + Directory.CreateDirectory(CacheDir); |
| 188 | + Directory.CreateDirectory(Constants.TempDir); |
| 189 | + |
| 190 | + var tempZipPath = Path.Combine(Constants.TempDir, "classdata_download.zip"); |
| 191 | + |
| 192 | + using (var client = new WebClient()) |
| 193 | + { |
| 194 | + client.DownloadFile(TpkDownloadUrl, tempZipPath); |
| 195 | + } |
| 196 | + |
| 197 | + if (ExtractTpkFromArchive(tempZipPath, CacheDir, CachedTpkPath) == null) |
| 198 | + { |
| 199 | + Debug.LogWarning("[ThunderKit] Downloaded archive does not contain a .tpk file"); |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + if (File.Exists(tempZipPath)) |
| 204 | + File.Delete(tempZipPath); |
| 205 | + |
| 206 | + Debug.Log("[ThunderKit] Successfully downloaded updated classdata.tpk"); |
| 207 | + return true; |
| 208 | + } |
| 209 | + catch (Exception e) |
| 210 | + { |
| 211 | + Debug.LogWarning($"[ThunderKit] Failed to download updated classdata.tpk: {e.Message}"); |
| 212 | + return false; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + internal static string ExtractTpkFromArchive(string archivePath, string destDir, string finalTpkPath) |
| 217 | + { |
| 218 | + using (var archive = ArchiveFactory.Open(archivePath)) |
| 219 | + { |
| 220 | + var tpkEntry = archive.Entries |
| 221 | + .FirstOrDefault(e => !e.IsDirectory && e.Key.EndsWith(".tpk", StringComparison.OrdinalIgnoreCase)); |
| 222 | + |
| 223 | + if (tpkEntry == null) |
| 224 | + return null; |
| 225 | + |
| 226 | + tpkEntry.WriteToDirectory(destDir, new ExtractionOptions |
| 227 | + { |
| 228 | + ExtractFullPath = false, |
| 229 | + Overwrite = true |
| 230 | + }); |
| 231 | + |
| 232 | + // The extracted file may have a different name (e.g. "uncompressed.tpk") |
| 233 | + var extractedName = Path.GetFileName(tpkEntry.Key); |
| 234 | + var extractedPath = Path.Combine(destDir, extractedName); |
| 235 | + var finalName = Path.GetFileName(finalTpkPath); |
| 236 | + if (!string.Equals(extractedName, finalName, StringComparison.OrdinalIgnoreCase) |
| 237 | + && File.Exists(extractedPath)) |
| 238 | + { |
| 239 | + if (File.Exists(finalTpkPath)) |
| 240 | + File.Delete(finalTpkPath); |
| 241 | + File.Move(extractedPath, finalTpkPath); |
| 242 | + } |
| 243 | + |
| 244 | + return finalTpkPath; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + static void ReportUnsupported(string unityVersion) |
| 249 | + { |
| 250 | + Debug.LogError($"[ThunderKit] The available class data (tpk) does not yet support the current version of Unity ({unityVersion}). " + |
| 251 | + "ProjectSettings import that relies on class data will be unavailable until AssetRipper publishes type data for this version."); |
| 252 | + } |
| 253 | + |
| 254 | + static void WriteAttemptMarker(DateTime nowUtc) |
| 255 | + { |
| 256 | + try |
| 257 | + { |
| 258 | + Directory.CreateDirectory(CacheDir); |
| 259 | + var metadata = new TpkMetadata { lastAttemptUtc = nowUtc.ToString("o") }; |
| 260 | + File.WriteAllText(MetadataPath, JsonUtility.ToJson(metadata)); |
| 261 | + } |
| 262 | + catch (Exception e) |
| 263 | + { |
| 264 | + Debug.LogWarning($"[ThunderKit] Failed to write class data attempt marker: {e.Message}"); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + static void ClearAttemptMarker() |
| 269 | + { |
| 270 | + SafeDelete(MetadataPath); |
| 271 | + } |
| 272 | + |
| 273 | + static void SafeDelete(string path) |
| 274 | + { |
| 275 | + try |
| 276 | + { |
| 277 | + if (File.Exists(path)) |
| 278 | + File.Delete(path); |
| 279 | + } |
| 280 | + catch (Exception e) |
| 281 | + { |
| 282 | + Debug.LogWarning($"[ThunderKit] Failed to delete {path}: {e.Message}"); |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | +} |
0 commit comments