|
|
@@ -0,0 +1,92 @@
|
|
|
+using System;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using UnityEditor;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+namespace HotPatch.Editor
|
|
|
+{
|
|
|
+ [InitializeOnLoad]
|
|
|
+ public static class HotPatchLocalTesting
|
|
|
+ {
|
|
|
+ static HotPatchLocalTesting()
|
|
|
+ {
|
|
|
+ HotPatchResourceActions.FindLatestResources = FindLatest;
|
|
|
+ HotPatchResourceActions.IsBuildRunning = () => HotPatchBuild.IsBuilding;
|
|
|
+ HotPatchResourceActions.ApplyResources = Apply;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string FindLatest()
|
|
|
+ {
|
|
|
+ var settings = Resources.Load<AppSettings>("AppSettings");
|
|
|
+ if (settings == null) return null;
|
|
|
+ return FindLatest(Path.GetFullPath("Build/HotUpdate"), settings.HotPatchAssemblyName,
|
|
|
+ EditorUserBuildSettings.activeBuildTarget.ToString(), settings.GameSceneName?.Trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string FindLatest(string root, string assembly, string platform, string scene)
|
|
|
+ {
|
|
|
+ if (!Directory.Exists(root) || string.IsNullOrWhiteSpace(assembly) || string.IsNullOrWhiteSpace(scene)) return null;
|
|
|
+ foreach (var directory in Directory.GetDirectories(root, "AllRes_*", SearchOption.TopDirectoryOnly).OrderByDescending(Path.GetFileName, StringComparer.Ordinal))
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ HotPatchResourcePackage.RequireChild(root, directory);
|
|
|
+ var original = ReadManifest(directory, "manifest.json");
|
|
|
+ original.Validate(assembly, platform, scene);
|
|
|
+ var hot = ReadManifest(directory, HotPatchResourcePackage.HotManifestFileName);
|
|
|
+ hot.ValidateAgainst(original);
|
|
|
+ if (original.aotBundle == null || hot.aotBundle == null || original.aotMetadata == null || hot.aotMetadata == null ||
|
|
|
+ !original.aotMetadata.Select(r => r.name).OrderBy(n => n).SequenceEqual(HotPatchBundleLoader.AotAssemblyNames.OrderBy(n => n)) ||
|
|
|
+ hot.aotMetadata.Length != original.aotMetadata.Length) continue;
|
|
|
+ var files = original.resources.Concat(hot.resources).Concat(new[] { original.aotBundle });
|
|
|
+ if (files.All(resource =>
|
|
|
+ {
|
|
|
+ var path = HotPatchResourcePackage.RequireChild(directory, Path.Combine(directory, resource.RelativePath));
|
|
|
+ return File.Exists(path) && new FileInfo(path).Length == resource.size;
|
|
|
+ })) return directory;
|
|
|
+ }
|
|
|
+ catch (Exception exception) when (exception is IOException || exception is InvalidDataException || exception is ArgumentException ||
|
|
|
+ exception is InvalidOperationException || exception is UnauthorizedAccessException) { }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ static HotPatchManifest ReadManifest(string directory, string name)
|
|
|
+ => HotPatchResourceStore.Parse(File.ReadAllText(HotPatchResourcePackage.RequireChild(directory, Path.Combine(directory, name))));
|
|
|
+
|
|
|
+ public static void Apply(bool localTesting)
|
|
|
+ {
|
|
|
+ if (HotPatchBuild.IsBuilding || S3UploadController.IsUploading || EditorApplication.isCompiling ||
|
|
|
+ EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer)
|
|
|
+ throw new InvalidOperationException("请等待构建或上传完成,并退出 Play Mode 后再切换资源。");
|
|
|
+ var source = FindLatest() ?? throw new InvalidOperationException("没有可用的最近打包资源,请先执行一键打包资源。");
|
|
|
+ ApplyPackage(source, Application.streamingAssetsPath, Path.GetFullPath("Library/HotPatch/Staging"), Application.dataPath, localTesting);
|
|
|
+ SetLocalMode(localTesting);
|
|
|
+ AssetDatabase.Refresh();
|
|
|
+ Debug.Log("[资源构建] " + (localTesting ? "已使用本地更新资源,Launch 将跳过远程热更新。" : "已恢复正式原始资源,Launch 将正常检查远程更新。") + " 来源: " + source);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void ApplyPackage(string source, string destination, string stagingParent, string destinationParent, bool localTesting)
|
|
|
+ {
|
|
|
+ var staging = Path.Combine(stagingParent, Guid.NewGuid().ToString("N"));
|
|
|
+ try
|
|
|
+ {
|
|
|
+ HotPatchResourcePackage.CopyOriginal(source, staging, localTesting ? HotPatchResourcePackage.HotManifestFileName : "manifest.json");
|
|
|
+ HotPatchResourcePackage.ReplaceDirectory(staging, destination, stagingParent, destinationParent);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ if (Directory.Exists(staging)) HotPatchResourcePackage.DeleteDirectory(staging, stagingParent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void SetLocalMode(bool enabled)
|
|
|
+ {
|
|
|
+ var settings = Resources.Load<AppSettings>("AppSettings") ?? throw new InvalidOperationException("AppSettings 不存在。");
|
|
|
+ settings.UseLocalHotPatchResources = enabled;
|
|
|
+ EditorUtility.SetDirty(settings);
|
|
|
+ AssetDatabase.SaveAssetIfDirty(settings);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|