| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text.RegularExpressions;
- using UnityEngine;
- namespace HotPatch
- {
- [Serializable]
- public sealed class HotPatchManifest
- {
- public int schemaVersion = 4;
- public string platform;
- public string playerBuildId;
- public string version;
- public string assemblyName;
- public string gameSceneName;
- public string gameScenePath;
- public bool developmentBuild;
- public HotPatchResource[] resources = Array.Empty<HotPatchResource>();
- public HotPatchResource[] aotBundles = Array.Empty<HotPatchResource>();
- public HotPatchResource aotBundle
- {
- get => (aotBundles ?? Array.Empty<HotPatchResource>()).SingleOrDefault();
- set => aotBundles = value == null ? Array.Empty<HotPatchResource>() : new[] { value };
- }
- public HotPatchResource[] aotMetadata = Array.Empty<HotPatchResource>();
- public void Validate(string expectedAssembly, string expectedPlatform, string expectedScene = null)
- {
- if (schemaVersion != 4 || (aotBundles?.Length ?? 0) > 1 || assemblyName != expectedAssembly || platform != expectedPlatform ||
- string.IsNullOrWhiteSpace(gameSceneName) || (expectedScene != null && gameSceneName != expectedScene) ||
- !IsScenePath(gameScenePath, gameSceneName) ||
- string.IsNullOrWhiteSpace(playerBuildId) || string.IsNullOrWhiteSpace(version) || resources == null || resources.Length == 0)
- throw new InvalidDataException("Invalid resource manifest version, platform, assembly, or scene. Use schemaVersion 4 bundle resources.");
- var names = new System.Collections.Generic.HashSet<string>(StringComparer.OrdinalIgnoreCase);
- int assemblies = 0, scenes = 0;
- foreach (var resource in resources)
- {
- if (resource == null || !HotPatchResource.IsBundleName(resource.name) || !names.Add(resource.name) ||
- resource.size <= 0 || !HotPatchHash.IsValid(resource.sha256))
- throw new InvalidDataException("Resource names are duplicate or unsafe, or a valid size / SHA-256 is missing.");
- if (resource.kind == HotPatchResource.AssemblyKind)
- {
- if (resource.name != HotPatchResource.AssemblyBundleName(assemblyName)) throw new InvalidDataException("Invalid hot-update bundle name in the manifest.");
- assemblies++;
- }
- else if (resource.kind == HotPatchResource.SceneKind)
- {
- if (resource.name != HotPatchResource.SceneBundleName(gameSceneName)) throw new InvalidDataException("Invalid scene bundle name in the manifest.");
- scenes++;
- }
- else throw new InvalidDataException("Update resources can contain only hot-update assembly and scene bundles: " + resource.kind);
- }
- if (assemblies != 1 || scenes != 1) throw new InvalidDataException("The resource manifest must contain one hot-update assembly bundle and one scene bundle.");
- if (aotBundle != null && (aotBundle.name != "aot.bundle" || aotBundle.kind != HotPatchResource.AotBundleKind ||
- aotBundle.size <= 0 || !HotPatchHash.IsValid(aotBundle.sha256)))
- throw new InvalidDataException("Invalid AOT bundle manifest.");
- foreach (var resource in aotMetadata ?? Array.Empty<HotPatchResource>())
- if (resource == null || resource.kind != HotPatchResource.AotKind || !HotPatchResource.IsValidName(resource.name) ||
- !names.Add(resource.name) || resource.size <= 0 || !HotPatchHash.IsValid(resource.sha256))
- throw new InvalidDataException("Invalid installed AOT metadata manifest.");
- }
- public void ValidateAgainst(HotPatchManifest original)
- {
- if (original == null) throw new ArgumentNullException(nameof(original));
- Validate(original.assemblyName, original.platform, original.gameSceneName);
- if (playerBuildId != original.playerBuildId || developmentBuild != original.developmentBuild)
- throw new InvalidDataException("Update resources are incompatible with the installed AOT baseline. Update the application package.");
- if (aotBundle != null && !aotBundle.SameContent(original.aotBundle))
- throw new InvalidDataException("The AOT bundle cannot be replaced by a remote update.");
- var aot = aotMetadata ?? Array.Empty<HotPatchResource>();
- var bundledAot = original.aotMetadata ?? Array.Empty<HotPatchResource>();
- if (aot.Length != 0 && (aot.Length != bundledAot.Length || aot.Any(r => !bundledAot.Any(r.SameContent))))
- throw new InvalidDataException("AOT metadata must be extracted from the application package and cannot be replaced by a remote update.");
- }
- public bool SameVersion(HotPatchManifest other) => other != null && schemaVersion == other.schemaVersion &&
- assemblyName == other.assemblyName && platform == other.platform && playerBuildId == other.playerBuildId &&
- gameSceneName == other.gameSceneName && gameScenePath == other.gameScenePath &&
- ((aotBundle == null && other.aotBundle == null) || (aotBundle != null && aotBundle.SameContent(other.aotBundle))) &&
- developmentBuild == other.developmentBuild && version == other.version &&
- SameResources(resources, other.resources) && SameResources(aotMetadata, other.aotMetadata);
- static bool SameResources(HotPatchResource[] first, HotPatchResource[] second)
- {
- first = first ?? Array.Empty<HotPatchResource>(); second = second ?? Array.Empty<HotPatchResource>();
- return first.Length == second.Length && first.All(r => second.Any(r.SameContent));
- }
- public HotPatchResource AssemblyResource => resources.Single(r => r.kind == HotPatchResource.AssemblyKind);
- public HotPatchResource SceneResource => resources.Single(r => r.kind == HotPatchResource.SceneKind);
- public static bool IsScenePath(string path, string scene) => !string.IsNullOrEmpty(path) &&
- path.StartsWith("Assets/", StringComparison.Ordinal) && !path.Contains("..") && !path.Contains('\\') &&
- path.EndsWith(".unity", StringComparison.OrdinalIgnoreCase) && Path.GetFileNameWithoutExtension(path) == scene;
- }
- [Serializable]
- public sealed class HotPatchResource
- {
- public const string AssemblyKind = "assembly-bundle";
- public const string SceneKind = "scene-bundle";
- public const string AotBundleKind = "aot-bundle";
- public const string AotKind = "aot";
- public string name;
- public string kind;
- public long size;
- public string sha256;
- public string RelativePath
- {
- get
- {
- if (!HotPatchHash.IsValid(sha256)) throw new InvalidDataException("Invalid resource SHA-256.");
- if (kind == AotBundleKind) return "aot.bundle";
- if (kind == AotKind)
- {
- if (!IsValidName(name)) throw new InvalidDataException("Invalid AOT file name.");
- return name + ".bytes";
- }
- return "files/" + sha256.ToLowerInvariant() + ".bundle";
- }
- }
- public bool SameContent(HotPatchResource other) => other != null && name == other.name && kind == other.kind &&
- size == other.size && string.Equals(sha256, other.sha256, StringComparison.OrdinalIgnoreCase);
- public static bool IsValidName(string value) => value != null &&
- Regex.IsMatch(value, @"\A[\p{L}\p{N}_.-]+\.dll\z") && !value.Contains("..");
- public static bool IsBundleName(string value) => value != null &&
- Regex.IsMatch(value, @"\A[\p{L}\p{N}_. -]+\.bundle\z") && !value.Contains("..");
- public static string AssemblyBundleName(string name) => "hotupdate." + name.ToLowerInvariant() + ".bundle";
- public static string SceneBundleName(string name) => "scene." + name.ToLowerInvariant() + ".bundle";
- }
- public readonly struct HotPatchProgress
- {
- public readonly string Stage;
- public readonly float Value;
- public readonly ulong DownloadedBytes;
- public readonly long TotalBytes;
- public HotPatchProgress(string stage, float value, ulong downloaded = 0, long total = 0)
- { Stage = stage; Value = Mathf.Clamp01(value); DownloadedBytes = downloaded; TotalBytes = total; }
- }
- public static class HotPatchHash
- {
- public static bool IsValid(string value) => value != null && Regex.IsMatch(value, "\\A[0-9a-fA-F]{64}\\z");
- public static string Of(byte[] bytes)
- {
- using var sha = SHA256.Create();
- return BitConverter.ToString(sha.ComputeHash(bytes)).Replace("-", "").ToLowerInvariant();
- }
- public static string OfFile(string path)
- {
- using var stream = File.OpenRead(path);
- using var sha = SHA256.Create();
- return BitConverter.ToString(sha.ComputeHash(stream)).Replace("-", "").ToLowerInvariant();
- }
- public static void RequireHttp(Uri uri)
- {
- if (uri == null || !uri.IsAbsoluteUri || (uri.Scheme != "https" && uri.Scheme != "http"))
- throw new InvalidDataException("The hot patch URL must be an absolute HTTP(S) URL.");
- }
- }
- }
|