using System.IO; using Cysharp.Threading.Tasks; using UnityEngine; using YooAsset; namespace HotMain.SHGame.YooAsset { public class RemoteServices : IRemoteServices { private string defaultHostServer; private string fallbackHostServer; public RemoteServices(string defaultHostServer, string fallbackHostServer) { this.defaultHostServer = defaultHostServer; this.fallbackHostServer = fallbackHostServer; } public string GetRemoteMainURL(string fileName) { return $"{defaultHostServer}/{fileName}"; } public string GetRemoteFallbackURL(string fileName) { return $"{fallbackHostServer}/{fileName}"; } } /// /// 资源文件解密流 /// public class BundleStream : FileStream { public const byte KEY = 64; public BundleStream(string path, FileMode mode, FileAccess access, FileShare share) : base(path, mode, access, share) { } public BundleStream(string path, FileMode mode) : base(path, mode) { } public override int Read(byte[] array, int offset, int count) { var index = base.Read(array, offset, count); for (int i = 0; i < array.Length; i++) { array[i] ^= KEY; } return index; } } /// /// 资源文件流加载解密类 /// public class FileStreamDecryption : IDecryptionServices { /// /// 同步方式获取解密的资源包对象 /// 注意:加载流对象在资源包对象释放的时候会自动释放 /// AssetBundle IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo, out Stream managedStream) { BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read); managedStream = bundleStream; return AssetBundle.LoadFromStream(bundleStream, fileInfo.ConentCRC, GetManagedReadBufferSize()); } /// /// 异步方式获取解密的资源包对象 /// 注意:加载流对象在资源包对象释放的时候会自动释放 /// AssetBundleCreateRequest IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream) { BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read); managedStream = bundleStream; return AssetBundle.LoadFromStreamAsync(bundleStream, fileInfo.ConentCRC, GetManagedReadBufferSize()); } private static uint GetManagedReadBufferSize() { return 1024; } } }