| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- namespace HotPatch.Editor
- {
- public static class HotPatchResourcePackage
- {
- public const string HotManifestFileName = "manifest_hot.json";
- public static HotPatchManifest Write(string destination, string assemblyBundle, string sceneBundle, string aotBundle,
- IReadOnlyDictionary<string, string> aotFiles, string assemblyName, string sceneName, string scenePath,
- string platform, string buildId, bool development, HotPatchManifest original = null)
- {
- if (Directory.Exists(destination) && Directory.EnumerateFileSystemEntries(destination).Any())
- throw new IOException("资源输出目录必须为空: " + destination);
- var inputs = new List<KeyValuePair<HotPatchResource, string>>
- {
- new KeyValuePair<HotPatchResource, string>(Describe(HotPatchResource.AssemblyBundleName(assemblyName), HotPatchResource.AssemblyKind, assemblyBundle), assemblyBundle),
- new KeyValuePair<HotPatchResource, string>(Describe(HotPatchResource.SceneBundleName(sceneName), HotPatchResource.SceneKind, sceneBundle), sceneBundle)
- };
- var manifest = new HotPatchManifest
- {
- assemblyName = assemblyName, gameSceneName = sceneName, gameScenePath = scenePath,
- platform = platform, playerBuildId = buildId, developmentBuild = development,
- resources = inputs.Select(p => p.Key).ToArray(),
- aotMetadata = aotFiles.OrderBy(p => p.Key, StringComparer.Ordinal)
- .Select(p => Describe(p.Key, HotPatchResource.AotKind, p.Value)).ToArray()
- };
- if (aotBundle != null)
- {
- manifest.aotBundle = Describe("aot.bundle", HotPatchResource.AotBundleKind, aotBundle);
- inputs.Add(new KeyValuePair<HotPatchResource, string>(manifest.aotBundle, aotBundle));
- }
- else
- {
- if (original?.aotBundle == null || (original.aotMetadata?.Length ?? 0) == 0)
- throw new InvalidDataException("更新资源必须继承原始资源的完整 AOT 清单。");
-
- var copy = HotPatchResourceStore.Parse(JsonUtility.ToJson(original));
- copy.Validate(assemblyName, platform, sceneName);
- manifest.aotBundles = copy.aotBundles;
- manifest.aotMetadata = copy.aotMetadata;
- }
- manifest.version = HotPatchHash.Of(Encoding.UTF8.GetBytes(scenePath + "\n" + string.Join("\n",
- manifest.resources.Select(r => r.kind + ":" + r.name + ":" + r.sha256))));
- manifest.Validate(assemblyName, platform, sceneName);
- if (original != null) manifest.ValidateAgainst(original);
- Directory.CreateDirectory(Path.Combine(destination, "files"));
- foreach (var input in inputs)
- {
- var output = Path.Combine(destination, input.Key.RelativePath);
- File.Copy(input.Value, output, true);
- if (!HotPatchResourceStore.IsVerified(output, input.Key)) throw new IOException("资源复制期间发生变化: " + input.Value);
- }
- File.WriteAllText(Path.Combine(destination, HotPatchResourceStore.ManifestFileName), JsonUtility.ToJson(manifest, true), new UTF8Encoding(false));
- return manifest;
- }
- public static void CopyOriginal(string sourceDirectory, string destination, string manifestFileName = HotPatchResourceStore.ManifestFileName)
- {
- if (manifestFileName != HotPatchResourceStore.ManifestFileName && manifestFileName != HotManifestFileName)
- throw new ArgumentException("资源清单名称不正确。", nameof(manifestFileName));
- if (Directory.Exists(destination) && Directory.EnumerateFileSystemEntries(destination).Any())
- throw new IOException("资源输出目录必须为空: " + destination);
- var json = File.ReadAllText(RequireChild(sourceDirectory, Path.Combine(sourceDirectory, manifestFileName)));
- var original = HotPatchResourceStore.Parse(json);
- original.Validate(original.assemblyName, original.platform, original.gameSceneName);
- if (original.aotBundle == null || (original.aotMetadata?.Length ?? 0) == 0)
- throw new InvalidDataException("原始资源缺少完整 AOT 清单。");
- var resources = original.resources.Concat(new[] { original.aotBundle }).ToArray();
- foreach (var resource in resources)
- if (!HotPatchResourceStore.IsVerified(RequireChild(sourceDirectory, Path.Combine(sourceDirectory, resource.RelativePath)), resource))
- throw new InvalidDataException("原始资源缺失或损坏: " + resource.name);
- foreach (var resource in resources)
- {
- var output = RequireChild(destination, Path.Combine(destination, resource.RelativePath));
- Directory.CreateDirectory(Path.GetDirectoryName(output));
- File.Copy(Path.Combine(sourceDirectory, resource.RelativePath), output);
- if (!HotPatchResourceStore.IsVerified(output, resource)) throw new IOException("原始资源复制失败: " + resource.name);
- }
- File.WriteAllText(Path.Combine(destination, HotPatchResourceStore.ManifestFileName), json, new UTF8Encoding(false));
- }
- public static void AppendUpdate(string originalDirectory, string updateDirectory)
- {
- var original = HotPatchResourceStore.Parse(File.ReadAllText(Path.Combine(originalDirectory, HotPatchResourceStore.ManifestFileName)));
- var updateJson = File.ReadAllText(Path.Combine(updateDirectory, HotPatchResourceStore.ManifestFileName));
- var update = HotPatchResourceStore.Parse(updateJson);
- original.Validate(original.assemblyName, original.platform, original.gameSceneName);
- update.ValidateAgainst(original);
- if (original.aotBundle == null || update.aotBundle == null ||
- (original.aotMetadata?.Length ?? 0) == 0 || update.aotMetadata?.Length != original.aotMetadata.Length)
- throw new InvalidDataException("合并资源需要两份完整且一致的 AOT 清单。");
- var hotManifest = RequireChild(originalDirectory, Path.Combine(originalDirectory, HotManifestFileName));
- if (File.Exists(hotManifest)) throw new IOException("原始资源暂存目录已包含更新清单。");
-
- foreach (var resource in original.resources.Concat(new[] { original.aotBundle }))
- if (!HotPatchResourceStore.IsVerified(RequireChild(originalDirectory, Path.Combine(originalDirectory, resource.RelativePath)), resource))
- throw new InvalidDataException("原始资源缺失或损坏: " + resource.name);
- foreach (var resource in update.resources)
- if (!HotPatchResourceStore.IsVerified(RequireChild(updateDirectory, Path.Combine(updateDirectory, resource.RelativePath)), resource))
- throw new InvalidDataException("更新资源缺失或损坏: " + resource.name);
- foreach (var resource in update.resources)
- {
- var destination = RequireChild(originalDirectory, Path.Combine(originalDirectory, resource.RelativePath));
- if (File.Exists(destination))
- {
- if (!HotPatchResourceStore.IsVerified(destination, resource)) throw new InvalidDataException("资源哈希路径冲突: " + resource.name);
- continue;
- }
- File.Copy(Path.Combine(updateDirectory, resource.RelativePath), destination);
- if (!HotPatchResourceStore.IsVerified(destination, resource)) throw new IOException("更新资源复制失败: " + resource.name);
- }
- File.WriteAllText(hotManifest, updateJson, new UTF8Encoding(false));
- }
- public static HotPatchResource Describe(string name, string kind, string source)
- {
- if (!File.Exists(source)) throw new FileNotFoundException("资源源文件不存在。", source);
- return new HotPatchResource { name = name, kind = kind, size = new FileInfo(source).Length, sha256 = HotPatchHash.OfFile(source) };
- }
-
- public static void ClearDirectory(string directory, string allowedParent)
- {
- directory = RequireChild(allowedParent, directory);
- if (!Directory.Exists(directory)) return;
- RejectLinks(directory);
- foreach (var entry in Directory.GetFileSystemEntries(directory))
- {
- var path = RequireChild(directory, entry);
- if (Directory.Exists(path)) Directory.Delete(path, true);
- else File.Delete(path);
- }
- }
-
- public static void DeleteDirectory(string directory, string allowedParent)
- {
- directory = RequireChild(allowedParent, directory);
- ClearDirectory(directory, allowedParent);
- if (Directory.Exists(directory)) Directory.Delete(directory);
- var meta = RequireChild(allowedParent, directory + ".meta");
- if (File.Exists(meta)) File.Delete(meta);
- }
-
- public static void ReplaceDirectory(string staging, string destination, string stagingParent, string destinationParent)
- {
- staging = RequireChild(stagingParent, staging);
- destination = RequireChild(destinationParent, destination);
- if (!Directory.Exists(staging)) throw new DirectoryNotFoundException(staging);
- RejectLinks(staging);
- if (Directory.Exists(destination)) RejectLinks(destination);
- var backup = RequireChild(stagingParent, Path.Combine(stagingParent, "replaced-" + Guid.NewGuid().ToString("N")));
- bool hadOld = Directory.Exists(destination);
- if (hadOld) Directory.Move(destination, backup);
- try { Directory.Move(staging, destination); }
- catch
- {
- if (hadOld) Directory.Move(backup, destination);
- throw;
- }
- if (hadOld) Directory.Delete(backup, true);
- }
- public static string RequireChild(string parent, string path)
- {
- var root = Path.GetFullPath(parent).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar;
- var full = Path.GetFullPath(path);
- if (!full.StartsWith(root, StringComparison.OrdinalIgnoreCase)) throw new IOException("资源路径超出指定目录: " + full);
-
- for (var ancestor = new DirectoryInfo(Path.GetDirectoryName(full)); ancestor != null; ancestor = ancestor.Parent)
- if (ancestor.Exists && (ancestor.Attributes & FileAttributes.ReparsePoint) != 0)
- throw new IOException("资源路径包含符号链接目录: " + ancestor.FullName);
- return full;
- }
- static void RejectLinks(string directory)
- {
- if ((File.GetAttributes(directory) & FileAttributes.ReparsePoint) != 0)
- throw new IOException("资源构建目录不能是符号链接: " + directory);
- foreach (var child in Directory.GetFileSystemEntries(directory))
- {
- if ((File.GetAttributes(child) & FileAttributes.ReparsePoint) != 0)
- throw new IOException("资源构建目录包含符号链接: " + child);
- if (Directory.Exists(child)) RejectLinks(child);
- }
- }
- }
- }
|