|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace ECS.StreamableLoading.AssetBundles |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Locations and maintenance of the abgen sidecar's on-disk bundle stores under |
| 10 | + /// <c>persistentDataPath</c>, one home per server flavor. |
| 11 | + /// </summary> |
| 12 | + public static class AbgenBundleDiskCache |
| 13 | + { |
| 14 | + /// <summary>Sidecar server home under persistentDataPath: bin/ (the binary), cache/ and out/ (bundles).</summary> |
| 15 | + public const string SIDECAR_DIR = "abgen"; |
| 16 | + |
| 17 | + /// <summary>Sidecar home for the local-scene-development flavor; kept apart from the catalyst one.</summary> |
| 18 | + public const string SIDECAR_LSD_DIR = "abgen-lsd"; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Every disk root the sidecar writes bundles to: each flavor's cache/ and out/ directories — |
| 22 | + /// deliberately never a sidecar's bin/, which holds the downloaded binary. Reads |
| 23 | + /// persistentDataPath; call from the main thread. |
| 24 | + /// </summary> |
| 25 | + public static string[] AllBundleRoots() |
| 26 | + { |
| 27 | + string persistent = UnityEngine.Application.persistentDataPath; |
| 28 | + |
| 29 | + return new[] |
| 30 | + { |
| 31 | + Path.Combine(persistent, SIDECAR_DIR, "cache"), |
| 32 | + Path.Combine(persistent, SIDECAR_DIR, "out"), |
| 33 | + Path.Combine(persistent, SIDECAR_LSD_DIR, "cache"), |
| 34 | + Path.Combine(persistent, SIDECAR_LSD_DIR, "out"), |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Deletes every cached bundle under <paramref name="root" />, including in-progress temp files. |
| 40 | + /// Files that cannot be deleted (e.g. memory-mapped by a loaded AssetBundle on Windows) are skipped |
| 41 | + /// and counted. Walks every shard — call from a background thread. |
| 42 | + /// </summary> |
| 43 | + public static ClearResult ClearAll(string root) |
| 44 | + { |
| 45 | + var result = new ClearResult(); |
| 46 | + |
| 47 | + if (!Directory.Exists(root)) return result; |
| 48 | + |
| 49 | + foreach (string file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)) |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + long size = new FileInfo(file).Length; |
| 54 | + File.Delete(file); |
| 55 | + result.DeletedFiles++; |
| 56 | + result.DeletedBytes += size; |
| 57 | + } |
| 58 | + catch (Exception e) when (e is IOException or UnauthorizedAccessException) { result.SkippedFiles++; } |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary>Aggregate of <see cref="ClearAll(string)" /> over every root.</summary> |
| 65 | + public static ClearResult ClearAll(string[] roots) |
| 66 | + { |
| 67 | + var total = new ClearResult(); |
| 68 | + |
| 69 | + foreach (string root in roots) |
| 70 | + { |
| 71 | + ClearResult result = ClearAll(root); |
| 72 | + total.DeletedFiles += result.DeletedFiles; |
| 73 | + total.DeletedBytes += result.DeletedBytes; |
| 74 | + total.SkippedFiles += result.SkippedFiles; |
| 75 | + } |
| 76 | + |
| 77 | + return total; |
| 78 | + } |
| 79 | + |
| 80 | + public struct ClearResult |
| 81 | + { |
| 82 | + public int DeletedFiles; |
| 83 | + public long DeletedBytes; |
| 84 | + public int SkippedFiles; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments