diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs index 9017fd8..bdf1383 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheFileInfo.cs @@ -3,34 +3,34 @@ using System.IO; namespace YooAsset { - internal class CacheFileInfo - { - private static readonly BufferWriter SharedBuffer = new BufferWriter(1024); + internal class CacheFileInfo + { + private static readonly BufferWriter SharedBuffer = new BufferWriter(1024); - /// - /// 写入资源包信息 - /// - public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize) - { - using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) - { - SharedBuffer.Clear(); - SharedBuffer.WriteUTF8(dataFileCRC); - SharedBuffer.WriteInt64(dataFileSize); - SharedBuffer.WriteToStream(fs); - fs.Flush(); - } - } + /// + /// 写入资源包信息 + /// + public static void WriteInfoToFile(string filePath, string dataFileCRC, long dataFileSize) + { + using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) + { + SharedBuffer.Clear(); + SharedBuffer.WriteUTF8(dataFileCRC); + SharedBuffer.WriteInt64(dataFileSize); + SharedBuffer.WriteToStream(fs); + fs.Flush(); + } + } - /// - /// 读取资源包信息 - /// - public static void ReadInfoFromFile(string filePath, out string dataFileCRC, out long dataFileSize) - { - byte[] binaryData = FileUtility.ReadAllBytes(filePath); - BufferReader buffer = new BufferReader(binaryData); - dataFileCRC = buffer.ReadUTF8(); - dataFileSize = buffer.ReadInt64(); - } - } + /// + /// 读取资源包信息 + /// + public static void ReadInfoFromFile(string filePath, out string dataFileCRC, out long dataFileSize) + { + byte[] binaryData = FileUtility.ReadAllBytes(filePath); + BufferReader buffer = new BufferReader(binaryData); + dataFileCRC = buffer.ReadUTF8(); + dataFileSize = buffer.ReadInt64(); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs index 23894c5..3242708 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheHelper.cs @@ -6,98 +6,98 @@ using System.Diagnostics; namespace YooAsset { - internal static class CacheHelper - { - /// - /// 禁用Unity缓存系统在WebGL平台 - /// - public static bool DisableUnityCacheOnWebGL = false; + internal static class CacheHelper + { + /// + /// 禁用Unity缓存系统在WebGL平台 + /// + public static bool DisableUnityCacheOnWebGL = false; - /// - /// 验证缓存文件(子线程内操作) - /// - public static EVerifyResult VerifyingCacheFile(VerifyCacheFileElement element, EVerifyLevel verifyLevel) - { - try - { - if (verifyLevel == EVerifyLevel.Low) - { - if (File.Exists(element.InfoFilePath) == false) - return EVerifyResult.InfoFileNotExisted; - if (File.Exists(element.DataFilePath) == false) - return EVerifyResult.DataFileNotExisted; - return EVerifyResult.Succeed; - } - else - { - if (File.Exists(element.InfoFilePath) == false) - return EVerifyResult.InfoFileNotExisted; + /// + /// 验证缓存文件(子线程内操作) + /// + public static EVerifyResult VerifyingCacheFile(VerifyCacheFileElement element, EVerifyLevel verifyLevel) + { + try + { + if (verifyLevel == EVerifyLevel.Low) + { + if (File.Exists(element.InfoFilePath) == false) + return EVerifyResult.InfoFileNotExisted; + if (File.Exists(element.DataFilePath) == false) + return EVerifyResult.DataFileNotExisted; + return EVerifyResult.Succeed; + } + else + { + if (File.Exists(element.InfoFilePath) == false) + return EVerifyResult.InfoFileNotExisted; - // 解析信息文件获取验证数据 - CacheFileInfo.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); - } - } - catch (Exception) - { - return EVerifyResult.Exception; - } + // 解析信息文件获取验证数据 + CacheFileInfo.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize); + } + } + catch (Exception) + { + return EVerifyResult.Exception; + } - return VerifyingInternal(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel); - } + return VerifyingInternal(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel); + } - /// - /// 验证下载文件(子线程内操作) - /// - public static EVerifyResult VerifyingTempFile(VerifyTempFileElement element) - { - return VerifyingInternal(element.TempDataFilePath, element.FileSize, element.FileCRC, EVerifyLevel.High); - } + /// + /// 验证下载文件(子线程内操作) + /// + public static EVerifyResult VerifyingTempFile(VerifyTempFileElement element) + { + return VerifyingInternal(element.TempDataFilePath, element.FileSize, element.FileCRC, EVerifyLevel.High); + } - /// - /// 验证记录文件(主线程内操作) - /// - public static EVerifyResult VerifyingRecordFile(CacheManager cache, string cacheGUID) - { - var wrapper = cache.TryGetWrapper(cacheGUID); - if (wrapper == null) - return EVerifyResult.CacheNotFound; + /// + /// 验证记录文件(主线程内操作) + /// + public static EVerifyResult VerifyingRecordFile(CacheManager cache, string cacheGUID) + { + var wrapper = cache.TryGetWrapper(cacheGUID); + if (wrapper == null) + return EVerifyResult.CacheNotFound; - EVerifyResult result = VerifyingInternal(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EVerifyLevel.High); - return result; - } + EVerifyResult result = VerifyingInternal(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EVerifyLevel.High); + return result; + } - private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel) - { - try - { - if (File.Exists(filePath) == false) - return EVerifyResult.DataFileNotExisted; + private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel) + { + try + { + if (File.Exists(filePath) == false) + return EVerifyResult.DataFileNotExisted; - // 先验证文件大小 - long size = FileUtility.GetFileSize(filePath); - if (size < fileSize) - return EVerifyResult.FileNotComplete; - else if (size > fileSize) - return EVerifyResult.FileOverflow; + // 先验证文件大小 + long size = FileUtility.GetFileSize(filePath); + if (size < fileSize) + return EVerifyResult.FileNotComplete; + else if (size > fileSize) + return EVerifyResult.FileOverflow; - // 再验证文件CRC - if (verifyLevel == EVerifyLevel.High) - { - string crc = HashUtility.FileCRC32(filePath); - if (crc == fileCRC) - return EVerifyResult.Succeed; - else - return EVerifyResult.FileCrcError; - } - else - { - return EVerifyResult.Succeed; - } - } - catch (Exception) - { - return EVerifyResult.Exception; - } - } - } + // 再验证文件CRC + if (verifyLevel == EVerifyLevel.High) + { + string crc = HashUtility.FileCRC32(filePath); + if (crc == fileCRC) + return EVerifyResult.Succeed; + else + return EVerifyResult.FileCrcError; + } + else + { + return EVerifyResult.Succeed; + } + } + catch (Exception) + { + return EVerifyResult.Exception; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheManager.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheManager.cs index 71ef9ac..9b94fc2 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheManager.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheManager.cs @@ -5,132 +5,132 @@ using System.Collections.Generic; namespace YooAsset { - internal class CacheManager - { - internal class RecordWrapper - { - public string InfoFilePath { private set; get; } - public string DataFilePath { private set; get; } - public string DataFileCRC { private set; get; } - public long DataFileSize { private set; get; } + internal class CacheManager + { + internal class RecordWrapper + { + public string InfoFilePath { private set; get; } + public string DataFilePath { private set; get; } + public string DataFileCRC { private set; get; } + public long DataFileSize { private set; get; } - public RecordWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize) - { - InfoFilePath = infoFilePath; - DataFilePath = dataFilePath; - DataFileCRC = dataFileCRC; - DataFileSize = dataFileSize; - } - } + public RecordWrapper(string infoFilePath, string dataFilePath, string dataFileCRC, long dataFileSize) + { + InfoFilePath = infoFilePath; + DataFilePath = dataFilePath; + DataFileCRC = dataFileCRC; + DataFileSize = dataFileSize; + } + } - private readonly Dictionary _wrappers = new Dictionary(); + private readonly Dictionary _wrappers = new Dictionary(); - /// - /// 所属包裹 - /// - public readonly string PackageName; + /// + /// 所属包裹 + /// + public readonly string PackageName; - /// - /// 验证级别 - /// - public readonly EVerifyLevel BootVerifyLevel; + /// + /// 验证级别 + /// + public readonly EVerifyLevel BootVerifyLevel; - public CacheManager(string packageName, EVerifyLevel bootVerifyLevel) - { - PackageName = packageName; - BootVerifyLevel = bootVerifyLevel; - } + public CacheManager(string packageName, EVerifyLevel bootVerifyLevel) + { + PackageName = packageName; + BootVerifyLevel = bootVerifyLevel; + } - /// - /// 清空所有数据 - /// - public void ClearAll() - { - _wrappers.Clear(); - } + /// + /// 清空所有数据 + /// + public void ClearAll() + { + _wrappers.Clear(); + } - /// - /// 查询缓存记录 - /// - public bool IsCached(string cacheGUID) - { - return _wrappers.ContainsKey(cacheGUID); - } + /// + /// 查询缓存记录 + /// + public bool IsCached(string cacheGUID) + { + return _wrappers.ContainsKey(cacheGUID); + } - /// - /// 记录验证结果 - /// - public void Record(string cacheGUID, RecordWrapper wrapper) - { - if (_wrappers.ContainsKey(cacheGUID) == false) - { - _wrappers.Add(cacheGUID, wrapper); - } - else - { - throw new Exception("Should never get here !"); - } - } + /// + /// 记录验证结果 + /// + public void Record(string cacheGUID, RecordWrapper wrapper) + { + if (_wrappers.ContainsKey(cacheGUID) == false) + { + _wrappers.Add(cacheGUID, wrapper); + } + else + { + throw new Exception("Should never get here !"); + } + } - /// - /// 丢弃验证结果并删除缓存文件 - /// - public void Discard(string cacheGUID) - { - var wrapper = TryGetWrapper(cacheGUID); - if (wrapper != null) - { - try - { - string dataFilePath = wrapper.DataFilePath; - FileInfo fileInfo = new FileInfo(dataFilePath); - if (fileInfo.Exists) - fileInfo.Directory.Delete(true); - } - catch (Exception e) - { - YooLogger.Error($"Failed to delete cache file ! {e.Message}"); - } - } + /// + /// 丢弃验证结果并删除缓存文件 + /// + public void Discard(string cacheGUID) + { + var wrapper = TryGetWrapper(cacheGUID); + if (wrapper != null) + { + try + { + string dataFilePath = wrapper.DataFilePath; + FileInfo fileInfo = new FileInfo(dataFilePath); + if (fileInfo.Exists) + fileInfo.Directory.Delete(true); + } + catch (Exception e) + { + YooLogger.Error($"Failed to delete cache file ! {e.Message}"); + } + } - if (_wrappers.ContainsKey(cacheGUID)) - { - _wrappers.Remove(cacheGUID); - } - } + if (_wrappers.ContainsKey(cacheGUID)) + { + _wrappers.Remove(cacheGUID); + } + } - /// - /// 获取记录对象 - /// - public RecordWrapper TryGetWrapper(string cacheGUID) - { - if (_wrappers.TryGetValue(cacheGUID, out RecordWrapper value)) - return value; - else - return null; - } + /// + /// 获取记录对象 + /// + public RecordWrapper TryGetWrapper(string cacheGUID) + { + if (_wrappers.TryGetValue(cacheGUID, out RecordWrapper value)) + return value; + else + return null; + } - /// - /// 获取缓存文件总数 - /// - public int GetAllCachedFilesCount() - { - return _wrappers.Count; - } + /// + /// 获取缓存文件总数 + /// + public int GetAllCachedFilesCount() + { + return _wrappers.Count; + } - /// - /// 获取缓存GUID集合 - /// - public List GetAllCachedGUIDs() - { - List keys = new List(_wrappers.Keys.Count); - var keyCollection = _wrappers.Keys; - foreach (var key in keyCollection) - { - keys.Add(key); - } - return keys; - } - } + /// + /// 获取缓存GUID集合 + /// + public List GetAllCachedGUIDs() + { + List keys = new List(_wrappers.Keys.Count); + var keyCollection = _wrappers.Keys; + foreach (var key in keyCollection) + { + keys.Add(key); + } + return keys; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/EVerifyLevel.cs b/Assets/YooAsset/Runtime/CacheSystem/EVerifyLevel.cs index ac60421..966a922 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/EVerifyLevel.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/EVerifyLevel.cs @@ -1,24 +1,24 @@  namespace YooAsset { - /// - /// 下载文件校验等级 - /// - public enum EVerifyLevel - { - /// - /// 验证文件存在 - /// - Low, + /// + /// 下载文件校验等级 + /// + public enum EVerifyLevel + { + /// + /// 验证文件存在 + /// + Low, - /// - /// 验证文件大小 - /// - Middle, + /// + /// 验证文件大小 + /// + Middle, - /// - /// 验证文件大小和CRC - /// - High, - } + /// + /// 验证文件大小和CRC + /// + High, + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearAllCacheFilesOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearAllCacheFilesOperation.cs index 9f3068a..deb1ac8 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearAllCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearAllCacheFilesOperation.cs @@ -4,68 +4,68 @@ using System.IO; namespace YooAsset { - /// - /// 清理本地包裹所有的缓存文件 - /// - public sealed class ClearAllCacheFilesOperation : AsyncOperationBase - { - private enum ESteps - { - None, - GetAllCacheFiles, - ClearAllCacheFiles, - Done, - } + /// + /// 清理本地包裹所有的缓存文件 + /// + public sealed class ClearAllCacheFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + GetAllCacheFiles, + ClearAllCacheFiles, + Done, + } - private readonly CacheManager _cache; - private List _allCacheGUIDs; - private int _fileTotalCount = 0; - private ESteps _steps = ESteps.None; - - internal ClearAllCacheFilesOperation(CacheManager cache) - { - _cache = cache; - } - internal override void InternalOnStart() - { - _steps = ESteps.GetAllCacheFiles; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + private readonly CacheManager _cache; + private List _allCacheGUIDs; + private int _fileTotalCount = 0; + private ESteps _steps = ESteps.None; - if (_steps == ESteps.GetAllCacheFiles) - { - _allCacheGUIDs = _cache.GetAllCachedGUIDs(); - _fileTotalCount = _allCacheGUIDs.Count; - YooLogger.Log($"Found all cache file count : {_fileTotalCount}"); - _steps = ESteps.ClearAllCacheFiles; - } + internal ClearAllCacheFilesOperation(CacheManager cache) + { + _cache = cache; + } + internal override void InternalOnStart() + { + _steps = ESteps.GetAllCacheFiles; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.ClearAllCacheFiles) - { - for (int i = _allCacheGUIDs.Count - 1; i >= 0; i--) - { - string cacheGUID = _allCacheGUIDs[i]; - _cache.Discard(cacheGUID); - _allCacheGUIDs.RemoveAt(i); + if (_steps == ESteps.GetAllCacheFiles) + { + _allCacheGUIDs = _cache.GetAllCachedGUIDs(); + _fileTotalCount = _allCacheGUIDs.Count; + YooLogger.Log($"Found all cache file count : {_fileTotalCount}"); + _steps = ESteps.ClearAllCacheFiles; + } - if (OperationSystem.IsBusy) - break; - } + if (_steps == ESteps.ClearAllCacheFiles) + { + for (int i = _allCacheGUIDs.Count - 1; i >= 0; i--) + { + string cacheGUID = _allCacheGUIDs[i]; + _cache.Discard(cacheGUID); + _allCacheGUIDs.RemoveAt(i); - if (_fileTotalCount == 0) - Progress = 1.0f; - else - Progress = 1.0f - (_allCacheGUIDs.Count / _fileTotalCount); + if (OperationSystem.IsBusy) + break; + } - if (_allCacheGUIDs.Count == 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - } + if (_fileTotalCount == 0) + Progress = 1.0f; + else + Progress = 1.0f - (_allCacheGUIDs.Count / _fileTotalCount); + + if (_allCacheGUIDs.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs index 1df9d20..2db369f 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/ClearUnusedCacheFilesOperation.cs @@ -4,84 +4,84 @@ using System.IO; namespace YooAsset { - /// - /// 清理本地包裹未使用的缓存文件 - /// - public sealed class ClearUnusedCacheFilesOperation : AsyncOperationBase - { - private enum ESteps - { - None, - GetUnusedCacheFiles, - ClearUnusedCacheFiles, - Done, - } + /// + /// 清理本地包裹未使用的缓存文件 + /// + public sealed class ClearUnusedCacheFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + GetUnusedCacheFiles, + ClearUnusedCacheFiles, + Done, + } - private readonly ResourcePackage _package; - private readonly CacheManager _cache; - private List _unusedCacheGUIDs; - private int _unusedFileTotalCount = 0; - private ESteps _steps = ESteps.None; + private readonly ResourcePackage _package; + private readonly CacheManager _cache; + private List _unusedCacheGUIDs; + private int _unusedFileTotalCount = 0; + private ESteps _steps = ESteps.None; - internal ClearUnusedCacheFilesOperation(ResourcePackage package, CacheManager cache) - { - _package = package; - _cache = cache; - } - internal override void InternalOnStart() - { - _steps = ESteps.GetUnusedCacheFiles; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal ClearUnusedCacheFilesOperation(ResourcePackage package, CacheManager cache) + { + _package = package; + _cache = cache; + } + internal override void InternalOnStart() + { + _steps = ESteps.GetUnusedCacheFiles; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.GetUnusedCacheFiles) - { - _unusedCacheGUIDs = GetUnusedCacheGUIDs(); - _unusedFileTotalCount = _unusedCacheGUIDs.Count; - YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}"); - _steps = ESteps.ClearUnusedCacheFiles; - } + if (_steps == ESteps.GetUnusedCacheFiles) + { + _unusedCacheGUIDs = GetUnusedCacheGUIDs(); + _unusedFileTotalCount = _unusedCacheGUIDs.Count; + YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}"); + _steps = ESteps.ClearUnusedCacheFiles; + } - if (_steps == ESteps.ClearUnusedCacheFiles) - { - for (int i = _unusedCacheGUIDs.Count - 1; i >= 0; i--) - { - string cacheGUID = _unusedCacheGUIDs[i]; - _cache.Discard(cacheGUID); - _unusedCacheGUIDs.RemoveAt(i); + if (_steps == ESteps.ClearUnusedCacheFiles) + { + for (int i = _unusedCacheGUIDs.Count - 1; i >= 0; i--) + { + string cacheGUID = _unusedCacheGUIDs[i]; + _cache.Discard(cacheGUID); + _unusedCacheGUIDs.RemoveAt(i); - if (OperationSystem.IsBusy) - break; - } + if (OperationSystem.IsBusy) + break; + } - if (_unusedFileTotalCount == 0) - Progress = 1.0f; - else - Progress = 1.0f - (_unusedCacheGUIDs.Count / _unusedFileTotalCount); + if (_unusedFileTotalCount == 0) + Progress = 1.0f; + else + Progress = 1.0f - (_unusedCacheGUIDs.Count / _unusedFileTotalCount); - if (_unusedCacheGUIDs.Count == 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } + if (_unusedCacheGUIDs.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } - private List GetUnusedCacheGUIDs() - { - var allCacheGUIDs = _cache.GetAllCachedGUIDs(); - List result = new List(allCacheGUIDs.Count); - foreach (var cacheGUID in allCacheGUIDs) - { - if (_package.IsIncludeBundleFile(cacheGUID) == false) - { - result.Add(cacheGUID); - } - } - return result; - } - } + private List GetUnusedCacheGUIDs() + { + var allCacheGUIDs = _cache.GetAllCachedGUIDs(); + List result = new List(allCacheGUIDs.Count); + foreach (var cacheGUID in allCacheGUIDs) + { + if (_package.IsIncludeBundleFile(cacheGUID) == false) + { + result.Add(cacheGUID); + } + } + return result; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/FindCacheFilesOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/FindCacheFilesOperation.cs index 7158e9b..da6d2f8 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/FindCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/FindCacheFilesOperation.cs @@ -5,127 +5,127 @@ using System.Collections.Generic; namespace YooAsset { - internal sealed class FindCacheFilesOperation : AsyncOperationBase - { - private enum ESteps - { - None, - Prepare, - UpdateCacheFiles, - Done, - } + internal sealed class FindCacheFilesOperation : AsyncOperationBase + { + private enum ESteps + { + None, + Prepare, + UpdateCacheFiles, + Done, + } - private readonly PersistentManager _persistent; - private readonly CacheManager _cache; - private IEnumerator _filesEnumerator = null; - private float _verifyStartTime; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private readonly CacheManager _cache; + private IEnumerator _filesEnumerator = null; + private float _verifyStartTime; + private ESteps _steps = ESteps.None; - /// - /// 需要验证的元素 - /// - public readonly List VerifyElements = new List(5000); + /// + /// 需要验证的元素 + /// + public readonly List VerifyElements = new List(5000); - public FindCacheFilesOperation(PersistentManager persistent, CacheManager cache) - { - _persistent = persistent; - _cache = cache; - } - internal override void InternalOnStart() - { - _steps = ESteps.Prepare; - _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public FindCacheFilesOperation(PersistentManager persistent, CacheManager cache) + { + _persistent = persistent; + _cache = cache; + } + internal override void InternalOnStart() + { + _steps = ESteps.Prepare; + _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.Prepare) - { - string rootPath = _persistent.SandboxCacheFilesRoot; - DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); - if (rootDirectory.Exists) - { - var directorieInfos = rootDirectory.EnumerateDirectories(); - _filesEnumerator = directorieInfos.GetEnumerator(); - } - _steps = ESteps.UpdateCacheFiles; - } + if (_steps == ESteps.Prepare) + { + string rootPath = _persistent.SandboxCacheFilesRoot; + DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); + if (rootDirectory.Exists) + { + var directorieInfos = rootDirectory.EnumerateDirectories(); + _filesEnumerator = directorieInfos.GetEnumerator(); + } + _steps = ESteps.UpdateCacheFiles; + } - if (_steps == ESteps.UpdateCacheFiles) - { - if (UpdateCacheFiles()) - return; + if (_steps == ESteps.UpdateCacheFiles) + { + if (UpdateCacheFiles()) + return; - // 注意:总是返回成功 - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; - YooLogger.Log($"Find cache files elapsed time {costTime:f1} seconds"); - } - } + // 注意:总是返回成功 + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; + YooLogger.Log($"Find cache files elapsed time {costTime:f1} seconds"); + } + } - private bool UpdateCacheFiles() - { - if (_filesEnumerator == null) - return false; + private bool UpdateCacheFiles() + { + if (_filesEnumerator == null) + return false; - bool isFindItem; - while (true) - { - isFindItem = _filesEnumerator.MoveNext(); - if (isFindItem == false) - break; + bool isFindItem; + while (true) + { + isFindItem = _filesEnumerator.MoveNext(); + if (isFindItem == false) + break; - var rootFoder = _filesEnumerator.Current; - var childDirectories = rootFoder.GetDirectories(); - foreach (var chidDirectory in childDirectories) - { - string cacheGUID = chidDirectory.Name; - if (_cache.IsCached(cacheGUID)) - continue; + var rootFoder = _filesEnumerator.Current; + var childDirectories = rootFoder.GetDirectories(); + foreach (var chidDirectory in childDirectories) + { + string cacheGUID = chidDirectory.Name; + if (_cache.IsCached(cacheGUID)) + continue; - // 创建验证元素类 - string fileRootPath = chidDirectory.FullName; - string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}"; - string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}"; - string dataFileExtension = FindDataFileExtension(chidDirectory); + // 创建验证元素类 + string fileRootPath = chidDirectory.FullName; + string dataFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleDataFileName}"; + string infoFilePath = $"{fileRootPath}/{ YooAssetSettings.CacheBundleInfoFileName}"; + string dataFileExtension = FindDataFileExtension(chidDirectory); - // 跳过断点续传的临时文件 - if (dataFileExtension == ".temp") - continue; + // 跳过断点续传的临时文件 + if (dataFileExtension == ".temp") + continue; - // 注意:根据配置需求数据文件会带文件格式 - if (_persistent.AppendFileExtension) - { - if (string.IsNullOrEmpty(dataFileExtension) == false) - dataFilePath += dataFileExtension; - } + // 注意:根据配置需求数据文件会带文件格式 + if (_persistent.AppendFileExtension) + { + if (string.IsNullOrEmpty(dataFileExtension) == false) + dataFilePath += dataFileExtension; + } - VerifyCacheFileElement element = new VerifyCacheFileElement(_cache.PackageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath); - VerifyElements.Add(element); - } + VerifyCacheFileElement element = new VerifyCacheFileElement(_cache.PackageName, cacheGUID, fileRootPath, dataFilePath, infoFilePath); + VerifyElements.Add(element); + } - if (OperationSystem.IsBusy) - break; - } + if (OperationSystem.IsBusy) + break; + } - return isFindItem; - } - private string FindDataFileExtension(DirectoryInfo directoryInfo) - { - string dataFileExtension = string.Empty; - var fileInfos = directoryInfo.GetFiles(); - foreach (var fileInfo in fileInfos) - { - if (fileInfo.Name.StartsWith(YooAssetSettings.CacheBundleDataFileName)) - { - dataFileExtension = fileInfo.Extension; - break; - } - } - return dataFileExtension; - } - } + return isFindItem; + } + private string FindDataFileExtension(DirectoryInfo directoryInfo) + { + string dataFileExtension = string.Empty; + var fileInfos = directoryInfo.GetFiles(); + foreach (var fileInfo in fileInfos) + { + if (fileInfo.Name.StartsWith(YooAssetSettings.CacheBundleDataFileName)) + { + dataFileExtension = fileInfo.Extension; + break; + } + } + return dataFileExtension; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyCacheFilesOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyCacheFilesOperation.cs index 56e9289..45f6d7e 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyCacheFilesOperation.cs @@ -6,249 +6,249 @@ using System.Threading; namespace YooAsset { - internal abstract class VerifyCacheFilesOperation : AsyncOperationBase - { - public static VerifyCacheFilesOperation CreateOperation(CacheManager cache, List elements) - { + internal abstract class VerifyCacheFilesOperation : AsyncOperationBase + { + public static VerifyCacheFilesOperation CreateOperation(CacheManager cache, List elements) + { #if UNITY_WEBGL var operation = new VerifyCacheFilesWithoutThreadOperation(cache, elements); #else - var operation = new VerifyCacheFilesWithThreadOperation(cache, elements); + var operation = new VerifyCacheFilesWithThreadOperation(cache, elements); #endif - return operation; - } - } + return operation; + } + } - /// - /// 本地缓存文件验证(线程版) - /// - internal class VerifyCacheFilesWithThreadOperation : VerifyCacheFilesOperation - { - private enum ESteps - { - None, - InitVerify, - UpdateVerify, - Done, - } + /// + /// 本地缓存文件验证(线程版) + /// + internal class VerifyCacheFilesWithThreadOperation : VerifyCacheFilesOperation + { + private enum ESteps + { + None, + InitVerify, + UpdateVerify, + Done, + } - private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); - private readonly CacheManager _cache; - private List _waitingList; - private List _verifyingList; - private int _verifyMaxNum; - private int _verifyTotalCount; - private float _verifyStartTime; - private int _succeedCount; - private int _failedCount; - private ESteps _steps = ESteps.None; + private readonly ThreadSyncContext _syncContext = new ThreadSyncContext(); + private readonly CacheManager _cache; + private List _waitingList; + private List _verifyingList; + private int _verifyMaxNum; + private int _verifyTotalCount; + private float _verifyStartTime; + private int _succeedCount; + private int _failedCount; + private ESteps _steps = ESteps.None; - public VerifyCacheFilesWithThreadOperation(CacheManager cache, List elements) - { - _cache = cache; - _waitingList = elements; - } - internal override void InternalOnStart() - { - _steps = ESteps.InitVerify; - _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public VerifyCacheFilesWithThreadOperation(CacheManager cache, List elements) + { + _cache = cache; + _waitingList = elements; + } + internal override void InternalOnStart() + { + _steps = ESteps.InitVerify; + _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.InitVerify) - { - int fileCount = _waitingList.Count; + if (_steps == ESteps.InitVerify) + { + int fileCount = _waitingList.Count; - // 设置同时验证的最大数 - ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads); - YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}"); - _verifyMaxNum = Math.Min(workerThreads, ioThreads); - _verifyTotalCount = fileCount; - if (_verifyMaxNum < 1) - _verifyMaxNum = 1; + // 设置同时验证的最大数 + ThreadPool.GetMaxThreads(out int workerThreads, out int ioThreads); + YooLogger.Log($"Work threads : {workerThreads}, IO threads : {ioThreads}"); + _verifyMaxNum = Math.Min(workerThreads, ioThreads); + _verifyTotalCount = fileCount; + if (_verifyMaxNum < 1) + _verifyMaxNum = 1; - _verifyingList = new List(_verifyMaxNum); - _steps = ESteps.UpdateVerify; - } + _verifyingList = new List(_verifyMaxNum); + _steps = ESteps.UpdateVerify; + } - if (_steps == ESteps.UpdateVerify) - { - _syncContext.Update(); + if (_steps == ESteps.UpdateVerify) + { + _syncContext.Update(); - Progress = GetProgress(); - if (_waitingList.Count == 0 && _verifyingList.Count == 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; - YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds"); - } + Progress = GetProgress(); + if (_waitingList.Count == 0 && _verifyingList.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; + YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds"); + } - for (int i = _waitingList.Count - 1; i >= 0; i--) - { - if (OperationSystem.IsBusy) - break; + for (int i = _waitingList.Count - 1; i >= 0; i--) + { + if (OperationSystem.IsBusy) + break; - if (_verifyingList.Count >= _verifyMaxNum) - break; + if (_verifyingList.Count >= _verifyMaxNum) + break; - var element = _waitingList[i]; - if (BeginVerifyFileWithThread(element)) - { - _waitingList.RemoveAt(i); - _verifyingList.Add(element); - } - else - { - YooLogger.Warning("The thread pool is failed queued."); - break; - } - } - } - } + var element = _waitingList[i]; + if (BeginVerifyFileWithThread(element)) + { + _waitingList.RemoveAt(i); + _verifyingList.Add(element); + } + else + { + YooLogger.Warning("The thread pool is failed queued."); + break; + } + } + } + } - private float GetProgress() - { - if (_verifyTotalCount == 0) - return 1f; - return (float)(_succeedCount + _failedCount) / _verifyTotalCount; - } - private bool BeginVerifyFileWithThread(VerifyCacheFileElement element) - { - return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element); - } - private void VerifyInThread(object obj) - { - VerifyCacheFileElement element = (VerifyCacheFileElement)obj; - element.Result = CacheHelper.VerifyingCacheFile(element, _cache.BootVerifyLevel); - _syncContext.Post(VerifyCallback, element); - } - private void VerifyCallback(object obj) - { - VerifyCacheFileElement element = (VerifyCacheFileElement)obj; - _verifyingList.Remove(element); + private float GetProgress() + { + if (_verifyTotalCount == 0) + return 1f; + return (float)(_succeedCount + _failedCount) / _verifyTotalCount; + } + private bool BeginVerifyFileWithThread(VerifyCacheFileElement element) + { + return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element); + } + private void VerifyInThread(object obj) + { + VerifyCacheFileElement element = (VerifyCacheFileElement)obj; + element.Result = CacheHelper.VerifyingCacheFile(element, _cache.BootVerifyLevel); + _syncContext.Post(VerifyCallback, element); + } + private void VerifyCallback(object obj) + { + VerifyCacheFileElement element = (VerifyCacheFileElement)obj; + _verifyingList.Remove(element); - if (element.Result == EVerifyResult.Succeed) - { - _succeedCount++; - var wrapper = new CacheManager.RecordWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); - _cache.Record(element.CacheGUID, wrapper); - } - else - { - _failedCount++; + if (element.Result == EVerifyResult.Succeed) + { + _succeedCount++; + var wrapper = new CacheManager.RecordWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); + _cache.Record(element.CacheGUID, wrapper); + } + else + { + _failedCount++; - YooLogger.Warning($"Failed verify file and delete files : {element.FileRootPath}"); - element.DeleteFiles(); - } - } - } + YooLogger.Warning($"Failed verify file and delete files : {element.FileRootPath}"); + element.DeleteFiles(); + } + } + } - /// - /// 本地缓存文件验证(非线程版) - /// - internal class VerifyCacheFilesWithoutThreadOperation : VerifyCacheFilesOperation - { - private enum ESteps - { - None, - InitVerify, - UpdateVerify, - Done, - } + /// + /// 本地缓存文件验证(非线程版) + /// + internal class VerifyCacheFilesWithoutThreadOperation : VerifyCacheFilesOperation + { + private enum ESteps + { + None, + InitVerify, + UpdateVerify, + Done, + } - private readonly CacheManager _cache; - private List _waitingList; - private List _verifyingList; - private int _verifyMaxNum; - private int _verifyTotalCount; - private float _verifyStartTime; - private int _succeedCount; - private int _failedCount; - private ESteps _steps = ESteps.None; - - public VerifyCacheFilesWithoutThreadOperation(CacheManager cache, List elements) - { - _cache = cache; - _waitingList = elements; - } - internal override void InternalOnStart() - { - _steps = ESteps.InitVerify; - _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + private readonly CacheManager _cache; + private List _waitingList; + private List _verifyingList; + private int _verifyMaxNum; + private int _verifyTotalCount; + private float _verifyStartTime; + private int _succeedCount; + private int _failedCount; + private ESteps _steps = ESteps.None; - if (_steps == ESteps.InitVerify) - { - int fileCount = _waitingList.Count; + public VerifyCacheFilesWithoutThreadOperation(CacheManager cache, List elements) + { + _cache = cache; + _waitingList = elements; + } + internal override void InternalOnStart() + { + _steps = ESteps.InitVerify; + _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - // 设置同时验证的最大数 - _verifyMaxNum = fileCount; - _verifyTotalCount = fileCount; + if (_steps == ESteps.InitVerify) + { + int fileCount = _waitingList.Count; - _verifyingList = new List(_verifyMaxNum); - _steps = ESteps.UpdateVerify; - } + // 设置同时验证的最大数 + _verifyMaxNum = fileCount; + _verifyTotalCount = fileCount; - if (_steps == ESteps.UpdateVerify) - { - Progress = GetProgress(); - if (_waitingList.Count == 0 && _verifyingList.Count == 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; - YooLogger.Log($"Package verify elapsed time {costTime:f1} seconds"); - } + _verifyingList = new List(_verifyMaxNum); + _steps = ESteps.UpdateVerify; + } - for (int i = _waitingList.Count - 1; i >= 0; i--) - { - if (OperationSystem.IsBusy) - break; + if (_steps == ESteps.UpdateVerify) + { + Progress = GetProgress(); + if (_waitingList.Count == 0 && _verifyingList.Count == 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; + YooLogger.Log($"Package verify elapsed time {costTime:f1} seconds"); + } - if (_verifyingList.Count >= _verifyMaxNum) - break; + for (int i = _waitingList.Count - 1; i >= 0; i--) + { + if (OperationSystem.IsBusy) + break; - var element = _waitingList[i]; - BeginVerifyFileWithoutThread(element); - _waitingList.RemoveAt(i); - _verifyingList.Add(element); - } + if (_verifyingList.Count >= _verifyMaxNum) + break; - // 主线程内验证,可以清空列表 - _verifyingList.Clear(); - } - } + var element = _waitingList[i]; + BeginVerifyFileWithoutThread(element); + _waitingList.RemoveAt(i); + _verifyingList.Add(element); + } - private float GetProgress() - { - if (_verifyTotalCount == 0) - return 1f; - return (float)(_succeedCount + _failedCount) / _verifyTotalCount; - } - private void BeginVerifyFileWithoutThread(VerifyCacheFileElement element) - { - element.Result = CacheHelper.VerifyingCacheFile(element, _cache.BootVerifyLevel); - if (element.Result == EVerifyResult.Succeed) - { - _succeedCount++; - var wrapper = new CacheManager.RecordWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); - _cache.Record(element.CacheGUID, wrapper); - } - else - { - _failedCount++; + // 主线程内验证,可以清空列表 + _verifyingList.Clear(); + } + } - YooLogger.Warning($"Failed verify file and delete files : {element.FileRootPath}"); - element.DeleteFiles(); - } - } - } + private float GetProgress() + { + if (_verifyTotalCount == 0) + return 1f; + return (float)(_succeedCount + _failedCount) / _verifyTotalCount; + } + private void BeginVerifyFileWithoutThread(VerifyCacheFileElement element) + { + element.Result = CacheHelper.VerifyingCacheFile(element, _cache.BootVerifyLevel); + if (element.Result == EVerifyResult.Succeed) + { + _succeedCount++; + var wrapper = new CacheManager.RecordWrapper(element.InfoFilePath, element.DataFilePath, element.DataFileCRC, element.DataFileSize); + _cache.Record(element.CacheGUID, wrapper); + } + else + { + _failedCount++; + + YooLogger.Warning($"Failed verify file and delete files : {element.FileRootPath}"); + element.DeleteFiles(); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyTempFileOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyTempFileOperation.cs index 3ae558a..0b96ff6 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyTempFileOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/Internal/VerifyTempFileOperation.cs @@ -6,136 +6,136 @@ using System.Threading; namespace YooAsset { - internal abstract class VerifyTempFileOperation : AsyncOperationBase - { - public EVerifyResult VerifyResult { protected set; get; } + internal abstract class VerifyTempFileOperation : AsyncOperationBase + { + public EVerifyResult VerifyResult { protected set; get; } - public static VerifyTempFileOperation CreateOperation(VerifyTempFileElement element) - { + public static VerifyTempFileOperation CreateOperation(VerifyTempFileElement element) + { #if UNITY_WEBGL var operation = new VerifyTempFileWithoutThreadOperation(element); #else - var operation = new VerifyTempFileWithThreadOperation(element); + var operation = new VerifyTempFileWithThreadOperation(element); #endif - return operation; - } - } + return operation; + } + } - /// - /// 下载文件验证(线程版) - /// - internal class VerifyTempFileWithThreadOperation : VerifyTempFileOperation - { - private enum ESteps - { - None, - VerifyFile, - Waiting, - Done, - } + /// + /// 下载文件验证(线程版) + /// + internal class VerifyTempFileWithThreadOperation : VerifyTempFileOperation + { + private enum ESteps + { + None, + VerifyFile, + Waiting, + Done, + } - private readonly VerifyTempFileElement _element; - private ESteps _steps = ESteps.None; + private readonly VerifyTempFileElement _element; + private ESteps _steps = ESteps.None; - public VerifyTempFileWithThreadOperation(VerifyTempFileElement element) - { - _element = element; - } - internal override void InternalOnStart() - { - _steps = ESteps.VerifyFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public VerifyTempFileWithThreadOperation(VerifyTempFileElement element) + { + _element = element; + } + internal override void InternalOnStart() + { + _steps = ESteps.VerifyFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.VerifyFile) - { - if (BeginVerifyFileWithThread(_element)) - { - _steps = ESteps.Waiting; - } - } + if (_steps == ESteps.VerifyFile) + { + if (BeginVerifyFileWithThread(_element)) + { + _steps = ESteps.Waiting; + } + } - if (_steps == ESteps.Waiting) - { - int result = _element.Result; - if (result == 0) - return; + if (_steps == ESteps.Waiting) + { + int result = _element.Result; + if (result == 0) + return; - VerifyResult = (EVerifyResult)result; - if (VerifyResult == EVerifyResult.Succeed) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Failed verify file : {_element.TempDataFilePath} ! ErrorCode : {VerifyResult}"; - } - } - } + VerifyResult = (EVerifyResult)result; + if (VerifyResult == EVerifyResult.Succeed) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Failed verify file : {_element.TempDataFilePath} ! ErrorCode : {VerifyResult}"; + } + } + } - private bool BeginVerifyFileWithThread(VerifyTempFileElement element) - { - return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element); - } - private void VerifyInThread(object obj) - { - VerifyTempFileElement element = (VerifyTempFileElement)obj; - int result = (int)CacheHelper.VerifyingTempFile(element); - element.Result = result; - } - } + private bool BeginVerifyFileWithThread(VerifyTempFileElement element) + { + return ThreadPool.QueueUserWorkItem(new WaitCallback(VerifyInThread), element); + } + private void VerifyInThread(object obj) + { + VerifyTempFileElement element = (VerifyTempFileElement)obj; + int result = (int)CacheHelper.VerifyingTempFile(element); + element.Result = result; + } + } - /// - /// 下载文件验证(非线程版) - /// - internal class VerifyTempFileWithoutThreadOperation : VerifyTempFileOperation - { - private enum ESteps - { - None, - VerifyFile, - Done, - } + /// + /// 下载文件验证(非线程版) + /// + internal class VerifyTempFileWithoutThreadOperation : VerifyTempFileOperation + { + private enum ESteps + { + None, + VerifyFile, + Done, + } - private readonly VerifyTempFileElement _element; - private ESteps _steps = ESteps.None; + private readonly VerifyTempFileElement _element; + private ESteps _steps = ESteps.None; - public VerifyTempFileWithoutThreadOperation(VerifyTempFileElement element) - { - _element = element; - } - internal override void InternalOnStart() - { - _steps = ESteps.VerifyFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public VerifyTempFileWithoutThreadOperation(VerifyTempFileElement element) + { + _element = element; + } + internal override void InternalOnStart() + { + _steps = ESteps.VerifyFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.VerifyFile) - { - _element.Result = (int)CacheHelper.VerifyingTempFile(_element); + if (_steps == ESteps.VerifyFile) + { + _element.Result = (int)CacheHelper.VerifyingTempFile(_element); - VerifyResult = (EVerifyResult)_element.Result; - if (VerifyResult == EVerifyResult.Succeed) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Failed verify file : {_element.TempDataFilePath} ! ErrorCode : {VerifyResult}"; - } - } - } - } + VerifyResult = (EVerifyResult)_element.Result; + if (VerifyResult == EVerifyResult.Succeed) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Failed verify file : {_element.TempDataFilePath} ! ErrorCode : {VerifyResult}"; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/Operation/PackageCachingOperation.cs b/Assets/YooAsset/Runtime/CacheSystem/Operation/PackageCachingOperation.cs index ad867a0..c8611bf 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/Operation/PackageCachingOperation.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/Operation/PackageCachingOperation.cs @@ -5,70 +5,70 @@ using System.Collections.Generic; namespace YooAsset { - internal class PackageCachingOperation : AsyncOperationBase - { - private enum ESteps - { - None, - FindCacheFiles, - VerifyCacheFiles, - Done, - } + internal class PackageCachingOperation : AsyncOperationBase + { + private enum ESteps + { + None, + FindCacheFiles, + VerifyCacheFiles, + Done, + } - private readonly PersistentManager _persistent; - private readonly CacheManager _cache; - private FindCacheFilesOperation _findCacheFilesOp; - private VerifyCacheFilesOperation _verifyCacheFilesOp; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private readonly CacheManager _cache; + private FindCacheFilesOperation _findCacheFilesOp; + private VerifyCacheFilesOperation _verifyCacheFilesOp; + private ESteps _steps = ESteps.None; - public PackageCachingOperation(PersistentManager persistent, CacheManager cache) - { - _persistent = persistent; - _cache = cache; - } - internal override void InternalOnStart() - { - _steps = ESteps.FindCacheFiles; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public PackageCachingOperation(PersistentManager persistent, CacheManager cache) + { + _persistent = persistent; + _cache = cache; + } + internal override void InternalOnStart() + { + _steps = ESteps.FindCacheFiles; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.FindCacheFiles) - { - if (_findCacheFilesOp == null) - { - _findCacheFilesOp = new FindCacheFilesOperation(_persistent, _cache); - OperationSystem.StartOperation(_cache.PackageName, _findCacheFilesOp); - } + if (_steps == ESteps.FindCacheFiles) + { + if (_findCacheFilesOp == null) + { + _findCacheFilesOp = new FindCacheFilesOperation(_persistent, _cache); + OperationSystem.StartOperation(_cache.PackageName, _findCacheFilesOp); + } - Progress = _findCacheFilesOp.Progress; - if (_findCacheFilesOp.IsDone == false) - return; + Progress = _findCacheFilesOp.Progress; + if (_findCacheFilesOp.IsDone == false) + return; - _steps = ESteps.VerifyCacheFiles; - } + _steps = ESteps.VerifyCacheFiles; + } - if (_steps == ESteps.VerifyCacheFiles) - { - if (_verifyCacheFilesOp == null) - { - _verifyCacheFilesOp = VerifyCacheFilesOperation.CreateOperation(_cache, _findCacheFilesOp.VerifyElements); - OperationSystem.StartOperation(_cache.PackageName, _verifyCacheFilesOp); - } + if (_steps == ESteps.VerifyCacheFiles) + { + if (_verifyCacheFilesOp == null) + { + _verifyCacheFilesOp = VerifyCacheFilesOperation.CreateOperation(_cache, _findCacheFilesOp.VerifyElements); + OperationSystem.StartOperation(_cache.PackageName, _verifyCacheFilesOp); + } - Progress = _verifyCacheFilesOp.Progress; - if (_verifyCacheFilesOp.IsDone == false) - return; + Progress = _verifyCacheFilesOp.Progress; + if (_verifyCacheFilesOp.IsDone == false) + return; - // 注意:总是返回成功 - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; + // 注意:总是返回成功 + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; - int totalCount = _cache.GetAllCachedFilesCount(); - YooLogger.Log($"Package '{_cache.PackageName}' cached files count : {totalCount}"); - } - } - } + int totalCount = _cache.GetAllCachedFilesCount(); + YooLogger.Log($"Package '{_cache.PackageName}' cached files count : {totalCount}"); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/PersistentHelper.cs b/Assets/YooAsset/Runtime/CacheSystem/PersistentHelper.cs index 8b6cbc1..ccd1b7a 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/PersistentHelper.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/PersistentHelper.cs @@ -1,15 +1,15 @@  namespace YooAsset { - internal static class PersistentHelper - { - /// - /// 获取WWW加载本地资源的路径 - /// - public static string ConvertToWWWPath(string path) - { + internal static class PersistentHelper + { + /// + /// 获取WWW加载本地资源的路径 + /// + public static string ConvertToWWWPath(string path) + { #if UNITY_EDITOR - return StringUtility.Format("file:///{0}", path); + return StringUtility.Format("file:///{0}", path); #elif UNITY_WEBGL return path; #elif UNITY_IPHONE @@ -23,6 +23,6 @@ namespace YooAsset #else return path; #endif - } - } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/PersistentManager.cs b/Assets/YooAsset/Runtime/CacheSystem/PersistentManager.cs index bc9468a..1f163e2 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/PersistentManager.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/PersistentManager.cs @@ -5,207 +5,207 @@ using System.Collections.Generic; namespace YooAsset { - internal class PersistentManager - { - private readonly Dictionary _cachedDataFilePaths = new Dictionary(10000); - private readonly Dictionary _cachedInfoFilePaths = new Dictionary(10000); - private readonly Dictionary _tempDataFilePaths = new Dictionary(10000); - private readonly Dictionary _buildinFilePaths = new Dictionary(10000); + internal class PersistentManager + { + private readonly Dictionary _cachedDataFilePaths = new Dictionary(10000); + private readonly Dictionary _cachedInfoFilePaths = new Dictionary(10000); + private readonly Dictionary _tempDataFilePaths = new Dictionary(10000); + private readonly Dictionary _buildinFilePaths = new Dictionary(10000); - /// - /// 所属包裹 - /// - public readonly string PackageName; + /// + /// 所属包裹 + /// + public readonly string PackageName; - public string BuildinRoot { private set; get; } - public string BuildinPackageRoot { private set; get; } + public string BuildinRoot { private set; get; } + public string BuildinPackageRoot { private set; get; } - public string SandboxRoot { private set; get; } - public string SandboxPackageRoot { private set; get; } - public string SandboxCacheFilesRoot { private set; get; } - public string SandboxManifestFilesRoot { private set; get; } - public string SandboxAppFootPrintFilePath { private set; get; } + public string SandboxRoot { private set; get; } + public string SandboxPackageRoot { private set; get; } + public string SandboxCacheFilesRoot { private set; get; } + public string SandboxManifestFilesRoot { private set; get; } + public string SandboxAppFootPrintFilePath { private set; get; } - public bool AppendFileExtension { private set; get; } + public bool AppendFileExtension { private set; get; } - public PersistentManager(string packageName) - { - PackageName = packageName; - } + public PersistentManager(string packageName) + { + PackageName = packageName; + } - /// - /// 初始化 - /// - public void Initialize(string buildinRoot, string sandboxRoot, bool appendFileExtension) - { - if (string.IsNullOrEmpty(buildinRoot)) - BuildinRoot = CreateDefaultBuildinRoot(); - else - BuildinRoot = buildinRoot; + /// + /// 初始化 + /// + public void Initialize(string buildinRoot, string sandboxRoot, bool appendFileExtension) + { + if (string.IsNullOrEmpty(buildinRoot)) + BuildinRoot = CreateDefaultBuildinRoot(); + else + BuildinRoot = buildinRoot; - if (string.IsNullOrEmpty(sandboxRoot)) - SandboxRoot = CreateDefaultSandboxRoot(); - else - SandboxRoot = sandboxRoot; + if (string.IsNullOrEmpty(sandboxRoot)) + SandboxRoot = CreateDefaultSandboxRoot(); + else + SandboxRoot = sandboxRoot; - BuildinPackageRoot = PathUtility.Combine(BuildinRoot, PackageName); - SandboxPackageRoot = PathUtility.Combine(SandboxRoot, PackageName); - SandboxCacheFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.CacheFilesFolderName); - SandboxManifestFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.ManifestFolderName); - SandboxAppFootPrintFilePath = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.AppFootPrintFileName); - AppendFileExtension = appendFileExtension; - } - private static string CreateDefaultBuildinRoot() - { - return PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName); - } - private static string CreateDefaultSandboxRoot() - { + BuildinPackageRoot = PathUtility.Combine(BuildinRoot, PackageName); + SandboxPackageRoot = PathUtility.Combine(SandboxRoot, PackageName); + SandboxCacheFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.CacheFilesFolderName); + SandboxManifestFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.ManifestFolderName); + SandboxAppFootPrintFilePath = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.AppFootPrintFileName); + AppendFileExtension = appendFileExtension; + } + private static string CreateDefaultBuildinRoot() + { + return PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName); + } + private static string CreateDefaultSandboxRoot() + { #if UNITY_EDITOR - // 注意:为了方便调试查看,编辑器下把存储目录放到项目里。 - string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath); - projectPath = PathUtility.RegularPath(projectPath); - return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName); + // 注意:为了方便调试查看,编辑器下把存储目录放到项目里。 + string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath); + projectPath = PathUtility.RegularPath(projectPath); + return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName); #elif UNITY_STANDALONE return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName); #else return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName); #endif - } + } - public string GetCachedDataFilePath(PackageBundle bundle) - { - if (_cachedDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) - { - string folderName = bundle.FileHash.Substring(0, 2); - filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleDataFileName); - if (AppendFileExtension) - filePath += bundle.FileExtension; - _cachedDataFilePaths.Add(bundle.CacheGUID, filePath); - } - return filePath; - } - public string GetCachedInfoFilePath(PackageBundle bundle) - { - if (_cachedInfoFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) - { - string folderName = bundle.FileHash.Substring(0, 2); - filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleInfoFileName); - _cachedInfoFilePaths.Add(bundle.CacheGUID, filePath); - } - return filePath; - } - public string GetTempDataFilePath(PackageBundle bundle) - { - if (_tempDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) - { - string cachedDataFilePath = GetCachedDataFilePath(bundle); - filePath = $"{cachedDataFilePath}.temp"; - _tempDataFilePaths.Add(bundle.CacheGUID, filePath); - } - return filePath; - } - public string GetBuildinFilePath(PackageBundle bundle) - { - if (_buildinFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) - { - filePath = PathUtility.Combine(BuildinPackageRoot, bundle.FileName); - _buildinFilePaths.Add(bundle.CacheGUID, filePath); - } - return filePath; - } + public string GetCachedDataFilePath(PackageBundle bundle) + { + if (_cachedDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) + { + string folderName = bundle.FileHash.Substring(0, 2); + filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleDataFileName); + if (AppendFileExtension) + filePath += bundle.FileExtension; + _cachedDataFilePaths.Add(bundle.CacheGUID, filePath); + } + return filePath; + } + public string GetCachedInfoFilePath(PackageBundle bundle) + { + if (_cachedInfoFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) + { + string folderName = bundle.FileHash.Substring(0, 2); + filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleInfoFileName); + _cachedInfoFilePaths.Add(bundle.CacheGUID, filePath); + } + return filePath; + } + public string GetTempDataFilePath(PackageBundle bundle) + { + if (_tempDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) + { + string cachedDataFilePath = GetCachedDataFilePath(bundle); + filePath = $"{cachedDataFilePath}.temp"; + _tempDataFilePaths.Add(bundle.CacheGUID, filePath); + } + return filePath; + } + public string GetBuildinFilePath(PackageBundle bundle) + { + if (_buildinFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false) + { + filePath = PathUtility.Combine(BuildinPackageRoot, bundle.FileName); + _buildinFilePaths.Add(bundle.CacheGUID, filePath); + } + return filePath; + } - /// - /// 删除沙盒里的包裹目录 - /// - public void DeleteSandboxPackageFolder() - { - if (Directory.Exists(SandboxPackageRoot)) - Directory.Delete(SandboxPackageRoot, true); - } + /// + /// 删除沙盒里的包裹目录 + /// + public void DeleteSandboxPackageFolder() + { + if (Directory.Exists(SandboxPackageRoot)) + Directory.Delete(SandboxPackageRoot, true); + } - /// - /// 删除沙盒内的缓存文件夹 - /// - public void DeleteSandboxCacheFilesFolder() - { - if (Directory.Exists(SandboxCacheFilesRoot)) - Directory.Delete(SandboxCacheFilesRoot, true); - } + /// + /// 删除沙盒内的缓存文件夹 + /// + public void DeleteSandboxCacheFilesFolder() + { + if (Directory.Exists(SandboxCacheFilesRoot)) + Directory.Delete(SandboxCacheFilesRoot, true); + } - /// - /// 删除沙盒内的清单文件夹 - /// - public void DeleteSandboxManifestFilesFolder() - { - if (Directory.Exists(SandboxManifestFilesRoot)) - Directory.Delete(SandboxManifestFilesRoot, true); - } + /// + /// 删除沙盒内的清单文件夹 + /// + public void DeleteSandboxManifestFilesFolder() + { + if (Directory.Exists(SandboxManifestFilesRoot)) + Directory.Delete(SandboxManifestFilesRoot, true); + } - /// - /// 获取沙盒内包裹的清单文件的路径 - /// - public string GetSandboxPackageManifestFilePath(string packageVersion) - { - string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion); - return PathUtility.Combine(SandboxManifestFilesRoot, fileName); - } + /// + /// 获取沙盒内包裹的清单文件的路径 + /// + public string GetSandboxPackageManifestFilePath(string packageVersion) + { + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion); + return PathUtility.Combine(SandboxManifestFilesRoot, fileName); + } - /// - /// 获取沙盒内包裹的哈希文件的路径 - /// - public string GetSandboxPackageHashFilePath(string packageVersion) - { - string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); - return PathUtility.Combine(SandboxManifestFilesRoot, fileName); - } + /// + /// 获取沙盒内包裹的哈希文件的路径 + /// + public string GetSandboxPackageHashFilePath(string packageVersion) + { + string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); + return PathUtility.Combine(SandboxManifestFilesRoot, fileName); + } - /// - /// 获取沙盒内包裹的版本文件的路径 - /// - public string GetSandboxPackageVersionFilePath() - { - string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName); - return PathUtility.Combine(SandboxManifestFilesRoot, fileName); - } + /// + /// 获取沙盒内包裹的版本文件的路径 + /// + public string GetSandboxPackageVersionFilePath() + { + string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName); + return PathUtility.Combine(SandboxManifestFilesRoot, fileName); + } - /// - /// 保存沙盒内默认的包裹版本 - /// - public void SaveSandboxPackageVersionFile(string version) - { - string filePath = GetSandboxPackageVersionFilePath(); - FileUtility.WriteAllText(filePath, version); - } + /// + /// 保存沙盒内默认的包裹版本 + /// + public void SaveSandboxPackageVersionFile(string version) + { + string filePath = GetSandboxPackageVersionFilePath(); + FileUtility.WriteAllText(filePath, version); + } - /// - /// 获取APP内包裹的清单文件的路径 - /// - public string GetBuildinPackageManifestFilePath(string packageVersion) - { - string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion); - return PathUtility.Combine(BuildinPackageRoot, fileName); - } + /// + /// 获取APP内包裹的清单文件的路径 + /// + public string GetBuildinPackageManifestFilePath(string packageVersion) + { + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion); + return PathUtility.Combine(BuildinPackageRoot, fileName); + } - /// - /// 获取APP内包裹的哈希文件的路径 - /// - public string GetBuildinPackageHashFilePath(string packageVersion) - { - string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); - return PathUtility.Combine(BuildinPackageRoot, fileName); - } + /// + /// 获取APP内包裹的哈希文件的路径 + /// + public string GetBuildinPackageHashFilePath(string packageVersion) + { + string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion); + return PathUtility.Combine(BuildinPackageRoot, fileName); + } - /// - /// 获取APP内包裹的版本文件的路径 - /// - public string GetBuildinPackageVersionFilePath() - { - string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName); - return PathUtility.Combine(BuildinPackageRoot, fileName); - } - } + /// + /// 获取APP内包裹的版本文件的路径 + /// + public string GetBuildinPackageVersionFilePath() + { + string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName); + return PathUtility.Combine(BuildinPackageRoot, fileName); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/CacheSystem/VerifyElement.cs b/Assets/YooAsset/Runtime/CacheSystem/VerifyElement.cs index 83ca07b..c8cbb46 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/VerifyElement.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/VerifyElement.cs @@ -2,59 +2,59 @@ namespace YooAsset { - /// - /// 缓存文件验证元素 - /// - internal class VerifyCacheFileElement - { - public string PackageName { private set; get; } - public string CacheGUID { private set; get; } - public string FileRootPath { private set; get; } - public string DataFilePath { private set; get; } - public string InfoFilePath { private set; get; } + /// + /// 缓存文件验证元素 + /// + internal class VerifyCacheFileElement + { + public string PackageName { private set; get; } + public string CacheGUID { private set; get; } + public string FileRootPath { private set; get; } + public string DataFilePath { private set; get; } + public string InfoFilePath { private set; get; } - public EVerifyResult Result; - public string DataFileCRC; - public long DataFileSize; + public EVerifyResult Result; + public string DataFileCRC; + public long DataFileSize; - public VerifyCacheFileElement(string packageName, string cacheGUID, string fileRootPath, string dataFilePath, string infoFilePath) - { - PackageName = packageName; - CacheGUID = cacheGUID; - FileRootPath = fileRootPath; - DataFilePath = dataFilePath; - InfoFilePath = infoFilePath; - } + public VerifyCacheFileElement(string packageName, string cacheGUID, string fileRootPath, string dataFilePath, string infoFilePath) + { + PackageName = packageName; + CacheGUID = cacheGUID; + FileRootPath = fileRootPath; + DataFilePath = dataFilePath; + InfoFilePath = infoFilePath; + } - public void DeleteFiles() - { - try - { - Directory.Delete(FileRootPath, true); - } - catch(System.Exception e) - { - YooLogger.Warning($"Failed delete cache bundle folder : {e}"); - } - } - } + public void DeleteFiles() + { + try + { + Directory.Delete(FileRootPath, true); + } + catch (System.Exception e) + { + YooLogger.Warning($"Failed delete cache bundle folder : {e}"); + } + } + } - /// - /// 下载文件验证元素 - /// - internal class VerifyTempFileElement - { - public string TempDataFilePath { private set; get; } - public string FileCRC { private set; get; } - public long FileSize { private set; get; } + /// + /// 下载文件验证元素 + /// + internal class VerifyTempFileElement + { + public string TempDataFilePath { private set; get; } + public string FileCRC { private set; get; } + public long FileSize { private set; get; } - public int Result = 0; // 注意:原子操作对象 + public int Result = 0; // 注意:原子操作对象 - public VerifyTempFileElement(string tempDataFilePath, string fileCRC, long fileSize) - { - TempDataFilePath = tempDataFilePath; - FileCRC = fileCRC; - FileSize = fileSize; - } - } + public VerifyTempFileElement(string tempDataFilePath, string fileCRC, long fileSize) + { + TempDataFilePath = tempDataFilePath; + FileCRC = fileCRC; + FileSize = fileSize; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs index 68ef204..fa564c4 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs @@ -4,36 +4,36 @@ using System.Collections.Generic; namespace YooAsset { - [Serializable] - internal class DebugBundleInfo : IComparer, IComparable - { - /// - /// 包裹名 - /// - public string PackageName { set; get; } + [Serializable] + internal class DebugBundleInfo : IComparer, IComparable + { + /// + /// 包裹名 + /// + public string PackageName { set; get; } - /// - /// 资源包名称 - /// - public string BundleName; + /// + /// 资源包名称 + /// + public string BundleName; - /// - /// 引用计数 - /// - public int RefCount; + /// + /// 引用计数 + /// + public int RefCount; - /// - /// 加载状态 - /// - public string Status; + /// + /// 加载状态 + /// + public string Status; - public int CompareTo(DebugBundleInfo other) - { - return Compare(this, other); - } - public int Compare(DebugBundleInfo a, DebugBundleInfo b) - { - return string.CompareOrdinal(a.BundleName, b.BundleName); - } - } + public int CompareTo(DebugBundleInfo other) + { + return Compare(this, other); + } + public int Compare(DebugBundleInfo a, DebugBundleInfo b) + { + return string.CompareOrdinal(a.BundleName, b.BundleName); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugPackageData.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugPackageData.cs index 12ac443..dd01afe 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugPackageData.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugPackageData.cs @@ -5,17 +5,17 @@ using System.Collections.Generic; namespace YooAsset { - [Serializable] - internal class DebugPackageData - { - /// - /// 包裹名称 - /// - public string PackageName; + [Serializable] + internal class DebugPackageData + { + /// + /// 包裹名称 + /// + public string PackageName; - /// - /// 调试数据列表 - /// - public List ProviderInfos = new List(1000); - } + /// + /// 调试数据列表 + /// + public List ProviderInfos = new List(1000); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugProviderInfo.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugProviderInfo.cs index 389b80e..f609a0c 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugProviderInfo.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugProviderInfo.cs @@ -4,56 +4,56 @@ using System.Collections.Generic; namespace YooAsset { - [Serializable] - internal class DebugProviderInfo : IComparer, IComparable - { - /// - /// 包裹名 - /// - public string PackageName { set; get; } + [Serializable] + internal class DebugProviderInfo : IComparer, IComparable + { + /// + /// 包裹名 + /// + public string PackageName { set; get; } - /// - /// 资源对象路径 - /// - public string AssetPath; + /// + /// 资源对象路径 + /// + public string AssetPath; - /// - /// 资源出生的场景 - /// - public string SpawnScene; + /// + /// 资源出生的场景 + /// + public string SpawnScene; - /// - /// 资源出生的时间 - /// - public string SpawnTime; + /// + /// 资源出生的时间 + /// + public string SpawnTime; - /// - /// 加载耗时(单位:毫秒) - /// - public long LoadingTime; + /// + /// 加载耗时(单位:毫秒) + /// + public long LoadingTime; - /// - /// 引用计数 - /// - public int RefCount; + /// + /// 引用计数 + /// + public int RefCount; - /// - /// 加载状态 - /// - public string Status; + /// + /// 加载状态 + /// + public string Status; - /// - /// 依赖的资源包列表 - /// - public List DependBundleInfos; + /// + /// 依赖的资源包列表 + /// + public List DependBundleInfos; - public int CompareTo(DebugProviderInfo other) - { - return Compare(this, other); - } - public int Compare(DebugProviderInfo a, DebugProviderInfo b) - { - return string.CompareOrdinal(a.AssetPath, b.AssetPath); - } - } + public int CompareTo(DebugProviderInfo other) + { + return Compare(this, other); + } + public int Compare(DebugProviderInfo a, DebugProviderInfo b) + { + return string.CompareOrdinal(a.AssetPath, b.AssetPath); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugReport.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugReport.cs index a049f48..1a3a748 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugReport.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugReport.cs @@ -6,34 +6,34 @@ using UnityEngine; namespace YooAsset { - /// - /// 资源系统调试信息 - /// - [Serializable] - internal class DebugReport - { - /// - /// 游戏帧 - /// - public int FrameCount; + /// + /// 资源系统调试信息 + /// + [Serializable] + internal class DebugReport + { + /// + /// 游戏帧 + /// + public int FrameCount; - /// - /// 调试的包裹数据列表 - /// - public List PackageDatas = new List(10); - + /// + /// 调试的包裹数据列表 + /// + public List PackageDatas = new List(10); - /// - /// 序列化 - /// - public static byte[] Serialize(DebugReport debugReport) + + /// + /// 序列化 + /// + public static byte[] Serialize(DebugReport debugReport) { return Encoding.UTF8.GetBytes(JsonUtility.ToJson(debugReport)); } - /// - /// 反序列化 - /// + /// + /// 反序列化 + /// public static DebugReport Deserialize(byte[] data) { return JsonUtility.FromJson(Encoding.UTF8.GetString(data)); diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteCommand.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteCommand.cs index 3d706be..fabf279 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteCommand.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteCommand.cs @@ -4,42 +4,42 @@ using UnityEngine; namespace YooAsset { - internal enum ERemoteCommand - { - /// - /// 采样一次 - /// - SampleOnce = 0, - } + internal enum ERemoteCommand + { + /// + /// 采样一次 + /// + SampleOnce = 0, + } - [Serializable] - internal class RemoteCommand - { - /// - /// 命令类型 - /// - public int CommandType; + [Serializable] + internal class RemoteCommand + { + /// + /// 命令类型 + /// + public int CommandType; - /// - /// 命令附加参数 - /// - public string CommandParam; + /// + /// 命令附加参数 + /// + public string CommandParam; - /// - /// 序列化 - /// - public static byte[] Serialize(RemoteCommand command) - { - return Encoding.UTF8.GetBytes(JsonUtility.ToJson(command)); - } + /// + /// 序列化 + /// + public static byte[] Serialize(RemoteCommand command) + { + return Encoding.UTF8.GetBytes(JsonUtility.ToJson(command)); + } - /// - /// 反序列化 - /// - public static RemoteCommand Deserialize(byte[] data) - { - return JsonUtility.FromJson(Encoding.UTF8.GetString(data)); - } - } + /// + /// 反序列化 + /// + public static RemoteCommand Deserialize(byte[] data) + { + return JsonUtility.FromJson(Encoding.UTF8.GetString(data)); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerDefine.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerDefine.cs index edd66b0..c94b842 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerDefine.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerDefine.cs @@ -1,12 +1,11 @@ using System; using System.Text; -using UnityEngine; namespace YooAsset { - internal class RemoteDebuggerDefine - { - public static readonly Guid kMsgSendPlayerToEditor = new Guid("e34a5702dd353724aa315fb8011f08c3"); - public static readonly Guid kMsgSendEditorToPlayer = new Guid("4d1926c9df5b052469a1c63448b7609a"); - } + internal class RemoteDebuggerDefine + { + public static readonly Guid kMsgSendPlayerToEditor = new Guid("e34a5702dd353724aa315fb8011f08c3"); + public static readonly Guid kMsgSendEditorToPlayer = new Guid("4d1926c9df5b052469a1c63448b7609a"); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerInRuntime.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerInRuntime.cs index af2e48f..b61295d 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerInRuntime.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/RemoteDebuggerInRuntime.cs @@ -5,25 +5,25 @@ using UnityEngine.Networking.PlayerConnection; namespace YooAsset { - internal class RemoteDebuggerInRuntime : MonoBehaviour - { + internal class RemoteDebuggerInRuntime : MonoBehaviour + { #if UNITY_EDITOR - /// - /// 编辑器下获取报告的回调 - /// - public static Action EditorHandleDebugReportCallback; + /// + /// 编辑器下获取报告的回调 + /// + public static Action EditorHandleDebugReportCallback; - /// - /// 编辑器下请求报告数据 - /// - public static void EditorRequestDebugReport() - { - if(UnityEditor.EditorApplication.isPlaying) - { - var report = YooAssets.GetDebugReport(); - EditorHandleDebugReportCallback?.Invoke(0, report); - } - } + /// + /// 编辑器下请求报告数据 + /// + public static void EditorRequestDebugReport() + { + if (UnityEditor.EditorApplication.isPlaying) + { + var report = YooAssets.GetDebugReport(); + EditorHandleDebugReportCallback?.Invoke(0, report); + } + } #else private void OnEnable() { @@ -49,5 +49,5 @@ namespace YooAsset } } #endif - } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadHelper.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadHelper.cs index 8aa06e1..458529f 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadHelper.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadHelper.cs @@ -7,35 +7,35 @@ using UnityEngine.Networking; namespace YooAsset { - /// - /// 自定义下载器的请求委托 - /// - public delegate UnityWebRequest DownloadRequestDelegate(string url); + /// + /// 自定义下载器的请求委托 + /// + public delegate UnityWebRequest DownloadRequestDelegate(string url); - internal static class DownloadHelper - { - /// - /// 下载失败后清理文件的HTTP错误码 - /// - public static List ClearFileResponseCodes { set; get; } + internal static class DownloadHelper + { + /// + /// 下载失败后清理文件的HTTP错误码 + /// + public static List ClearFileResponseCodes { set; get; } - /// - /// 自定义下载器的请求委托 - /// - public static DownloadRequestDelegate RequestDelegate = null; + /// + /// 自定义下载器的请求委托 + /// + public static DownloadRequestDelegate RequestDelegate = null; - /// - /// 创建一个新的网络请求 - /// - public static UnityWebRequest NewRequest(string requestURL) - { - UnityWebRequest webRequest; - if (RequestDelegate != null) - webRequest = RequestDelegate.Invoke(requestURL); - else - webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET); - return webRequest; - } - } + /// + /// 创建一个新的网络请求 + /// + public static UnityWebRequest NewRequest(string requestURL) + { + UnityWebRequest webRequest; + if (RequestDelegate != null) + webRequest = RequestDelegate.Invoke(requestURL); + else + webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET); + return webRequest; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadManager.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadManager.cs index 6daf127..71a693c 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadManager.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadManager.cs @@ -6,99 +6,99 @@ using UnityEngine.Networking; namespace YooAsset { - /// - /// 1. 保证每一时刻资源文件只存在一个下载器 - /// 2. 保证下载器下载完成后立刻验证并缓存 - /// 3. 保证资源文件不会被重复下载 - /// - internal class DownloadManager - { - private readonly Dictionary _downloaders = new Dictionary(1000); - private readonly List _removeList = new List(1000); + /// + /// 1. 保证每一时刻资源文件只存在一个下载器 + /// 2. 保证下载器下载完成后立刻验证并缓存 + /// 3. 保证资源文件不会被重复下载 + /// + internal class DownloadManager + { + private readonly Dictionary _downloaders = new Dictionary(1000); + private readonly List _removeList = new List(1000); - private uint _breakpointResumeFileSize; + private uint _breakpointResumeFileSize; - /// - /// 所属包裹 - /// - public readonly string PackageName; + /// + /// 所属包裹 + /// + public readonly string PackageName; - public DownloadManager(string packageName) - { - PackageName = packageName; - } + public DownloadManager(string packageName) + { + PackageName = packageName; + } - /// - /// 初始化 - /// - public void Initialize(uint breakpointResumeFileSize) - { - _breakpointResumeFileSize = breakpointResumeFileSize; - } + /// + /// 初始化 + /// + public void Initialize(uint breakpointResumeFileSize) + { + _breakpointResumeFileSize = breakpointResumeFileSize; + } - /// - /// 更新下载器 - /// - public void Update() - { - // 更新下载器 - _removeList.Clear(); - foreach (var valuePair in _downloaders) - { - var downloader = valuePair.Value; - downloader.Update(); - if (downloader.IsDone()) - { - _removeList.Add(valuePair.Key); - } - } + /// + /// 更新下载器 + /// + public void Update() + { + // 更新下载器 + _removeList.Clear(); + foreach (var valuePair in _downloaders) + { + var downloader = valuePair.Value; + downloader.Update(); + if (downloader.IsDone()) + { + _removeList.Add(valuePair.Key); + } + } - // 移除下载器 - foreach (var key in _removeList) - { - _downloaders.Remove(key); - } - } + // 移除下载器 + foreach (var key in _removeList) + { + _downloaders.Remove(key); + } + } - /// - /// 销毁所有下载器 - /// - public void DestroyAll() - { - foreach (var valuePair in _downloaders) - { - var downloader = valuePair.Value; - downloader.Abort(); - } + /// + /// 销毁所有下载器 + /// + public void DestroyAll() + { + foreach (var valuePair in _downloaders) + { + var downloader = valuePair.Value; + downloader.Abort(); + } - _downloaders.Clear(); - _removeList.Clear(); - } + _downloaders.Clear(); + _removeList.Clear(); + } - /// - /// 创建下载器 - /// 注意:只有第一次请求的参数才有效 - /// - public DownloaderBase CreateDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60) - { - // 查询存在的下载器 - if (_downloaders.TryGetValue(bundleInfo.CachedDataFilePath, out var downloader)) - { - downloader.Reference(); - return downloader; - } + /// + /// 创建下载器 + /// 注意:只有第一次请求的参数才有效 + /// + public DownloaderBase CreateDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60) + { + // 查询存在的下载器 + if (_downloaders.TryGetValue(bundleInfo.CachedDataFilePath, out var downloader)) + { + downloader.Reference(); + return downloader; + } - // 如果资源已经缓存 - if (bundleInfo.IsCached()) - { - var completedDownloader = new CompletedDownloader(bundleInfo); - return completedDownloader; - } + // 如果资源已经缓存 + if (bundleInfo.IsCached()) + { + var completedDownloader = new CompletedDownloader(bundleInfo); + return completedDownloader; + } - // 创建新的下载器 - DownloaderBase newDownloader = null; - YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}"); + // 创建新的下载器 + DownloaderBase newDownloader = null; + YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}"); #if UNITY_WEBGL if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) { @@ -112,39 +112,39 @@ namespace YooAsset newDownloader = new WebDownloader(bundleInfo, requesterType, failedTryAgain, timeout); } #else - FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath); - bool resumeDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize; - if (resumeDownload) - { - System.Type requesterType = typeof(FileResumeRequest); - newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout); - } - else - { - System.Type requesterType = typeof(FileGeneralRequest); - newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout); - } + FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath); + bool resumeDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize; + if (resumeDownload) + { + System.Type requesterType = typeof(FileResumeRequest); + newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout); + } + else + { + System.Type requesterType = typeof(FileGeneralRequest); + newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout); + } #endif - // 返回新创建的下载器 - _downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader); - newDownloader.Reference(); - return newDownloader; - } + // 返回新创建的下载器 + _downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader); + newDownloader.Reference(); + return newDownloader; + } - /// - /// 停止不再使用的下载器 - /// - public void AbortUnusedDownloader() - { - foreach (var valuePair in _downloaders) - { - var downloader = valuePair.Value; - if (downloader.RefCount <= 0) - { - downloader.Abort(); - } - } - } - } + /// + /// 停止不再使用的下载器 + /// + public void AbortUnusedDownloader() + { + foreach (var valuePair in _downloaders) + { + var downloader = valuePair.Value; + if (downloader.RefCount <= 0) + { + downloader.Abort(); + } + } + } + } } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadStatus.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadStatus.cs index d9296ea..880d81d 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadStatus.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadStatus.cs @@ -1,36 +1,36 @@  namespace YooAsset { - public struct DownloadStatus - { - /// - /// 下载是否完成 - /// - public bool IsDone; + public struct DownloadStatus + { + /// + /// 下载是否完成 + /// + public bool IsDone; - /// - /// 下载进度(0f~1f) - /// - public float Progress; + /// + /// 下载进度(0f~1f) + /// + public float Progress; - /// - /// 需要下载的总字节数 - /// - public ulong TotalBytes; + /// + /// 需要下载的总字节数 + /// + public ulong TotalBytes; - /// - /// 已经下载的字节数 - /// - public ulong DownloadedBytes; + /// + /// 已经下载的字节数 + /// + public ulong DownloadedBytes; - public static DownloadStatus CreateDefaultStatus() - { - DownloadStatus status = new DownloadStatus(); - status.IsDone = false; - status.Progress = 0f; - status.TotalBytes = 0; - status.DownloadedBytes = 0; - return status; - } - } + public static DownloadStatus CreateDefaultStatus() + { + DownloadStatus status = new DownloadStatus(); + status.IsDone = false; + status.Progress = 0f; + status.TotalBytes = 0; + status.DownloadedBytes = 0; + return status; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/CompletedDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/CompletedDownloader.cs index a027bd8..4495409 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/CompletedDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/CompletedDownloader.cs @@ -1,29 +1,28 @@ - -using UnityEngine; +using UnityEngine; namespace YooAsset { - internal sealed class CompletedDownloader : DownloaderBase - { - public CompletedDownloader(BundleInfo bundleInfo) : base(bundleInfo, null, 0, 0) - { - DownloadProgress = 1f; - DownloadedBytes = (ulong)bundleInfo.Bundle.FileSize; - _status = EStatus.Succeed; - } + internal sealed class CompletedDownloader : DownloaderBase + { + public CompletedDownloader(BundleInfo bundleInfo) : base(bundleInfo, null, 0, 0) + { + DownloadProgress = 1f; + DownloadedBytes = (ulong)bundleInfo.Bundle.FileSize; + _status = EStatus.Succeed; + } - public override void SendRequest(params object[] param) - { - } - public override void Update() - { - } - public override void Abort() - { - } - public override AssetBundle GetAssetBundle() - { - throw new System.NotImplementedException(); - } - } + public override void SendRequest(params object[] param) + { + } + public override void Update() + { + } + public override void Abort() + { + } + public override AssetBundle GetAssetBundle() + { + throw new System.NotImplementedException(); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloaderBase.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloaderBase.cs index e64f954..df531d5 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloaderBase.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/DownloaderBase.cs @@ -4,179 +4,179 @@ using UnityEngine.Networking; namespace YooAsset { - internal abstract class DownloaderBase - { - public enum EStatus - { - None = 0, - Succeed, - Failed - } + internal abstract class DownloaderBase + { + public enum EStatus + { + None = 0, + Succeed, + Failed + } - protected readonly BundleInfo _bundleInfo; - protected readonly System.Type _requesterType; - protected readonly int _timeout; - protected int _failedTryAgain; + protected readonly BundleInfo _bundleInfo; + protected readonly System.Type _requesterType; + protected readonly int _timeout; + protected int _failedTryAgain; - protected IWebRequester _requester; - protected EStatus _status = EStatus.None; - protected string _lastestNetError = string.Empty; - protected long _lastestHttpCode = 0; + protected IWebRequester _requester; + protected EStatus _status = EStatus.None; + protected string _lastestNetError = string.Empty; + protected long _lastestHttpCode = 0; - // 请求次数 - protected int _requestCount = 0; - protected string _requestURL; + // 请求次数 + protected int _requestCount = 0; + protected string _requestURL; - // 超时相关 - protected bool _isAbort = false; - protected ulong _latestDownloadBytes; - protected float _latestDownloadRealtime; - protected float _tryAgainTimer; + // 超时相关 + protected bool _isAbort = false; + protected ulong _latestDownloadBytes; + protected float _latestDownloadRealtime; + protected float _tryAgainTimer; - /// - /// 是否等待异步结束 - /// 警告:只能用于解压APP内部资源 - /// - public bool WaitForAsyncComplete = false; + /// + /// 是否等待异步结束 + /// 警告:只能用于解压APP内部资源 + /// + public bool WaitForAsyncComplete = false; - /// - /// 下载进度(0f~1f) - /// - public float DownloadProgress { protected set; get; } + /// + /// 下载进度(0f~1f) + /// + public float DownloadProgress { protected set; get; } - /// - /// 已经下载的总字节数 - /// - public ulong DownloadedBytes { protected set; get; } + /// + /// 已经下载的总字节数 + /// + public ulong DownloadedBytes { protected set; get; } - /// - /// 引用计数 - /// - public int RefCount { private set; get; } + /// + /// 引用计数 + /// + public int RefCount { private set; get; } - public DownloaderBase(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) - { - _bundleInfo = bundleInfo; - _requesterType = requesterType; - _failedTryAgain = failedTryAgain; - _timeout = timeout; - } - public abstract void SendRequest(params object[] args); - public abstract void Update(); - public abstract void Abort(); - public abstract AssetBundle GetAssetBundle(); + public DownloaderBase(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) + { + _bundleInfo = bundleInfo; + _requesterType = requesterType; + _failedTryAgain = failedTryAgain; + _timeout = timeout; + } + public abstract void SendRequest(params object[] args); + public abstract void Update(); + public abstract void Abort(); + public abstract AssetBundle GetAssetBundle(); - /// - /// 引用(引用计数递加) - /// - public void Reference() - { - RefCount++; - } + /// + /// 引用(引用计数递加) + /// + public void Reference() + { + RefCount++; + } - /// - /// 释放(引用计数递减) - /// - public void Release() - { - RefCount--; - } + /// + /// 释放(引用计数递减) + /// + public void Release() + { + RefCount--; + } - /// - /// 检测下载器是否已经完成(无论成功或失败) - /// - public bool IsDone() - { - return _status == EStatus.Succeed || _status == EStatus.Failed; - } + /// + /// 检测下载器是否已经完成(无论成功或失败) + /// + public bool IsDone() + { + return _status == EStatus.Succeed || _status == EStatus.Failed; + } - /// - /// 下载过程是否发生错误 - /// - public bool HasError() - { - return _status == EStatus.Failed; - } + /// + /// 下载过程是否发生错误 + /// + public bool HasError() + { + return _status == EStatus.Failed; + } - /// - /// 按照错误级别打印错误 - /// - public void ReportError() - { - YooLogger.Error(GetLastError()); - } + /// + /// 按照错误级别打印错误 + /// + public void ReportError() + { + YooLogger.Error(GetLastError()); + } - /// - /// 按照警告级别打印错误 - /// - public void ReportWarning() - { - YooLogger.Warning(GetLastError()); - } + /// + /// 按照警告级别打印错误 + /// + public void ReportWarning() + { + YooLogger.Warning(GetLastError()); + } - /// - /// 获取最近发生的错误信息 - /// - public string GetLastError() - { - return $"Failed to download : {_requestURL} Error : {_lastestNetError} Code : {_lastestHttpCode}"; - } + /// + /// 获取最近发生的错误信息 + /// + public string GetLastError() + { + return $"Failed to download : {_requestURL} Error : {_lastestNetError} Code : {_lastestHttpCode}"; + } - /// - /// 获取下载文件的大小 - /// - /// - public long GetDownloadFileSize() - { - return _bundleInfo.Bundle.FileSize; - } + /// + /// 获取下载文件的大小 + /// + /// + public long GetDownloadFileSize() + { + return _bundleInfo.Bundle.FileSize; + } - /// - /// 获取下载的资源包名称 - /// - public string GetDownloadBundleName() - { - return _bundleInfo.Bundle.BundleName; - } + /// + /// 获取下载的资源包名称 + /// + public string GetDownloadBundleName() + { + return _bundleInfo.Bundle.BundleName; + } - /// - /// 获取网络请求地址 - /// - protected string GetRequestURL() - { - // 轮流返回请求地址 - _requestCount++; - if (_requestCount % 2 == 0) - return _bundleInfo.RemoteFallbackURL; - else - return _bundleInfo.RemoteMainURL; - } + /// + /// 获取网络请求地址 + /// + protected string GetRequestURL() + { + // 轮流返回请求地址 + _requestCount++; + if (_requestCount % 2 == 0) + return _bundleInfo.RemoteFallbackURL; + else + return _bundleInfo.RemoteMainURL; + } - /// - /// 超时判定方法 - /// - protected void CheckTimeout() - { - // 注意:在连续时间段内无新增下载数据及判定为超时 - if (_isAbort == false) - { - if (_latestDownloadBytes != DownloadedBytes) - { - _latestDownloadBytes = DownloadedBytes; - _latestDownloadRealtime = Time.realtimeSinceStartup; - } + /// + /// 超时判定方法 + /// + protected void CheckTimeout() + { + // 注意:在连续时间段内无新增下载数据及判定为超时 + if (_isAbort == false) + { + if (_latestDownloadBytes != DownloadedBytes) + { + _latestDownloadBytes = DownloadedBytes; + _latestDownloadRealtime = Time.realtimeSinceStartup; + } - float offset = Time.realtimeSinceStartup - _latestDownloadRealtime; - if (offset > _timeout) - { - YooLogger.Warning($"Web file request timeout : {_requestURL}"); - if(_requester != null) - _requester.Abort(); - _isAbort = true; - } - } - } - } + float offset = Time.realtimeSinceStartup - _latestDownloadRealtime; + if (offset > _timeout) + { + YooLogger.Warning($"Web file request timeout : {_requestURL}"); + if (_requester != null) + _requester.Abort(); + _isAbort = true; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs index f34338f..f7a2b99 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/FileDownloader.cs @@ -6,211 +6,211 @@ using UnityEngine; namespace YooAsset { - /// - /// 文件下载器 - /// - internal sealed class FileDownloader : DownloaderBase - { - private enum ESteps - { - None, - PrepareDownload, - CreateDownloader, - CheckDownload, - VerifyTempFile, - WaitingVerifyTempFile, - CachingFile, - TryAgain, - Done, - } + /// + /// 文件下载器 + /// + internal sealed class FileDownloader : DownloaderBase + { + private enum ESteps + { + None, + PrepareDownload, + CreateDownloader, + CheckDownload, + VerifyTempFile, + WaitingVerifyTempFile, + CachingFile, + TryAgain, + Done, + } - private VerifyTempFileOperation _verifyFileOp = null; - private ESteps _steps = ESteps.None; + private VerifyTempFileOperation _verifyFileOp = null; + private ESteps _steps = ESteps.None; - public FileDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout) - { - } - public override void SendRequest(params object[] args) - { - if (_steps == ESteps.None) - { - _steps = ESteps.PrepareDownload; - } - } - public override void Update() - { - if (_steps == ESteps.None) - return; - if (IsDone()) - return; + public FileDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout) + { + } + public override void SendRequest(params object[] args) + { + if (_steps == ESteps.None) + { + _steps = ESteps.PrepareDownload; + } + } + public override void Update() + { + if (_steps == ESteps.None) + return; + if (IsDone()) + return; - // 准备下载 - if (_steps == ESteps.PrepareDownload) - { - // 获取请求地址 - _requestURL = GetRequestURL(); + // 准备下载 + if (_steps == ESteps.PrepareDownload) + { + // 获取请求地址 + _requestURL = GetRequestURL(); - // 重置变量 - DownloadProgress = 0f; - DownloadedBytes = 0; + // 重置变量 + DownloadProgress = 0f; + DownloadedBytes = 0; - // 重置变量 - _isAbort = false; - _latestDownloadBytes = 0; - _latestDownloadRealtime = Time.realtimeSinceStartup; + // 重置变量 + _isAbort = false; + _latestDownloadBytes = 0; + _latestDownloadRealtime = Time.realtimeSinceStartup; - // 重置计时器 - if (_tryAgainTimer > 0f) - YooLogger.Warning($"Try again download : {_requestURL}"); - _tryAgainTimer = 0f; + // 重置计时器 + if (_tryAgainTimer > 0f) + YooLogger.Warning($"Try again download : {_requestURL}"); + _tryAgainTimer = 0f; - _steps = ESteps.CreateDownloader; - } + _steps = ESteps.CreateDownloader; + } - // 创建下载器 - if (_steps == ESteps.CreateDownloader) - { - _requester = (IWebRequester)Activator.CreateInstance(_requesterType); - _requester.Create(_requestURL, _bundleInfo); - _steps = ESteps.CheckDownload; - } + // 创建下载器 + if (_steps == ESteps.CreateDownloader) + { + _requester = (IWebRequester)Activator.CreateInstance(_requesterType); + _requester.Create(_requestURL, _bundleInfo); + _steps = ESteps.CheckDownload; + } - // 检测下载结果 - if (_steps == ESteps.CheckDownload) - { - _requester.Update(); - DownloadedBytes = _requester.DownloadedBytes; - DownloadProgress = _requester.DownloadProgress; - if (_requester.IsDone() == false) - { - CheckTimeout(); - return; - } + // 检测下载结果 + if (_steps == ESteps.CheckDownload) + { + _requester.Update(); + DownloadedBytes = _requester.DownloadedBytes; + DownloadProgress = _requester.DownloadProgress; + if (_requester.IsDone() == false) + { + CheckTimeout(); + return; + } - _lastestNetError = _requester.RequestNetError; - _lastestHttpCode = _requester.RequestHttpCode; - if (_requester.Status != ERequestStatus.Success) - { - _steps = ESteps.TryAgain; - } - else - { - _steps = ESteps.VerifyTempFile; - } - } + _lastestNetError = _requester.RequestNetError; + _lastestHttpCode = _requester.RequestHttpCode; + if (_requester.Status != ERequestStatus.Success) + { + _steps = ESteps.TryAgain; + } + else + { + _steps = ESteps.VerifyTempFile; + } + } - // 验证下载文件 - if (_steps == ESteps.VerifyTempFile) - { - VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize); - _verifyFileOp = VerifyTempFileOperation.CreateOperation(element); - OperationSystem.StartOperation(_bundleInfo.Bundle.PackageName, _verifyFileOp); - _steps = ESteps.WaitingVerifyTempFile; - } + // 验证下载文件 + if (_steps == ESteps.VerifyTempFile) + { + VerifyTempFileElement element = new VerifyTempFileElement(_bundleInfo.TempDataFilePath, _bundleInfo.Bundle.FileCRC, _bundleInfo.Bundle.FileSize); + _verifyFileOp = VerifyTempFileOperation.CreateOperation(element); + OperationSystem.StartOperation(_bundleInfo.Bundle.PackageName, _verifyFileOp); + _steps = ESteps.WaitingVerifyTempFile; + } - // 等待验证完成 - if (_steps == ESteps.WaitingVerifyTempFile) - { - if (WaitForAsyncComplete) - _verifyFileOp.InternalOnUpdate(); + // 等待验证完成 + if (_steps == ESteps.WaitingVerifyTempFile) + { + if (WaitForAsyncComplete) + _verifyFileOp.InternalOnUpdate(); - if (_verifyFileOp.IsDone == false) - return; + if (_verifyFileOp.IsDone == false) + return; - if (_verifyFileOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.CachingFile; - } - else - { - string tempFilePath = _bundleInfo.TempDataFilePath; - if (File.Exists(tempFilePath)) - File.Delete(tempFilePath); + if (_verifyFileOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.CachingFile; + } + else + { + string tempFilePath = _bundleInfo.TempDataFilePath; + if (File.Exists(tempFilePath)) + File.Delete(tempFilePath); - _lastestNetError = _verifyFileOp.Error; - _steps = ESteps.TryAgain; - } - } + _lastestNetError = _verifyFileOp.Error; + _steps = ESteps.TryAgain; + } + } - // 缓存下载文件 - if (_steps == ESteps.CachingFile) - { - try - { - CachingFile(); - _status = EStatus.Succeed; - _steps = ESteps.Done; - } - catch (Exception e) - { - _lastestNetError = e.Message; - _steps = ESteps.TryAgain; - } - } + // 缓存下载文件 + if (_steps == ESteps.CachingFile) + { + try + { + CachingFile(); + _status = EStatus.Succeed; + _steps = ESteps.Done; + } + catch (Exception e) + { + _lastestNetError = e.Message; + _steps = ESteps.TryAgain; + } + } - // 重新尝试下载 - if (_steps == ESteps.TryAgain) - { - if (_failedTryAgain <= 0) - { - ReportError(); - _status = EStatus.Failed; - _steps = ESteps.Done; - return; - } + // 重新尝试下载 + if (_steps == ESteps.TryAgain) + { + if (_failedTryAgain <= 0) + { + ReportError(); + _status = EStatus.Failed; + _steps = ESteps.Done; + return; + } - _tryAgainTimer += Time.unscaledDeltaTime; - if (_tryAgainTimer > 1f) - { - _failedTryAgain--; - _steps = ESteps.PrepareDownload; - ReportWarning(); - } - } - } - public override void Abort() - { - if (_requester != null) - _requester.Abort(); + _tryAgainTimer += Time.unscaledDeltaTime; + if (_tryAgainTimer > 1f) + { + _failedTryAgain--; + _steps = ESteps.PrepareDownload; + ReportWarning(); + } + } + } + public override void Abort() + { + if (_requester != null) + _requester.Abort(); - if (IsDone() == false) - { - _status = EStatus.Failed; - _steps = ESteps.Done; - _lastestNetError = "user abort"; - _lastestHttpCode = 0; - } - } - public override AssetBundle GetAssetBundle() - { - throw new NotImplementedException(); - } + if (IsDone() == false) + { + _status = EStatus.Failed; + _steps = ESteps.Done; + _lastestNetError = "user abort"; + _lastestHttpCode = 0; + } + } + public override AssetBundle GetAssetBundle() + { + throw new NotImplementedException(); + } - /// - /// 缓存下载文件 - /// - private void CachingFile() - { - string tempFilePath = _bundleInfo.TempDataFilePath; - string infoFilePath = _bundleInfo.CachedInfoFilePath; - string dataFilePath = _bundleInfo.CachedDataFilePath; - string dataFileCRC = _bundleInfo.Bundle.FileCRC; - long dataFileSize = _bundleInfo.Bundle.FileSize; + /// + /// 缓存下载文件 + /// + private void CachingFile() + { + string tempFilePath = _bundleInfo.TempDataFilePath; + string infoFilePath = _bundleInfo.CachedInfoFilePath; + string dataFilePath = _bundleInfo.CachedDataFilePath; + string dataFileCRC = _bundleInfo.Bundle.FileCRC; + long dataFileSize = _bundleInfo.Bundle.FileSize; - if (File.Exists(infoFilePath)) - File.Delete(infoFilePath); - if (File.Exists(dataFilePath)) - File.Delete(dataFilePath); + if (File.Exists(infoFilePath)) + File.Delete(infoFilePath); + if (File.Exists(dataFilePath)) + File.Delete(dataFilePath); - // 移动临时文件路径 - FileInfo fileInfo = new FileInfo(tempFilePath); - fileInfo.MoveTo(dataFilePath); + // 移动临时文件路径 + FileInfo fileInfo = new FileInfo(tempFilePath); + fileInfo.MoveTo(dataFilePath); - // 写入信息文件记录验证数据 - CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize); + // 写入信息文件记录验证数据 + CacheFileInfo.WriteInfoToFile(infoFilePath, dataFileCRC, dataFileSize); - // 记录缓存文件 - _bundleInfo.CacheRecord(); - } - } + // 记录缓存文件 + _bundleInfo.CacheRecord(); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs index 87fb363..83ac7e5 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs @@ -6,135 +6,135 @@ using UnityEngine; namespace YooAsset { - internal sealed class WebDownloader : DownloaderBase - { - private enum ESteps - { - None, - PrepareDownload, - CreateDownloader, - CheckDownload, - TryAgain, - Done, - } + internal sealed class WebDownloader : DownloaderBase + { + private enum ESteps + { + None, + PrepareDownload, + CreateDownloader, + CheckDownload, + TryAgain, + Done, + } - private ESteps _steps = ESteps.None; - private bool _getAssetBundle = false; + private ESteps _steps = ESteps.None; + private bool _getAssetBundle = false; - public WebDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout) - { - } - public override void SendRequest(params object[] args) - { - if (_steps == ESteps.None) - { - if (args.Length > 0) - { - _getAssetBundle = (bool)args[0]; - } - _steps = ESteps.PrepareDownload; - } - } - public override void Update() - { - if (_steps == ESteps.None) - return; - if (IsDone()) - return; + public WebDownloader(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout) : base(bundleInfo, requesterType, failedTryAgain, timeout) + { + } + public override void SendRequest(params object[] args) + { + if (_steps == ESteps.None) + { + if (args.Length > 0) + { + _getAssetBundle = (bool)args[0]; + } + _steps = ESteps.PrepareDownload; + } + } + public override void Update() + { + if (_steps == ESteps.None) + return; + if (IsDone()) + return; - // 创建下载器 - if (_steps == ESteps.PrepareDownload) - { - // 获取请求地址 - _requestURL = GetRequestURL(); + // 创建下载器 + if (_steps == ESteps.PrepareDownload) + { + // 获取请求地址 + _requestURL = GetRequestURL(); - // 重置变量 - DownloadProgress = 0f; - DownloadedBytes = 0; + // 重置变量 + DownloadProgress = 0f; + DownloadedBytes = 0; - // 重置变量 - _isAbort = false; - _latestDownloadBytes = 0; - _latestDownloadRealtime = Time.realtimeSinceStartup; + // 重置变量 + _isAbort = false; + _latestDownloadBytes = 0; + _latestDownloadRealtime = Time.realtimeSinceStartup; - // 重置计时器 - if (_tryAgainTimer > 0f) - YooLogger.Warning($"Try again download : {_requestURL}"); - _tryAgainTimer = 0f; + // 重置计时器 + if (_tryAgainTimer > 0f) + YooLogger.Warning($"Try again download : {_requestURL}"); + _tryAgainTimer = 0f; - _steps = ESteps.CreateDownloader; - } + _steps = ESteps.CreateDownloader; + } - // 创建下载器 - if (_steps == ESteps.CreateDownloader) - { - _requester = (IWebRequester)Activator.CreateInstance(_requesterType); - _requester.Create(_requestURL, _bundleInfo, _getAssetBundle); - _steps = ESteps.CheckDownload; - } + // 创建下载器 + if (_steps == ESteps.CreateDownloader) + { + _requester = (IWebRequester)Activator.CreateInstance(_requesterType); + _requester.Create(_requestURL, _bundleInfo, _getAssetBundle); + _steps = ESteps.CheckDownload; + } - // 检测下载结果 - if (_steps == ESteps.CheckDownload) - { - _requester.Update(); - DownloadedBytes = _requester.DownloadedBytes; - DownloadProgress = _requester.DownloadProgress; - if (_requester.IsDone() == false) - { - CheckTimeout(); - return; - } + // 检测下载结果 + if (_steps == ESteps.CheckDownload) + { + _requester.Update(); + DownloadedBytes = _requester.DownloadedBytes; + DownloadProgress = _requester.DownloadProgress; + if (_requester.IsDone() == false) + { + CheckTimeout(); + return; + } - _lastestNetError = _requester.RequestNetError; - _lastestHttpCode = _requester.RequestHttpCode; - if (_requester.Status != ERequestStatus.Success) - { - _steps = ESteps.TryAgain; - } - else - { - _status = EStatus.Succeed; - _steps = ESteps.Done; - } - } + _lastestNetError = _requester.RequestNetError; + _lastestHttpCode = _requester.RequestHttpCode; + if (_requester.Status != ERequestStatus.Success) + { + _steps = ESteps.TryAgain; + } + else + { + _status = EStatus.Succeed; + _steps = ESteps.Done; + } + } - // 重新尝试下载 - if (_steps == ESteps.TryAgain) - { - if (_failedTryAgain <= 0) - { - ReportError(); - _status = EStatus.Failed; - _steps = ESteps.Done; - return; - } + // 重新尝试下载 + if (_steps == ESteps.TryAgain) + { + if (_failedTryAgain <= 0) + { + ReportError(); + _status = EStatus.Failed; + _steps = ESteps.Done; + return; + } - _tryAgainTimer += Time.unscaledDeltaTime; - if (_tryAgainTimer > 1f) - { - _failedTryAgain--; - _steps = ESteps.PrepareDownload; - ReportWarning(); - YooLogger.Warning($"Try again download : {_requestURL}"); - } - } - } - public override void Abort() - { - if (_requester != null) - _requester.Abort(); + _tryAgainTimer += Time.unscaledDeltaTime; + if (_tryAgainTimer > 1f) + { + _failedTryAgain--; + _steps = ESteps.PrepareDownload; + ReportWarning(); + YooLogger.Warning($"Try again download : {_requestURL}"); + } + } + } + public override void Abort() + { + if (_requester != null) + _requester.Abort(); - if (IsDone() == false) - { - _status = EStatus.Failed; - _steps = ESteps.Done; - _lastestNetError = "user abort"; - _lastestHttpCode = 0; - } - } - public override AssetBundle GetAssetBundle() - { - return (AssetBundle)_requester.GetRequestObject(); - } - } + if (IsDone() == false) + { + _status = EStatus.Failed; + _steps = ESteps.Done; + _lastestNetError = "user abort"; + _lastestHttpCode = 0; + } + } + public override AssetBundle GetAssetBundle() + { + return (AssetBundle)_requester.GetRequestObject(); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs b/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs index 22f4652..3f99b6f 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs @@ -4,33 +4,33 @@ using System.Collections.Generic; namespace YooAsset { - public class RequestHelper - { - /// - /// 记录网络请求失败事件的次数 - /// - private static readonly Dictionary _requestFailedRecorder = new Dictionary(1000); + public class RequestHelper + { + /// + /// 记录网络请求失败事件的次数 + /// + private static readonly Dictionary _requestFailedRecorder = new Dictionary(1000); - /// - /// 记录请求失败事件 - /// - public static void RecordRequestFailed(string packageName, string eventName) - { - string key = $"{packageName}_{eventName}"; - if (_requestFailedRecorder.ContainsKey(key) == false) - _requestFailedRecorder.Add(key, 0); - _requestFailedRecorder[key]++; - } + /// + /// 记录请求失败事件 + /// + public static void RecordRequestFailed(string packageName, string eventName) + { + string key = $"{packageName}_{eventName}"; + if (_requestFailedRecorder.ContainsKey(key) == false) + _requestFailedRecorder.Add(key, 0); + _requestFailedRecorder[key]++; + } - /// - /// 获取请求失败的次数 - /// - public static int GetRequestFailedCount(string packageName, string eventName) - { - string key = $"{packageName}_{eventName}"; - if (_requestFailedRecorder.ContainsKey(key) == false) - _requestFailedRecorder.Add(key, 0); - return _requestFailedRecorder[key]; - } - } + /// + /// 获取请求失败的次数 + /// + public static int GetRequestFailedCount(string packageName, string eventName) + { + string key = $"{packageName}_{eventName}"; + if (_requestFailedRecorder.ContainsKey(key) == false) + _requestFailedRecorder.Add(key, 0); + return _requestFailedRecorder[key]; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Requester/AssetBundleWebRequest.cs b/Assets/YooAsset/Runtime/DownloadSystem/Requester/AssetBundleWebRequest.cs index 234f7d8..797b8c7 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Requester/AssetBundleWebRequest.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Requester/AssetBundleWebRequest.cs @@ -4,76 +4,76 @@ using UnityEngine.Networking; namespace YooAsset { - internal class AssetBundleWebRequest : IWebRequester - { - private UnityWebRequest _webRequest; - private DownloadHandlerAssetBundle _downloadhandler; - private AssetBundle _cacheAssetBundle; - private bool _getAssetBundle = false; + internal class AssetBundleWebRequest : IWebRequester + { + private UnityWebRequest _webRequest; + private DownloadHandlerAssetBundle _downloadhandler; + private AssetBundle _cacheAssetBundle; + private bool _getAssetBundle = false; - public ERequestStatus Status { private set; get; } = ERequestStatus.None; - public float DownloadProgress { private set; get; } - public ulong DownloadedBytes { private set; get; } - public string RequestNetError { private set; get; } - public long RequestHttpCode { private set; get; } + public ERequestStatus Status { private set; get; } = ERequestStatus.None; + public float DownloadProgress { private set; get; } + public ulong DownloadedBytes { private set; get; } + public string RequestNetError { private set; get; } + public long RequestHttpCode { private set; get; } - public AssetBundleWebRequest() { } - public void Create(string requestURL, BundleInfo bundleInfo, params object[] args) - { - if (Status != ERequestStatus.None) - throw new System.Exception("Should never get here !"); + public AssetBundleWebRequest() { } + public void Create(string requestURL, BundleInfo bundleInfo, params object[] args) + { + if (Status != ERequestStatus.None) + throw new System.Exception("Should never get here !"); - if (args.Length == 0) - throw new System.Exception("Not found param value"); + if (args.Length == 0) + throw new System.Exception("Not found param value"); - // 解析附加参数 - _getAssetBundle = (bool)args[0]; + // 解析附加参数 + _getAssetBundle = (bool)args[0]; - // 创建下载器 - _webRequest = DownloadHelper.NewRequest(requestURL); - if (CacheHelper.DisableUnityCacheOnWebGL) - { - uint crc = bundleInfo.Bundle.UnityCRC; - _downloadhandler = new DownloadHandlerAssetBundle(requestURL, crc); - } - else - { - uint crc = bundleInfo.Bundle.UnityCRC; - var hash = Hash128.Parse(bundleInfo.Bundle.FileHash); - _downloadhandler = new DownloadHandlerAssetBundle(requestURL, hash, crc); - } + // 创建下载器 + _webRequest = DownloadHelper.NewRequest(requestURL); + if (CacheHelper.DisableUnityCacheOnWebGL) + { + uint crc = bundleInfo.Bundle.UnityCRC; + _downloadhandler = new DownloadHandlerAssetBundle(requestURL, crc); + } + else + { + uint crc = bundleInfo.Bundle.UnityCRC; + var hash = Hash128.Parse(bundleInfo.Bundle.FileHash); + _downloadhandler = new DownloadHandlerAssetBundle(requestURL, hash, crc); + } #if UNITY_2020_3_OR_NEWER - _downloadhandler.autoLoadAssetBundle = false; + _downloadhandler.autoLoadAssetBundle = false; #endif - _webRequest.downloadHandler = _downloadhandler; - _webRequest.disposeDownloadHandlerOnDispose = true; - _webRequest.SendWebRequest(); - Status = ERequestStatus.InProgress; - } - public void Update() - { - if (Status == ERequestStatus.None) - return; - if (IsDone()) - return; + _webRequest.downloadHandler = _downloadhandler; + _webRequest.disposeDownloadHandlerOnDispose = true; + _webRequest.SendWebRequest(); + Status = ERequestStatus.InProgress; + } + public void Update() + { + if (Status == ERequestStatus.None) + return; + if (IsDone()) + return; - DownloadProgress = _webRequest.downloadProgress; - DownloadedBytes = _webRequest.downloadedBytes; - if (_webRequest.isDone == false) - return; + DownloadProgress = _webRequest.downloadProgress; + DownloadedBytes = _webRequest.downloadedBytes; + if (_webRequest.isDone == false) + return; - // 检查网络错误 + // 检查网络错误 #if UNITY_2020_3_OR_NEWER - RequestHttpCode = _webRequest.responseCode; - if (_webRequest.result != UnityWebRequest.Result.Success) - { - RequestNetError = _webRequest.error; - Status = ERequestStatus.Error; - } - else - { - Status = ERequestStatus.Success; - } + RequestHttpCode = _webRequest.responseCode; + if (_webRequest.result != UnityWebRequest.Result.Success) + { + RequestNetError = _webRequest.error; + Status = ERequestStatus.Error; + } + else + { + Status = ERequestStatus.Success; + } #else RequestHttpCode = _webRequest.responseCode; if (_webRequest.isNetworkError || _webRequest.isHttpError) @@ -87,59 +87,59 @@ namespace YooAsset } #endif - // 缓存加载的AssetBundle对象 - if (Status == ERequestStatus.Success) - { - if (_getAssetBundle) - { - _cacheAssetBundle = _downloadhandler.assetBundle; - if (_cacheAssetBundle == null) - { - RequestNetError = "assetBundle is null"; - Status = ERequestStatus.Error; - } - } - } + // 缓存加载的AssetBundle对象 + if (Status == ERequestStatus.Success) + { + if (_getAssetBundle) + { + _cacheAssetBundle = _downloadhandler.assetBundle; + if (_cacheAssetBundle == null) + { + RequestNetError = "assetBundle is null"; + Status = ERequestStatus.Error; + } + } + } - // 最终释放下载器 - DisposeWebRequest(); - } - public void Abort() - { - // 如果下载任务还未开始 - if (Status == ERequestStatus.None) - { - RequestNetError = "user cancel"; - Status = ERequestStatus.Error; - } - else - { - // 注意:为了防止同一个文件强制停止之后立马创建新的请求,应该让进行中的请求自然终止。 - if (_webRequest != null) - { - if (_webRequest.isDone == false) - _webRequest.Abort(); // If in progress, halts the UnityWebRequest as soon as possible. - } - } - } - public bool IsDone() - { - if (Status == ERequestStatus.Success || Status == ERequestStatus.Error) - return true; - else - return false; - } - public object GetRequestObject() - { - return _cacheAssetBundle; - } - private void DisposeWebRequest() - { - if (_webRequest != null) - { - _webRequest.Dispose(); - _webRequest = null; - } - } - } + // 最终释放下载器 + DisposeWebRequest(); + } + public void Abort() + { + // 如果下载任务还未开始 + if (Status == ERequestStatus.None) + { + RequestNetError = "user cancel"; + Status = ERequestStatus.Error; + } + else + { + // 注意:为了防止同一个文件强制停止之后立马创建新的请求,应该让进行中的请求自然终止。 + if (_webRequest != null) + { + if (_webRequest.isDone == false) + _webRequest.Abort(); // If in progress, halts the UnityWebRequest as soon as possible. + } + } + } + public bool IsDone() + { + if (Status == ERequestStatus.Success || Status == ERequestStatus.Error) + return true; + else + return false; + } + public object GetRequestObject() + { + return _cacheAssetBundle; + } + private void DisposeWebRequest() + { + if (_webRequest != null) + { + _webRequest.Dispose(); + _webRequest = null; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Requester/DownloadHandlerFileRange.cs b/Assets/YooAsset/Runtime/DownloadSystem/Requester/DownloadHandlerFileRange.cs index f439e47..c2e11f4 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Requester/DownloadHandlerFileRange.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Requester/DownloadHandlerFileRange.cs @@ -5,83 +5,83 @@ using UnityEngine.Networking; namespace YooAsset { - /// - /// 支持Unity2018版本的断点续传下载器 - /// - internal class DownloadHandlerFileRange : DownloadHandlerScript - { - private string _fileSavePath; - private long _fileTotalSize; - private UnityWebRequest _webRequest; - private FileStream _fileStream; + /// + /// 支持Unity2018版本的断点续传下载器 + /// + internal class DownloadHandlerFileRange : DownloadHandlerScript + { + private string _fileSavePath; + private long _fileTotalSize; + private UnityWebRequest _webRequest; + private FileStream _fileStream; - private long _localFileSize = 0; - private long _curFileSize = 0; + private long _localFileSize = 0; + private long _curFileSize = 0; - public DownloadHandlerFileRange(string fileSavePath, long fileTotalSize, UnityWebRequest webRequest) : base(new byte[1024 * 1024]) - { - _fileSavePath = fileSavePath; - _fileTotalSize = fileTotalSize; - _webRequest = webRequest; + public DownloadHandlerFileRange(string fileSavePath, long fileTotalSize, UnityWebRequest webRequest) : base(new byte[1024 * 1024]) + { + _fileSavePath = fileSavePath; + _fileTotalSize = fileTotalSize; + _webRequest = webRequest; - if (File.Exists(fileSavePath)) - { - FileInfo fileInfo = new FileInfo(fileSavePath); - _localFileSize = fileInfo.Length; - } + if (File.Exists(fileSavePath)) + { + FileInfo fileInfo = new FileInfo(fileSavePath); + _localFileSize = fileInfo.Length; + } - _fileStream = new FileStream(_fileSavePath, FileMode.Append, FileAccess.Write); - _curFileSize = _localFileSize; - } - protected override bool ReceiveData(byte[] data, int dataLength) - { - if (data == null || dataLength == 0 || _webRequest.responseCode >= 400) - return false; + _fileStream = new FileStream(_fileSavePath, FileMode.Append, FileAccess.Write); + _curFileSize = _localFileSize; + } + protected override bool ReceiveData(byte[] data, int dataLength) + { + if (data == null || dataLength == 0 || _webRequest.responseCode >= 400) + return false; - if (_fileStream == null) - return false; + if (_fileStream == null) + return false; - _fileStream.Write(data, 0, dataLength); - _curFileSize += dataLength; - return true; - } + _fileStream.Write(data, 0, dataLength); + _curFileSize += dataLength; + return true; + } - /// - /// UnityWebRequest.downloadHandler.data - /// - protected override byte[] GetData() - { - return null; - } + /// + /// UnityWebRequest.downloadHandler.data + /// + protected override byte[] GetData() + { + return null; + } - /// - /// UnityWebRequest.downloadHandler.text - /// - protected override string GetText() - { - return null; - } + /// + /// UnityWebRequest.downloadHandler.text + /// + protected override string GetText() + { + return null; + } - /// - /// UnityWebRequest.downloadProgress - /// - protected override float GetProgress() - { - return _fileTotalSize == 0 ? 0 : ((float)_curFileSize) / _fileTotalSize; - } + /// + /// UnityWebRequest.downloadProgress + /// + protected override float GetProgress() + { + return _fileTotalSize == 0 ? 0 : ((float)_curFileSize) / _fileTotalSize; + } - /// - /// 释放下载句柄 - /// - public void Cleanup() - { - if (_fileStream != null) - { - _fileStream.Flush(); - _fileStream.Dispose(); - _fileStream = null; - } - } - } + /// + /// 释放下载句柄 + /// + public void Cleanup() + { + if (_fileStream != null) + { + _fileStream.Flush(); + _fileStream.Dispose(); + _fileStream = null; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileGeneralRequest.cs b/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileGeneralRequest.cs index 0838f53..4d90ba3 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileGeneralRequest.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileGeneralRequest.cs @@ -3,61 +3,61 @@ using UnityEngine.Networking; namespace YooAsset { - internal class FileGeneralRequest : IWebRequester - { - private UnityWebRequest _webRequest; + internal class FileGeneralRequest : IWebRequester + { + private UnityWebRequest _webRequest; - public ERequestStatus Status { private set; get; } = ERequestStatus.None; - public float DownloadProgress { private set; get; } - public ulong DownloadedBytes { private set; get; } - public string RequestNetError { private set; get; } - public long RequestHttpCode { private set; get; } - - public FileGeneralRequest() { } - public void Create(string requestURL, BundleInfo bundleInfo, params object[] args) - { - if (Status != ERequestStatus.None) - throw new System.Exception("Should never get here !"); + public ERequestStatus Status { private set; get; } = ERequestStatus.None; + public float DownloadProgress { private set; get; } + public ulong DownloadedBytes { private set; get; } + public string RequestNetError { private set; get; } + public long RequestHttpCode { private set; get; } - string tempFilePath = bundleInfo.TempDataFilePath; + public FileGeneralRequest() { } + public void Create(string requestURL, BundleInfo bundleInfo, params object[] args) + { + if (Status != ERequestStatus.None) + throw new System.Exception("Should never get here !"); - // 删除临时文件 - if (File.Exists(tempFilePath)) - File.Delete(tempFilePath); + string tempFilePath = bundleInfo.TempDataFilePath; - // 创建下载器 - _webRequest = DownloadHelper.NewRequest(requestURL); - DownloadHandlerFile handler = new DownloadHandlerFile(tempFilePath); - handler.removeFileOnAbort = true; - _webRequest.downloadHandler = handler; - _webRequest.disposeDownloadHandlerOnDispose = true; - _webRequest.SendWebRequest(); - Status = ERequestStatus.InProgress; - } - public void Update() - { - if (Status == ERequestStatus.None) - return; - if (IsDone()) - return; + // 删除临时文件 + if (File.Exists(tempFilePath)) + File.Delete(tempFilePath); - DownloadProgress = _webRequest.downloadProgress; - DownloadedBytes = _webRequest.downloadedBytes; - if (_webRequest.isDone == false) - return; + // 创建下载器 + _webRequest = DownloadHelper.NewRequest(requestURL); + DownloadHandlerFile handler = new DownloadHandlerFile(tempFilePath); + handler.removeFileOnAbort = true; + _webRequest.downloadHandler = handler; + _webRequest.disposeDownloadHandlerOnDispose = true; + _webRequest.SendWebRequest(); + Status = ERequestStatus.InProgress; + } + public void Update() + { + if (Status == ERequestStatus.None) + return; + if (IsDone()) + return; - // 检查网络错误 + DownloadProgress = _webRequest.downloadProgress; + DownloadedBytes = _webRequest.downloadedBytes; + if (_webRequest.isDone == false) + return; + + // 检查网络错误 #if UNITY_2020_3_OR_NEWER - RequestHttpCode = _webRequest.responseCode; - if (_webRequest.result != UnityWebRequest.Result.Success) - { - RequestNetError = _webRequest.error; - Status = ERequestStatus.Error; - } - else - { - Status = ERequestStatus.Success; - } + RequestHttpCode = _webRequest.responseCode; + if (_webRequest.result != UnityWebRequest.Result.Success) + { + RequestNetError = _webRequest.error; + Status = ERequestStatus.Error; + } + else + { + Status = ERequestStatus.Success; + } #else RequestHttpCode = _webRequest.responseCode; if (_webRequest.isNetworkError || _webRequest.isHttpError) @@ -71,37 +71,37 @@ namespace YooAsset } #endif - // 最终释放下载器 - DisposeWebRequest(); - } - public void Abort() - { - DisposeWebRequest(); - if (IsDone() == false) - { - RequestNetError = "user abort"; - RequestHttpCode = 0; - Status = ERequestStatus.Error; - } - } - public bool IsDone() - { - if (Status == ERequestStatus.Success || Status == ERequestStatus.Error) - return true; - else - return false; - } - public object GetRequestObject() - { - throw new System.NotImplementedException(); - } - private void DisposeWebRequest() - { - if (_webRequest != null) - { - _webRequest.Dispose(); //注意:引擎底层会自动调用Abort方法 - _webRequest = null; - } - } - } + // 最终释放下载器 + DisposeWebRequest(); + } + public void Abort() + { + DisposeWebRequest(); + if (IsDone() == false) + { + RequestNetError = "user abort"; + RequestHttpCode = 0; + Status = ERequestStatus.Error; + } + } + public bool IsDone() + { + if (Status == ERequestStatus.Success || Status == ERequestStatus.Error) + return true; + else + return false; + } + public object GetRequestObject() + { + throw new System.NotImplementedException(); + } + private void DisposeWebRequest() + { + if (_webRequest != null) + { + _webRequest.Dispose(); //注意:引擎底层会自动调用Abort方法 + _webRequest = null; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileResumeRequest.cs b/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileResumeRequest.cs index d04d8ce..ff304d1 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileResumeRequest.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Requester/FileResumeRequest.cs @@ -3,85 +3,85 @@ using UnityEngine.Networking; namespace YooAsset { - internal class FileResumeRequest : IWebRequester - { - private string _tempFilePath; - private UnityWebRequest _webRequest; - private DownloadHandlerFileRange _downloadHandle; - private ulong _fileOriginLength = 0; + internal class FileResumeRequest : IWebRequester + { + private string _tempFilePath; + private UnityWebRequest _webRequest; + private DownloadHandlerFileRange _downloadHandle; + private ulong _fileOriginLength = 0; - public ERequestStatus Status { private set; get; } = ERequestStatus.None; - public float DownloadProgress { private set; get; } - public ulong DownloadedBytes { private set; get; } - public string RequestNetError { private set; get; } - public long RequestHttpCode { private set; get; } - - public FileResumeRequest() { } - public void Create(string requestURL, BundleInfo bundleInfo, params object[] args) - { - if (Status != ERequestStatus.None) - throw new System.Exception("Should never get here !"); + public ERequestStatus Status { private set; get; } = ERequestStatus.None; + public float DownloadProgress { private set; get; } + public ulong DownloadedBytes { private set; get; } + public string RequestNetError { private set; get; } + public long RequestHttpCode { private set; get; } - _tempFilePath = bundleInfo.TempDataFilePath; - long fileBytes = bundleInfo.Bundle.FileSize; + public FileResumeRequest() { } + public void Create(string requestURL, BundleInfo bundleInfo, params object[] args) + { + if (Status != ERequestStatus.None) + throw new System.Exception("Should never get here !"); - // 获取下载的起始位置 - long fileLength = -1; - if (File.Exists(_tempFilePath)) - { - FileInfo fileInfo = new FileInfo(_tempFilePath); - fileLength = fileInfo.Length; - _fileOriginLength = (ulong)fileLength; - DownloadedBytes = _fileOriginLength; - } + _tempFilePath = bundleInfo.TempDataFilePath; + long fileBytes = bundleInfo.Bundle.FileSize; - // 检测下载起始位置是否有效 - if (fileLength >= fileBytes) - { - if (File.Exists(_tempFilePath)) - File.Delete(_tempFilePath); - } + // 获取下载的起始位置 + long fileLength = -1; + if (File.Exists(_tempFilePath)) + { + FileInfo fileInfo = new FileInfo(_tempFilePath); + fileLength = fileInfo.Length; + _fileOriginLength = (ulong)fileLength; + DownloadedBytes = _fileOriginLength; + } - // 创建下载器 - _webRequest = DownloadHelper.NewRequest(requestURL); + // 检测下载起始位置是否有效 + if (fileLength >= fileBytes) + { + if (File.Exists(_tempFilePath)) + File.Delete(_tempFilePath); + } + + // 创建下载器 + _webRequest = DownloadHelper.NewRequest(requestURL); #if UNITY_2019_4_OR_NEWER - var handler = new DownloadHandlerFile(_tempFilePath, true); - handler.removeFileOnAbort = false; + var handler = new DownloadHandlerFile(_tempFilePath, true); + handler.removeFileOnAbort = false; #else var handler = new DownloadHandlerFileRange(tempFilePath, _bundleInfo.Bundle.FileSize, _webRequest); _downloadHandle = handler; #endif - _webRequest.downloadHandler = handler; - _webRequest.disposeDownloadHandlerOnDispose = true; - if (fileLength > 0) - _webRequest.SetRequestHeader("Range", $"bytes={fileLength}-"); - _webRequest.SendWebRequest(); - Status = ERequestStatus.InProgress; - } - public void Update() - { - if (Status == ERequestStatus.None) - return; - if (IsDone()) - return; + _webRequest.downloadHandler = handler; + _webRequest.disposeDownloadHandlerOnDispose = true; + if (fileLength > 0) + _webRequest.SetRequestHeader("Range", $"bytes={fileLength}-"); + _webRequest.SendWebRequest(); + Status = ERequestStatus.InProgress; + } + public void Update() + { + if (Status == ERequestStatus.None) + return; + if (IsDone()) + return; - DownloadProgress = _webRequest.downloadProgress; - DownloadedBytes = _fileOriginLength + _webRequest.downloadedBytes; - if (_webRequest.isDone == false) - return; + DownloadProgress = _webRequest.downloadProgress; + DownloadedBytes = _fileOriginLength + _webRequest.downloadedBytes; + if (_webRequest.isDone == false) + return; - // 检查网络错误 + // 检查网络错误 #if UNITY_2020_3_OR_NEWER - RequestHttpCode = _webRequest.responseCode; - if (_webRequest.result != UnityWebRequest.Result.Success) - { - RequestNetError = _webRequest.error; - Status = ERequestStatus.Error; - } - else - { - Status = ERequestStatus.Success; - } + RequestHttpCode = _webRequest.responseCode; + if (_webRequest.result != UnityWebRequest.Result.Success) + { + RequestNetError = _webRequest.error; + Status = ERequestStatus.Error; + } + else + { + Status = ERequestStatus.Success; + } #else RequestHttpCode = _webRequest.responseCode; if (_webRequest.isNetworkError || _webRequest.isHttpError) @@ -95,56 +95,56 @@ namespace YooAsset } #endif - // 注意:下载断点续传文件发生特殊错误码之后删除文件 - if (Status == ERequestStatus.Error) - { - if (DownloadHelper.ClearFileResponseCodes != null) - { - if (DownloadHelper.ClearFileResponseCodes.Contains(RequestHttpCode)) - { - if (File.Exists(_tempFilePath)) - File.Delete(_tempFilePath); - } - } - } + // 注意:下载断点续传文件发生特殊错误码之后删除文件 + if (Status == ERequestStatus.Error) + { + if (DownloadHelper.ClearFileResponseCodes != null) + { + if (DownloadHelper.ClearFileResponseCodes.Contains(RequestHttpCode)) + { + if (File.Exists(_tempFilePath)) + File.Delete(_tempFilePath); + } + } + } - // 最终释放下载器 - DisposeWebRequest(); - } - public void Abort() - { - DisposeWebRequest(); - if (IsDone() == false) - { - RequestNetError = "user abort"; - RequestHttpCode = 0; - Status = ERequestStatus.Error; - } - } - public bool IsDone() - { - if (Status == ERequestStatus.Success || Status == ERequestStatus.Error) - return true; - else - return false; - } - public object GetRequestObject() - { - throw new System.NotImplementedException(); - } - private void DisposeWebRequest() - { - if (_downloadHandle != null) - { - _downloadHandle.Cleanup(); - _downloadHandle = null; - } + // 最终释放下载器 + DisposeWebRequest(); + } + public void Abort() + { + DisposeWebRequest(); + if (IsDone() == false) + { + RequestNetError = "user abort"; + RequestHttpCode = 0; + Status = ERequestStatus.Error; + } + } + public bool IsDone() + { + if (Status == ERequestStatus.Success || Status == ERequestStatus.Error) + return true; + else + return false; + } + public object GetRequestObject() + { + throw new System.NotImplementedException(); + } + private void DisposeWebRequest() + { + if (_downloadHandle != null) + { + _downloadHandle.Cleanup(); + _downloadHandle = null; + } - if (_webRequest != null) - { - _webRequest.Dispose(); - _webRequest = null; - } - } - } + if (_webRequest != null) + { + _webRequest.Dispose(); + _webRequest = null; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Requester/IWebRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/Requester/IWebRequester.cs index 4274451..65a9558 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Requester/IWebRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Requester/IWebRequester.cs @@ -1,65 +1,65 @@  namespace YooAsset { - internal enum ERequestStatus - { - None, - InProgress, - Error, - Success, - } + internal enum ERequestStatus + { + None, + InProgress, + Error, + Success, + } - internal interface IWebRequester - { - /// - /// 任务状态 - /// - public ERequestStatus Status { get; } + internal interface IWebRequester + { + /// + /// 任务状态 + /// + public ERequestStatus Status { get; } - /// - /// 下载进度(0f~1f) - /// - public float DownloadProgress { get; } + /// + /// 下载进度(0f~1f) + /// + public float DownloadProgress { get; } - /// - /// 已经下载的总字节数 - /// - public ulong DownloadedBytes { get; } + /// + /// 已经下载的总字节数 + /// + public ulong DownloadedBytes { get; } - /// - /// 返回的网络错误 - /// - public string RequestNetError { get; } + /// + /// 返回的网络错误 + /// + public string RequestNetError { get; } - /// - /// 返回的HTTP CODE - /// - public long RequestHttpCode { get; } + /// + /// 返回的HTTP CODE + /// + public long RequestHttpCode { get; } - /// - /// 创建任务 - /// - public void Create(string url, BundleInfo bundleInfo, params object[] args); + /// + /// 创建任务 + /// + public void Create(string url, BundleInfo bundleInfo, params object[] args); - /// - /// 更新任务 - /// - public void Update(); + /// + /// 更新任务 + /// + public void Update(); - /// - /// 终止任务 - /// - public void Abort(); + /// + /// 终止任务 + /// + public void Abort(); - /// - /// 是否已经完成(无论成功或失败) - /// - public bool IsDone(); + /// + /// 是否已经完成(无论成功或失败) + /// + public bool IsDone(); - /// - /// 获取请求的对象 - /// - public object GetRequestObject(); - } + /// + /// 获取请求的对象 + /// + public object GetRequestObject(); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/ThreadSyncContext.cs b/Assets/YooAsset/Runtime/DownloadSystem/ThreadSyncContext.cs index fb5f5c5..65af1f1 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/ThreadSyncContext.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/ThreadSyncContext.cs @@ -5,34 +5,34 @@ using System.Threading; namespace YooAsset { - /// - /// 同步其它线程里的回调到主线程里 - /// 注意:Unity3D中需要设置Scripting Runtime Version为.NET4.6 - /// - internal sealed class ThreadSyncContext : SynchronizationContext - { - private readonly ConcurrentQueue _safeQueue = new ConcurrentQueue(); + /// + /// 同步其它线程里的回调到主线程里 + /// 注意:Unity3D中需要设置Scripting Runtime Version为.NET4.6 + /// + internal sealed class ThreadSyncContext : SynchronizationContext + { + private readonly ConcurrentQueue _safeQueue = new ConcurrentQueue(); - /// - /// 更新同步队列 - /// - public void Update() - { - while (true) - { - if (_safeQueue.TryDequeue(out Action action) == false) - return; - action.Invoke(); - } - } + /// + /// 更新同步队列 + /// + public void Update() + { + while (true) + { + if (_safeQueue.TryDequeue(out Action action) == false) + return; + action.Invoke(); + } + } - /// - /// 向同步队列里投递一个回调方法 - /// - public override void Post(SendOrPostCallback callback, object state) - { - Action action = new Action(() => { callback(state); }); - _safeQueue.Enqueue(action); - } - } + /// + /// 向同步队列里投递一个回调方法 + /// + public override void Post(SendOrPostCallback callback, object state) + { + Action action = new Action(() => { callback(state); }); + _safeQueue.Enqueue(action); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs index fb84e7c..fb109dc 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebDataRequester.cs @@ -6,46 +6,46 @@ using UnityEngine; namespace YooAsset { - internal class UnityWebDataRequester : UnityWebRequesterBase - { - /// - /// 发送GET请求 - /// - public void SendRequest(string url, int timeout = 60) - { - if (_webRequest == null) - { - URL = url; - ResetTimeout(timeout); + internal class UnityWebDataRequester : UnityWebRequesterBase + { + /// + /// 发送GET请求 + /// + public void SendRequest(string url, int timeout = 60) + { + if (_webRequest == null) + { + URL = url; + ResetTimeout(timeout); - _webRequest = DownloadHelper.NewRequest(URL); - DownloadHandlerBuffer handler = new DownloadHandlerBuffer(); - _webRequest.downloadHandler = handler; - _webRequest.disposeDownloadHandlerOnDispose = true; - _operationHandle = _webRequest.SendWebRequest(); - } - } + _webRequest = DownloadHelper.NewRequest(URL); + DownloadHandlerBuffer handler = new DownloadHandlerBuffer(); + _webRequest.downloadHandler = handler; + _webRequest.disposeDownloadHandlerOnDispose = true; + _operationHandle = _webRequest.SendWebRequest(); + } + } - /// - /// 获取下载的字节数据 - /// - public byte[] GetData() - { - if (_webRequest != null && IsDone()) - return _webRequest.downloadHandler.data; - else - return null; - } + /// + /// 获取下载的字节数据 + /// + public byte[] GetData() + { + if (_webRequest != null && IsDone()) + return _webRequest.downloadHandler.data; + else + return null; + } - /// - /// 获取下载的文本数据 - /// - public string GetText() - { - if (_webRequest != null && IsDone()) - return _webRequest.downloadHandler.text; - else - return null; - } - } + /// + /// 获取下载的文本数据 + /// + public string GetText() + { + if (_webRequest != null && IsDone()) + return _webRequest.downloadHandler.text; + else + return null; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs index 2d15aac..09e1745 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebFileRequester.cs @@ -6,25 +6,25 @@ using UnityEngine; namespace YooAsset { - internal class UnityWebFileRequester : UnityWebRequesterBase - { - /// - /// 发送GET请求 - /// - public void SendRequest(string url, string fileSavePath, int timeout = 60) - { - if (_webRequest == null) - { - URL = url; - ResetTimeout(timeout); + internal class UnityWebFileRequester : UnityWebRequesterBase + { + /// + /// 发送GET请求 + /// + public void SendRequest(string url, string fileSavePath, int timeout = 60) + { + if (_webRequest == null) + { + URL = url; + ResetTimeout(timeout); - _webRequest = DownloadHelper.NewRequest(URL); - DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath); - handler.removeFileOnAbort = true; - _webRequest.downloadHandler = handler; - _webRequest.disposeDownloadHandlerOnDispose = true; - _operationHandle = _webRequest.SendWebRequest(); - } - } - } + _webRequest = DownloadHelper.NewRequest(URL); + DownloadHandlerFile handler = new DownloadHandlerFile(fileSavePath); + handler.removeFileOnAbort = true; + _webRequest.downloadHandler = handler; + _webRequest.disposeDownloadHandlerOnDispose = true; + _operationHandle = _webRequest.SendWebRequest(); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebRequesterBase.cs b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebRequesterBase.cs index 86753f7..620230f 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/UnityWebRequesterBase.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/UnityWebRequesterBase.cs @@ -6,111 +6,111 @@ using UnityEngine; namespace YooAsset { - internal abstract class UnityWebRequesterBase - { - protected UnityWebRequest _webRequest; - protected UnityWebRequestAsyncOperation _operationHandle; + internal abstract class UnityWebRequesterBase + { + protected UnityWebRequest _webRequest; + protected UnityWebRequestAsyncOperation _operationHandle; - // 超时相关 - private float _timeout; - private bool _isAbort = false; - private ulong _latestDownloadBytes; - private float _latestDownloadRealtime; + // 超时相关 + private float _timeout; + private bool _isAbort = false; + private ulong _latestDownloadBytes; + private float _latestDownloadRealtime; - /// - /// 请求URL地址 - /// - public string URL { protected set; get; } + /// + /// 请求URL地址 + /// + public string URL { protected set; get; } - protected void ResetTimeout(float timeout) - { - _timeout = timeout; - _latestDownloadBytes = 0; - _latestDownloadRealtime = Time.realtimeSinceStartup; - } + protected void ResetTimeout(float timeout) + { + _timeout = timeout; + _latestDownloadBytes = 0; + _latestDownloadRealtime = Time.realtimeSinceStartup; + } - /// - /// 释放下载器 - /// - public void Dispose() - { - if (_webRequest != null) - { - _webRequest.Dispose(); - _webRequest = null; - _operationHandle = null; - } - } + /// + /// 释放下载器 + /// + public void Dispose() + { + if (_webRequest != null) + { + _webRequest.Dispose(); + _webRequest = null; + _operationHandle = null; + } + } - /// - /// 是否完毕(无论成功失败) - /// - public bool IsDone() - { - if (_operationHandle == null) - return false; - return _operationHandle.isDone; - } + /// + /// 是否完毕(无论成功失败) + /// + public bool IsDone() + { + if (_operationHandle == null) + return false; + return _operationHandle.isDone; + } - /// - /// 下载进度 - /// - public float Progress() - { - if (_operationHandle == null) - return 0; - return _operationHandle.progress; - } + /// + /// 下载进度 + /// + public float Progress() + { + if (_operationHandle == null) + return 0; + return _operationHandle.progress; + } - /// - /// 下载是否发生错误 - /// - public bool HasError() - { + /// + /// 下载是否发生错误 + /// + public bool HasError() + { #if UNITY_2020_3_OR_NEWER - return _webRequest.result != UnityWebRequest.Result.Success; + return _webRequest.result != UnityWebRequest.Result.Success; #else if (_webRequest.isNetworkError || _webRequest.isHttpError) return true; else return false; #endif - } + } - /// - /// 获取错误信息 - /// - public string GetError() - { - if (_webRequest != null) - { - return $"URL : {URL} Error : {_webRequest.error}"; - } - return string.Empty; - } + /// + /// 获取错误信息 + /// + public string GetError() + { + if (_webRequest != null) + { + return $"URL : {URL} Error : {_webRequest.error}"; + } + return string.Empty; + } - /// - /// 检测超时 - /// - public void CheckTimeout() - { - // 注意:在连续时间段内无新增下载数据及判定为超时 - if (_isAbort == false) - { - if (_latestDownloadBytes != _webRequest.downloadedBytes) - { - _latestDownloadBytes = _webRequest.downloadedBytes; - _latestDownloadRealtime = Time.realtimeSinceStartup; - } + /// + /// 检测超时 + /// + public void CheckTimeout() + { + // 注意:在连续时间段内无新增下载数据及判定为超时 + if (_isAbort == false) + { + if (_latestDownloadBytes != _webRequest.downloadedBytes) + { + _latestDownloadBytes = _webRequest.downloadedBytes; + _latestDownloadRealtime = Time.realtimeSinceStartup; + } - float offset = Time.realtimeSinceStartup - _latestDownloadRealtime; - if (offset > _timeout) - { - _webRequest.Abort(); - _isAbort = true; - } - } - } - } + float offset = Time.realtimeSinceStartup - _latestDownloadRealtime; + if (offset > _timeout) + { + _webRequest.Abort(); + _isAbort = true; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index fb2cbce..8925dbb 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -1,154 +1,154 @@  namespace YooAsset { - /// - /// 默认的构建管线 - /// - public enum EDefaultBuildPipeline - { - /// - /// 内置构建管线 - /// - BuiltinBuildPipeline, + /// + /// 默认的构建管线 + /// + public enum EDefaultBuildPipeline + { + /// + /// 内置构建管线 + /// + BuiltinBuildPipeline, - /// - /// 可编程构建管线 - /// - ScriptableBuildPipeline, + /// + /// 可编程构建管线 + /// + ScriptableBuildPipeline, - /// - /// 原生文件构建管线 - /// - RawFileBuildPipeline, - } + /// + /// 原生文件构建管线 + /// + RawFileBuildPipeline, + } - /// - /// 运行模式 - /// - public enum EPlayMode - { - /// - /// 编辑器下的模拟模式 - /// - EditorSimulateMode, + /// + /// 运行模式 + /// + public enum EPlayMode + { + /// + /// 编辑器下的模拟模式 + /// + EditorSimulateMode, - /// - /// 离线运行模式 - /// - OfflinePlayMode, + /// + /// 离线运行模式 + /// + OfflinePlayMode, - /// - /// 联机运行模式 - /// - HostPlayMode, + /// + /// 联机运行模式 + /// + HostPlayMode, - /// - /// WebGL运行模式 - /// - WebPlayMode, - } + /// + /// WebGL运行模式 + /// + WebPlayMode, + } - /// - /// 初始化参数 - /// - public abstract class InitializeParameters - { - /// - /// 内置文件的根路径 - /// 注意:当参数为空的时候会使用默认的根目录。 - /// - public string BuildinRootDirectory = string.Empty; + /// + /// 初始化参数 + /// + public abstract class InitializeParameters + { + /// + /// 内置文件的根路径 + /// 注意:当参数为空的时候会使用默认的根目录。 + /// + public string BuildinRootDirectory = string.Empty; - /// - /// 沙盒文件的根路径 - /// 注意:当参数为空的时候会使用默认的根目录。 - /// - public string SandboxRootDirectory = string.Empty; + /// + /// 沙盒文件的根路径 + /// 注意:当参数为空的时候会使用默认的根目录。 + /// + public string SandboxRootDirectory = string.Empty; - /// - /// 缓存文件追加原始后缀格式 - /// - public bool CacheFileAppendExtension = false; + /// + /// 缓存文件追加原始后缀格式 + /// + public bool CacheFileAppendExtension = false; - /// - /// 缓存系统启动时的验证级别 - /// - public EVerifyLevel CacheBootVerifyLevel = EVerifyLevel.Middle; + /// + /// 缓存系统启动时的验证级别 + /// + public EVerifyLevel CacheBootVerifyLevel = EVerifyLevel.Middle; - /// - /// 自动销毁不再使用的资源提供者 - /// - public bool AutoDestroyAssetProvider = false; + /// + /// 自动销毁不再使用的资源提供者 + /// + public bool AutoDestroyAssetProvider = false; - /// - /// 启用断点续传参数 - /// 说明:当文件的大小大于设置的字节数时启用断点续传下载器 - /// - public uint BreakpointResumeFileSize = int.MaxValue; + /// + /// 启用断点续传参数 + /// 说明:当文件的大小大于设置的字节数时启用断点续传下载器 + /// + public uint BreakpointResumeFileSize = int.MaxValue; - /// - /// 文件解密服务接口 - /// - public IDecryptionServices DecryptionServices = null; - } + /// + /// 文件解密服务接口 + /// + public IDecryptionServices DecryptionServices = null; + } - /// - /// 编辑器下模拟运行模式的初始化参数 - /// - public class EditorSimulateModeParameters : InitializeParameters - { - /// - /// 用于模拟运行的资源清单路径 - /// - public string SimulateManifestFilePath = string.Empty; - } + /// + /// 编辑器下模拟运行模式的初始化参数 + /// + public class EditorSimulateModeParameters : InitializeParameters + { + /// + /// 用于模拟运行的资源清单路径 + /// + public string SimulateManifestFilePath = string.Empty; + } - /// - /// 离线运行模式的初始化参数 - /// - public class OfflinePlayModeParameters : InitializeParameters - { - } + /// + /// 离线运行模式的初始化参数 + /// + public class OfflinePlayModeParameters : InitializeParameters + { + } - /// - /// 联机运行模式的初始化参数 - /// - public class HostPlayModeParameters : InitializeParameters - { - /// - /// 远端资源地址查询服务类 - /// - public IRemoteServices RemoteServices = null; + /// + /// 联机运行模式的初始化参数 + /// + public class HostPlayModeParameters : InitializeParameters + { + /// + /// 远端资源地址查询服务类 + /// + public IRemoteServices RemoteServices = null; - /// - /// 内置资源查询服务接口 - /// - public IBuildinQueryServices BuildinQueryServices = null; + /// + /// 内置资源查询服务接口 + /// + public IBuildinQueryServices BuildinQueryServices = null; - /// - /// 分发资源查询服务接口 - /// - public IDeliveryQueryServices DeliveryQueryServices = null; + /// + /// 分发资源查询服务接口 + /// + public IDeliveryQueryServices DeliveryQueryServices = null; - /// - /// 分发资源加载服务接口 - /// - public IDeliveryLoadServices DeliveryLoadServices = null; - } + /// + /// 分发资源加载服务接口 + /// + public IDeliveryLoadServices DeliveryLoadServices = null; + } - /// - /// WebGL运行模式的初始化参数 - /// - public class WebPlayModeParameters : InitializeParameters - { - /// - /// 远端资源地址查询服务类 - /// - public IRemoteServices RemoteServices = null; + /// + /// WebGL运行模式的初始化参数 + /// + public class WebPlayModeParameters : InitializeParameters + { + /// + /// 远端资源地址查询服务类 + /// + public IRemoteServices RemoteServices = null; - /// - /// 内置资源查询服务接口 - /// - public IBuildinQueryServices BuildinQueryServices = null; - } + /// + /// 内置资源查询服务接口 + /// + public IBuildinQueryServices BuildinQueryServices = null; + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index b09522d..892b77f 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -5,148 +5,148 @@ using System.Threading.Tasks; namespace YooAsset { - public abstract class AsyncOperationBase : IEnumerator, IComparable - { - // 用户请求的回调 - private Action _callback; + public abstract class AsyncOperationBase : IEnumerator, IComparable + { + // 用户请求的回调 + private Action _callback; - // 是否已经完成 - internal bool IsFinish = false; - - /// - /// 所属包裹 - /// - public string PackageName { private set; get; } + // 是否已经完成 + internal bool IsFinish = false; - /// - /// 优先级 - /// - public uint Priority { set; get; } = 0; + /// + /// 所属包裹 + /// + public string PackageName { private set; get; } - /// - /// 状态 - /// - public EOperationStatus Status { get; protected set; } = EOperationStatus.None; + /// + /// 优先级 + /// + public uint Priority { set; get; } = 0; - /// - /// 错误信息 - /// - public string Error { get; protected set; } + /// + /// 状态 + /// + public EOperationStatus Status { get; protected set; } = EOperationStatus.None; - /// - /// 处理进度 - /// - public float Progress { get; protected set; } + /// + /// 错误信息 + /// + public string Error { get; protected set; } - /// - /// 是否已经完成 - /// - public bool IsDone - { - get - { - return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed; - } - } + /// + /// 处理进度 + /// + public float Progress { get; protected set; } - /// - /// 完成事件 - /// - public event Action Completed - { - add - { - if (IsDone) - value.Invoke(this); - else - _callback += value; - } - remove - { - _callback -= value; - } - } + /// + /// 是否已经完成 + /// + public bool IsDone + { + get + { + return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed; + } + } - /// - /// 异步操作任务 - /// - public Task Task - { - get - { - if (_taskCompletionSource == null) - { - _taskCompletionSource = new TaskCompletionSource(); - if (IsDone) - _taskCompletionSource.SetResult(null); - } - return _taskCompletionSource.Task; - } - } + /// + /// 完成事件 + /// + public event Action Completed + { + add + { + if (IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + _callback -= value; + } + } - internal abstract void InternalOnStart(); - internal abstract void InternalOnUpdate(); - internal virtual void InternalOnAbort() { } + /// + /// 异步操作任务 + /// + public Task Task + { + get + { + if (_taskCompletionSource == null) + { + _taskCompletionSource = new TaskCompletionSource(); + if (IsDone) + _taskCompletionSource.SetResult(null); + } + return _taskCompletionSource.Task; + } + } - internal void Init(string packageName) - { - PackageName = packageName; - } - internal void SetStart() - { - Status = EOperationStatus.Processing; - InternalOnStart(); - } - internal void SetFinish() - { - IsFinish = true; + internal abstract void InternalOnStart(); + internal abstract void InternalOnUpdate(); + internal virtual void InternalOnAbort() { } - // 进度百分百完成 - Progress = 1f; + internal void Init(string packageName) + { + PackageName = packageName; + } + internal void SetStart() + { + Status = EOperationStatus.Processing; + InternalOnStart(); + } + internal void SetFinish() + { + IsFinish = true; - //注意:如果完成回调内发生异常,会导致Task无限期等待 - _callback?.Invoke(this); + // 进度百分百完成 + Progress = 1f; - if (_taskCompletionSource != null) - _taskCompletionSource.TrySetResult(null); - } - internal void SetAbort() - { - if (IsDone == false) - { - Status = EOperationStatus.Failed; - Error = "user abort"; - YooLogger.Warning($"Async operaiton has been abort : {this.GetType().Name}"); - InternalOnAbort(); - } - } + //注意:如果完成回调内发生异常,会导致Task无限期等待 + _callback?.Invoke(this); - /// - /// 清空完成回调 - /// - protected void ClearCompletedCallback() - { - _callback = null; - } + if (_taskCompletionSource != null) + _taskCompletionSource.TrySetResult(null); + } + internal void SetAbort() + { + if (IsDone == false) + { + Status = EOperationStatus.Failed; + Error = "user abort"; + YooLogger.Warning($"Async operaiton has been abort : {this.GetType().Name}"); + InternalOnAbort(); + } + } - #region 排序接口实现 - public int CompareTo(AsyncOperationBase other) - { - return other.Priority.CompareTo(this.Priority); - } - #endregion + /// + /// 清空完成回调 + /// + protected void ClearCompletedCallback() + { + _callback = null; + } - #region 异步编程相关 - bool IEnumerator.MoveNext() - { - return !IsDone; - } - void IEnumerator.Reset() - { - } - object IEnumerator.Current => null; + #region 排序接口实现 + public int CompareTo(AsyncOperationBase other) + { + return other.Priority.CompareTo(this.Priority); + } + #endregion - private TaskCompletionSource _taskCompletionSource; - #endregion - } + #region 异步编程相关 + bool IEnumerator.MoveNext() + { + return !IsDone; + } + void IEnumerator.Reset() + { + } + object IEnumerator.Current => null; + + private TaskCompletionSource _taskCompletionSource; + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/OperationSystem/EOperationStatus.cs b/Assets/YooAsset/Runtime/OperationSystem/EOperationStatus.cs index cedb72e..dddaa7d 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/EOperationStatus.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/EOperationStatus.cs @@ -1,11 +1,11 @@  namespace YooAsset { - public enum EOperationStatus - { - None, - Processing, - Succeed, - Failed - } + public enum EOperationStatus + { + None, + Processing, + Succeed, + Failed + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs b/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs index 0ff31fe..22a935f 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs @@ -1,42 +1,42 @@  namespace YooAsset { - public abstract class GameAsyncOperation : AsyncOperationBase - { - internal override void InternalOnStart() - { - OnStart(); - } - internal override void InternalOnUpdate() - { - OnUpdate(); - } - internal override void InternalOnAbort() - { - OnAbort(); - } + public abstract class GameAsyncOperation : AsyncOperationBase + { + internal override void InternalOnStart() + { + OnStart(); + } + internal override void InternalOnUpdate() + { + OnUpdate(); + } + internal override void InternalOnAbort() + { + OnAbort(); + } - /// - /// 异步操作开始 - /// - protected abstract void OnStart(); + /// + /// 异步操作开始 + /// + protected abstract void OnStart(); - /// - /// 异步操作更新 - /// - protected abstract void OnUpdate(); + /// + /// 异步操作更新 + /// + protected abstract void OnUpdate(); - /// - /// 异步操作终止 - /// - protected abstract void OnAbort(); + /// + /// 异步操作终止 + /// + protected abstract void OnAbort(); - /// - /// 异步操作系统是否繁忙 - /// - protected bool IsBusy() - { - return OperationSystem.IsBusy; - } - } + /// + /// 异步操作系统是否繁忙 + /// + protected bool IsBusy() + { + return OperationSystem.IsBusy; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs index 1638b9b..ed85e1c 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs @@ -4,138 +4,138 @@ using System.Diagnostics; namespace YooAsset { - internal class OperationSystem - { - private static readonly List _operations = new List(1000); - private static readonly List _newList = new List(1000); + internal class OperationSystem + { + private static readonly List _operations = new List(1000); + private static readonly List _newList = new List(1000); - // 计时器相关 - private static Stopwatch _watch; - private static long _frameTime; + // 计时器相关 + private static Stopwatch _watch; + private static long _frameTime; - /// - /// 异步操作的最小时间片段 - /// - public static long MaxTimeSlice { set; get; } = long.MaxValue; + /// + /// 异步操作的最小时间片段 + /// + public static long MaxTimeSlice { set; get; } = long.MaxValue; - /// - /// 处理器是否繁忙 - /// - public static bool IsBusy - { - get - { - return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice; - } - } + /// + /// 处理器是否繁忙 + /// + public static bool IsBusy + { + get + { + return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice; + } + } - /// - /// 初始化异步操作系统 - /// - public static void Initialize() - { - _watch = Stopwatch.StartNew(); - } + /// + /// 初始化异步操作系统 + /// + public static void Initialize() + { + _watch = Stopwatch.StartNew(); + } - /// - /// 更新异步操作系统 - /// - public static void Update() - { - _frameTime = _watch.ElapsedMilliseconds; + /// + /// 更新异步操作系统 + /// + public static void Update() + { + _frameTime = _watch.ElapsedMilliseconds; - // 添加新增的异步操作 - if (_newList.Count > 0) - { - bool sorting = false; - foreach (var operation in _newList) - { - if (operation.Priority > 0) - { - sorting = true; - break; - } - } + // 添加新增的异步操作 + if (_newList.Count > 0) + { + bool sorting = false; + foreach (var operation in _newList) + { + if (operation.Priority > 0) + { + sorting = true; + break; + } + } - _operations.AddRange(_newList); - _newList.Clear(); + _operations.AddRange(_newList); + _newList.Clear(); - // 重新排序优先级 - if (sorting) - _operations.Sort(); - } + // 重新排序优先级 + if (sorting) + _operations.Sort(); + } - // 更新进行中的异步操作 - for (int i = 0; i < _operations.Count; i++) - { - if (IsBusy) - break; + // 更新进行中的异步操作 + for (int i = 0; i < _operations.Count; i++) + { + if (IsBusy) + break; - var operation = _operations[i]; - if (operation.IsFinish) - continue; + var operation = _operations[i]; + if (operation.IsFinish) + continue; - if (operation.IsDone == false) - operation.InternalOnUpdate(); + if (operation.IsDone == false) + operation.InternalOnUpdate(); - if (operation.IsDone) - operation.SetFinish(); - } + if (operation.IsDone) + operation.SetFinish(); + } - // 移除已经完成的异步操作 - for (int i = _operations.Count - 1; i >= 0; i--) - { - var operation = _operations[i]; - if (operation.IsFinish) - _operations.RemoveAt(i); - } - } + // 移除已经完成的异步操作 + for (int i = _operations.Count - 1; i >= 0; i--) + { + var operation = _operations[i]; + if (operation.IsFinish) + _operations.RemoveAt(i); + } + } - /// - /// 销毁异步操作系统 - /// - public static void DestroyAll() - { - _operations.Clear(); - _newList.Clear(); - _watch = null; - _frameTime = 0; - MaxTimeSlice = long.MaxValue; - } + /// + /// 销毁异步操作系统 + /// + public static void DestroyAll() + { + _operations.Clear(); + _newList.Clear(); + _watch = null; + _frameTime = 0; + MaxTimeSlice = long.MaxValue; + } - /// - /// 销毁包裹的所有任务 - /// - public static void ClearPackageOperation(string packageName) - { - // 终止临时队列里的任务 - foreach (var operation in _newList) - { - if (operation.PackageName == packageName) - { - operation.SetAbort(); - } - } + /// + /// 销毁包裹的所有任务 + /// + public static void ClearPackageOperation(string packageName) + { + // 终止临时队列里的任务 + foreach (var operation in _newList) + { + if (operation.PackageName == packageName) + { + operation.SetAbort(); + } + } - // 终止正在进行的任务 - foreach (var operation in _operations) - { - if (operation.PackageName == packageName) - { - operation.SetAbort(); - } - } - } + // 终止正在进行的任务 + foreach (var operation in _operations) + { + if (operation.PackageName == packageName) + { + operation.SetAbort(); + } + } + } - /// - /// 开始处理异步操作类 - /// - public static void StartOperation(string packageName, AsyncOperationBase operation) - { - _newList.Add(operation); - operation.Init(packageName); - operation.SetStart(); - } - } + /// + /// 开始处理异步操作类 + /// + public static void StartOperation(string packageName, AsyncOperationBase operation) + { + _newList.Add(operation); + operation.Init(packageName); + operation.SetStart(); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs index 4d4206c..abce66e 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs @@ -3,78 +3,78 @@ using System.Collections.Generic; namespace YooAsset { - public sealed class AllAssetsHandle : HandleBase, IDisposable - { - private System.Action _callback; + public sealed class AllAssetsHandle : HandleBase, IDisposable + { + private System.Action _callback; - internal AllAssetsHandle(ProviderBase provider) : base(provider) - { - } - internal override void InvokeCallback() - { - _callback?.Invoke(this); - } - - /// - /// 完成委托 - /// - public event System.Action Completed - { - add - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(AllAssetsHandle)} is invalid"); - if (Provider.IsDone) - value.Invoke(this); - else - _callback += value; - } - remove - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(AllAssetsHandle)} is invalid"); - _callback -= value; - } - } + internal AllAssetsHandle(ProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + _callback?.Invoke(this); + } - /// - /// 等待异步执行完毕 - /// - public void WaitForAsyncComplete() - { - if (IsValidWithWarning == false) - return; - Provider.WaitForAsyncComplete(); - } + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(AllAssetsHandle)} is invalid"); + if (Provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(AllAssetsHandle)} is invalid"); + _callback -= value; + } + } - /// - /// 释放资源句柄 - /// - public void Release() - { - this.ReleaseInternal(); - } + /// + /// 等待异步执行完毕 + /// + public void WaitForAsyncComplete() + { + if (IsValidWithWarning == false) + return; + Provider.WaitForAsyncComplete(); + } - /// - /// 释放资源句柄 - /// - public void Dispose() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Release() + { + this.ReleaseInternal(); + } + + /// + /// 释放资源句柄 + /// + public void Dispose() + { + this.ReleaseInternal(); + } - /// - /// 子资源对象集合 - /// - public UnityEngine.Object[] AllAssetObjects - { - get - { - if (IsValidWithWarning == false) - return null; - return Provider.AllAssetObjects; - } - } - } + /// + /// 子资源对象集合 + /// + public UnityEngine.Object[] AllAssetObjects + { + get + { + if (IsValidWithWarning == false) + return null; + return Provider.AllAssetObjects; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs index f67533c..a9125a1 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs @@ -4,154 +4,154 @@ using UnityEngine; namespace YooAsset { - public sealed class AssetHandle : HandleBase, IDisposable - { - private System.Action _callback; + public sealed class AssetHandle : HandleBase, IDisposable + { + private System.Action _callback; - internal AssetHandle(ProviderBase provider) : base(provider) - { - } - internal override void InvokeCallback() - { - _callback?.Invoke(this); - } + internal AssetHandle(ProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + _callback?.Invoke(this); + } - /// - /// 完成委托 - /// - public event System.Action Completed - { - add - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(AssetHandle)} is invalid"); - if (Provider.IsDone) - value.Invoke(this); - else - _callback += value; - } - remove - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(AssetHandle)} is invalid"); - _callback -= value; - } - } + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(AssetHandle)} is invalid"); + if (Provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(AssetHandle)} is invalid"); + _callback -= value; + } + } - /// - /// 等待异步执行完毕 - /// - public void WaitForAsyncComplete() - { - if (IsValidWithWarning == false) - return; - Provider.WaitForAsyncComplete(); - } + /// + /// 等待异步执行完毕 + /// + public void WaitForAsyncComplete() + { + if (IsValidWithWarning == false) + return; + Provider.WaitForAsyncComplete(); + } - /// - /// 释放资源句柄 - /// - public void Release() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Release() + { + this.ReleaseInternal(); + } - /// - /// 释放资源句柄 - /// - public void Dispose() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Dispose() + { + this.ReleaseInternal(); + } - /// - /// 资源对象 - /// - public UnityEngine.Object AssetObject - { - get - { - if (IsValidWithWarning == false) - return null; - return Provider.AssetObject; - } - } + /// + /// 资源对象 + /// + public UnityEngine.Object AssetObject + { + get + { + if (IsValidWithWarning == false) + return null; + return Provider.AssetObject; + } + } - /// - /// 获取资源对象 - /// - /// 资源类型 - public TAsset GetAssetObject() where TAsset : UnityEngine.Object - { - if (IsValidWithWarning == false) - return null; - return Provider.AssetObject as TAsset; - } + /// + /// 获取资源对象 + /// + /// 资源类型 + public TAsset GetAssetObject() where TAsset : UnityEngine.Object + { + if (IsValidWithWarning == false) + return null; + return Provider.AssetObject as TAsset; + } - /// - /// 同步初始化游戏对象 - /// - public GameObject InstantiateSync() - { - return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, null, false); - } - public GameObject InstantiateSync(Transform parent) - { - return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, false); - } - public GameObject InstantiateSync(Transform parent, bool worldPositionStays) - { - return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays); - } - public GameObject InstantiateSync(Vector3 position, Quaternion rotation) - { - return InstantiateSyncInternal(true, position, rotation, null, false); - } - public GameObject InstantiateSync(Vector3 position, Quaternion rotation, Transform parent) - { - return InstantiateSyncInternal(true, position, rotation, parent, false); - } + /// + /// 同步初始化游戏对象 + /// + public GameObject InstantiateSync() + { + return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, null, false); + } + public GameObject InstantiateSync(Transform parent) + { + return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, false); + } + public GameObject InstantiateSync(Transform parent, bool worldPositionStays) + { + return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays); + } + public GameObject InstantiateSync(Vector3 position, Quaternion rotation) + { + return InstantiateSyncInternal(true, position, rotation, null, false); + } + public GameObject InstantiateSync(Vector3 position, Quaternion rotation, Transform parent) + { + return InstantiateSyncInternal(true, position, rotation, parent, false); + } - /// - /// 异步初始化游戏对象 - /// - public InstantiateOperation InstantiateAsync() - { - return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, null, false); - } - public InstantiateOperation InstantiateAsync(Transform parent) - { - return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, false); - } - public InstantiateOperation InstantiateAsync(Transform parent, bool worldPositionStays) - { - return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays); - } - public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation) - { - return InstantiateAsyncInternal(true, position, rotation, null, false); - } - public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent) - { - return InstantiateAsyncInternal(true, position, rotation, parent, false); - } + /// + /// 异步初始化游戏对象 + /// + public InstantiateOperation InstantiateAsync() + { + return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, null, false); + } + public InstantiateOperation InstantiateAsync(Transform parent) + { + return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, false); + } + public InstantiateOperation InstantiateAsync(Transform parent, bool worldPositionStays) + { + return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays); + } + public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation) + { + return InstantiateAsyncInternal(true, position, rotation, null, false); + } + public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent) + { + return InstantiateAsyncInternal(true, position, rotation, parent, false); + } - private GameObject InstantiateSyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) - { - if (IsValidWithWarning == false) - return null; - if (Provider.AssetObject == null) - return null; + private GameObject InstantiateSyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) + { + if (IsValidWithWarning == false) + return null; + if (Provider.AssetObject == null) + return null; - return InstantiateOperation.InstantiateInternal(Provider.AssetObject, setPositionAndRotation, position, rotation, parent, worldPositionStays); - } - private InstantiateOperation InstantiateAsyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) - { - string packageName = GetAssetInfo().PackageName; - InstantiateOperation operation = new InstantiateOperation(this, setPositionAndRotation, position, rotation, parent, worldPositionStays); - OperationSystem.StartOperation(packageName, operation); - return operation; - } - } + return InstantiateOperation.InstantiateInternal(Provider.AssetObject, setPositionAndRotation, position, rotation, parent, worldPositionStays); + } + private InstantiateOperation InstantiateAsyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) + { + string packageName = GetAssetInfo().PackageName; + InstantiateOperation operation = new InstantiateOperation(this, setPositionAndRotation, position, rotation, parent, worldPositionStays); + OperationSystem.StartOperation(packageName, operation); + return operation; + } + } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs index f78b0d8..1a6262c 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs @@ -3,159 +3,159 @@ using System.Collections; namespace YooAsset { - public abstract class HandleBase : IEnumerator - { - private readonly AssetInfo _assetInfo; - internal ProviderBase Provider { private set; get; } + public abstract class HandleBase : IEnumerator + { + private readonly AssetInfo _assetInfo; + internal ProviderBase Provider { private set; get; } - internal HandleBase(ProviderBase provider) - { - Provider = provider; - _assetInfo = provider.MainAssetInfo; - } - internal abstract void InvokeCallback(); + internal HandleBase(ProviderBase provider) + { + Provider = provider; + _assetInfo = provider.MainAssetInfo; + } + internal abstract void InvokeCallback(); - /// - /// 获取资源信息 - /// - public AssetInfo GetAssetInfo() - { - return _assetInfo; - } + /// + /// 获取资源信息 + /// + public AssetInfo GetAssetInfo() + { + return _assetInfo; + } - /// - /// 获取下载报告 - /// - public DownloadStatus GetDownloadStatus() - { - if (IsValidWithWarning == false) - { - return DownloadStatus.CreateDefaultStatus(); - } - return Provider.GetDownloadStatus(); - } + /// + /// 获取下载报告 + /// + public DownloadStatus GetDownloadStatus() + { + if (IsValidWithWarning == false) + { + return DownloadStatus.CreateDefaultStatus(); + } + return Provider.GetDownloadStatus(); + } - /// - /// 当前状态 - /// - public EOperationStatus Status - { - get - { - if (IsValidWithWarning == false) - return EOperationStatus.None; + /// + /// 当前状态 + /// + public EOperationStatus Status + { + get + { + if (IsValidWithWarning == false) + return EOperationStatus.None; - return Provider.Status; - } - } + return Provider.Status; + } + } - /// - /// 最近的错误信息 - /// - public string LastError - { - get - { - if (IsValidWithWarning == false) - return string.Empty; - return Provider.Error; - } - } + /// + /// 最近的错误信息 + /// + public string LastError + { + get + { + if (IsValidWithWarning == false) + return string.Empty; + return Provider.Error; + } + } - /// - /// 加载进度 - /// - public float Progress - { - get - { - if (IsValidWithWarning == false) - return 0; - return Provider.Progress; - } - } + /// + /// 加载进度 + /// + public float Progress + { + get + { + if (IsValidWithWarning == false) + return 0; + return Provider.Progress; + } + } - /// - /// 是否加载完毕 - /// - public bool IsDone - { - get - { - if (IsValidWithWarning == false) - return false; - return Provider.IsDone; - } - } + /// + /// 是否加载完毕 + /// + public bool IsDone + { + get + { + if (IsValidWithWarning == false) + return false; + return Provider.IsDone; + } + } - /// - /// 句柄是否有效 - /// - public bool IsValid - { - get - { - if (Provider != null && Provider.IsDestroyed == false) - return true; - else - return false; - } - } + /// + /// 句柄是否有效 + /// + public bool IsValid + { + get + { + if (Provider != null && Provider.IsDestroyed == false) + return true; + else + return false; + } + } - /// - /// 句柄是否有效 - /// - internal bool IsValidWithWarning - { - get - { - if (Provider != null && Provider.IsDestroyed == false) - { - return true; - } - else - { - if (Provider == null) - YooLogger.Warning($"Operation handle is released : {_assetInfo.AssetPath}"); - else if (Provider.IsDestroyed) - YooLogger.Warning($"Provider is destroyed : {_assetInfo.AssetPath}"); - return false; - } - } - } + /// + /// 句柄是否有效 + /// + internal bool IsValidWithWarning + { + get + { + if (Provider != null && Provider.IsDestroyed == false) + { + return true; + } + else + { + if (Provider == null) + YooLogger.Warning($"Operation handle is released : {_assetInfo.AssetPath}"); + else if (Provider.IsDestroyed) + YooLogger.Warning($"Provider is destroyed : {_assetInfo.AssetPath}"); + return false; + } + } + } - /// - /// 释放句柄 - /// - internal void ReleaseInternal() - { - if (IsValidWithWarning == false) - return; - Provider.ReleaseHandle(this); - Provider = null; - } + /// + /// 释放句柄 + /// + internal void ReleaseInternal() + { + if (IsValidWithWarning == false) + return; + Provider.ReleaseHandle(this); + Provider = null; + } - #region 异步操作相关 - /// - /// 异步操作任务 - /// - public System.Threading.Tasks.Task Task - { - get { return Provider.Task; } - } + #region 异步操作相关 + /// + /// 异步操作任务 + /// + public System.Threading.Tasks.Task Task + { + get { return Provider.Task; } + } - // 协程相关 - bool IEnumerator.MoveNext() - { - return !IsDone; - } - void IEnumerator.Reset() - { - } - object IEnumerator.Current - { - get { return Provider; } - } - #endregion - } + // 协程相关 + bool IEnumerator.MoveNext() + { + return !IsDone; + } + void IEnumerator.Reset() + { + } + object IEnumerator.Current + { + get { return Provider; } + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs index f700973..446e2fe 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs @@ -4,97 +4,97 @@ using System.Text; namespace YooAsset { - public class RawFileHandle : HandleBase, IDisposable - { - private System.Action _callback; + public class RawFileHandle : HandleBase, IDisposable + { + private System.Action _callback; - internal RawFileHandle(ProviderBase provider) : base(provider) - { - } - internal override void InvokeCallback() - { - _callback?.Invoke(this); - } + internal RawFileHandle(ProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + _callback?.Invoke(this); + } - /// - /// 完成委托 - /// - public event System.Action Completed - { - add - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(RawFileHandle)} is invalid"); - if (Provider.IsDone) - value.Invoke(this); - else - _callback += value; - } - remove - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(RawFileHandle)} is invalid"); - _callback -= value; - } - } + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(RawFileHandle)} is invalid"); + if (Provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(RawFileHandle)} is invalid"); + _callback -= value; + } + } - /// - /// 等待异步执行完毕 - /// - public void WaitForAsyncComplete() - { - if (IsValidWithWarning == false) - return; - Provider.WaitForAsyncComplete(); - } + /// + /// 等待异步执行完毕 + /// + public void WaitForAsyncComplete() + { + if (IsValidWithWarning == false) + return; + Provider.WaitForAsyncComplete(); + } - /// - /// 释放资源句柄 - /// - public void Release() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Release() + { + this.ReleaseInternal(); + } - /// - /// 释放资源句柄 - /// - public void Dispose() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Dispose() + { + this.ReleaseInternal(); + } - /// - /// 获取原生文件的二进制数据 - /// - public byte[] GetRawFileData() - { - if (IsValidWithWarning == false) - return null; - string filePath = Provider.RawFilePath; - return FileUtility.ReadAllBytes(filePath); - } + /// + /// 获取原生文件的二进制数据 + /// + public byte[] GetRawFileData() + { + if (IsValidWithWarning == false) + return null; + string filePath = Provider.RawFilePath; + return FileUtility.ReadAllBytes(filePath); + } - /// - /// 获取原生文件的文本数据 - /// - public string GetRawFileText() - { - if (IsValidWithWarning == false) - return null; - string filePath = Provider.RawFilePath; - return FileUtility.ReadAllText(filePath); - } + /// + /// 获取原生文件的文本数据 + /// + public string GetRawFileText() + { + if (IsValidWithWarning == false) + return null; + string filePath = Provider.RawFilePath; + return FileUtility.ReadAllText(filePath); + } - /// - /// 获取原生文件的路径 - /// - public string GetRawFilePath() - { - if (IsValidWithWarning == false) - return string.Empty; - return Provider.RawFilePath; - } - } + /// + /// 获取原生文件的路径 + /// + public string GetRawFilePath() + { + if (IsValidWithWarning == false) + return string.Empty; + return Provider.RawFilePath; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs index d057034..fcca55f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs @@ -2,174 +2,174 @@ namespace YooAsset { - public class SceneHandle : HandleBase - { - private System.Action _callback; - internal string PackageName { set; get; } + public class SceneHandle : HandleBase + { + private System.Action _callback; + internal string PackageName { set; get; } - internal SceneHandle(ProviderBase provider) : base(provider) - { - } - internal override void InvokeCallback() - { - _callback?.Invoke(this); - } + internal SceneHandle(ProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + _callback?.Invoke(this); + } - /// - /// 完成委托 - /// - public event System.Action Completed - { - add - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(SceneHandle)} is invalid"); - if (Provider.IsDone) - value.Invoke(this); - else - _callback += value; - } - remove - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(SceneHandle)} is invalid"); - _callback -= value; - } - } + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(SceneHandle)} is invalid"); + if (Provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(SceneHandle)} is invalid"); + _callback -= value; + } + } - /// - /// 场景名称 - /// - public string SceneName - { - get - { - if (IsValidWithWarning == false) - return string.Empty; - return Provider.SceneName; - } - } + /// + /// 场景名称 + /// + public string SceneName + { + get + { + if (IsValidWithWarning == false) + return string.Empty; + return Provider.SceneName; + } + } - /// - /// 场景对象 - /// - public Scene SceneObject - { - get - { - if (IsValidWithWarning == false) - return new Scene(); - return Provider.SceneObject; - } - } + /// + /// 场景对象 + /// + public Scene SceneObject + { + get + { + if (IsValidWithWarning == false) + return new Scene(); + return Provider.SceneObject; + } + } - /// - /// 激活场景(当同时存在多个场景时用于切换激活场景) - /// - public bool ActivateScene() - { - if (IsValidWithWarning == false) - return false; + /// + /// 激活场景(当同时存在多个场景时用于切换激活场景) + /// + public bool ActivateScene() + { + if (IsValidWithWarning == false) + return false; - if (SceneObject.IsValid() && SceneObject.isLoaded) - { - return SceneManager.SetActiveScene(SceneObject); - } - else - { - YooLogger.Warning($"Scene is invalid or not loaded : {SceneObject.name}"); - return false; - } - } + if (SceneObject.IsValid() && SceneObject.isLoaded) + { + return SceneManager.SetActiveScene(SceneObject); + } + else + { + YooLogger.Warning($"Scene is invalid or not loaded : {SceneObject.name}"); + return false; + } + } - /// - /// 解除场景加载挂起操作 - /// - public bool UnSuspend() - { - if (IsValidWithWarning == false) - return false; + /// + /// 解除场景加载挂起操作 + /// + public bool UnSuspend() + { + if (IsValidWithWarning == false) + return false; - if (SceneObject.IsValid()) - { - if (Provider is DatabaseSceneProvider) - { - var temp = Provider as DatabaseSceneProvider; - return temp.UnSuspendLoad(); - } - else if (Provider is BundledSceneProvider) - { - var temp = Provider as BundledSceneProvider; - return temp.UnSuspendLoad(); - } - else - { - throw new System.NotImplementedException(); - } - } - else - { - YooLogger.Warning($"Scene is invalid : {SceneObject.name}"); - return false; - } - } + if (SceneObject.IsValid()) + { + if (Provider is DatabaseSceneProvider) + { + var temp = Provider as DatabaseSceneProvider; + return temp.UnSuspendLoad(); + } + else if (Provider is BundledSceneProvider) + { + var temp = Provider as BundledSceneProvider; + return temp.UnSuspendLoad(); + } + else + { + throw new System.NotImplementedException(); + } + } + else + { + YooLogger.Warning($"Scene is invalid : {SceneObject.name}"); + return false; + } + } - /// - /// 是否为主场景 - /// - public bool IsMainScene() - { - if (IsValidWithWarning == false) - return false; + /// + /// 是否为主场景 + /// + public bool IsMainScene() + { + if (IsValidWithWarning == false) + return false; - if (Provider is DatabaseSceneProvider) - { - var temp = Provider as DatabaseSceneProvider; - return temp.SceneMode == LoadSceneMode.Single; - } - else if (Provider is BundledSceneProvider) - { - var temp = Provider as BundledSceneProvider; - return temp.SceneMode == LoadSceneMode.Single; - } - else - { - throw new System.NotImplementedException(); - } - } + if (Provider is DatabaseSceneProvider) + { + var temp = Provider as DatabaseSceneProvider; + return temp.SceneMode == LoadSceneMode.Single; + } + else if (Provider is BundledSceneProvider) + { + var temp = Provider as BundledSceneProvider; + return temp.SceneMode == LoadSceneMode.Single; + } + else + { + throw new System.NotImplementedException(); + } + } - /// - /// 异步卸载子场景 - /// - public UnloadSceneOperation UnloadAsync() - { - string packageName = GetAssetInfo().PackageName; + /// + /// 异步卸载子场景 + /// + public UnloadSceneOperation UnloadAsync() + { + string packageName = GetAssetInfo().PackageName; - // 如果句柄无效 - if (IsValidWithWarning == false) - { - string error = $"{nameof(SceneHandle)} is invalid."; - var operation = new UnloadSceneOperation(error); - OperationSystem.StartOperation(packageName, operation); - return operation; - } + // 如果句柄无效 + if (IsValidWithWarning == false) + { + string error = $"{nameof(SceneHandle)} is invalid."; + var operation = new UnloadSceneOperation(error); + OperationSystem.StartOperation(packageName, operation); + return operation; + } - // 如果是主场景 - if (IsMainScene()) - { - string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !"; - YooLogger.Error(error); - var operation = new UnloadSceneOperation(error); - OperationSystem.StartOperation(packageName, operation); - return operation; - } + // 如果是主场景 + if (IsMainScene()) + { + string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !"; + YooLogger.Error(error); + var operation = new UnloadSceneOperation(error); + OperationSystem.StartOperation(packageName, operation); + return operation; + } - // 卸载子场景 - { - var operation = new UnloadSceneOperation(Provider); - OperationSystem.StartOperation(packageName, operation); - return operation; - } - } - } + // 卸载子场景 + { + var operation = new UnloadSceneOperation(Provider); + OperationSystem.StartOperation(packageName, operation); + return operation; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs index 7c9ed89..48421a0 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs @@ -3,117 +3,117 @@ using System.Collections.Generic; namespace YooAsset { - public sealed class SubAssetsHandle : HandleBase, IDisposable - { - private System.Action _callback; + public sealed class SubAssetsHandle : HandleBase, IDisposable + { + private System.Action _callback; - internal SubAssetsHandle(ProviderBase provider) : base(provider) - { - } - internal override void InvokeCallback() - { - _callback?.Invoke(this); - } + internal SubAssetsHandle(ProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + _callback?.Invoke(this); + } - /// - /// 完成委托 - /// - public event System.Action Completed - { - add - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(SubAssetsHandle)} is invalid"); - if (Provider.IsDone) - value.Invoke(this); - else - _callback += value; - } - remove - { - if (IsValidWithWarning == false) - throw new System.Exception($"{nameof(SubAssetsHandle)} is invalid"); - _callback -= value; - } - } + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(SubAssetsHandle)} is invalid"); + if (Provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValidWithWarning == false) + throw new System.Exception($"{nameof(SubAssetsHandle)} is invalid"); + _callback -= value; + } + } - /// - /// 等待异步执行完毕 - /// - public void WaitForAsyncComplete() - { - if (IsValidWithWarning == false) - return; - Provider.WaitForAsyncComplete(); - } + /// + /// 等待异步执行完毕 + /// + public void WaitForAsyncComplete() + { + if (IsValidWithWarning == false) + return; + Provider.WaitForAsyncComplete(); + } - /// - /// 释放资源句柄 - /// - public void Release() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Release() + { + this.ReleaseInternal(); + } - /// - /// 释放资源句柄 - /// - public void Dispose() - { - this.ReleaseInternal(); - } + /// + /// 释放资源句柄 + /// + public void Dispose() + { + this.ReleaseInternal(); + } - /// - /// 子资源对象集合 - /// - public UnityEngine.Object[] AllAssetObjects - { - get - { - if (IsValidWithWarning == false) - return null; - return Provider.AllAssetObjects; - } - } + /// + /// 子资源对象集合 + /// + public UnityEngine.Object[] AllAssetObjects + { + get + { + if (IsValidWithWarning == false) + return null; + return Provider.AllAssetObjects; + } + } - /// - /// 获取子资源对象 - /// - /// 子资源对象类型 - /// 子资源对象名称 - public TObject GetSubAssetObject(string assetName) where TObject : UnityEngine.Object - { - if (IsValidWithWarning == false) - return null; + /// + /// 获取子资源对象 + /// + /// 子资源对象类型 + /// 子资源对象名称 + public TObject GetSubAssetObject(string assetName) where TObject : UnityEngine.Object + { + if (IsValidWithWarning == false) + return null; - foreach (var assetObject in Provider.AllAssetObjects) - { - if (assetObject.name == assetName) - return assetObject as TObject; - } + foreach (var assetObject in Provider.AllAssetObjects) + { + if (assetObject.name == assetName) + return assetObject as TObject; + } - YooLogger.Warning($"Not found sub asset object : {assetName}"); - return null; - } + YooLogger.Warning($"Not found sub asset object : {assetName}"); + return null; + } - /// - /// 获取所有的子资源对象集合 - /// - /// 子资源对象类型 - public TObject[] GetSubAssetObjects() where TObject : UnityEngine.Object - { - if (IsValidWithWarning == false) - return null; + /// + /// 获取所有的子资源对象集合 + /// + /// 子资源对象类型 + public TObject[] GetSubAssetObjects() where TObject : UnityEngine.Object + { + if (IsValidWithWarning == false) + return null; - List ret = new List(Provider.AllAssetObjects.Length); - foreach (var assetObject in Provider.AllAssetObjects) - { - var retObject = assetObject as TObject; - if (retObject != null) - ret.Add(retObject); - } - return ret.ToArray(); - } - } + List ret = new List(Provider.AllAssetObjects.Length); + foreach (var assetObject in Provider.AllAssetObjects) + { + var retObject = assetObject as TObject; + if (retObject != null) + ret.Add(retObject); + } + return ret.ToArray(); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs index 5d69f18..3bfc1f2 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleFileLoader.cs @@ -6,289 +6,289 @@ using UnityEngine; namespace YooAsset { - internal sealed class AssetBundleFileLoader : BundleLoaderBase - { - private enum ESteps - { - None = 0, - Download, - CheckDownload, - Unpack, - CheckUnpack, - LoadBundleFile, - LoadDeliveryFile, - CheckLoadFile, - Done, - } + internal sealed class AssetBundleFileLoader : BundleLoaderBase + { + private enum ESteps + { + None = 0, + Download, + CheckDownload, + Unpack, + CheckUnpack, + LoadBundleFile, + LoadDeliveryFile, + CheckLoadFile, + Done, + } - private ESteps _steps = ESteps.None; - private bool _isWaitForAsyncComplete = false; - private bool _isShowWaitForAsyncError = false; - private DownloaderBase _unpacker; - private DownloaderBase _downloader; - private AssetBundleCreateRequest _createRequest; - private Stream _managedStream; + private ESteps _steps = ESteps.None; + private bool _isWaitForAsyncComplete = false; + private bool _isShowWaitForAsyncError = false; + private DownloaderBase _unpacker; + private DownloaderBase _downloader; + private AssetBundleCreateRequest _createRequest; + private Stream _managedStream; - public AssetBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) - { - } + public AssetBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) + { + } - /// - /// 轮询更新 - /// - public override void Update() - { - if (_steps == ESteps.Done) - return; + /// + /// 轮询更新 + /// + public override void Update() + { + if (_steps == ESteps.Done) + return; - if (_steps == ESteps.None) - { - if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - { - _steps = ESteps.Download; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) - { + if (_steps == ESteps.None) + { + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + { + _steps = ESteps.Download; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) + { #if UNITY_ANDROID - if (MainBundleInfo.Bundle.Encrypted) - { - _steps = ESteps.Unpack; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else - { - _steps = ESteps.LoadBundleFile; - FileLoadPath = MainBundleInfo.BuildinFilePath; - } + if (MainBundleInfo.Bundle.Encrypted) + { + _steps = ESteps.Unpack; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else + { + _steps = ESteps.LoadBundleFile; + FileLoadPath = MainBundleInfo.BuildinFilePath; + } #else _steps = ESteps.LoadBundleFile; FileLoadPath = MainBundleInfo.BuildinFilePath; #endif - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) - { - _steps = ESteps.LoadBundleFile; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromDelivery) - { - _steps = ESteps.LoadDeliveryFile; - FileLoadPath = MainBundleInfo.DeliveryFilePath; - } - else - { - throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); - } - } + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) + { + _steps = ESteps.LoadBundleFile; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromDelivery) + { + _steps = ESteps.LoadDeliveryFile; + FileLoadPath = MainBundleInfo.DeliveryFilePath; + } + else + { + throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); + } + } - // 1. 从服务器下载 - if (_steps == ESteps.Download) - { - _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); - _downloader.SendRequest(); - _steps = ESteps.CheckDownload; - } + // 1. 从服务器下载 + if (_steps == ESteps.Download) + { + _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); + _downloader.SendRequest(); + _steps = ESteps.CheckDownload; + } - // 2. 检测服务器下载结果 - if (_steps == ESteps.CheckDownload) - { - DownloadProgress = _downloader.DownloadProgress; - DownloadedBytes = _downloader.DownloadedBytes; - if (_downloader.IsDone() == false) - return; + // 2. 检测服务器下载结果 + if (_steps == ESteps.CheckDownload) + { + DownloadProgress = _downloader.DownloadProgress; + DownloadedBytes = _downloader.DownloadedBytes; + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = _downloader.GetLastError(); - } - else - { - _steps = ESteps.LoadBundleFile; - return; //下载完毕等待一帧再去加载! - } - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _downloader.GetLastError(); + } + else + { + _steps = ESteps.LoadBundleFile; + return; //下载完毕等待一帧再去加载! + } + } - // 3. 内置文件解压 - if (_steps == ESteps.Unpack) - { - int failedTryAgain = 1; - _unpacker = MainBundleInfo.CreateUnpacker(failedTryAgain); - _unpacker.SendRequest(); - _steps = ESteps.CheckUnpack; - } + // 3. 内置文件解压 + if (_steps == ESteps.Unpack) + { + int failedTryAgain = 1; + _unpacker = MainBundleInfo.CreateUnpacker(failedTryAgain); + _unpacker.SendRequest(); + _steps = ESteps.CheckUnpack; + } - // 4.检测内置文件解压结果 - if (_steps == ESteps.CheckUnpack) - { - DownloadProgress = _unpacker.DownloadProgress; - DownloadedBytes = _unpacker.DownloadedBytes; - if (_unpacker.IsDone() == false) - return; + // 4.检测内置文件解压结果 + if (_steps == ESteps.CheckUnpack) + { + DownloadProgress = _unpacker.DownloadProgress; + DownloadedBytes = _unpacker.DownloadedBytes; + if (_unpacker.IsDone() == false) + return; - if (_unpacker.HasError()) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = _unpacker.GetLastError(); - } - else - { - _steps = ESteps.LoadBundleFile; - } - } + if (_unpacker.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _unpacker.GetLastError(); + } + else + { + _steps = ESteps.LoadBundleFile; + } + } - // 5. 加载AssetBundle - if (_steps == ESteps.LoadBundleFile) - { + // 5. 加载AssetBundle + if (_steps == ESteps.LoadBundleFile) + { #if UNITY_EDITOR - // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。 - if (System.IO.File.Exists(FileLoadPath) == false) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = $"Not found assetBundle file : {FileLoadPath}"; - YooLogger.Error(LastError); - return; - } + // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。 + if (System.IO.File.Exists(FileLoadPath) == false) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = $"Not found assetBundle file : {FileLoadPath}"; + YooLogger.Error(LastError); + return; + } #endif - // 设置下载进度 - DownloadProgress = 1f; - DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; + // 设置下载进度 + DownloadProgress = 1f; + DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; - // 加载AssetBundle资源对象 - // 注意:解密服务类可能会返回空的对象。 - if (_isWaitForAsyncComplete) - CacheBundle = MainBundleInfo.LoadAssetBundle(FileLoadPath, out _managedStream); - else - _createRequest = MainBundleInfo.LoadAssetBundleAsync(FileLoadPath, out _managedStream); + // 加载AssetBundle资源对象 + // 注意:解密服务类可能会返回空的对象。 + if (_isWaitForAsyncComplete) + CacheBundle = MainBundleInfo.LoadAssetBundle(FileLoadPath, out _managedStream); + else + _createRequest = MainBundleInfo.LoadAssetBundleAsync(FileLoadPath, out _managedStream); - _steps = ESteps.CheckLoadFile; - } + _steps = ESteps.CheckLoadFile; + } - // 6. 加载AssetBundle - if (_steps == ESteps.LoadDeliveryFile) - { - // 设置下载进度 - DownloadProgress = 1f; - DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; + // 6. 加载AssetBundle + if (_steps == ESteps.LoadDeliveryFile) + { + // 设置下载进度 + DownloadProgress = 1f; + DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; - // Load assetBundle file - if (_isWaitForAsyncComplete) - CacheBundle = MainBundleInfo.LoadDeliveryAssetBundle(FileLoadPath); - else - _createRequest = MainBundleInfo.LoadDeliveryAssetBundleAsync(FileLoadPath); + // Load assetBundle file + if (_isWaitForAsyncComplete) + CacheBundle = MainBundleInfo.LoadDeliveryAssetBundle(FileLoadPath); + else + _createRequest = MainBundleInfo.LoadDeliveryAssetBundleAsync(FileLoadPath); - _steps = ESteps.CheckLoadFile; - } + _steps = ESteps.CheckLoadFile; + } - // 7. 检测AssetBundle加载结果 - if (_steps == ESteps.CheckLoadFile) - { - if (_createRequest != null) - { - if (_isWaitForAsyncComplete || IsForceDestroyComplete) - { - // 强制挂起主线程(注意:该操作会很耗时) - YooLogger.Warning("Suspend the main thread to load unity bundle."); - CacheBundle = _createRequest.assetBundle; - } - else - { - if (_createRequest.isDone == false) - return; - CacheBundle = _createRequest.assetBundle; - } - } + // 7. 检测AssetBundle加载结果 + if (_steps == ESteps.CheckLoadFile) + { + if (_createRequest != null) + { + if (_isWaitForAsyncComplete || IsForceDestroyComplete) + { + // 强制挂起主线程(注意:该操作会很耗时) + YooLogger.Warning("Suspend the main thread to load unity bundle."); + CacheBundle = _createRequest.assetBundle; + } + else + { + if (_createRequest.isDone == false) + return; + CacheBundle = _createRequest.assetBundle; + } + } - // Check error - if (CacheBundle == null) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = $"Failed to load assetBundle : {MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(LastError); + // Check error + if (CacheBundle == null) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = $"Failed to load assetBundle : {MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(LastError); - // 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。 - // 在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性! - if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) - { - var result = MainBundleInfo.VerifySelf(); - if (result != EVerifyResult.Succeed) - { - YooLogger.Error($"Found possibly corrupt file ! {MainBundleInfo.Bundle.CacheGUID} Verify result : {result}"); - MainBundleInfo.CacheDiscard(); - } - } - } - else - { - _steps = ESteps.Done; - Status = EStatus.Succeed; - } - } - } + // 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。 + // 在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性! + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) + { + var result = MainBundleInfo.VerifySelf(); + if (result != EVerifyResult.Succeed) + { + YooLogger.Error($"Found possibly corrupt file ! {MainBundleInfo.Bundle.CacheGUID} Verify result : {result}"); + MainBundleInfo.CacheDiscard(); + } + } + } + else + { + _steps = ESteps.Done; + Status = EStatus.Succeed; + } + } + } - /// - /// 销毁 - /// - public override void Destroy() - { - base.Destroy(); + /// + /// 销毁 + /// + public override void Destroy() + { + base.Destroy(); - if (_managedStream != null) - { - _managedStream.Close(); - _managedStream.Dispose(); - _managedStream = null; - } - } + if (_managedStream != null) + { + _managedStream.Close(); + _managedStream.Dispose(); + _managedStream = null; + } + } - /// - /// 主线程等待异步操作完毕 - /// - public override void WaitForAsyncComplete() - { - _isWaitForAsyncComplete = true; + /// + /// 主线程等待异步操作完毕 + /// + public override void WaitForAsyncComplete() + { + _isWaitForAsyncComplete = true; - int frame = 1000; - while (true) - { - // 文件解压 - if (_unpacker != null) - { - if (_unpacker.IsDone() == false) - { - _unpacker.WaitForAsyncComplete = true; - _unpacker.Update(); - continue; - } - } + int frame = 1000; + while (true) + { + // 文件解压 + if (_unpacker != null) + { + if (_unpacker.IsDone() == false) + { + _unpacker.WaitForAsyncComplete = true; + _unpacker.Update(); + continue; + } + } - // 保险机制 - // 注意:如果需要从WEB端下载资源,可能会触发保险机制! - frame--; - if (frame == 0) - { - if (_isShowWaitForAsyncError == false) - { - _isShowWaitForAsyncError = true; - YooLogger.Error($"{nameof(WaitForAsyncComplete)} failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); - } - break; - } + // 保险机制 + // 注意:如果需要从WEB端下载资源,可能会触发保险机制! + frame--; + if (frame == 0) + { + if (_isShowWaitForAsyncError == false) + { + _isShowWaitForAsyncError = true; + YooLogger.Error($"{nameof(WaitForAsyncComplete)} failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); + } + break; + } - // 驱动流程 - Update(); + // 驱动流程 + Update(); - // 完成后退出 - if (IsDone()) - break; - } - } - } + // 完成后退出 + if (IsDone()) + break; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleWebLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleWebLoader.cs index 20d013c..2d43aed 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleWebLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/AssetBundleWebLoader.cs @@ -7,105 +7,105 @@ using UnityEngine.Networking; namespace YooAsset { - /// - /// WebGL平台加载器 - /// - internal sealed class AssetBundleWebLoader : BundleLoaderBase - { - private enum ESteps - { - None = 0, - LoadWebSiteFile, - LoadRemoteFile, - CheckLoadFile, - Done, - } + /// + /// WebGL平台加载器 + /// + internal sealed class AssetBundleWebLoader : BundleLoaderBase + { + private enum ESteps + { + None = 0, + LoadWebSiteFile, + LoadRemoteFile, + CheckLoadFile, + Done, + } - private ESteps _steps = ESteps.None; - private DownloaderBase _downloader; + private ESteps _steps = ESteps.None; + private DownloaderBase _downloader; - public AssetBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) - { - } + public AssetBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) + { + } - /// - /// 轮询更新 - /// - public override void Update() - { - if (_steps == ESteps.Done) - return; + /// + /// 轮询更新 + /// + public override void Update() + { + if (_steps == ESteps.Done) + return; - if (_steps == ESteps.None) - { - if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - { - _steps = ESteps.LoadRemoteFile; - FileLoadPath = string.Empty; - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) - { - _steps = ESteps.LoadWebSiteFile; - FileLoadPath = string.Empty; - } - else - { - throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); - } - } + if (_steps == ESteps.None) + { + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + { + _steps = ESteps.LoadRemoteFile; + FileLoadPath = string.Empty; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) + { + _steps = ESteps.LoadWebSiteFile; + FileLoadPath = string.Empty; + } + else + { + throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); + } + } - // 1. 跨域获取资源包 - if (_steps == ESteps.LoadRemoteFile) - { - _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); - _downloader.SendRequest(true); - _steps = ESteps.CheckLoadFile; - } + // 1. 跨域获取资源包 + if (_steps == ESteps.LoadRemoteFile) + { + _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); + _downloader.SendRequest(true); + _steps = ESteps.CheckLoadFile; + } - // 2. 从站点获取资源包 - if (_steps == ESteps.LoadWebSiteFile) - { - _downloader = MainBundleInfo.CreateUnpacker(int.MaxValue); - _downloader.SendRequest(true); - _steps = ESteps.CheckLoadFile; - } + // 2. 从站点获取资源包 + if (_steps == ESteps.LoadWebSiteFile) + { + _downloader = MainBundleInfo.CreateUnpacker(int.MaxValue); + _downloader.SendRequest(true); + _steps = ESteps.CheckLoadFile; + } - // 3. 检测加载结果 - if (_steps == ESteps.CheckLoadFile) - { - DownloadProgress = _downloader.DownloadProgress; - DownloadedBytes = _downloader.DownloadedBytes; - if (_downloader.IsDone() == false) - return; + // 3. 检测加载结果 + if (_steps == ESteps.CheckLoadFile) + { + DownloadProgress = _downloader.DownloadProgress; + DownloadedBytes = _downloader.DownloadedBytes; + if (_downloader.IsDone() == false) + return; - CacheBundle = _downloader.GetAssetBundle(); - if (CacheBundle == null) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = $"AssetBundle file is invalid : {MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(LastError); - } - else - { - _steps = ESteps.Done; - Status = EStatus.Succeed; - } - } - } + CacheBundle = _downloader.GetAssetBundle(); + if (CacheBundle == null) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = $"AssetBundle file is invalid : {MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(LastError); + } + else + { + _steps = ESteps.Done; + Status = EStatus.Succeed; + } + } + } - /// - /// 主线程等待异步操作完毕 - /// - public override void WaitForAsyncComplete() - { - if (IsDone() == false) - { - Status = EStatus.Failed; - LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !"; - YooLogger.Error(LastError); - } - } - } + /// + /// 主线程等待异步操作完毕 + /// + public override void WaitForAsyncComplete() + { + if (IsDone() == false) + { + Status = EStatus.Failed; + LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !"; + YooLogger.Error(LastError); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs index 35a1c6d..6039f89 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs @@ -5,177 +5,177 @@ using UnityEngine; namespace YooAsset { - internal abstract class BundleLoaderBase - { - public enum EStatus - { - None = 0, - Succeed, - Failed - } + internal abstract class BundleLoaderBase + { + public enum EStatus + { + None = 0, + Succeed, + Failed + } - /// - /// 所属资源系统 - /// - public ResourceManager Impl { private set; get; } + /// + /// 所属资源系统 + /// + public ResourceManager Impl { private set; get; } - /// - /// 资源包文件信息 - /// - public BundleInfo MainBundleInfo { private set; get; } + /// + /// 资源包文件信息 + /// + public BundleInfo MainBundleInfo { private set; get; } - /// - /// 引用计数 - /// - public int RefCount { private set; get; } + /// + /// 引用计数 + /// + public int RefCount { private set; get; } - /// - /// 加载状态 - /// - public EStatus Status { protected set; get; } + /// + /// 加载状态 + /// + public EStatus Status { protected set; get; } - /// - /// 最近的错误信息 - /// - public string LastError { protected set; get; } + /// + /// 最近的错误信息 + /// + public string LastError { protected set; get; } - /// - /// 是否已经销毁 - /// - public bool IsDestroyed { private set; get; } = false; + /// + /// 是否已经销毁 + /// + public bool IsDestroyed { private set; get; } = false; - private readonly List _providers = new List(100); - private readonly List _removeList = new List(100); - protected bool IsForceDestroyComplete { private set; get; } = false; - internal AssetBundle CacheBundle { set; get; } - internal string FileLoadPath { set; get; } - internal float DownloadProgress { set; get; } - internal ulong DownloadedBytes { set; get; } + private readonly List _providers = new List(100); + private readonly List _removeList = new List(100); + protected bool IsForceDestroyComplete { private set; get; } = false; + internal AssetBundle CacheBundle { set; get; } + internal string FileLoadPath { set; get; } + internal float DownloadProgress { set; get; } + internal ulong DownloadedBytes { set; get; } - public BundleLoaderBase(ResourceManager impl, BundleInfo bundleInfo) - { - Impl = impl; - MainBundleInfo = bundleInfo; - RefCount = 0; - Status = EStatus.None; - } + public BundleLoaderBase(ResourceManager impl, BundleInfo bundleInfo) + { + Impl = impl; + MainBundleInfo = bundleInfo; + RefCount = 0; + Status = EStatus.None; + } - /// - /// 引用(引用计数递加) - /// - public void Reference() - { - RefCount++; - } + /// + /// 引用(引用计数递加) + /// + public void Reference() + { + RefCount++; + } - /// - /// 释放(引用计数递减) - /// - public void Release() - { - RefCount--; - } + /// + /// 释放(引用计数递减) + /// + public void Release() + { + RefCount--; + } - /// - /// 是否完毕(无论成功或失败) - /// - public bool IsDone() - { - return Status == EStatus.Succeed || Status == EStatus.Failed; - } + /// + /// 是否完毕(无论成功或失败) + /// + public bool IsDone() + { + return Status == EStatus.Succeed || Status == EStatus.Failed; + } - /// - /// 是否可以销毁 - /// - public bool CanDestroy() - { - if (IsDone() == false) - return false; + /// + /// 是否可以销毁 + /// + public bool CanDestroy() + { + if (IsDone() == false) + return false; - return RefCount <= 0; - } + return RefCount <= 0; + } - /// - /// 添加附属的资源提供者 - /// - public void AddProvider(ProviderBase provider) - { - if (_providers.Contains(provider) == false) - _providers.Add(provider); - } + /// + /// 添加附属的资源提供者 + /// + public void AddProvider(ProviderBase provider) + { + if (_providers.Contains(provider) == false) + _providers.Add(provider); + } - /// - /// 尝试销毁资源提供者 - /// - public void TryDestroyProviders() - { - // 获取移除列表 - _removeList.Clear(); - foreach (var provider in _providers) - { - if (provider.CanDestroy()) - { - _removeList.Add(provider); - } - } + /// + /// 尝试销毁资源提供者 + /// + public void TryDestroyProviders() + { + // 获取移除列表 + _removeList.Clear(); + foreach (var provider in _providers) + { + if (provider.CanDestroy()) + { + _removeList.Add(provider); + } + } - // 销毁资源提供者 - foreach (var provider in _removeList) - { - _providers.Remove(provider); - provider.Destroy(); - } + // 销毁资源提供者 + foreach (var provider in _removeList) + { + _providers.Remove(provider); + provider.Destroy(); + } - // 移除资源提供者 - if (_removeList.Count > 0) - { - Impl.RemoveBundleProviders(_removeList); - _removeList.Clear(); - } - } + // 移除资源提供者 + if (_removeList.Count > 0) + { + Impl.RemoveBundleProviders(_removeList); + _removeList.Clear(); + } + } - /// - /// 轮询更新 - /// - public abstract void Update(); + /// + /// 轮询更新 + /// + public abstract void Update(); - /// - /// 销毁 - /// - public virtual void Destroy() - { - IsDestroyed = true; + /// + /// 销毁 + /// + public virtual void Destroy() + { + IsDestroyed = true; - // Check fatal - if (RefCount > 0) - throw new Exception($"Bundle file loader ref is not zero : {MainBundleInfo.Bundle.BundleName}"); - if (IsDone() == false) - throw new Exception($"Bundle file loader is not done : {MainBundleInfo.Bundle.BundleName}"); + // Check fatal + if (RefCount > 0) + throw new Exception($"Bundle file loader ref is not zero : {MainBundleInfo.Bundle.BundleName}"); + if (IsDone() == false) + throw new Exception($"Bundle file loader is not done : {MainBundleInfo.Bundle.BundleName}"); - if (CacheBundle != null) - { - CacheBundle.Unload(true); - CacheBundle = null; - } - } + if (CacheBundle != null) + { + CacheBundle.Unload(true); + CacheBundle = null; + } + } - /// - /// 强制销毁资源提供者 - /// - public void ForceDestroyComplete() - { - IsForceDestroyComplete = true; + /// + /// 强制销毁资源提供者 + /// + public void ForceDestroyComplete() + { + IsForceDestroyComplete = true; - // 注意:主动轮询更新完成同步加载 - // 说明:如果正在下载或解压也可以放心销毁。 - Update(); - } + // 注意:主动轮询更新完成同步加载 + // 说明:如果正在下载或解压也可以放心销毁。 + Update(); + } - /// - /// 主线程等待异步操作完毕 - /// - public abstract void WaitForAsyncComplete(); - } + /// + /// 主线程等待异步操作完毕 + /// + public abstract void WaitForAsyncComplete(); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/DependAssetBundles.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/DependAssetBundles.cs index 7252f84..d2e5496 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/DependAssetBundles.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/DependAssetBundles.cs @@ -4,109 +4,109 @@ using System.Collections.Generic; namespace YooAsset { - internal class DependAssetBundles - { - /// - /// 依赖的资源包加载器列表 - /// - internal readonly List DependList; + internal class DependAssetBundles + { + /// + /// 依赖的资源包加载器列表 + /// + internal readonly List DependList; - public DependAssetBundles(List dpendList) - { - DependList = dpendList; - } + public DependAssetBundles(List dpendList) + { + DependList = dpendList; + } - /// - /// 是否已经完成(无论成功或失败) - /// - public bool IsDone() - { - foreach (var loader in DependList) - { - if (loader.IsDone() == false) - return false; - } - return true; - } + /// + /// 是否已经完成(无论成功或失败) + /// + public bool IsDone() + { + foreach (var loader in DependList) + { + if (loader.IsDone() == false) + return false; + } + return true; + } - /// - /// 依赖资源包是否全部加载成功 - /// - public bool IsSucceed() - { - foreach (var loader in DependList) - { - if (loader.Status != BundleLoaderBase.EStatus.Succeed) - { - return false; - } - } - return true; - } + /// + /// 依赖资源包是否全部加载成功 + /// + public bool IsSucceed() + { + foreach (var loader in DependList) + { + if (loader.Status != BundleLoaderBase.EStatus.Succeed) + { + return false; + } + } + return true; + } - /// - /// 获取某个加载失败的资源包错误信息 - /// - public string GetLastError() - { - foreach (var loader in DependList) - { - if (loader.Status != BundleLoaderBase.EStatus.Succeed) - { - return loader.LastError; - } - } - return string.Empty; - } + /// + /// 获取某个加载失败的资源包错误信息 + /// + public string GetLastError() + { + foreach (var loader in DependList) + { + if (loader.Status != BundleLoaderBase.EStatus.Succeed) + { + return loader.LastError; + } + } + return string.Empty; + } - /// - /// 主线程等待异步操作完毕 - /// - public void WaitForAsyncComplete() - { - foreach (var loader in DependList) - { - if (loader.IsDone() == false) - loader.WaitForAsyncComplete(); - } - } + /// + /// 主线程等待异步操作完毕 + /// + public void WaitForAsyncComplete() + { + foreach (var loader in DependList) + { + if (loader.IsDone() == false) + loader.WaitForAsyncComplete(); + } + } - /// - /// 增加引用计数 - /// - public void Reference() - { - foreach (var loader in DependList) - { - loader.Reference(); - } - } + /// + /// 增加引用计数 + /// + public void Reference() + { + foreach (var loader in DependList) + { + loader.Reference(); + } + } - /// - /// 减少引用计数 - /// - public void Release() - { - foreach (var loader in DependList) - { - loader.Release(); - } - } + /// + /// 减少引用计数 + /// + public void Release() + { + foreach (var loader in DependList) + { + loader.Release(); + } + } - /// - /// 获取资源包的调试信息列表 - /// - internal void GetBundleDebugInfos(List output) - { - foreach (var loader in DependList) - { - var bundleInfo = new DebugBundleInfo(); - bundleInfo.BundleName = loader.MainBundleInfo.Bundle.BundleName; - bundleInfo.RefCount = loader.RefCount; - bundleInfo.Status = loader.Status.ToString(); - output.Add(bundleInfo); - } - } - } + /// + /// 获取资源包的调试信息列表 + /// + internal void GetBundleDebugInfos(List output) + { + foreach (var loader in DependList) + { + var bundleInfo = new DebugBundleInfo(); + bundleInfo.BundleName = loader.MainBundleInfo.Bundle.BundleName; + bundleInfo.RefCount = loader.RefCount; + bundleInfo.Status = loader.Status.ToString(); + output.Add(bundleInfo); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleFileLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleFileLoader.cs index 34a3989..61c496a 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleFileLoader.cs @@ -2,187 +2,187 @@ namespace YooAsset { - internal class RawBundleFileLoader : BundleLoaderBase - { - private enum ESteps - { - None, - Download, - CheckDownload, - Unpack, - CheckUnpack, - CheckFile, - Done, - } + internal class RawBundleFileLoader : BundleLoaderBase + { + private enum ESteps + { + None, + Download, + CheckDownload, + Unpack, + CheckUnpack, + CheckFile, + Done, + } - private ESteps _steps = ESteps.None; - private DownloaderBase _unpacker; - private DownloaderBase _downloader; + private ESteps _steps = ESteps.None; + private DownloaderBase _unpacker; + private DownloaderBase _downloader; - public RawBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) - { - } + public RawBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) + { + } - /// - /// 轮询更新 - /// - public override void Update() - { - if (_steps == ESteps.Done) - return; + /// + /// 轮询更新 + /// + public override void Update() + { + if (_steps == ESteps.Done) + return; - if (_steps == ESteps.None) - { - if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - { - _steps = ESteps.Download; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) - { + if (_steps == ESteps.None) + { + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + { + _steps = ESteps.Download; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) + { #if UNITY_ANDROID - _steps = ESteps.Unpack; - FileLoadPath = MainBundleInfo.CachedDataFilePath; + _steps = ESteps.Unpack; + FileLoadPath = MainBundleInfo.CachedDataFilePath; #else _steps = ESteps.CheckFile; FileLoadPath = MainBundleInfo.BuildinFilePath; #endif - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) - { - _steps = ESteps.CheckFile; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromDelivery) - { - _steps = ESteps.CheckFile; - FileLoadPath = MainBundleInfo.DeliveryFilePath; - } - else - { - throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); - } - } + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) + { + _steps = ESteps.CheckFile; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromDelivery) + { + _steps = ESteps.CheckFile; + FileLoadPath = MainBundleInfo.DeliveryFilePath; + } + else + { + throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); + } + } - // 1. 下载远端文件 - if (_steps == ESteps.Download) - { - _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); - _downloader.SendRequest(); - _steps = ESteps.CheckDownload; - } + // 1. 下载远端文件 + if (_steps == ESteps.Download) + { + _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); + _downloader.SendRequest(); + _steps = ESteps.CheckDownload; + } - // 2. 检测下载结果 - if (_steps == ESteps.CheckDownload) - { - DownloadProgress = _downloader.DownloadProgress; - DownloadedBytes = _downloader.DownloadedBytes; - if (_downloader.IsDone() == false) - return; + // 2. 检测下载结果 + if (_steps == ESteps.CheckDownload) + { + DownloadProgress = _downloader.DownloadProgress; + DownloadedBytes = _downloader.DownloadedBytes; + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = _downloader.GetLastError(); - } - else - { - _steps = ESteps.CheckFile; - } - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _downloader.GetLastError(); + } + else + { + _steps = ESteps.CheckFile; + } + } - // 3. 解压内置文件 - if (_steps == ESteps.Unpack) - { - int failedTryAgain = 1; - _unpacker = MainBundleInfo.CreateUnpacker(failedTryAgain); - _unpacker.SendRequest(); - _steps = ESteps.CheckUnpack; - } + // 3. 解压内置文件 + if (_steps == ESteps.Unpack) + { + int failedTryAgain = 1; + _unpacker = MainBundleInfo.CreateUnpacker(failedTryAgain); + _unpacker.SendRequest(); + _steps = ESteps.CheckUnpack; + } - // 4. 检测解压结果 - if (_steps == ESteps.CheckUnpack) - { - DownloadProgress = _unpacker.DownloadProgress; - DownloadedBytes = _unpacker.DownloadedBytes; - if (_unpacker.IsDone() == false) - return; + // 4. 检测解压结果 + if (_steps == ESteps.CheckUnpack) + { + DownloadProgress = _unpacker.DownloadProgress; + DownloadedBytes = _unpacker.DownloadedBytes; + if (_unpacker.IsDone() == false) + return; - if (_unpacker.HasError()) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = _unpacker.GetLastError(); - } - else - { - _steps = ESteps.CheckFile; - } - } + if (_unpacker.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _unpacker.GetLastError(); + } + else + { + _steps = ESteps.CheckFile; + } + } - // 5. 检测结果 - if (_steps == ESteps.CheckFile) - { - // 设置下载进度 - DownloadProgress = 1f; - DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; + // 5. 检测结果 + if (_steps == ESteps.CheckFile) + { + // 设置下载进度 + DownloadProgress = 1f; + DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; - if (File.Exists(FileLoadPath)) - { - _steps = ESteps.Done; - Status = EStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = $"Raw file not found : {FileLoadPath}"; - } - } - } + if (File.Exists(FileLoadPath)) + { + _steps = ESteps.Done; + Status = EStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = $"Raw file not found : {FileLoadPath}"; + } + } + } - /// - /// 主线程等待异步操作完毕 - /// - public override void WaitForAsyncComplete() - { - int frame = 1000; - while (true) - { - // 文件解压 - if (_unpacker != null) - { - if (_unpacker.IsDone() == false) - { - _unpacker.WaitForAsyncComplete = true; - _unpacker.Update(); - continue; - } - } + /// + /// 主线程等待异步操作完毕 + /// + public override void WaitForAsyncComplete() + { + int frame = 1000; + while (true) + { + // 文件解压 + if (_unpacker != null) + { + if (_unpacker.IsDone() == false) + { + _unpacker.WaitForAsyncComplete = true; + _unpacker.Update(); + continue; + } + } - // 保险机制 - // 注意:如果需要从远端下载资源,可能会触发保险机制! - frame--; - if (frame == 0) - { - if (IsDone() == false) - { - Status = EStatus.Failed; - LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"; - YooLogger.Error(LastError); - } - break; - } + // 保险机制 + // 注意:如果需要从远端下载资源,可能会触发保险机制! + frame--; + if (frame == 0) + { + if (IsDone() == false) + { + Status = EStatus.Failed; + LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"; + YooLogger.Error(LastError); + } + break; + } - // 驱动流程 - Update(); + // 驱动流程 + Update(); - // 完成后退出 - if (IsDone()) - break; - } - } - } + // 完成后退出 + if (IsDone()) + break; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleWebLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleWebLoader.cs index b5dbbc3..18c37a3 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleWebLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/RawBundleWebLoader.cs @@ -2,144 +2,144 @@ namespace YooAsset { - /// - /// WebGL平台加载器 - /// - internal class RawBundleWebLoader : BundleLoaderBase - { - private enum ESteps - { - None, - Download, - CheckDownload, - Website, - CheckWebsite, - CheckFile, - Done, - } + /// + /// WebGL平台加载器 + /// + internal class RawBundleWebLoader : BundleLoaderBase + { + private enum ESteps + { + None, + Download, + CheckDownload, + Website, + CheckWebsite, + CheckFile, + Done, + } - private ESteps _steps = ESteps.None; - private DownloaderBase _website; - private DownloaderBase _downloader; + private ESteps _steps = ESteps.None; + private DownloaderBase _website; + private DownloaderBase _downloader; - public RawBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) - { - } + public RawBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) + { + } - /// - /// 轮询更新 - /// - public override void Update() - { - if (_steps == ESteps.Done) - return; + /// + /// 轮询更新 + /// + public override void Update() + { + if (_steps == ESteps.Done) + return; - if (_steps == ESteps.None) - { - if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - { - _steps = ESteps.Download; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) - { - _steps = ESteps.Website; - FileLoadPath = MainBundleInfo.CachedDataFilePath; - } - else - { - throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); - } - } + if (_steps == ESteps.None) + { + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + { + _steps = ESteps.Download; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) + { + _steps = ESteps.Website; + FileLoadPath = MainBundleInfo.CachedDataFilePath; + } + else + { + throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); + } + } - // 1. 下载远端文件 - if (_steps == ESteps.Download) - { - _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); - _downloader.SendRequest(); - _steps = ESteps.CheckDownload; - } + // 1. 下载远端文件 + if (_steps == ESteps.Download) + { + _downloader = MainBundleInfo.CreateDownloader(int.MaxValue); + _downloader.SendRequest(); + _steps = ESteps.CheckDownload; + } - // 2. 检测下载结果 - if (_steps == ESteps.CheckDownload) - { - DownloadProgress = _downloader.DownloadProgress; - DownloadedBytes = _downloader.DownloadedBytes; - if (_downloader.IsDone() == false) - return; + // 2. 检测下载结果 + if (_steps == ESteps.CheckDownload) + { + DownloadProgress = _downloader.DownloadProgress; + DownloadedBytes = _downloader.DownloadedBytes; + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = _downloader.GetLastError(); - } - else - { - _steps = ESteps.CheckFile; - } - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _downloader.GetLastError(); + } + else + { + _steps = ESteps.CheckFile; + } + } - // 3. 从站点下载 - if (_steps == ESteps.Website) - { - _website = MainBundleInfo.CreateUnpacker(int.MaxValue); - _website.SendRequest(); - _steps = ESteps.CheckWebsite; - } + // 3. 从站点下载 + if (_steps == ESteps.Website) + { + _website = MainBundleInfo.CreateUnpacker(int.MaxValue); + _website.SendRequest(); + _steps = ESteps.CheckWebsite; + } - // 4. 检测站点下载 - if (_steps == ESteps.CheckWebsite) - { - DownloadProgress = _website.DownloadProgress; - DownloadedBytes = _website.DownloadedBytes; - if (_website.IsDone() == false) - return; + // 4. 检测站点下载 + if (_steps == ESteps.CheckWebsite) + { + DownloadProgress = _website.DownloadProgress; + DownloadedBytes = _website.DownloadedBytes; + if (_website.IsDone() == false) + return; - if (_website.HasError()) - { - _steps = ESteps.Done; - Status = EStatus.Failed; - LastError = _website.GetLastError(); - } - else - { - _steps = ESteps.CheckFile; - } - } + if (_website.HasError()) + { + _steps = ESteps.Done; + Status = EStatus.Failed; + LastError = _website.GetLastError(); + } + else + { + _steps = ESteps.CheckFile; + } + } - // 5. 检测结果 - if (_steps == ESteps.CheckFile) - { - // 设置下载进度 - DownloadProgress = 1f; - DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; + // 5. 检测结果 + if (_steps == ESteps.CheckFile) + { + // 设置下载进度 + DownloadProgress = 1f; + DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; - _steps = ESteps.Done; - if (File.Exists(FileLoadPath)) - { - Status = EStatus.Succeed; - } - else - { - Status = EStatus.Failed; - LastError = $"Raw file not found : {FileLoadPath}"; - } - } - } + _steps = ESteps.Done; + if (File.Exists(FileLoadPath)) + { + Status = EStatus.Succeed; + } + else + { + Status = EStatus.Failed; + LastError = $"Raw file not found : {FileLoadPath}"; + } + } + } - /// - /// 主线程等待异步操作完毕 - /// - public override void WaitForAsyncComplete() - { - if (IsDone() == false) - { - Status = EStatus.Failed; - LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !"; - YooLogger.Error(LastError); - } - } - } + /// + /// 主线程等待异步操作完毕 + /// + public override void WaitForAsyncComplete() + { + if (IsDone() == false) + { + Status = EStatus.Failed; + LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !"; + YooLogger.Error(LastError); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/VirtualBundleFileLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/VirtualBundleFileLoader.cs index b9cabbb..c90a6dd 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/VirtualBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/VirtualBundleFileLoader.cs @@ -1,82 +1,82 @@  namespace YooAsset { - internal class VirtualBundleFileLoader : BundleLoaderBase - { - private enum ESteps - { - None, - CheckFile, - Done, - } + internal class VirtualBundleFileLoader : BundleLoaderBase + { + private enum ESteps + { + None, + CheckFile, + Done, + } - private ESteps _steps = ESteps.None; + private ESteps _steps = ESteps.None; - public VirtualBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) - { - } + public VirtualBundleFileLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo) + { + } - /// - /// 轮询更新 - /// - public override void Update() - { - if (_steps == ESteps.Done) - return; + /// + /// 轮询更新 + /// + public override void Update() + { + if (_steps == ESteps.Done) + return; - if (_steps == ESteps.None) - { - if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromEditor) - { - _steps = ESteps.CheckFile; - } - else - { - throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); - } - } + if (_steps == ESteps.None) + { + if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromEditor) + { + _steps = ESteps.CheckFile; + } + else + { + throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); + } + } - // 1. 检测结果 - if (_steps == ESteps.CheckFile) - { - // 设置下载进度 - DownloadProgress = 1f; - DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; + // 1. 检测结果 + if (_steps == ESteps.CheckFile) + { + // 设置下载进度 + DownloadProgress = 1f; + DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize; - _steps = ESteps.Done; - Status = EStatus.Succeed; - } - } + _steps = ESteps.Done; + Status = EStatus.Succeed; + } + } - /// - /// 主线程等待异步操作完毕 - /// - public override void WaitForAsyncComplete() - { - int frame = 1000; - while (true) - { - // 保险机制 - // 注意:如果需要从远端下载资源,可能会触发保险机制! - frame--; - if (frame == 0) - { - if (IsDone() == false) - { - Status = EStatus.Failed; - LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"; - YooLogger.Error(LastError); - } - break; - } + /// + /// 主线程等待异步操作完毕 + /// + public override void WaitForAsyncComplete() + { + int frame = 1000; + while (true) + { + // 保险机制 + // 注意:如果需要从远端下载资源,可能会触发保险机制! + frame--; + if (frame == 0) + { + if (IsDone() == false) + { + Status = EStatus.Failed; + LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"; + YooLogger.Error(LastError); + } + break; + } - // 驱动流程 - Update(); + // 驱动流程 + Update(); - // 完成后退出 - if (IsDone()) - break; - } - } - } + // 完成后退出 + if (IsDone()) + break; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs index 4c6f909..1b4b762 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs @@ -2,131 +2,131 @@ namespace YooAsset { - public sealed class InstantiateOperation : AsyncOperationBase - { - private enum ESteps - { - None, - Clone, - Done, - } + public sealed class InstantiateOperation : AsyncOperationBase + { + private enum ESteps + { + None, + Clone, + Done, + } - private readonly AssetHandle _handle; - private readonly bool _setPositionAndRotation; - private readonly Vector3 _position; - private readonly Quaternion _rotation; - private readonly Transform _parent; - private readonly bool _worldPositionStays; - private ESteps _steps = ESteps.None; + private readonly AssetHandle _handle; + private readonly bool _setPositionAndRotation; + private readonly Vector3 _position; + private readonly Quaternion _rotation; + private readonly Transform _parent; + private readonly bool _worldPositionStays; + private ESteps _steps = ESteps.None; - /// - /// 实例化的游戏对象 - /// - public GameObject Result = null; + /// + /// 实例化的游戏对象 + /// + public GameObject Result = null; - internal InstantiateOperation(AssetHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) - { - _handle = handle; - _setPositionAndRotation = setPositionAndRotation; - _position = position; - _rotation = rotation; - _parent = parent; - _worldPositionStays = worldPositionStays; - } - internal override void InternalOnStart() - { - _steps = ESteps.Clone; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal InstantiateOperation(AssetHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) + { + _handle = handle; + _setPositionAndRotation = setPositionAndRotation; + _position = position; + _rotation = rotation; + _parent = parent; + _worldPositionStays = worldPositionStays; + } + internal override void InternalOnStart() + { + _steps = ESteps.Clone; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.Clone) - { - if (_handle.IsValidWithWarning == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"{nameof(AssetHandle)} is invalid."; - return; - } + if (_steps == ESteps.Clone) + { + if (_handle.IsValidWithWarning == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"{nameof(AssetHandle)} is invalid."; + return; + } - if (_handle.IsDone == false) - return; + if (_handle.IsDone == false) + return; - if (_handle.AssetObject == null) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"{nameof(AssetHandle.AssetObject)} is null."; - return; - } + if (_handle.AssetObject == null) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"{nameof(AssetHandle.AssetObject)} is null."; + return; + } - // 实例化游戏对象 - Result = InstantiateInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays); + // 实例化游戏对象 + Result = InstantiateInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays); - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } - /// - /// 取消实例化对象操作 - /// - public void Cancel() - { - if (IsDone == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"User cancelled !"; - } - } + /// + /// 取消实例化对象操作 + /// + public void Cancel() + { + if (IsDone == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"User cancelled !"; + } + } - /// - /// 等待异步实例化结束 - /// - public void WaitForAsyncComplete() - { - if (_steps == ESteps.Done) - return; - _handle.WaitForAsyncComplete(); - InternalOnUpdate(); - } + /// + /// 等待异步实例化结束 + /// + public void WaitForAsyncComplete() + { + if (_steps == ESteps.Done) + return; + _handle.WaitForAsyncComplete(); + InternalOnUpdate(); + } - internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) - { - if (assetObject == null) - return null; + internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays) + { + if (assetObject == null) + return null; - if (setPositionAndRotation) - { - if (parent != null) - { - GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent); - return clone; - } - else - { - GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation); - return clone; - } - } - else - { - if (parent != null) - { - GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays); - return clone; - } - else - { - GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject); - return clone; - } - } - } - } + if (setPositionAndRotation) + { + if (parent != null) + { + GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent); + return clone; + } + else + { + GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation); + return clone; + } + } + else + { + if (parent != null) + { + GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays); + return clone; + } + else + { + GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject); + return clone; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs index f23b6a7..febb030 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs @@ -3,98 +3,98 @@ using UnityEngine.SceneManagement; namespace YooAsset { - /// - /// 场景卸载异步操作类 - /// - public sealed class UnloadSceneOperation : AsyncOperationBase - { - private enum ESteps - { - None, - CheckError, - PrepareDone, - UnLoadScene, - Checking, - Done, - } + /// + /// 场景卸载异步操作类 + /// + public sealed class UnloadSceneOperation : AsyncOperationBase + { + private enum ESteps + { + None, + CheckError, + PrepareDone, + UnLoadScene, + Checking, + Done, + } - private ESteps _steps = ESteps.None; - private readonly string _error; - private readonly ProviderBase _provider; - private AsyncOperation _asyncOp; + private ESteps _steps = ESteps.None; + private readonly string _error; + private readonly ProviderBase _provider; + private AsyncOperation _asyncOp; - internal UnloadSceneOperation(string error) - { - _error = error; - } - internal UnloadSceneOperation(ProviderBase provider) - { - _error = null; - _provider = provider; - } - internal override void InternalOnStart() - { - _steps = ESteps.CheckError; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal UnloadSceneOperation(string error) + { + _error = error; + } + internal UnloadSceneOperation(ProviderBase provider) + { + _error = null; + _provider = provider; + } + internal override void InternalOnStart() + { + _steps = ESteps.CheckError; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.CheckError) - { - if (string.IsNullOrEmpty(_error) == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _error; - return; - } + if (_steps == ESteps.CheckError) + { + if (string.IsNullOrEmpty(_error) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _error; + return; + } - _steps = ESteps.PrepareDone; - } + _steps = ESteps.PrepareDone; + } - if(_steps == ESteps.PrepareDone) - { - if (_provider.IsDone == false) - return; + if (_steps == ESteps.PrepareDone) + { + if (_provider.IsDone == false) + return; - if (_provider.SceneObject.IsValid() == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Scene is invalid !"; - return; - } + if (_provider.SceneObject.IsValid() == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Scene is invalid !"; + return; + } - if (_provider.SceneObject.isLoaded == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Scene is not loaded !"; - return; - } + if (_provider.SceneObject.isLoaded == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Scene is not loaded !"; + return; + } - _steps = ESteps.UnLoadScene; - } + _steps = ESteps.UnLoadScene; + } - if (_steps == ESteps.UnLoadScene) - { - _asyncOp = SceneManager.UnloadSceneAsync(_provider.SceneObject); - _provider.ResourceMgr.UnloadSubScene(_provider.SceneName); - _provider.ResourceMgr.TryUnloadUnusedAsset(_provider.MainAssetInfo); - _steps = ESteps.Checking; - } + if (_steps == ESteps.UnLoadScene) + { + _asyncOp = SceneManager.UnloadSceneAsync(_provider.SceneObject); + _provider.ResourceMgr.UnloadSubScene(_provider.SceneName); + _provider.ResourceMgr.TryUnloadUnusedAsset(_provider.MainAssetInfo); + _steps = ESteps.Checking; + } - if (_steps == ESteps.Checking) - { - Progress = _asyncOp.progress; - if (_asyncOp.isDone == false) - return; + if (_steps == ESteps.Checking) + { + Progress = _asyncOp.progress; + if (_asyncOp.isDone == false) + return; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs index 82bd6a1..7182738 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs @@ -4,119 +4,119 @@ using UnityEngine; namespace YooAsset { - internal sealed class BundledAllAssetsProvider : ProviderBase - { - private AssetBundleRequest _cacheRequest; + internal sealed class BundledAllAssetsProvider : ProviderBase + { + private AssetBundleRequest _cacheRequest; - public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { - if (IsDone) - return; + public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { + if (IsDone) + return; - if (_steps == ESteps.None) - { - _steps = ESteps.CheckBundle; - } + if (_steps == ESteps.None) + { + _steps = ESteps.CheckBundle; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - DependBundles.WaitForAsyncComplete(); - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + DependBundles.WaitForAsyncComplete(); + OwnerBundle.WaitForAsyncComplete(); + } - if (DependBundles.IsDone() == false) - return; - if (OwnerBundle.IsDone() == false) - return; + if (DependBundles.IsDone() == false) + return; + if (OwnerBundle.IsDone() == false) + return; - if (DependBundles.IsSucceed() == false) - { - string error = DependBundles.GetLastError(); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (DependBundles.IsSucceed() == false) + { + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.CacheBundle == null) - { - ProcessCacheBundleException(); - return; - } + if (OwnerBundle.CacheBundle == null) + { + ProcessCacheBundleException(); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - if (IsWaitForAsyncComplete || IsForceDestroyComplete) - { - if (MainAssetInfo.AssetType == null) - AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(); - else - AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(MainAssetInfo.AssetType); - } - else - { - if (MainAssetInfo.AssetType == null) - _cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(); - else - _cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(MainAssetInfo.AssetType); - } - _steps = ESteps.Checking; - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + if (IsWaitForAsyncComplete || IsForceDestroyComplete) + { + if (MainAssetInfo.AssetType == null) + AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(); + else + AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(MainAssetInfo.AssetType); + } + else + { + if (MainAssetInfo.AssetType == null) + _cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(); + else + _cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(MainAssetInfo.AssetType); + } + _steps = ESteps.Checking; + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - if (_cacheRequest != null) - { - if (IsWaitForAsyncComplete || IsForceDestroyComplete) - { - // 强制挂起主线程(注意:该操作会很耗时) - YooLogger.Warning("Suspend the main thread to load unity asset."); - AllAssetObjects = _cacheRequest.allAssets; - } - else - { - Progress = _cacheRequest.progress; - if (_cacheRequest.isDone == false) - return; - AllAssetObjects = _cacheRequest.allAssets; - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + if (_cacheRequest != null) + { + if (IsWaitForAsyncComplete || IsForceDestroyComplete) + { + // 强制挂起主线程(注意:该操作会很耗时) + YooLogger.Warning("Suspend the main thread to load unity asset."); + AllAssetObjects = _cacheRequest.allAssets; + } + else + { + Progress = _cacheRequest.progress; + if (_cacheRequest.isDone == false) + return; + AllAssetObjects = _cacheRequest.allAssets; + } + } - if (AllAssetObjects == null) - { - string error; - if (MainAssetInfo.AssetType == null) - error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - else - error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - else - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } - } - } + if (AllAssetObjects == null) + { + string error; + if (MainAssetInfo.AssetType == null) + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + else + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs index 72dc1ab..a77a1f3 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs @@ -4,119 +4,119 @@ using UnityEngine; namespace YooAsset { - internal sealed class BundledAssetProvider : ProviderBase - { - private AssetBundleRequest _cacheRequest; + internal sealed class BundledAssetProvider : ProviderBase + { + private AssetBundleRequest _cacheRequest; - public BundledAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { - if (IsDone) - return; + public BundledAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { + if (IsDone) + return; - if (_steps == ESteps.None) - { - _steps = ESteps.CheckBundle; - } + if (_steps == ESteps.None) + { + _steps = ESteps.CheckBundle; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - DependBundles.WaitForAsyncComplete(); - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + DependBundles.WaitForAsyncComplete(); + OwnerBundle.WaitForAsyncComplete(); + } - if (DependBundles.IsDone() == false) - return; - if (OwnerBundle.IsDone() == false) - return; + if (DependBundles.IsDone() == false) + return; + if (OwnerBundle.IsDone() == false) + return; - if (DependBundles.IsSucceed() == false) - { - string error = DependBundles.GetLastError(); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (DependBundles.IsSucceed() == false) + { + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.CacheBundle == null) - { - ProcessCacheBundleException(); - return; - } + if (OwnerBundle.CacheBundle == null) + { + ProcessCacheBundleException(); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - if (IsWaitForAsyncComplete || IsForceDestroyComplete) - { - if (MainAssetInfo.AssetType == null) - AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath); - else - AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); - } - else - { - if (MainAssetInfo.AssetType == null) - _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath); - else - _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); - } - _steps = ESteps.Checking; - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + if (IsWaitForAsyncComplete || IsForceDestroyComplete) + { + if (MainAssetInfo.AssetType == null) + AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath); + else + AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); + } + else + { + if (MainAssetInfo.AssetType == null) + _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath); + else + _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); + } + _steps = ESteps.Checking; + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - if (_cacheRequest != null) - { - if (IsWaitForAsyncComplete || IsForceDestroyComplete) - { - // 强制挂起主线程(注意:该操作会很耗时) - YooLogger.Warning("Suspend the main thread to load unity asset."); - AssetObject = _cacheRequest.asset; - } - else - { - Progress = _cacheRequest.progress; - if (_cacheRequest.isDone == false) - return; - AssetObject = _cacheRequest.asset; - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + if (_cacheRequest != null) + { + if (IsWaitForAsyncComplete || IsForceDestroyComplete) + { + // 强制挂起主线程(注意:该操作会很耗时) + YooLogger.Warning("Suspend the main thread to load unity asset."); + AssetObject = _cacheRequest.asset; + } + else + { + Progress = _cacheRequest.progress; + if (_cacheRequest.isDone == false) + return; + AssetObject = _cacheRequest.asset; + } + } - if (AssetObject == null) - { - string error; - if (MainAssetInfo.AssetType == null) - error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - else - error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - else - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } - } - } + if (AssetObject == null) + { + string error; + if (MainAssetInfo.AssetType == null) + error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + else + error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs index dfd0042..4a104a8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs @@ -1,52 +1,52 @@  namespace YooAsset { - internal class BundledRawFileProvider : ProviderBase - { - public BundledRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { - if (IsDone) - return; + internal class BundledRawFileProvider : ProviderBase + { + public BundledRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { + if (IsDone) + return; - if (_steps == ESteps.None) - { - _steps = ESteps.CheckBundle; - } + if (_steps == ESteps.None) + { + _steps = ESteps.CheckBundle; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + OwnerBundle.WaitForAsyncComplete(); + } - if (OwnerBundle.IsDone() == false) - return; + if (OwnerBundle.IsDone() == false) + return; - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Checking; - } + _steps = ESteps.Checking; + } - // 2. 检测加载结果 - if (_steps == ESteps.Checking) - { - RawFilePath = OwnerBundle.FileLoadPath; - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } - } + // 2. 检测加载结果 + if (_steps == ESteps.Checking) + { + RawFilePath = OwnerBundle.FileLoadPath; + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs index 0e313dc..9757355 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs @@ -6,107 +6,107 @@ using UnityEngine.SceneManagement; namespace YooAsset { - internal sealed class BundledSceneProvider : ProviderBase - { - public readonly LoadSceneMode SceneMode; - private readonly bool _suspendLoad; - private AsyncOperation _asyncOperation; + internal sealed class BundledSceneProvider : ProviderBase + { + public readonly LoadSceneMode SceneMode; + private readonly bool _suspendLoad; + private AsyncOperation _asyncOperation; - public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) - { - SceneMode = sceneMode; - SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); - _suspendLoad = suspendLoad; - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { - if (IsDone) - return; + public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) + { + SceneMode = sceneMode; + SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); + _suspendLoad = suspendLoad; + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { + if (IsDone) + return; - if (_steps == ESteps.None) - { - _steps = ESteps.CheckBundle; - } + if (_steps == ESteps.None) + { + _steps = ESteps.CheckBundle; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (DependBundles.IsDone() == false) - return; - if (OwnerBundle.IsDone() == false) - return; + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (DependBundles.IsDone() == false) + return; + if (OwnerBundle.IsDone() == false) + return; - if (DependBundles.IsSucceed() == false) - { - string error = DependBundles.GetLastError(); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (DependBundles.IsSucceed() == false) + { + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载场景 - if (_steps == ESteps.Loading) - { - // 注意:如果场景不存在则返回NULL - _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode); - if (_asyncOperation != null) - { - _asyncOperation.allowSceneActivation = !_suspendLoad; - _asyncOperation.priority = 100; - SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); - _steps = ESteps.Checking; - } - else - { - string error = $"Failed to load scene : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - } + // 2. 加载场景 + if (_steps == ESteps.Loading) + { + // 注意:如果场景不存在则返回NULL + _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode); + if (_asyncOperation != null) + { + _asyncOperation.allowSceneActivation = !_suspendLoad; + _asyncOperation.priority = 100; + SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); + _steps = ESteps.Checking; + } + else + { + string error = $"Failed to load scene : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - Progress = _asyncOperation.progress; - if (_asyncOperation.isDone) - { - if (SceneObject.IsValid()) - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - else - { - string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - } - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + Progress = _asyncOperation.progress; + if (_asyncOperation.isDone) + { + if (SceneObject.IsValid()) + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + else + { + string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + } + } + } - /// - /// 解除场景加载挂起操作 - /// - public bool UnSuspendLoad() - { - if (_asyncOperation == null) - return false; + /// + /// 解除场景加载挂起操作 + /// + public bool UnSuspendLoad() + { + if (_asyncOperation == null) + return false; - _asyncOperation.allowSceneActivation = true; - return true; - } - } + _asyncOperation.allowSceneActivation = true; + return true; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs index c33612f..acc8641 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs @@ -4,119 +4,119 @@ using UnityEngine; namespace YooAsset { - internal sealed class BundledSubAssetsProvider : ProviderBase - { - private AssetBundleRequest _cacheRequest; + internal sealed class BundledSubAssetsProvider : ProviderBase + { + private AssetBundleRequest _cacheRequest; - public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { - if (IsDone) - return; + public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { + if (IsDone) + return; - if (_steps == ESteps.None) - { - _steps = ESteps.CheckBundle; - } + if (_steps == ESteps.None) + { + _steps = ESteps.CheckBundle; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - DependBundles.WaitForAsyncComplete(); - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + DependBundles.WaitForAsyncComplete(); + OwnerBundle.WaitForAsyncComplete(); + } - if (DependBundles.IsDone() == false) - return; - if (OwnerBundle.IsDone() == false) - return; + if (DependBundles.IsDone() == false) + return; + if (OwnerBundle.IsDone() == false) + return; - if (DependBundles.IsSucceed() == false) - { - string error = DependBundles.GetLastError(); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (DependBundles.IsSucceed() == false) + { + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - if (OwnerBundle.CacheBundle == null) - { - ProcessCacheBundleException(); - return; - } + if (OwnerBundle.CacheBundle == null) + { + ProcessCacheBundleException(); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - if (IsWaitForAsyncComplete || IsForceDestroyComplete) - { - if (MainAssetInfo.AssetType == null) - AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath); - else - AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); - } - else - { - if (MainAssetInfo.AssetType == null) - _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath); - else - _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); - } - _steps = ESteps.Checking; - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + if (IsWaitForAsyncComplete || IsForceDestroyComplete) + { + if (MainAssetInfo.AssetType == null) + AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath); + else + AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); + } + else + { + if (MainAssetInfo.AssetType == null) + _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath); + else + _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); + } + _steps = ESteps.Checking; + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - if (_cacheRequest != null) - { - if (IsWaitForAsyncComplete || IsForceDestroyComplete) - { - // 强制挂起主线程(注意:该操作会很耗时) - YooLogger.Warning("Suspend the main thread to load unity asset."); - AllAssetObjects = _cacheRequest.allAssets; - } - else - { - Progress = _cacheRequest.progress; - if (_cacheRequest.isDone == false) - return; - AllAssetObjects = _cacheRequest.allAssets; - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + if (_cacheRequest != null) + { + if (IsWaitForAsyncComplete || IsForceDestroyComplete) + { + // 强制挂起主线程(注意:该操作会很耗时) + YooLogger.Warning("Suspend the main thread to load unity asset."); + AllAssetObjects = _cacheRequest.allAssets; + } + else + { + Progress = _cacheRequest.progress; + if (_cacheRequest.isDone == false) + return; + AllAssetObjects = _cacheRequest.allAssets; + } + } - if (AllAssetObjects == null) - { - string error; - if (MainAssetInfo.AssetType == null) - error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - else - error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - else - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } - } - } + if (AllAssetObjects == null) + { + string error; + if (MainAssetInfo.AssetType == null) + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + else + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs index fa495ef..8fea77f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs @@ -1,25 +1,25 @@  namespace YooAsset { - internal sealed class CompletedProvider : ProviderBase - { - public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo) - { - } + internal sealed class CompletedProvider : ProviderBase + { + public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo) + { + } - internal override void InternalOnStart() - { - } - internal override void InternalOnUpdate() - { - } + internal override void InternalOnStart() + { + } + internal override void InternalOnUpdate() + { + } - public void SetCompleted(string error) - { - if (_steps == ESteps.None) - { - InvokeCompletion(error, EOperationStatus.Failed); - } - } - } + public void SetCompleted(string error) + { + if (_steps == ESteps.None) + { + InvokeCompletion(error, EOperationStatus.Failed); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs index 174d665..8e19271 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs @@ -4,108 +4,108 @@ using UnityEngine; namespace YooAsset { - internal sealed class DatabaseAllAssetsProvider : ProviderBase - { - public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { + internal sealed class DatabaseAllAssetsProvider : ProviderBase + { + public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { #if UNITY_EDITOR - if (IsDone) - return; + if (IsDone) + return; - if (_steps == ESteps.None) - { - // 检测资源文件是否存在 - string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); - if (string.IsNullOrEmpty(guid)) - { - string error = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (_steps == ESteps.None) + { + // 检测资源文件是否存在 + string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); + if (string.IsNullOrEmpty(guid)) + { + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.CheckBundle; + _steps = ESteps.CheckBundle; - // 注意:模拟异步加载效果提前返回 - if (IsWaitForAsyncComplete == false) - return; - } + // 注意:模拟异步加载效果提前返回 + if (IsWaitForAsyncComplete == false) + return; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + OwnerBundle.WaitForAsyncComplete(); + } - if (OwnerBundle.IsDone() == false) - return; + if (OwnerBundle.IsDone() == false) + return; - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - if (MainAssetInfo.AssetType == null) - { - List result = new List(); - foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor) - { - UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetPath); - if (mainAsset != null) - result.Add(mainAsset); - } - AllAssetObjects = result.ToArray(); - } - else - { - List result = new List(); - foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor) - { - UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, MainAssetInfo.AssetType); - if (mainAsset != null) - result.Add(mainAsset); - } - AllAssetObjects = result.ToArray(); - } - _steps = ESteps.Checking; - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + if (MainAssetInfo.AssetType == null) + { + List result = new List(); + foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor) + { + UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetPath); + if (mainAsset != null) + result.Add(mainAsset); + } + AllAssetObjects = result.ToArray(); + } + else + { + List result = new List(); + foreach (var assetPath in OwnerBundle.MainBundleInfo.IncludeAssetsInEditor) + { + UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, MainAssetInfo.AssetType); + if (mainAsset != null) + result.Add(mainAsset); + } + AllAssetObjects = result.ToArray(); + } + _steps = ESteps.Checking; + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - if (AllAssetObjects == null) - { - string error; - if (MainAssetInfo.AssetType == null) - error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null"; - else - error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - else - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + if (AllAssetObjects == null) + { + string error; + if (MainAssetInfo.AssetType == null) + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null"; + else + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } #endif - } - } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs index 96f790f..d42b266 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs @@ -4,90 +4,90 @@ using UnityEngine; namespace YooAsset { - internal sealed class DatabaseAssetProvider : ProviderBase - { - public DatabaseAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { + internal sealed class DatabaseAssetProvider : ProviderBase + { + public DatabaseAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { #if UNITY_EDITOR - if (IsDone) - return; + if (IsDone) + return; - if (_steps == ESteps.None) - { - // 检测资源文件是否存在 - string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); - if (string.IsNullOrEmpty(guid)) - { - string error = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (_steps == ESteps.None) + { + // 检测资源文件是否存在 + string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); + if (string.IsNullOrEmpty(guid)) + { + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.CheckBundle; + _steps = ESteps.CheckBundle; - // 注意:模拟异步加载效果提前返回 - if (IsWaitForAsyncComplete == false) - return; - } + // 注意:模拟异步加载效果提前返回 + if (IsWaitForAsyncComplete == false) + return; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + OwnerBundle.WaitForAsyncComplete(); + } - if (OwnerBundle.IsDone() == false) - return; + if (OwnerBundle.IsDone() == false) + return; - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - if (MainAssetInfo.AssetType == null) - AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath); - else - AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); - _steps = ESteps.Checking; - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + if (MainAssetInfo.AssetType == null) + AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath); + else + AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); + _steps = ESteps.Checking; + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - if (AssetObject == null) - { - string error; - if (MainAssetInfo.AssetType == null) - error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null"; - else - error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - else - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + if (AssetObject == null) + { + string error; + if (MainAssetInfo.AssetType == null) + error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null"; + else + error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } #endif - } - } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs index a674524..ddfb56f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs @@ -1,68 +1,68 @@  namespace YooAsset { - internal class DatabaseRawFileProvider : ProviderBase - { - public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { + internal class DatabaseRawFileProvider : ProviderBase + { + public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { #if UNITY_EDITOR - if (IsDone) - return; + if (IsDone) + return; - if (_steps == ESteps.None) - { - // 检测资源文件是否存在 - string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); - if (string.IsNullOrEmpty(guid)) - { - string error = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (_steps == ESteps.None) + { + // 检测资源文件是否存在 + string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); + if (string.IsNullOrEmpty(guid)) + { + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.CheckBundle; + _steps = ESteps.CheckBundle; - // 注意:模拟异步加载效果提前返回 - if (IsWaitForAsyncComplete == false) - return; - } + // 注意:模拟异步加载效果提前返回 + if (IsWaitForAsyncComplete == false) + return; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + OwnerBundle.WaitForAsyncComplete(); + } - if (OwnerBundle.IsDone() == false) - return; + if (OwnerBundle.IsDone() == false) + return; - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Checking; - } + _steps = ESteps.Checking; + } - // 2. 检测加载结果 - if (_steps == ESteps.Checking) - { - RawFilePath = MainAssetInfo.AssetPath; - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } + // 2. 检测加载结果 + if (_steps == ESteps.Checking) + { + RawFilePath = MainAssetInfo.AssetPath; + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } #endif - } - } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs index 49e5d66..8173b14 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs @@ -6,106 +6,106 @@ using UnityEngine.SceneManagement; namespace YooAsset { - internal sealed class DatabaseSceneProvider : ProviderBase - { - public readonly LoadSceneMode SceneMode; - private readonly bool _suspendLoad; - private AsyncOperation _asyncOperation; + internal sealed class DatabaseSceneProvider : ProviderBase + { + public readonly LoadSceneMode SceneMode; + private readonly bool _suspendLoad; + private AsyncOperation _asyncOperation; - public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) - { - SceneMode = sceneMode; - SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); - _suspendLoad = suspendLoad; - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { + public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) + { + SceneMode = sceneMode; + SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); + _suspendLoad = suspendLoad; + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { #if UNITY_EDITOR - if (IsDone) - return; + if (IsDone) + return; - if (_steps == ESteps.None) - { - _steps = ESteps.CheckBundle; - } + if (_steps == ESteps.None) + { + _steps = ESteps.CheckBundle; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + OwnerBundle.WaitForAsyncComplete(); + } - if (OwnerBundle.IsDone() == false) - return; + if (OwnerBundle.IsDone() == false) + return; - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - LoadSceneParameters loadSceneParameters = new LoadSceneParameters(); - loadSceneParameters.loadSceneMode = SceneMode; - _asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters); - if (_asyncOperation != null) - { - _asyncOperation.allowSceneActivation = !_suspendLoad; - _asyncOperation.priority = 100; - SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); - _steps = ESteps.Checking; - } - else - { - string error = $"Failed to load scene : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + LoadSceneParameters loadSceneParameters = new LoadSceneParameters(); + loadSceneParameters.loadSceneMode = SceneMode; + _asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters); + if (_asyncOperation != null) + { + _asyncOperation.allowSceneActivation = !_suspendLoad; + _asyncOperation.priority = 100; + SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); + _steps = ESteps.Checking; + } + else + { + string error = $"Failed to load scene : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - Progress = _asyncOperation.progress; - if (_asyncOperation.isDone) - { - if (SceneObject.IsValid()) - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - else - { - string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + Progress = _asyncOperation.progress; + if (_asyncOperation.isDone) + { + if (SceneObject.IsValid()) + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + else + { + string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + } + } #endif - } + } - /// - /// 解除场景加载挂起操作 - /// - public bool UnSuspendLoad() - { - if (_asyncOperation == null) - return false; + /// + /// 解除场景加载挂起操作 + /// + public bool UnSuspendLoad() + { + if (_asyncOperation == null) + return false; - _asyncOperation.allowSceneActivation = true; - return true; - } - } + _asyncOperation.allowSceneActivation = true; + return true; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs index 5f97102..c3e2e82 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs @@ -4,101 +4,101 @@ using UnityEngine; namespace YooAsset { - internal sealed class DatabaseSubAssetsProvider : ProviderBase - { - public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) - { - } - internal override void InternalOnStart() - { - DebugBeginRecording(); - } - internal override void InternalOnUpdate() - { + internal sealed class DatabaseSubAssetsProvider : ProviderBase + { + public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) + { + } + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() + { #if UNITY_EDITOR - if (IsDone) - return; + if (IsDone) + return; - if (_steps == ESteps.None) - { - // 检测资源文件是否存在 - string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); - if (string.IsNullOrEmpty(guid)) - { - string error = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (_steps == ESteps.None) + { + // 检测资源文件是否存在 + string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); + if (string.IsNullOrEmpty(guid)) + { + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.CheckBundle; + _steps = ESteps.CheckBundle; - // 注意:模拟异步加载效果提前返回 - if (IsWaitForAsyncComplete == false) - return; - } + // 注意:模拟异步加载效果提前返回 + if (IsWaitForAsyncComplete == false) + return; + } - // 1. 检测资源包 - if (_steps == ESteps.CheckBundle) - { - if (IsWaitForAsyncComplete) - { - OwnerBundle.WaitForAsyncComplete(); - } + // 1. 检测资源包 + if (_steps == ESteps.CheckBundle) + { + if (IsWaitForAsyncComplete) + { + OwnerBundle.WaitForAsyncComplete(); + } - if (OwnerBundle.IsDone() == false) - return; + if (OwnerBundle.IsDone() == false) + return; - if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) - { - string error = OwnerBundle.LastError; - InvokeCompletion(error, EOperationStatus.Failed); - return; - } + if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) + { + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); + return; + } - _steps = ESteps.Loading; - } + _steps = ESteps.Loading; + } - // 2. 加载资源对象 - if (_steps == ESteps.Loading) - { - if (MainAssetInfo.AssetType == null) - { - AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath); - } - else - { - UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath); - List result = new List(findAssets.Length); - foreach (var findAsset in findAssets) - { - if (MainAssetInfo.AssetType.IsAssignableFrom(findAsset.GetType())) - result.Add(findAsset); - } - AllAssetObjects = result.ToArray(); - } - _steps = ESteps.Checking; - } + // 2. 加载资源对象 + if (_steps == ESteps.Loading) + { + if (MainAssetInfo.AssetType == null) + { + AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath); + } + else + { + UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(MainAssetInfo.AssetPath); + List result = new List(findAssets.Length); + foreach (var findAsset in findAssets) + { + if (MainAssetInfo.AssetType.IsAssignableFrom(findAsset.GetType())) + result.Add(findAsset); + } + AllAssetObjects = result.ToArray(); + } + _steps = ESteps.Checking; + } - // 3. 检测加载结果 - if (_steps == ESteps.Checking) - { - if (AllAssetObjects == null) - { - string error; - if (MainAssetInfo.AssetType == null) - error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null"; - else - error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; - YooLogger.Error(error); - InvokeCompletion(error, EOperationStatus.Failed); - } - else - { - InvokeCompletion(string.Empty, EOperationStatus.Succeed); - } - } + // 3. 检测加载结果 + if (_steps == ESteps.Checking) + { + if (AllAssetObjects == null) + { + string error; + if (MainAssetInfo.AssetType == null) + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null"; + else + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + } #endif - } - } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs index 0421d70..5a54f11 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs @@ -6,339 +6,339 @@ using System; namespace YooAsset { - internal abstract class ProviderBase : AsyncOperationBase - { - protected enum ESteps - { - None = 0, - CheckBundle, - Loading, - Checking, - Done, - } + internal abstract class ProviderBase : AsyncOperationBase + { + protected enum ESteps + { + None = 0, + CheckBundle, + Loading, + Checking, + Done, + } - /// - /// 资源提供者唯一标识符 - /// - public string ProviderGUID { private set; get; } + /// + /// 资源提供者唯一标识符 + /// + public string ProviderGUID { private set; get; } - /// - /// 所属资源系统 - /// - public ResourceManager ResourceMgr { private set; get; } + /// + /// 所属资源系统 + /// + public ResourceManager ResourceMgr { private set; get; } - /// - /// 资源信息 - /// - public AssetInfo MainAssetInfo { private set; get; } + /// + /// 资源信息 + /// + public AssetInfo MainAssetInfo { private set; get; } - /// - /// 获取的资源对象 - /// - public UnityEngine.Object AssetObject { protected set; get; } + /// + /// 获取的资源对象 + /// + public UnityEngine.Object AssetObject { protected set; get; } - /// - /// 获取的资源对象集合 - /// - public UnityEngine.Object[] AllAssetObjects { protected set; get; } + /// + /// 获取的资源对象集合 + /// + public UnityEngine.Object[] AllAssetObjects { protected set; get; } - /// - /// 获取的场景对象 - /// - public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; } + /// + /// 获取的场景对象 + /// + public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; } - /// - /// 加载的场景名称 - /// - public string SceneName { protected set; get; } + /// + /// 加载的场景名称 + /// + public string SceneName { protected set; get; } - /// - /// 原生文件路径 - /// - public string RawFilePath { protected set; get; } + /// + /// 原生文件路径 + /// + public string RawFilePath { protected set; get; } - /// - /// 引用计数 - /// - public int RefCount { private set; get; } = 0; + /// + /// 引用计数 + /// + public int RefCount { private set; get; } = 0; - /// - /// 是否已经销毁 - /// - public bool IsDestroyed { private set; get; } = false; + /// + /// 是否已经销毁 + /// + public bool IsDestroyed { private set; get; } = false; - protected ESteps _steps = ESteps.None; - protected BundleLoaderBase OwnerBundle { private set; get; } - protected DependAssetBundles DependBundles { private set; get; } - protected bool IsWaitForAsyncComplete { private set; get; } = false; - protected bool IsForceDestroyComplete { private set; get; } = false; - private readonly List _handles = new List(); + protected ESteps _steps = ESteps.None; + protected BundleLoaderBase OwnerBundle { private set; get; } + protected DependAssetBundles DependBundles { private set; get; } + protected bool IsWaitForAsyncComplete { private set; get; } = false; + protected bool IsForceDestroyComplete { private set; get; } = false; + private readonly List _handles = new List(); - public ProviderBase(ResourceManager manager, string providerGUID, AssetInfo assetInfo) - { - ResourceMgr = manager; - ProviderGUID = providerGUID; - MainAssetInfo = assetInfo; + public ProviderBase(ResourceManager manager, string providerGUID, AssetInfo assetInfo) + { + ResourceMgr = manager; + ProviderGUID = providerGUID; + MainAssetInfo = assetInfo; - // 创建资源包加载器 - if (manager != null) - { - OwnerBundle = manager.CreateOwnerAssetBundleLoader(assetInfo); - OwnerBundle.Reference(); - OwnerBundle.AddProvider(this); + // 创建资源包加载器 + if (manager != null) + { + OwnerBundle = manager.CreateOwnerAssetBundleLoader(assetInfo); + OwnerBundle.Reference(); + OwnerBundle.AddProvider(this); - var dependList = manager.CreateDependAssetBundleLoaders(assetInfo); - DependBundles = new DependAssetBundles(dependList); - DependBundles.Reference(); - } - } + var dependList = manager.CreateDependAssetBundleLoaders(assetInfo); + DependBundles = new DependAssetBundles(dependList); + DependBundles.Reference(); + } + } - /// - /// 销毁资源提供者 - /// - public void Destroy() - { - IsDestroyed = true; + /// + /// 销毁资源提供者 + /// + public void Destroy() + { + IsDestroyed = true; - // 检测是否为正常销毁 - if (IsDone == false) - { - Error = "User abort !"; - Status = EOperationStatus.Failed; - } + // 检测是否为正常销毁 + if (IsDone == false) + { + Error = "User abort !"; + Status = EOperationStatus.Failed; + } - // 释放资源包加载器 - if (OwnerBundle != null) - { - OwnerBundle.Release(); - OwnerBundle = null; - } - if (DependBundles != null) - { - DependBundles.Release(); - DependBundles = null; - } - } + // 释放资源包加载器 + if (OwnerBundle != null) + { + OwnerBundle.Release(); + OwnerBundle = null; + } + if (DependBundles != null) + { + DependBundles.Release(); + DependBundles = null; + } + } - /// - /// 是否可以销毁 - /// - public bool CanDestroy() - { - // 注意:在进行资源加载过程时不可以销毁 - if (_steps == ESteps.Loading || _steps == ESteps.Checking) - return false; + /// + /// 是否可以销毁 + /// + public bool CanDestroy() + { + // 注意:在进行资源加载过程时不可以销毁 + if (_steps == ESteps.Loading || _steps == ESteps.Checking) + return false; - return RefCount <= 0; - } + return RefCount <= 0; + } - /// - /// 创建资源句柄 - /// - public T CreateHandle() where T : HandleBase - { - // 引用计数增加 - RefCount++; + /// + /// 创建资源句柄 + /// + public T CreateHandle() where T : HandleBase + { + // 引用计数增加 + RefCount++; - HandleBase handle; - if (typeof(T) == typeof(AssetHandle)) - handle = new AssetHandle(this); - else if (typeof(T) == typeof(SceneHandle)) - handle = new SceneHandle(this); - else if (typeof(T) == typeof(SubAssetsHandle)) - handle = new SubAssetsHandle(this); - else if (typeof(T) == typeof(AllAssetsHandle)) - handle = new AllAssetsHandle(this); - else if (typeof(T) == typeof(RawFileHandle)) - handle = new RawFileHandle(this); - else - throw new System.NotImplementedException(); + HandleBase handle; + if (typeof(T) == typeof(AssetHandle)) + handle = new AssetHandle(this); + else if (typeof(T) == typeof(SceneHandle)) + handle = new SceneHandle(this); + else if (typeof(T) == typeof(SubAssetsHandle)) + handle = new SubAssetsHandle(this); + else if (typeof(T) == typeof(AllAssetsHandle)) + handle = new AllAssetsHandle(this); + else if (typeof(T) == typeof(RawFileHandle)) + handle = new RawFileHandle(this); + else + throw new System.NotImplementedException(); - _handles.Add(handle); - return handle as T; - } + _handles.Add(handle); + return handle as T; + } - /// - /// 释放资源句柄 - /// - public void ReleaseHandle(HandleBase handle) - { - if (RefCount <= 0) - throw new System.Exception("Should never get here !"); + /// + /// 释放资源句柄 + /// + public void ReleaseHandle(HandleBase handle) + { + if (RefCount <= 0) + throw new System.Exception("Should never get here !"); - if (_handles.Remove(handle) == false) - throw new System.Exception("Should never get here !"); + if (_handles.Remove(handle) == false) + throw new System.Exception("Should never get here !"); - // 引用计数减少 - RefCount--; - } + // 引用计数减少 + RefCount--; + } - /// - /// 释放所有资源句柄 - /// - public void ReleaseAllHandles() - { - for (int i = _handles.Count - 1; i >= 0; i--) - { - var handle = _handles[i]; - handle.ReleaseInternal(); - } - } + /// + /// 释放所有资源句柄 + /// + public void ReleaseAllHandles() + { + for (int i = _handles.Count - 1; i >= 0; i--) + { + var handle = _handles[i]; + handle.ReleaseInternal(); + } + } - /// - /// 等待异步执行完毕 - /// - public void WaitForAsyncComplete() - { - IsWaitForAsyncComplete = true; + /// + /// 等待异步执行完毕 + /// + public void WaitForAsyncComplete() + { + IsWaitForAsyncComplete = true; - // 注意:主动轮询更新完成同步加载 - InternalOnUpdate(); + // 注意:主动轮询更新完成同步加载 + InternalOnUpdate(); - // 验证结果 - if (IsDone == false) - { - YooLogger.Warning($"{nameof(WaitForAsyncComplete)} failed to loading : {MainAssetInfo.AssetPath}"); - } - } + // 验证结果 + if (IsDone == false) + { + YooLogger.Warning($"{nameof(WaitForAsyncComplete)} failed to loading : {MainAssetInfo.AssetPath}"); + } + } - /// - /// 强制销毁资源提供者 - /// - public void ForceDestroyComplete() - { - IsForceDestroyComplete = true; + /// + /// 强制销毁资源提供者 + /// + public void ForceDestroyComplete() + { + IsForceDestroyComplete = true; - // 注意:主动轮询更新完成同步加载 - // 说明:如果资源包未准备完毕也可以放心销毁。 - InternalOnUpdate(); - } + // 注意:主动轮询更新完成同步加载 + // 说明:如果资源包未准备完毕也可以放心销毁。 + InternalOnUpdate(); + } - /// - /// 处理特殊异常 - /// - protected void ProcessCacheBundleException() - { - if (OwnerBundle.IsDestroyed) - throw new System.Exception("Should never get here !"); + /// + /// 处理特殊异常 + /// + protected void ProcessCacheBundleException() + { + if (OwnerBundle.IsDestroyed) + throw new System.Exception("Should never get here !"); - string error = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !"; - YooLogger.Error(error); - InvokeCompletion(Error, EOperationStatus.Failed); - } + string error = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !"; + YooLogger.Error(error); + InvokeCompletion(Error, EOperationStatus.Failed); + } - /// - /// 结束流程 - /// - protected void InvokeCompletion(string error, EOperationStatus status) - { - DebugEndRecording(); + /// + /// 结束流程 + /// + protected void InvokeCompletion(string error, EOperationStatus status) + { + DebugEndRecording(); - _steps = ESteps.Done; - Error = error; - Status = status; + _steps = ESteps.Done; + Error = error; + Status = status; - // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。 - // 注意:回调方法如果发生异常,会阻断列表里的后续回调方法! - List tempers = new List(_handles); - foreach (var hande in tempers) - { - if (hande.IsValid) - { - hande.InvokeCallback(); - } - } - } + // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。 + // 注意:回调方法如果发生异常,会阻断列表里的后续回调方法! + List tempers = new List(_handles); + foreach (var hande in tempers) + { + if (hande.IsValid) + { + hande.InvokeCallback(); + } + } + } - #region 调试信息相关 - /// - /// 出生的场景 - /// - public string SpawnScene = string.Empty; + #region 调试信息相关 + /// + /// 出生的场景 + /// + public string SpawnScene = string.Empty; - /// - /// 出生的时间 - /// - public string SpawnTime = string.Empty; + /// + /// 出生的时间 + /// + public string SpawnTime = string.Empty; - /// - /// 加载耗时(单位:毫秒) - /// - public long LoadingTime { protected set; get; } + /// + /// 加载耗时(单位:毫秒) + /// + public long LoadingTime { protected set; get; } - // 加载耗时统计 - private Stopwatch _watch = null; + // 加载耗时统计 + private Stopwatch _watch = null; - [Conditional("DEBUG")] - public void InitSpawnDebugInfo() - { - SpawnScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; ; - SpawnTime = SpawnTimeToString(UnityEngine.Time.realtimeSinceStartup); - } - private string SpawnTimeToString(float spawnTime) - { - float h = UnityEngine.Mathf.FloorToInt(spawnTime / 3600f); - float m = UnityEngine.Mathf.FloorToInt(spawnTime / 60f - h * 60f); - float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f); - return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00"); - } + [Conditional("DEBUG")] + public void InitSpawnDebugInfo() + { + SpawnScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; ; + SpawnTime = SpawnTimeToString(UnityEngine.Time.realtimeSinceStartup); + } + private string SpawnTimeToString(float spawnTime) + { + float h = UnityEngine.Mathf.FloorToInt(spawnTime / 3600f); + float m = UnityEngine.Mathf.FloorToInt(spawnTime / 60f - h * 60f); + float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f); + return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00"); + } - [Conditional("DEBUG")] - protected void DebugBeginRecording() - { - if (_watch == null) - { - _watch = Stopwatch.StartNew(); - } - } + [Conditional("DEBUG")] + protected void DebugBeginRecording() + { + if (_watch == null) + { + _watch = Stopwatch.StartNew(); + } + } - [Conditional("DEBUG")] - private void DebugEndRecording() - { - if (_watch != null) - { - LoadingTime = _watch.ElapsedMilliseconds; - _watch = null; - } - } + [Conditional("DEBUG")] + private void DebugEndRecording() + { + if (_watch != null) + { + LoadingTime = _watch.ElapsedMilliseconds; + _watch = null; + } + } - /// - /// 获取下载报告 - /// - internal DownloadStatus GetDownloadStatus() - { - DownloadStatus status = new DownloadStatus(); - status.TotalBytes = (ulong)OwnerBundle.MainBundleInfo.Bundle.FileSize; - status.DownloadedBytes = OwnerBundle.DownloadedBytes; - foreach (var dependBundle in DependBundles.DependList) - { - status.TotalBytes += (ulong)dependBundle.MainBundleInfo.Bundle.FileSize; - status.DownloadedBytes += dependBundle.DownloadedBytes; - } + /// + /// 获取下载报告 + /// + internal DownloadStatus GetDownloadStatus() + { + DownloadStatus status = new DownloadStatus(); + status.TotalBytes = (ulong)OwnerBundle.MainBundleInfo.Bundle.FileSize; + status.DownloadedBytes = OwnerBundle.DownloadedBytes; + foreach (var dependBundle in DependBundles.DependList) + { + status.TotalBytes += (ulong)dependBundle.MainBundleInfo.Bundle.FileSize; + status.DownloadedBytes += dependBundle.DownloadedBytes; + } - if (status.TotalBytes == 0) - throw new System.Exception("Should never get here !"); + if (status.TotalBytes == 0) + throw new System.Exception("Should never get here !"); - status.IsDone = status.DownloadedBytes == status.TotalBytes; - status.Progress = (float)status.DownloadedBytes / status.TotalBytes; - return status; - } + status.IsDone = status.DownloadedBytes == status.TotalBytes; + status.Progress = (float)status.DownloadedBytes / status.TotalBytes; + return status; + } - /// - /// 获取资源包的调试信息列表 - /// - internal void GetBundleDebugInfos(List output) - { - var bundleInfo = new DebugBundleInfo(); - bundleInfo.BundleName = OwnerBundle.MainBundleInfo.Bundle.BundleName; - bundleInfo.RefCount = OwnerBundle.RefCount; - bundleInfo.Status = OwnerBundle.Status.ToString(); - output.Add(bundleInfo); + /// + /// 获取资源包的调试信息列表 + /// + internal void GetBundleDebugInfos(List output) + { + var bundleInfo = new DebugBundleInfo(); + bundleInfo.BundleName = OwnerBundle.MainBundleInfo.Bundle.BundleName; + bundleInfo.RefCount = OwnerBundle.RefCount; + bundleInfo.Status = OwnerBundle.Status.ToString(); + output.Add(bundleInfo); - DependBundles.GetBundleDebugInfos(output); - } - #endregion - } + DependBundles.GetBundleDebugInfos(output); + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs index a897a65..029c0b7 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs @@ -3,101 +3,101 @@ using UnityEngine; namespace YooAsset { - internal class ResourceLoader - { - private IDecryptionServices _decryption; - private IDeliveryLoadServices _delivery; + internal class ResourceLoader + { + private IDecryptionServices _decryption; + private IDeliveryLoadServices _delivery; - public void Init(IDecryptionServices decryption, IDeliveryLoadServices delivery) - { - _decryption = decryption; - _delivery = delivery; - } + public void Init(IDecryptionServices decryption, IDeliveryLoadServices delivery) + { + _decryption = decryption; + _delivery = delivery; + } - /// - /// 同步加载资源包对象 - /// - public AssetBundle LoadAssetBundle(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream) - { - managedStream = null; - if (bundleInfo.Bundle.Encrypted) - { - if (_decryption == null) - { - YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!"); - return null; - } + /// + /// 同步加载资源包对象 + /// + public AssetBundle LoadAssetBundle(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream) + { + managedStream = null; + if (bundleInfo.Bundle.Encrypted) + { + if (_decryption == null) + { + YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!"); + return null; + } - DecryptFileInfo fileInfo = new DecryptFileInfo(); - fileInfo.BundleName = bundleInfo.Bundle.BundleName; - fileInfo.FileLoadPath = fileLoadPath; - fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; - return _decryption.LoadAssetBundle(fileInfo, out managedStream); - } - else - { - return AssetBundle.LoadFromFile(fileLoadPath); - } - } + DecryptFileInfo fileInfo = new DecryptFileInfo(); + fileInfo.BundleName = bundleInfo.Bundle.BundleName; + fileInfo.FileLoadPath = fileLoadPath; + fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; + return _decryption.LoadAssetBundle(fileInfo, out managedStream); + } + else + { + return AssetBundle.LoadFromFile(fileLoadPath); + } + } - /// - /// 异步加载资源包对象 - /// - public AssetBundleCreateRequest LoadAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream) - { - managedStream = null; - if (bundleInfo.Bundle.Encrypted) - { - if (_decryption == null) - { - YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!"); - return null; - } + /// + /// 异步加载资源包对象 + /// + public AssetBundleCreateRequest LoadAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream) + { + managedStream = null; + if (bundleInfo.Bundle.Encrypted) + { + if (_decryption == null) + { + YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!"); + return null; + } - DecryptFileInfo fileInfo = new DecryptFileInfo(); - fileInfo.BundleName = bundleInfo.Bundle.BundleName; - fileInfo.FileLoadPath = fileLoadPath; - fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; - return _decryption.LoadAssetBundleAsync(fileInfo, out managedStream); - } - else - { - return AssetBundle.LoadFromFileAsync(fileLoadPath); - } - } + DecryptFileInfo fileInfo = new DecryptFileInfo(); + fileInfo.BundleName = bundleInfo.Bundle.BundleName; + fileInfo.FileLoadPath = fileLoadPath; + fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; + return _decryption.LoadAssetBundleAsync(fileInfo, out managedStream); + } + else + { + return AssetBundle.LoadFromFileAsync(fileLoadPath); + } + } - /// - /// 同步加载分发的资源包对象 - /// - public AssetBundle LoadDeliveryAssetBundle(BundleInfo bundleInfo, string fileLoadPath) - { - if (_delivery == null) - throw new System.Exception("Should never get here !"); + /// + /// 同步加载分发的资源包对象 + /// + public AssetBundle LoadDeliveryAssetBundle(BundleInfo bundleInfo, string fileLoadPath) + { + if (_delivery == null) + throw new System.Exception("Should never get here !"); - // 注意:对于已经加密的资源包,需要开发者自行解密。 - DeliveryFileInfo fileInfo = new DeliveryFileInfo(); - fileInfo.BundleName = bundleInfo.Bundle.BundleName; - fileInfo.FileLoadPath = fileLoadPath; - fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; - fileInfo.Encrypted = bundleInfo.Bundle.Encrypted; - return _delivery.LoadAssetBundle(fileInfo); - } + // 注意:对于已经加密的资源包,需要开发者自行解密。 + DeliveryFileInfo fileInfo = new DeliveryFileInfo(); + fileInfo.BundleName = bundleInfo.Bundle.BundleName; + fileInfo.FileLoadPath = fileLoadPath; + fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; + fileInfo.Encrypted = bundleInfo.Bundle.Encrypted; + return _delivery.LoadAssetBundle(fileInfo); + } - /// - /// 异步加载分发的资源包对象 - /// - public AssetBundleCreateRequest LoadDeliveryAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath) - { - if (_delivery == null) - throw new System.Exception("Should never get here !"); + /// + /// 异步加载分发的资源包对象 + /// + public AssetBundleCreateRequest LoadDeliveryAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath) + { + if (_delivery == null) + throw new System.Exception("Should never get here !"); - // 注意:对于已经加密的资源包,需要开发者自行解密。 - DeliveryFileInfo fileInfo = new DeliveryFileInfo(); - fileInfo.BundleName = bundleInfo.Bundle.BundleName; - fileInfo.FileLoadPath = fileLoadPath; - fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; - fileInfo.Encrypted = bundleInfo.Bundle.Encrypted; - return _delivery.LoadAssetBundleAsync(fileInfo); - } - } + // 注意:对于已经加密的资源包,需要开发者自行解密。 + DeliveryFileInfo fileInfo = new DeliveryFileInfo(); + fileInfo.BundleName = bundleInfo.Bundle.BundleName; + fileInfo.FileLoadPath = fileLoadPath; + fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; + fileInfo.Encrypted = bundleInfo.Bundle.Encrypted; + return _delivery.LoadAssetBundleAsync(fileInfo); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs index ee135cd..90eb619 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs @@ -7,478 +7,478 @@ using UnityEngine.SceneManagement; namespace YooAsset { - internal class ResourceManager - { - // 全局场景句柄集合 - private readonly static Dictionary _sceneHandles = new Dictionary(100); - private static long _sceneCreateCount = 0; + internal class ResourceManager + { + // 全局场景句柄集合 + private readonly static Dictionary _sceneHandles = new Dictionary(100); + private static long _sceneCreateCount = 0; - private readonly Dictionary _providerDic = new Dictionary(5000); - private readonly Dictionary _loaderDic = new Dictionary(5000); - private readonly List _loaderList = new List(5000); + private readonly Dictionary _providerDic = new Dictionary(5000); + private readonly Dictionary _loaderDic = new Dictionary(5000); + private readonly List _loaderList = new List(5000); - private bool _simulationOnEditor; - private bool _autoDestroyAssetProvider; - private IBundleQuery _bundleQuery; + private bool _simulationOnEditor; + private bool _autoDestroyAssetProvider; + private IBundleQuery _bundleQuery; - /// - /// 所属包裹 - /// - public readonly string PackageName; + /// + /// 所属包裹 + /// + public readonly string PackageName; - public ResourceManager(string packageName) - { - PackageName = packageName; - } + public ResourceManager(string packageName) + { + PackageName = packageName; + } - /// - /// 初始化 - /// - public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, IBundleQuery bundleServices) - { - _simulationOnEditor = simulationOnEditor; - _autoDestroyAssetProvider = autoDestroyAssetProvider; - _bundleQuery = bundleServices; - } + /// + /// 初始化 + /// + public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, IBundleQuery bundleServices) + { + _simulationOnEditor = simulationOnEditor; + _autoDestroyAssetProvider = autoDestroyAssetProvider; + _bundleQuery = bundleServices; + } - /// - /// 更新 - /// - public void Update() - { - foreach (var loader in _loaderList) - { - loader.Update(); + /// + /// 更新 + /// + public void Update() + { + foreach (var loader in _loaderList) + { + loader.Update(); - if (_autoDestroyAssetProvider) - loader.TryDestroyProviders(); - } - } + if (_autoDestroyAssetProvider) + loader.TryDestroyProviders(); + } + } - /// - /// 资源回收(卸载引用计数为零的资源) - /// - public void UnloadUnusedAssets() - { - for (int i = _loaderList.Count - 1; i >= 0; i--) - { - BundleLoaderBase loader = _loaderList[i]; - loader.TryDestroyProviders(); - } + /// + /// 资源回收(卸载引用计数为零的资源) + /// + public void UnloadUnusedAssets() + { + for (int i = _loaderList.Count - 1; i >= 0; i--) + { + BundleLoaderBase loader = _loaderList[i]; + loader.TryDestroyProviders(); + } - for (int i = _loaderList.Count - 1; i >= 0; i--) - { - BundleLoaderBase loader = _loaderList[i]; - if (loader.CanDestroy()) - { - string bundleName = loader.MainBundleInfo.Bundle.BundleName; - loader.Destroy(); - _loaderList.RemoveAt(i); - _loaderDic.Remove(bundleName); - } - } - } + for (int i = _loaderList.Count - 1; i >= 0; i--) + { + BundleLoaderBase loader = _loaderList[i]; + if (loader.CanDestroy()) + { + string bundleName = loader.MainBundleInfo.Bundle.BundleName; + loader.Destroy(); + _loaderList.RemoveAt(i); + _loaderDic.Remove(bundleName); + } + } + } - /// - /// 尝试卸载指定资源的资源包(包括依赖资源) - /// - public void TryUnloadUnusedAsset(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - { - YooLogger.Error($"Failed to unload asset ! {assetInfo.Error}"); - return; - } + /// + /// 尝试卸载指定资源的资源包(包括依赖资源) + /// + public void TryUnloadUnusedAsset(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + { + YooLogger.Error($"Failed to unload asset ! {assetInfo.Error}"); + return; + } - // 卸载主资源包加载器 - string manBundleName = _bundleQuery.GetMainBundleName(assetInfo); - var mainLoader = TryGetAssetBundleLoader(manBundleName); - if (mainLoader != null) - { - mainLoader.TryDestroyProviders(); - if (mainLoader.CanDestroy()) - { - string bundleName = mainLoader.MainBundleInfo.Bundle.BundleName; - mainLoader.Destroy(); - _loaderList.Remove(mainLoader); - _loaderDic.Remove(bundleName); - } - } + // 卸载主资源包加载器 + string manBundleName = _bundleQuery.GetMainBundleName(assetInfo); + var mainLoader = TryGetAssetBundleLoader(manBundleName); + if (mainLoader != null) + { + mainLoader.TryDestroyProviders(); + if (mainLoader.CanDestroy()) + { + string bundleName = mainLoader.MainBundleInfo.Bundle.BundleName; + mainLoader.Destroy(); + _loaderList.Remove(mainLoader); + _loaderDic.Remove(bundleName); + } + } - // 卸载依赖资源包加载器 - string[] dependBundleNames = _bundleQuery.GetDependBundleNames(assetInfo); - foreach (var dependBundleName in dependBundleNames) - { - var dependLoader = TryGetAssetBundleLoader(dependBundleName); - if (dependLoader != null) - { - if (dependLoader.CanDestroy()) - { - string bundleName = dependLoader.MainBundleInfo.Bundle.BundleName; - dependLoader.Destroy(); - _loaderList.Remove(dependLoader); - _loaderDic.Remove(bundleName); - } - } - } - } + // 卸载依赖资源包加载器 + string[] dependBundleNames = _bundleQuery.GetDependBundleNames(assetInfo); + foreach (var dependBundleName in dependBundleNames) + { + var dependLoader = TryGetAssetBundleLoader(dependBundleName); + if (dependLoader != null) + { + if (dependLoader.CanDestroy()) + { + string bundleName = dependLoader.MainBundleInfo.Bundle.BundleName; + dependLoader.Destroy(); + _loaderList.Remove(dependLoader); + _loaderDic.Remove(bundleName); + } + } + } + } - /// - /// 强制回收所有资源 - /// 注意:加载器在销毁后关联的下载器还会继续下载! - /// - public void ForceUnloadAllAssets() - { + /// + /// 强制回收所有资源 + /// 注意:加载器在销毁后关联的下载器还会继续下载! + /// + public void ForceUnloadAllAssets() + { #if UNITY_WEBGL throw new Exception($"WebGL not support invoke {nameof(ForceUnloadAllAssets)}"); #else - // 注意:因为场景无法异步转同步,需要等待所有场景加载完毕! - foreach (var sceneHandlePair in _sceneHandles) - { - var sceneHandle = sceneHandlePair.Value; - if (sceneHandle.PackageName == PackageName) - { - if (sceneHandle.IsDone == false) - throw new Exception($"{nameof(ForceUnloadAllAssets)} cannot be called when loading the scene !"); - } - } + // 注意:因为场景无法异步转同步,需要等待所有场景加载完毕! + foreach (var sceneHandlePair in _sceneHandles) + { + var sceneHandle = sceneHandlePair.Value; + if (sceneHandle.PackageName == PackageName) + { + if (sceneHandle.IsDone == false) + throw new Exception($"{nameof(ForceUnloadAllAssets)} cannot be called when loading the scene !"); + } + } - // 释放所有资源句柄 - foreach (var provider in _providerDic.Values) - { - provider.ReleaseAllHandles(); - } + // 释放所有资源句柄 + foreach (var provider in _providerDic.Values) + { + provider.ReleaseAllHandles(); + } - // 强制销毁资源提供者 - foreach (var provider in _providerDic.Values) - { - provider.ForceDestroyComplete(); - provider.Destroy(); - } + // 强制销毁资源提供者 + foreach (var provider in _providerDic.Values) + { + provider.ForceDestroyComplete(); + provider.Destroy(); + } - // 强制销毁资源加载器 - foreach (var loader in _loaderList) - { - loader.ForceDestroyComplete(); - loader.Destroy(); - } + // 强制销毁资源加载器 + foreach (var loader in _loaderList) + { + loader.ForceDestroyComplete(); + loader.Destroy(); + } - // 清空数据 - _providerDic.Clear(); - _loaderList.Clear(); - _loaderDic.Clear(); - ClearSceneHandle(); + // 清空数据 + _providerDic.Clear(); + _loaderList.Clear(); + _loaderDic.Clear(); + ClearSceneHandle(); - // 注意:调用底层接口释放所有资源 - Resources.UnloadUnusedAssets(); + // 注意:调用底层接口释放所有资源 + Resources.UnloadUnusedAssets(); #endif - } + } - /// - /// 加载场景对象 - /// 注意:返回的场景句柄是唯一的,每个场景句柄对应自己的场景提供者对象。 - /// 注意:业务逻辑层应该避免同时加载一个子场景。 - /// - public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, uint priority) - { - if (assetInfo.IsInvalid) - { - YooLogger.Error($"Failed to load scene ! {assetInfo.Error}"); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(assetInfo.Error); - return completedProvider.CreateHandle(); - } + /// + /// 加载场景对象 + /// 注意:返回的场景句柄是唯一的,每个场景句柄对应自己的场景提供者对象。 + /// 注意:业务逻辑层应该避免同时加载一个子场景。 + /// + public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, uint priority) + { + if (assetInfo.IsInvalid) + { + YooLogger.Error($"Failed to load scene ! {assetInfo.Error}"); + CompletedProvider completedProvider = new CompletedProvider(assetInfo); + completedProvider.SetCompleted(assetInfo.Error); + return completedProvider.CreateHandle(); + } - // 如果加载的是主场景,则卸载所有缓存的场景 - if (sceneMode == LoadSceneMode.Single) - { - UnloadAllScene(); - } + // 如果加载的是主场景,则卸载所有缓存的场景 + if (sceneMode == LoadSceneMode.Single) + { + UnloadAllScene(); + } - // 注意:同一个场景的ProviderGUID每次加载都会变化 - string providerGUID = $"{assetInfo.GUID}-{++_sceneCreateCount}"; - ProviderBase provider; - { - if (_simulationOnEditor) - provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad); - else - provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad); - provider.InitSpawnDebugInfo(); - _providerDic.Add(providerGUID, provider); - OperationSystem.StartOperation(PackageName, provider); - } + // 注意:同一个场景的ProviderGUID每次加载都会变化 + string providerGUID = $"{assetInfo.GUID}-{++_sceneCreateCount}"; + ProviderBase provider; + { + if (_simulationOnEditor) + provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad); + else + provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad); + provider.InitSpawnDebugInfo(); + _providerDic.Add(providerGUID, provider); + OperationSystem.StartOperation(PackageName, provider); + } - provider.Priority = priority; - var handle = provider.CreateHandle(); - handle.PackageName = PackageName; - _sceneHandles.Add(providerGUID, handle); - return handle; - } + provider.Priority = priority; + var handle = provider.CreateHandle(); + handle.PackageName = PackageName; + _sceneHandles.Add(providerGUID, handle); + return handle; + } - /// - /// 加载资源对象 - /// - public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority) - { - if (assetInfo.IsInvalid) - { - YooLogger.Error($"Failed to load asset ! {assetInfo.Error}"); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(assetInfo.Error); - return completedProvider.CreateHandle(); - } + /// + /// 加载资源对象 + /// + public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority) + { + if (assetInfo.IsInvalid) + { + YooLogger.Error($"Failed to load asset ! {assetInfo.Error}"); + CompletedProvider completedProvider = new CompletedProvider(assetInfo); + completedProvider.SetCompleted(assetInfo.Error); + return completedProvider.CreateHandle(); + } - string providerGUID = nameof(LoadAssetAsync) + assetInfo.GUID; - ProviderBase provider = TryGetProvider(providerGUID); - if (provider == null) - { - if (_simulationOnEditor) - provider = new DatabaseAssetProvider(this, providerGUID, assetInfo); - else - provider = new BundledAssetProvider(this, providerGUID, assetInfo); - provider.InitSpawnDebugInfo(); - _providerDic.Add(providerGUID, provider); - OperationSystem.StartOperation(PackageName, provider); - } + string providerGUID = nameof(LoadAssetAsync) + assetInfo.GUID; + ProviderBase provider = TryGetProvider(providerGUID); + if (provider == null) + { + if (_simulationOnEditor) + provider = new DatabaseAssetProvider(this, providerGUID, assetInfo); + else + provider = new BundledAssetProvider(this, providerGUID, assetInfo); + provider.InitSpawnDebugInfo(); + _providerDic.Add(providerGUID, provider); + OperationSystem.StartOperation(PackageName, provider); + } - provider.Priority = priority; - return provider.CreateHandle(); - } + provider.Priority = priority; + return provider.CreateHandle(); + } - /// - /// 加载子资源对象 - /// - public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority) - { - if (assetInfo.IsInvalid) - { - YooLogger.Error($"Failed to load sub assets ! {assetInfo.Error}"); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(assetInfo.Error); - return completedProvider.CreateHandle(); - } + /// + /// 加载子资源对象 + /// + public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority) + { + if (assetInfo.IsInvalid) + { + YooLogger.Error($"Failed to load sub assets ! {assetInfo.Error}"); + CompletedProvider completedProvider = new CompletedProvider(assetInfo); + completedProvider.SetCompleted(assetInfo.Error); + return completedProvider.CreateHandle(); + } - string providerGUID = nameof(LoadSubAssetsAsync) + assetInfo.GUID; - ProviderBase provider = TryGetProvider(providerGUID); - if (provider == null) - { - if (_simulationOnEditor) - provider = new DatabaseSubAssetsProvider(this, providerGUID, assetInfo); - else - provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo); - provider.InitSpawnDebugInfo(); - _providerDic.Add(providerGUID, provider); - OperationSystem.StartOperation(PackageName, provider); - } + string providerGUID = nameof(LoadSubAssetsAsync) + assetInfo.GUID; + ProviderBase provider = TryGetProvider(providerGUID); + if (provider == null) + { + if (_simulationOnEditor) + provider = new DatabaseSubAssetsProvider(this, providerGUID, assetInfo); + else + provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo); + provider.InitSpawnDebugInfo(); + _providerDic.Add(providerGUID, provider); + OperationSystem.StartOperation(PackageName, provider); + } - provider.Priority = priority; - return provider.CreateHandle(); - } + provider.Priority = priority; + return provider.CreateHandle(); + } - /// - /// 加载所有资源对象 - /// - public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority) - { - if (assetInfo.IsInvalid) - { - YooLogger.Error($"Failed to load all assets ! {assetInfo.Error}"); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(assetInfo.Error); - return completedProvider.CreateHandle(); - } + /// + /// 加载所有资源对象 + /// + public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority) + { + if (assetInfo.IsInvalid) + { + YooLogger.Error($"Failed to load all assets ! {assetInfo.Error}"); + CompletedProvider completedProvider = new CompletedProvider(assetInfo); + completedProvider.SetCompleted(assetInfo.Error); + return completedProvider.CreateHandle(); + } - string providerGUID = nameof(LoadAllAssetsAsync) + assetInfo.GUID; - ProviderBase provider = TryGetProvider(providerGUID); - if (provider == null) - { - if (_simulationOnEditor) - provider = new DatabaseAllAssetsProvider(this, providerGUID, assetInfo); - else - provider = new BundledAllAssetsProvider(this, providerGUID, assetInfo); - provider.InitSpawnDebugInfo(); - _providerDic.Add(providerGUID, provider); - OperationSystem.StartOperation(PackageName, provider); - } + string providerGUID = nameof(LoadAllAssetsAsync) + assetInfo.GUID; + ProviderBase provider = TryGetProvider(providerGUID); + if (provider == null) + { + if (_simulationOnEditor) + provider = new DatabaseAllAssetsProvider(this, providerGUID, assetInfo); + else + provider = new BundledAllAssetsProvider(this, providerGUID, assetInfo); + provider.InitSpawnDebugInfo(); + _providerDic.Add(providerGUID, provider); + OperationSystem.StartOperation(PackageName, provider); + } - provider.Priority = priority; - return provider.CreateHandle(); - } + provider.Priority = priority; + return provider.CreateHandle(); + } - /// - /// 加载原生文件 - /// - public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority) - { - if (assetInfo.IsInvalid) - { - YooLogger.Error($"Failed to load raw file ! {assetInfo.Error}"); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(assetInfo.Error); - return completedProvider.CreateHandle(); - } + /// + /// 加载原生文件 + /// + public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority) + { + if (assetInfo.IsInvalid) + { + YooLogger.Error($"Failed to load raw file ! {assetInfo.Error}"); + CompletedProvider completedProvider = new CompletedProvider(assetInfo); + completedProvider.SetCompleted(assetInfo.Error); + return completedProvider.CreateHandle(); + } - string providerGUID = nameof(LoadRawFileAsync) + assetInfo.GUID; - ProviderBase provider = TryGetProvider(providerGUID); - if (provider == null) - { - if (_simulationOnEditor) - provider = new DatabaseRawFileProvider(this, providerGUID, assetInfo); - else - provider = new BundledRawFileProvider(this, providerGUID, assetInfo); - provider.InitSpawnDebugInfo(); - _providerDic.Add(providerGUID, provider); - OperationSystem.StartOperation(PackageName, provider); - } + string providerGUID = nameof(LoadRawFileAsync) + assetInfo.GUID; + ProviderBase provider = TryGetProvider(providerGUID); + if (provider == null) + { + if (_simulationOnEditor) + provider = new DatabaseRawFileProvider(this, providerGUID, assetInfo); + else + provider = new BundledRawFileProvider(this, providerGUID, assetInfo); + provider.InitSpawnDebugInfo(); + _providerDic.Add(providerGUID, provider); + OperationSystem.StartOperation(PackageName, provider); + } - provider.Priority = priority; - return provider.CreateHandle(); - } + provider.Priority = priority; + return provider.CreateHandle(); + } - internal void UnloadSubScene(string sceneName) - { - List removeKeys = new List(); - foreach (var valuePair in _sceneHandles) - { - var sceneHandle = valuePair.Value; - if (sceneHandle.SceneName == sceneName) - { - // 释放子场景句柄 - sceneHandle.ReleaseInternal(); - removeKeys.Add(valuePair.Key); - } - } + internal void UnloadSubScene(string sceneName) + { + List removeKeys = new List(); + foreach (var valuePair in _sceneHandles) + { + var sceneHandle = valuePair.Value; + if (sceneHandle.SceneName == sceneName) + { + // 释放子场景句柄 + sceneHandle.ReleaseInternal(); + removeKeys.Add(valuePair.Key); + } + } - foreach (string key in removeKeys) - { - _sceneHandles.Remove(key); - } - } - private void UnloadAllScene() - { - // 释放所有场景句柄 - foreach (var valuePair in _sceneHandles) - { - valuePair.Value.ReleaseInternal(); - } - _sceneHandles.Clear(); - } - private void ClearSceneHandle() - { - // 释放资源包下的所有场景 - if (_bundleQuery.ManifestValid()) - { - string packageName = PackageName; - List removeList = new List(); - foreach (var valuePair in _sceneHandles) - { - if (valuePair.Value.PackageName == packageName) - { - removeList.Add(valuePair.Key); - } - } - foreach (var key in removeList) - { - _sceneHandles.Remove(key); - } - } - } + foreach (string key in removeKeys) + { + _sceneHandles.Remove(key); + } + } + private void UnloadAllScene() + { + // 释放所有场景句柄 + foreach (var valuePair in _sceneHandles) + { + valuePair.Value.ReleaseInternal(); + } + _sceneHandles.Clear(); + } + private void ClearSceneHandle() + { + // 释放资源包下的所有场景 + if (_bundleQuery.ManifestValid()) + { + string packageName = PackageName; + List removeList = new List(); + foreach (var valuePair in _sceneHandles) + { + if (valuePair.Value.PackageName == packageName) + { + removeList.Add(valuePair.Key); + } + } + foreach (var key in removeList) + { + _sceneHandles.Remove(key); + } + } + } - internal BundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) - { - BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo); - return CreateAssetBundleLoaderInternal(bundleInfo); - } - internal List CreateDependAssetBundleLoaders(AssetInfo assetInfo) - { - BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo); - List result = new List(depends.Length); - foreach (var bundleInfo in depends) - { - BundleLoaderBase dependLoader = CreateAssetBundleLoaderInternal(bundleInfo); - result.Add(dependLoader); - } - return result; - } - internal void RemoveBundleProviders(List removeList) - { - foreach (var provider in removeList) - { - _providerDic.Remove(provider.ProviderGUID); - } - } - internal bool HasAnyLoader() - { - return _loaderList.Count > 0; - } + internal BundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) + { + BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo); + return CreateAssetBundleLoaderInternal(bundleInfo); + } + internal List CreateDependAssetBundleLoaders(AssetInfo assetInfo) + { + BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo); + List result = new List(depends.Length); + foreach (var bundleInfo in depends) + { + BundleLoaderBase dependLoader = CreateAssetBundleLoaderInternal(bundleInfo); + result.Add(dependLoader); + } + return result; + } + internal void RemoveBundleProviders(List removeList) + { + foreach (var provider in removeList) + { + _providerDic.Remove(provider.ProviderGUID); + } + } + internal bool HasAnyLoader() + { + return _loaderList.Count > 0; + } - private BundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo) - { - // 如果加载器已经存在 - string bundleName = bundleInfo.Bundle.BundleName; - BundleLoaderBase loader = TryGetAssetBundleLoader(bundleName); - if (loader != null) - return loader; + private BundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo) + { + // 如果加载器已经存在 + string bundleName = bundleInfo.Bundle.BundleName; + BundleLoaderBase loader = TryGetAssetBundleLoader(bundleName); + if (loader != null) + return loader; - // 新增下载需求 - if (_simulationOnEditor) - { - loader = new VirtualBundleFileLoader(this, bundleInfo); - } - else - { + // 新增下载需求 + if (_simulationOnEditor) + { + loader = new VirtualBundleFileLoader(this, bundleInfo); + } + else + { #if UNITY_WEBGL if (bundleInfo.Bundle.Buildpipeline== EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) loader = new RawBundleWebLoader(this, bundleInfo); else loader = new AssetBundleWebLoader(this, bundleInfo); #else - if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) - loader = new RawBundleFileLoader(this, bundleInfo); - else - loader = new AssetBundleFileLoader(this, bundleInfo); + if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) + loader = new RawBundleFileLoader(this, bundleInfo); + else + loader = new AssetBundleFileLoader(this, bundleInfo); #endif - } + } - _loaderList.Add(loader); - _loaderDic.Add(bundleName, loader); - return loader; - } - private BundleLoaderBase TryGetAssetBundleLoader(string bundleName) - { - if (_loaderDic.TryGetValue(bundleName, out BundleLoaderBase value)) - return value; - else - return null; - } - private ProviderBase TryGetProvider(string providerGUID) - { - if (_providerDic.TryGetValue(providerGUID, out ProviderBase value)) - return value; - else - return null; - } + _loaderList.Add(loader); + _loaderDic.Add(bundleName, loader); + return loader; + } + private BundleLoaderBase TryGetAssetBundleLoader(string bundleName) + { + if (_loaderDic.TryGetValue(bundleName, out BundleLoaderBase value)) + return value; + else + return null; + } + private ProviderBase TryGetProvider(string providerGUID) + { + if (_providerDic.TryGetValue(providerGUID, out ProviderBase value)) + return value; + else + return null; + } - #region 调试信息 - internal List GetDebugReportInfos() - { - List result = new List(_providerDic.Count); - foreach (var provider in _providerDic.Values) - { - DebugProviderInfo providerInfo = new DebugProviderInfo(); - providerInfo.AssetPath = provider.MainAssetInfo.AssetPath; - providerInfo.SpawnScene = provider.SpawnScene; - providerInfo.SpawnTime = provider.SpawnTime; - providerInfo.LoadingTime = provider.LoadingTime; - providerInfo.RefCount = provider.RefCount; - providerInfo.Status = provider.Status.ToString(); - providerInfo.DependBundleInfos = new List(); - provider.GetBundleDebugInfos(providerInfo.DependBundleInfos); - result.Add(providerInfo); - } - return result; - } - #endregion - } + #region 调试信息 + internal List GetDebugReportInfos() + { + List result = new List(_providerDic.Count); + foreach (var provider in _providerDic.Values) + { + DebugProviderInfo providerInfo = new DebugProviderInfo(); + providerInfo.AssetPath = provider.MainAssetInfo.AssetPath; + providerInfo.SpawnScene = provider.SpawnScene; + providerInfo.SpawnTime = provider.SpawnTime; + providerInfo.LoadingTime = provider.LoadingTime; + providerInfo.RefCount = provider.RefCount; + providerInfo.Status = provider.Status.ToString(); + providerInfo.DependBundleInfos = new List(); + provider.GetBundleDebugInfos(providerInfo.DependBundleInfos); + result.Add(providerInfo); + } + return result; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/AssetInfo.cs b/Assets/YooAsset/Runtime/ResourcePackage/AssetInfo.cs index 10775f7..189feb6 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/AssetInfo.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/AssetInfo.cs @@ -1,105 +1,105 @@  namespace YooAsset { - public class AssetInfo - { - private readonly PackageAsset _packageAsset; - private string _providerGUID; + public class AssetInfo + { + private readonly PackageAsset _packageAsset; + private string _providerGUID; - /// - /// 所属包裹 - /// - public string PackageName { private set; get; } + /// + /// 所属包裹 + /// + public string PackageName { private set; get; } - /// - /// 资源类型 - /// - public System.Type AssetType { private set; get; } + /// + /// 资源类型 + /// + public System.Type AssetType { private set; get; } - /// - /// 错误信息 - /// - public string Error { private set; get; } + /// + /// 错误信息 + /// + public string Error { private set; get; } - /// - /// 唯一标识符 - /// - internal string GUID - { - get - { - if (string.IsNullOrEmpty(_providerGUID) == false) - return _providerGUID; + /// + /// 唯一标识符 + /// + internal string GUID + { + get + { + if (string.IsNullOrEmpty(_providerGUID) == false) + return _providerGUID; - if (AssetType == null) - _providerGUID = $"[{AssetPath}][null]"; - else - _providerGUID = $"[{AssetPath}][{AssetType.Name}]"; - return _providerGUID; - } - } + if (AssetType == null) + _providerGUID = $"[{AssetPath}][null]"; + else + _providerGUID = $"[{AssetPath}][{AssetType.Name}]"; + return _providerGUID; + } + } - /// - /// 身份是否无效 - /// - internal bool IsInvalid - { - get - { - return _packageAsset == null; - } - } + /// + /// 身份是否无效 + /// + internal bool IsInvalid + { + get + { + return _packageAsset == null; + } + } - /// - /// 可寻址地址 - /// - public string Address - { - get - { - if (_packageAsset == null) - return string.Empty; - return _packageAsset.Address; - } - } + /// + /// 可寻址地址 + /// + public string Address + { + get + { + if (_packageAsset == null) + return string.Empty; + return _packageAsset.Address; + } + } - /// - /// 资源路径 - /// - public string AssetPath - { - get - { - if (_packageAsset == null) - return string.Empty; - return _packageAsset.AssetPath; - } - } + /// + /// 资源路径 + /// + public string AssetPath + { + get + { + if (_packageAsset == null) + return string.Empty; + return _packageAsset.AssetPath; + } + } - private AssetInfo() - { - // 注意:禁止从外部创建该类 - } - internal AssetInfo(string packageName, PackageAsset packageAsset, System.Type assetType) - { - if (packageAsset == null) - throw new System.Exception("Should never get here !"); + private AssetInfo() + { + // 注意:禁止从外部创建该类 + } + internal AssetInfo(string packageName, PackageAsset packageAsset, System.Type assetType) + { + if (packageAsset == null) + throw new System.Exception("Should never get here !"); - _providerGUID = string.Empty; - _packageAsset = packageAsset; - PackageName = packageName; - AssetType = assetType; - Error = string.Empty; - } - internal AssetInfo(string packageName, string error) - { - _providerGUID = string.Empty; - _packageAsset = null; - PackageName = packageName; - AssetType = null; - Error = error; - } - } + _providerGUID = string.Empty; + _packageAsset = packageAsset; + PackageName = packageName; + AssetType = assetType; + Error = string.Empty; + } + internal AssetInfo(string packageName, string error) + { + _providerGUID = string.Empty; + _packageAsset = null; + PackageName = packageName; + AssetType = null; + Error = error; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs b/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs index 74d8826..5a0dcd6 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs @@ -4,206 +4,206 @@ using UnityEngine; namespace YooAsset { - internal class BundleInfo - { - public enum ELoadMode - { - None, - LoadFromDelivery, - LoadFromStreaming, - LoadFromCache, - LoadFromRemote, - LoadFromEditor, - } + internal class BundleInfo + { + public enum ELoadMode + { + None, + LoadFromDelivery, + LoadFromStreaming, + LoadFromCache, + LoadFromRemote, + LoadFromEditor, + } - private readonly ResourceAssist _assist; + private readonly ResourceAssist _assist; - /// - /// 资源包对象 - /// - public readonly PackageBundle Bundle; + /// + /// 资源包对象 + /// + public readonly PackageBundle Bundle; - /// - /// 资源包加载模式 - /// - public readonly ELoadMode LoadMode; + /// + /// 资源包加载模式 + /// + public readonly ELoadMode LoadMode; - /// - /// 远端下载地址 - /// - public string RemoteMainURL { private set; get; } + /// + /// 远端下载地址 + /// + public string RemoteMainURL { private set; get; } - /// - /// 远端下载备用地址 - /// - public string RemoteFallbackURL { private set; get; } + /// + /// 远端下载备用地址 + /// + public string RemoteFallbackURL { private set; get; } - /// - /// 分发资源文件路径 - /// - public string DeliveryFilePath { private set; get; } + /// + /// 分发资源文件路径 + /// + public string DeliveryFilePath { private set; get; } - /// - /// 注意:该字段只用于帮助编辑器下的模拟模式。 - /// - public string[] IncludeAssetsInEditor; + /// + /// 注意:该字段只用于帮助编辑器下的模拟模式。 + /// + public string[] IncludeAssetsInEditor; - private BundleInfo() - { - } - public BundleInfo(ResourceAssist assist, PackageBundle bundle, ELoadMode loadMode, string mainURL, string fallbackURL) - { - _assist = assist; - Bundle = bundle; - LoadMode = loadMode; - RemoteMainURL = mainURL; - RemoteFallbackURL = fallbackURL; - DeliveryFilePath = string.Empty; - } - public BundleInfo(ResourceAssist assist, PackageBundle bundle, ELoadMode loadMode, string deliveryFilePath) - { - _assist = assist; - Bundle = bundle; - LoadMode = loadMode; - RemoteMainURL = string.Empty; - RemoteFallbackURL = string.Empty; - DeliveryFilePath = deliveryFilePath; - } - public BundleInfo(ResourceAssist assist, PackageBundle bundle, ELoadMode loadMode) - { - _assist = assist; - Bundle = bundle; - LoadMode = loadMode; - RemoteMainURL = string.Empty; - RemoteFallbackURL = string.Empty; - DeliveryFilePath = string.Empty; - } + private BundleInfo() + { + } + public BundleInfo(ResourceAssist assist, PackageBundle bundle, ELoadMode loadMode, string mainURL, string fallbackURL) + { + _assist = assist; + Bundle = bundle; + LoadMode = loadMode; + RemoteMainURL = mainURL; + RemoteFallbackURL = fallbackURL; + DeliveryFilePath = string.Empty; + } + public BundleInfo(ResourceAssist assist, PackageBundle bundle, ELoadMode loadMode, string deliveryFilePath) + { + _assist = assist; + Bundle = bundle; + LoadMode = loadMode; + RemoteMainURL = string.Empty; + RemoteFallbackURL = string.Empty; + DeliveryFilePath = deliveryFilePath; + } + public BundleInfo(ResourceAssist assist, PackageBundle bundle, ELoadMode loadMode) + { + _assist = assist; + Bundle = bundle; + LoadMode = loadMode; + RemoteMainURL = string.Empty; + RemoteFallbackURL = string.Empty; + DeliveryFilePath = string.Empty; + } - #region Cache - public bool IsCached() - { - return _assist.Cache.IsCached(Bundle.CacheGUID); - } - public void CacheRecord() - { - string infoFilePath = CachedInfoFilePath; - string dataFilePath = CachedDataFilePath; - string dataFileCRC = Bundle.FileCRC; - long dataFileSize = Bundle.FileSize; - var wrapper = new CacheManager.RecordWrapper(infoFilePath, dataFilePath, dataFileCRC, dataFileSize); - _assist.Cache.Record(Bundle.CacheGUID, wrapper); - } - public void CacheDiscard() - { - _assist.Cache.Discard(Bundle.CacheGUID); - } - public EVerifyResult VerifySelf() - { - return CacheHelper.VerifyingRecordFile(_assist.Cache, Bundle.CacheGUID); - } - #endregion + #region Cache + public bool IsCached() + { + return _assist.Cache.IsCached(Bundle.CacheGUID); + } + public void CacheRecord() + { + string infoFilePath = CachedInfoFilePath; + string dataFilePath = CachedDataFilePath; + string dataFileCRC = Bundle.FileCRC; + long dataFileSize = Bundle.FileSize; + var wrapper = new CacheManager.RecordWrapper(infoFilePath, dataFilePath, dataFileCRC, dataFileSize); + _assist.Cache.Record(Bundle.CacheGUID, wrapper); + } + public void CacheDiscard() + { + _assist.Cache.Discard(Bundle.CacheGUID); + } + public EVerifyResult VerifySelf() + { + return CacheHelper.VerifyingRecordFile(_assist.Cache, Bundle.CacheGUID); + } + #endregion - #region Persistent - public string CachedDataFilePath - { - get - { - return _assist.Persistent.GetCachedDataFilePath(Bundle); - } - } - public string CachedInfoFilePath - { - get - { - return _assist.Persistent.GetCachedInfoFilePath(Bundle); - } - } - public string TempDataFilePath - { - get - { - return _assist.Persistent.GetTempDataFilePath(Bundle); - } - } - public string BuildinFilePath - { - get - { - return _assist.Persistent.GetBuildinFilePath(Bundle); - } - } - #endregion + #region Persistent + public string CachedDataFilePath + { + get + { + return _assist.Persistent.GetCachedDataFilePath(Bundle); + } + } + public string CachedInfoFilePath + { + get + { + return _assist.Persistent.GetCachedInfoFilePath(Bundle); + } + } + public string TempDataFilePath + { + get + { + return _assist.Persistent.GetTempDataFilePath(Bundle); + } + } + public string BuildinFilePath + { + get + { + return _assist.Persistent.GetBuildinFilePath(Bundle); + } + } + #endregion - #region Download - public DownloaderBase CreateDownloader(int failedTryAgain, int timeout = 60) - { - return _assist.Download.CreateDownload(this, failedTryAgain, timeout); - } - public DownloaderBase CreateUnpacker(int failedTryAgain, int timeout = 60) - { - var unpackBundleInfo = ConvertToUnpackInfo(); - return _assist.Download.CreateDownload(unpackBundleInfo, failedTryAgain, timeout); - } - #endregion + #region Download + public DownloaderBase CreateDownloader(int failedTryAgain, int timeout = 60) + { + return _assist.Download.CreateDownload(this, failedTryAgain, timeout); + } + public DownloaderBase CreateUnpacker(int failedTryAgain, int timeout = 60) + { + var unpackBundleInfo = ConvertToUnpackInfo(); + return _assist.Download.CreateDownload(unpackBundleInfo, failedTryAgain, timeout); + } + #endregion - #region AssetBundle - internal AssetBundle LoadAssetBundle(string fileLoadPath, out Stream managedStream) - { - return _assist.Loader.LoadAssetBundle(this, fileLoadPath, out managedStream); - } - internal AssetBundleCreateRequest LoadAssetBundleAsync(string fileLoadPath, out Stream managedStream) - { - return _assist.Loader.LoadAssetBundleAsync(this, fileLoadPath, out managedStream); - } - internal AssetBundle LoadDeliveryAssetBundle(string fileLoadPath) - { - return _assist.Loader.LoadDeliveryAssetBundle(this, fileLoadPath); - } - internal AssetBundleCreateRequest LoadDeliveryAssetBundleAsync(string fileLoadPath) - { - return _assist.Loader.LoadDeliveryAssetBundleAsync(this, fileLoadPath); - } - #endregion + #region AssetBundle + internal AssetBundle LoadAssetBundle(string fileLoadPath, out Stream managedStream) + { + return _assist.Loader.LoadAssetBundle(this, fileLoadPath, out managedStream); + } + internal AssetBundleCreateRequest LoadAssetBundleAsync(string fileLoadPath, out Stream managedStream) + { + return _assist.Loader.LoadAssetBundleAsync(this, fileLoadPath, out managedStream); + } + internal AssetBundle LoadDeliveryAssetBundle(string fileLoadPath) + { + return _assist.Loader.LoadDeliveryAssetBundle(this, fileLoadPath); + } + internal AssetBundleCreateRequest LoadDeliveryAssetBundleAsync(string fileLoadPath) + { + return _assist.Loader.LoadDeliveryAssetBundleAsync(this, fileLoadPath); + } + #endregion - /// - /// 转换为解压BundleInfo - /// - private BundleInfo ConvertToUnpackInfo() - { - string streamingPath = PersistentHelper.ConvertToWWWPath(BuildinFilePath); - BundleInfo newBundleInfo = new BundleInfo(_assist, Bundle, ELoadMode.LoadFromStreaming, streamingPath, streamingPath); - return newBundleInfo; - } + /// + /// 转换为解压BundleInfo + /// + private BundleInfo ConvertToUnpackInfo() + { + string streamingPath = PersistentHelper.ConvertToWWWPath(BuildinFilePath); + BundleInfo newBundleInfo = new BundleInfo(_assist, Bundle, ELoadMode.LoadFromStreaming, streamingPath, streamingPath); + return newBundleInfo; + } - /// - /// 批量创建解压BundleInfo - /// - public static List CreateUnpackInfos(ResourceAssist assist, List unpackList) - { - List result = new List(unpackList.Count); - foreach (var packageBundle in unpackList) - { - var bundleInfo = CreateUnpackInfo(assist, packageBundle); - result.Add(bundleInfo); - } - return result; - } - private static BundleInfo CreateUnpackInfo(ResourceAssist assist, PackageBundle packageBundle) - { - string streamingPath = PersistentHelper.ConvertToWWWPath(assist.Persistent.GetBuildinFilePath(packageBundle)); - BundleInfo newBundleInfo = new BundleInfo(assist, packageBundle, ELoadMode.LoadFromStreaming, streamingPath, streamingPath); - return newBundleInfo; - } + /// + /// 批量创建解压BundleInfo + /// + public static List CreateUnpackInfos(ResourceAssist assist, List unpackList) + { + List result = new List(unpackList.Count); + foreach (var packageBundle in unpackList) + { + var bundleInfo = CreateUnpackInfo(assist, packageBundle); + result.Add(bundleInfo); + } + return result; + } + private static BundleInfo CreateUnpackInfo(ResourceAssist assist, PackageBundle packageBundle) + { + string streamingPath = PersistentHelper.ConvertToWWWPath(assist.Persistent.GetBuildinFilePath(packageBundle)); + BundleInfo newBundleInfo = new BundleInfo(assist, packageBundle, ELoadMode.LoadFromStreaming, streamingPath, streamingPath); + return newBundleInfo; + } - /// - /// 创建导入BundleInfo - /// - public static BundleInfo CreateImportInfo(ResourceAssist assist, PackageBundle packageBundle, string filePath) - { - // 注意:我们把本地文件路径指定为远端下载地址 - string persistentPath = PersistentHelper.ConvertToWWWPath(filePath); - BundleInfo bundleInfo = new BundleInfo(assist, packageBundle, BundleInfo.ELoadMode.None, persistentPath, persistentPath); - return bundleInfo; - } - } + /// + /// 创建导入BundleInfo + /// + public static BundleInfo CreateImportInfo(ResourceAssist assist, PackageBundle packageBundle, string filePath) + { + // 注意:我们把本地文件路径指定为远端下载地址 + string persistentPath = PersistentHelper.ConvertToWWWPath(filePath); + BundleInfo bundleInfo = new BundleInfo(assist, packageBundle, BundleInfo.ELoadMode.None, persistentPath, persistentPath); + return bundleInfo; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Interface/IBundleQuery.cs b/Assets/YooAsset/Runtime/ResourcePackage/Interface/IBundleQuery.cs index 6dcab86..bac81f1 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Interface/IBundleQuery.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Interface/IBundleQuery.cs @@ -1,31 +1,31 @@  namespace YooAsset { - internal interface IBundleQuery - { - /// - /// 获取主资源包信息 - /// - BundleInfo GetMainBundleInfo(AssetInfo assetInfo); + internal interface IBundleQuery + { + /// + /// 获取主资源包信息 + /// + BundleInfo GetMainBundleInfo(AssetInfo assetInfo); - /// - /// 获取依赖的资源包信息集合 - /// - BundleInfo[] GetDependBundleInfos(AssetInfo assetPath); + /// + /// 获取依赖的资源包信息集合 + /// + BundleInfo[] GetDependBundleInfos(AssetInfo assetPath); - /// - /// 获取主资源包名称 - /// - string GetMainBundleName(AssetInfo assetInfo); + /// + /// 获取主资源包名称 + /// + string GetMainBundleName(AssetInfo assetInfo); - /// - /// 获取依赖的资源包名称集合 - /// - string[] GetDependBundleNames(AssetInfo assetInfo); + /// + /// 获取依赖的资源包名称集合 + /// + string[] GetDependBundleNames(AssetInfo assetInfo); - /// - /// 清单是否有效 - /// - bool ManifestValid(); - } + /// + /// 清单是否有效 + /// + bool ManifestValid(); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Interface/IPlayMode.cs b/Assets/YooAsset/Runtime/ResourcePackage/Interface/IPlayMode.cs index 2d0471a..ca51c1e 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Interface/IPlayMode.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Interface/IPlayMode.cs @@ -1,43 +1,43 @@  namespace YooAsset { - internal interface IPlayMode - { - /// - /// 激活的清单 - /// - PackageManifest ActiveManifest { set; get; } - - /// - /// 保存清单版本文件到沙盒 - /// - void FlushManifestVersionFile(); + internal interface IPlayMode + { + /// + /// 激活的清单 + /// + PackageManifest ActiveManifest { set; get; } - /// - /// 向网络端请求最新的资源版本 - /// - UpdatePackageVersionOperation UpdatePackageVersionAsync(bool appendTimeTicks, int timeout); + /// + /// 保存清单版本文件到沙盒 + /// + void FlushManifestVersionFile(); - /// - /// 向网络端请求并更新清单 - /// - UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout); + /// + /// 向网络端请求最新的资源版本 + /// + UpdatePackageVersionOperation UpdatePackageVersionAsync(bool appendTimeTicks, int timeout); - /// - /// 预下载指定版本的包裹内容 - /// - PreDownloadContentOperation PreDownloadContentAsync(string packageVersion, int timeout); + /// + /// 向网络端请求并更新清单 + /// + UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout); - // 下载相关 - ResourceDownloaderOperation CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout); - ResourceDownloaderOperation CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout); - ResourceDownloaderOperation CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout); + /// + /// 预下载指定版本的包裹内容 + /// + PreDownloadContentOperation PreDownloadContentAsync(string packageVersion, int timeout); - // 解压相关 - ResourceUnpackerOperation CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout); - ResourceUnpackerOperation CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout); + // 下载相关 + ResourceDownloaderOperation CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout); + ResourceDownloaderOperation CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout); + ResourceDownloaderOperation CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout); - // 导入相关 - ResourceImporterOperation CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout); - } + // 解压相关 + ResourceUnpackerOperation CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout); + ResourceUnpackerOperation CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout); + + // 导入相关 + ResourceImporterOperation CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ManifestTools.cs b/Assets/YooAsset/Runtime/ResourcePackage/ManifestTools.cs index 182bce3..05f3465 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ManifestTools.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ManifestTools.cs @@ -6,228 +6,228 @@ using UnityEngine; namespace YooAsset { - internal static class ManifestTools - { + internal static class ManifestTools + { #if UNITY_EDITOR - /// - /// 序列化(JSON文件) - /// - public static void SerializeToJson(string savePath, PackageManifest manifest) - { - string json = JsonUtility.ToJson(manifest, true); - FileUtility.WriteAllText(savePath, json); - } + /// + /// 序列化(JSON文件) + /// + public static void SerializeToJson(string savePath, PackageManifest manifest) + { + string json = JsonUtility.ToJson(manifest, true); + FileUtility.WriteAllText(savePath, json); + } - /// - /// 序列化(二进制文件) - /// - public static void SerializeToBinary(string savePath, PackageManifest manifest) - { - using (FileStream fs = new FileStream(savePath, FileMode.Create)) - { - // 创建缓存器 - BufferWriter buffer = new BufferWriter(YooAssetSettings.ManifestFileMaxSize); + /// + /// 序列化(二进制文件) + /// + public static void SerializeToBinary(string savePath, PackageManifest manifest) + { + using (FileStream fs = new FileStream(savePath, FileMode.Create)) + { + // 创建缓存器 + BufferWriter buffer = new BufferWriter(YooAssetSettings.ManifestFileMaxSize); - // 写入文件标记 - buffer.WriteUInt32(YooAssetSettings.ManifestFileSign); + // 写入文件标记 + buffer.WriteUInt32(YooAssetSettings.ManifestFileSign); - // 写入文件版本 - buffer.WriteUTF8(manifest.FileVersion); + // 写入文件版本 + buffer.WriteUTF8(manifest.FileVersion); - // 写入文件头信息 - buffer.WriteBool(manifest.EnableAddressable); - buffer.WriteBool(manifest.LocationToLower); - buffer.WriteBool(manifest.IncludeAssetGUID); - buffer.WriteInt32(manifest.OutputNameStyle); - buffer.WriteUTF8(manifest.BuildPipeline); - buffer.WriteUTF8(manifest.PackageName); - buffer.WriteUTF8(manifest.PackageVersion); + // 写入文件头信息 + buffer.WriteBool(manifest.EnableAddressable); + buffer.WriteBool(manifest.LocationToLower); + buffer.WriteBool(manifest.IncludeAssetGUID); + buffer.WriteInt32(manifest.OutputNameStyle); + buffer.WriteUTF8(manifest.BuildPipeline); + buffer.WriteUTF8(manifest.PackageName); + buffer.WriteUTF8(manifest.PackageVersion); - // 写入资源列表 - buffer.WriteInt32(manifest.AssetList.Count); - for (int i = 0; i < manifest.AssetList.Count; i++) - { - var packageAsset = manifest.AssetList[i]; - buffer.WriteUTF8(packageAsset.Address); - buffer.WriteUTF8(packageAsset.AssetPath); - buffer.WriteUTF8(packageAsset.AssetGUID); - buffer.WriteUTF8Array(packageAsset.AssetTags); - buffer.WriteInt32(packageAsset.BundleID); - } + // 写入资源列表 + buffer.WriteInt32(manifest.AssetList.Count); + for (int i = 0; i < manifest.AssetList.Count; i++) + { + var packageAsset = manifest.AssetList[i]; + buffer.WriteUTF8(packageAsset.Address); + buffer.WriteUTF8(packageAsset.AssetPath); + buffer.WriteUTF8(packageAsset.AssetGUID); + buffer.WriteUTF8Array(packageAsset.AssetTags); + buffer.WriteInt32(packageAsset.BundleID); + } - // 写入资源包列表 - buffer.WriteInt32(manifest.BundleList.Count); - for (int i = 0; i < manifest.BundleList.Count; i++) - { - var packageBundle = manifest.BundleList[i]; - buffer.WriteUTF8(packageBundle.BundleName); - buffer.WriteUInt32(packageBundle.UnityCRC); - buffer.WriteUTF8(packageBundle.FileHash); - buffer.WriteUTF8(packageBundle.FileCRC); - buffer.WriteInt64(packageBundle.FileSize); - buffer.WriteBool(packageBundle.Encrypted); - buffer.WriteUTF8Array(packageBundle.Tags); - buffer.WriteInt32Array(packageBundle.DependIDs); - } + // 写入资源包列表 + buffer.WriteInt32(manifest.BundleList.Count); + for (int i = 0; i < manifest.BundleList.Count; i++) + { + var packageBundle = manifest.BundleList[i]; + buffer.WriteUTF8(packageBundle.BundleName); + buffer.WriteUInt32(packageBundle.UnityCRC); + buffer.WriteUTF8(packageBundle.FileHash); + buffer.WriteUTF8(packageBundle.FileCRC); + buffer.WriteInt64(packageBundle.FileSize); + buffer.WriteBool(packageBundle.Encrypted); + buffer.WriteUTF8Array(packageBundle.Tags); + buffer.WriteInt32Array(packageBundle.DependIDs); + } - // 写入文件流 - buffer.WriteToStream(fs); - fs.Flush(); - } - } + // 写入文件流 + buffer.WriteToStream(fs); + fs.Flush(); + } + } - /// - /// 反序列化(JSON文件) - /// - public static PackageManifest DeserializeFromJson(string jsonContent) - { - return JsonUtility.FromJson(jsonContent); - } + /// + /// 反序列化(JSON文件) + /// + public static PackageManifest DeserializeFromJson(string jsonContent) + { + return JsonUtility.FromJson(jsonContent); + } - /// - /// 反序列化(二进制文件) - /// - public static PackageManifest DeserializeFromBinary(byte[] binaryData) - { - // 创建缓存器 - BufferReader buffer = new BufferReader(binaryData); + /// + /// 反序列化(二进制文件) + /// + public static PackageManifest DeserializeFromBinary(byte[] binaryData) + { + // 创建缓存器 + BufferReader buffer = new BufferReader(binaryData); - // 读取文件标记 - uint fileSign = buffer.ReadUInt32(); - if (fileSign != YooAssetSettings.ManifestFileSign) - throw new Exception("Invalid manifest file !"); + // 读取文件标记 + uint fileSign = buffer.ReadUInt32(); + if (fileSign != YooAssetSettings.ManifestFileSign) + throw new Exception("Invalid manifest file !"); - // 读取文件版本 - string fileVersion = buffer.ReadUTF8(); - if (fileVersion != YooAssetSettings.ManifestFileVersion) - throw new Exception($"The manifest file version are not compatible : {fileVersion} != {YooAssetSettings.ManifestFileVersion}"); + // 读取文件版本 + string fileVersion = buffer.ReadUTF8(); + if (fileVersion != YooAssetSettings.ManifestFileVersion) + throw new Exception($"The manifest file version are not compatible : {fileVersion} != {YooAssetSettings.ManifestFileVersion}"); - PackageManifest manifest = new PackageManifest(); - { - // 读取文件头信息 - manifest.FileVersion = fileVersion; - manifest.EnableAddressable = buffer.ReadBool(); - manifest.LocationToLower = buffer.ReadBool(); - manifest.IncludeAssetGUID = buffer.ReadBool(); - manifest.OutputNameStyle = buffer.ReadInt32(); - manifest.BuildPipeline = buffer.ReadUTF8(); - manifest.PackageName = buffer.ReadUTF8(); - manifest.PackageVersion = buffer.ReadUTF8(); + PackageManifest manifest = new PackageManifest(); + { + // 读取文件头信息 + manifest.FileVersion = fileVersion; + manifest.EnableAddressable = buffer.ReadBool(); + manifest.LocationToLower = buffer.ReadBool(); + manifest.IncludeAssetGUID = buffer.ReadBool(); + manifest.OutputNameStyle = buffer.ReadInt32(); + manifest.BuildPipeline = buffer.ReadUTF8(); + manifest.PackageName = buffer.ReadUTF8(); + manifest.PackageVersion = buffer.ReadUTF8(); - // 检测配置 - if (manifest.EnableAddressable && manifest.LocationToLower) - throw new Exception("Addressable not support location to lower !"); + // 检测配置 + if (manifest.EnableAddressable && manifest.LocationToLower) + throw new Exception("Addressable not support location to lower !"); - // 读取资源列表 - int packageAssetCount = buffer.ReadInt32(); - manifest.AssetList = new List(packageAssetCount); - for (int i = 0; i < packageAssetCount; i++) - { - var packageAsset = new PackageAsset(); - packageAsset.Address = buffer.ReadUTF8(); - packageAsset.AssetPath = buffer.ReadUTF8(); - packageAsset.AssetGUID = buffer.ReadUTF8(); - packageAsset.AssetTags = buffer.ReadUTF8Array(); - packageAsset.BundleID = buffer.ReadInt32(); - manifest.AssetList.Add(packageAsset); - } + // 读取资源列表 + int packageAssetCount = buffer.ReadInt32(); + manifest.AssetList = new List(packageAssetCount); + for (int i = 0; i < packageAssetCount; i++) + { + var packageAsset = new PackageAsset(); + packageAsset.Address = buffer.ReadUTF8(); + packageAsset.AssetPath = buffer.ReadUTF8(); + packageAsset.AssetGUID = buffer.ReadUTF8(); + packageAsset.AssetTags = buffer.ReadUTF8Array(); + packageAsset.BundleID = buffer.ReadInt32(); + manifest.AssetList.Add(packageAsset); + } - // 读取资源包列表 - int packageBundleCount = buffer.ReadInt32(); - manifest.BundleList = new List(packageBundleCount); - for (int i = 0; i < packageBundleCount; i++) - { - var packageBundle = new PackageBundle(); - packageBundle.BundleName = buffer.ReadUTF8(); - packageBundle.UnityCRC = buffer.ReadUInt32(); - packageBundle.FileHash = buffer.ReadUTF8(); - packageBundle.FileCRC = buffer.ReadUTF8(); - packageBundle.FileSize = buffer.ReadInt64(); - packageBundle.Encrypted = buffer.ReadBool(); - packageBundle.Tags = buffer.ReadUTF8Array(); - packageBundle.DependIDs = buffer.ReadInt32Array(); - manifest.BundleList.Add(packageBundle); - } - } + // 读取资源包列表 + int packageBundleCount = buffer.ReadInt32(); + manifest.BundleList = new List(packageBundleCount); + for (int i = 0; i < packageBundleCount; i++) + { + var packageBundle = new PackageBundle(); + packageBundle.BundleName = buffer.ReadUTF8(); + packageBundle.UnityCRC = buffer.ReadUInt32(); + packageBundle.FileHash = buffer.ReadUTF8(); + packageBundle.FileCRC = buffer.ReadUTF8(); + packageBundle.FileSize = buffer.ReadInt64(); + packageBundle.Encrypted = buffer.ReadBool(); + packageBundle.Tags = buffer.ReadUTF8Array(); + packageBundle.DependIDs = buffer.ReadInt32Array(); + manifest.BundleList.Add(packageBundle); + } + } - // 填充BundleDic - manifest.BundleDic1 = new Dictionary(manifest.BundleList.Count); - manifest.BundleDic2 = new Dictionary(manifest.BundleList.Count); - foreach (var packageBundle in manifest.BundleList) - { - packageBundle.ParseBundle(manifest); - manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle); - manifest.BundleDic2.Add(packageBundle.FileName, packageBundle); - } + // 填充BundleDic + manifest.BundleDic1 = new Dictionary(manifest.BundleList.Count); + manifest.BundleDic2 = new Dictionary(manifest.BundleList.Count); + foreach (var packageBundle in manifest.BundleList) + { + packageBundle.ParseBundle(manifest); + manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle); + manifest.BundleDic2.Add(packageBundle.FileName, packageBundle); + } - // 填充AssetDic - manifest.AssetDic = new Dictionary(manifest.AssetList.Count); - foreach (var packageAsset in manifest.AssetList) - { - // 注意:我们不允许原始路径存在重名 - string assetPath = packageAsset.AssetPath; - if (manifest.AssetDic.ContainsKey(assetPath)) - throw new Exception($"AssetPath have existed : {assetPath}"); - else - manifest.AssetDic.Add(assetPath, packageAsset); - } + // 填充AssetDic + manifest.AssetDic = new Dictionary(manifest.AssetList.Count); + foreach (var packageAsset in manifest.AssetList) + { + // 注意:我们不允许原始路径存在重名 + string assetPath = packageAsset.AssetPath; + if (manifest.AssetDic.ContainsKey(assetPath)) + throw new Exception($"AssetPath have existed : {assetPath}"); + else + manifest.AssetDic.Add(assetPath, packageAsset); + } - return manifest; - } + return manifest; + } #endif - /// - /// 注意:该类拷贝自编辑器 - /// - private enum EFileNameStyle - { - /// - /// 哈希值名称 - /// - HashName = 0, + /// + /// 注意:该类拷贝自编辑器 + /// + private enum EFileNameStyle + { + /// + /// 哈希值名称 + /// + HashName = 0, - /// - /// 资源包名称(不推荐) - /// - BundleName = 1, + /// + /// 资源包名称(不推荐) + /// + BundleName = 1, - /// - /// 资源包名称 + 哈希值名称 - /// - BundleName_HashName = 2, - } + /// + /// 资源包名称 + 哈希值名称 + /// + BundleName_HashName = 2, + } - /// - /// 获取资源文件的后缀名 - /// - public static string GetRemoteBundleFileExtension(string bundleName) - { - string fileExtension = Path.GetExtension(bundleName); - return fileExtension; - } + /// + /// 获取资源文件的后缀名 + /// + public static string GetRemoteBundleFileExtension(string bundleName) + { + string fileExtension = Path.GetExtension(bundleName); + return fileExtension; + } - /// - /// 获取远端的资源文件名 - /// - public static string GetRemoteBundleFileName(int nameStyle, string bundleName, string fileExtension, string fileHash) - { - if (nameStyle == (int)EFileNameStyle.HashName) - { - return StringUtility.Format("{0}{1}", fileHash, fileExtension); - } - else if (nameStyle == (int)EFileNameStyle.BundleName) - { - return bundleName; - } - else if (nameStyle == (int)EFileNameStyle.BundleName_HashName) - { - string fileName = bundleName.Remove(bundleName.LastIndexOf('.')); - return StringUtility.Format("{0}_{1}{2}", fileName, fileHash, fileExtension); - } - else - { - throw new NotImplementedException($"Invalid name style : {nameStyle}"); - } - } - } + /// + /// 获取远端的资源文件名 + /// + public static string GetRemoteBundleFileName(int nameStyle, string bundleName, string fileExtension, string fileHash) + { + if (nameStyle == (int)EFileNameStyle.HashName) + { + return StringUtility.Format("{0}{1}", fileHash, fileExtension); + } + else if (nameStyle == (int)EFileNameStyle.BundleName) + { + return bundleName; + } + else if (nameStyle == (int)EFileNameStyle.BundleName_HashName) + { + string fileName = bundleName.Remove(bundleName.LastIndexOf('.')); + return StringUtility.Format("{0}_{1}{2}", fileName, fileHash, fileExtension); + } + else + { + throw new NotImplementedException($"Invalid name style : {nameStyle}"); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs index 8aea98b..0645055 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs @@ -3,366 +3,366 @@ using System.Collections.Generic; namespace YooAsset { - public abstract class DownloaderOperation : AsyncOperationBase - { - private enum ESteps - { - None, - Check, - Loading, - Done, - } + public abstract class DownloaderOperation : AsyncOperationBase + { + private enum ESteps + { + None, + Check, + Loading, + Done, + } - private const int MAX_LOADER_COUNT = 64; + private const int MAX_LOADER_COUNT = 64; - public delegate void OnDownloadOver(bool isSucceed); - public delegate void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes); - public delegate void OnDownloadError(string fileName, string error); - public delegate void OnStartDownloadFile(string fileName, long sizeBytes); + public delegate void OnDownloadOver(bool isSucceed); + public delegate void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes); + public delegate void OnDownloadError(string fileName, string error); + public delegate void OnStartDownloadFile(string fileName, long sizeBytes); - private readonly DownloadManager _downloadMgr; - private readonly string _packageName; - private readonly int _downloadingMaxNumber; - private readonly int _failedTryAgain; - private readonly int _timeout; - private readonly List _bundleInfoList; - private readonly List _downloaders = new List(MAX_LOADER_COUNT); - private readonly List _removeList = new List(MAX_LOADER_COUNT); - private readonly List _failedList = new List(MAX_LOADER_COUNT); + private readonly DownloadManager _downloadMgr; + private readonly string _packageName; + private readonly int _downloadingMaxNumber; + private readonly int _failedTryAgain; + private readonly int _timeout; + private readonly List _bundleInfoList; + private readonly List _downloaders = new List(MAX_LOADER_COUNT); + private readonly List _removeList = new List(MAX_LOADER_COUNT); + private readonly List _failedList = new List(MAX_LOADER_COUNT); - // 数据相关 - private bool _isPause = false; - private long _lastDownloadBytes = 0; - private int _lastDownloadCount = 0; - private long _cachedDownloadBytes = 0; - private int _cachedDownloadCount = 0; - private ESteps _steps = ESteps.None; + // 数据相关 + private bool _isPause = false; + private long _lastDownloadBytes = 0; + private int _lastDownloadCount = 0; + private long _cachedDownloadBytes = 0; + private int _cachedDownloadCount = 0; + private ESteps _steps = ESteps.None; - /// - /// 统计的下载文件总数量 - /// - public int TotalDownloadCount { private set; get; } + /// + /// 统计的下载文件总数量 + /// + public int TotalDownloadCount { private set; get; } - /// - /// 统计的下载文件的总大小 - /// - public long TotalDownloadBytes { private set; get; } + /// + /// 统计的下载文件的总大小 + /// + public long TotalDownloadBytes { private set; get; } - /// - /// 当前已经完成的下载总数量 - /// - public int CurrentDownloadCount - { - get { return _lastDownloadCount; } - } + /// + /// 当前已经完成的下载总数量 + /// + public int CurrentDownloadCount + { + get { return _lastDownloadCount; } + } - /// - /// 当前已经完成的下载总大小 - /// - public long CurrentDownloadBytes - { - get { return _lastDownloadBytes; } - } + /// + /// 当前已经完成的下载总大小 + /// + public long CurrentDownloadBytes + { + get { return _lastDownloadBytes; } + } - /// - /// 当下载器结束(无论成功或失败) - /// - public OnDownloadOver OnDownloadOverCallback { set; get; } + /// + /// 当下载器结束(无论成功或失败) + /// + public OnDownloadOver OnDownloadOverCallback { set; get; } - /// - /// 当下载进度发生变化 - /// - public OnDownloadProgress OnDownloadProgressCallback { set; get; } + /// + /// 当下载进度发生变化 + /// + public OnDownloadProgress OnDownloadProgressCallback { set; get; } - /// - /// 当某个文件下载失败 - /// - public OnDownloadError OnDownloadErrorCallback { set; get; } + /// + /// 当某个文件下载失败 + /// + public OnDownloadError OnDownloadErrorCallback { set; get; } - /// - /// 当开始下载某个文件 - /// - public OnStartDownloadFile OnStartDownloadFileCallback { set; get; } + /// + /// 当开始下载某个文件 + /// + public OnStartDownloadFile OnStartDownloadFileCallback { set; get; } - internal DownloaderOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - _downloadMgr = downloadMgr; - _packageName = packageName; - _bundleInfoList = downloadList; - _downloadingMaxNumber = UnityEngine.Mathf.Clamp(downloadingMaxNumber, 1, MAX_LOADER_COUNT); ; - _failedTryAgain = failedTryAgain; - _timeout = timeout; + internal DownloaderOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + _downloadMgr = downloadMgr; + _packageName = packageName; + _bundleInfoList = downloadList; + _downloadingMaxNumber = UnityEngine.Mathf.Clamp(downloadingMaxNumber, 1, MAX_LOADER_COUNT); ; + _failedTryAgain = failedTryAgain; + _timeout = timeout; - // 统计下载信息 - CalculatDownloaderInfo(); - } - internal override void InternalOnStart() - { - YooLogger.Log($"Begine to download : {TotalDownloadCount} files and {TotalDownloadBytes} bytes"); - _steps = ESteps.Check; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + // 统计下载信息 + CalculatDownloaderInfo(); + } + internal override void InternalOnStart() + { + YooLogger.Log($"Begine to download : {TotalDownloadCount} files and {TotalDownloadBytes} bytes"); + _steps = ESteps.Check; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.Check) - { - if (_bundleInfoList == null) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Download list is null."; - } - else - { - _steps = ESteps.Loading; - } - } + if (_steps == ESteps.Check) + { + if (_bundleInfoList == null) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Download list is null."; + } + else + { + _steps = ESteps.Loading; + } + } - if (_steps == ESteps.Loading) - { - // 检测下载器结果 - _removeList.Clear(); - long downloadBytes = _cachedDownloadBytes; - foreach (var downloader in _downloaders) - { - downloadBytes += (long)downloader.DownloadedBytes; - if (downloader.IsDone() == false) - continue; + if (_steps == ESteps.Loading) + { + // 检测下载器结果 + _removeList.Clear(); + long downloadBytes = _cachedDownloadBytes; + foreach (var downloader in _downloaders) + { + downloadBytes += (long)downloader.DownloadedBytes; + if (downloader.IsDone() == false) + continue; - // 检测是否下载失败 - if (downloader.HasError()) - { - _removeList.Add(downloader); - _failedList.Add(downloader); - continue; - } + // 检测是否下载失败 + if (downloader.HasError()) + { + _removeList.Add(downloader); + _failedList.Add(downloader); + continue; + } - // 下载成功 - _removeList.Add(downloader); - _cachedDownloadCount++; - _cachedDownloadBytes += downloader.GetDownloadFileSize(); - } + // 下载成功 + _removeList.Add(downloader); + _cachedDownloadCount++; + _cachedDownloadBytes += downloader.GetDownloadFileSize(); + } - // 移除已经完成的下载器(无论成功或失败) - foreach (var loader in _removeList) - { - _downloaders.Remove(loader); - } + // 移除已经完成的下载器(无论成功或失败) + foreach (var loader in _removeList) + { + _downloaders.Remove(loader); + } - // 如果下载进度发生变化 - if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != _cachedDownloadCount) - { - _lastDownloadBytes = downloadBytes; - _lastDownloadCount = _cachedDownloadCount; - Progress = (float)_lastDownloadBytes / TotalDownloadBytes; - OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes); - } + // 如果下载进度发生变化 + if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != _cachedDownloadCount) + { + _lastDownloadBytes = downloadBytes; + _lastDownloadCount = _cachedDownloadCount; + Progress = (float)_lastDownloadBytes / TotalDownloadBytes; + OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes); + } - // 动态创建新的下载器到最大数量限制 - // 注意:如果期间有下载失败的文件,暂停动态创建下载器 - if (_bundleInfoList.Count > 0 && _failedList.Count == 0) - { - if (_isPause) - return; + // 动态创建新的下载器到最大数量限制 + // 注意:如果期间有下载失败的文件,暂停动态创建下载器 + if (_bundleInfoList.Count > 0 && _failedList.Count == 0) + { + if (_isPause) + return; - if (_downloaders.Count < _downloadingMaxNumber) - { - int index = _bundleInfoList.Count - 1; - var bundleInfo = _bundleInfoList[index]; - var downloader = bundleInfo.CreateDownloader(_failedTryAgain, _timeout); - downloader.SendRequest(); - _downloaders.Add(downloader); - _bundleInfoList.RemoveAt(index); - OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize); - } - } + if (_downloaders.Count < _downloadingMaxNumber) + { + int index = _bundleInfoList.Count - 1; + var bundleInfo = _bundleInfoList[index]; + var downloader = bundleInfo.CreateDownloader(_failedTryAgain, _timeout); + downloader.SendRequest(); + _downloaders.Add(downloader); + _bundleInfoList.RemoveAt(index); + OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize); + } + } - // 下载结算 - if (_downloaders.Count == 0) - { - if (_failedList.Count > 0) - { - var failedDownloader = _failedList[0]; - string bundleName = failedDownloader.GetDownloadBundleName(); - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Failed to download file : {bundleName}"; - OnDownloadErrorCallback?.Invoke(bundleName, failedDownloader.GetLastError()); - OnDownloadOverCallback?.Invoke(false); - } - else - { - // 结算成功 - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - OnDownloadOverCallback?.Invoke(true); - } - } - } - } - private void CalculatDownloaderInfo() - { - if (_bundleInfoList != null) - { - TotalDownloadBytes = 0; - TotalDownloadCount = _bundleInfoList.Count; - foreach (var packageBundle in _bundleInfoList) - { - TotalDownloadBytes += packageBundle.Bundle.FileSize; - } - } - else - { - TotalDownloadBytes = 0; - TotalDownloadCount = 0; - } - } + // 下载结算 + if (_downloaders.Count == 0) + { + if (_failedList.Count > 0) + { + var failedDownloader = _failedList[0]; + string bundleName = failedDownloader.GetDownloadBundleName(); + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Failed to download file : {bundleName}"; + OnDownloadErrorCallback?.Invoke(bundleName, failedDownloader.GetLastError()); + OnDownloadOverCallback?.Invoke(false); + } + else + { + // 结算成功 + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + OnDownloadOverCallback?.Invoke(true); + } + } + } + } + private void CalculatDownloaderInfo() + { + if (_bundleInfoList != null) + { + TotalDownloadBytes = 0; + TotalDownloadCount = _bundleInfoList.Count; + foreach (var packageBundle in _bundleInfoList) + { + TotalDownloadBytes += packageBundle.Bundle.FileSize; + } + } + else + { + TotalDownloadBytes = 0; + TotalDownloadCount = 0; + } + } - /// - /// 合并其它下载器 - /// - /// 合并的下载器 - public void Combine(DownloaderOperation downloader) - { - if (_packageName != downloader._packageName) - { - YooLogger.Error("The downloaders have different resource packages !"); - return; - } + /// + /// 合并其它下载器 + /// + /// 合并的下载器 + public void Combine(DownloaderOperation downloader) + { + if (_packageName != downloader._packageName) + { + YooLogger.Error("The downloaders have different resource packages !"); + return; + } - if (Status != EOperationStatus.None) - { - YooLogger.Error("The downloader is running, can not combine with other downloader !"); - return; - } + if (Status != EOperationStatus.None) + { + YooLogger.Error("The downloader is running, can not combine with other downloader !"); + return; + } - HashSet temper = new HashSet(); - foreach (var bundleInfo in _bundleInfoList) - { - if (temper.Contains(bundleInfo.CachedDataFilePath) == false) - { - temper.Add(bundleInfo.CachedDataFilePath); - } - } + HashSet temper = new HashSet(); + foreach (var bundleInfo in _bundleInfoList) + { + if (temper.Contains(bundleInfo.CachedDataFilePath) == false) + { + temper.Add(bundleInfo.CachedDataFilePath); + } + } - // 合并下载列表 - foreach (var bundleInfo in downloader._bundleInfoList) - { - if (temper.Contains(bundleInfo.CachedDataFilePath) == false) - { - _bundleInfoList.Add(bundleInfo); - } - } + // 合并下载列表 + foreach (var bundleInfo in downloader._bundleInfoList) + { + if (temper.Contains(bundleInfo.CachedDataFilePath) == false) + { + _bundleInfoList.Add(bundleInfo); + } + } - // 重新统计下载信息 - CalculatDownloaderInfo(); - } + // 重新统计下载信息 + CalculatDownloaderInfo(); + } - /// - /// 开始下载 - /// - public void BeginDownload() - { - if (_steps == ESteps.None) - { - OperationSystem.StartOperation(_packageName, this); - } - } + /// + /// 开始下载 + /// + public void BeginDownload() + { + if (_steps == ESteps.None) + { + OperationSystem.StartOperation(_packageName, this); + } + } - /// - /// 暂停下载 - /// - public void PauseDownload() - { - _isPause = true; - } + /// + /// 暂停下载 + /// + public void PauseDownload() + { + _isPause = true; + } - /// - /// 恢复下载 - /// - public void ResumeDownload() - { - _isPause = false; - } + /// + /// 恢复下载 + /// + public void ResumeDownload() + { + _isPause = false; + } - /// - /// 取消下载 - /// - public void CancelDownload() - { - if (_steps != ESteps.Done) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "User cancel."; - ReleaseAllDownloader(); - } - } - private void ReleaseAllDownloader() - { - foreach (var downloader in _downloaders) - { - downloader.Release(); - } + /// + /// 取消下载 + /// + public void CancelDownload() + { + if (_steps != ESteps.Done) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "User cancel."; + ReleaseAllDownloader(); + } + } + private void ReleaseAllDownloader() + { + foreach (var downloader in _downloaders) + { + downloader.Release(); + } - // 注意:停止不再使用的下载器 - _downloadMgr.AbortUnusedDownloader(); - } - } + // 注意:停止不再使用的下载器 + _downloadMgr.AbortUnusedDownloader(); + } + } - public sealed class ResourceDownloaderOperation : DownloaderOperation - { - internal ResourceDownloaderOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) - : base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout) - { - } + public sealed class ResourceDownloaderOperation : DownloaderOperation + { + internal ResourceDownloaderOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) + : base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout) + { + } - /// - /// 创建空的下载器 - /// - internal static ResourceDownloaderOperation CreateEmptyDownloader(DownloadManager downloadMgr, string packageName, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = new List(); - var operation = new ResourceDownloaderOperation(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - } - public sealed class ResourceUnpackerOperation : DownloaderOperation - { - internal ResourceUnpackerOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) - : base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout) - { - } + /// + /// 创建空的下载器 + /// + internal static ResourceDownloaderOperation CreateEmptyDownloader(DownloadManager downloadMgr, string packageName, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = new List(); + var operation = new ResourceDownloaderOperation(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + } + public sealed class ResourceUnpackerOperation : DownloaderOperation + { + internal ResourceUnpackerOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) + : base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout) + { + } - /// - /// 创建空的解压器 - /// - internal static ResourceUnpackerOperation CreateEmptyUnpacker(DownloadManager downloadMgr, string packageName, int upackingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = new List(); - var operation = new ResourceUnpackerOperation(downloadMgr, packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue); - return operation; - } - } - public sealed class ResourceImporterOperation : DownloaderOperation - { - internal ResourceImporterOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) - : base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout) - { - } + /// + /// 创建空的解压器 + /// + internal static ResourceUnpackerOperation CreateEmptyUnpacker(DownloadManager downloadMgr, string packageName, int upackingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = new List(); + var operation = new ResourceUnpackerOperation(downloadMgr, packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue); + return operation; + } + } + public sealed class ResourceImporterOperation : DownloaderOperation + { + internal ResourceImporterOperation(DownloadManager downloadMgr, string packageName, List downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout) + : base(downloadMgr, packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout) + { + } - /// - /// 创建空的导入器 - /// - internal static ResourceImporterOperation CreateEmptyImporter(DownloadManager downloadMgr, string packageName, int upackingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = new List(); - var operation = new ResourceImporterOperation(downloadMgr, packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue); - return operation; - } - } + /// + /// 创建空的导入器 + /// + internal static ResourceImporterOperation CreateEmptyImporter(DownloadManager downloadMgr, string packageName, int upackingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = new List(); + var operation = new ResourceImporterOperation(downloadMgr, packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue); + return operation; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/InitializationOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/InitializationOperation.cs index a9879c2..f7a7fa1 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/InitializationOperation.cs @@ -5,500 +5,500 @@ using UnityEngine; namespace YooAsset { - /// - /// 初始化操作 - /// - public abstract class InitializationOperation : AsyncOperationBase - { - public string PackageVersion { protected set; get; } - } + /// + /// 初始化操作 + /// + public abstract class InitializationOperation : AsyncOperationBase + { + public string PackageVersion { protected set; get; } + } - /// - /// 编辑器下模拟模式的初始化操作 - /// - internal sealed class EditorSimulateModeInitializationOperation : InitializationOperation - { - private enum ESteps - { - None, - LoadEditorManifest, - Done, - } + /// + /// 编辑器下模拟模式的初始化操作 + /// + internal sealed class EditorSimulateModeInitializationOperation : InitializationOperation + { + private enum ESteps + { + None, + LoadEditorManifest, + Done, + } - private readonly EditorSimulateModeImpl _impl; - private readonly string _simulateManifestFilePath; - private LoadEditorManifestOperation _loadEditorManifestOp; - private ESteps _steps = ESteps.None; + private readonly EditorSimulateModeImpl _impl; + private readonly string _simulateManifestFilePath; + private LoadEditorManifestOperation _loadEditorManifestOp; + private ESteps _steps = ESteps.None; - internal EditorSimulateModeInitializationOperation(EditorSimulateModeImpl impl, string simulateManifestFilePath) - { - _impl = impl; - _simulateManifestFilePath = simulateManifestFilePath; - } - internal override void InternalOnStart() - { - _steps = ESteps.LoadEditorManifest; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.LoadEditorManifest) - { - if (_loadEditorManifestOp == null) - { - _loadEditorManifestOp = new LoadEditorManifestOperation(_impl.PackageName, _simulateManifestFilePath); - OperationSystem.StartOperation(_impl.PackageName, _loadEditorManifestOp); - } + internal EditorSimulateModeInitializationOperation(EditorSimulateModeImpl impl, string simulateManifestFilePath) + { + _impl = impl; + _simulateManifestFilePath = simulateManifestFilePath; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadEditorManifest; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.LoadEditorManifest) + { + if (_loadEditorManifestOp == null) + { + _loadEditorManifestOp = new LoadEditorManifestOperation(_impl.PackageName, _simulateManifestFilePath); + OperationSystem.StartOperation(_impl.PackageName, _loadEditorManifestOp); + } - if (_loadEditorManifestOp.IsDone == false) - return; + if (_loadEditorManifestOp.IsDone == false) + return; - if (_loadEditorManifestOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _loadEditorManifestOp.Manifest.PackageVersion; - _impl.ActiveManifest = _loadEditorManifestOp.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadEditorManifestOp.Error; - } - } - } - } + if (_loadEditorManifestOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _loadEditorManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadEditorManifestOp.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadEditorManifestOp.Error; + } + } + } + } - /// - /// 离线运行模式的初始化操作 - /// - internal sealed class OfflinePlayModeInitializationOperation : InitializationOperation - { - private enum ESteps - { - None, - QueryBuildinPackageVersion, - LoadBuildinManifest, - PackageCaching, - Done, - } + /// + /// 离线运行模式的初始化操作 + /// + internal sealed class OfflinePlayModeInitializationOperation : InitializationOperation + { + private enum ESteps + { + None, + QueryBuildinPackageVersion, + LoadBuildinManifest, + PackageCaching, + Done, + } - private readonly OfflinePlayModeImpl _impl; - private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; - private LoadBuildinManifestOperation _loadBuildinManifestOp; - private PackageCachingOperation _cachingOperation; - private ESteps _steps = ESteps.None; + private readonly OfflinePlayModeImpl _impl; + private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; + private LoadBuildinManifestOperation _loadBuildinManifestOp; + private PackageCachingOperation _cachingOperation; + private ESteps _steps = ESteps.None; - internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl) - { - _impl = impl; - } - internal override void InternalOnStart() - { - _steps = ESteps.QueryBuildinPackageVersion; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal OfflinePlayModeInitializationOperation(OfflinePlayModeImpl impl) + { + _impl = impl; + } + internal override void InternalOnStart() + { + _steps = ESteps.QueryBuildinPackageVersion; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.QueryBuildinPackageVersion) - { - if (_queryBuildinPackageVersionOp == null) - { - _queryBuildinPackageVersionOp = new QueryBuildinPackageVersionOperation(_impl.Persistent); - OperationSystem.StartOperation(_impl.PackageName, _queryBuildinPackageVersionOp); - } + if (_steps == ESteps.QueryBuildinPackageVersion) + { + if (_queryBuildinPackageVersionOp == null) + { + _queryBuildinPackageVersionOp = new QueryBuildinPackageVersionOperation(_impl.Persistent); + OperationSystem.StartOperation(_impl.PackageName, _queryBuildinPackageVersionOp); + } - if (_queryBuildinPackageVersionOp.IsDone == false) - return; + if (_queryBuildinPackageVersionOp.IsDone == false) + return; - if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.LoadBuildinManifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _queryBuildinPackageVersionOp.Error; - } - } + if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.LoadBuildinManifest; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryBuildinPackageVersionOp.Error; + } + } - if (_steps == ESteps.LoadBuildinManifest) - { - if (_loadBuildinManifestOp == null) - { - _loadBuildinManifestOp = new LoadBuildinManifestOperation(_impl.Persistent, _queryBuildinPackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_impl.PackageName, _loadBuildinManifestOp); - } + if (_steps == ESteps.LoadBuildinManifest) + { + if (_loadBuildinManifestOp == null) + { + _loadBuildinManifestOp = new LoadBuildinManifestOperation(_impl.Persistent, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_impl.PackageName, _loadBuildinManifestOp); + } - Progress = _loadBuildinManifestOp.Progress; - if (_loadBuildinManifestOp.IsDone == false) - return; + Progress = _loadBuildinManifestOp.Progress; + if (_loadBuildinManifestOp.IsDone == false) + return; - if (_loadBuildinManifestOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _loadBuildinManifestOp.Manifest.PackageVersion; - _impl.ActiveManifest = _loadBuildinManifestOp.Manifest; - _steps = ESteps.PackageCaching; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadBuildinManifestOp.Error; - } - } + if (_loadBuildinManifestOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _loadBuildinManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadBuildinManifestOp.Manifest; + _steps = ESteps.PackageCaching; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadBuildinManifestOp.Error; + } + } - if (_steps == ESteps.PackageCaching) - { - if (_cachingOperation == null) - { - _cachingOperation = new PackageCachingOperation(_impl.Persistent, _impl.Cache); - OperationSystem.StartOperation(_impl.PackageName, _cachingOperation); - } + if (_steps == ESteps.PackageCaching) + { + if (_cachingOperation == null) + { + _cachingOperation = new PackageCachingOperation(_impl.Persistent, _impl.Cache); + OperationSystem.StartOperation(_impl.PackageName, _cachingOperation); + } - Progress = _cachingOperation.Progress; - if (_cachingOperation.IsDone) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - } + Progress = _cachingOperation.Progress; + if (_cachingOperation.IsDone) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } - /// - /// 联机运行模式的初始化操作 - /// 注意:优先从沙盒里加载清单,如果沙盒里不存在就尝试把内置清单拷贝到沙盒并加载该清单。 - /// - internal sealed class HostPlayModeInitializationOperation : InitializationOperation - { - private enum ESteps - { - None, - CheckAppFootPrint, - QueryCachePackageVersion, - TryLoadCacheManifest, - QueryBuildinPackageVersion, - UnpackBuildinManifest, - LoadBuildinManifest, - PackageCaching, - Done, - } + /// + /// 联机运行模式的初始化操作 + /// 注意:优先从沙盒里加载清单,如果沙盒里不存在就尝试把内置清单拷贝到沙盒并加载该清单。 + /// + internal sealed class HostPlayModeInitializationOperation : InitializationOperation + { + private enum ESteps + { + None, + CheckAppFootPrint, + QueryCachePackageVersion, + TryLoadCacheManifest, + QueryBuildinPackageVersion, + UnpackBuildinManifest, + LoadBuildinManifest, + PackageCaching, + Done, + } - private readonly HostPlayModeImpl _impl; - private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; - private QueryCachePackageVersionOperation _queryCachePackageVersionOp; - private UnpackBuildinManifestOperation _unpackBuildinManifestOp; - private LoadBuildinManifestOperation _loadBuildinManifestOp; - private LoadCacheManifestOperation _loadCacheManifestOp; - private PackageCachingOperation _cachingOperation; - private ESteps _steps = ESteps.None; + private readonly HostPlayModeImpl _impl; + private QueryBuildinPackageVersionOperation _queryBuildinPackageVersionOp; + private QueryCachePackageVersionOperation _queryCachePackageVersionOp; + private UnpackBuildinManifestOperation _unpackBuildinManifestOp; + private LoadBuildinManifestOperation _loadBuildinManifestOp; + private LoadCacheManifestOperation _loadCacheManifestOp; + private PackageCachingOperation _cachingOperation; + private ESteps _steps = ESteps.None; - internal HostPlayModeInitializationOperation(HostPlayModeImpl impl) - { - _impl = impl; - } - internal override void InternalOnStart() - { - _steps = ESteps.CheckAppFootPrint; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal HostPlayModeInitializationOperation(HostPlayModeImpl impl) + { + _impl = impl; + } + internal override void InternalOnStart() + { + _steps = ESteps.CheckAppFootPrint; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.CheckAppFootPrint) - { - var appFootPrint = new AppFootPrint(_impl.Persistent); - appFootPrint.Load(_impl.PackageName); + if (_steps == ESteps.CheckAppFootPrint) + { + var appFootPrint = new AppFootPrint(_impl.Persistent); + appFootPrint.Load(_impl.PackageName); - // 如果水印发生变化,则说明覆盖安装后首次打开游戏 - if (appFootPrint.IsDirty()) - { - _impl.Persistent.DeleteSandboxManifestFilesFolder(); - appFootPrint.Coverage(_impl.PackageName); - YooLogger.Log("Delete manifest files when application foot print dirty !"); - } - _steps = ESteps.QueryCachePackageVersion; - } + // 如果水印发生变化,则说明覆盖安装后首次打开游戏 + if (appFootPrint.IsDirty()) + { + _impl.Persistent.DeleteSandboxManifestFilesFolder(); + appFootPrint.Coverage(_impl.PackageName); + YooLogger.Log("Delete manifest files when application foot print dirty !"); + } + _steps = ESteps.QueryCachePackageVersion; + } - if (_steps == ESteps.QueryCachePackageVersion) - { - if (_queryCachePackageVersionOp == null) - { - _queryCachePackageVersionOp = new QueryCachePackageVersionOperation(_impl.Persistent); - OperationSystem.StartOperation(_impl.PackageName, _queryCachePackageVersionOp); - } + if (_steps == ESteps.QueryCachePackageVersion) + { + if (_queryCachePackageVersionOp == null) + { + _queryCachePackageVersionOp = new QueryCachePackageVersionOperation(_impl.Persistent); + OperationSystem.StartOperation(_impl.PackageName, _queryCachePackageVersionOp); + } - if (_queryCachePackageVersionOp.IsDone == false) - return; + if (_queryCachePackageVersionOp.IsDone == false) + return; - if (_queryCachePackageVersionOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.TryLoadCacheManifest; - } - else - { - _steps = ESteps.QueryBuildinPackageVersion; - } - } + if (_queryCachePackageVersionOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.TryLoadCacheManifest; + } + else + { + _steps = ESteps.QueryBuildinPackageVersion; + } + } - if (_steps == ESteps.TryLoadCacheManifest) - { - if (_loadCacheManifestOp == null) - { - _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _queryCachePackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); - } + if (_steps == ESteps.TryLoadCacheManifest) + { + if (_loadCacheManifestOp == null) + { + _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _queryCachePackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); + } - if (_loadCacheManifestOp.IsDone == false) - return; + if (_loadCacheManifestOp.IsDone == false) + return; - if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _loadCacheManifestOp.Manifest.PackageVersion; - _impl.ActiveManifest = _loadCacheManifestOp.Manifest; - _steps = ESteps.PackageCaching; - } - else - { - _steps = ESteps.QueryBuildinPackageVersion; - } - } + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _loadCacheManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadCacheManifestOp.Manifest; + _steps = ESteps.PackageCaching; + } + else + { + _steps = ESteps.QueryBuildinPackageVersion; + } + } - if (_steps == ESteps.QueryBuildinPackageVersion) - { - if (_queryBuildinPackageVersionOp == null) - { - _queryBuildinPackageVersionOp = new QueryBuildinPackageVersionOperation(_impl.Persistent); - OperationSystem.StartOperation(_impl.PackageName, _queryBuildinPackageVersionOp); - } + if (_steps == ESteps.QueryBuildinPackageVersion) + { + if (_queryBuildinPackageVersionOp == null) + { + _queryBuildinPackageVersionOp = new QueryBuildinPackageVersionOperation(_impl.Persistent); + OperationSystem.StartOperation(_impl.PackageName, _queryBuildinPackageVersionOp); + } - if (_queryBuildinPackageVersionOp.IsDone == false) - return; + if (_queryBuildinPackageVersionOp.IsDone == false) + return; - if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.UnpackBuildinManifest; - } - else - { - // 注意:为了兼容MOD模式,初始化动态新增的包裹的时候,如果内置清单不存在也不需要报错! - _steps = ESteps.PackageCaching; - string error = _queryBuildinPackageVersionOp.Error; - YooLogger.Log($"Failed to load buildin package version file : {error}"); - } - } + if (_queryBuildinPackageVersionOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.UnpackBuildinManifest; + } + else + { + // 注意:为了兼容MOD模式,初始化动态新增的包裹的时候,如果内置清单不存在也不需要报错! + _steps = ESteps.PackageCaching; + string error = _queryBuildinPackageVersionOp.Error; + YooLogger.Log($"Failed to load buildin package version file : {error}"); + } + } - if (_steps == ESteps.UnpackBuildinManifest) - { - if (_unpackBuildinManifestOp == null) - { - _unpackBuildinManifestOp = new UnpackBuildinManifestOperation(_impl.Persistent, _queryBuildinPackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_impl.PackageName, _unpackBuildinManifestOp); - } + if (_steps == ESteps.UnpackBuildinManifest) + { + if (_unpackBuildinManifestOp == null) + { + _unpackBuildinManifestOp = new UnpackBuildinManifestOperation(_impl.Persistent, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_impl.PackageName, _unpackBuildinManifestOp); + } - if (_unpackBuildinManifestOp.IsDone == false) - return; + if (_unpackBuildinManifestOp.IsDone == false) + return; - if (_unpackBuildinManifestOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.LoadBuildinManifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _unpackBuildinManifestOp.Error; - } - } + if (_unpackBuildinManifestOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.LoadBuildinManifest; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _unpackBuildinManifestOp.Error; + } + } - if (_steps == ESteps.LoadBuildinManifest) - { - if (_loadBuildinManifestOp == null) - { - _loadBuildinManifestOp = new LoadBuildinManifestOperation(_impl.Persistent, _queryBuildinPackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_impl.PackageName, _loadBuildinManifestOp); - } + if (_steps == ESteps.LoadBuildinManifest) + { + if (_loadBuildinManifestOp == null) + { + _loadBuildinManifestOp = new LoadBuildinManifestOperation(_impl.Persistent, _queryBuildinPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_impl.PackageName, _loadBuildinManifestOp); + } - Progress = _loadBuildinManifestOp.Progress; - if (_loadBuildinManifestOp.IsDone == false) - return; + Progress = _loadBuildinManifestOp.Progress; + if (_loadBuildinManifestOp.IsDone == false) + return; - if (_loadBuildinManifestOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _loadBuildinManifestOp.Manifest.PackageVersion; - _impl.ActiveManifest = _loadBuildinManifestOp.Manifest; - _steps = ESteps.PackageCaching; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadBuildinManifestOp.Error; - } - } + if (_loadBuildinManifestOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _loadBuildinManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadBuildinManifestOp.Manifest; + _steps = ESteps.PackageCaching; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadBuildinManifestOp.Error; + } + } - if (_steps == ESteps.PackageCaching) - { - if (_cachingOperation == null) - { - _cachingOperation = new PackageCachingOperation(_impl.Persistent, _impl.Cache); - OperationSystem.StartOperation(_impl.PackageName,_cachingOperation); - } + if (_steps == ESteps.PackageCaching) + { + if (_cachingOperation == null) + { + _cachingOperation = new PackageCachingOperation(_impl.Persistent, _impl.Cache); + OperationSystem.StartOperation(_impl.PackageName, _cachingOperation); + } - Progress = _cachingOperation.Progress; - if (_cachingOperation.IsDone) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - } + Progress = _cachingOperation.Progress; + if (_cachingOperation.IsDone) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } - /// - /// WebGL运行模式的初始化操作 - /// - internal sealed class WebPlayModeInitializationOperation : InitializationOperation - { - private enum ESteps - { - None, - QueryWebPackageVersion, - LoadWebManifest, - Done, - } + /// + /// WebGL运行模式的初始化操作 + /// + internal sealed class WebPlayModeInitializationOperation : InitializationOperation + { + private enum ESteps + { + None, + QueryWebPackageVersion, + LoadWebManifest, + Done, + } - private readonly WebPlayModeImpl _impl; - private QueryBuildinPackageVersionOperation _queryWebPackageVersionOp; - private LoadBuildinManifestOperation _loadWebManifestOp; - private ESteps _steps = ESteps.None; + private readonly WebPlayModeImpl _impl; + private QueryBuildinPackageVersionOperation _queryWebPackageVersionOp; + private LoadBuildinManifestOperation _loadWebManifestOp; + private ESteps _steps = ESteps.None; - internal WebPlayModeInitializationOperation(WebPlayModeImpl impl) - { - _impl = impl; - } - internal override void InternalOnStart() - { - _steps = ESteps.QueryWebPackageVersion; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal WebPlayModeInitializationOperation(WebPlayModeImpl impl) + { + _impl = impl; + } + internal override void InternalOnStart() + { + _steps = ESteps.QueryWebPackageVersion; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.QueryWebPackageVersion) - { - if (_queryWebPackageVersionOp == null) - { - _queryWebPackageVersionOp = new QueryBuildinPackageVersionOperation(_impl.Persistent); - OperationSystem.StartOperation(_impl.PackageName, _queryWebPackageVersionOp); - } + if (_steps == ESteps.QueryWebPackageVersion) + { + if (_queryWebPackageVersionOp == null) + { + _queryWebPackageVersionOp = new QueryBuildinPackageVersionOperation(_impl.Persistent); + OperationSystem.StartOperation(_impl.PackageName, _queryWebPackageVersionOp); + } - if (_queryWebPackageVersionOp.IsDone == false) - return; + if (_queryWebPackageVersionOp.IsDone == false) + return; - if (_queryWebPackageVersionOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.LoadWebManifest; - } - else - { - // 注意:WebGL平台可能因为网络的原因会导致请求失败。如果内置清单不存在或者超时也不需要报错! - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - string error = _queryWebPackageVersionOp.Error; - YooLogger.Log($"Failed to load web package version file : {error}"); - } - } + if (_queryWebPackageVersionOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.LoadWebManifest; + } + else + { + // 注意:WebGL平台可能因为网络的原因会导致请求失败。如果内置清单不存在或者超时也不需要报错! + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + string error = _queryWebPackageVersionOp.Error; + YooLogger.Log($"Failed to load web package version file : {error}"); + } + } - if (_steps == ESteps.LoadWebManifest) - { - if (_loadWebManifestOp == null) - { - _loadWebManifestOp = new LoadBuildinManifestOperation(_impl.Persistent, _queryWebPackageVersionOp.PackageVersion); - OperationSystem.StartOperation(_impl.PackageName, _loadWebManifestOp); - } + if (_steps == ESteps.LoadWebManifest) + { + if (_loadWebManifestOp == null) + { + _loadWebManifestOp = new LoadBuildinManifestOperation(_impl.Persistent, _queryWebPackageVersionOp.PackageVersion); + OperationSystem.StartOperation(_impl.PackageName, _loadWebManifestOp); + } - Progress = _loadWebManifestOp.Progress; - if (_loadWebManifestOp.IsDone == false) - return; + Progress = _loadWebManifestOp.Progress; + if (_loadWebManifestOp.IsDone == false) + return; - if (_loadWebManifestOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _loadWebManifestOp.Manifest.PackageVersion; - _impl.ActiveManifest = _loadWebManifestOp.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadWebManifestOp.Error; - } - } - } - } + if (_loadWebManifestOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _loadWebManifestOp.Manifest.PackageVersion; + _impl.ActiveManifest = _loadWebManifestOp.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadWebManifestOp.Error; + } + } + } + } - /// - /// 应用程序水印 - /// - internal class AppFootPrint - { - private PersistentManager _persistent; - private string _footPrint; + /// + /// 应用程序水印 + /// + internal class AppFootPrint + { + private PersistentManager _persistent; + private string _footPrint; - public AppFootPrint(PersistentManager persistent) - { - _persistent = persistent; - } + public AppFootPrint(PersistentManager persistent) + { + _persistent = persistent; + } - /// - /// 读取应用程序水印 - /// - public void Load(string packageName) - { - string footPrintFilePath = _persistent.SandboxAppFootPrintFilePath; - if (File.Exists(footPrintFilePath)) - { - _footPrint = FileUtility.ReadAllText(footPrintFilePath); - } - else - { - Coverage(packageName); - } - } + /// + /// 读取应用程序水印 + /// + public void Load(string packageName) + { + string footPrintFilePath = _persistent.SandboxAppFootPrintFilePath; + if (File.Exists(footPrintFilePath)) + { + _footPrint = FileUtility.ReadAllText(footPrintFilePath); + } + else + { + Coverage(packageName); + } + } - /// - /// 检测水印是否发生变化 - /// - public bool IsDirty() - { + /// + /// 检测水印是否发生变化 + /// + public bool IsDirty() + { #if UNITY_EDITOR - return _footPrint != Application.version; + return _footPrint != Application.version; #else return _footPrint != Application.buildGUID; #endif - } + } - /// - /// 覆盖掉水印 - /// - public void Coverage(string packageName) - { + /// + /// 覆盖掉水印 + /// + public void Coverage(string packageName) + { #if UNITY_EDITOR - _footPrint = Application.version; + _footPrint = Application.version; #else _footPrint = Application.buildGUID; #endif - string footPrintFilePath = _persistent.SandboxAppFootPrintFilePath; - FileUtility.WriteAllText(footPrintFilePath, _footPrint); - YooLogger.Log($"Save application foot print : {_footPrint}"); - } - } + string footPrintFilePath = _persistent.SandboxAppFootPrintFilePath; + FileUtility.WriteAllText(footPrintFilePath, _footPrint); + YooLogger.Log($"Save application foot print : {_footPrint}"); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs index 903986c..b1d062f 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs @@ -4,237 +4,237 @@ using System.Collections.Generic; namespace YooAsset { - internal class DeserializeManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - DeserializeFileHeader, - PrepareAssetList, - DeserializeAssetList, - PrepareBundleList, - DeserializeBundleList, - Done, - } + internal class DeserializeManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DeserializeFileHeader, + PrepareAssetList, + DeserializeAssetList, + PrepareBundleList, + DeserializeBundleList, + Done, + } - private readonly BufferReader _buffer; - private int _packageAssetCount; - private int _packageBundleCount; - private int _progressTotalValue; - private ESteps _steps = ESteps.None; + private readonly BufferReader _buffer; + private int _packageAssetCount; + private int _packageBundleCount; + private int _progressTotalValue; + private ESteps _steps = ESteps.None; - /// - /// 解析的清单实例 - /// - public PackageManifest Manifest { private set; get; } + /// + /// 解析的清单实例 + /// + public PackageManifest Manifest { private set; get; } - public DeserializeManifestOperation(byte[] binaryData) - { - _buffer = new BufferReader(binaryData); - } - internal override void InternalOnStart() - { - _steps = ESteps.DeserializeFileHeader; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public DeserializeManifestOperation(byte[] binaryData) + { + _buffer = new BufferReader(binaryData); + } + internal override void InternalOnStart() + { + _steps = ESteps.DeserializeFileHeader; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - try - { - if (_steps == ESteps.DeserializeFileHeader) - { - if (_buffer.IsValid == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Buffer is invalid !"; - return; - } + try + { + if (_steps == ESteps.DeserializeFileHeader) + { + if (_buffer.IsValid == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Buffer is invalid !"; + return; + } - // 读取文件标记 - uint fileSign = _buffer.ReadUInt32(); - if (fileSign != YooAssetSettings.ManifestFileSign) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "The manifest file format is invalid !"; - return; - } + // 读取文件标记 + uint fileSign = _buffer.ReadUInt32(); + if (fileSign != YooAssetSettings.ManifestFileSign) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "The manifest file format is invalid !"; + return; + } - // 读取文件版本 - string fileVersion = _buffer.ReadUTF8(); - if (fileVersion != YooAssetSettings.ManifestFileVersion) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"The manifest file version are not compatible : {fileVersion} != {YooAssetSettings.ManifestFileVersion}"; - return; - } + // 读取文件版本 + string fileVersion = _buffer.ReadUTF8(); + if (fileVersion != YooAssetSettings.ManifestFileVersion) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"The manifest file version are not compatible : {fileVersion} != {YooAssetSettings.ManifestFileVersion}"; + return; + } - // 读取文件头信息 - Manifest = new PackageManifest(); - Manifest.FileVersion = fileVersion; - Manifest.EnableAddressable = _buffer.ReadBool(); - Manifest.LocationToLower = _buffer.ReadBool(); - Manifest.IncludeAssetGUID = _buffer.ReadBool(); - Manifest.OutputNameStyle = _buffer.ReadInt32(); - Manifest.BuildPipeline = _buffer.ReadUTF8(); - Manifest.PackageName = _buffer.ReadUTF8(); - Manifest.PackageVersion = _buffer.ReadUTF8(); + // 读取文件头信息 + Manifest = new PackageManifest(); + Manifest.FileVersion = fileVersion; + Manifest.EnableAddressable = _buffer.ReadBool(); + Manifest.LocationToLower = _buffer.ReadBool(); + Manifest.IncludeAssetGUID = _buffer.ReadBool(); + Manifest.OutputNameStyle = _buffer.ReadInt32(); + Manifest.BuildPipeline = _buffer.ReadUTF8(); + Manifest.PackageName = _buffer.ReadUTF8(); + Manifest.PackageVersion = _buffer.ReadUTF8(); - // 检测配置 - if (Manifest.EnableAddressable && Manifest.LocationToLower) - throw new System.Exception("Addressable not support location to lower !"); + // 检测配置 + if (Manifest.EnableAddressable && Manifest.LocationToLower) + throw new System.Exception("Addressable not support location to lower !"); - _steps = ESteps.PrepareAssetList; - } + _steps = ESteps.PrepareAssetList; + } - if (_steps == ESteps.PrepareAssetList) - { - _packageAssetCount = _buffer.ReadInt32(); - Manifest.AssetList = new List(_packageAssetCount); - Manifest.AssetDic = new Dictionary(_packageAssetCount); + if (_steps == ESteps.PrepareAssetList) + { + _packageAssetCount = _buffer.ReadInt32(); + Manifest.AssetList = new List(_packageAssetCount); + Manifest.AssetDic = new Dictionary(_packageAssetCount); - if (Manifest.EnableAddressable) - Manifest.AssetPathMapping1 = new Dictionary(_packageAssetCount * 3); - else - Manifest.AssetPathMapping1 = new Dictionary(_packageAssetCount * 2); + if (Manifest.EnableAddressable) + Manifest.AssetPathMapping1 = new Dictionary(_packageAssetCount * 3); + else + Manifest.AssetPathMapping1 = new Dictionary(_packageAssetCount * 2); - if (Manifest.IncludeAssetGUID) - Manifest.AssetPathMapping2 = new Dictionary(_packageAssetCount); - else - Manifest.AssetPathMapping2 = new Dictionary(); + if (Manifest.IncludeAssetGUID) + Manifest.AssetPathMapping2 = new Dictionary(_packageAssetCount); + else + Manifest.AssetPathMapping2 = new Dictionary(); - _progressTotalValue = _packageAssetCount; - _steps = ESteps.DeserializeAssetList; - } - if (_steps == ESteps.DeserializeAssetList) - { - while (_packageAssetCount > 0) - { - var packageAsset = new PackageAsset(); - packageAsset.Address = _buffer.ReadUTF8(); - packageAsset.AssetPath = _buffer.ReadUTF8(); - packageAsset.AssetGUID = _buffer.ReadUTF8(); - packageAsset.AssetTags = _buffer.ReadUTF8Array(); - packageAsset.BundleID = _buffer.ReadInt32(); - Manifest.AssetList.Add(packageAsset); + _progressTotalValue = _packageAssetCount; + _steps = ESteps.DeserializeAssetList; + } + if (_steps == ESteps.DeserializeAssetList) + { + while (_packageAssetCount > 0) + { + var packageAsset = new PackageAsset(); + packageAsset.Address = _buffer.ReadUTF8(); + packageAsset.AssetPath = _buffer.ReadUTF8(); + packageAsset.AssetGUID = _buffer.ReadUTF8(); + packageAsset.AssetTags = _buffer.ReadUTF8Array(); + packageAsset.BundleID = _buffer.ReadInt32(); + Manifest.AssetList.Add(packageAsset); - // 注意:我们不允许原始路径存在重名 - string assetPath = packageAsset.AssetPath; - if (Manifest.AssetDic.ContainsKey(assetPath)) - throw new System.Exception($"AssetPath have existed : {assetPath}"); - else - Manifest.AssetDic.Add(assetPath, packageAsset); + // 注意:我们不允许原始路径存在重名 + string assetPath = packageAsset.AssetPath; + if (Manifest.AssetDic.ContainsKey(assetPath)) + throw new System.Exception($"AssetPath have existed : {assetPath}"); + else + Manifest.AssetDic.Add(assetPath, packageAsset); - // 填充AssetPathMapping1 - { - string location = packageAsset.AssetPath; - if (Manifest.LocationToLower) - location = location.ToLower(); + // 填充AssetPathMapping1 + { + string location = packageAsset.AssetPath; + if (Manifest.LocationToLower) + location = location.ToLower(); - // 添加原生路径的映射 - if (Manifest.AssetPathMapping1.ContainsKey(location)) - throw new System.Exception($"Location have existed : {location}"); - else - Manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath); + // 添加原生路径的映射 + if (Manifest.AssetPathMapping1.ContainsKey(location)) + throw new System.Exception($"Location have existed : {location}"); + else + Manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath); - // 添加无后缀名路径的映射 - if (Path.HasExtension(location)) - { - string locationWithoutExtension = PathUtility.RemoveExtension(location); - if (Manifest.AssetPathMapping1.ContainsKey(locationWithoutExtension)) - YooLogger.Warning($"Location have existed : {locationWithoutExtension}"); - else - Manifest.AssetPathMapping1.Add(locationWithoutExtension, packageAsset.AssetPath); - } - } - if (Manifest.EnableAddressable) - { - string location = packageAsset.Address; - if (string.IsNullOrEmpty(location) == false) - { - if (Manifest.AssetPathMapping1.ContainsKey(location)) - throw new System.Exception($"Location have existed : {location}"); - else - Manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath); - } - } + // 添加无后缀名路径的映射 + if (Path.HasExtension(location)) + { + string locationWithoutExtension = PathUtility.RemoveExtension(location); + if (Manifest.AssetPathMapping1.ContainsKey(locationWithoutExtension)) + YooLogger.Warning($"Location have existed : {locationWithoutExtension}"); + else + Manifest.AssetPathMapping1.Add(locationWithoutExtension, packageAsset.AssetPath); + } + } + if (Manifest.EnableAddressable) + { + string location = packageAsset.Address; + if (string.IsNullOrEmpty(location) == false) + { + if (Manifest.AssetPathMapping1.ContainsKey(location)) + throw new System.Exception($"Location have existed : {location}"); + else + Manifest.AssetPathMapping1.Add(location, packageAsset.AssetPath); + } + } - // 填充AssetPathMapping2 - if (Manifest.IncludeAssetGUID) - { - if (Manifest.AssetPathMapping2.ContainsKey(packageAsset.AssetGUID)) - throw new System.Exception($"AssetGUID have existed : {packageAsset.AssetGUID}"); - else - Manifest.AssetPathMapping2.Add(packageAsset.AssetGUID, packageAsset.AssetPath); - } + // 填充AssetPathMapping2 + if (Manifest.IncludeAssetGUID) + { + if (Manifest.AssetPathMapping2.ContainsKey(packageAsset.AssetGUID)) + throw new System.Exception($"AssetGUID have existed : {packageAsset.AssetGUID}"); + else + Manifest.AssetPathMapping2.Add(packageAsset.AssetGUID, packageAsset.AssetPath); + } - _packageAssetCount--; - Progress = 1f - _packageAssetCount / _progressTotalValue; - if (OperationSystem.IsBusy) - break; - } + _packageAssetCount--; + Progress = 1f - _packageAssetCount / _progressTotalValue; + if (OperationSystem.IsBusy) + break; + } - if (_packageAssetCount <= 0) - { - _steps = ESteps.PrepareBundleList; - } - } + if (_packageAssetCount <= 0) + { + _steps = ESteps.PrepareBundleList; + } + } - if (_steps == ESteps.PrepareBundleList) - { - _packageBundleCount = _buffer.ReadInt32(); - Manifest.BundleList = new List(_packageBundleCount); - Manifest.BundleDic1 = new Dictionary(_packageBundleCount); - Manifest.BundleDic2 = new Dictionary(_packageBundleCount); - _progressTotalValue = _packageBundleCount; - _steps = ESteps.DeserializeBundleList; - } - if (_steps == ESteps.DeserializeBundleList) - { - while (_packageBundleCount > 0) - { - var packageBundle = new PackageBundle(); - packageBundle.BundleName = _buffer.ReadUTF8(); - packageBundle.UnityCRC = _buffer.ReadUInt32(); - packageBundle.FileHash = _buffer.ReadUTF8(); - packageBundle.FileCRC = _buffer.ReadUTF8(); - packageBundle.FileSize = _buffer.ReadInt64(); - packageBundle.Encrypted = _buffer.ReadBool(); - packageBundle.Tags = _buffer.ReadUTF8Array(); - packageBundle.DependIDs = _buffer.ReadInt32Array(); - packageBundle.ParseBundle(Manifest); - Manifest.BundleList.Add(packageBundle); - Manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle); - Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle); + if (_steps == ESteps.PrepareBundleList) + { + _packageBundleCount = _buffer.ReadInt32(); + Manifest.BundleList = new List(_packageBundleCount); + Manifest.BundleDic1 = new Dictionary(_packageBundleCount); + Manifest.BundleDic2 = new Dictionary(_packageBundleCount); + _progressTotalValue = _packageBundleCount; + _steps = ESteps.DeserializeBundleList; + } + if (_steps == ESteps.DeserializeBundleList) + { + while (_packageBundleCount > 0) + { + var packageBundle = new PackageBundle(); + packageBundle.BundleName = _buffer.ReadUTF8(); + packageBundle.UnityCRC = _buffer.ReadUInt32(); + packageBundle.FileHash = _buffer.ReadUTF8(); + packageBundle.FileCRC = _buffer.ReadUTF8(); + packageBundle.FileSize = _buffer.ReadInt64(); + packageBundle.Encrypted = _buffer.ReadBool(); + packageBundle.Tags = _buffer.ReadUTF8Array(); + packageBundle.DependIDs = _buffer.ReadInt32Array(); + packageBundle.ParseBundle(Manifest); + Manifest.BundleList.Add(packageBundle); + Manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle); + Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle); - // 注意:原始文件可能存在相同的CacheGUID - if (Manifest.CacheGUIDs.Contains(packageBundle.CacheGUID) == false) - Manifest.CacheGUIDs.Add(packageBundle.CacheGUID); + // 注意:原始文件可能存在相同的CacheGUID + if (Manifest.CacheGUIDs.Contains(packageBundle.CacheGUID) == false) + Manifest.CacheGUIDs.Add(packageBundle.CacheGUID); - _packageBundleCount--; - Progress = 1f - _packageBundleCount / _progressTotalValue; - if (OperationSystem.IsBusy) - break; - } + _packageBundleCount--; + Progress = 1f - _packageBundleCount / _progressTotalValue; + if (OperationSystem.IsBusy) + break; + } - if (_packageBundleCount <= 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - catch (System.Exception e) - { - Manifest = null; - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = e.Message; - } - } - } + if (_packageBundleCount <= 0) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + catch (System.Exception e) + { + Manifest = null; + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = e.Message; + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DownloadManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DownloadManifestOperation.cs index a97431e..d0454b1 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DownloadManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DownloadManifestOperation.cs @@ -1,113 +1,113 @@  namespace YooAsset { - internal class DownloadManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - DownloadPackageHashFile, - DownloadManifestFile, - Done, - } - - private readonly PersistentManager _persistent; - private readonly IRemoteServices _remoteServices; - private readonly string _packageVersion; - private readonly int _timeout; - private UnityWebFileRequester _downloader1; - private UnityWebFileRequester _downloader2; - private ESteps _steps = ESteps.None; - private int _requestCount = 0; + internal class DownloadManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DownloadPackageHashFile, + DownloadManifestFile, + Done, + } - internal DownloadManifestOperation(PersistentManager persistent, IRemoteServices remoteServices, string packageVersion, int timeout) - { - _persistent = persistent; - _remoteServices = remoteServices; - _packageVersion = packageVersion; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _requestCount = RequestHelper.GetRequestFailedCount(_persistent.PackageName, nameof(DownloadManifestOperation)); - _steps = ESteps.DownloadPackageHashFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + private readonly PersistentManager _persistent; + private readonly IRemoteServices _remoteServices; + private readonly string _packageVersion; + private readonly int _timeout; + private UnityWebFileRequester _downloader1; + private UnityWebFileRequester _downloader2; + private ESteps _steps = ESteps.None; + private int _requestCount = 0; - if (_steps == ESteps.DownloadPackageHashFile) - { - if (_downloader1 == null) - { - string savePath = _persistent.GetSandboxPackageHashFilePath(_packageVersion); - string fileName = YooAssetSettingsData.GetPackageHashFileName(_persistent.PackageName, _packageVersion); - string webURL = GetDownloadRequestURL(fileName); - YooLogger.Log($"Beginning to download package hash file : {webURL}"); - _downloader1 = new UnityWebFileRequester(); - _downloader1.SendRequest(webURL, savePath, _timeout); - } + internal DownloadManifestOperation(PersistentManager persistent, IRemoteServices remoteServices, string packageVersion, int timeout) + { + _persistent = persistent; + _remoteServices = remoteServices; + _packageVersion = packageVersion; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _requestCount = RequestHelper.GetRequestFailedCount(_persistent.PackageName, nameof(DownloadManifestOperation)); + _steps = ESteps.DownloadPackageHashFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - _downloader1.CheckTimeout(); - if (_downloader1.IsDone() == false) - return; + if (_steps == ESteps.DownloadPackageHashFile) + { + if (_downloader1 == null) + { + string savePath = _persistent.GetSandboxPackageHashFilePath(_packageVersion); + string fileName = YooAssetSettingsData.GetPackageHashFileName(_persistent.PackageName, _packageVersion); + string webURL = GetDownloadRequestURL(fileName); + YooLogger.Log($"Beginning to download package hash file : {webURL}"); + _downloader1 = new UnityWebFileRequester(); + _downloader1.SendRequest(webURL, savePath, _timeout); + } - if (_downloader1.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader1.GetError(); - RequestHelper.RecordRequestFailed(_persistent.PackageName, nameof(DownloadManifestOperation)); - } - else - { - _steps = ESteps.DownloadManifestFile; - } + _downloader1.CheckTimeout(); + if (_downloader1.IsDone() == false) + return; - _downloader1.Dispose(); - } + if (_downloader1.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader1.GetError(); + RequestHelper.RecordRequestFailed(_persistent.PackageName, nameof(DownloadManifestOperation)); + } + else + { + _steps = ESteps.DownloadManifestFile; + } - if (_steps == ESteps.DownloadManifestFile) - { - if (_downloader2 == null) - { - string savePath = _persistent.GetSandboxPackageManifestFilePath(_packageVersion); - string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_persistent.PackageName, _packageVersion); - string webURL = GetDownloadRequestURL(fileName); - YooLogger.Log($"Beginning to download package manifest file : {webURL}"); - _downloader2 = new UnityWebFileRequester(); - _downloader2.SendRequest(webURL, savePath, _timeout); - } + _downloader1.Dispose(); + } - _downloader2.CheckTimeout(); - if (_downloader2.IsDone() == false) - return; + if (_steps == ESteps.DownloadManifestFile) + { + if (_downloader2 == null) + { + string savePath = _persistent.GetSandboxPackageManifestFilePath(_packageVersion); + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_persistent.PackageName, _packageVersion); + string webURL = GetDownloadRequestURL(fileName); + YooLogger.Log($"Beginning to download package manifest file : {webURL}"); + _downloader2 = new UnityWebFileRequester(); + _downloader2.SendRequest(webURL, savePath, _timeout); + } - if (_downloader2.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader2.GetError(); - RequestHelper.RecordRequestFailed(_persistent.PackageName, nameof(DownloadManifestOperation)); - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } + _downloader2.CheckTimeout(); + if (_downloader2.IsDone() == false) + return; - _downloader2.Dispose(); - } - } + if (_downloader2.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader2.GetError(); + RequestHelper.RecordRequestFailed(_persistent.PackageName, nameof(DownloadManifestOperation)); + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } - private string GetDownloadRequestURL(string fileName) - { - // 轮流返回请求地址 - if (_requestCount % 2 == 0) - return _remoteServices.GetRemoteMainURL(fileName); - else - return _remoteServices.GetRemoteFallbackURL(fileName); - } - } + _downloader2.Dispose(); + } + } + + private string GetDownloadRequestURL(string fileName) + { + // 轮流返回请求地址 + if (_requestCount % 2 == 0) + return _remoteServices.GetRemoteMainURL(fileName); + else + return _remoteServices.GetRemoteFallbackURL(fileName); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadBuildinManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadBuildinManifestOperation.cs index e827daf..98d1c22 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadBuildinManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadBuildinManifestOperation.cs @@ -1,91 +1,91 @@  namespace YooAsset { - internal class LoadBuildinManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - LoadBuildinManifest, - CheckDeserializeManifest, - Done, - } + internal class LoadBuildinManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadBuildinManifest, + CheckDeserializeManifest, + Done, + } - private readonly PersistentManager _persistent; - private readonly string _buildinPackageVersion; - private UnityWebDataRequester _downloader; - private DeserializeManifestOperation _deserializer; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private readonly string _buildinPackageVersion; + private UnityWebDataRequester _downloader; + private DeserializeManifestOperation _deserializer; + private ESteps _steps = ESteps.None; - /// - /// 加载的清单实例 - /// - public PackageManifest Manifest { private set; get; } + /// + /// 加载的清单实例 + /// + public PackageManifest Manifest { private set; get; } - public LoadBuildinManifestOperation(PersistentManager persistent, string buildinPackageVersion) - { - _persistent = persistent; - _buildinPackageVersion = buildinPackageVersion; - } - internal override void InternalOnStart() - { - _steps = ESteps.LoadBuildinManifest; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public LoadBuildinManifestOperation(PersistentManager persistent, string buildinPackageVersion) + { + _persistent = persistent; + _buildinPackageVersion = buildinPackageVersion; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadBuildinManifest; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.LoadBuildinManifest) - { - if (_downloader == null) - { - string filePath = _persistent.GetBuildinPackageManifestFilePath(_buildinPackageVersion); - string url = PersistentHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(url); - } + if (_steps == ESteps.LoadBuildinManifest) + { + if (_downloader == null) + { + string filePath = _persistent.GetBuildinPackageManifestFilePath(_buildinPackageVersion); + string url = PersistentHelper.ConvertToWWWPath(filePath); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(url); + } - if (_downloader.IsDone() == false) - return; + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - } - else - { - byte[] bytesData = _downloader.GetData(); - _deserializer = new DeserializeManifestOperation(bytesData); - OperationSystem.StartOperation(_persistent.PackageName, _deserializer); - _steps = ESteps.CheckDeserializeManifest; - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader.GetError(); + } + else + { + byte[] bytesData = _downloader.GetData(); + _deserializer = new DeserializeManifestOperation(bytesData); + OperationSystem.StartOperation(_persistent.PackageName, _deserializer); + _steps = ESteps.CheckDeserializeManifest; + } - _downloader.Dispose(); - } + _downloader.Dispose(); + } - if (_steps == ESteps.CheckDeserializeManifest) - { - Progress = _deserializer.Progress; - if (_deserializer.IsDone == false) - return; + if (_steps == ESteps.CheckDeserializeManifest) + { + Progress = _deserializer.Progress; + if (_deserializer.IsDone == false) + return; - if (_deserializer.Status == EOperationStatus.Succeed) - { - Manifest = _deserializer.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _deserializer.Error; - } - } - } - } + if (_deserializer.Status == EOperationStatus.Succeed) + { + Manifest = _deserializer.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _deserializer.Error; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadCacheManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadCacheManifestOperation.cs index 3453790..57baf65 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadCacheManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadCacheManifestOperation.cs @@ -2,140 +2,140 @@ namespace YooAsset { - internal class LoadCacheManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - QueryCachePackageHash, - VerifyFileHash, - LoadCacheManifest, - CheckDeserializeManifest, - Done, - } + internal class LoadCacheManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + QueryCachePackageHash, + VerifyFileHash, + LoadCacheManifest, + CheckDeserializeManifest, + Done, + } - private readonly PersistentManager _persistent; - private readonly string _packageVersion; - private QueryCachePackageHashOperation _queryCachePackageHashOp; - private DeserializeManifestOperation _deserializer; - private string _manifestFilePath; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private readonly string _packageVersion; + private QueryCachePackageHashOperation _queryCachePackageHashOp; + private DeserializeManifestOperation _deserializer; + private string _manifestFilePath; + private ESteps _steps = ESteps.None; - /// - /// 加载的清单实例 - /// - public PackageManifest Manifest { private set; get; } + /// + /// 加载的清单实例 + /// + public PackageManifest Manifest { private set; get; } - public LoadCacheManifestOperation(PersistentManager persistent, string packageVersion) - { - _persistent = persistent; - _packageVersion = packageVersion; - } - internal override void InternalOnStart() - { - _steps = ESteps.QueryCachePackageHash; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public LoadCacheManifestOperation(PersistentManager persistent, string packageVersion) + { + _persistent = persistent; + _packageVersion = packageVersion; + } + internal override void InternalOnStart() + { + _steps = ESteps.QueryCachePackageHash; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.QueryCachePackageHash) - { - if (_queryCachePackageHashOp == null) - { - _queryCachePackageHashOp = new QueryCachePackageHashOperation(_persistent, _packageVersion); - OperationSystem.StartOperation(_persistent.PackageName, _queryCachePackageHashOp); - } + if (_steps == ESteps.QueryCachePackageHash) + { + if (_queryCachePackageHashOp == null) + { + _queryCachePackageHashOp = new QueryCachePackageHashOperation(_persistent, _packageVersion); + OperationSystem.StartOperation(_persistent.PackageName, _queryCachePackageHashOp); + } - if (_queryCachePackageHashOp.IsDone == false) - return; + if (_queryCachePackageHashOp.IsDone == false) + return; - if (_queryCachePackageHashOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.VerifyFileHash; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _queryCachePackageHashOp.Error; - ClearCacheFile(); - } - } + if (_queryCachePackageHashOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.VerifyFileHash; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryCachePackageHashOp.Error; + ClearCacheFile(); + } + } - if (_steps == ESteps.VerifyFileHash) - { - _manifestFilePath = _persistent.GetSandboxPackageManifestFilePath(_packageVersion); - if (File.Exists(_manifestFilePath) == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Not found cache manifest file : {_manifestFilePath}"; - ClearCacheFile(); - return; - } + if (_steps == ESteps.VerifyFileHash) + { + _manifestFilePath = _persistent.GetSandboxPackageManifestFilePath(_packageVersion); + if (File.Exists(_manifestFilePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Not found cache manifest file : {_manifestFilePath}"; + ClearCacheFile(); + return; + } - string fileHash = HashUtility.FileMD5(_manifestFilePath); - if (fileHash != _queryCachePackageHashOp.PackageHash) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Failed to verify cache manifest file hash !"; - ClearCacheFile(); - } - else - { - _steps = ESteps.LoadCacheManifest; - } - } + string fileHash = HashUtility.FileMD5(_manifestFilePath); + if (fileHash != _queryCachePackageHashOp.PackageHash) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Failed to verify cache manifest file hash !"; + ClearCacheFile(); + } + else + { + _steps = ESteps.LoadCacheManifest; + } + } - if (_steps == ESteps.LoadCacheManifest) - { - byte[] bytesData = File.ReadAllBytes(_manifestFilePath); - _deserializer = new DeserializeManifestOperation(bytesData); - OperationSystem.StartOperation(_persistent.PackageName, _deserializer); - _steps = ESteps.CheckDeserializeManifest; - } + if (_steps == ESteps.LoadCacheManifest) + { + byte[] bytesData = File.ReadAllBytes(_manifestFilePath); + _deserializer = new DeserializeManifestOperation(bytesData); + OperationSystem.StartOperation(_persistent.PackageName, _deserializer); + _steps = ESteps.CheckDeserializeManifest; + } - if (_steps == ESteps.CheckDeserializeManifest) - { - Progress = _deserializer.Progress; - if (_deserializer.IsDone == false) - return; + if (_steps == ESteps.CheckDeserializeManifest) + { + Progress = _deserializer.Progress; + if (_deserializer.IsDone == false) + return; - if (_deserializer.Status == EOperationStatus.Succeed) - { - Manifest = _deserializer.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _deserializer.Error; - ClearCacheFile(); - } - } - } + if (_deserializer.Status == EOperationStatus.Succeed) + { + Manifest = _deserializer.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _deserializer.Error; + ClearCacheFile(); + } + } + } - private void ClearCacheFile() - { - // 注意:如果加载沙盒内的清单报错,为了避免流程被卡住,主动把损坏的文件删除。 - if (File.Exists(_manifestFilePath)) - { - YooLogger.Warning($"Failed to load cache manifest file : {Error}"); - YooLogger.Warning($"Invalid cache manifest file have been removed : {_manifestFilePath}"); - File.Delete(_manifestFilePath); - } + private void ClearCacheFile() + { + // 注意:如果加载沙盒内的清单报错,为了避免流程被卡住,主动把损坏的文件删除。 + if (File.Exists(_manifestFilePath)) + { + YooLogger.Warning($"Failed to load cache manifest file : {Error}"); + YooLogger.Warning($"Invalid cache manifest file have been removed : {_manifestFilePath}"); + File.Delete(_manifestFilePath); + } - string hashFilePath = _persistent.GetSandboxPackageHashFilePath(_packageVersion); - if (File.Exists(hashFilePath)) - { - File.Delete(hashFilePath); - } - } - } + string hashFilePath = _persistent.GetSandboxPackageHashFilePath(_packageVersion); + if (File.Exists(hashFilePath)) + { + File.Delete(hashFilePath); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadEditorManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadEditorManifestOperation.cs index d698a2b..2dc1093 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadEditorManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadEditorManifestOperation.cs @@ -2,77 +2,77 @@ namespace YooAsset { - internal class LoadEditorManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - LoadEditorManifest, - CheckDeserializeManifest, - Done, - } + internal class LoadEditorManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadEditorManifest, + CheckDeserializeManifest, + Done, + } - private readonly string _packageName; - private readonly string _manifestFilePath; - private DeserializeManifestOperation _deserializer; - private ESteps _steps = ESteps.None; + private readonly string _packageName; + private readonly string _manifestFilePath; + private DeserializeManifestOperation _deserializer; + private ESteps _steps = ESteps.None; - /// - /// 加载的清单实例 - /// - public PackageManifest Manifest { private set; get; } + /// + /// 加载的清单实例 + /// + public PackageManifest Manifest { private set; get; } - public LoadEditorManifestOperation(string packageName, string manifestFilePath) - { - _packageName = packageName; - _manifestFilePath = manifestFilePath; - } - internal override void InternalOnStart() - { - _steps = ESteps.LoadEditorManifest; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public LoadEditorManifestOperation(string packageName, string manifestFilePath) + { + _packageName = packageName; + _manifestFilePath = manifestFilePath; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadEditorManifest; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.LoadEditorManifest) - { - if (File.Exists(_manifestFilePath) == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Not found simulation manifest file : {_manifestFilePath}"; - return; - } - - YooLogger.Log($"Load editor manifest file : {_manifestFilePath}"); - byte[] bytesData = FileUtility.ReadAllBytes(_manifestFilePath); - _deserializer = new DeserializeManifestOperation(bytesData); - OperationSystem.StartOperation(_packageName, _deserializer); - _steps = ESteps.CheckDeserializeManifest; - } + if (_steps == ESteps.LoadEditorManifest) + { + if (File.Exists(_manifestFilePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Not found simulation manifest file : {_manifestFilePath}"; + return; + } - if (_steps == ESteps.CheckDeserializeManifest) - { - Progress = _deserializer.Progress; - if (_deserializer.IsDone == false) - return; + YooLogger.Log($"Load editor manifest file : {_manifestFilePath}"); + byte[] bytesData = FileUtility.ReadAllBytes(_manifestFilePath); + _deserializer = new DeserializeManifestOperation(bytesData); + OperationSystem.StartOperation(_packageName, _deserializer); + _steps = ESteps.CheckDeserializeManifest; + } - if (_deserializer.Status == EOperationStatus.Succeed) - { - Manifest = _deserializer.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _deserializer.Error; - } - } - } - } + if (_steps == ESteps.CheckDeserializeManifest) + { + Progress = _deserializer.Progress; + if (_deserializer.IsDone == false) + return; + + if (_deserializer.Status == EOperationStatus.Succeed) + { + Manifest = _deserializer.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _deserializer.Error; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadRemoteManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadRemoteManifestOperation.cs index ff9af72..7cd168d 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadRemoteManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/LoadRemoteManifestOperation.cs @@ -1,151 +1,151 @@  namespace YooAsset { - internal class LoadRemoteManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - DownloadPackageHashFile, - DownloadManifestFile, - VerifyFileHash, - CheckDeserializeManifest, - Done, - } + internal class LoadRemoteManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DownloadPackageHashFile, + DownloadManifestFile, + VerifyFileHash, + CheckDeserializeManifest, + Done, + } - private readonly IRemoteServices _remoteServices; - private readonly string _packageName; - private readonly string _packageVersion; - private readonly int _timeout; - private QueryRemotePackageHashOperation _queryRemotePackageHashOp; - private UnityWebDataRequester _downloader; - private DeserializeManifestOperation _deserializer; - private byte[] _fileData; - private ESteps _steps = ESteps.None; - private int _requestCount = 0; + private readonly IRemoteServices _remoteServices; + private readonly string _packageName; + private readonly string _packageVersion; + private readonly int _timeout; + private QueryRemotePackageHashOperation _queryRemotePackageHashOp; + private UnityWebDataRequester _downloader; + private DeserializeManifestOperation _deserializer; + private byte[] _fileData; + private ESteps _steps = ESteps.None; + private int _requestCount = 0; - /// - /// 加载的清单实例 - /// - public PackageManifest Manifest { private set; get; } + /// + /// 加载的清单实例 + /// + public PackageManifest Manifest { private set; get; } - internal LoadRemoteManifestOperation(IRemoteServices remoteServices, string packageName, string packageVersion, int timeout) - { - _remoteServices = remoteServices; - _packageName = packageName; - _packageVersion = packageVersion; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(LoadRemoteManifestOperation)); - _steps = ESteps.DownloadPackageHashFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal LoadRemoteManifestOperation(IRemoteServices remoteServices, string packageName, string packageVersion, int timeout) + { + _remoteServices = remoteServices; + _packageName = packageName; + _packageVersion = packageVersion; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(LoadRemoteManifestOperation)); + _steps = ESteps.DownloadPackageHashFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.DownloadPackageHashFile) - { - if (_queryRemotePackageHashOp == null) - { - _queryRemotePackageHashOp = new QueryRemotePackageHashOperation(_remoteServices, _packageName, _packageVersion, _timeout); - OperationSystem.StartOperation(_packageName, _queryRemotePackageHashOp); - } + if (_steps == ESteps.DownloadPackageHashFile) + { + if (_queryRemotePackageHashOp == null) + { + _queryRemotePackageHashOp = new QueryRemotePackageHashOperation(_remoteServices, _packageName, _packageVersion, _timeout); + OperationSystem.StartOperation(_packageName, _queryRemotePackageHashOp); + } - if (_queryRemotePackageHashOp.IsDone == false) - return; + if (_queryRemotePackageHashOp.IsDone == false) + return; - if (_queryRemotePackageHashOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.DownloadManifestFile; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _queryRemotePackageHashOp.Error; - } - } + if (_queryRemotePackageHashOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.DownloadManifestFile; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryRemotePackageHashOp.Error; + } + } - if (_steps == ESteps.DownloadManifestFile) - { - if (_downloader == null) - { - string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion); - string webURL = GetDownloadRequestURL(fileName); - YooLogger.Log($"Beginning to download manifest file : {webURL}"); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(webURL, _timeout); - } + if (_steps == ESteps.DownloadManifestFile) + { + if (_downloader == null) + { + string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion); + string webURL = GetDownloadRequestURL(fileName); + YooLogger.Log($"Beginning to download manifest file : {webURL}"); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(webURL, _timeout); + } - _downloader.CheckTimeout(); - if (_downloader.IsDone() == false) - return; + _downloader.CheckTimeout(); + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - RequestHelper.RecordRequestFailed(_packageName, nameof(LoadRemoteManifestOperation)); - } - else - { - _fileData = _downloader.GetData(); - _steps = ESteps.VerifyFileHash; - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(LoadRemoteManifestOperation)); + } + else + { + _fileData = _downloader.GetData(); + _steps = ESteps.VerifyFileHash; + } - _downloader.Dispose(); - } + _downloader.Dispose(); + } - if (_steps == ESteps.VerifyFileHash) - { - string fileHash = HashUtility.BytesMD5(_fileData); - if (fileHash != _queryRemotePackageHashOp.PackageHash) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Failed to verify remote manifest file hash !"; - } - else - { - _deserializer = new DeserializeManifestOperation(_fileData); - OperationSystem.StartOperation(_packageName, _deserializer); - _steps = ESteps.CheckDeserializeManifest; - } - } + if (_steps == ESteps.VerifyFileHash) + { + string fileHash = HashUtility.BytesMD5(_fileData); + if (fileHash != _queryRemotePackageHashOp.PackageHash) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Failed to verify remote manifest file hash !"; + } + else + { + _deserializer = new DeserializeManifestOperation(_fileData); + OperationSystem.StartOperation(_packageName, _deserializer); + _steps = ESteps.CheckDeserializeManifest; + } + } - if (_steps == ESteps.CheckDeserializeManifest) - { - Progress = _deserializer.Progress; - if (_deserializer.IsDone == false) - return; + if (_steps == ESteps.CheckDeserializeManifest) + { + Progress = _deserializer.Progress; + if (_deserializer.IsDone == false) + return; - if (_deserializer.Status == EOperationStatus.Succeed) - { - Manifest = _deserializer.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _deserializer.Error; - } - } - } + if (_deserializer.Status == EOperationStatus.Succeed) + { + Manifest = _deserializer.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _deserializer.Error; + } + } + } - private string GetDownloadRequestURL(string fileName) - { - // 轮流返回请求地址 - if (_requestCount % 2 == 0) - return _remoteServices.GetRemoteMainURL(fileName); - else - return _remoteServices.GetRemoteFallbackURL(fileName); - } - } + private string GetDownloadRequestURL(string fileName) + { + // 轮流返回请求地址 + if (_requestCount % 2 == 0) + return _remoteServices.GetRemoteMainURL(fileName); + else + return _remoteServices.GetRemoteFallbackURL(fileName); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryBuildinPackageVersionOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryBuildinPackageVersionOperation.cs index c3ec396..b072b0c 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryBuildinPackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryBuildinPackageVersionOperation.cs @@ -1,75 +1,75 @@  namespace YooAsset { - internal class QueryBuildinPackageVersionOperation : AsyncOperationBase - { - private enum ESteps - { - None, - LoadBuildinPackageVersionFile, - Done, - } + internal class QueryBuildinPackageVersionOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadBuildinPackageVersionFile, + Done, + } - private readonly PersistentManager _persistent; - private UnityWebDataRequester _downloader; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private UnityWebDataRequester _downloader; + private ESteps _steps = ESteps.None; - /// - /// 包裹版本 - /// - public string PackageVersion { private set; get; } + /// + /// 包裹版本 + /// + public string PackageVersion { private set; get; } - public QueryBuildinPackageVersionOperation(PersistentManager persistent) - { - _persistent = persistent; - } - internal override void InternalOnStart() - { - _steps = ESteps.LoadBuildinPackageVersionFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public QueryBuildinPackageVersionOperation(PersistentManager persistent) + { + _persistent = persistent; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadBuildinPackageVersionFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.LoadBuildinPackageVersionFile) - { - if (_downloader == null) - { - string filePath = _persistent.GetBuildinPackageVersionFilePath(); - string url = PersistentHelper.ConvertToWWWPath(filePath); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(url); - } + if (_steps == ESteps.LoadBuildinPackageVersionFile) + { + if (_downloader == null) + { + string filePath = _persistent.GetBuildinPackageVersionFilePath(); + string url = PersistentHelper.ConvertToWWWPath(filePath); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(url); + } - if (_downloader.IsDone() == false) - return; + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - } - else - { - PackageVersion = _downloader.GetText(); - if (string.IsNullOrEmpty(PackageVersion)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Buildin package version file content is empty !"; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader.GetError(); + } + else + { + PackageVersion = _downloader.GetText(); + if (string.IsNullOrEmpty(PackageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Buildin package version file content is empty !"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } - _downloader.Dispose(); - } - } - } + _downloader.Dispose(); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageHashOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageHashOperation.cs index e325095..72f8ed9 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageHashOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageHashOperation.cs @@ -2,63 +2,63 @@ namespace YooAsset { - internal class QueryCachePackageHashOperation : AsyncOperationBase - { - private enum ESteps - { - None, - LoadCachePackageHashFile, - Done, - } + internal class QueryCachePackageHashOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadCachePackageHashFile, + Done, + } - private readonly PersistentManager _persistent; - private readonly string _packageVersion; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private readonly string _packageVersion; + private ESteps _steps = ESteps.None; - /// - /// 包裹哈希值 - /// - public string PackageHash { private set; get; } + /// + /// 包裹哈希值 + /// + public string PackageHash { private set; get; } - public QueryCachePackageHashOperation(PersistentManager persistent, string packageVersion) - { - _persistent = persistent; - _packageVersion = packageVersion; - } - internal override void InternalOnStart() - { - _steps = ESteps.LoadCachePackageHashFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public QueryCachePackageHashOperation(PersistentManager persistent, string packageVersion) + { + _persistent = persistent; + _packageVersion = packageVersion; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadCachePackageHashFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.LoadCachePackageHashFile) - { - string filePath = _persistent.GetSandboxPackageHashFilePath(_packageVersion); - if (File.Exists(filePath) == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Cache package hash file not found : {filePath}"; - return; - } + if (_steps == ESteps.LoadCachePackageHashFile) + { + string filePath = _persistent.GetSandboxPackageHashFilePath(_packageVersion); + if (File.Exists(filePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package hash file not found : {filePath}"; + return; + } - PackageHash = FileUtility.ReadAllText(filePath); - if (string.IsNullOrEmpty(PackageHash)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Cache package hash file content is empty !"; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - } + PackageHash = FileUtility.ReadAllText(filePath); + if (string.IsNullOrEmpty(PackageHash)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package hash file content is empty !"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageVersionOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageVersionOperation.cs index f5a8fbf..2584940 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryCachePackageVersionOperation.cs @@ -2,61 +2,61 @@ namespace YooAsset { - internal class QueryCachePackageVersionOperation : AsyncOperationBase - { - private enum ESteps - { - None, - LoadCachePackageVersionFile, - Done, - } + internal class QueryCachePackageVersionOperation : AsyncOperationBase + { + private enum ESteps + { + None, + LoadCachePackageVersionFile, + Done, + } - private readonly PersistentManager _persistent; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private ESteps _steps = ESteps.None; - /// - /// 包裹版本 - /// - public string PackageVersion { private set; get; } + /// + /// 包裹版本 + /// + public string PackageVersion { private set; get; } - public QueryCachePackageVersionOperation(PersistentManager persistent) - { - _persistent = persistent; - } - internal override void InternalOnStart() - { - _steps = ESteps.LoadCachePackageVersionFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public QueryCachePackageVersionOperation(PersistentManager persistent) + { + _persistent = persistent; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadCachePackageVersionFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.LoadCachePackageVersionFile) - { - string filePath = _persistent.GetSandboxPackageVersionFilePath(); - if (File.Exists(filePath) == false) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Cache package version file not found : {filePath}"; - return; - } + if (_steps == ESteps.LoadCachePackageVersionFile) + { + string filePath = _persistent.GetSandboxPackageVersionFilePath(); + if (File.Exists(filePath) == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package version file not found : {filePath}"; + return; + } - PackageVersion = FileUtility.ReadAllText(filePath); - if (string.IsNullOrEmpty(PackageVersion)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Cache package version file content is empty !"; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - } + PackageVersion = FileUtility.ReadAllText(filePath); + if (string.IsNullOrEmpty(PackageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Cache package version file content is empty !"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageHashOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageHashOperation.cs index 0c853eb..7ce2b8e 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageHashOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageHashOperation.cs @@ -1,100 +1,100 @@  namespace YooAsset { - internal class QueryRemotePackageHashOperation : AsyncOperationBase - { - private enum ESteps - { - None, - DownloadPackageHash, - Done, - } - - private readonly IRemoteServices _remoteServices; - private readonly string _packageName; - private readonly string _packageVersion; - private readonly int _timeout; - private UnityWebDataRequester _downloader; - private ESteps _steps = ESteps.None; - private int _requestCount = 0; + internal class QueryRemotePackageHashOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DownloadPackageHash, + Done, + } - /// - /// 包裹哈希值 - /// - public string PackageHash { private set; get; } + private readonly IRemoteServices _remoteServices; + private readonly string _packageName; + private readonly string _packageVersion; + private readonly int _timeout; + private UnityWebDataRequester _downloader; + private ESteps _steps = ESteps.None; + private int _requestCount = 0; + + /// + /// 包裹哈希值 + /// + public string PackageHash { private set; get; } - public QueryRemotePackageHashOperation(IRemoteServices remoteServices, string packageName, string packageVersion, int timeout) - { - _remoteServices = remoteServices; - _packageName = packageName; - _packageVersion = packageVersion; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePackageHashOperation)); - _steps = ESteps.DownloadPackageHash; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public QueryRemotePackageHashOperation(IRemoteServices remoteServices, string packageName, string packageVersion, int timeout) + { + _remoteServices = remoteServices; + _packageName = packageName; + _packageVersion = packageVersion; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePackageHashOperation)); + _steps = ESteps.DownloadPackageHash; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.DownloadPackageHash) - { - if (_downloader == null) - { - string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion); - string webURL = GetPackageHashRequestURL(fileName); - YooLogger.Log($"Beginning to request package hash : {webURL}"); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(webURL, _timeout); - } + if (_steps == ESteps.DownloadPackageHash) + { + if (_downloader == null) + { + string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion); + string webURL = GetPackageHashRequestURL(fileName); + YooLogger.Log($"Beginning to request package hash : {webURL}"); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(webURL, _timeout); + } - Progress = _downloader.Progress(); - _downloader.CheckTimeout(); - if (_downloader.IsDone() == false) - return; + Progress = _downloader.Progress(); + _downloader.CheckTimeout(); + if (_downloader.IsDone() == false) + return; - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePackageHashOperation)); - } - else - { - PackageHash = _downloader.GetText(); - if (string.IsNullOrEmpty(PackageHash)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Remote package hash is empty : {_downloader.URL}"; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePackageHashOperation)); + } + else + { + PackageHash = _downloader.GetText(); + if (string.IsNullOrEmpty(PackageHash)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Remote package hash is empty : {_downloader.URL}"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } - _downloader.Dispose(); - } - } + _downloader.Dispose(); + } + } - private string GetPackageHashRequestURL(string fileName) - { - string url; + private string GetPackageHashRequestURL(string fileName) + { + string url; - // 轮流返回请求地址 - if (_requestCount % 2 == 0) - url = _remoteServices.GetRemoteMainURL(fileName); - else - url = _remoteServices.GetRemoteFallbackURL(fileName); + // 轮流返回请求地址 + if (_requestCount % 2 == 0) + url = _remoteServices.GetRemoteMainURL(fileName); + else + url = _remoteServices.GetRemoteFallbackURL(fileName); - return url; - } - } + return url; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageVersionOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageVersionOperation.cs index c58ed21..47c0faf 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/QueryRemotePackageVersionOperation.cs @@ -1,104 +1,104 @@  namespace YooAsset { - internal class QueryRemotePackageVersionOperation : AsyncOperationBase - { - private enum ESteps - { - None, - DownloadPackageVersion, - Done, - } + internal class QueryRemotePackageVersionOperation : AsyncOperationBase + { + private enum ESteps + { + None, + DownloadPackageVersion, + Done, + } - private readonly IRemoteServices _remoteServices; - private readonly string _packageName; - private readonly bool _appendTimeTicks; - private readonly int _timeout; - private UnityWebDataRequester _downloader; - private ESteps _steps = ESteps.None; - private int _requestCount = 0; + private readonly IRemoteServices _remoteServices; + private readonly string _packageName; + private readonly bool _appendTimeTicks; + private readonly int _timeout; + private UnityWebDataRequester _downloader; + private ESteps _steps = ESteps.None; + private int _requestCount = 0; - /// - /// 包裹版本 - /// - public string PackageVersion { private set; get; } - + /// + /// 包裹版本 + /// + public string PackageVersion { private set; get; } - public QueryRemotePackageVersionOperation(IRemoteServices remoteServices, string packageName, bool appendTimeTicks, int timeout) - { - _remoteServices = remoteServices; - _packageName = packageName; - _appendTimeTicks = appendTimeTicks; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePackageVersionOperation)); - _steps = ESteps.DownloadPackageVersion; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; - if (_steps == ESteps.DownloadPackageVersion) - { - if (_downloader == null) - { - string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName); - string webURL = GetPackageVersionRequestURL(fileName); - YooLogger.Log($"Beginning to request package version : {webURL}"); - _downloader = new UnityWebDataRequester(); - _downloader.SendRequest(webURL, _timeout); - } + public QueryRemotePackageVersionOperation(IRemoteServices remoteServices, string packageName, bool appendTimeTicks, int timeout) + { + _remoteServices = remoteServices; + _packageName = packageName; + _appendTimeTicks = appendTimeTicks; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePackageVersionOperation)); + _steps = ESteps.DownloadPackageVersion; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - Progress = _downloader.Progress(); - _downloader.CheckTimeout(); - if (_downloader.IsDone() == false) - return; + if (_steps == ESteps.DownloadPackageVersion) + { + if (_downloader == null) + { + string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName); + string webURL = GetPackageVersionRequestURL(fileName); + YooLogger.Log($"Beginning to request package version : {webURL}"); + _downloader = new UnityWebDataRequester(); + _downloader.SendRequest(webURL, _timeout); + } - if (_downloader.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader.GetError(); - RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePackageVersionOperation)); - } - else - { - PackageVersion = _downloader.GetText(); - if (string.IsNullOrEmpty(PackageVersion)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Remote package version is empty : {_downloader.URL}"; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } + Progress = _downloader.Progress(); + _downloader.CheckTimeout(); + if (_downloader.IsDone() == false) + return; - _downloader.Dispose(); - } - } + if (_downloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePackageVersionOperation)); + } + else + { + PackageVersion = _downloader.GetText(); + if (string.IsNullOrEmpty(PackageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Remote package version is empty : {_downloader.URL}"; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } - private string GetPackageVersionRequestURL(string fileName) - { - string url; + _downloader.Dispose(); + } + } - // 轮流返回请求地址 - if (_requestCount % 2 == 0) - url = _remoteServices.GetRemoteMainURL(fileName); - else - url = _remoteServices.GetRemoteFallbackURL(fileName); + private string GetPackageVersionRequestURL(string fileName) + { + string url; - // 在URL末尾添加时间戳 - if (_appendTimeTicks) - return $"{url}?{System.DateTime.UtcNow.Ticks}"; - else - return url; - } - } + // 轮流返回请求地址 + if (_requestCount % 2 == 0) + url = _remoteServices.GetRemoteMainURL(fileName); + else + url = _remoteServices.GetRemoteFallbackURL(fileName); + + // 在URL末尾添加时间戳 + if (_appendTimeTicks) + return $"{url}?{System.DateTime.UtcNow.Ticks}"; + else + return url; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/UnpackBuildinManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/UnpackBuildinManifestOperation.cs index 898ade8..477c2b2 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/UnpackBuildinManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/UnpackBuildinManifestOperation.cs @@ -1,92 +1,92 @@  namespace YooAsset { - internal class UnpackBuildinManifestOperation : AsyncOperationBase - { - private enum ESteps - { - None, - UnpackManifestHashFile, - UnpackManifestFile, - Done, - } + internal class UnpackBuildinManifestOperation : AsyncOperationBase + { + private enum ESteps + { + None, + UnpackManifestHashFile, + UnpackManifestFile, + Done, + } - private readonly PersistentManager _persistent; - private readonly string _buildinPackageVersion; - private UnityWebFileRequester _downloader1; - private UnityWebFileRequester _downloader2; - private ESteps _steps = ESteps.None; + private readonly PersistentManager _persistent; + private readonly string _buildinPackageVersion; + private UnityWebFileRequester _downloader1; + private UnityWebFileRequester _downloader2; + private ESteps _steps = ESteps.None; - public UnpackBuildinManifestOperation(PersistentManager persistent, string buildinPackageVersion) - { - _persistent = persistent; - _buildinPackageVersion = buildinPackageVersion; - } - internal override void InternalOnStart() - { - _steps = ESteps.UnpackManifestHashFile; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + public UnpackBuildinManifestOperation(PersistentManager persistent, string buildinPackageVersion) + { + _persistent = persistent; + _buildinPackageVersion = buildinPackageVersion; + } + internal override void InternalOnStart() + { + _steps = ESteps.UnpackManifestHashFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.UnpackManifestHashFile) - { - if (_downloader1 == null) - { - string savePath = _persistent.GetSandboxPackageHashFilePath(_buildinPackageVersion); - string filePath = _persistent.GetBuildinPackageHashFilePath(_buildinPackageVersion); - string url = PersistentHelper.ConvertToWWWPath(filePath); - _downloader1 = new UnityWebFileRequester(); - _downloader1.SendRequest(url, savePath); - } + if (_steps == ESteps.UnpackManifestHashFile) + { + if (_downloader1 == null) + { + string savePath = _persistent.GetSandboxPackageHashFilePath(_buildinPackageVersion); + string filePath = _persistent.GetBuildinPackageHashFilePath(_buildinPackageVersion); + string url = PersistentHelper.ConvertToWWWPath(filePath); + _downloader1 = new UnityWebFileRequester(); + _downloader1.SendRequest(url, savePath); + } - if (_downloader1.IsDone() == false) - return; + if (_downloader1.IsDone() == false) + return; - if (_downloader1.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader1.GetError(); - } - else - { - _steps = ESteps.UnpackManifestFile; - } + if (_downloader1.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader1.GetError(); + } + else + { + _steps = ESteps.UnpackManifestFile; + } - _downloader1.Dispose(); - } + _downloader1.Dispose(); + } - if (_steps == ESteps.UnpackManifestFile) - { - if (_downloader2 == null) - { - string savePath = _persistent.GetSandboxPackageManifestFilePath(_buildinPackageVersion); - string filePath = _persistent.GetBuildinPackageManifestFilePath(_buildinPackageVersion); - string url = PersistentHelper.ConvertToWWWPath(filePath); - _downloader2 = new UnityWebFileRequester(); - _downloader2.SendRequest(url, savePath); - } + if (_steps == ESteps.UnpackManifestFile) + { + if (_downloader2 == null) + { + string savePath = _persistent.GetSandboxPackageManifestFilePath(_buildinPackageVersion); + string filePath = _persistent.GetBuildinPackageManifestFilePath(_buildinPackageVersion); + string url = PersistentHelper.ConvertToWWWPath(filePath); + _downloader2 = new UnityWebFileRequester(); + _downloader2.SendRequest(url, savePath); + } - if (_downloader2.IsDone() == false) - return; + if (_downloader2.IsDone() == false) + return; - if (_downloader2.HasError()) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloader2.GetError(); - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } + if (_downloader2.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloader2.GetError(); + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } - _downloader2.Dispose(); - } - } - } + _downloader2.Dispose(); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs index 68349ff..bab304b 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/PreDownloadContentOperation.cs @@ -4,360 +4,360 @@ using System.Collections.Generic; namespace YooAsset { - public abstract class PreDownloadContentOperation : AsyncOperationBase - { - /// - /// 创建资源下载器,用于下载当前资源版本所有的资源包文件 - /// - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public abstract ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60); + public abstract class PreDownloadContentOperation : AsyncOperationBase + { + /// + /// 创建资源下载器,用于下载当前资源版本所有的资源包文件 + /// + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public abstract ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60); - /// - /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件 - /// - /// 资源标签 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public abstract ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); + /// + /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件 + /// + /// 资源标签 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public abstract ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); - /// - /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件 - /// - /// 资源标签列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public abstract ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); + /// + /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件 + /// + /// 资源标签列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public abstract ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); - /// - /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 - /// - /// 资源定位地址 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public abstract ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); + /// + /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 + /// + /// 资源定位地址 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public abstract ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); - /// - /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 - /// - /// 资源定位地址列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public abstract ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); - } + /// + /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 + /// + /// 资源定位地址列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public abstract ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60); + } - internal class EditorPlayModePreDownloadContentOperation : PreDownloadContentOperation - { - private readonly EditorSimulateModeImpl _impl; + internal class EditorPlayModePreDownloadContentOperation : PreDownloadContentOperation + { + private readonly EditorSimulateModeImpl _impl; - public EditorPlayModePreDownloadContentOperation(EditorSimulateModeImpl impl) - { - _impl = impl; - } - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } + public EditorPlayModePreDownloadContentOperation(EditorSimulateModeImpl impl) + { + _impl = impl; + } + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } - public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - } - internal class OfflinePlayModePreDownloadContentOperation : PreDownloadContentOperation - { - private readonly OfflinePlayModeImpl _impl; + public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + } + internal class OfflinePlayModePreDownloadContentOperation : PreDownloadContentOperation + { + private readonly OfflinePlayModeImpl _impl; - public OfflinePlayModePreDownloadContentOperation(OfflinePlayModeImpl impl) - { - _impl = impl; - } - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } + public OfflinePlayModePreDownloadContentOperation(OfflinePlayModeImpl impl) + { + _impl = impl; + } + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } - public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - } - internal class HostPlayModePreDownloadContentOperation : PreDownloadContentOperation - { - private enum ESteps - { - None, - CheckActiveManifest, - TryLoadCacheManifest, - DownloadManifest, - LoadCacheManifest, - CheckDeserializeManifest, - Done, - } + public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + } + internal class HostPlayModePreDownloadContentOperation : PreDownloadContentOperation + { + private enum ESteps + { + None, + CheckActiveManifest, + TryLoadCacheManifest, + DownloadManifest, + LoadCacheManifest, + CheckDeserializeManifest, + Done, + } - private readonly HostPlayModeImpl _impl; - private readonly string _packageVersion; - private readonly int _timeout; - private LoadCacheManifestOperation _tryLoadCacheManifestOp; - private LoadCacheManifestOperation _loadCacheManifestOp; - private DownloadManifestOperation _downloadManifestOp; - private PackageManifest _manifest; - private ESteps _steps = ESteps.None; + private readonly HostPlayModeImpl _impl; + private readonly string _packageVersion; + private readonly int _timeout; + private LoadCacheManifestOperation _tryLoadCacheManifestOp; + private LoadCacheManifestOperation _loadCacheManifestOp; + private DownloadManifestOperation _downloadManifestOp; + private PackageManifest _manifest; + private ESteps _steps = ESteps.None; - internal HostPlayModePreDownloadContentOperation(HostPlayModeImpl impl, string packageVersion, int timeout) - { - _impl = impl; - _packageVersion = packageVersion; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _steps = ESteps.CheckActiveManifest; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal HostPlayModePreDownloadContentOperation(HostPlayModeImpl impl, string packageVersion, int timeout) + { + _impl = impl; + _packageVersion = packageVersion; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _steps = ESteps.CheckActiveManifest; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.CheckActiveManifest) - { - // 检测当前激活的清单对象 - if (_impl.ActiveManifest != null) - { - if (_impl.ActiveManifest.PackageVersion == _packageVersion) - { - _manifest = _impl.ActiveManifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - return; - } - } - _steps = ESteps.TryLoadCacheManifest; - } + if (_steps == ESteps.CheckActiveManifest) + { + // 检测当前激活的清单对象 + if (_impl.ActiveManifest != null) + { + if (_impl.ActiveManifest.PackageVersion == _packageVersion) + { + _manifest = _impl.ActiveManifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + return; + } + } + _steps = ESteps.TryLoadCacheManifest; + } - if (_steps == ESteps.TryLoadCacheManifest) - { - if (_tryLoadCacheManifestOp == null) - { - _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); - OperationSystem.StartOperation(_impl.PackageName, _tryLoadCacheManifestOp); - } + if (_steps == ESteps.TryLoadCacheManifest) + { + if (_tryLoadCacheManifestOp == null) + { + _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); + OperationSystem.StartOperation(_impl.PackageName, _tryLoadCacheManifestOp); + } - if (_tryLoadCacheManifestOp.IsDone == false) - return; + if (_tryLoadCacheManifestOp.IsDone == false) + return; - if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed) - { - _manifest = _tryLoadCacheManifestOp.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.DownloadManifest; - } - } + if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _manifest = _tryLoadCacheManifestOp.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.DownloadManifest; + } + } - if (_steps == ESteps.DownloadManifest) - { - if (_downloadManifestOp == null) - { - _downloadManifestOp = new DownloadManifestOperation(_impl.Persistent, _impl.RemoteServices, _packageVersion, _timeout); - OperationSystem.StartOperation(_impl.PackageName, _downloadManifestOp); - } + if (_steps == ESteps.DownloadManifest) + { + if (_downloadManifestOp == null) + { + _downloadManifestOp = new DownloadManifestOperation(_impl.Persistent, _impl.RemoteServices, _packageVersion, _timeout); + OperationSystem.StartOperation(_impl.PackageName, _downloadManifestOp); + } - if (_downloadManifestOp.IsDone == false) - return; + if (_downloadManifestOp.IsDone == false) + return; - if (_downloadManifestOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.LoadCacheManifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloadManifestOp.Error; - } - } + if (_downloadManifestOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.LoadCacheManifest; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloadManifestOp.Error; + } + } - if (_steps == ESteps.LoadCacheManifest) - { - if (_loadCacheManifestOp == null) - { - _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); - OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); - } + if (_steps == ESteps.LoadCacheManifest) + { + if (_loadCacheManifestOp == null) + { + _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); + OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); + } - if (_loadCacheManifestOp.IsDone == false) - return; + if (_loadCacheManifestOp.IsDone == false) + return; - if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) - { - _manifest = _loadCacheManifestOp.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadCacheManifestOp.Error; - } - } - } + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _manifest = _loadCacheManifestOp.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadCacheManifestOp.Error; + } + } + } - public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - if (Status != EOperationStatus.Succeed) - { - YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + if (Status != EOperationStatus.Succeed) + { + YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - List downloadList = _impl.GetDownloadListByAll(_manifest); - var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - if (Status != EOperationStatus.Succeed) - { - YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + List downloadList = _impl.GetDownloadListByAll(_manifest); + var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + if (Status != EOperationStatus.Succeed) + { + YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - List downloadList = _impl.GetDownloadListByTags(_manifest, new string[] { tag }); - var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - if (Status != EOperationStatus.Succeed) - { - YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + List downloadList = _impl.GetDownloadListByTags(_manifest, new string[] { tag }); + var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + if (Status != EOperationStatus.Succeed) + { + YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - List downloadList = _impl.GetDownloadListByTags(_manifest, tags); - var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - if (Status != EOperationStatus.Succeed) - { - YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + List downloadList = _impl.GetDownloadListByTags(_manifest, tags); + var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + if (Status != EOperationStatus.Succeed) + { + YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - List assetInfos = new List(); - var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null); - assetInfos.Add(assetInfo); + List assetInfos = new List(); + var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null); + assetInfos.Add(assetInfo); - List downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray()); - var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - if (Status != EOperationStatus.Succeed) - { - YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + List downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray()); + var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + if (Status != EOperationStatus.Succeed) + { + YooLogger.Warning($"{nameof(PreDownloadContentOperation)} status is not succeed !"); + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - List assetInfos = new List(locations.Length); - foreach (var location in locations) - { - var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null); - assetInfos.Add(assetInfo); - } + List assetInfos = new List(locations.Length); + foreach (var location in locations) + { + var assetInfo = _manifest.ConvertLocationToAssetInfo(location, null); + assetInfos.Add(assetInfo); + } - List downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray()); - var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - } - internal class WebPlayModePreDownloadContentOperation : PreDownloadContentOperation - { - private readonly WebPlayModeImpl _impl; + List downloadList = _impl.GetDownloadListByPaths(_manifest, assetInfos.ToArray()); + var operation = new ResourceDownloaderOperation(_impl.Download, _impl.PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + } + internal class WebPlayModePreDownloadContentOperation : PreDownloadContentOperation + { + private readonly WebPlayModeImpl _impl; - public WebPlayModePreDownloadContentOperation(WebPlayModeImpl impl) - { - _impl = impl; - } - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } + public WebPlayModePreDownloadContentOperation(WebPlayModeImpl impl) + { + _impl = impl; + } + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } - public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - } + public override ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + public override ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(_impl.Download, _impl.PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageManifestOperation.cs index f2ce4d1..1db5774 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageManifestOperation.cs @@ -4,291 +4,291 @@ using System.Collections.Generic; namespace YooAsset { - /// - /// 向远端请求并更新清单 - /// - public abstract class UpdatePackageManifestOperation : AsyncOperationBase - { - /// - /// 保存当前清单的版本,用于下次启动时自动加载的版本。 - /// - public virtual void SavePackageVersion() { } - } + /// + /// 向远端请求并更新清单 + /// + public abstract class UpdatePackageManifestOperation : AsyncOperationBase + { + /// + /// 保存当前清单的版本,用于下次启动时自动加载的版本。 + /// + public virtual void SavePackageVersion() { } + } - /// - /// 编辑器下模拟运行的更新清单操作 - /// - internal sealed class EditorPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation - { - public EditorPlayModeUpdatePackageManifestOperation() - { - } - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } - } + /// + /// 编辑器下模拟运行的更新清单操作 + /// + internal sealed class EditorPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation + { + public EditorPlayModeUpdatePackageManifestOperation() + { + } + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } + } - /// - /// 离线模式的更新清单操作 - /// - internal sealed class OfflinePlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation - { - public OfflinePlayModeUpdatePackageManifestOperation() - { - } - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } - } + /// + /// 离线模式的更新清单操作 + /// + internal sealed class OfflinePlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation + { + public OfflinePlayModeUpdatePackageManifestOperation() + { + } + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } + } - /// - /// 联机模式的更新清单操作 - /// 注意:优先加载沙盒里缓存的清单文件,如果缓存没找到就下载远端清单文件,并保存到本地。 - /// - internal sealed class HostPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation - { - private enum ESteps - { - None, - CheckParams, - CheckActiveManifest, - TryLoadCacheManifest, - DownloadManifest, - LoadCacheManifest, - CheckDeserializeManifest, - Done, - } + /// + /// 联机模式的更新清单操作 + /// 注意:优先加载沙盒里缓存的清单文件,如果缓存没找到就下载远端清单文件,并保存到本地。 + /// + internal sealed class HostPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation + { + private enum ESteps + { + None, + CheckParams, + CheckActiveManifest, + TryLoadCacheManifest, + DownloadManifest, + LoadCacheManifest, + CheckDeserializeManifest, + Done, + } - private readonly HostPlayModeImpl _impl; - private readonly string _packageVersion; - private readonly bool _autoSaveVersion; - private readonly int _timeout; - private LoadCacheManifestOperation _tryLoadCacheManifestOp; - private LoadCacheManifestOperation _loadCacheManifestOp; - private DownloadManifestOperation _downloadManifestOp; - private ESteps _steps = ESteps.None; + private readonly HostPlayModeImpl _impl; + private readonly string _packageVersion; + private readonly bool _autoSaveVersion; + private readonly int _timeout; + private LoadCacheManifestOperation _tryLoadCacheManifestOp; + private LoadCacheManifestOperation _loadCacheManifestOp; + private DownloadManifestOperation _downloadManifestOp; + private ESteps _steps = ESteps.None; - internal HostPlayModeUpdatePackageManifestOperation(HostPlayModeImpl impl, string packageVersion, bool autoSaveVersion, int timeout) - { - _impl = impl; - _packageVersion = packageVersion; - _autoSaveVersion = autoSaveVersion; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _steps = ESteps.CheckParams; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal HostPlayModeUpdatePackageManifestOperation(HostPlayModeImpl impl, string packageVersion, bool autoSaveVersion, int timeout) + { + _impl = impl; + _packageVersion = packageVersion; + _autoSaveVersion = autoSaveVersion; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _steps = ESteps.CheckParams; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.CheckParams) - { - if (string.IsNullOrEmpty(_packageVersion)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Package version is null or empty."; - return; - } + if (_steps == ESteps.CheckParams) + { + if (string.IsNullOrEmpty(_packageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Package version is null or empty."; + return; + } - _steps = ESteps.CheckActiveManifest; - } + _steps = ESteps.CheckActiveManifest; + } - if (_steps == ESteps.CheckActiveManifest) - { - // 检测当前激活的清单对象 - if (_impl.ActiveManifest != null && _impl.ActiveManifest.PackageVersion == _packageVersion) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.TryLoadCacheManifest; - } - } + if (_steps == ESteps.CheckActiveManifest) + { + // 检测当前激活的清单对象 + if (_impl.ActiveManifest != null && _impl.ActiveManifest.PackageVersion == _packageVersion) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.TryLoadCacheManifest; + } + } - if (_steps == ESteps.TryLoadCacheManifest) - { - if (_tryLoadCacheManifestOp == null) - { - _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); - OperationSystem.StartOperation(_impl.PackageName, _tryLoadCacheManifestOp); - } + if (_steps == ESteps.TryLoadCacheManifest) + { + if (_tryLoadCacheManifestOp == null) + { + _tryLoadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); + OperationSystem.StartOperation(_impl.PackageName, _tryLoadCacheManifestOp); + } - if (_tryLoadCacheManifestOp.IsDone == false) - return; + if (_tryLoadCacheManifestOp.IsDone == false) + return; - if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed) - { - _impl.ActiveManifest = _tryLoadCacheManifestOp.Manifest; - if (_autoSaveVersion) - SavePackageVersion(); - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.DownloadManifest; - } - } + if (_tryLoadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _impl.ActiveManifest = _tryLoadCacheManifestOp.Manifest; + if (_autoSaveVersion) + SavePackageVersion(); + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.DownloadManifest; + } + } - if (_steps == ESteps.DownloadManifest) - { - if (_downloadManifestOp == null) - { - _downloadManifestOp = new DownloadManifestOperation(_impl.Persistent, _impl.RemoteServices, _packageVersion, _timeout); - OperationSystem.StartOperation(_impl.PackageName, _downloadManifestOp); - } + if (_steps == ESteps.DownloadManifest) + { + if (_downloadManifestOp == null) + { + _downloadManifestOp = new DownloadManifestOperation(_impl.Persistent, _impl.RemoteServices, _packageVersion, _timeout); + OperationSystem.StartOperation(_impl.PackageName, _downloadManifestOp); + } - if (_downloadManifestOp.IsDone == false) - return; + if (_downloadManifestOp.IsDone == false) + return; - if (_downloadManifestOp.Status == EOperationStatus.Succeed) - { - _steps = ESteps.LoadCacheManifest; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _downloadManifestOp.Error; - } - } + if (_downloadManifestOp.Status == EOperationStatus.Succeed) + { + _steps = ESteps.LoadCacheManifest; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _downloadManifestOp.Error; + } + } - if (_steps == ESteps.LoadCacheManifest) - { - if (_loadCacheManifestOp == null) - { - _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); - OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); - } + if (_steps == ESteps.LoadCacheManifest) + { + if (_loadCacheManifestOp == null) + { + _loadCacheManifestOp = new LoadCacheManifestOperation(_impl.Persistent, _packageVersion); + OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); + } - if (_loadCacheManifestOp.IsDone == false) - return; + if (_loadCacheManifestOp.IsDone == false) + return; - if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) - { - _impl.ActiveManifest = _loadCacheManifestOp.Manifest; - if (_autoSaveVersion) - SavePackageVersion(); - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadCacheManifestOp.Error; - } - } - } + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _impl.ActiveManifest = _loadCacheManifestOp.Manifest; + if (_autoSaveVersion) + SavePackageVersion(); + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadCacheManifestOp.Error; + } + } + } - public override void SavePackageVersion() - { - _impl.FlushManifestVersionFile(); - } - } + public override void SavePackageVersion() + { + _impl.FlushManifestVersionFile(); + } + } - /// - /// WebGL模式的更新清单操作 - /// - internal sealed class WebPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation - { - private enum ESteps - { - None, - CheckParams, - CheckActiveManifest, - LoadRemoteManifest, - Done, - } + /// + /// WebGL模式的更新清单操作 + /// + internal sealed class WebPlayModeUpdatePackageManifestOperation : UpdatePackageManifestOperation + { + private enum ESteps + { + None, + CheckParams, + CheckActiveManifest, + LoadRemoteManifest, + Done, + } - private readonly WebPlayModeImpl _impl; - private readonly string _packageVersion; - private readonly int _timeout; - private LoadRemoteManifestOperation _loadCacheManifestOp; - private ESteps _steps = ESteps.None; + private readonly WebPlayModeImpl _impl; + private readonly string _packageVersion; + private readonly int _timeout; + private LoadRemoteManifestOperation _loadCacheManifestOp; + private ESteps _steps = ESteps.None; - internal WebPlayModeUpdatePackageManifestOperation(WebPlayModeImpl impl, string packageVersion, int timeout) - { - _impl = impl; - _packageVersion = packageVersion; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _steps = ESteps.CheckParams; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal WebPlayModeUpdatePackageManifestOperation(WebPlayModeImpl impl, string packageVersion, int timeout) + { + _impl = impl; + _packageVersion = packageVersion; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _steps = ESteps.CheckParams; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.CheckParams) - { - if (string.IsNullOrEmpty(_packageVersion)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Package version is null or empty."; - return; - } + if (_steps == ESteps.CheckParams) + { + if (string.IsNullOrEmpty(_packageVersion)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Package version is null or empty."; + return; + } - _steps = ESteps.CheckActiveManifest; - } + _steps = ESteps.CheckActiveManifest; + } - if (_steps == ESteps.CheckActiveManifest) - { - // 检测当前激活的清单对象 - if (_impl.ActiveManifest != null && _impl.ActiveManifest.PackageVersion == _packageVersion) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.LoadRemoteManifest; - } - } + if (_steps == ESteps.CheckActiveManifest) + { + // 检测当前激活的清单对象 + if (_impl.ActiveManifest != null && _impl.ActiveManifest.PackageVersion == _packageVersion) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.LoadRemoteManifest; + } + } - if (_steps == ESteps.LoadRemoteManifest) - { - if (_loadCacheManifestOp == null) - { - _loadCacheManifestOp = new LoadRemoteManifestOperation(_impl.RemoteServices, _impl.PackageName, _packageVersion, _timeout); - OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); - } + if (_steps == ESteps.LoadRemoteManifest) + { + if (_loadCacheManifestOp == null) + { + _loadCacheManifestOp = new LoadRemoteManifestOperation(_impl.RemoteServices, _impl.PackageName, _packageVersion, _timeout); + OperationSystem.StartOperation(_impl.PackageName, _loadCacheManifestOp); + } - if (_loadCacheManifestOp.IsDone == false) - return; + if (_loadCacheManifestOp.IsDone == false) + return; - if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) - { - _impl.ActiveManifest = _loadCacheManifestOp.Manifest; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _loadCacheManifestOp.Error; - } - } - } - } + if (_loadCacheManifestOp.Status == EOperationStatus.Succeed) + { + _impl.ActiveManifest = _loadCacheManifestOp.Manifest; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _loadCacheManifestOp.Error; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageVersionOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageVersionOperation.cs index b580c78..4ca4dfc 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/UpdatePackageVersionOperation.cs @@ -4,162 +4,162 @@ using UnityEngine; namespace YooAsset { - /// - /// 请求远端包裹的最新版本 - /// - public abstract class UpdatePackageVersionOperation : AsyncOperationBase - { - /// - /// 当前最新的包裹版本 - /// - public string PackageVersion { protected set; get; } - } + /// + /// 请求远端包裹的最新版本 + /// + public abstract class UpdatePackageVersionOperation : AsyncOperationBase + { + /// + /// 当前最新的包裹版本 + /// + public string PackageVersion { protected set; get; } + } - /// - /// 编辑器下模拟模式的请求远端包裹的最新版本 - /// - internal sealed class EditorPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation - { - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } - } + /// + /// 编辑器下模拟模式的请求远端包裹的最新版本 + /// + internal sealed class EditorPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation + { + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } + } - /// - /// 离线模式的请求远端包裹的最新版本 - /// - internal sealed class OfflinePlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation - { - internal override void InternalOnStart() - { - Status = EOperationStatus.Succeed; - } - internal override void InternalOnUpdate() - { - } - } + /// + /// 离线模式的请求远端包裹的最新版本 + /// + internal sealed class OfflinePlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation + { + internal override void InternalOnStart() + { + Status = EOperationStatus.Succeed; + } + internal override void InternalOnUpdate() + { + } + } - /// - /// 联机模式的请求远端包裹的最新版本 - /// - internal sealed class HostPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation - { - private enum ESteps - { - None, - QueryRemotePackageVersion, - Done, - } + /// + /// 联机模式的请求远端包裹的最新版本 + /// + internal sealed class HostPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation + { + private enum ESteps + { + None, + QueryRemotePackageVersion, + Done, + } - private readonly HostPlayModeImpl _impl; - private readonly bool _appendTimeTicks; - private readonly int _timeout; - private QueryRemotePackageVersionOperation _queryRemotePackageVersionOp; - private ESteps _steps = ESteps.None; + private readonly HostPlayModeImpl _impl; + private readonly bool _appendTimeTicks; + private readonly int _timeout; + private QueryRemotePackageVersionOperation _queryRemotePackageVersionOp; + private ESteps _steps = ESteps.None; - internal HostPlayModeUpdatePackageVersionOperation(HostPlayModeImpl impl, bool appendTimeTicks, int timeout) - { - _impl = impl; - _appendTimeTicks = appendTimeTicks; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _steps = ESteps.QueryRemotePackageVersion; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + internal HostPlayModeUpdatePackageVersionOperation(HostPlayModeImpl impl, bool appendTimeTicks, int timeout) + { + _impl = impl; + _appendTimeTicks = appendTimeTicks; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _steps = ESteps.QueryRemotePackageVersion; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_steps == ESteps.QueryRemotePackageVersion) - { - if (_queryRemotePackageVersionOp == null) - { - _queryRemotePackageVersionOp = new QueryRemotePackageVersionOperation(_impl.RemoteServices, _impl.PackageName, _appendTimeTicks, _timeout); - OperationSystem.StartOperation(_impl.PackageName, _queryRemotePackageVersionOp); - } + if (_steps == ESteps.QueryRemotePackageVersion) + { + if (_queryRemotePackageVersionOp == null) + { + _queryRemotePackageVersionOp = new QueryRemotePackageVersionOperation(_impl.RemoteServices, _impl.PackageName, _appendTimeTicks, _timeout); + OperationSystem.StartOperation(_impl.PackageName, _queryRemotePackageVersionOp); + } - if (_queryRemotePackageVersionOp.IsDone == false) - return; + if (_queryRemotePackageVersionOp.IsDone == false) + return; - if (_queryRemotePackageVersionOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _queryRemotePackageVersionOp.PackageVersion; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _queryRemotePackageVersionOp.Error; - } - } - } - } - - /// - /// WebGL模式的请求远端包裹的最新版本 - /// - internal sealed class WebPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation - { - private enum ESteps - { - None, - QueryRemotePackageVersion, - Done, - } + if (_queryRemotePackageVersionOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _queryRemotePackageVersionOp.PackageVersion; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryRemotePackageVersionOp.Error; + } + } + } + } - private readonly WebPlayModeImpl _impl; - private readonly bool _appendTimeTicks; - private readonly int _timeout; - private QueryRemotePackageVersionOperation _queryRemotePackageVersionOp; - private ESteps _steps = ESteps.None; - - internal WebPlayModeUpdatePackageVersionOperation(WebPlayModeImpl impl, bool appendTimeTicks, int timeout) - { - _impl = impl; - _appendTimeTicks = appendTimeTicks; - _timeout = timeout; - } - internal override void InternalOnStart() - { - _steps = ESteps.QueryRemotePackageVersion; - } - internal override void InternalOnUpdate() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; + /// + /// WebGL模式的请求远端包裹的最新版本 + /// + internal sealed class WebPlayModeUpdatePackageVersionOperation : UpdatePackageVersionOperation + { + private enum ESteps + { + None, + QueryRemotePackageVersion, + Done, + } - if (_steps == ESteps.QueryRemotePackageVersion) - { - if (_queryRemotePackageVersionOp == null) - { - _queryRemotePackageVersionOp = new QueryRemotePackageVersionOperation(_impl.RemoteServices, _impl.PackageName, _appendTimeTicks, _timeout); - OperationSystem.StartOperation(_impl.PackageName, _queryRemotePackageVersionOp); - } + private readonly WebPlayModeImpl _impl; + private readonly bool _appendTimeTicks; + private readonly int _timeout; + private QueryRemotePackageVersionOperation _queryRemotePackageVersionOp; + private ESteps _steps = ESteps.None; - if (_queryRemotePackageVersionOp.IsDone == false) - return; + internal WebPlayModeUpdatePackageVersionOperation(WebPlayModeImpl impl, bool appendTimeTicks, int timeout) + { + _impl = impl; + _appendTimeTicks = appendTimeTicks; + _timeout = timeout; + } + internal override void InternalOnStart() + { + _steps = ESteps.QueryRemotePackageVersion; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; - if (_queryRemotePackageVersionOp.Status == EOperationStatus.Succeed) - { - PackageVersion = _queryRemotePackageVersionOp.PackageVersion; - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = _queryRemotePackageVersionOp.Error; - } - } - } - } + if (_steps == ESteps.QueryRemotePackageVersion) + { + if (_queryRemotePackageVersionOp == null) + { + _queryRemotePackageVersionOp = new QueryRemotePackageVersionOperation(_impl.RemoteServices, _impl.PackageName, _appendTimeTicks, _timeout); + OperationSystem.StartOperation(_impl.PackageName, _queryRemotePackageVersionOp); + } + + if (_queryRemotePackageVersionOp.IsDone == false) + return; + + if (_queryRemotePackageVersionOp.Status == EOperationStatus.Succeed) + { + PackageVersion = _queryRemotePackageVersionOp.PackageVersion; + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _queryRemotePackageVersionOp.Error; + } + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PackageAsset.cs b/Assets/YooAsset/Runtime/ResourcePackage/PackageAsset.cs index 96f69ac..edfe4cc 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PackageAsset.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PackageAsset.cs @@ -3,50 +3,50 @@ using System.Linq; namespace YooAsset { - [Serializable] - internal class PackageAsset - { - /// - /// 可寻址地址 - /// - public string Address; - - /// - /// 资源路径 - /// - public string AssetPath; + [Serializable] + internal class PackageAsset + { + /// + /// 可寻址地址 + /// + public string Address; - /// - /// 资源GUID - /// - public string AssetGUID; + /// + /// 资源路径 + /// + public string AssetPath; - /// - /// 资源的分类标签 - /// - public string[] AssetTags; - - /// - /// 所属资源包ID - /// - public int BundleID; + /// + /// 资源GUID + /// + public string AssetGUID; - /// - /// 是否包含Tag - /// - public bool HasTag(string[] tags) - { - if (tags == null || tags.Length == 0) - return false; - if (AssetTags == null || AssetTags.Length == 0) - return false; + /// + /// 资源的分类标签 + /// + public string[] AssetTags; - foreach (var tag in tags) - { - if (AssetTags.Contains(tag)) - return true; - } - return false; - } - } + /// + /// 所属资源包ID + /// + public int BundleID; + + /// + /// 是否包含Tag + /// + public bool HasTag(string[] tags) + { + if (tags == null || tags.Length == 0) + return false; + if (AssetTags == null || AssetTags.Length == 0) + return false; + + foreach (var tag in tags) + { + if (AssetTags.Contains(tag)) + return true; + } + return false; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PackageBundle.cs b/Assets/YooAsset/Runtime/ResourcePackage/PackageBundle.cs index 5d0e9a6..f390838 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PackageBundle.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PackageBundle.cs @@ -3,150 +3,150 @@ using System.Linq; namespace YooAsset { - [Serializable] - internal class PackageBundle - { - /// - /// 资源包名称 - /// - public string BundleName; + [Serializable] + internal class PackageBundle + { + /// + /// 资源包名称 + /// + public string BundleName; - /// - /// Unity引擎生成的CRC - /// - public uint UnityCRC; + /// + /// Unity引擎生成的CRC + /// + public uint UnityCRC; - /// - /// 文件哈希值 - /// - public string FileHash; + /// + /// 文件哈希值 + /// + public string FileHash; - /// - /// 文件校验码 - /// - public string FileCRC; + /// + /// 文件校验码 + /// + public string FileCRC; - /// - /// 文件大小(字节数) - /// - public long FileSize; + /// + /// 文件大小(字节数) + /// + public long FileSize; - /// - /// 文件是否加密 - /// - public bool Encrypted; - - /// - /// 资源包的分类标签 - /// - public string[] Tags; + /// + /// 文件是否加密 + /// + public bool Encrypted; - /// - /// 依赖的资源包ID集合 - /// - public int[] DependIDs; + /// + /// 资源包的分类标签 + /// + public string[] Tags; + + /// + /// 依赖的资源包ID集合 + /// + public int[] DependIDs; - /// - /// 所属的包裹名称 - /// - public string PackageName { private set; get; } + /// + /// 所属的包裹名称 + /// + public string PackageName { private set; get; } - /// - /// 所属的构建管线 - /// - public string Buildpipeline { private set; get; } + /// + /// 所属的构建管线 + /// + public string Buildpipeline { private set; get; } - /// - /// 缓存GUID - /// - public string CacheGUID - { - get { return FileHash; } - } + /// + /// 缓存GUID + /// + public string CacheGUID + { + get { return FileHash; } + } - /// - /// 文件名称 - /// - private string _fileName; - public string FileName - { - get - { - if (string.IsNullOrEmpty(_fileName)) - throw new Exception("Should never get here !"); - return _fileName; - } - } + /// + /// 文件名称 + /// + private string _fileName; + public string FileName + { + get + { + if (string.IsNullOrEmpty(_fileName)) + throw new Exception("Should never get here !"); + return _fileName; + } + } - /// - /// 文件后缀名 - /// - private string _fileExtension; - public string FileExtension - { - get - { - if (string.IsNullOrEmpty(_fileExtension)) - throw new Exception("Should never get here !"); - return _fileExtension; - } - } + /// + /// 文件后缀名 + /// + private string _fileExtension; + public string FileExtension + { + get + { + if (string.IsNullOrEmpty(_fileExtension)) + throw new Exception("Should never get here !"); + return _fileExtension; + } + } - public PackageBundle() - { - } + public PackageBundle() + { + } - /// - /// 解析资源包 - /// - public void ParseBundle(PackageManifest manifest) - { - PackageName = manifest.PackageName; - Buildpipeline = manifest.BuildPipeline; - _fileExtension = ManifestTools.GetRemoteBundleFileExtension(BundleName); - _fileName = ManifestTools.GetRemoteBundleFileName(manifest.OutputNameStyle, BundleName, _fileExtension, FileHash); - } + /// + /// 解析资源包 + /// + public void ParseBundle(PackageManifest manifest) + { + PackageName = manifest.PackageName; + Buildpipeline = manifest.BuildPipeline; + _fileExtension = ManifestTools.GetRemoteBundleFileExtension(BundleName); + _fileName = ManifestTools.GetRemoteBundleFileName(manifest.OutputNameStyle, BundleName, _fileExtension, FileHash); + } - /// - /// 是否包含Tag - /// - public bool HasTag(string[] tags) - { - if (tags == null || tags.Length == 0) - return false; - if (Tags == null || Tags.Length == 0) - return false; + /// + /// 是否包含Tag + /// + public bool HasTag(string[] tags) + { + if (tags == null || tags.Length == 0) + return false; + if (Tags == null || Tags.Length == 0) + return false; - foreach (var tag in tags) - { - if (Tags.Contains(tag)) - return true; - } - return false; - } + foreach (var tag in tags) + { + if (Tags.Contains(tag)) + return true; + } + return false; + } - /// - /// 是否包含任意Tags - /// - public bool HasAnyTags() - { - if (Tags != null && Tags.Length > 0) - return true; - else - return false; - } + /// + /// 是否包含任意Tags + /// + public bool HasAnyTags() + { + if (Tags != null && Tags.Length > 0) + return true; + else + return false; + } - /// - /// 检测资源包文件内容是否相同 - /// - public bool Equals(PackageBundle otherBundle) - { - if (FileHash == otherBundle.FileHash) - return true; + /// + /// 检测资源包文件内容是否相同 + /// + public bool Equals(PackageBundle otherBundle) + { + if (FileHash == otherBundle.FileHash) + return true; - return false; - } - } + return false; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs b/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs index 46fc396..a9df1c7 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PackageManifest.cs @@ -6,347 +6,347 @@ using System.Collections.Generic; namespace YooAsset { - /// - /// 清单文件 - /// - [Serializable] - internal class PackageManifest - { - /// - /// 文件版本 - /// - public string FileVersion; + /// + /// 清单文件 + /// + [Serializable] + internal class PackageManifest + { + /// + /// 文件版本 + /// + public string FileVersion; - /// - /// 启用可寻址资源定位 - /// - public bool EnableAddressable; + /// + /// 启用可寻址资源定位 + /// + public bool EnableAddressable; - /// - /// 资源定位地址大小写不敏感 - /// - public bool LocationToLower; + /// + /// 资源定位地址大小写不敏感 + /// + public bool LocationToLower; - /// - /// 包含资源GUID数据 - /// - public bool IncludeAssetGUID; + /// + /// 包含资源GUID数据 + /// + public bool IncludeAssetGUID; - /// - /// 文件名称样式 - /// - public int OutputNameStyle; + /// + /// 文件名称样式 + /// + public int OutputNameStyle; - /// - /// 构建管线名称 - /// - public string BuildPipeline; + /// + /// 构建管线名称 + /// + public string BuildPipeline; - /// - /// 资源包裹名称 - /// - public string PackageName; + /// + /// 资源包裹名称 + /// + public string PackageName; - /// - /// 资源包裹的版本信息 - /// - public string PackageVersion; + /// + /// 资源包裹的版本信息 + /// + public string PackageVersion; - /// - /// 资源列表(主动收集的资源列表) - /// - public List AssetList = new List(); + /// + /// 资源列表(主动收集的资源列表) + /// + public List AssetList = new List(); - /// - /// 资源包列表 - /// - public List BundleList = new List(); + /// + /// 资源包列表 + /// + public List BundleList = new List(); - /// - /// 资源包集合(提供BundleName获取PackageBundle) - /// - [NonSerialized] - public Dictionary BundleDic1; + /// + /// 资源包集合(提供BundleName获取PackageBundle) + /// + [NonSerialized] + public Dictionary BundleDic1; - /// - /// 资源包集合(提供FileName获取PackageBundle) - /// - [NonSerialized] - public Dictionary BundleDic2; + /// + /// 资源包集合(提供FileName获取PackageBundle) + /// + [NonSerialized] + public Dictionary BundleDic2; - /// - /// 资源映射集合(提供AssetPath获取PackageAsset) - /// - [NonSerialized] - public Dictionary AssetDic; + /// + /// 资源映射集合(提供AssetPath获取PackageAsset) + /// + [NonSerialized] + public Dictionary AssetDic; - /// - /// 资源路径映射集合(提供Location获取AssetPath) - /// - [NonSerialized] - public Dictionary AssetPathMapping1; + /// + /// 资源路径映射集合(提供Location获取AssetPath) + /// + [NonSerialized] + public Dictionary AssetPathMapping1; - /// - /// 资源路径映射集合(提供AssetGUID获取AssetPath) - /// - [NonSerialized] - public Dictionary AssetPathMapping2; + /// + /// 资源路径映射集合(提供AssetGUID获取AssetPath) + /// + [NonSerialized] + public Dictionary AssetPathMapping2; - /// - /// 该资源清单所有文件的缓存GUID集合 - /// - [NonSerialized] - public HashSet CacheGUIDs = new HashSet(); + /// + /// 该资源清单所有文件的缓存GUID集合 + /// + [NonSerialized] + public HashSet CacheGUIDs = new HashSet(); - /// - /// 尝试映射为资源路径 - /// - public string TryMappingToAssetPath(string location) - { - if (string.IsNullOrEmpty(location)) - return string.Empty; + /// + /// 尝试映射为资源路径 + /// + public string TryMappingToAssetPath(string location) + { + if (string.IsNullOrEmpty(location)) + return string.Empty; - if (LocationToLower) - location = location.ToLower(); + if (LocationToLower) + location = location.ToLower(); - if (AssetPathMapping1.TryGetValue(location, out string assetPath)) - return assetPath; - else - return string.Empty; - } + if (AssetPathMapping1.TryGetValue(location, out string assetPath)) + return assetPath; + else + return string.Empty; + } - /// - /// 获取主资源包 - /// 注意:传入的资源路径一定合法有效! - /// - public PackageBundle GetMainPackageBundle(string assetPath) - { - if (AssetDic.TryGetValue(assetPath, out PackageAsset packageAsset)) - { - int bundleID = packageAsset.BundleID; - if (bundleID >= 0 && bundleID < BundleList.Count) - { - var packageBundle = BundleList[bundleID]; - return packageBundle; - } - else - { - throw new Exception($"Invalid bundle id : {bundleID} Asset path : {assetPath}"); - } - } - else - { - throw new Exception("Should never get here !"); - } - } + /// + /// 获取主资源包 + /// 注意:传入的资源路径一定合法有效! + /// + public PackageBundle GetMainPackageBundle(string assetPath) + { + if (AssetDic.TryGetValue(assetPath, out PackageAsset packageAsset)) + { + int bundleID = packageAsset.BundleID; + if (bundleID >= 0 && bundleID < BundleList.Count) + { + var packageBundle = BundleList[bundleID]; + return packageBundle; + } + else + { + throw new Exception($"Invalid bundle id : {bundleID} Asset path : {assetPath}"); + } + } + else + { + throw new Exception("Should never get here !"); + } + } - /// - /// 获取资源依赖列表 - /// 注意:传入的资源路径一定合法有效! - /// - public PackageBundle[] GetAllDependencies(string assetPath) - { - var packageBundle = GetMainPackageBundle(assetPath); - List result = new List(packageBundle.DependIDs.Length); - foreach (var dependID in packageBundle.DependIDs) - { - if (dependID >= 0 && dependID < BundleList.Count) - { - var dependBundle = BundleList[dependID]; - result.Add(dependBundle); - } - else - { - throw new Exception($"Invalid bundle id : {dependID} Asset path : {assetPath}"); - } - } - return result.ToArray(); - } + /// + /// 获取资源依赖列表 + /// 注意:传入的资源路径一定合法有效! + /// + public PackageBundle[] GetAllDependencies(string assetPath) + { + var packageBundle = GetMainPackageBundle(assetPath); + List result = new List(packageBundle.DependIDs.Length); + foreach (var dependID in packageBundle.DependIDs) + { + if (dependID >= 0 && dependID < BundleList.Count) + { + var dependBundle = BundleList[dependID]; + result.Add(dependBundle); + } + else + { + throw new Exception($"Invalid bundle id : {dependID} Asset path : {assetPath}"); + } + } + return result.ToArray(); + } - /// - /// 尝试获取包裹的资源 - /// - public bool TryGetPackageAsset(string assetPath, out PackageAsset result) - { - return AssetDic.TryGetValue(assetPath, out result); - } + /// + /// 尝试获取包裹的资源 + /// + public bool TryGetPackageAsset(string assetPath, out PackageAsset result) + { + return AssetDic.TryGetValue(assetPath, out result); + } - /// - /// 尝试获取包裹的资源包 - /// - public bool TryGetPackageBundleByBundleName(string bundleName, out PackageBundle result) - { - return BundleDic1.TryGetValue(bundleName, out result); - } + /// + /// 尝试获取包裹的资源包 + /// + public bool TryGetPackageBundleByBundleName(string bundleName, out PackageBundle result) + { + return BundleDic1.TryGetValue(bundleName, out result); + } - /// - /// 尝试获取包裹的资源包 - /// - public bool TryGetPackageBundleByFileName(string fileName, out PackageBundle result) - { - return BundleDic2.TryGetValue(fileName, out result); - } + /// + /// 尝试获取包裹的资源包 + /// + public bool TryGetPackageBundleByFileName(string fileName, out PackageBundle result) + { + return BundleDic2.TryGetValue(fileName, out result); + } - /// - /// 是否包含资源文件 - /// - public bool IsIncludeBundleFile(string cacheGUID) - { - return CacheGUIDs.Contains(cacheGUID); - } + /// + /// 是否包含资源文件 + /// + public bool IsIncludeBundleFile(string cacheGUID) + { + return CacheGUIDs.Contains(cacheGUID); + } - /// - /// 获取资源信息列表 - /// - public AssetInfo[] GetAssetsInfoByTags(string[] tags) - { - List result = new List(100); - foreach (var packageAsset in AssetList) - { - if (packageAsset.HasTag(tags)) - { - AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, null); - result.Add(assetInfo); - } - } - return result.ToArray(); - } + /// + /// 获取资源信息列表 + /// + public AssetInfo[] GetAssetsInfoByTags(string[] tags) + { + List result = new List(100); + foreach (var packageAsset in AssetList) + { + if (packageAsset.HasTag(tags)) + { + AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, null); + result.Add(assetInfo); + } + } + return result.ToArray(); + } - /// - /// 资源定位地址转换为资源信息。 - /// - /// 如果转换失败会返回一个无效的资源信息类 - public AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) - { - DebugCheckLocation(location); + /// + /// 资源定位地址转换为资源信息。 + /// + /// 如果转换失败会返回一个无效的资源信息类 + public AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) + { + DebugCheckLocation(location); - string assetPath = ConvertLocationToAssetInfoMapping(location); - if (TryGetPackageAsset(assetPath, out PackageAsset packageAsset)) - { - AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, assetType); - return assetInfo; - } - else - { - string error; - if (string.IsNullOrEmpty(location)) - error = $"The location is null or empty !"; - else - error = $"The location is invalid : {location}"; - AssetInfo assetInfo = new AssetInfo(PackageName, error); - return assetInfo; - } - } - private string ConvertLocationToAssetInfoMapping(string location) - { - if (string.IsNullOrEmpty(location)) - { - YooLogger.Error("Failed to mapping location to asset path, The location is null or empty."); - return string.Empty; - } + string assetPath = ConvertLocationToAssetInfoMapping(location); + if (TryGetPackageAsset(assetPath, out PackageAsset packageAsset)) + { + AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, assetType); + return assetInfo; + } + else + { + string error; + if (string.IsNullOrEmpty(location)) + error = $"The location is null or empty !"; + else + error = $"The location is invalid : {location}"; + AssetInfo assetInfo = new AssetInfo(PackageName, error); + return assetInfo; + } + } + private string ConvertLocationToAssetInfoMapping(string location) + { + if (string.IsNullOrEmpty(location)) + { + YooLogger.Error("Failed to mapping location to asset path, The location is null or empty."); + return string.Empty; + } - if (LocationToLower) - location = location.ToLower(); + if (LocationToLower) + location = location.ToLower(); - if (AssetPathMapping1.TryGetValue(location, out string assetPath)) - { - return assetPath; - } - else - { - YooLogger.Warning($"Failed to mapping location to asset path : {location}"); - return string.Empty; - } - } + if (AssetPathMapping1.TryGetValue(location, out string assetPath)) + { + return assetPath; + } + else + { + YooLogger.Warning($"Failed to mapping location to asset path : {location}"); + return string.Empty; + } + } - /// - /// 资源GUID转换为资源信息。 - /// - /// 如果转换失败会返回一个无效的资源信息类 - public AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType) - { - if (IncludeAssetGUID == false) - { - YooLogger.Warning("Package manifest not include asset guid ! Please check asset bundle collector settings."); - AssetInfo assetInfo = new AssetInfo(PackageName, "AssetGUID data is empty !"); - return assetInfo; - } + /// + /// 资源GUID转换为资源信息。 + /// + /// 如果转换失败会返回一个无效的资源信息类 + public AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType) + { + if (IncludeAssetGUID == false) + { + YooLogger.Warning("Package manifest not include asset guid ! Please check asset bundle collector settings."); + AssetInfo assetInfo = new AssetInfo(PackageName, "AssetGUID data is empty !"); + return assetInfo; + } - string assetPath = ConvertAssetGUIDToAssetInfoMapping(assetGUID); - if (TryGetPackageAsset(assetPath, out PackageAsset packageAsset)) - { - AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, assetType); - return assetInfo; - } - else - { - string error; - if (string.IsNullOrEmpty(assetGUID)) - error = $"The assetGUID is null or empty !"; - else - error = $"The assetGUID is invalid : {assetGUID}"; - AssetInfo assetInfo = new AssetInfo(PackageName, error); - return assetInfo; - } - } - private string ConvertAssetGUIDToAssetInfoMapping(string assetGUID) - { - if (string.IsNullOrEmpty(assetGUID)) - { - YooLogger.Error("Failed to mapping assetGUID to asset path, The assetGUID is null or empty."); - return string.Empty; - } + string assetPath = ConvertAssetGUIDToAssetInfoMapping(assetGUID); + if (TryGetPackageAsset(assetPath, out PackageAsset packageAsset)) + { + AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, assetType); + return assetInfo; + } + else + { + string error; + if (string.IsNullOrEmpty(assetGUID)) + error = $"The assetGUID is null or empty !"; + else + error = $"The assetGUID is invalid : {assetGUID}"; + AssetInfo assetInfo = new AssetInfo(PackageName, error); + return assetInfo; + } + } + private string ConvertAssetGUIDToAssetInfoMapping(string assetGUID) + { + if (string.IsNullOrEmpty(assetGUID)) + { + YooLogger.Error("Failed to mapping assetGUID to asset path, The assetGUID is null or empty."); + return string.Empty; + } - if (AssetPathMapping2.TryGetValue(assetGUID, out string assetPath)) - { - return assetPath; - } - else - { - YooLogger.Warning($"Failed to mapping assetGUID to asset path : {assetGUID}"); - return string.Empty; - } - } + if (AssetPathMapping2.TryGetValue(assetGUID, out string assetPath)) + { + return assetPath; + } + else + { + YooLogger.Warning($"Failed to mapping assetGUID to asset path : {assetGUID}"); + return string.Empty; + } + } - /// - /// 获取资源包内的主资源列表 - /// - public string[] GetBundleIncludeAssets(string assetPath) - { - List assetList = new List(); - if (TryGetPackageAsset(assetPath, out PackageAsset result)) - { - foreach (var packageAsset in AssetList) - { - if (packageAsset.BundleID == result.BundleID) - { - assetList.Add(packageAsset.AssetPath); - } - } - } - return assetList.ToArray(); - } + /// + /// 获取资源包内的主资源列表 + /// + public string[] GetBundleIncludeAssets(string assetPath) + { + List assetList = new List(); + if (TryGetPackageAsset(assetPath, out PackageAsset result)) + { + foreach (var packageAsset in AssetList) + { + if (packageAsset.BundleID == result.BundleID) + { + assetList.Add(packageAsset.AssetPath); + } + } + } + return assetList.ToArray(); + } - #region 调试方法 - [Conditional("DEBUG")] - private void DebugCheckLocation(string location) - { - if (string.IsNullOrEmpty(location) == false) - { - // 检查路径末尾是否有空格 - int index = location.LastIndexOf(" "); - if (index != -1) - { - if (location.Length == index + 1) - YooLogger.Warning($"Found blank character in location : \"{location}\""); - } + #region 调试方法 + [Conditional("DEBUG")] + private void DebugCheckLocation(string location) + { + if (string.IsNullOrEmpty(location) == false) + { + // 检查路径末尾是否有空格 + int index = location.LastIndexOf(" "); + if (index != -1) + { + if (location.Length == index + 1) + YooLogger.Warning($"Found blank character in location : \"{location}\""); + } - if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0) - YooLogger.Warning($"Found illegal character in location : \"{location}\""); - } - } - #endregion - } + if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0) + YooLogger.Warning($"Found illegal character in location : \"{location}\""); + } + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeHelper.cs b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeHelper.cs index bc3bf6b..bff9625 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeHelper.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeHelper.cs @@ -3,41 +3,41 @@ using System.Reflection; namespace YooAsset { - public static class EditorSimulateModeHelper - { - private static System.Type _classType; + public static class EditorSimulateModeHelper + { + private static System.Type _classType; - /// - /// 编辑器下模拟构建清单 - /// - public static string SimulateBuild(string buildPipelineName, string packageName) - { - if (_classType == null) - _classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder"); + /// + /// 编辑器下模拟构建清单 + /// + public static string SimulateBuild(string buildPipelineName, string packageName) + { + if (_classType == null) + _classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder"); - string manifestFilePath = (string)InvokePublicStaticMethod(_classType, "SimulateBuild", buildPipelineName, packageName); - return manifestFilePath; - } + string manifestFilePath = (string)InvokePublicStaticMethod(_classType, "SimulateBuild", buildPipelineName, packageName); + return manifestFilePath; + } - /// - /// 编辑器下模拟构建清单 - /// - public static string SimulateBuild(EDefaultBuildPipeline buildPipeline, string packageName) - { - return SimulateBuild(buildPipeline.ToString(), packageName); - } + /// + /// 编辑器下模拟构建清单 + /// + public static string SimulateBuild(EDefaultBuildPipeline buildPipeline, string packageName) + { + return SimulateBuild(buildPipeline.ToString(), packageName); + } - private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters) - { - var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static); - if (methodInfo == null) - { - UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}"); - return null; - } - return methodInfo.Invoke(null, parameters); - } - } + private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters) + { + var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static); + if (methodInfo == null) + { + UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}"); + return null; + } + return methodInfo.Invoke(null, parameters); + } + } } #else namespace YooAsset diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeImpl.cs index 85ca63a..159842e 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/EditorSimulateModeImpl.cs @@ -4,158 +4,158 @@ using System.Collections.Generic; namespace YooAsset { - internal class EditorSimulateModeImpl : IPlayMode, IBundleQuery - { - private PackageManifest _activeManifest; - private ResourceAssist _assist; + internal class EditorSimulateModeImpl : IPlayMode, IBundleQuery + { + private PackageManifest _activeManifest; + private ResourceAssist _assist; - public readonly string PackageName; - public DownloadManager Download - { - get { return _assist.Download; } - } + public readonly string PackageName; + public DownloadManager Download + { + get { return _assist.Download; } + } - public EditorSimulateModeImpl(string packageName) - { - PackageName = packageName; - } + public EditorSimulateModeImpl(string packageName) + { + PackageName = packageName; + } - /// - /// 异步初始化 - /// - public InitializationOperation InitializeAsync(ResourceAssist assist, string simulateManifestFilePath) - { - _assist = assist; + /// + /// 异步初始化 + /// + public InitializationOperation InitializeAsync(ResourceAssist assist, string simulateManifestFilePath) + { + _assist = assist; - var operation = new EditorSimulateModeInitializationOperation(this, simulateManifestFilePath); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + var operation = new EditorSimulateModeInitializationOperation(this, simulateManifestFilePath); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - #region IPlayMode接口 - public PackageManifest ActiveManifest - { - set - { - _activeManifest = value; - } - get - { - return _activeManifest; - } - } - public void FlushManifestVersionFile() - { - } + #region IPlayMode接口 + public PackageManifest ActiveManifest + { + set + { + _activeManifest = value; + } + get + { + return _activeManifest; + } + } + public void FlushManifestVersionFile() + { + } - UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) - { - var operation = new EditorPlayModeUpdatePackageVersionOperation(); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) - { - var operation = new EditorPlayModeUpdatePackageManifestOperation(); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) - { - var operation = new EditorPlayModePreDownloadContentOperation(this); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) + { + var operation = new EditorPlayModeUpdatePackageVersionOperation(); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) + { + var operation = new EditorPlayModeUpdatePackageManifestOperation(); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) + { + var operation = new EditorPlayModePreDownloadContentOperation(this); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); - } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); - } + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); + } + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); + } - ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) - { - return ResourceImporterOperation.CreateEmptyImporter(Download, PackageName, importerMaxNumber, failedTryAgain, timeout); - } - #endregion + ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) + { + return ResourceImporterOperation.CreateEmptyImporter(Download, PackageName, importerMaxNumber, failedTryAgain, timeout); + } + #endregion - #region IBundleQuery接口 - private BundleInfo CreateBundleInfo(PackageBundle packageBundle, AssetInfo assetInfo) - { - if (packageBundle == null) - throw new Exception("Should never get here !"); + #region IBundleQuery接口 + private BundleInfo CreateBundleInfo(PackageBundle packageBundle, AssetInfo assetInfo) + { + if (packageBundle == null) + throw new Exception("Should never get here !"); - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromEditor); - bundleInfo.IncludeAssetsInEditor = _activeManifest.GetBundleIncludeAssets(assetInfo.AssetPath); - return bundleInfo; - } - BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromEditor); + bundleInfo.IncludeAssetsInEditor = _activeManifest.GetBundleIncludeAssets(assetInfo.AssetPath); + return bundleInfo; + } + BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return CreateBundleInfo(packageBundle, assetInfo); - } - BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return CreateBundleInfo(packageBundle, assetInfo); + } + BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - BundleInfo bundleInfo = CreateBundleInfo(packageBundle, assetInfo); - result.Add(bundleInfo); - } - return result.ToArray(); - } - string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + BundleInfo bundleInfo = CreateBundleInfo(packageBundle, assetInfo); + result.Add(bundleInfo); + } + return result.ToArray(); + } + string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return packageBundle.BundleName; - } - string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return packageBundle.BundleName; + } + string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - result.Add(packageBundle.BundleName); - } - return result.ToArray(); - } - bool IBundleQuery.ManifestValid() - { - return _activeManifest != null; - } - #endregion - } + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + result.Add(packageBundle.BundleName); + } + return result.ToArray(); + } + bool IBundleQuery.ManifestValid() + { + return _activeManifest != null; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/HostPlayModeImpl.cs index b5bf902..601f61e 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/HostPlayModeImpl.cs @@ -4,414 +4,414 @@ using System.Collections.Generic; namespace YooAsset { - internal class HostPlayModeImpl : IPlayMode, IBundleQuery - { - private PackageManifest _activeManifest; - private ResourceAssist _assist; - private IBuildinQueryServices _buildinQueryServices; - private IDeliveryQueryServices _deliveryQueryServices; - private IRemoteServices _remoteServices; - - public readonly string PackageName; - public DownloadManager Download - { - get { return _assist.Download; } - } - public PersistentManager Persistent - { - get { return _assist.Persistent; } - } - public CacheManager Cache - { - get { return _assist.Cache; } - } - public IRemoteServices RemoteServices - { - get { return _remoteServices; } - } + internal class HostPlayModeImpl : IPlayMode, IBundleQuery + { + private PackageManifest _activeManifest; + private ResourceAssist _assist; + private IBuildinQueryServices _buildinQueryServices; + private IDeliveryQueryServices _deliveryQueryServices; + private IRemoteServices _remoteServices; + + public readonly string PackageName; + public DownloadManager Download + { + get { return _assist.Download; } + } + public PersistentManager Persistent + { + get { return _assist.Persistent; } + } + public CacheManager Cache + { + get { return _assist.Cache; } + } + public IRemoteServices RemoteServices + { + get { return _remoteServices; } + } - public HostPlayModeImpl(string packageName) - { - PackageName = packageName; - } + public HostPlayModeImpl(string packageName) + { + PackageName = packageName; + } - /// - /// 异步初始化 - /// - public InitializationOperation InitializeAsync(ResourceAssist assist, IBuildinQueryServices buildinQueryServices, IDeliveryQueryServices deliveryQueryServices, IRemoteServices remoteServices) - { - _assist = assist; - _buildinQueryServices = buildinQueryServices; - _deliveryQueryServices = deliveryQueryServices; - _remoteServices = remoteServices; + /// + /// 异步初始化 + /// + public InitializationOperation InitializeAsync(ResourceAssist assist, IBuildinQueryServices buildinQueryServices, IDeliveryQueryServices deliveryQueryServices, IRemoteServices remoteServices) + { + _assist = assist; + _buildinQueryServices = buildinQueryServices; + _deliveryQueryServices = deliveryQueryServices; + _remoteServices = remoteServices; - var operation = new HostPlayModeInitializationOperation(this); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + var operation = new HostPlayModeInitializationOperation(this); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - // 下载相关 - private List ConvertToDownloadList(List downloadList) - { - List result = new List(downloadList.Count); - foreach (var packageBundle in downloadList) - { - var bundleInfo = ConvertToDownloadInfo(packageBundle); - result.Add(bundleInfo); - } - return result; - } - private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle) - { - string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName); - string remoteFallbackURL = _remoteServices.GetRemoteFallbackURL(packageBundle.FileName); - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL); - return bundleInfo; - } + // 下载相关 + private List ConvertToDownloadList(List downloadList) + { + List result = new List(downloadList.Count); + foreach (var packageBundle in downloadList) + { + var bundleInfo = ConvertToDownloadInfo(packageBundle); + result.Add(bundleInfo); + } + return result; + } + private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle) + { + string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName); + string remoteFallbackURL = _remoteServices.GetRemoteFallbackURL(packageBundle.FileName); + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL); + return bundleInfo; + } - // 查询相关 - private bool IsDeliveryPackageBundle(PackageBundle packageBundle) - { - if (_deliveryQueryServices == null) - return false; - return _deliveryQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); - } - private bool IsCachedPackageBundle(PackageBundle packageBundle) - { - return _assist.Cache.IsCached(packageBundle.CacheGUID); - } - private bool IsBuildinPackageBundle(PackageBundle packageBundle) - { - return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); - } + // 查询相关 + private bool IsDeliveryPackageBundle(PackageBundle packageBundle) + { + if (_deliveryQueryServices == null) + return false; + return _deliveryQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); + } + private bool IsCachedPackageBundle(PackageBundle packageBundle) + { + return _assist.Cache.IsCached(packageBundle.CacheGUID); + } + private bool IsBuildinPackageBundle(PackageBundle packageBundle) + { + return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); + } - #region IPlayMode接口 - public PackageManifest ActiveManifest - { - set - { - _activeManifest = value; - } - get - { - return _activeManifest; - } - } - public void FlushManifestVersionFile() - { - if (_activeManifest != null) - { - _assist.Persistent.SaveSandboxPackageVersionFile(_activeManifest.PackageVersion); - } - } + #region IPlayMode接口 + public PackageManifest ActiveManifest + { + set + { + _activeManifest = value; + } + get + { + return _activeManifest; + } + } + public void FlushManifestVersionFile() + { + if (_activeManifest != null) + { + _assist.Persistent.SaveSandboxPackageVersionFile(_activeManifest.PackageVersion); + } + } - UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) - { - var operation = new HostPlayModeUpdatePackageVersionOperation(this, appendTimeTicks, timeout); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) - { - var operation = new HostPlayModeUpdatePackageManifestOperation(this, packageVersion, autoSaveVersion, timeout); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) - { - var operation = new HostPlayModePreDownloadContentOperation(this, packageVersion, timeout); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) + { + var operation = new HostPlayModeUpdatePackageVersionOperation(this, appendTimeTicks, timeout); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) + { + var operation = new HostPlayModeUpdatePackageManifestOperation(this, packageVersion, autoSaveVersion, timeout); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) + { + var operation = new HostPlayModePreDownloadContentOperation(this, packageVersion, timeout); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = GetDownloadListByAll(_activeManifest); - var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public List GetDownloadListByAll(PackageManifest manifest) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略分发文件 - if (IsDeliveryPackageBundle(packageBundle)) - continue; + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = GetDownloadListByAll(_activeManifest); + var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByAll(PackageManifest manifest) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略分发文件 + if (IsDeliveryPackageBundle(packageBundle)) + continue; - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - // 忽略APP资源 - if (IsBuildinPackageBundle(packageBundle)) - continue; + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; - downloadList.Add(packageBundle); - } + downloadList.Add(packageBundle); + } - return ConvertToDownloadList(downloadList); - } + return ConvertToDownloadList(downloadList); + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = GetDownloadListByTags(_activeManifest, tags); - var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public List GetDownloadListByTags(PackageManifest manifest, string[] tags) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略分发文件 - if (IsDeliveryPackageBundle(packageBundle)) - continue; + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = GetDownloadListByTags(_activeManifest, tags); + var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByTags(PackageManifest manifest, string[] tags) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略分发文件 + if (IsDeliveryPackageBundle(packageBundle)) + continue; - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - // 忽略APP资源 - if (IsBuildinPackageBundle(packageBundle)) - continue; + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; - // 如果未带任何标记,则统一下载 - if (packageBundle.HasAnyTags() == false) - { - downloadList.Add(packageBundle); - } - else - { - // 查询DLC资源 - if (packageBundle.HasTag(tags)) - { - downloadList.Add(packageBundle); - } - } - } + // 如果未带任何标记,则统一下载 + if (packageBundle.HasAnyTags() == false) + { + downloadList.Add(packageBundle); + } + else + { + // 查询DLC资源 + if (packageBundle.HasTag(tags)) + { + downloadList.Add(packageBundle); + } + } + } - return ConvertToDownloadList(downloadList); - } + return ConvertToDownloadList(downloadList); + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = GetDownloadListByPaths(_activeManifest, assetInfos); - var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public List GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos) - { - // 获取资源对象的资源包和所有依赖资源包 - List checkList = new List(); - foreach (var assetInfo in assetInfos) - { - if (assetInfo.IsInvalid) - { - YooLogger.Warning(assetInfo.Error); - continue; - } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = GetDownloadListByPaths(_activeManifest, assetInfos); + var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos) + { + // 获取资源对象的资源包和所有依赖资源包 + List checkList = new List(); + foreach (var assetInfo in assetInfos) + { + if (assetInfo.IsInvalid) + { + YooLogger.Warning(assetInfo.Error); + continue; + } - // 注意:如果清单里未找到资源包会抛出异常! - PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath); - if (checkList.Contains(mainBundle) == false) - checkList.Add(mainBundle); + // 注意:如果清单里未找到资源包会抛出异常! + PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath); + if (checkList.Contains(mainBundle) == false) + checkList.Add(mainBundle); - // 注意:如果清单里未找到资源包会抛出异常! - PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath); - foreach (var dependBundle in dependBundles) - { - if (checkList.Contains(dependBundle) == false) - checkList.Add(dependBundle); - } - } + // 注意:如果清单里未找到资源包会抛出异常! + PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath); + foreach (var dependBundle in dependBundles) + { + if (checkList.Contains(dependBundle) == false) + checkList.Add(dependBundle); + } + } - List downloadList = new List(1000); - foreach (var packageBundle in checkList) - { - // 忽略分发文件 - if (IsDeliveryPackageBundle(packageBundle)) - continue; + List downloadList = new List(1000); + foreach (var packageBundle in checkList) + { + // 忽略分发文件 + if (IsDeliveryPackageBundle(packageBundle)) + continue; - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - // 忽略APP资源 - if (IsBuildinPackageBundle(packageBundle)) - continue; + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; - downloadList.Add(packageBundle); - } + downloadList.Add(packageBundle); + } - return ConvertToDownloadList(downloadList); - } + return ConvertToDownloadList(downloadList); + } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) - { - List unpcakList = GetUnpackListByAll(_activeManifest); - var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); - return operation; - } - private List GetUnpackListByAll(PackageManifest manifest) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) + { + List unpcakList = GetUnpackListByAll(_activeManifest); + var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); + return operation; + } + private List GetUnpackListByAll(PackageManifest manifest) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - if (IsBuildinPackageBundle(packageBundle)) - { - downloadList.Add(packageBundle); - } - } + if (IsBuildinPackageBundle(packageBundle)) + { + downloadList.Add(packageBundle); + } + } - return BundleInfo.CreateUnpackInfos(_assist, downloadList); - } + return BundleInfo.CreateUnpackInfos(_assist, downloadList); + } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) - { - List unpcakList = GetUnpackListByTags(_activeManifest, tags); - var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); - return operation; - } - private List GetUnpackListByTags(PackageManifest manifest, string[] tags) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) + { + List unpcakList = GetUnpackListByTags(_activeManifest, tags); + var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); + return operation; + } + private List GetUnpackListByTags(PackageManifest manifest, string[] tags) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - // 查询DLC资源 - if (IsBuildinPackageBundle(packageBundle)) - { - if (packageBundle.HasTag(tags)) - { - downloadList.Add(packageBundle); - } - } - } + // 查询DLC资源 + if (IsBuildinPackageBundle(packageBundle)) + { + if (packageBundle.HasTag(tags)) + { + downloadList.Add(packageBundle); + } + } + } - return BundleInfo.CreateUnpackInfos(_assist, downloadList); - } + return BundleInfo.CreateUnpackInfos(_assist, downloadList); + } - ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) - { - List importerList = GetImporterListByFilePaths(_activeManifest, filePaths); - var operation = new ResourceImporterOperation(Download, PackageName, importerList, importerMaxNumber, failedTryAgain, timeout); - return operation; - } - private List GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths) - { - List result = new List(); - foreach (var filePath in filePaths) - { - string fileName = System.IO.Path.GetFileName(filePath); - if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle)) - { - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) + { + List importerList = GetImporterListByFilePaths(_activeManifest, filePaths); + var operation = new ResourceImporterOperation(Download, PackageName, importerList, importerMaxNumber, failedTryAgain, timeout); + return operation; + } + private List GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths) + { + List result = new List(); + foreach (var filePath in filePaths) + { + string fileName = System.IO.Path.GetFileName(filePath); + if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle)) + { + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath); - result.Add(bundleInfo); - } - else - { - YooLogger.Warning($"Not found package bundle, importer file path : {filePath}"); - } - } - return result; - } - #endregion + var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath); + result.Add(bundleInfo); + } + else + { + YooLogger.Warning($"Not found package bundle, importer file path : {filePath}"); + } + } + return result; + } + #endregion - #region IBundleQuery接口 - private BundleInfo CreateBundleInfo(PackageBundle packageBundle) - { - if (packageBundle == null) - throw new Exception("Should never get here !"); + #region IBundleQuery接口 + private BundleInfo CreateBundleInfo(PackageBundle packageBundle) + { + if (packageBundle == null) + throw new Exception("Should never get here !"); - // 查询分发资源 - if (IsDeliveryPackageBundle(packageBundle)) - { - string deliveryFilePath = _deliveryQueryServices.GetFilePath(PackageName, packageBundle.FileName); - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromDelivery, deliveryFilePath); - return bundleInfo; - } + // 查询分发资源 + if (IsDeliveryPackageBundle(packageBundle)) + { + string deliveryFilePath = _deliveryQueryServices.GetFilePath(PackageName, packageBundle.FileName); + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromDelivery, deliveryFilePath); + return bundleInfo; + } - // 查询沙盒资源 - if (IsCachedPackageBundle(packageBundle)) - { - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromCache); - return bundleInfo; - } + // 查询沙盒资源 + if (IsCachedPackageBundle(packageBundle)) + { + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromCache); + return bundleInfo; + } - // 查询APP资源 - if (IsBuildinPackageBundle(packageBundle)) - { - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming); - return bundleInfo; - } + // 查询APP资源 + if (IsBuildinPackageBundle(packageBundle)) + { + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming); + return bundleInfo; + } - // 从服务端下载 - return ConvertToDownloadInfo(packageBundle); - } - BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 从服务端下载 + return ConvertToDownloadInfo(packageBundle); + } + BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return CreateBundleInfo(packageBundle); - } - BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return CreateBundleInfo(packageBundle); + } + BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - BundleInfo bundleInfo = CreateBundleInfo(packageBundle); - result.Add(bundleInfo); - } - return result.ToArray(); - } - string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + BundleInfo bundleInfo = CreateBundleInfo(packageBundle); + result.Add(bundleInfo); + } + return result.ToArray(); + } + string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return packageBundle.BundleName; - } - string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return packageBundle.BundleName; + } + string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - result.Add(packageBundle.BundleName); - } - return result.ToArray(); - } - bool IBundleQuery.ManifestValid() - { - return _activeManifest != null; - } - #endregion - } + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + result.Add(packageBundle.BundleName); + } + return result.ToArray(); + } + bool IBundleQuery.ManifestValid() + { + return _activeManifest != null; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/OfflinePlayModeImpl.cs index bbb1166..c6d7cc9 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/OfflinePlayModeImpl.cs @@ -4,243 +4,243 @@ using System.Collections.Generic; namespace YooAsset { - internal class OfflinePlayModeImpl : IPlayMode, IBundleQuery - { - private PackageManifest _activeManifest; - private ResourceAssist _assist; + internal class OfflinePlayModeImpl : IPlayMode, IBundleQuery + { + private PackageManifest _activeManifest; + private ResourceAssist _assist; - public readonly string PackageName; - public DownloadManager Download - { - get { return _assist.Download; } - } - public PersistentManager Persistent - { - get { return _assist.Persistent; } - } - public CacheManager Cache - { - get { return _assist.Cache; } - } + public readonly string PackageName; + public DownloadManager Download + { + get { return _assist.Download; } + } + public PersistentManager Persistent + { + get { return _assist.Persistent; } + } + public CacheManager Cache + { + get { return _assist.Cache; } + } - public OfflinePlayModeImpl(string packageName) - { - PackageName = packageName; - } + public OfflinePlayModeImpl(string packageName) + { + PackageName = packageName; + } - /// - /// 异步初始化 - /// - public InitializationOperation InitializeAsync(ResourceAssist assist) - { - _assist = assist; + /// + /// 异步初始化 + /// + public InitializationOperation InitializeAsync(ResourceAssist assist) + { + _assist = assist; - var operation = new OfflinePlayModeInitializationOperation(this); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + var operation = new OfflinePlayModeInitializationOperation(this); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - // 查询相关 - private bool IsCachedPackageBundle(PackageBundle packageBundle) - { - return _assist.Cache.IsCached(packageBundle.CacheGUID); - } + // 查询相关 + private bool IsCachedPackageBundle(PackageBundle packageBundle) + { + return _assist.Cache.IsCached(packageBundle.CacheGUID); + } - #region IPlayMode接口 - public PackageManifest ActiveManifest - { - set - { - _activeManifest = value; - } - get - { - return _activeManifest; - } - } - public void FlushManifestVersionFile() - { - } + #region IPlayMode接口 + public PackageManifest ActiveManifest + { + set + { + _activeManifest = value; + } + get + { + return _activeManifest; + } + } + public void FlushManifestVersionFile() + { + } - UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) - { - var operation = new OfflinePlayModeUpdatePackageVersionOperation(); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) - { - var operation = new OfflinePlayModeUpdatePackageManifestOperation(); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) - { - var operation = new OfflinePlayModePreDownloadContentOperation(this); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) + { + var operation = new OfflinePlayModeUpdatePackageVersionOperation(); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) + { + var operation = new OfflinePlayModeUpdatePackageManifestOperation(); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) + { + var operation = new OfflinePlayModePreDownloadContentOperation(this); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); - } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceDownloaderOperation.CreateEmptyDownloader(Download, PackageName, downloadingMaxNumber, failedTryAgain, timeout); + } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) - { - List unpcakList = GetUnpackListByAll(_activeManifest); - var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); - return operation; - } - private List GetUnpackListByAll(PackageManifest manifest) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) + { + List unpcakList = GetUnpackListByAll(_activeManifest); + var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); + return operation; + } + private List GetUnpackListByAll(PackageManifest manifest) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - downloadList.Add(packageBundle); - } + downloadList.Add(packageBundle); + } - return BundleInfo.CreateUnpackInfos(_assist, downloadList); - } + return BundleInfo.CreateUnpackInfos(_assist, downloadList); + } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) - { - List unpcakList = GetUnpackListByTags(_activeManifest, tags); - var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); - return operation; - } - private List GetUnpackListByTags(PackageManifest manifest, string[] tags) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) + { + List unpcakList = GetUnpackListByTags(_activeManifest, tags); + var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout); + return operation; + } + private List GetUnpackListByTags(PackageManifest manifest, string[] tags) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - // 查询DLC资源 - if (packageBundle.HasTag(tags)) - { - downloadList.Add(packageBundle); - } - } + // 查询DLC资源 + if (packageBundle.HasTag(tags)) + { + downloadList.Add(packageBundle); + } + } - return BundleInfo.CreateUnpackInfos(_assist, downloadList); - } + return BundleInfo.CreateUnpackInfos(_assist, downloadList); + } - ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) - { - List importerList = GetImporterListByFilePaths(_activeManifest, filePaths); - var operation = new ResourceImporterOperation(Download, PackageName, importerList, importerMaxNumber, failedTryAgain, timeout); - return operation; - } - private List GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths) - { - List result = new List(); - foreach (var filePath in filePaths) - { - string fileName = System.IO.Path.GetFileName(filePath); - if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle)) - { - // 忽略缓存文件 - if (IsCachedPackageBundle(packageBundle)) - continue; + ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) + { + List importerList = GetImporterListByFilePaths(_activeManifest, filePaths); + var operation = new ResourceImporterOperation(Download, PackageName, importerList, importerMaxNumber, failedTryAgain, timeout); + return operation; + } + private List GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths) + { + List result = new List(); + foreach (var filePath in filePaths) + { + string fileName = System.IO.Path.GetFileName(filePath); + if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle)) + { + // 忽略缓存文件 + if (IsCachedPackageBundle(packageBundle)) + continue; - var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath); - result.Add(bundleInfo); - } - else - { - YooLogger.Warning($"Not found package bundle, importer file path : {filePath}"); - } - } - return result; - } - #endregion + var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath); + result.Add(bundleInfo); + } + else + { + YooLogger.Warning($"Not found package bundle, importer file path : {filePath}"); + } + } + return result; + } + #endregion - #region IBundleQuery接口 - private BundleInfo CreateBundleInfo(PackageBundle packageBundle) - { - if (packageBundle == null) - throw new Exception("Should never get here !"); + #region IBundleQuery接口 + private BundleInfo CreateBundleInfo(PackageBundle packageBundle) + { + if (packageBundle == null) + throw new Exception("Should never get here !"); - // 查询沙盒资源 - if (IsCachedPackageBundle(packageBundle)) - { - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromCache); - return bundleInfo; - } + // 查询沙盒资源 + if (IsCachedPackageBundle(packageBundle)) + { + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromCache); + return bundleInfo; + } - // 查询APP资源 - { - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming); - return bundleInfo; - } - } - BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 查询APP资源 + { + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming); + return bundleInfo; + } + } + BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return CreateBundleInfo(packageBundle); - } - BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return CreateBundleInfo(packageBundle); + } + BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - BundleInfo bundleInfo = CreateBundleInfo(packageBundle); - result.Add(bundleInfo); - } - return result.ToArray(); - } - string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + BundleInfo bundleInfo = CreateBundleInfo(packageBundle); + result.Add(bundleInfo); + } + return result.ToArray(); + } + string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return packageBundle.BundleName; - } - string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return packageBundle.BundleName; + } + string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - result.Add(packageBundle.BundleName); - } - return result.ToArray(); - } - bool IBundleQuery.ManifestValid() - { - return _activeManifest != null; - } - #endregion - } + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + result.Add(packageBundle.BundleName); + } + return result.ToArray(); + } + bool IBundleQuery.ManifestValid() + { + return _activeManifest != null; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs index 106ee6c..f5cf33b 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs @@ -4,288 +4,288 @@ using System.Collections.Generic; namespace YooAsset { - internal class WebPlayModeImpl : IPlayMode, IBundleQuery - { - private PackageManifest _activeManifest; - private ResourceAssist _assist; - private IBuildinQueryServices _buildinQueryServices; - private IRemoteServices _remoteServices; + internal class WebPlayModeImpl : IPlayMode, IBundleQuery + { + private PackageManifest _activeManifest; + private ResourceAssist _assist; + private IBuildinQueryServices _buildinQueryServices; + private IRemoteServices _remoteServices; - public readonly string PackageName; - public DownloadManager Download - { - get { return _assist.Download; } - } - public PersistentManager Persistent - { - get { return _assist.Persistent; } - } - public IRemoteServices RemoteServices - { - get { return _remoteServices; } - } + public readonly string PackageName; + public DownloadManager Download + { + get { return _assist.Download; } + } + public PersistentManager Persistent + { + get { return _assist.Persistent; } + } + public IRemoteServices RemoteServices + { + get { return _remoteServices; } + } - public WebPlayModeImpl(string packageName) - { - PackageName = packageName; - } + public WebPlayModeImpl(string packageName) + { + PackageName = packageName; + } - /// - /// 异步初始化 - /// - public InitializationOperation InitializeAsync(ResourceAssist assist, IBuildinQueryServices buildinQueryServices, IRemoteServices remoteServices) - { - _assist = assist; - _buildinQueryServices = buildinQueryServices; - _remoteServices = remoteServices; + /// + /// 异步初始化 + /// + public InitializationOperation InitializeAsync(ResourceAssist assist, IBuildinQueryServices buildinQueryServices, IRemoteServices remoteServices) + { + _assist = assist; + _buildinQueryServices = buildinQueryServices; + _remoteServices = remoteServices; - var operation = new WebPlayModeInitializationOperation(this); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + var operation = new WebPlayModeInitializationOperation(this); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - // 下载相关 - private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle) - { - string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName); - string remoteFallbackURL = _remoteServices.GetRemoteFallbackURL(packageBundle.FileName); - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL); - return bundleInfo; - } - private List ConvertToDownloadList(List downloadList) - { - List result = new List(downloadList.Count); - foreach (var packageBundle in downloadList) - { - var bundleInfo = ConvertToDownloadInfo(packageBundle); - result.Add(bundleInfo); - } - return result; - } + // 下载相关 + private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle) + { + string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName); + string remoteFallbackURL = _remoteServices.GetRemoteFallbackURL(packageBundle.FileName); + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL); + return bundleInfo; + } + private List ConvertToDownloadList(List downloadList) + { + List result = new List(downloadList.Count); + foreach (var packageBundle in downloadList) + { + var bundleInfo = ConvertToDownloadInfo(packageBundle); + result.Add(bundleInfo); + } + return result; + } - // 查询相关 - private bool IsBuildinPackageBundle(PackageBundle packageBundle) - { - return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); - } + // 查询相关 + private bool IsBuildinPackageBundle(PackageBundle packageBundle) + { + return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); + } - #region IPlayMode接口 - public PackageManifest ActiveManifest - { - set - { - _activeManifest = value; - } - get - { - return _activeManifest; - } - } - public void FlushManifestVersionFile() - { - } + #region IPlayMode接口 + public PackageManifest ActiveManifest + { + set + { + _activeManifest = value; + } + get + { + return _activeManifest; + } + } + public void FlushManifestVersionFile() + { + } - UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) - { - var operation = new WebPlayModeUpdatePackageVersionOperation(this, appendTimeTicks, timeout); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) - { - var operation = new WebPlayModeUpdatePackageManifestOperation(this, packageVersion, timeout); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) - { - var operation = new WebPlayModePreDownloadContentOperation(this); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } + UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout) + { + var operation = new WebPlayModeUpdatePackageVersionOperation(this, appendTimeTicks, timeout); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout) + { + var operation = new WebPlayModeUpdatePackageManifestOperation(this, packageVersion, timeout); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout) + { + var operation = new WebPlayModePreDownloadContentOperation(this); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = GetDownloadListByAll(_activeManifest); - var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public List GetDownloadListByAll(PackageManifest manifest) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略APP资源 - if (IsBuildinPackageBundle(packageBundle)) - continue; + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = GetDownloadListByAll(_activeManifest); + var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByAll(PackageManifest manifest) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; - downloadList.Add(packageBundle); - } + downloadList.Add(packageBundle); + } - return ConvertToDownloadList(downloadList); - } + return ConvertToDownloadList(downloadList); + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = GetDownloadListByTags(_activeManifest, tags); - var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public List GetDownloadListByTags(PackageManifest manifest, string[] tags) - { - List downloadList = new List(1000); - foreach (var packageBundle in manifest.BundleList) - { - // 忽略APP资源 - if (IsBuildinPackageBundle(packageBundle)) - continue; + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = GetDownloadListByTags(_activeManifest, tags); + var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByTags(PackageManifest manifest, string[] tags) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; - // 如果未带任何标记,则统一下载 - if (packageBundle.HasAnyTags() == false) - { - downloadList.Add(packageBundle); - } - else - { - // 查询DLC资源 - if (packageBundle.HasTag(tags)) - { - downloadList.Add(packageBundle); - } - } - } + // 如果未带任何标记,则统一下载 + if (packageBundle.HasAnyTags() == false) + { + downloadList.Add(packageBundle); + } + else + { + // 查询DLC资源 + if (packageBundle.HasTag(tags)) + { + downloadList.Add(packageBundle); + } + } + } - return ConvertToDownloadList(downloadList); - } + return ConvertToDownloadList(downloadList); + } - ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) - { - List downloadList = GetDownloadListByPaths(_activeManifest, assetInfos); - var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); - return operation; - } - public List GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos) - { - // 获取资源对象的资源包和所有依赖资源包 - List checkList = new List(); - foreach (var assetInfo in assetInfos) - { - if (assetInfo.IsInvalid) - { - YooLogger.Warning(assetInfo.Error); - continue; - } + ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) + { + List downloadList = GetDownloadListByPaths(_activeManifest, assetInfos); + var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos) + { + // 获取资源对象的资源包和所有依赖资源包 + List checkList = new List(); + foreach (var assetInfo in assetInfos) + { + if (assetInfo.IsInvalid) + { + YooLogger.Warning(assetInfo.Error); + continue; + } - // 注意:如果清单里未找到资源包会抛出异常! - PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath); - if (checkList.Contains(mainBundle) == false) - checkList.Add(mainBundle); + // 注意:如果清单里未找到资源包会抛出异常! + PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath); + if (checkList.Contains(mainBundle) == false) + checkList.Add(mainBundle); - // 注意:如果清单里未找到资源包会抛出异常! - PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath); - foreach (var dependBundle in dependBundles) - { - if (checkList.Contains(dependBundle) == false) - checkList.Add(dependBundle); - } - } + // 注意:如果清单里未找到资源包会抛出异常! + PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath); + foreach (var dependBundle in dependBundles) + { + if (checkList.Contains(dependBundle) == false) + checkList.Add(dependBundle); + } + } - List downloadList = new List(1000); - foreach (var packageBundle in checkList) - { - // 忽略APP资源 - if (IsBuildinPackageBundle(packageBundle)) - continue; + List downloadList = new List(1000); + foreach (var packageBundle in checkList) + { + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; - downloadList.Add(packageBundle); - } + downloadList.Add(packageBundle); + } - return ConvertToDownloadList(downloadList); - } + return ConvertToDownloadList(downloadList); + } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); - } - ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) - { - return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); - } + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); + } + ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout) + { + return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout); + } - ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) - { - return ResourceImporterOperation.CreateEmptyImporter(Download, PackageName, importerMaxNumber, failedTryAgain, timeout); - } - #endregion + ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout) + { + return ResourceImporterOperation.CreateEmptyImporter(Download, PackageName, importerMaxNumber, failedTryAgain, timeout); + } + #endregion - #region IBundleQuery接口 - private BundleInfo CreateBundleInfo(PackageBundle packageBundle) - { - if (packageBundle == null) - throw new Exception("Should never get here !"); + #region IBundleQuery接口 + private BundleInfo CreateBundleInfo(PackageBundle packageBundle) + { + if (packageBundle == null) + throw new Exception("Should never get here !"); - // 查询APP资源 - if (IsBuildinPackageBundle(packageBundle)) - { - BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming); - return bundleInfo; - } + // 查询APP资源 + if (IsBuildinPackageBundle(packageBundle)) + { + BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming); + return bundleInfo; + } - // 从服务端下载 - return ConvertToDownloadInfo(packageBundle); - } - BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 从服务端下载 + return ConvertToDownloadInfo(packageBundle); + } + BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return CreateBundleInfo(packageBundle); - } - BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return CreateBundleInfo(packageBundle); + } + BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - BundleInfo bundleInfo = CreateBundleInfo(packageBundle); - result.Add(bundleInfo); - } - return result.ToArray(); - } - string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + BundleInfo bundleInfo = CreateBundleInfo(packageBundle); + result.Add(bundleInfo); + } + return result.ToArray(); + } + string IBundleQuery.GetMainBundleName(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); - return packageBundle.BundleName; - } - string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - throw new Exception("Should never get here !"); + // 注意:如果清单里未找到资源包会抛出异常! + var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath); + return packageBundle.BundleName; + } + string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + throw new Exception("Should never get here !"); - // 注意:如果清单里未找到资源包会抛出异常! - var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); - List result = new List(depends.Length); - foreach (var packageBundle in depends) - { - result.Add(packageBundle.BundleName); - } - return result.ToArray(); - } - bool IBundleQuery.ManifestValid() - { - return _activeManifest != null; - } - #endregion - } + // 注意:如果清单里未找到资源包会抛出异常! + var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath); + List result = new List(depends.Length); + foreach (var packageBundle in depends) + { + result.Add(packageBundle.BundleName); + } + return result.ToArray(); + } + bool IBundleQuery.ManifestValid() + { + return _activeManifest != null; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourceAssist.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourceAssist.cs index 1da4b5a..be91901 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourceAssist.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourceAssist.cs @@ -1,11 +1,11 @@  namespace YooAsset { - internal class ResourceAssist - { - public CacheManager Cache { set; get; } - public PersistentManager Persistent { set; get; } - public DownloadManager Download { set; get; } - public ResourceLoader Loader { set; get; } - } + internal class ResourceAssist + { + public CacheManager Cache { set; get; } + public PersistentManager Persistent { set; get; } + public DownloadManager Download { set; get; } + public ResourceLoader Loader { set; get; } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs index 4384b6e..fdb2173 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs @@ -6,1185 +6,1185 @@ using UnityEngine.SceneManagement; namespace YooAsset { - public class ResourcePackage - { - private bool _isInitialize = false; - private string _initializeError = string.Empty; - private EOperationStatus _initializeStatus = EOperationStatus.None; - private EPlayMode _playMode; + public class ResourcePackage + { + private bool _isInitialize = false; + private string _initializeError = string.Empty; + private EOperationStatus _initializeStatus = EOperationStatus.None; + private EPlayMode _playMode; - // 管理器 - private CacheManager _cacheMgr; - private PersistentManager _persistentMgr; - private DownloadManager _downloadMgr; - private ResourceManager _resourceMgr; - private ResourceLoader _resourceLoader; - private IBundleQuery _bundleQuery; - private IPlayMode _playModeImpl; + // 管理器 + private CacheManager _cacheMgr; + private PersistentManager _persistentMgr; + private DownloadManager _downloadMgr; + private ResourceManager _resourceMgr; + private ResourceLoader _resourceLoader; + private IBundleQuery _bundleQuery; + private IPlayMode _playModeImpl; - /// - /// 包裹名 - /// - public readonly string PackageName; + /// + /// 包裹名 + /// + public readonly string PackageName; - /// - /// 初始化状态 - /// - public EOperationStatus InitializeStatus - { - get { return _initializeStatus; } - } + /// + /// 初始化状态 + /// + public EOperationStatus InitializeStatus + { + get { return _initializeStatus; } + } - private ResourcePackage() - { - } - internal ResourcePackage(string packageName) - { - PackageName = packageName; - } + private ResourcePackage() + { + } + internal ResourcePackage(string packageName) + { + PackageName = packageName; + } - /// - /// 更新资源包裹 - /// - internal void UpdatePackage() - { - if (_resourceMgr != null) - _resourceMgr.Update(); + /// + /// 更新资源包裹 + /// + internal void UpdatePackage() + { + if (_resourceMgr != null) + _resourceMgr.Update(); - if (_downloadMgr != null) - _downloadMgr.Update(); - } + if (_downloadMgr != null) + _downloadMgr.Update(); + } - /// - /// 销毁资源包裹 - /// - internal void DestroyPackage() - { - if (_isInitialize) - { - _isInitialize = false; - _initializeError = string.Empty; - _initializeStatus = EOperationStatus.None; + /// + /// 销毁资源包裹 + /// + internal void DestroyPackage() + { + if (_isInitialize) + { + _isInitialize = false; + _initializeError = string.Empty; + _initializeStatus = EOperationStatus.None; - _bundleQuery = null; - _playModeImpl = null; - _persistentMgr = null; - _resourceLoader = null; + _bundleQuery = null; + _playModeImpl = null; + _persistentMgr = null; + _resourceLoader = null; - if (_resourceMgr != null) - { - _resourceMgr.ForceUnloadAllAssets(); - _resourceMgr = null; - } + if (_resourceMgr != null) + { + _resourceMgr.ForceUnloadAllAssets(); + _resourceMgr = null; + } - if (_downloadMgr != null) - { - _downloadMgr.DestroyAll(); - _downloadMgr = null; - } + if (_downloadMgr != null) + { + _downloadMgr.DestroyAll(); + _downloadMgr = null; + } - if (_cacheMgr != null) - { - _cacheMgr.ClearAll(); - _cacheMgr = null; - } + if (_cacheMgr != null) + { + _cacheMgr.ClearAll(); + _cacheMgr = null; + } - // 最后清理该包裹的异步任务 - // 注意:对于有线程操作的异步任务,需要保证线程安全释放。 - OperationSystem.ClearPackageOperation(PackageName); - } - } + // 最后清理该包裹的异步任务 + // 注意:对于有线程操作的异步任务,需要保证线程安全释放。 + OperationSystem.ClearPackageOperation(PackageName); + } + } - /// - /// 异步初始化 - /// - public InitializationOperation InitializeAsync(InitializeParameters parameters) - { - // 注意:WebGL平台因为网络原因可能会初始化失败! - ResetInitializeAfterFailed(); + /// + /// 异步初始化 + /// + public InitializationOperation InitializeAsync(InitializeParameters parameters) + { + // 注意:WebGL平台因为网络原因可能会初始化失败! + ResetInitializeAfterFailed(); - // 检测初始化参数合法性 - CheckInitializeParameters(parameters); + // 检测初始化参数合法性 + CheckInitializeParameters(parameters); - // 创建缓存管理器 - _cacheMgr = new CacheManager(PackageName, parameters.CacheBootVerifyLevel); + // 创建缓存管理器 + _cacheMgr = new CacheManager(PackageName, parameters.CacheBootVerifyLevel); - // 创建持久化管理器 - _persistentMgr = new PersistentManager(PackageName); - _persistentMgr.Initialize(parameters.BuildinRootDirectory, parameters.SandboxRootDirectory, parameters.CacheFileAppendExtension); + // 创建持久化管理器 + _persistentMgr = new PersistentManager(PackageName); + _persistentMgr.Initialize(parameters.BuildinRootDirectory, parameters.SandboxRootDirectory, parameters.CacheFileAppendExtension); - // 创建下载管理器 - _downloadMgr = new DownloadManager(PackageName); - _downloadMgr.Initialize(parameters.BreakpointResumeFileSize); + // 创建下载管理器 + _downloadMgr = new DownloadManager(PackageName); + _downloadMgr.Initialize(parameters.BreakpointResumeFileSize); - // 创建资源包加载器 - if (_playMode == EPlayMode.HostPlayMode) - { - var initializeParameters = parameters as HostPlayModeParameters; - _resourceLoader = new ResourceLoader(); - _resourceLoader.Init(parameters.DecryptionServices, initializeParameters.DeliveryLoadServices); - } - else - { - _resourceLoader = new ResourceLoader(); - _resourceLoader.Init(parameters.DecryptionServices, null); - } + // 创建资源包加载器 + if (_playMode == EPlayMode.HostPlayMode) + { + var initializeParameters = parameters as HostPlayModeParameters; + _resourceLoader = new ResourceLoader(); + _resourceLoader.Init(parameters.DecryptionServices, initializeParameters.DeliveryLoadServices); + } + else + { + _resourceLoader = new ResourceLoader(); + _resourceLoader.Init(parameters.DecryptionServices, null); + } - // 创建资源协助类 - ResourceAssist assist = new ResourceAssist(); - assist.Cache = _cacheMgr; - assist.Persistent = _persistentMgr; - assist.Download = _downloadMgr; - assist.Loader = _resourceLoader; + // 创建资源协助类 + ResourceAssist assist = new ResourceAssist(); + assist.Cache = _cacheMgr; + assist.Persistent = _persistentMgr; + assist.Download = _downloadMgr; + assist.Loader = _resourceLoader; - // 创建资源管理器 - InitializationOperation initializeOperation; - _resourceMgr = new ResourceManager(PackageName); - if (_playMode == EPlayMode.EditorSimulateMode) - { - var editorSimulateModeImpl = new EditorSimulateModeImpl(PackageName); - _bundleQuery = editorSimulateModeImpl; - _playModeImpl = editorSimulateModeImpl; - _resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, _bundleQuery); + // 创建资源管理器 + InitializationOperation initializeOperation; + _resourceMgr = new ResourceManager(PackageName); + if (_playMode == EPlayMode.EditorSimulateMode) + { + var editorSimulateModeImpl = new EditorSimulateModeImpl(PackageName); + _bundleQuery = editorSimulateModeImpl; + _playModeImpl = editorSimulateModeImpl; + _resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, _bundleQuery); - var initializeParameters = parameters as EditorSimulateModeParameters; - initializeOperation = editorSimulateModeImpl.InitializeAsync(assist, initializeParameters.SimulateManifestFilePath); - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - var offlinePlayModeImpl = new OfflinePlayModeImpl(PackageName); - _bundleQuery = offlinePlayModeImpl; - _playModeImpl = offlinePlayModeImpl; - _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); + var initializeParameters = parameters as EditorSimulateModeParameters; + initializeOperation = editorSimulateModeImpl.InitializeAsync(assist, initializeParameters.SimulateManifestFilePath); + } + else if (_playMode == EPlayMode.OfflinePlayMode) + { + var offlinePlayModeImpl = new OfflinePlayModeImpl(PackageName); + _bundleQuery = offlinePlayModeImpl; + _playModeImpl = offlinePlayModeImpl; + _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); - var initializeParameters = parameters as OfflinePlayModeParameters; - initializeOperation = offlinePlayModeImpl.InitializeAsync(assist); - } - else if (_playMode == EPlayMode.HostPlayMode) - { - var hostPlayModeImpl = new HostPlayModeImpl(PackageName); - _bundleQuery = hostPlayModeImpl; - _playModeImpl = hostPlayModeImpl; - _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); + var initializeParameters = parameters as OfflinePlayModeParameters; + initializeOperation = offlinePlayModeImpl.InitializeAsync(assist); + } + else if (_playMode == EPlayMode.HostPlayMode) + { + var hostPlayModeImpl = new HostPlayModeImpl(PackageName); + _bundleQuery = hostPlayModeImpl; + _playModeImpl = hostPlayModeImpl; + _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); - var initializeParameters = parameters as HostPlayModeParameters; - initializeOperation = hostPlayModeImpl.InitializeAsync(assist, - initializeParameters.BuildinQueryServices, - initializeParameters.DeliveryQueryServices, - initializeParameters.RemoteServices); - } - else if (_playMode == EPlayMode.WebPlayMode) - { - var webPlayModeImpl = new WebPlayModeImpl(PackageName); - _bundleQuery = webPlayModeImpl; - _playModeImpl = webPlayModeImpl; - _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); + var initializeParameters = parameters as HostPlayModeParameters; + initializeOperation = hostPlayModeImpl.InitializeAsync(assist, + initializeParameters.BuildinQueryServices, + initializeParameters.DeliveryQueryServices, + initializeParameters.RemoteServices); + } + else if (_playMode == EPlayMode.WebPlayMode) + { + var webPlayModeImpl = new WebPlayModeImpl(PackageName); + _bundleQuery = webPlayModeImpl; + _playModeImpl = webPlayModeImpl; + _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); - var initializeParameters = parameters as WebPlayModeParameters; - initializeOperation = webPlayModeImpl.InitializeAsync(assist, - initializeParameters.BuildinQueryServices, - initializeParameters.RemoteServices); - } - else - { - throw new NotImplementedException(); - } + var initializeParameters = parameters as WebPlayModeParameters; + initializeOperation = webPlayModeImpl.InitializeAsync(assist, + initializeParameters.BuildinQueryServices, + initializeParameters.RemoteServices); + } + else + { + throw new NotImplementedException(); + } - // 监听初始化结果 - _isInitialize = true; - initializeOperation.Completed += InitializeOperation_Completed; - return initializeOperation; - } - private void ResetInitializeAfterFailed() - { - if (_isInitialize && _initializeStatus == EOperationStatus.Failed) - { - _isInitialize = false; - _initializeStatus = EOperationStatus.None; - _initializeError = string.Empty; - } - } - private void CheckInitializeParameters(InitializeParameters parameters) - { - if (_isInitialize) - throw new Exception($"{nameof(ResourcePackage)} is initialized yet."); + // 监听初始化结果 + _isInitialize = true; + initializeOperation.Completed += InitializeOperation_Completed; + return initializeOperation; + } + private void ResetInitializeAfterFailed() + { + if (_isInitialize && _initializeStatus == EOperationStatus.Failed) + { + _isInitialize = false; + _initializeStatus = EOperationStatus.None; + _initializeError = string.Empty; + } + } + private void CheckInitializeParameters(InitializeParameters parameters) + { + if (_isInitialize) + throw new Exception($"{nameof(ResourcePackage)} is initialized yet."); - if (parameters == null) - throw new Exception($"{nameof(ResourcePackage)} create parameters is null."); + if (parameters == null) + throw new Exception($"{nameof(ResourcePackage)} create parameters is null."); #if !UNITY_EDITOR if (parameters is EditorSimulateModeParameters) throw new Exception($"Editor simulate mode only support unity editor."); #endif - if (parameters is EditorSimulateModeParameters) - { - var editorSimulateModeParameters = parameters as EditorSimulateModeParameters; - if (string.IsNullOrEmpty(editorSimulateModeParameters.SimulateManifestFilePath)) - throw new Exception($"{nameof(editorSimulateModeParameters.SimulateManifestFilePath)} is null or empty."); - } + if (parameters is EditorSimulateModeParameters) + { + var editorSimulateModeParameters = parameters as EditorSimulateModeParameters; + if (string.IsNullOrEmpty(editorSimulateModeParameters.SimulateManifestFilePath)) + throw new Exception($"{nameof(editorSimulateModeParameters.SimulateManifestFilePath)} is null or empty."); + } - if (parameters is HostPlayModeParameters) - { - var hostPlayModeParameters = parameters as HostPlayModeParameters; - if (hostPlayModeParameters.RemoteServices == null) - throw new Exception($"{nameof(IRemoteServices)} is null."); - if (hostPlayModeParameters.BuildinQueryServices == null) - throw new Exception($"{nameof(IBuildinQueryServices)} is null."); - if (hostPlayModeParameters.DeliveryQueryServices != null) - { - if (hostPlayModeParameters.DeliveryLoadServices == null) - throw new Exception($"{nameof(IDeliveryLoadServices)} is null."); - } - } + if (parameters is HostPlayModeParameters) + { + var hostPlayModeParameters = parameters as HostPlayModeParameters; + if (hostPlayModeParameters.RemoteServices == null) + throw new Exception($"{nameof(IRemoteServices)} is null."); + if (hostPlayModeParameters.BuildinQueryServices == null) + throw new Exception($"{nameof(IBuildinQueryServices)} is null."); + if (hostPlayModeParameters.DeliveryQueryServices != null) + { + if (hostPlayModeParameters.DeliveryLoadServices == null) + throw new Exception($"{nameof(IDeliveryLoadServices)} is null."); + } + } - // 鉴定运行模式 - if (parameters is EditorSimulateModeParameters) - _playMode = EPlayMode.EditorSimulateMode; - else if (parameters is OfflinePlayModeParameters) - _playMode = EPlayMode.OfflinePlayMode; - else if (parameters is HostPlayModeParameters) - _playMode = EPlayMode.HostPlayMode; - else if (parameters is WebPlayModeParameters) - _playMode = EPlayMode.WebPlayMode; - else - throw new NotImplementedException(); + // 鉴定运行模式 + if (parameters is EditorSimulateModeParameters) + _playMode = EPlayMode.EditorSimulateMode; + else if (parameters is OfflinePlayModeParameters) + _playMode = EPlayMode.OfflinePlayMode; + else if (parameters is HostPlayModeParameters) + _playMode = EPlayMode.HostPlayMode; + else if (parameters is WebPlayModeParameters) + _playMode = EPlayMode.WebPlayMode; + else + throw new NotImplementedException(); - // 检测运行时平台 - if (_playMode != EPlayMode.EditorSimulateMode) - { + // 检测运行时平台 + if (_playMode != EPlayMode.EditorSimulateMode) + { #if UNITY_WEBGL if (_playMode != EPlayMode.WebPlayMode) { throw new Exception($"{_playMode} can not support WebGL plateform ! Please use {nameof(EPlayMode.WebPlayMode)}"); } #else - if (_playMode == EPlayMode.WebPlayMode) - { - throw new Exception($"{nameof(EPlayMode.WebPlayMode)} only support WebGL plateform !"); - } + if (_playMode == EPlayMode.WebPlayMode) + { + throw new Exception($"{nameof(EPlayMode.WebPlayMode)} only support WebGL plateform !"); + } #endif - } - } - private void InitializeOperation_Completed(AsyncOperationBase op) - { - _initializeStatus = op.Status; - _initializeError = op.Error; - } - - /// - /// 向网络端请求最新的资源版本 - /// - /// 在URL末尾添加时间戳 - /// 超时时间(默认值:60秒) - public UpdatePackageVersionOperation UpdatePackageVersionAsync(bool appendTimeTicks = true, int timeout = 60) - { - DebugCheckInitialize(false); - return _playModeImpl.UpdatePackageVersionAsync(appendTimeTicks, timeout); - } - - /// - /// 向网络端请求并更新清单 - /// - /// 更新的包裹版本 - /// 更新成功后自动保存版本号,作为下次初始化的版本。 - /// 超时时间(默认值:60秒) - public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion = true, int timeout = 60) - { - DebugCheckInitialize(false); - - // 注意:强烈建议在更新之前保持加载器为空! - if (_resourceMgr.HasAnyLoader()) - { - YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !"); - } - - return _playModeImpl.UpdatePackageManifestAsync(packageVersion, autoSaveVersion, timeout); - } - - /// - /// 预下载指定版本的包裹资源 - /// - /// 下载的包裹版本 - /// 超时时间(默认值:60秒) - public PreDownloadContentOperation PreDownloadContentAsync(string packageVersion, int timeout = 60) - { - DebugCheckInitialize(false); - return _playModeImpl.PreDownloadContentAsync(packageVersion, timeout); - } - - /// - /// 清理包裹未使用的缓存文件 - /// - public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync() - { - DebugCheckInitialize(); - var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - - /// - /// 清理包裹本地所有的缓存文件 - /// - public ClearAllCacheFilesOperation ClearAllCacheFilesAsync() - { - DebugCheckInitialize(); - var operation = new ClearAllCacheFilesOperation(_cacheMgr); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - - /// - /// 获取本地包裹的版本信息 - /// - public string GetPackageVersion() - { - DebugCheckInitialize(); - return _playModeImpl.ActiveManifest.PackageVersion; - } - - #region 资源卸载 - /// - /// 资源回收(卸载引用计数为零的资源) - /// - public void UnloadUnusedAssets() - { - DebugCheckInitialize(); - _resourceMgr.UnloadUnusedAssets(); - } - - /// - /// 资源回收(尝试卸载指定的资源) - /// - public void TryUnloadUnusedAsset(string location) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - _resourceMgr.TryUnloadUnusedAsset(assetInfo); - } - - /// - /// 资源回收(尝试卸载指定的资源) - /// - public void TryUnloadUnusedAsset(AssetInfo assetInfo) - { - DebugCheckInitialize(); - _resourceMgr.TryUnloadUnusedAsset(assetInfo); - } - - /// - /// 强制回收所有资源 - /// - public void ForceUnloadAllAssets() - { - DebugCheckInitialize(); - _resourceMgr.ForceUnloadAllAssets(); - } - #endregion - - #region 沙盒相关 - /// - /// 获取包裹的内置文件根路径 - /// - public string GetPackageBuildinRootDirectory() - { - DebugCheckInitialize(); - return _persistentMgr.BuildinRoot; - } - - /// - /// 获取包裹的沙盒文件根路径 - /// - public string GetPackageSandboxRootDirectory() - { - DebugCheckInitialize(); - return _persistentMgr.SandboxRoot; - } - - /// - /// 清空包裹的沙盒目录 - /// - public void ClearPackageSandbox() - { - DebugCheckInitialize(); - _persistentMgr.DeleteSandboxPackageFolder(); - _cacheMgr.ClearAll(); - } - #endregion - - #region 资源信息 - /// - /// 是否需要从远端更新下载 - /// - /// 资源的定位地址 - public bool IsNeedDownloadFromRemote(string location) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - return IsNeedDownloadFromRemoteInternal(assetInfo); - } - - /// - /// 是否需要从远端更新下载 - /// - /// 资源的定位地址 - public bool IsNeedDownloadFromRemote(AssetInfo assetInfo) - { - DebugCheckInitialize(); - return IsNeedDownloadFromRemoteInternal(assetInfo); - } - - /// - /// 获取资源信息列表 - /// - /// 资源标签 - public AssetInfo[] GetAssetInfos(string tag) - { - DebugCheckInitialize(); - string[] tags = new string[] { tag }; - return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags); - } - - /// - /// 获取资源信息列表 - /// - /// 资源标签列表 - public AssetInfo[] GetAssetInfos(string[] tags) - { - DebugCheckInitialize(); - return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags); - } - - /// - /// 获取资源信息 - /// - /// 资源的定位地址 - public AssetInfo GetAssetInfo(string location) - { - DebugCheckInitialize(); - return ConvertLocationToAssetInfo(location, null); - } - - /// - /// 获取资源信息 - /// - /// 资源的定位地址 - /// 资源类型 - public AssetInfo GetAssetInfo(string location, System.Type type) - { - DebugCheckInitialize(); - return ConvertLocationToAssetInfo(location, type); - } - - /// - /// 获取资源信息 - /// - /// 资源GUID - public AssetInfo GetAssetInfoByGUID(string assetGUID) - { - DebugCheckInitialize(); - return ConvertAssetGUIDToAssetInfo(assetGUID, null); - } - - /// - /// 获取资源信息 - /// - /// 资源GUID - /// 资源类型 - public AssetInfo GetAssetInfoByGUID(string assetGUID, System.Type type) - { - DebugCheckInitialize(); - return ConvertAssetGUIDToAssetInfo(assetGUID, type); - } - - /// - /// 检查资源定位地址是否有效 - /// - /// 资源的定位地址 - public bool CheckLocationValid(string location) - { - DebugCheckInitialize(); - string assetPath = _playModeImpl.ActiveManifest.TryMappingToAssetPath(location); - return string.IsNullOrEmpty(assetPath) == false; - } - - private bool IsNeedDownloadFromRemoteInternal(AssetInfo assetInfo) - { - if (assetInfo.IsInvalid) - { - YooLogger.Warning(assetInfo.Error); - return false; - } - - BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo); - if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - return true; - - BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo); - foreach (var depend in depends) - { - if (depend.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - return true; - } - - return false; - } - #endregion - - #region 原生文件 - /// - /// 同步加载原生文件 - /// - /// 资源信息 - public RawFileHandle LoadRawFileSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - return LoadRawFileInternal(assetInfo, true, 0); - } - - /// - /// 同步加载原生文件 - /// - /// 资源的定位地址 - public RawFileHandle LoadRawFileSync(string location) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - return LoadRawFileInternal(assetInfo, true, 0); - } - - /// - /// 异步加载原生文件 - /// - /// 资源信息 - /// 加载的优先级 - public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckInitialize(); - return LoadRawFileInternal(assetInfo, false, priority); - } - - /// - /// 异步加载原生文件 - /// - /// 资源的定位地址 - /// 加载的优先级 - public RawFileHandle LoadRawFileAsync(string location, uint priority = 0) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - return LoadRawFileInternal(assetInfo, false, priority); - } - - - private RawFileHandle LoadRawFileInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) - { - DebugCheckRawFileLoadMethod(nameof(LoadRawFileAsync)); - var handle = _resourceMgr.LoadRawFileAsync(assetInfo, priority); - if (waitForAsyncComplete) - handle.WaitForAsyncComplete(); - return handle; - } - #endregion - - #region 场景加载 - /// - /// 异步加载场景 - /// - /// 场景的定位地址 - /// 场景加载模式 - /// 场景加载到90%自动挂起 - /// 加载的优先级 - public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); - return handle; - } - - /// - /// 异步加载场景 - /// - /// 场景的资源信息 - /// 场景加载模式 - /// 场景加载到90%自动挂起 - /// 加载的优先级 - public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) - { - DebugCheckInitialize(); - var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); - return handle; - } - #endregion - - #region 资源加载 - /// - /// 同步加载资源对象 - /// - /// 资源信息 - public AssetHandle LoadAssetSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - return LoadAssetInternal(assetInfo, true, 0); - } - - /// - /// 同步加载资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public AssetHandle LoadAssetSync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadAssetInternal(assetInfo, true, 0); - } - - /// - /// 同步加载资源对象 - /// - /// 资源的定位地址 - /// 资源类型 - public AssetHandle LoadAssetSync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAssetInternal(assetInfo, true, 0); - } - - /// - /// 同步加载资源对象 - /// - /// 资源的定位地址 - public AssetHandle LoadAssetSync(string location) - { - DebugCheckInitialize(); - Type type = typeof(UnityEngine.Object); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAssetInternal(assetInfo, true, 0); - } - - - /// - /// 异步加载资源对象 - /// - /// 资源信息 - /// 加载的优先级 - public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckInitialize(); - return LoadAssetInternal(assetInfo, false, priority); - } - - /// - /// 异步加载资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - /// 加载的优先级 - public AssetHandle LoadAssetAsync(string location, uint priority = 0) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadAssetInternal(assetInfo, false, priority); - } - - /// - /// 异步加载资源对象 - /// - /// 资源的定位地址 - /// 资源类型 - /// 加载的优先级 - public AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAssetInternal(assetInfo, false, priority); - } - - /// - /// 异步加载资源对象 - /// - /// 资源的定位地址 - /// 加载的优先级 - public AssetHandle LoadAssetAsync(string location, uint priority = 0) - { - DebugCheckInitialize(); - Type type = typeof(UnityEngine.Object); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAssetInternal(assetInfo, false, priority); - } - - - private AssetHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) - { - DebugCheckAssetLoadMethod(nameof(LoadAssetAsync)); - DebugCheckAssetLoadType(assetInfo.AssetType); - var handle = _resourceMgr.LoadAssetAsync(assetInfo, priority); - if (waitForAsyncComplete) - handle.WaitForAsyncComplete(); - return handle; - } - #endregion - - #region 资源加载 - /// - /// 同步加载子资源对象 - /// - /// 资源信息 - public SubAssetsHandle LoadSubAssetsSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - return LoadSubAssetsInternal(assetInfo, true, 0); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public SubAssetsHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadSubAssetsInternal(assetInfo, true, 0); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public SubAssetsHandle LoadSubAssetsSync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadSubAssetsInternal(assetInfo, true, 0); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源的定位地址 - public SubAssetsHandle LoadSubAssetsSync(string location) - { - DebugCheckInitialize(); - Type type = typeof(UnityEngine.Object); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadSubAssetsInternal(assetInfo, true, 0); - } - - - /// - /// 异步加载子资源对象 - /// - /// 资源信息 - /// 加载的优先级 - public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckInitialize(); - return LoadSubAssetsInternal(assetInfo, false, priority); - } - - /// - /// 异步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - /// 加载的优先级 - public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadSubAssetsInternal(assetInfo, false, priority); - } - - /// - /// 异步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - /// 加载的优先级 - public SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadSubAssetsInternal(assetInfo, false, priority); - } - - /// - /// 异步加载子资源对象 - /// - /// 资源的定位地址 - /// 加载的优先级 - public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) - { - DebugCheckInitialize(); - Type type = typeof(UnityEngine.Object); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadSubAssetsInternal(assetInfo, false, priority); - } - - - private SubAssetsHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) - { - DebugCheckAssetLoadMethod(nameof(LoadSubAssetsAsync)); - DebugCheckAssetLoadType(assetInfo.AssetType); - var handle = _resourceMgr.LoadSubAssetsAsync(assetInfo, priority); - if (waitForAsyncComplete) - handle.WaitForAsyncComplete(); - return handle; - } - #endregion - - #region 资源加载 - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源信息 - public AllAssetsHandle LoadAllAssetsSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - return LoadAllAssetsInternal(assetInfo, true, 0); - } - - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public AllAssetsHandle LoadAllAssetsSync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadAllAssetsInternal(assetInfo, true, 0); - } - - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public AllAssetsHandle LoadAllAssetsSync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAllAssetsInternal(assetInfo, true, 0); - } - - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - public AllAssetsHandle LoadAllAssetsSync(string location) - { - DebugCheckInitialize(); - Type type = typeof(UnityEngine.Object); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAllAssetsInternal(assetInfo, true, 0); - } - - - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源信息 - /// 加载的优先级 - public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckInitialize(); - return LoadAllAssetsInternal(assetInfo, false, priority); - } - - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - /// 加载的优先级 - public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadAllAssetsInternal(assetInfo, false, priority); - } - - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - /// 加载的优先级 - public AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAllAssetsInternal(assetInfo, false, priority); - } - - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - /// 加载的优先级 - public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) - { - DebugCheckInitialize(); - Type type = typeof(UnityEngine.Object); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAllAssetsInternal(assetInfo, false, priority); - } - - - private AllAssetsHandle LoadAllAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) - { - DebugCheckAssetLoadMethod(nameof(LoadAllAssetsAsync)); - DebugCheckAssetLoadType(assetInfo.AssetType); - var handle = _resourceMgr.LoadAllAssetsAsync(assetInfo, priority); - if (waitForAsyncComplete) - handle.WaitForAsyncComplete(); - return handle; - } - #endregion - - #region 资源下载 - /// - /// 创建资源下载器,用于下载当前资源版本所有的资源包文件 - /// - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceDownloaderByAll(downloadingMaxNumber, failedTryAgain, timeout); - } - - /// - /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件 - /// - /// 资源标签 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceDownloaderByTags(new string[] { tag }, downloadingMaxNumber, failedTryAgain, timeout); - } - - /// - /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件 - /// - /// 资源标签列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain, timeout); - } - - /// - /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 - /// - /// 资源的定位地址 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - var assetInfo = ConvertLocationToAssetInfo(location, null); - AssetInfo[] assetInfos = new AssetInfo[] { assetInfo }; - return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout); - } - - /// - /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 - /// - /// 资源的定位地址列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - List assetInfos = new List(locations.Length); - foreach (var location in locations) - { - var assetInfo = ConvertLocationToAssetInfo(location, null); - assetInfos.Add(assetInfo); - } - return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos.ToArray(), downloadingMaxNumber, failedTryAgain, timeout); - } - - /// - /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 - /// - /// 资源信息 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - AssetInfo[] assetInfos = new AssetInfo[] { assetInfo }; - return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout); - } - - /// - /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 - /// - /// 资源信息列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - /// 超时时间 - public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout); - } - #endregion - - #region 资源解压 - /// - /// 创建内置资源解压器,用于解压当前资源版本所有的资源包文件 - /// - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceUnpackerByAll(unpackingMaxNumber, failedTryAgain, int.MaxValue); - } - - /// - /// 创建内置资源解压器,用于解压指定的资源标签关联的资源包文件 - /// - /// 资源标签 - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceUnpackerByTags(new string[] { tag }, unpackingMaxNumber, failedTryAgain, int.MaxValue); - } - - /// - /// 创建内置资源解压器,用于解压指定的资源标签列表关联的资源包文件 - /// - /// 资源标签列表 - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain, int.MaxValue); - } - #endregion - - #region 资源导入 - /// - /// 创建资源导入器 - /// 注意:资源文件名称必须和资源服务器部署的文件名称一致! - /// - /// 资源路径列表 - /// 同时导入的最大文件数 - /// 导入失败的重试次数 - public ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain, int.MaxValue); - } - #endregion - - #region 内部方法 - /// - /// 是否包含资源文件 - /// - internal bool IsIncludeBundleFile(string cacheGUID) - { - // NOTE : 编辑器模拟模式下始终返回TRUE - if (_playMode == EPlayMode.EditorSimulateMode) - return true; - return _playModeImpl.ActiveManifest.IsIncludeBundleFile(cacheGUID); - } - - private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) - { - return _playModeImpl.ActiveManifest.ConvertLocationToAssetInfo(location, assetType); - } - private AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType) - { - return _playModeImpl.ActiveManifest.ConvertAssetGUIDToAssetInfo(assetGUID, assetType); - } - #endregion - - #region 调试方法 - [Conditional("DEBUG")] - private void DebugCheckInitialize(bool checkActiveManifest = true) - { - if (_initializeStatus == EOperationStatus.None) - throw new Exception("Package initialize not completed !"); - else if (_initializeStatus == EOperationStatus.Failed) - throw new Exception($"Package initialize failed ! {_initializeError}"); - - if (checkActiveManifest) - { - if (_playModeImpl.ActiveManifest == null) - throw new Exception("Not found active package manifest !"); - } - } - - [Conditional("DEBUG")] - private void DebugCheckRawFileLoadMethod(string method) - { - if (_playModeImpl.ActiveManifest.BuildPipeline != EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) - { - throw new Exception($"Cannot load asset bundle file using {method} method !"); - } - } - - [Conditional("DEBUG")] - private void DebugCheckAssetLoadMethod(string method) - { - if (_playModeImpl.ActiveManifest.BuildPipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) - { - throw new Exception($"Cannot load raw file using {method} method !"); - } - } - - [Conditional("DEBUG")] - private void DebugCheckAssetLoadType(System.Type type) - { - if (type == null) - return; - - if (typeof(UnityEngine.Behaviour).IsAssignableFrom(type)) - { - throw new Exception($"Load asset type is invalid : {type.FullName} !"); - } - - if (typeof(UnityEngine.Object).IsAssignableFrom(type) == false) - { - throw new Exception($"Load asset type is invalid : {type.FullName} !"); - } - } - #endregion - - #region 调试信息 - internal DebugPackageData GetDebugPackageData() - { - DebugPackageData data = new DebugPackageData(); - data.PackageName = PackageName; - data.ProviderInfos = _resourceMgr.GetDebugReportInfos(); - return data; - } - #endregion - } + } + } + private void InitializeOperation_Completed(AsyncOperationBase op) + { + _initializeStatus = op.Status; + _initializeError = op.Error; + } + + /// + /// 向网络端请求最新的资源版本 + /// + /// 在URL末尾添加时间戳 + /// 超时时间(默认值:60秒) + public UpdatePackageVersionOperation UpdatePackageVersionAsync(bool appendTimeTicks = true, int timeout = 60) + { + DebugCheckInitialize(false); + return _playModeImpl.UpdatePackageVersionAsync(appendTimeTicks, timeout); + } + + /// + /// 向网络端请求并更新清单 + /// + /// 更新的包裹版本 + /// 更新成功后自动保存版本号,作为下次初始化的版本。 + /// 超时时间(默认值:60秒) + public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion = true, int timeout = 60) + { + DebugCheckInitialize(false); + + // 注意:强烈建议在更新之前保持加载器为空! + if (_resourceMgr.HasAnyLoader()) + { + YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !"); + } + + return _playModeImpl.UpdatePackageManifestAsync(packageVersion, autoSaveVersion, timeout); + } + + /// + /// 预下载指定版本的包裹资源 + /// + /// 下载的包裹版本 + /// 超时时间(默认值:60秒) + public PreDownloadContentOperation PreDownloadContentAsync(string packageVersion, int timeout = 60) + { + DebugCheckInitialize(false); + return _playModeImpl.PreDownloadContentAsync(packageVersion, timeout); + } + + /// + /// 清理包裹未使用的缓存文件 + /// + public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync() + { + DebugCheckInitialize(); + var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + + /// + /// 清理包裹本地所有的缓存文件 + /// + public ClearAllCacheFilesOperation ClearAllCacheFilesAsync() + { + DebugCheckInitialize(); + var operation = new ClearAllCacheFilesOperation(_cacheMgr); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + + /// + /// 获取本地包裹的版本信息 + /// + public string GetPackageVersion() + { + DebugCheckInitialize(); + return _playModeImpl.ActiveManifest.PackageVersion; + } + + #region 资源卸载 + /// + /// 资源回收(卸载引用计数为零的资源) + /// + public void UnloadUnusedAssets() + { + DebugCheckInitialize(); + _resourceMgr.UnloadUnusedAssets(); + } + + /// + /// 资源回收(尝试卸载指定的资源) + /// + public void TryUnloadUnusedAsset(string location) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); + _resourceMgr.TryUnloadUnusedAsset(assetInfo); + } + + /// + /// 资源回收(尝试卸载指定的资源) + /// + public void TryUnloadUnusedAsset(AssetInfo assetInfo) + { + DebugCheckInitialize(); + _resourceMgr.TryUnloadUnusedAsset(assetInfo); + } + + /// + /// 强制回收所有资源 + /// + public void ForceUnloadAllAssets() + { + DebugCheckInitialize(); + _resourceMgr.ForceUnloadAllAssets(); + } + #endregion + + #region 沙盒相关 + /// + /// 获取包裹的内置文件根路径 + /// + public string GetPackageBuildinRootDirectory() + { + DebugCheckInitialize(); + return _persistentMgr.BuildinRoot; + } + + /// + /// 获取包裹的沙盒文件根路径 + /// + public string GetPackageSandboxRootDirectory() + { + DebugCheckInitialize(); + return _persistentMgr.SandboxRoot; + } + + /// + /// 清空包裹的沙盒目录 + /// + public void ClearPackageSandbox() + { + DebugCheckInitialize(); + _persistentMgr.DeleteSandboxPackageFolder(); + _cacheMgr.ClearAll(); + } + #endregion + + #region 资源信息 + /// + /// 是否需要从远端更新下载 + /// + /// 资源的定位地址 + public bool IsNeedDownloadFromRemote(string location) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); + return IsNeedDownloadFromRemoteInternal(assetInfo); + } + + /// + /// 是否需要从远端更新下载 + /// + /// 资源的定位地址 + public bool IsNeedDownloadFromRemote(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return IsNeedDownloadFromRemoteInternal(assetInfo); + } + + /// + /// 获取资源信息列表 + /// + /// 资源标签 + public AssetInfo[] GetAssetInfos(string tag) + { + DebugCheckInitialize(); + string[] tags = new string[] { tag }; + return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags); + } + + /// + /// 获取资源信息列表 + /// + /// 资源标签列表 + public AssetInfo[] GetAssetInfos(string[] tags) + { + DebugCheckInitialize(); + return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags); + } + + /// + /// 获取资源信息 + /// + /// 资源的定位地址 + public AssetInfo GetAssetInfo(string location) + { + DebugCheckInitialize(); + return ConvertLocationToAssetInfo(location, null); + } + + /// + /// 获取资源信息 + /// + /// 资源的定位地址 + /// 资源类型 + public AssetInfo GetAssetInfo(string location, System.Type type) + { + DebugCheckInitialize(); + return ConvertLocationToAssetInfo(location, type); + } + + /// + /// 获取资源信息 + /// + /// 资源GUID + public AssetInfo GetAssetInfoByGUID(string assetGUID) + { + DebugCheckInitialize(); + return ConvertAssetGUIDToAssetInfo(assetGUID, null); + } + + /// + /// 获取资源信息 + /// + /// 资源GUID + /// 资源类型 + public AssetInfo GetAssetInfoByGUID(string assetGUID, System.Type type) + { + DebugCheckInitialize(); + return ConvertAssetGUIDToAssetInfo(assetGUID, type); + } + + /// + /// 检查资源定位地址是否有效 + /// + /// 资源的定位地址 + public bool CheckLocationValid(string location) + { + DebugCheckInitialize(); + string assetPath = _playModeImpl.ActiveManifest.TryMappingToAssetPath(location); + return string.IsNullOrEmpty(assetPath) == false; + } + + private bool IsNeedDownloadFromRemoteInternal(AssetInfo assetInfo) + { + if (assetInfo.IsInvalid) + { + YooLogger.Warning(assetInfo.Error); + return false; + } + + BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo); + if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + return true; + + BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo); + foreach (var depend in depends) + { + if (depend.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) + return true; + } + + return false; + } + #endregion + + #region 原生文件 + /// + /// 同步加载原生文件 + /// + /// 资源信息 + public RawFileHandle LoadRawFileSync(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return LoadRawFileInternal(assetInfo, true, 0); + } + + /// + /// 同步加载原生文件 + /// + /// 资源的定位地址 + public RawFileHandle LoadRawFileSync(string location) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); + return LoadRawFileInternal(assetInfo, true, 0); + } + + /// + /// 异步加载原生文件 + /// + /// 资源信息 + /// 加载的优先级 + public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckInitialize(); + return LoadRawFileInternal(assetInfo, false, priority); + } + + /// + /// 异步加载原生文件 + /// + /// 资源的定位地址 + /// 加载的优先级 + public RawFileHandle LoadRawFileAsync(string location, uint priority = 0) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); + return LoadRawFileInternal(assetInfo, false, priority); + } + + + private RawFileHandle LoadRawFileInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) + { + DebugCheckRawFileLoadMethod(nameof(LoadRawFileAsync)); + var handle = _resourceMgr.LoadRawFileAsync(assetInfo, priority); + if (waitForAsyncComplete) + handle.WaitForAsyncComplete(); + return handle; + } + #endregion + + #region 场景加载 + /// + /// 异步加载场景 + /// + /// 场景的定位地址 + /// 场景加载模式 + /// 场景加载到90%自动挂起 + /// 加载的优先级 + public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); + var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); + return handle; + } + + /// + /// 异步加载场景 + /// + /// 场景的资源信息 + /// 场景加载模式 + /// 场景加载到90%自动挂起 + /// 加载的优先级 + public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) + { + DebugCheckInitialize(); + var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); + return handle; + } + #endregion + + #region 资源加载 + /// + /// 同步加载资源对象 + /// + /// 资源信息 + public AssetHandle LoadAssetSync(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return LoadAssetInternal(assetInfo, true, 0); + } + + /// + /// 同步加载资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public AssetHandle LoadAssetSync(string location) where TObject : UnityEngine.Object + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); + return LoadAssetInternal(assetInfo, true, 0); + } + + /// + /// 同步加载资源对象 + /// + /// 资源的定位地址 + /// 资源类型 + public AssetHandle LoadAssetSync(string location, System.Type type) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAssetInternal(assetInfo, true, 0); + } + + /// + /// 同步加载资源对象 + /// + /// 资源的定位地址 + public AssetHandle LoadAssetSync(string location) + { + DebugCheckInitialize(); + Type type = typeof(UnityEngine.Object); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAssetInternal(assetInfo, true, 0); + } + + + /// + /// 异步加载资源对象 + /// + /// 资源信息 + /// 加载的优先级 + public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckInitialize(); + return LoadAssetInternal(assetInfo, false, priority); + } + + /// + /// 异步加载资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + /// 加载的优先级 + public AssetHandle LoadAssetAsync(string location, uint priority = 0) where TObject : UnityEngine.Object + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); + return LoadAssetInternal(assetInfo, false, priority); + } + + /// + /// 异步加载资源对象 + /// + /// 资源的定位地址 + /// 资源类型 + /// 加载的优先级 + public AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAssetInternal(assetInfo, false, priority); + } + + /// + /// 异步加载资源对象 + /// + /// 资源的定位地址 + /// 加载的优先级 + public AssetHandle LoadAssetAsync(string location, uint priority = 0) + { + DebugCheckInitialize(); + Type type = typeof(UnityEngine.Object); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAssetInternal(assetInfo, false, priority); + } + + + private AssetHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) + { + DebugCheckAssetLoadMethod(nameof(LoadAssetAsync)); + DebugCheckAssetLoadType(assetInfo.AssetType); + var handle = _resourceMgr.LoadAssetAsync(assetInfo, priority); + if (waitForAsyncComplete) + handle.WaitForAsyncComplete(); + return handle; + } + #endregion + + #region 资源加载 + /// + /// 同步加载子资源对象 + /// + /// 资源信息 + public SubAssetsHandle LoadSubAssetsSync(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return LoadSubAssetsInternal(assetInfo, true, 0); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public SubAssetsHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); + return LoadSubAssetsInternal(assetInfo, true, 0); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public SubAssetsHandle LoadSubAssetsSync(string location, System.Type type) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadSubAssetsInternal(assetInfo, true, 0); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源的定位地址 + public SubAssetsHandle LoadSubAssetsSync(string location) + { + DebugCheckInitialize(); + Type type = typeof(UnityEngine.Object); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadSubAssetsInternal(assetInfo, true, 0); + } + + + /// + /// 异步加载子资源对象 + /// + /// 资源信息 + /// 加载的优先级 + public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckInitialize(); + return LoadSubAssetsInternal(assetInfo, false, priority); + } + + /// + /// 异步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + /// 加载的优先级 + public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); + return LoadSubAssetsInternal(assetInfo, false, priority); + } + + /// + /// 异步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + /// 加载的优先级 + public SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadSubAssetsInternal(assetInfo, false, priority); + } + + /// + /// 异步加载子资源对象 + /// + /// 资源的定位地址 + /// 加载的优先级 + public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) + { + DebugCheckInitialize(); + Type type = typeof(UnityEngine.Object); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadSubAssetsInternal(assetInfo, false, priority); + } + + + private SubAssetsHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) + { + DebugCheckAssetLoadMethod(nameof(LoadSubAssetsAsync)); + DebugCheckAssetLoadType(assetInfo.AssetType); + var handle = _resourceMgr.LoadSubAssetsAsync(assetInfo, priority); + if (waitForAsyncComplete) + handle.WaitForAsyncComplete(); + return handle; + } + #endregion + + #region 资源加载 + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源信息 + public AllAssetsHandle LoadAllAssetsSync(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return LoadAllAssetsInternal(assetInfo, true, 0); + } + + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public AllAssetsHandle LoadAllAssetsSync(string location) where TObject : UnityEngine.Object + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); + return LoadAllAssetsInternal(assetInfo, true, 0); + } + + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public AllAssetsHandle LoadAllAssetsSync(string location, System.Type type) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAllAssetsInternal(assetInfo, true, 0); + } + + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + public AllAssetsHandle LoadAllAssetsSync(string location) + { + DebugCheckInitialize(); + Type type = typeof(UnityEngine.Object); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAllAssetsInternal(assetInfo, true, 0); + } + + + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源信息 + /// 加载的优先级 + public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckInitialize(); + return LoadAllAssetsInternal(assetInfo, false, priority); + } + + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + /// 加载的优先级 + public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); + return LoadAllAssetsInternal(assetInfo, false, priority); + } + + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + /// 加载的优先级 + public AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0) + { + DebugCheckInitialize(); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAllAssetsInternal(assetInfo, false, priority); + } + + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + /// 加载的优先级 + public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) + { + DebugCheckInitialize(); + Type type = typeof(UnityEngine.Object); + AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); + return LoadAllAssetsInternal(assetInfo, false, priority); + } + + + private AllAssetsHandle LoadAllAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority) + { + DebugCheckAssetLoadMethod(nameof(LoadAllAssetsAsync)); + DebugCheckAssetLoadType(assetInfo.AssetType); + var handle = _resourceMgr.LoadAllAssetsAsync(assetInfo, priority); + if (waitForAsyncComplete) + handle.WaitForAsyncComplete(); + return handle; + } + #endregion + + #region 资源下载 + /// + /// 创建资源下载器,用于下载当前资源版本所有的资源包文件 + /// + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceDownloaderByAll(downloadingMaxNumber, failedTryAgain, timeout); + } + + /// + /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件 + /// + /// 资源标签 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceDownloaderByTags(new string[] { tag }, downloadingMaxNumber, failedTryAgain, timeout); + } + + /// + /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件 + /// + /// 资源标签列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain, timeout); + } + + /// + /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 + /// + /// 资源的定位地址 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + var assetInfo = ConvertLocationToAssetInfo(location, null); + AssetInfo[] assetInfos = new AssetInfo[] { assetInfo }; + return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout); + } + + /// + /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 + /// + /// 资源的定位地址列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + List assetInfos = new List(locations.Length); + foreach (var location in locations) + { + var assetInfo = ConvertLocationToAssetInfo(location, null); + assetInfos.Add(assetInfo); + } + return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos.ToArray(), downloadingMaxNumber, failedTryAgain, timeout); + } + + /// + /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 + /// + /// 资源信息 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + AssetInfo[] assetInfos = new AssetInfo[] { assetInfo }; + return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout); + } + + /// + /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 + /// + /// 资源信息列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + /// 超时时间 + public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout = 60) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout); + } + #endregion + + #region 资源解压 + /// + /// 创建内置资源解压器,用于解压当前资源版本所有的资源包文件 + /// + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceUnpackerByAll(unpackingMaxNumber, failedTryAgain, int.MaxValue); + } + + /// + /// 创建内置资源解压器,用于解压指定的资源标签关联的资源包文件 + /// + /// 资源标签 + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceUnpackerByTags(new string[] { tag }, unpackingMaxNumber, failedTryAgain, int.MaxValue); + } + + /// + /// 创建内置资源解压器,用于解压指定的资源标签列表关联的资源包文件 + /// + /// 资源标签列表 + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain, int.MaxValue); + } + #endregion + + #region 资源导入 + /// + /// 创建资源导入器 + /// 注意:资源文件名称必须和资源服务器部署的文件名称一致! + /// + /// 资源路径列表 + /// 同时导入的最大文件数 + /// 导入失败的重试次数 + public ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain) + { + DebugCheckInitialize(); + return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain, int.MaxValue); + } + #endregion + + #region 内部方法 + /// + /// 是否包含资源文件 + /// + internal bool IsIncludeBundleFile(string cacheGUID) + { + // NOTE : 编辑器模拟模式下始终返回TRUE + if (_playMode == EPlayMode.EditorSimulateMode) + return true; + return _playModeImpl.ActiveManifest.IsIncludeBundleFile(cacheGUID); + } + + private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) + { + return _playModeImpl.ActiveManifest.ConvertLocationToAssetInfo(location, assetType); + } + private AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType) + { + return _playModeImpl.ActiveManifest.ConvertAssetGUIDToAssetInfo(assetGUID, assetType); + } + #endregion + + #region 调试方法 + [Conditional("DEBUG")] + private void DebugCheckInitialize(bool checkActiveManifest = true) + { + if (_initializeStatus == EOperationStatus.None) + throw new Exception("Package initialize not completed !"); + else if (_initializeStatus == EOperationStatus.Failed) + throw new Exception($"Package initialize failed ! {_initializeError}"); + + if (checkActiveManifest) + { + if (_playModeImpl.ActiveManifest == null) + throw new Exception("Not found active package manifest !"); + } + } + + [Conditional("DEBUG")] + private void DebugCheckRawFileLoadMethod(string method) + { + if (_playModeImpl.ActiveManifest.BuildPipeline != EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) + { + throw new Exception($"Cannot load asset bundle file using {method} method !"); + } + } + + [Conditional("DEBUG")] + private void DebugCheckAssetLoadMethod(string method) + { + if (_playModeImpl.ActiveManifest.BuildPipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString()) + { + throw new Exception($"Cannot load raw file using {method} method !"); + } + } + + [Conditional("DEBUG")] + private void DebugCheckAssetLoadType(System.Type type) + { + if (type == null) + return; + + if (typeof(UnityEngine.Behaviour).IsAssignableFrom(type)) + { + throw new Exception($"Load asset type is invalid : {type.FullName} !"); + } + + if (typeof(UnityEngine.Object).IsAssignableFrom(type) == false) + { + throw new Exception($"Load asset type is invalid : {type.FullName} !"); + } + } + #endregion + + #region 调试信息 + internal DebugPackageData GetDebugPackageData() + { + DebugPackageData data = new DebugPackageData(); + data.PackageName = PackageName; + data.ProviderInfos = _resourceMgr.GetDebugReportInfos(); + return data; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs index 28ac565..a85877e 100644 --- a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs @@ -3,42 +3,42 @@ using UnityEngine; namespace YooAsset { - /// - /// 解密文件的信息 - /// - public struct DecryptFileInfo - { - /// - /// 资源包名称 - /// - public string BundleName; + /// + /// 解密文件的信息 + /// + public struct DecryptFileInfo + { + /// + /// 资源包名称 + /// + public string BundleName; - /// - /// 文件加载路径 - /// - public string FileLoadPath; + /// + /// 文件加载路径 + /// + public string FileLoadPath; - /// - /// Unity引擎用于内容校验的CRC - /// - public uint ConentCRC; - } + /// + /// Unity引擎用于内容校验的CRC + /// + public uint ConentCRC; + } - /// - /// 解密类服务接口 - /// - public interface IDecryptionServices - { - /// - /// 同步方式获取解密的资源包对象 - /// 注意:加载流对象在资源包对象释放的时候会自动释放 - /// - AssetBundle LoadAssetBundle(DecryptFileInfo fileInfo, out Stream managedStream); + /// + /// 解密类服务接口 + /// + public interface IDecryptionServices + { + /// + /// 同步方式获取解密的资源包对象 + /// 注意:加载流对象在资源包对象释放的时候会自动释放 + /// + AssetBundle LoadAssetBundle(DecryptFileInfo fileInfo, out Stream managedStream); - /// - /// 异步方式获取解密的资源包对象 - /// 注意:加载流对象在资源包对象释放的时候会自动释放 - /// - AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream); - } + /// + /// 异步方式获取解密的资源包对象 + /// 注意:加载流对象在资源包对象释放的时候会自动释放 + /// + AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IDeliveryLoadServices.cs b/Assets/YooAsset/Runtime/Services/IDeliveryLoadServices.cs index ada7a3a..f6c84d4 100644 --- a/Assets/YooAsset/Runtime/Services/IDeliveryLoadServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDeliveryLoadServices.cs @@ -2,42 +2,42 @@ namespace YooAsset { - /// - /// 分发的资源信息 - /// - public struct DeliveryFileInfo - { - /// - /// 资源包名称 - /// - public string BundleName; + /// + /// 分发的资源信息 + /// + public struct DeliveryFileInfo + { + /// + /// 资源包名称 + /// + public string BundleName; - /// - /// 文件加载路径 - /// - public string FileLoadPath; + /// + /// 文件加载路径 + /// + public string FileLoadPath; - /// - /// Unity引擎用于内容校验的CRC - /// - public uint ConentCRC; + /// + /// Unity引擎用于内容校验的CRC + /// + public uint ConentCRC; - /// - /// 资源包是否加密 - /// - public bool Encrypted; - } - - public interface IDeliveryLoadServices - { - /// - /// 同步方式获取分发的资源包对象 - /// - AssetBundle LoadAssetBundle(DeliveryFileInfo fileInfo); + /// + /// 资源包是否加密 + /// + public bool Encrypted; + } - /// - /// 异步方式获取分发的资源包对象 - /// - AssetBundleCreateRequest LoadAssetBundleAsync(DeliveryFileInfo fileInfo); - } + public interface IDeliveryLoadServices + { + /// + /// 同步方式获取分发的资源包对象 + /// + AssetBundle LoadAssetBundle(DeliveryFileInfo fileInfo); + + /// + /// 异步方式获取分发的资源包对象 + /// + AssetBundleCreateRequest LoadAssetBundleAsync(DeliveryFileInfo fileInfo); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IEncryptionServices.cs b/Assets/YooAsset/Runtime/Services/IEncryptionServices.cs index f22c289..cc68a1f 100644 --- a/Assets/YooAsset/Runtime/Services/IEncryptionServices.cs +++ b/Assets/YooAsset/Runtime/Services/IEncryptionServices.cs @@ -1,37 +1,37 @@  namespace YooAsset { - public struct EncryptResult - { - /// - /// 文件是否加密 - /// - public bool Encrypted; - - /// - /// 加密后的文件数据 - /// - public byte[] EncryptedData; - } + public struct EncryptResult + { + /// + /// 文件是否加密 + /// + public bool Encrypted; - public struct EncryptFileInfo - { - /// - /// 资源包名称 - /// - public string BundleName; + /// + /// 加密后的文件数据 + /// + public byte[] EncryptedData; + } - /// - /// 文件路径 - /// - public string FilePath; - } + public struct EncryptFileInfo + { + /// + /// 资源包名称 + /// + public string BundleName; - /// - /// 加密服务类接口 - /// - public interface IEncryptionServices - { - EncryptResult Encrypt(EncryptFileInfo fileInfo); - } + /// + /// 文件路径 + /// + public string FilePath; + } + + /// + /// 加密服务类接口 + /// + public interface IEncryptionServices + { + EncryptResult Encrypt(EncryptFileInfo fileInfo); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IRemoteServices.cs b/Assets/YooAsset/Runtime/Services/IRemoteServices.cs index 5debd30..041b206 100644 --- a/Assets/YooAsset/Runtime/Services/IRemoteServices.cs +++ b/Assets/YooAsset/Runtime/Services/IRemoteServices.cs @@ -1,18 +1,18 @@  namespace YooAsset { - public interface IRemoteServices - { - /// - /// 获取主资源站的资源地址 - /// - /// 请求的文件名称 - string GetRemoteMainURL(string fileName); + public interface IRemoteServices + { + /// + /// 获取主资源站的资源地址 + /// + /// 请求的文件名称 + string GetRemoteMainURL(string fileName); - /// - /// 获取备用资源站的资源地址 - /// - /// 请求的文件名称 - string GetRemoteFallbackURL(string fileName); - } + /// + /// 获取备用资源站的资源地址 + /// + /// 请求的文件名称 + string GetRemoteFallbackURL(string fileName); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Settings/YooAssetSettings.cs b/Assets/YooAsset/Runtime/Settings/YooAssetSettings.cs index b2e7d00..99b18ad 100644 --- a/Assets/YooAsset/Runtime/Settings/YooAssetSettings.cs +++ b/Assets/YooAsset/Runtime/Settings/YooAssetSettings.cs @@ -2,70 +2,70 @@ namespace YooAsset { - [CreateAssetMenu(fileName = "YooAssetSettings", menuName = "YooAsset/Create YooAsset Settings")] - internal class YooAssetSettings : ScriptableObject - { - /// - /// 清单文件名称 - /// - public string ManifestFileName = "PackageManifest"; + [CreateAssetMenu(fileName = "YooAssetSettings", menuName = "YooAsset/Create YooAsset Settings")] + internal class YooAssetSettings : ScriptableObject + { + /// + /// 清单文件名称 + /// + public string ManifestFileName = "PackageManifest"; - /// - /// 默认的YooAsset文件夹名称 - /// - public string DefaultYooFolderName = "yoo"; + /// + /// 默认的YooAsset文件夹名称 + /// + public string DefaultYooFolderName = "yoo"; - /// - /// 清单文件头标记 - /// - public const uint ManifestFileSign = 0x594F4F; + /// + /// 清单文件头标记 + /// + public const uint ManifestFileSign = 0x594F4F; - /// - /// 清单文件极限大小(100MB) - /// - public const int ManifestFileMaxSize = 104857600; + /// + /// 清单文件极限大小(100MB) + /// + public const int ManifestFileMaxSize = 104857600; - /// - /// 清单文件格式版本 - /// - public const string ManifestFileVersion = "2.0.0"; + /// + /// 清单文件格式版本 + /// + public const string ManifestFileVersion = "2.0.0"; - /// - /// 缓存的数据文件名称 - /// - public const string CacheBundleDataFileName = "__data"; + /// + /// 缓存的数据文件名称 + /// + public const string CacheBundleDataFileName = "__data"; - /// - /// 缓存的信息文件名称 - /// - public const string CacheBundleInfoFileName = "__info"; + /// + /// 缓存的信息文件名称 + /// + public const string CacheBundleInfoFileName = "__info"; - /// - /// 缓存的资源文件的文件夹名称 - /// - public const string CacheFilesFolderName = "CacheFiles"; - - /// - /// 缓存的清单文件的文件夹名称 - /// - public const string ManifestFolderName = "ManifestFiles"; + /// + /// 缓存的资源文件的文件夹名称 + /// + public const string CacheFilesFolderName = "CacheFiles"; - /// - /// 记录应用程序版本的文件名称 - /// - public const string AppFootPrintFileName = "ApplicationFootPrint.bytes"; + /// + /// 缓存的清单文件的文件夹名称 + /// + public const string ManifestFolderName = "ManifestFiles"; + + /// + /// 记录应用程序版本的文件名称 + /// + public const string AppFootPrintFileName = "ApplicationFootPrint.bytes"; - /// - /// 构建输出文件夹名称 - /// - public const string OutputFolderName = "OutputCache"; + /// + /// 构建输出文件夹名称 + /// + public const string OutputFolderName = "OutputCache"; - /// - /// 构建输出的报告文件 - /// - public const string ReportFileName = "BuildReport"; - } + /// + /// 构建输出的报告文件 + /// + public const string ReportFileName = "BuildReport"; + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs b/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs index 16ac6b8..9a897ee 100644 --- a/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs +++ b/Assets/YooAsset/Runtime/Settings/YooAssetSettingsData.cs @@ -2,74 +2,74 @@ namespace YooAsset { - internal static class YooAssetSettingsData - { - private static YooAssetSettings _setting = null; - public static YooAssetSettings Setting - { - get - { - if (_setting == null) - LoadSettingData(); - return _setting; - } - } + internal static class YooAssetSettingsData + { + private static YooAssetSettings _setting = null; + public static YooAssetSettings Setting + { + get + { + if (_setting == null) + LoadSettingData(); + return _setting; + } + } - /// - /// 加载配置文件 - /// - private static void LoadSettingData() - { - _setting = Resources.Load("YooAssetSettings"); - if (_setting == null) - { - YooLogger.Log("YooAsset use default settings."); - _setting = ScriptableObject.CreateInstance(); - } - else - { - YooLogger.Log("YooAsset use user settings."); - } - } + /// + /// 加载配置文件 + /// + private static void LoadSettingData() + { + _setting = Resources.Load("YooAssetSettings"); + if (_setting == null) + { + YooLogger.Log("YooAsset use default settings."); + _setting = ScriptableObject.CreateInstance(); + } + else + { + YooLogger.Log("YooAsset use user settings."); + } + } - /// - /// 获取构建报告文件名 - /// - public static string GetReportFileName(string packageName, string packageVersion) - { - return $"{YooAssetSettings.ReportFileName}_{packageName}_{packageVersion}.json"; - } + /// + /// 获取构建报告文件名 + /// + public static string GetReportFileName(string packageName, string packageVersion) + { + return $"{YooAssetSettings.ReportFileName}_{packageName}_{packageVersion}.json"; + } - /// - /// 获取清单文件完整名称 - /// - public static string GetManifestBinaryFileName(string packageName, string packageVersion) - { - return $"{Setting.ManifestFileName}_{packageName}_{packageVersion}.bytes"; - } + /// + /// 获取清单文件完整名称 + /// + public static string GetManifestBinaryFileName(string packageName, string packageVersion) + { + return $"{Setting.ManifestFileName}_{packageName}_{packageVersion}.bytes"; + } - /// - /// 获取清单文件完整名称 - /// - public static string GetManifestJsonFileName(string packageName, string packageVersion) - { - return $"{Setting.ManifestFileName}_{packageName}_{packageVersion}.json"; - } + /// + /// 获取清单文件完整名称 + /// + public static string GetManifestJsonFileName(string packageName, string packageVersion) + { + return $"{Setting.ManifestFileName}_{packageName}_{packageVersion}.json"; + } - /// - /// 获取包裹的哈希文件完整名称 - /// - public static string GetPackageHashFileName(string packageName, string packageVersion) - { - return $"{Setting.ManifestFileName}_{packageName}_{packageVersion}.hash"; - } + /// + /// 获取包裹的哈希文件完整名称 + /// + public static string GetPackageHashFileName(string packageName, string packageVersion) + { + return $"{Setting.ManifestFileName}_{packageName}_{packageVersion}.hash"; + } - /// - /// 获取包裹的版本文件完整名称 - /// - public static string GetPackageVersionFileName(string packageName) - { - return $"{Setting.ManifestFileName}_{packageName}.version"; - } - } + /// + /// 获取包裹的版本文件完整名称 + /// + public static string GetPackageVersionFileName(string packageName) + { + return $"{Setting.ManifestFileName}_{packageName}.version"; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Utility/BufferReader.cs b/Assets/YooAsset/Runtime/Utility/BufferReader.cs index bd37abb..3a54c58 100644 --- a/Assets/YooAsset/Runtime/Utility/BufferReader.cs +++ b/Assets/YooAsset/Runtime/Utility/BufferReader.cs @@ -6,169 +6,169 @@ using System.Diagnostics; namespace YooAsset { - internal class BufferReader - { - private readonly byte[] _buffer; - private int _index = 0; + internal class BufferReader + { + private readonly byte[] _buffer; + private int _index = 0; - public BufferReader(byte[] data) - { - _buffer = data; - } + public BufferReader(byte[] data) + { + _buffer = data; + } - /// - /// 是否有效 - /// - public bool IsValid - { - get - { - if (_buffer == null || _buffer.Length == 0) - return false; - else - return true; - } - } + /// + /// 是否有效 + /// + public bool IsValid + { + get + { + if (_buffer == null || _buffer.Length == 0) + return false; + else + return true; + } + } - /// - /// 缓冲区容量 - /// - public int Capacity - { - get { return _buffer.Length; } - } + /// + /// 缓冲区容量 + /// + public int Capacity + { + get { return _buffer.Length; } + } - public byte[] ReadBytes(int count) - { - CheckReaderIndex(count); - var data = new byte[count]; - Buffer.BlockCopy(_buffer, _index, data, 0, count); - _index += count; - return data; - } - public byte ReadByte() - { - CheckReaderIndex(1); - return _buffer[_index++]; - } + public byte[] ReadBytes(int count) + { + CheckReaderIndex(count); + var data = new byte[count]; + Buffer.BlockCopy(_buffer, _index, data, 0, count); + _index += count; + return data; + } + public byte ReadByte() + { + CheckReaderIndex(1); + return _buffer[_index++]; + } - public bool ReadBool() - { - CheckReaderIndex(1); - return _buffer[_index++] == 1; - } - public short ReadInt16() - { - CheckReaderIndex(2); - if (BitConverter.IsLittleEndian) - { - short value = (short)((_buffer[_index]) | (_buffer[_index + 1] << 8)); - _index += 2; - return value; - } - else - { - short value = (short)((_buffer[_index] << 8) | (_buffer[_index + 1])); - _index += 2; - return value; - } - } - public ushort ReadUInt16() - { - return (ushort)ReadInt16(); - } - public int ReadInt32() - { - CheckReaderIndex(4); - if (BitConverter.IsLittleEndian) - { - int value = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24); - _index += 4; - return value; - } - else - { - int value = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]); - _index += 4; - return value; - } - } - public uint ReadUInt32() - { - return (uint)ReadInt32(); - } - public long ReadInt64() - { - CheckReaderIndex(8); - if (BitConverter.IsLittleEndian) - { - int i1 = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24); - int i2 = (_buffer[_index + 4]) | (_buffer[_index + 5] << 8) | (_buffer[_index + 6] << 16) | (_buffer[_index + 7] << 24); - _index += 8; - return (uint)i1 | ((long)i2 << 32); - } - else - { - int i1 = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]); - int i2 = (_buffer[_index + 4] << 24) | (_buffer[_index + 5] << 16) | (_buffer[_index + 6] << 8) | (_buffer[_index + 7]); - _index += 8; - return (uint)i2 | ((long)i1 << 32); - } - } - public ulong ReadUInt64() - { - return (ulong)ReadInt64(); - } + public bool ReadBool() + { + CheckReaderIndex(1); + return _buffer[_index++] == 1; + } + public short ReadInt16() + { + CheckReaderIndex(2); + if (BitConverter.IsLittleEndian) + { + short value = (short)((_buffer[_index]) | (_buffer[_index + 1] << 8)); + _index += 2; + return value; + } + else + { + short value = (short)((_buffer[_index] << 8) | (_buffer[_index + 1])); + _index += 2; + return value; + } + } + public ushort ReadUInt16() + { + return (ushort)ReadInt16(); + } + public int ReadInt32() + { + CheckReaderIndex(4); + if (BitConverter.IsLittleEndian) + { + int value = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24); + _index += 4; + return value; + } + else + { + int value = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]); + _index += 4; + return value; + } + } + public uint ReadUInt32() + { + return (uint)ReadInt32(); + } + public long ReadInt64() + { + CheckReaderIndex(8); + if (BitConverter.IsLittleEndian) + { + int i1 = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24); + int i2 = (_buffer[_index + 4]) | (_buffer[_index + 5] << 8) | (_buffer[_index + 6] << 16) | (_buffer[_index + 7] << 24); + _index += 8; + return (uint)i1 | ((long)i2 << 32); + } + else + { + int i1 = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]); + int i2 = (_buffer[_index + 4] << 24) | (_buffer[_index + 5] << 16) | (_buffer[_index + 6] << 8) | (_buffer[_index + 7]); + _index += 8; + return (uint)i2 | ((long)i1 << 32); + } + } + public ulong ReadUInt64() + { + return (ulong)ReadInt64(); + } - public string ReadUTF8() - { - ushort count = ReadUInt16(); - if (count == 0) - return string.Empty; + public string ReadUTF8() + { + ushort count = ReadUInt16(); + if (count == 0) + return string.Empty; - CheckReaderIndex(count); - string value = Encoding.UTF8.GetString(_buffer, _index, count); - _index += count; - return value; - } - public int[] ReadInt32Array() - { - ushort count = ReadUInt16(); - int[] values = new int[count]; - for (int i = 0; i < count; i++) - { - values[i] = ReadInt32(); - } - return values; - } - public long[] ReadInt64Array() - { - ushort count = ReadUInt16(); - long[] values = new long[count]; - for (int i = 0; i < count; i++) - { - values[i] = ReadInt64(); - } - return values; - } - public string[] ReadUTF8Array() - { - ushort count = ReadUInt16(); - string[] values = new string[count]; - for (int i = 0; i < count; i++) - { - values[i] = ReadUTF8(); - } - return values; - } + CheckReaderIndex(count); + string value = Encoding.UTF8.GetString(_buffer, _index, count); + _index += count; + return value; + } + public int[] ReadInt32Array() + { + ushort count = ReadUInt16(); + int[] values = new int[count]; + for (int i = 0; i < count; i++) + { + values[i] = ReadInt32(); + } + return values; + } + public long[] ReadInt64Array() + { + ushort count = ReadUInt16(); + long[] values = new long[count]; + for (int i = 0; i < count; i++) + { + values[i] = ReadInt64(); + } + return values; + } + public string[] ReadUTF8Array() + { + ushort count = ReadUInt16(); + string[] values = new string[count]; + for (int i = 0; i < count; i++) + { + values[i] = ReadUTF8(); + } + return values; + } - [Conditional("DEBUG")] - private void CheckReaderIndex(int length) - { - if (_index + length > Capacity) - { - throw new IndexOutOfRangeException(); - } - } - } + [Conditional("DEBUG")] + private void CheckReaderIndex(int length) + { + if (_index + length > Capacity) + { + throw new IndexOutOfRangeException(); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Utility/BufferWriter.cs b/Assets/YooAsset/Runtime/Utility/BufferWriter.cs index 940b438..553b62f 100644 --- a/Assets/YooAsset/Runtime/Utility/BufferWriter.cs +++ b/Assets/YooAsset/Runtime/Utility/BufferWriter.cs @@ -7,181 +7,181 @@ using System.IO; namespace YooAsset { - /// - /// 数据存储以小端字节序为标准 - /// - internal class BufferWriter - { - private readonly byte[] _buffer; - private int _index = 0; + /// + /// 数据存储以小端字节序为标准 + /// + internal class BufferWriter + { + private readonly byte[] _buffer; + private int _index = 0; - public BufferWriter(int capacity) - { - _buffer = new byte[capacity]; - } + public BufferWriter(int capacity) + { + _buffer = new byte[capacity]; + } - /// - /// 缓冲区容量 - /// - public int Capacity - { - get { return _buffer.Length; } - } + /// + /// 缓冲区容量 + /// + public int Capacity + { + get { return _buffer.Length; } + } - /// - /// 清空缓冲区 - /// - public void Clear() - { - _index = 0; - } + /// + /// 清空缓冲区 + /// + public void Clear() + { + _index = 0; + } - /// - /// 将有效数据写入文件流 - /// - public void WriteToStream(FileStream fileStream) - { - fileStream.Write(_buffer, 0, _index); - } + /// + /// 将有效数据写入文件流 + /// + public void WriteToStream(FileStream fileStream) + { + fileStream.Write(_buffer, 0, _index); + } - public void WriteBytes(byte[] data) - { - int count = data.Length; - CheckWriterIndex(count); - Buffer.BlockCopy(data, 0, _buffer, _index, count); - _index += count; - } - public void WriteByte(byte value) - { - CheckWriterIndex(1); - _buffer[_index++] = value; - } + public void WriteBytes(byte[] data) + { + int count = data.Length; + CheckWriterIndex(count); + Buffer.BlockCopy(data, 0, _buffer, _index, count); + _index += count; + } + public void WriteByte(byte value) + { + CheckWriterIndex(1); + _buffer[_index++] = value; + } - public void WriteBool(bool value) - { - WriteByte((byte)(value ? 1 : 0)); - } - public void WriteInt16(short value) - { - WriteUInt16((ushort)value); - } - public void WriteUInt16(ushort value) - { - CheckWriterIndex(2); - _buffer[_index++] = (byte)value; - _buffer[_index++] = (byte)(value >> 8); - } - public void WriteInt32(int value) - { - WriteUInt32((uint)value); - } - public void WriteUInt32(uint value) - { - CheckWriterIndex(4); - _buffer[_index++] = (byte)value; - _buffer[_index++] = (byte)(value >> 8); - _buffer[_index++] = (byte)(value >> 16); - _buffer[_index++] = (byte)(value >> 24); - } - public void WriteInt64(long value) - { - WriteUInt64((ulong)value); - } - public void WriteUInt64(ulong value) - { - CheckWriterIndex(8); - _buffer[_index++] = (byte)value; - _buffer[_index++] = (byte)(value >> 8); - _buffer[_index++] = (byte)(value >> 16); - _buffer[_index++] = (byte)(value >> 24); - _buffer[_index++] = (byte)(value >> 32); - _buffer[_index++] = (byte)(value >> 40); - _buffer[_index++] = (byte)(value >> 48); - _buffer[_index++] = (byte)(value >> 56); - } + public void WriteBool(bool value) + { + WriteByte((byte)(value ? 1 : 0)); + } + public void WriteInt16(short value) + { + WriteUInt16((ushort)value); + } + public void WriteUInt16(ushort value) + { + CheckWriterIndex(2); + _buffer[_index++] = (byte)value; + _buffer[_index++] = (byte)(value >> 8); + } + public void WriteInt32(int value) + { + WriteUInt32((uint)value); + } + public void WriteUInt32(uint value) + { + CheckWriterIndex(4); + _buffer[_index++] = (byte)value; + _buffer[_index++] = (byte)(value >> 8); + _buffer[_index++] = (byte)(value >> 16); + _buffer[_index++] = (byte)(value >> 24); + } + public void WriteInt64(long value) + { + WriteUInt64((ulong)value); + } + public void WriteUInt64(ulong value) + { + CheckWriterIndex(8); + _buffer[_index++] = (byte)value; + _buffer[_index++] = (byte)(value >> 8); + _buffer[_index++] = (byte)(value >> 16); + _buffer[_index++] = (byte)(value >> 24); + _buffer[_index++] = (byte)(value >> 32); + _buffer[_index++] = (byte)(value >> 40); + _buffer[_index++] = (byte)(value >> 48); + _buffer[_index++] = (byte)(value >> 56); + } - public void WriteUTF8(string value) - { - if (string.IsNullOrEmpty(value)) - { - WriteUInt16(0); - } - else - { - byte[] bytes = Encoding.UTF8.GetBytes(value); - int count = bytes.Length; - if (count > ushort.MaxValue) - throw new FormatException($"Write string length cannot be greater than {ushort.MaxValue} !"); + public void WriteUTF8(string value) + { + if (string.IsNullOrEmpty(value)) + { + WriteUInt16(0); + } + else + { + byte[] bytes = Encoding.UTF8.GetBytes(value); + int count = bytes.Length; + if (count > ushort.MaxValue) + throw new FormatException($"Write string length cannot be greater than {ushort.MaxValue} !"); - WriteUInt16(Convert.ToUInt16(count)); - WriteBytes(bytes); - } - } - public void WriteInt32Array(int[] values) - { - if (values == null) - { - WriteUInt16(0); - } - else - { - int count = values.Length; - if (count > ushort.MaxValue) - throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !"); + WriteUInt16(Convert.ToUInt16(count)); + WriteBytes(bytes); + } + } + public void WriteInt32Array(int[] values) + { + if (values == null) + { + WriteUInt16(0); + } + else + { + int count = values.Length; + if (count > ushort.MaxValue) + throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !"); - WriteUInt16(Convert.ToUInt16(count)); - for (int i = 0; i < count; i++) - { - WriteInt32(values[i]); - } - } - } - public void WriteInt64Array(long[] values) - { - if (values == null) - { - WriteUInt16(0); - } - else - { - int count = values.Length; - if (count > ushort.MaxValue) - throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !"); + WriteUInt16(Convert.ToUInt16(count)); + for (int i = 0; i < count; i++) + { + WriteInt32(values[i]); + } + } + } + public void WriteInt64Array(long[] values) + { + if (values == null) + { + WriteUInt16(0); + } + else + { + int count = values.Length; + if (count > ushort.MaxValue) + throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !"); - WriteUInt16(Convert.ToUInt16(count)); - for (int i = 0; i < count; i++) - { - WriteInt64(values[i]); - } - } - } - public void WriteUTF8Array(string[] values) - { - if (values == null) - { - WriteUInt16(0); - } - else - { - int count = values.Length; - if (count > ushort.MaxValue) - throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !"); + WriteUInt16(Convert.ToUInt16(count)); + for (int i = 0; i < count; i++) + { + WriteInt64(values[i]); + } + } + } + public void WriteUTF8Array(string[] values) + { + if (values == null) + { + WriteUInt16(0); + } + else + { + int count = values.Length; + if (count > ushort.MaxValue) + throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !"); - WriteUInt16(Convert.ToUInt16(count)); - for (int i = 0; i < count; i++) - { - WriteUTF8(values[i]); - } - } - } + WriteUInt16(Convert.ToUInt16(count)); + for (int i = 0; i < count; i++) + { + WriteUTF8(values[i]); + } + } + } - [Conditional("DEBUG")] - private void CheckWriterIndex(int length) - { - if (_index + length > Capacity) - { - throw new IndexOutOfRangeException(); - } - } - } + [Conditional("DEBUG")] + private void CheckWriterIndex(int length) + { + if (_index + length > Capacity) + { + throw new IndexOutOfRangeException(); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs b/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs index 93f38df..b729ea9 100644 --- a/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs +++ b/Assets/YooAsset/Runtime/Utility/CRC32Algorithm.cs @@ -3,240 +3,240 @@ using System.Security.Cryptography; namespace YooAsset { - internal class SafeProxy - { - private const uint Poly = 0xedb88320u; - private readonly uint[] _table = new uint[16 * 256]; + internal class SafeProxy + { + private const uint Poly = 0xedb88320u; + private readonly uint[] _table = new uint[16 * 256]; - internal SafeProxy() - { - Init(Poly); - } - public void Init(uint poly) - { - var table = _table; - for (uint i = 0; i < 256; i++) - { - uint res = i; - for (int t = 0; t < 16; t++) - { - for (int k = 0; k < 8; k++) res = (res & 1) == 1 ? poly ^ (res >> 1) : (res >> 1); - table[(t * 256) + i] = res; - } - } - } - public uint Append(uint crc, byte[] input, int offset, int length) - { - uint crcLocal = uint.MaxValue ^ crc; + internal SafeProxy() + { + Init(Poly); + } + public void Init(uint poly) + { + var table = _table; + for (uint i = 0; i < 256; i++) + { + uint res = i; + for (int t = 0; t < 16; t++) + { + for (int k = 0; k < 8; k++) res = (res & 1) == 1 ? poly ^ (res >> 1) : (res >> 1); + table[(t * 256) + i] = res; + } + } + } + public uint Append(uint crc, byte[] input, int offset, int length) + { + uint crcLocal = uint.MaxValue ^ crc; - uint[] table = _table; - while (length >= 16) - { - var a = table[(3 * 256) + input[offset + 12]] - ^ table[(2 * 256) + input[offset + 13]] - ^ table[(1 * 256) + input[offset + 14]] - ^ table[(0 * 256) + input[offset + 15]]; + uint[] table = _table; + while (length >= 16) + { + var a = table[(3 * 256) + input[offset + 12]] + ^ table[(2 * 256) + input[offset + 13]] + ^ table[(1 * 256) + input[offset + 14]] + ^ table[(0 * 256) + input[offset + 15]]; - var b = table[(7 * 256) + input[offset + 8]] - ^ table[(6 * 256) + input[offset + 9]] - ^ table[(5 * 256) + input[offset + 10]] - ^ table[(4 * 256) + input[offset + 11]]; + var b = table[(7 * 256) + input[offset + 8]] + ^ table[(6 * 256) + input[offset + 9]] + ^ table[(5 * 256) + input[offset + 10]] + ^ table[(4 * 256) + input[offset + 11]]; - var c = table[(11 * 256) + input[offset + 4]] - ^ table[(10 * 256) + input[offset + 5]] - ^ table[(9 * 256) + input[offset + 6]] - ^ table[(8 * 256) + input[offset + 7]]; + var c = table[(11 * 256) + input[offset + 4]] + ^ table[(10 * 256) + input[offset + 5]] + ^ table[(9 * 256) + input[offset + 6]] + ^ table[(8 * 256) + input[offset + 7]]; - var d = table[(15 * 256) + ((byte)crcLocal ^ input[offset])] - ^ table[(14 * 256) + ((byte)(crcLocal >> 8) ^ input[offset + 1])] - ^ table[(13 * 256) + ((byte)(crcLocal >> 16) ^ input[offset + 2])] - ^ table[(12 * 256) + ((crcLocal >> 24) ^ input[offset + 3])]; + var d = table[(15 * 256) + ((byte)crcLocal ^ input[offset])] + ^ table[(14 * 256) + ((byte)(crcLocal >> 8) ^ input[offset + 1])] + ^ table[(13 * 256) + ((byte)(crcLocal >> 16) ^ input[offset + 2])] + ^ table[(12 * 256) + ((crcLocal >> 24) ^ input[offset + 3])]; - crcLocal = d ^ c ^ b ^ a; - offset += 16; - length -= 16; - } + crcLocal = d ^ c ^ b ^ a; + offset += 16; + length -= 16; + } - while (--length >= 0) - crcLocal = table[(byte)(crcLocal ^ input[offset++])] ^ crcLocal >> 8; + while (--length >= 0) + crcLocal = table[(byte)(crcLocal ^ input[offset++])] ^ crcLocal >> 8; - return crcLocal ^ uint.MaxValue; - } - } + return crcLocal ^ uint.MaxValue; + } + } - /// - /// This is .NET safe implementation of Crc32 algorithm. - /// Implementation of CRC-32. - /// This class supports several convenient static methods returning the CRC as UInt32. - /// - internal class CRC32Algorithm : HashAlgorithm - { - private uint _currentCrc; + /// + /// This is .NET safe implementation of Crc32 algorithm. + /// Implementation of CRC-32. + /// This class supports several convenient static methods returning the CRC as UInt32. + /// + internal class CRC32Algorithm : HashAlgorithm + { + private uint _currentCrc; - /// - /// Initializes a new instance of the class. - /// - public CRC32Algorithm() - { + /// + /// Initializes a new instance of the class. + /// + public CRC32Algorithm() + { #if !NETCORE13 - HashSizeValue = 32; + HashSizeValue = 32; #endif - } + } - /// - /// Resets internal state of the algorithm. Used internally. - /// - public override void Initialize() - { - _currentCrc = 0; - } + /// + /// Resets internal state of the algorithm. Used internally. + /// + public override void Initialize() + { + _currentCrc = 0; + } - /// - /// Appends CRC-32 from given buffer - /// - protected override void HashCore(byte[] input, int offset, int length) - { - _currentCrc = AppendInternal(_currentCrc, input, offset, length); - } + /// + /// Appends CRC-32 from given buffer + /// + protected override void HashCore(byte[] input, int offset, int length) + { + _currentCrc = AppendInternal(_currentCrc, input, offset, length); + } - /// - /// Computes CRC-32 from - /// - protected override byte[] HashFinal() - { - if(BitConverter.IsLittleEndian) - return new[] { (byte)_currentCrc, (byte)(_currentCrc >> 8), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 24) }; - else - return new[] { (byte)(_currentCrc >> 24), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 8), (byte)_currentCrc }; - } + /// + /// Computes CRC-32 from + /// + protected override byte[] HashFinal() + { + if (BitConverter.IsLittleEndian) + return new[] { (byte)_currentCrc, (byte)(_currentCrc >> 8), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 24) }; + else + return new[] { (byte)(_currentCrc >> 24), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 8), (byte)_currentCrc }; + } - /// - /// Computes CRC-32 from multiple buffers. - /// Call this method multiple times to chain multiple buffers. - /// - /// - /// Initial CRC value for the algorithm. It is zero for the first buffer. - /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method. - /// - /// Input buffer with data to be checksummed. - /// Offset of the input data within the buffer. - /// Length of the input data in the buffer. - /// Accumulated CRC-32 of all buffers processed so far. - public static uint Append(uint initial, byte[] input, int offset, int length) - { - if (input == null) - throw new ArgumentNullException("input"); - if (offset < 0 || length < 0 || offset + length > input.Length) - throw new ArgumentOutOfRangeException("length"); - return AppendInternal(initial, input, offset, length); - } + /// + /// Computes CRC-32 from multiple buffers. + /// Call this method multiple times to chain multiple buffers. + /// + /// + /// Initial CRC value for the algorithm. It is zero for the first buffer. + /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method. + /// + /// Input buffer with data to be checksummed. + /// Offset of the input data within the buffer. + /// Length of the input data in the buffer. + /// Accumulated CRC-32 of all buffers processed so far. + public static uint Append(uint initial, byte[] input, int offset, int length) + { + if (input == null) + throw new ArgumentNullException("input"); + if (offset < 0 || length < 0 || offset + length > input.Length) + throw new ArgumentOutOfRangeException("length"); + return AppendInternal(initial, input, offset, length); + } - /// - /// Computes CRC-32 from multiple buffers. - /// Call this method multiple times to chain multiple buffers. - /// - /// - /// Initial CRC value for the algorithm. It is zero for the first buffer. - /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method. - /// - /// Input buffer containing data to be checksummed. - /// Accumulated CRC-32 of all buffers processed so far. - public static uint Append(uint initial, byte[] input) - { - if (input == null) - throw new ArgumentNullException(); - return AppendInternal(initial, input, 0, input.Length); - } + /// + /// Computes CRC-32 from multiple buffers. + /// Call this method multiple times to chain multiple buffers. + /// + /// + /// Initial CRC value for the algorithm. It is zero for the first buffer. + /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method. + /// + /// Input buffer containing data to be checksummed. + /// Accumulated CRC-32 of all buffers processed so far. + public static uint Append(uint initial, byte[] input) + { + if (input == null) + throw new ArgumentNullException(); + return AppendInternal(initial, input, 0, input.Length); + } - /// - /// Computes CRC-32 from input buffer. - /// - /// Input buffer with data to be checksummed. - /// Offset of the input data within the buffer. - /// Length of the input data in the buffer. - /// CRC-32 of the data in the buffer. - public static uint Compute(byte[] input, int offset, int length) - { - return Append(0, input, offset, length); - } + /// + /// Computes CRC-32 from input buffer. + /// + /// Input buffer with data to be checksummed. + /// Offset of the input data within the buffer. + /// Length of the input data in the buffer. + /// CRC-32 of the data in the buffer. + public static uint Compute(byte[] input, int offset, int length) + { + return Append(0, input, offset, length); + } - /// - /// Computes CRC-32 from input buffer. - /// - /// Input buffer containing data to be checksummed. - /// CRC-32 of the buffer. - public static uint Compute(byte[] input) - { - return Append(0, input); - } + /// + /// Computes CRC-32 from input buffer. + /// + /// Input buffer containing data to be checksummed. + /// CRC-32 of the buffer. + public static uint Compute(byte[] input) + { + return Append(0, input); + } - /// - /// Computes CRC-32 from input buffer and writes it after end of data (buffer should have 4 bytes reserved space for it). Can be used in conjunction with - /// - /// Input buffer with data to be checksummed. - /// Offset of the input data within the buffer. - /// Length of the input data in the buffer. - /// CRC-32 of the data in the buffer. - public static uint ComputeAndWriteToEnd(byte[] input, int offset, int length) - { - if (length + 4 > input.Length) - throw new ArgumentOutOfRangeException("length", "Length of data should be less than array length - 4 bytes of CRC data"); - var crc = Append(0, input, offset, length); - var r = offset + length; - input[r] = (byte)crc; - input[r + 1] = (byte)(crc >> 8); - input[r + 2] = (byte)(crc >> 16); - input[r + 3] = (byte)(crc >> 24); - return crc; - } + /// + /// Computes CRC-32 from input buffer and writes it after end of data (buffer should have 4 bytes reserved space for it). Can be used in conjunction with + /// + /// Input buffer with data to be checksummed. + /// Offset of the input data within the buffer. + /// Length of the input data in the buffer. + /// CRC-32 of the data in the buffer. + public static uint ComputeAndWriteToEnd(byte[] input, int offset, int length) + { + if (length + 4 > input.Length) + throw new ArgumentOutOfRangeException("length", "Length of data should be less than array length - 4 bytes of CRC data"); + var crc = Append(0, input, offset, length); + var r = offset + length; + input[r] = (byte)crc; + input[r + 1] = (byte)(crc >> 8); + input[r + 2] = (byte)(crc >> 16); + input[r + 3] = (byte)(crc >> 24); + return crc; + } - /// - /// Computes CRC-32 from input buffer - 4 bytes and writes it as last 4 bytes of buffer. Can be used in conjunction with - /// - /// Input buffer with data to be checksummed. - /// CRC-32 of the data in the buffer. - public static uint ComputeAndWriteToEnd(byte[] input) - { - if (input.Length < 4) - throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least"); - return ComputeAndWriteToEnd(input, 0, input.Length - 4); - } + /// + /// Computes CRC-32 from input buffer - 4 bytes and writes it as last 4 bytes of buffer. Can be used in conjunction with + /// + /// Input buffer with data to be checksummed. + /// CRC-32 of the data in the buffer. + public static uint ComputeAndWriteToEnd(byte[] input) + { + if (input.Length < 4) + throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least"); + return ComputeAndWriteToEnd(input, 0, input.Length - 4); + } - /// - /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with - /// - /// Input buffer with data to be checksummed. - /// Offset of the input data within the buffer. - /// Length of the input data in the buffer with CRC-32 bytes. - /// Is checksum valid. - public static bool IsValidWithCrcAtEnd(byte[] input, int offset, int lengthWithCrc) - { - return Append(0, input, offset, lengthWithCrc) == 0x2144DF1C; - } + /// + /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with + /// + /// Input buffer with data to be checksummed. + /// Offset of the input data within the buffer. + /// Length of the input data in the buffer with CRC-32 bytes. + /// Is checksum valid. + public static bool IsValidWithCrcAtEnd(byte[] input, int offset, int lengthWithCrc) + { + return Append(0, input, offset, lengthWithCrc) == 0x2144DF1C; + } - /// - /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with - /// - /// Input buffer with data to be checksummed. - /// Is checksum valid. - public static bool IsValidWithCrcAtEnd(byte[] input) - { - if (input.Length < 4) - throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least"); - return Append(0, input, 0, input.Length) == 0x2144DF1C; - } + /// + /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with + /// + /// Input buffer with data to be checksummed. + /// Is checksum valid. + public static bool IsValidWithCrcAtEnd(byte[] input) + { + if (input.Length < 4) + throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least"); + return Append(0, input, 0, input.Length) == 0x2144DF1C; + } - private static readonly SafeProxy _proxy = new SafeProxy(); - private static uint AppendInternal(uint initial, byte[] input, int offset, int length) - { - if (length > 0) - { - return _proxy.Append(initial, input, offset, length); - } - else - return initial; - } - } + private static readonly SafeProxy _proxy = new SafeProxy(); + private static uint AppendInternal(uint initial, byte[] input, int offset, int length) + { + if (length > 0) + { + return _proxy.Append(initial, input, offset, length); + } + else + return initial; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Utility/YooLogger.cs b/Assets/YooAsset/Runtime/Utility/YooLogger.cs index 5962e95..6bf555c 100644 --- a/Assets/YooAsset/Runtime/Utility/YooLogger.cs +++ b/Assets/YooAsset/Runtime/Utility/YooLogger.cs @@ -2,80 +2,80 @@ namespace YooAsset { - /// - /// 自定义日志处理 - /// - public interface ILogger - { - void Log(string message); - void Warning(string message); - void Error(string message); - void Exception(System.Exception exception); - } + /// + /// 自定义日志处理 + /// + public interface ILogger + { + void Log(string message); + void Warning(string message); + void Error(string message); + void Exception(System.Exception exception); + } - internal static class YooLogger - { - public static ILogger Logger = null; + internal static class YooLogger + { + public static ILogger Logger = null; - /// - /// 日志 - /// - [Conditional("DEBUG")] - public static void Log(string info) - { - if (Logger != null) - { - Logger.Log(info); - } - else - { - UnityEngine.Debug.Log(info); - } - } + /// + /// 日志 + /// + [Conditional("DEBUG")] + public static void Log(string info) + { + if (Logger != null) + { + Logger.Log(info); + } + else + { + UnityEngine.Debug.Log(info); + } + } - /// - /// 警告 - /// - public static void Warning(string info) - { - if (Logger != null) - { - Logger.Warning(info); - } - else - { - UnityEngine.Debug.LogWarning(info); - } - } + /// + /// 警告 + /// + public static void Warning(string info) + { + if (Logger != null) + { + Logger.Warning(info); + } + else + { + UnityEngine.Debug.LogWarning(info); + } + } - /// - /// 错误 - /// - public static void Error(string info) - { - if (Logger != null) - { - Logger.Error(info); - } - else - { - UnityEngine.Debug.LogError(info); - } - } + /// + /// 错误 + /// + public static void Error(string info) + { + if (Logger != null) + { + Logger.Error(info); + } + else + { + UnityEngine.Debug.LogError(info); + } + } - /// - /// 异常 - /// - public static void Exception(System.Exception exception) - { - if (Logger != null) - { - Logger.Exception(exception); - } - else - { - UnityEngine.Debug.LogException(exception); - } - } - } + /// + /// 异常 + /// + public static void Exception(System.Exception exception) + { + if (Logger != null) + { + Logger.Exception(exception); + } + else + { + UnityEngine.Debug.LogException(exception); + } + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Utility/YooUtility.cs b/Assets/YooAsset/Runtime/Utility/YooUtility.cs index f1dba37..6c1132a 100644 --- a/Assets/YooAsset/Runtime/Utility/YooUtility.cs +++ b/Assets/YooAsset/Runtime/Utility/YooUtility.cs @@ -7,349 +7,349 @@ using System.Security.Cryptography; namespace YooAsset { - /// - /// 路径工具类 - /// - internal static class PathUtility - { - /// - /// 路径归一化 - /// 注意:替换为Linux路径格式 - /// - public static string RegularPath(string path) - { - return path.Replace('\\', '/').Replace("\\", "/"); - } - - /// - /// 移除路径里的后缀名 - /// - public static string RemoveExtension(string str) - { - if (string.IsNullOrEmpty(str)) - return str; + /// + /// 路径工具类 + /// + internal static class PathUtility + { + /// + /// 路径归一化 + /// 注意:替换为Linux路径格式 + /// + public static string RegularPath(string path) + { + return path.Replace('\\', '/').Replace("\\", "/"); + } - int index = str.LastIndexOf("."); - if (index == -1) - return str; - else - return str.Remove(index); //"assets/config/test.unity3d" --> "assets/config/test" - } + /// + /// 移除路径里的后缀名 + /// + public static string RemoveExtension(string str) + { + if (string.IsNullOrEmpty(str)) + return str; - /// - /// 合并路径 - /// - public static string Combine(string path1, string path2) - { - return StringUtility.Format("{0}/{1}", path1, path2); - } + int index = str.LastIndexOf("."); + if (index == -1) + return str; + else + return str.Remove(index); //"assets/config/test.unity3d" --> "assets/config/test" + } - /// - /// 合并路径 - /// - public static string Combine(string path1, string path2, string path3) - { - return StringUtility.Format("{0}/{1}/{2}", path1, path2, path3); - } + /// + /// 合并路径 + /// + public static string Combine(string path1, string path2) + { + return StringUtility.Format("{0}/{1}", path1, path2); + } - /// - /// 合并路径 - /// - public static string Combine(string path1, string path2, string path3, string path4) - { - return StringUtility.Format("{0}/{1}/{2}/{3}", path1, path2, path3, path4); - } - } + /// + /// 合并路径 + /// + public static string Combine(string path1, string path2, string path3) + { + return StringUtility.Format("{0}/{1}/{2}", path1, path2, path3); + } - /// - /// 字符串工具类 - /// - internal static class StringUtility - { - [ThreadStatic] - private static StringBuilder _cacheBuilder = new StringBuilder(2048); + /// + /// 合并路径 + /// + public static string Combine(string path1, string path2, string path3, string path4) + { + return StringUtility.Format("{0}/{1}/{2}/{3}", path1, path2, path3, path4); + } + } - public static string Format(string format, object arg0) - { - if (string.IsNullOrEmpty(format)) - throw new ArgumentNullException(); + /// + /// 字符串工具类 + /// + internal static class StringUtility + { + [ThreadStatic] + private static StringBuilder _cacheBuilder = new StringBuilder(2048); - _cacheBuilder.Length = 0; - _cacheBuilder.AppendFormat(format, arg0); - return _cacheBuilder.ToString(); - } - public static string Format(string format, object arg0, object arg1) - { - if (string.IsNullOrEmpty(format)) - throw new ArgumentNullException(); + public static string Format(string format, object arg0) + { + if (string.IsNullOrEmpty(format)) + throw new ArgumentNullException(); - _cacheBuilder.Length = 0; - _cacheBuilder.AppendFormat(format, arg0, arg1); - return _cacheBuilder.ToString(); - } - public static string Format(string format, object arg0, object arg1, object arg2) - { - if (string.IsNullOrEmpty(format)) - throw new ArgumentNullException(); + _cacheBuilder.Length = 0; + _cacheBuilder.AppendFormat(format, arg0); + return _cacheBuilder.ToString(); + } + public static string Format(string format, object arg0, object arg1) + { + if (string.IsNullOrEmpty(format)) + throw new ArgumentNullException(); - _cacheBuilder.Length = 0; - _cacheBuilder.AppendFormat(format, arg0, arg1, arg2); - return _cacheBuilder.ToString(); - } - public static string Format(string format, params object[] args) - { - if (string.IsNullOrEmpty(format)) - throw new ArgumentNullException(); + _cacheBuilder.Length = 0; + _cacheBuilder.AppendFormat(format, arg0, arg1); + return _cacheBuilder.ToString(); + } + public static string Format(string format, object arg0, object arg1, object arg2) + { + if (string.IsNullOrEmpty(format)) + throw new ArgumentNullException(); - if (args == null) - throw new ArgumentNullException(); + _cacheBuilder.Length = 0; + _cacheBuilder.AppendFormat(format, arg0, arg1, arg2); + return _cacheBuilder.ToString(); + } + public static string Format(string format, params object[] args) + { + if (string.IsNullOrEmpty(format)) + throw new ArgumentNullException(); - _cacheBuilder.Length = 0; - _cacheBuilder.AppendFormat(format, args); - return _cacheBuilder.ToString(); - } - } + if (args == null) + throw new ArgumentNullException(); - /// - /// 文件工具类 - /// - internal static class FileUtility - { - /// - /// 读取文件的文本数据 - /// - public static string ReadAllText(string filePath) - { - if (File.Exists(filePath) == false) - return string.Empty; - return File.ReadAllText(filePath, Encoding.UTF8); - } + _cacheBuilder.Length = 0; + _cacheBuilder.AppendFormat(format, args); + return _cacheBuilder.ToString(); + } + } - /// - /// 读取文件的字节数据 - /// - public static byte[] ReadAllBytes(string filePath) - { - if (File.Exists(filePath) == false) - return null; - return File.ReadAllBytes(filePath); - } + /// + /// 文件工具类 + /// + internal static class FileUtility + { + /// + /// 读取文件的文本数据 + /// + public static string ReadAllText(string filePath) + { + if (File.Exists(filePath) == false) + return string.Empty; + return File.ReadAllText(filePath, Encoding.UTF8); + } - /// - /// 写入文本数据(会覆盖指定路径的文件) - /// - public static void WriteAllText(string filePath, string content) - { - // 创建文件夹路径 - CreateFileDirectory(filePath); + /// + /// 读取文件的字节数据 + /// + public static byte[] ReadAllBytes(string filePath) + { + if (File.Exists(filePath) == false) + return null; + return File.ReadAllBytes(filePath); + } - byte[] bytes = Encoding.UTF8.GetBytes(content); - File.WriteAllBytes(filePath, bytes); //避免写入BOM标记 - } + /// + /// 写入文本数据(会覆盖指定路径的文件) + /// + public static void WriteAllText(string filePath, string content) + { + // 创建文件夹路径 + CreateFileDirectory(filePath); - /// - /// 写入字节数据(会覆盖指定路径的文件) - /// - public static void WriteAllBytes(string filePath, byte[] data) - { - // 创建文件夹路径 - CreateFileDirectory(filePath); + byte[] bytes = Encoding.UTF8.GetBytes(content); + File.WriteAllBytes(filePath, bytes); //避免写入BOM标记 + } - File.WriteAllBytes(filePath, data); - } + /// + /// 写入字节数据(会覆盖指定路径的文件) + /// + public static void WriteAllBytes(string filePath, byte[] data) + { + // 创建文件夹路径 + CreateFileDirectory(filePath); - /// - /// 创建文件的文件夹路径 - /// - public static void CreateFileDirectory(string filePath) - { - // 获取文件的文件夹路径 - string directory = Path.GetDirectoryName(filePath); - CreateDirectory(directory); - } + File.WriteAllBytes(filePath, data); + } - /// - /// 创建文件夹路径 - /// - public static void CreateDirectory(string directory) - { - // If the directory doesn't exist, create it. - if (Directory.Exists(directory) == false) - Directory.CreateDirectory(directory); - } + /// + /// 创建文件的文件夹路径 + /// + public static void CreateFileDirectory(string filePath) + { + // 获取文件的文件夹路径 + string directory = Path.GetDirectoryName(filePath); + CreateDirectory(directory); + } - /// - /// 获取文件大小(字节数) - /// - public static long GetFileSize(string filePath) - { - FileInfo fileInfo = new FileInfo(filePath); - return fileInfo.Length; - } - } + /// + /// 创建文件夹路径 + /// + public static void CreateDirectory(string directory) + { + // If the directory doesn't exist, create it. + if (Directory.Exists(directory) == false) + Directory.CreateDirectory(directory); + } - /// - /// 哈希工具类 - /// - internal static class HashUtility - { - private static string ToString(byte[] hashBytes) - { - string result = BitConverter.ToString(hashBytes); - result = result.Replace("-", ""); - return result.ToLower(); - } + /// + /// 获取文件大小(字节数) + /// + public static long GetFileSize(string filePath) + { + FileInfo fileInfo = new FileInfo(filePath); + return fileInfo.Length; + } + } - #region SHA1 - /// - /// 获取字符串的Hash值 - /// - public static string StringSHA1(string str) - { - byte[] buffer = Encoding.UTF8.GetBytes(str); - return BytesSHA1(buffer); - } + /// + /// 哈希工具类 + /// + internal static class HashUtility + { + private static string ToString(byte[] hashBytes) + { + string result = BitConverter.ToString(hashBytes); + result = result.Replace("-", ""); + return result.ToLower(); + } - /// - /// 获取文件的Hash值 - /// - public static string FileSHA1(string filePath) - { - try - { - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - return StreamSHA1(fs); - } - } - catch (Exception e) - { - YooLogger.Exception(e); - return string.Empty; - } - } + #region SHA1 + /// + /// 获取字符串的Hash值 + /// + public static string StringSHA1(string str) + { + byte[] buffer = Encoding.UTF8.GetBytes(str); + return BytesSHA1(buffer); + } - /// - /// 获取数据流的Hash值 - /// - public static string StreamSHA1(Stream stream) - { - // 说明:创建的是SHA1类的实例,生成的是160位的散列码 - HashAlgorithm hash = HashAlgorithm.Create(); - byte[] hashBytes = hash.ComputeHash(stream); - return ToString(hashBytes); - } + /// + /// 获取文件的Hash值 + /// + public static string FileSHA1(string filePath) + { + try + { + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + return StreamSHA1(fs); + } + } + catch (Exception e) + { + YooLogger.Exception(e); + return string.Empty; + } + } - /// - /// 获取字节数组的Hash值 - /// - public static string BytesSHA1(byte[] buffer) - { - // 说明:创建的是SHA1类的实例,生成的是160位的散列码 - HashAlgorithm hash = HashAlgorithm.Create(); - byte[] hashBytes = hash.ComputeHash(buffer); - return ToString(hashBytes); - } - #endregion + /// + /// 获取数据流的Hash值 + /// + public static string StreamSHA1(Stream stream) + { + // 说明:创建的是SHA1类的实例,生成的是160位的散列码 + HashAlgorithm hash = HashAlgorithm.Create(); + byte[] hashBytes = hash.ComputeHash(stream); + return ToString(hashBytes); + } - #region MD5 - /// - /// 获取字符串的MD5 - /// - public static string StringMD5(string str) - { - byte[] buffer = Encoding.UTF8.GetBytes(str); - return BytesMD5(buffer); - } + /// + /// 获取字节数组的Hash值 + /// + public static string BytesSHA1(byte[] buffer) + { + // 说明:创建的是SHA1类的实例,生成的是160位的散列码 + HashAlgorithm hash = HashAlgorithm.Create(); + byte[] hashBytes = hash.ComputeHash(buffer); + return ToString(hashBytes); + } + #endregion - /// - /// 获取文件的MD5 - /// - public static string FileMD5(string filePath) - { - try - { - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - return StreamMD5(fs); - } - } - catch (Exception e) - { - YooLogger.Exception(e); - return string.Empty; - } - } + #region MD5 + /// + /// 获取字符串的MD5 + /// + public static string StringMD5(string str) + { + byte[] buffer = Encoding.UTF8.GetBytes(str); + return BytesMD5(buffer); + } - /// - /// 获取数据流的MD5 - /// - public static string StreamMD5(Stream stream) - { - MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); - byte[] hashBytes = provider.ComputeHash(stream); - return ToString(hashBytes); - } + /// + /// 获取文件的MD5 + /// + public static string FileMD5(string filePath) + { + try + { + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + return StreamMD5(fs); + } + } + catch (Exception e) + { + YooLogger.Exception(e); + return string.Empty; + } + } - /// - /// 获取字节数组的MD5 - /// - public static string BytesMD5(byte[] buffer) - { - MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); - byte[] hashBytes = provider.ComputeHash(buffer); - return ToString(hashBytes); - } - #endregion + /// + /// 获取数据流的MD5 + /// + public static string StreamMD5(Stream stream) + { + MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); + byte[] hashBytes = provider.ComputeHash(stream); + return ToString(hashBytes); + } - #region CRC32 - /// - /// 获取字符串的CRC32 - /// - public static string StringCRC32(string str) - { - byte[] buffer = Encoding.UTF8.GetBytes(str); - return BytesCRC32(buffer); - } + /// + /// 获取字节数组的MD5 + /// + public static string BytesMD5(byte[] buffer) + { + MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); + byte[] hashBytes = provider.ComputeHash(buffer); + return ToString(hashBytes); + } + #endregion - /// - /// 获取文件的CRC32 - /// - public static string FileCRC32(string filePath) - { - try - { - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - return StreamCRC32(fs); - } - } - catch (Exception e) - { - YooLogger.Exception(e); - return string.Empty; - } - } + #region CRC32 + /// + /// 获取字符串的CRC32 + /// + public static string StringCRC32(string str) + { + byte[] buffer = Encoding.UTF8.GetBytes(str); + return BytesCRC32(buffer); + } - /// - /// 获取数据流的CRC32 - /// - public static string StreamCRC32(Stream stream) - { - CRC32Algorithm hash = new CRC32Algorithm(); - byte[] hashBytes = hash.ComputeHash(stream); - return ToString(hashBytes); - } + /// + /// 获取文件的CRC32 + /// + public static string FileCRC32(string filePath) + { + try + { + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + return StreamCRC32(fs); + } + } + catch (Exception e) + { + YooLogger.Exception(e); + return string.Empty; + } + } - /// - /// 获取字节数组的CRC32 - /// - public static string BytesCRC32(byte[] buffer) - { - CRC32Algorithm hash = new CRC32Algorithm(); - byte[] hashBytes = hash.ComputeHash(buffer); - return ToString(hashBytes); - } - #endregion - } + /// + /// 获取数据流的CRC32 + /// + public static string StreamCRC32(Stream stream) + { + CRC32Algorithm hash = new CRC32Algorithm(); + byte[] hashBytes = hash.ComputeHash(stream); + return ToString(hashBytes); + } + + /// + /// 获取字节数组的CRC32 + /// + public static string BytesCRC32(byte[] buffer) + { + CRC32Algorithm hash = new CRC32Algorithm(); + byte[] hashBytes = hash.ComputeHash(buffer); + return ToString(hashBytes); + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index bcf06a6..8d3a5c8 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -6,238 +6,238 @@ using UnityEngine; namespace YooAsset { - public static partial class YooAssets - { - private static bool _isInitialize = false; - private static GameObject _driver = null; - private static readonly List _packages = new List(); + public static partial class YooAssets + { + private static bool _isInitialize = false; + private static GameObject _driver = null; + private static readonly List _packages = new List(); - /// - /// 是否已经初始化 - /// - public static bool Initialized - { - get { return _isInitialize; } - } + /// + /// 是否已经初始化 + /// + public static bool Initialized + { + get { return _isInitialize; } + } - /// - /// 初始化资源系统 - /// - /// 自定义日志处理 - public static void Initialize(ILogger logger = null) - { - if (_isInitialize) - { - UnityEngine.Debug.LogWarning($"{nameof(YooAssets)} is initialized !"); - return; - } + /// + /// 初始化资源系统 + /// + /// 自定义日志处理 + public static void Initialize(ILogger logger = null) + { + if (_isInitialize) + { + UnityEngine.Debug.LogWarning($"{nameof(YooAssets)} is initialized !"); + return; + } - if (_isInitialize == false) - { - YooLogger.Logger = logger; + if (_isInitialize == false) + { + YooLogger.Logger = logger; - // 创建驱动器 - _isInitialize = true; - _driver = new UnityEngine.GameObject($"[{nameof(YooAssets)}]"); - _driver.AddComponent(); - UnityEngine.Object.DontDestroyOnLoad(_driver); - YooLogger.Log($"{nameof(YooAssets)} initialize !"); + // 创建驱动器 + _isInitialize = true; + _driver = new UnityEngine.GameObject($"[{nameof(YooAssets)}]"); + _driver.AddComponent(); + UnityEngine.Object.DontDestroyOnLoad(_driver); + YooLogger.Log($"{nameof(YooAssets)} initialize !"); #if DEBUG - // 添加远程调试脚本 - _driver.AddComponent(); + // 添加远程调试脚本 + _driver.AddComponent(); #endif - OperationSystem.Initialize(); - } - } + OperationSystem.Initialize(); + } + } - /// - /// 销毁资源系统 - /// - public static void Destroy() - { - if (_isInitialize) - { - OperationSystem.DestroyAll(); + /// + /// 销毁资源系统 + /// + public static void Destroy() + { + if (_isInitialize) + { + OperationSystem.DestroyAll(); - foreach (var package in _packages) - { - package.DestroyPackage(); - } - _packages.Clear(); + foreach (var package in _packages) + { + package.DestroyPackage(); + } + _packages.Clear(); - _isInitialize = false; - if (_driver != null) - GameObject.Destroy(_driver); - YooLogger.Log($"{nameof(YooAssets)} destroy all !"); - } - } + _isInitialize = false; + if (_driver != null) + GameObject.Destroy(_driver); + YooLogger.Log($"{nameof(YooAssets)} destroy all !"); + } + } - /// - /// 更新资源系统 - /// - internal static void Update() - { - if (_isInitialize) - { - OperationSystem.Update(); + /// + /// 更新资源系统 + /// + internal static void Update() + { + if (_isInitialize) + { + OperationSystem.Update(); - for (int i = 0; i < _packages.Count; i++) - { - _packages[i].UpdatePackage(); - } - } - } + for (int i = 0; i < _packages.Count; i++) + { + _packages[i].UpdatePackage(); + } + } + } - /// - /// 创建资源包 - /// - /// 资源包名称 - public static ResourcePackage CreatePackage(string packageName) - { - CheckException(packageName); - if (ContainsPackage(packageName)) - throw new Exception($"Package {packageName} already existed !"); + /// + /// 创建资源包 + /// + /// 资源包名称 + public static ResourcePackage CreatePackage(string packageName) + { + CheckException(packageName); + if (ContainsPackage(packageName)) + throw new Exception($"Package {packageName} already existed !"); - YooLogger.Log($"Create resource package : {packageName}"); - ResourcePackage package = new ResourcePackage(packageName); - _packages.Add(package); - return package; - } + YooLogger.Log($"Create resource package : {packageName}"); + ResourcePackage package = new ResourcePackage(packageName); + _packages.Add(package); + return package; + } - /// - /// 获取资源包 - /// - /// 资源包名称 - public static ResourcePackage GetPackage(string packageName) - { - CheckException(packageName); - var package = GetPackageInternal(packageName); - if (package == null) - YooLogger.Error($"Not found resource package : {packageName}"); - return package; - } + /// + /// 获取资源包 + /// + /// 资源包名称 + public static ResourcePackage GetPackage(string packageName) + { + CheckException(packageName); + var package = GetPackageInternal(packageName); + if (package == null) + YooLogger.Error($"Not found resource package : {packageName}"); + return package; + } - /// - /// 尝试获取资源包 - /// - /// 资源包名称 - public static ResourcePackage TryGetPackage(string packageName) - { - CheckException(packageName); - return GetPackageInternal(packageName); - } + /// + /// 尝试获取资源包 + /// + /// 资源包名称 + public static ResourcePackage TryGetPackage(string packageName) + { + CheckException(packageName); + return GetPackageInternal(packageName); + } - /// - /// 销毁资源包 - /// - /// 资源包名称 - public static void DestroyPackage(string packageName) - { - CheckException(packageName); - ResourcePackage package = GetPackageInternal(packageName); - if (package == null) - return; + /// + /// 销毁资源包 + /// + /// 资源包名称 + public static void DestroyPackage(string packageName) + { + CheckException(packageName); + ResourcePackage package = GetPackageInternal(packageName); + if (package == null) + return; - YooLogger.Log($"Destroy resource package : {packageName}"); - _packages.Remove(package); - package.DestroyPackage(); - } + YooLogger.Log($"Destroy resource package : {packageName}"); + _packages.Remove(package); + package.DestroyPackage(); + } - /// - /// 检测资源包是否存在 - /// - /// 资源包名称 - public static bool ContainsPackage(string packageName) - { - CheckException(packageName); - var package = GetPackageInternal(packageName); - return package != null; - } + /// + /// 检测资源包是否存在 + /// + /// 资源包名称 + public static bool ContainsPackage(string packageName) + { + CheckException(packageName); + var package = GetPackageInternal(packageName); + return package != null; + } - /// - /// 开启一个异步操作 - /// - /// 异步操作对象 - public static void StartOperation(GameAsyncOperation operation) - { - // 注意:游戏业务逻辑的包裹填写为空 - OperationSystem.StartOperation(string.Empty, operation); - } + /// + /// 开启一个异步操作 + /// + /// 异步操作对象 + public static void StartOperation(GameAsyncOperation operation) + { + // 注意:游戏业务逻辑的包裹填写为空 + OperationSystem.StartOperation(string.Empty, operation); + } - private static ResourcePackage GetPackageInternal(string packageName) - { - foreach (var package in _packages) - { - if (package.PackageName == packageName) - return package; - } - return null; - } - private static void CheckException(string packageName) - { - if (_isInitialize == false) - throw new Exception($"{nameof(YooAssets)} not initialize !"); + private static ResourcePackage GetPackageInternal(string packageName) + { + foreach (var package in _packages) + { + if (package.PackageName == packageName) + return package; + } + return null; + } + private static void CheckException(string packageName) + { + if (_isInitialize == false) + throw new Exception($"{nameof(YooAssets)} not initialize !"); - if (string.IsNullOrEmpty(packageName)) - throw new Exception("Package name is null or empty !"); - } + if (string.IsNullOrEmpty(packageName)) + throw new Exception("Package name is null or empty !"); + } - #region 系统参数 - /// - /// 设置下载系统参数,下载失败后清理文件的HTTP错误码 - /// - public static void SetDownloadSystemClearFileResponseCode(List codes) - { - DownloadHelper.ClearFileResponseCodes = codes; - } + #region 系统参数 + /// + /// 设置下载系统参数,下载失败后清理文件的HTTP错误码 + /// + public static void SetDownloadSystemClearFileResponseCode(List codes) + { + DownloadHelper.ClearFileResponseCodes = codes; + } - /// - /// 设置下载系统参数,自定义下载请求 - /// - public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate) - { - DownloadHelper.RequestDelegate = requestDelegate; - } + /// + /// 设置下载系统参数,自定义下载请求 + /// + public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate) + { + DownloadHelper.RequestDelegate = requestDelegate; + } - /// - /// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒) - /// - public static void SetOperationSystemMaxTimeSlice(long milliseconds) - { - if (milliseconds < 10) - { - milliseconds = 10; - YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds."); - } - OperationSystem.MaxTimeSlice = milliseconds; - } + /// + /// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒) + /// + public static void SetOperationSystemMaxTimeSlice(long milliseconds) + { + if (milliseconds < 10) + { + milliseconds = 10; + YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds."); + } + OperationSystem.MaxTimeSlice = milliseconds; + } - /// - /// 设置缓存系统参数,禁用缓存在WebGL平台 - /// - public static void SetCacheSystemDisableCacheOnWebGL() - { - CacheHelper.DisableUnityCacheOnWebGL = true; - } - #endregion + /// + /// 设置缓存系统参数,禁用缓存在WebGL平台 + /// + public static void SetCacheSystemDisableCacheOnWebGL() + { + CacheHelper.DisableUnityCacheOnWebGL = true; + } + #endregion - #region 调试信息 - internal static DebugReport GetDebugReport() - { - DebugReport report = new DebugReport(); - report.FrameCount = Time.frameCount; + #region 调试信息 + internal static DebugReport GetDebugReport() + { + DebugReport report = new DebugReport(); + report.FrameCount = Time.frameCount; - foreach (var package in _packages) - { - var packageData = package.GetDebugPackageData(); - report.PackageDatas.Add(packageData); - } - return report; - } - #endregion - } + foreach (var package in _packages) + { + var packageData = package.GetDebugPackageData(); + report.PackageDatas.Add(packageData); + } + return report; + } + #endregion + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssetsDriver.cs b/Assets/YooAsset/Runtime/YooAssetsDriver.cs index 4208685..338f29b 100644 --- a/Assets/YooAsset/Runtime/YooAssetsDriver.cs +++ b/Assets/YooAsset/Runtime/YooAssetsDriver.cs @@ -3,26 +3,26 @@ using UnityEngine; namespace YooAsset { - internal class YooAssetsDriver : MonoBehaviour - { - private static int LastestUpdateFrame = 0; + internal class YooAssetsDriver : MonoBehaviour + { + private static int LastestUpdateFrame = 0; - void Update() - { - DebugCheckDuplicateDriver(); - YooAssets.Update(); - } + void Update() + { + DebugCheckDuplicateDriver(); + YooAssets.Update(); + } - [Conditional("DEBUG")] - private void DebugCheckDuplicateDriver() - { - if (LastestUpdateFrame > 0) - { - if (LastestUpdateFrame == Time.frameCount) - YooLogger.Warning($"There are two {nameof(YooAssetsDriver)} in the scene. Please ensure there is always exactly one driver in the scene."); - } + [Conditional("DEBUG")] + private void DebugCheckDuplicateDriver() + { + if (LastestUpdateFrame > 0) + { + if (LastestUpdateFrame == Time.frameCount) + YooLogger.Warning($"There are two {nameof(YooAssetsDriver)} in the scene. Please ensure there is always exactly one driver in the scene."); + } - LastestUpdateFrame = Time.frameCount; - } - } + LastestUpdateFrame = Time.frameCount; + } + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssetsExtension.cs b/Assets/YooAsset/Runtime/YooAssetsExtension.cs index ef4dcd6..927aa82 100644 --- a/Assets/YooAsset/Runtime/YooAssetsExtension.cs +++ b/Assets/YooAsset/Runtime/YooAssetsExtension.cs @@ -6,588 +6,588 @@ using UnityEngine.SceneManagement; namespace YooAsset { - public static partial class YooAssets - { - private static ResourcePackage _defaultPackage; + public static partial class YooAssets + { + private static ResourcePackage _defaultPackage; - /// - /// 设置默认的资源包 - /// - public static void SetDefaultPackage(ResourcePackage package) - { - _defaultPackage = package; - } + /// + /// 设置默认的资源包 + /// + public static void SetDefaultPackage(ResourcePackage package) + { + _defaultPackage = package; + } - #region 资源信息 - /// - /// 是否需要从远端更新下载 - /// - /// 资源的定位地址 - public static bool IsNeedDownloadFromRemote(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.IsNeedDownloadFromRemote(location); - } + #region 资源信息 + /// + /// 是否需要从远端更新下载 + /// + /// 资源的定位地址 + public static bool IsNeedDownloadFromRemote(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.IsNeedDownloadFromRemote(location); + } - /// - /// 是否需要从远端更新下载 - /// - /// 资源的定位地址 - public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.IsNeedDownloadFromRemote(assetInfo); - } + /// + /// 是否需要从远端更新下载 + /// + /// 资源的定位地址 + public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.IsNeedDownloadFromRemote(assetInfo); + } - /// - /// 获取资源信息列表 - /// - /// 资源标签 - public static AssetInfo[] GetAssetInfos(string tag) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.GetAssetInfos(tag); - } + /// + /// 获取资源信息列表 + /// + /// 资源标签 + public static AssetInfo[] GetAssetInfos(string tag) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfos(tag); + } - /// - /// 获取资源信息列表 - /// - /// 资源标签列表 - public static AssetInfo[] GetAssetInfos(string[] tags) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.GetAssetInfos(tags); - } + /// + /// 获取资源信息列表 + /// + /// 资源标签列表 + public static AssetInfo[] GetAssetInfos(string[] tags) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfos(tags); + } - /// - /// 获取资源信息 - /// - /// 资源的定位地址 - public static AssetInfo GetAssetInfo(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.GetAssetInfo(location); - } + /// + /// 获取资源信息 + /// + /// 资源的定位地址 + public static AssetInfo GetAssetInfo(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfo(location); + } - /// - /// 获取资源信息 - /// - /// 资源的定位地址 - /// 资源类型 - public static AssetInfo GetAssetInfo(string location, System.Type type) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.GetAssetInfo(location, type); - } + /// + /// 获取资源信息 + /// + /// 资源的定位地址 + /// 资源类型 + public static AssetInfo GetAssetInfo(string location, System.Type type) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfo(location, type); + } - /// - /// 获取资源信息 - /// - /// 资源GUID - public static AssetInfo GetAssetInfoByGUID(string assetGUID) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.GetAssetInfoByGUID(assetGUID); - } + /// + /// 获取资源信息 + /// + /// 资源GUID + public static AssetInfo GetAssetInfoByGUID(string assetGUID) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfoByGUID(assetGUID); + } - /// - /// 获取资源信息 - /// - /// 资源GUID - /// 资源类型 - public static AssetInfo GetAssetInfoByGUID(string assetGUID, System.Type type) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.GetAssetInfoByGUID(assetGUID, type); - } + /// + /// 获取资源信息 + /// + /// 资源GUID + /// 资源类型 + public static AssetInfo GetAssetInfoByGUID(string assetGUID, System.Type type) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfoByGUID(assetGUID, type); + } - /// - /// 检查资源定位地址是否有效 - /// - /// 资源的定位地址 - public static bool CheckLocationValid(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CheckLocationValid(location); - } - #endregion + /// + /// 检查资源定位地址是否有效 + /// + /// 资源的定位地址 + public static bool CheckLocationValid(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CheckLocationValid(location); + } + #endregion - #region 原生文件 - /// - /// 同步加载原生文件 - /// - /// 资源信息 - public static RawFileHandle LoadRawFileSync(AssetInfo assetInfo) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadRawFileSync(assetInfo); - } + #region 原生文件 + /// + /// 同步加载原生文件 + /// + /// 资源信息 + public static RawFileHandle LoadRawFileSync(AssetInfo assetInfo) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadRawFileSync(assetInfo); + } - /// - /// 同步加载原生文件 - /// - /// 资源的定位地址 - public static RawFileHandle LoadRawFileSync(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadRawFileSync(location); - } + /// + /// 同步加载原生文件 + /// + /// 资源的定位地址 + public static RawFileHandle LoadRawFileSync(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadRawFileSync(location); + } - /// - /// 异步加载原生文件 - /// - /// 资源信息 - public static RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadRawFileAsync(assetInfo, priority); - } + /// + /// 异步加载原生文件 + /// + /// 资源信息 + public static RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadRawFileAsync(assetInfo, priority); + } - /// - /// 异步加载原生文件 - /// - /// 资源的定位地址 - public static RawFileHandle LoadRawFileAsync(string location, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadRawFileAsync(location, priority); - } - #endregion + /// + /// 异步加载原生文件 + /// + /// 资源的定位地址 + public static RawFileHandle LoadRawFileAsync(string location, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadRawFileAsync(location, priority); + } + #endregion - #region 场景加载 - /// - /// 异步加载场景 - /// - /// 场景的定位地址 - /// 场景加载模式 - /// 场景加载到90%自动挂起 - /// 优先级 - public static SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSceneAsync(location, sceneMode, suspendLoad, priority); - } + #region 场景加载 + /// + /// 异步加载场景 + /// + /// 场景的定位地址 + /// 场景加载模式 + /// 场景加载到90%自动挂起 + /// 优先级 + public static SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSceneAsync(location, sceneMode, suspendLoad, priority); + } - /// - /// 异步加载场景 - /// - /// 场景的资源信息 - /// 场景加载模式 - /// 场景加载到90%自动挂起 - /// 优先级 - public static SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); - } - #endregion + /// + /// 异步加载场景 + /// + /// 场景的资源信息 + /// 场景加载模式 + /// 场景加载到90%自动挂起 + /// 优先级 + public static SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); + } + #endregion - #region 资源加载 - /// - /// 同步加载资源对象 - /// - /// 资源信息 - public static AssetHandle LoadAssetSync(AssetInfo assetInfo) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetSync(assetInfo); - } + #region 资源加载 + /// + /// 同步加载资源对象 + /// + /// 资源信息 + public static AssetHandle LoadAssetSync(AssetInfo assetInfo) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(assetInfo); + } - /// - /// 同步加载资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static AssetHandle LoadAssetSync(string location) where TObject : UnityEngine.Object - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetSync(location); - } + /// + /// 同步加载资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static AssetHandle LoadAssetSync(string location) where TObject : UnityEngine.Object + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(location); + } - /// - /// 同步加载资源对象 - /// - /// 资源的定位地址 - /// 资源类型 - public static AssetHandle LoadAssetSync(string location, System.Type type) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetSync(location, type); - } + /// + /// 同步加载资源对象 + /// + /// 资源的定位地址 + /// 资源类型 + public static AssetHandle LoadAssetSync(string location, System.Type type) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(location, type); + } - /// - /// 同步加载资源对象 - /// - /// 资源的定位地址 - public static AssetHandle LoadAssetSync(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetSync(location); - } + /// + /// 同步加载资源对象 + /// + /// 资源的定位地址 + public static AssetHandle LoadAssetSync(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(location); + } - /// - /// 异步加载资源对象 - /// - /// 资源信息 - public static AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetAsync(assetInfo, priority); - } + /// + /// 异步加载资源对象 + /// + /// 资源信息 + public static AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(assetInfo, priority); + } - /// - /// 异步加载资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static AssetHandle LoadAssetAsync(string location, uint priority = 0) where TObject : UnityEngine.Object - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetAsync(location, priority); - } + /// + /// 异步加载资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static AssetHandle LoadAssetAsync(string location, uint priority = 0) where TObject : UnityEngine.Object + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(location, priority); + } - /// - /// 异步加载资源对象 - /// - /// 资源的定位地址 - /// 资源类型 - public static AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetAsync(location, type, priority); - } + /// + /// 异步加载资源对象 + /// + /// 资源的定位地址 + /// 资源类型 + public static AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(location, type, priority); + } - /// - /// 异步加载资源对象 - /// - /// 资源的定位地址 - public static AssetHandle LoadAssetAsync(string location, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAssetAsync(location, priority); - } - #endregion + /// + /// 异步加载资源对象 + /// + /// 资源的定位地址 + public static AssetHandle LoadAssetAsync(string location, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(location, priority); + } + #endregion - #region 资源加载 - /// - /// 同步加载子资源对象 - /// - /// 资源信息 - public static SubAssetsHandle LoadSubAssetsSync(AssetInfo assetInfo) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsSync(assetInfo); - } + #region 资源加载 + /// + /// 同步加载子资源对象 + /// + /// 资源信息 + public static SubAssetsHandle LoadSubAssetsSync(AssetInfo assetInfo) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(assetInfo); + } - /// - /// 同步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static SubAssetsHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsSync(location); - } + /// + /// 同步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static SubAssetsHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(location); + } - /// - /// 同步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static SubAssetsHandle LoadSubAssetsSync(string location, System.Type type) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsSync(location, type); - } + /// + /// 同步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static SubAssetsHandle LoadSubAssetsSync(string location, System.Type type) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(location, type); + } - /// - /// 同步加载子资源对象 - /// - /// 资源的定位地址 - public static SubAssetsHandle LoadSubAssetsSync(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsSync(location); - } + /// + /// 同步加载子资源对象 + /// + /// 资源的定位地址 + public static SubAssetsHandle LoadSubAssetsSync(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(location); + } - /// - /// 异步加载子资源对象 - /// - /// 资源信息 - public static SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsAsync(assetInfo, priority); - } + /// + /// 异步加载子资源对象 + /// + /// 资源信息 + public static SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(assetInfo, priority); + } - /// - /// 异步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsAsync(location, priority); - } + /// + /// 异步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(location, priority); + } - /// - /// 异步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsAsync(location, type, priority); - } + /// + /// 异步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(location, type, priority); + } - /// - /// 异步加载子资源对象 - /// - /// 资源的定位地址 - public static SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadSubAssetsAsync(location, priority); - } - #endregion + /// + /// 异步加载子资源对象 + /// + /// 资源的定位地址 + public static SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(location, priority); + } + #endregion - #region 资源加载 - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源信息 - public static AllAssetsHandle LoadAllAssetsSync(AssetInfo assetInfo) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsSync(assetInfo); - } + #region 资源加载 + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源信息 + public static AllAssetsHandle LoadAllAssetsSync(AssetInfo assetInfo) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsSync(assetInfo); + } - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static AllAssetsHandle LoadAllAssetsSync(string location) where TObject : UnityEngine.Object - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsSync(location); - } + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static AllAssetsHandle LoadAllAssetsSync(string location) where TObject : UnityEngine.Object + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsSync(location); + } - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static AllAssetsHandle LoadAllAssetsSync(string location, System.Type type) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsSync(location, type); - } + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static AllAssetsHandle LoadAllAssetsSync(string location, System.Type type) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsSync(location, type); + } - /// - /// 同步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - public static AllAssetsHandle LoadAllAssetsSync(string location) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsSync(location); - } + /// + /// 同步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + public static AllAssetsHandle LoadAllAssetsSync(string location) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsSync(location); + } - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源信息 - public static AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsAsync(assetInfo, priority); - } + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源信息 + public static AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsAsync(assetInfo, priority); + } - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsAsync(location, priority); - } + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsAsync(location, priority); + } - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsAsync(location, type, priority); - } + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsAsync(location, type, priority); + } - /// - /// 异步加载资源包内所有资源对象 - /// - /// 资源的定位地址 - public static AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.LoadAllAssetsAsync(location, priority); - } - #endregion + /// + /// 异步加载资源包内所有资源对象 + /// + /// 资源的定位地址 + public static AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAllAssetsAsync(location, priority); + } + #endregion - #region 资源下载 - /// - /// 创建资源下载器,用于下载当前资源版本所有的资源包文件 - /// - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceDownloader(downloadingMaxNumber, failedTryAgain); - } + #region 资源下载 + /// + /// 创建资源下载器,用于下载当前资源版本所有的资源包文件 + /// + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceDownloader(downloadingMaxNumber, failedTryAgain); + } - /// - /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件 - /// - /// 资源标签 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); - } + /// + /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件 + /// + /// 资源标签 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); + } - /// - /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件 - /// - /// 资源标签列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceDownloader(tags, downloadingMaxNumber, failedTryAgain); - } + /// + /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件 + /// + /// 资源标签列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceDownloader(tags, downloadingMaxNumber, failedTryAgain); + } - /// - /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 - /// - /// 资源定位地址 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateBundleDownloader(location, downloadingMaxNumber, failedTryAgain); - } + /// + /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 + /// + /// 资源定位地址 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateBundleDownloader(location, downloadingMaxNumber, failedTryAgain); + } - /// - /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 - /// - /// 资源定位地址列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateBundleDownloader(locations, downloadingMaxNumber, failedTryAgain); - } + /// + /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 + /// + /// 资源定位地址列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateBundleDownloader(locations, downloadingMaxNumber, failedTryAgain); + } - /// - /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 - /// - /// 资源信息 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateBundleDownloader(assetInfo, downloadingMaxNumber, failedTryAgain); - } + /// + /// 创建资源下载器,用于下载指定的资源依赖的资源包文件 + /// + /// 资源信息 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateBundleDownloader(assetInfo, downloadingMaxNumber, failedTryAgain); + } - /// - /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 - /// - /// 资源信息列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateBundleDownloader(assetInfos, downloadingMaxNumber, failedTryAgain); - } - #endregion + /// + /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件 + /// + /// 资源信息列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateBundleDownloader(assetInfos, downloadingMaxNumber, failedTryAgain); + } + #endregion - #region 资源解压 - /// - /// 创建内置资源解压器,用于解压当前资源版本所有的资源包文件 - /// - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public static ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceUnpacker(unpackingMaxNumber, failedTryAgain); - } + #region 资源解压 + /// + /// 创建内置资源解压器,用于解压当前资源版本所有的资源包文件 + /// + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public static ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceUnpacker(unpackingMaxNumber, failedTryAgain); + } - /// - /// 创建内置资源解压器,用于解压指定的资源标签关联的资源包文件 - /// - /// 资源标签 - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public static ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceUnpacker(tag, unpackingMaxNumber, failedTryAgain); - } + /// + /// 创建内置资源解压器,用于解压指定的资源标签关联的资源包文件 + /// + /// 资源标签 + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public static ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceUnpacker(tag, unpackingMaxNumber, failedTryAgain); + } - /// - /// 创建内置资源解压器,用于解压指定的资源标签列表关联的资源包文件 - /// - /// 资源标签列表 - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public static ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceUnpacker(tags, unpackingMaxNumber, failedTryAgain); - } - #endregion + /// + /// 创建内置资源解压器,用于解压指定的资源标签列表关联的资源包文件 + /// + /// 资源标签列表 + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public static ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceUnpacker(tags, unpackingMaxNumber, failedTryAgain); + } + #endregion - #region 资源导入 - /// - /// 创建资源导入器 - /// 注意:资源文件名称必须和资源服务器部署的文件名称一致! - /// - /// 资源路径列表 - /// 同时导入的最大文件数 - /// 导入失败的重试次数 - public static ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain) - { - DebugCheckDefaultPackageValid(); - return _defaultPackage.CreateResourceImporter(filePaths, importerMaxNumber, failedTryAgain); - } - #endregion + #region 资源导入 + /// + /// 创建资源导入器 + /// 注意:资源文件名称必须和资源服务器部署的文件名称一致! + /// + /// 资源路径列表 + /// 同时导入的最大文件数 + /// 导入失败的重试次数 + public static ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain) + { + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateResourceImporter(filePaths, importerMaxNumber, failedTryAgain); + } + #endregion - #region 调试方法 - [Conditional("DEBUG")] - private static void DebugCheckDefaultPackageValid() - { - if (_defaultPackage == null) - throw new Exception($"Default package is null. Please use {nameof(YooAssets.SetDefaultPackage)} !"); - } - #endregion - } + #region 调试方法 + [Conditional("DEBUG")] + private static void DebugCheckDefaultPackageValid() + { + if (_defaultPackage == null) + throw new Exception($"Default package is null. Please use {nameof(YooAssets.SetDefaultPackage)} !"); + } + #endregion + } } \ No newline at end of file