using System; using System.Collections.Generic; using System.IO; using System.Threading; using Cysharp.Threading.Tasks; using UnityEngine; namespace HotPatch { public static class HotPatchBundleLoader { public static readonly IReadOnlyList AotAssemblyNames = Array.AsReadOnly(new[] { "mscorlib.dll", "Newtonsoft.Json.dll", "System.Core.dll", "UniTask.dll" }); public static async UniTask LoadAsync(string path, string kind, CancellationToken ct) { ct.ThrowIfCancellationRequested(); HotPatchLog.Info("bundle_load_start kind=" + kind + " path=" + path); var bundle = await AssetBundle.LoadFromFileAsync(path).ToUniTask(); if (ct.IsCancellationRequested) { if (bundle != null) bundle.Unload(true); ct.ThrowIfCancellationRequested(); } if (bundle == null) throw new InvalidDataException("Failed to load AssetBundle: " + path); HotPatchLog.Info("bundle_load_complete kind=" + kind + " name=" + bundle.name); return bundle; } public static async UniTask ReadDllAsync(AssetBundle bundle, string dllName, CancellationToken ct, HotPatchResource expected = null) { ct.ThrowIfCancellationRequested(); var assetName = dllName + ".bytes"; var asset = await bundle.LoadAssetAsync(assetName).ToUniTask() as TextAsset; ct.ThrowIfCancellationRequested(); if (asset == null) throw new InvalidDataException("DLL missing from bundle: " + assetName); var bytes = asset.bytes; if (expected != null && (bytes.LongLength != expected.size || !string.Equals(HotPatchHash.Of(bytes), expected.sha256, StringComparison.OrdinalIgnoreCase))) throw new InvalidDataException("DLL verification failed inside bundle: " + dllName); HotPatchLog.Info("bundle_dll_read name=" + dllName + " bytes=" + bytes.Length); return bytes; } } }