From 850ae88bc61608e43780b6a981f4b33a5a5fd328 Mon Sep 17 00:00:00 2001 From: Blank Date: Tue, 27 Aug 2024 19:10:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[=E5=A2=9E=E5=8A=A0]1.=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=8A=A0=E8=BD=BD=E6=88=90=E5=8A=9F=E7=9A=84?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E5=88=A4=E6=96=AD=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 该接口主要方便于外部直接判断是否成功,而无需再次使用枚举的方式判断 --- .../Runtime/ResourceManager/Handle/HandleBase.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs index 5638d20..8b281bb 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs @@ -15,6 +15,14 @@ namespace YooAsset } internal abstract void InvokeCallback(); + /// + /// 是否成功 + /// + public bool IsSucceed + { + get { return IsDone && Status == EOperationStatus.Succeed; } + } + /// /// 获取资源信息 /// @@ -158,4 +166,4 @@ namespace YooAsset } #endregion } -} \ No newline at end of file +} From a6b333fe3541457bce54462f6271020a08ee212c Mon Sep 17 00:00:00 2001 From: Blank Date: Tue, 27 Aug 2024 19:13:36 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[=E4=BF=AE=E6=94=B9]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E5=8F=AF=E4=BB=A5=E8=8E=B7=E5=8F=96=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E6=93=8D=E4=BD=9C=E5=AF=B9=E8=B1=A1=E7=9A=84=E5=8C=85?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 主要是外部如果是回调的时候,不一定在之前的代码位置了.所需要需要从操作对象中直接获取当前的包名 --- Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index 11f5cab..f187d0d 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -93,7 +93,7 @@ namespace YooAsset throw new System.NotImplementedException(this.GetType().Name); } - internal string GetPackageName() + public string GetPackageName() { return _packageName; } @@ -191,4 +191,4 @@ namespace YooAsset private TaskCompletionSource _taskCompletionSource; #endregion } -} \ No newline at end of file +} From dda2a4c6599291c9cebe8e2caad97dbe87941832 Mon Sep 17 00:00:00 2001 From: Blank Date: Tue, 27 Aug 2024 19:16:18 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[=E5=A2=9E=E5=8A=A0]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E7=9A=84=E6=A0=87=E8=AE=B0?= =?UTF-8?q?=E5=92=8C=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 该日志标记在查询定位的时候非常有用.主要区分是否是业务日志还是资源日志. 2. 关于时间:可以查看在真机上的资源加载时间长短和资源问题的定位. --- Assets/YooAsset/Runtime/Utility/YooUtility.cs | 408 +++--------------- 1 file changed, 59 insertions(+), 349 deletions(-) diff --git a/Assets/YooAsset/Runtime/Utility/YooUtility.cs b/Assets/YooAsset/Runtime/Utility/YooUtility.cs index 1f12aa6..57132f2 100644 --- a/Assets/YooAsset/Runtime/Utility/YooUtility.cs +++ b/Assets/YooAsset/Runtime/Utility/YooUtility.cs @@ -1,377 +1,87 @@ -using System; -using System.IO; -using System.Security.Cryptography; -using System.Text; +using System; +using System.Diagnostics; namespace YooAsset { /// - /// 路径工具类 + /// 自定义日志处理 /// - internal static class PathUtility + public interface ILogger { - /// - /// 路径归一化 - /// 注意:替换为Linux路径格式 - /// - public static string RegularPath(string path) - { - return path.Replace('\\', '/').Replace("\\", "/"); - } + 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; /// - /// 移除路径里的后缀名 + /// 日志 /// - public static string RemoveExtension(string str) + [Conditional("DEBUG")] + public static void Log(string info) { - if (string.IsNullOrEmpty(str)) - return str; - - int index = str.LastIndexOf('.'); - if (index == -1) - return str; + if (Logger != null) + { + Logger.Log(GetTime() + info); + } else - return str.Remove(index); //"assets/config/test.unity3d" --> "assets/config/test" + { + UnityEngine.Debug.Log(GetTime() + info); + } } /// - /// 合并路径 + /// 警告 /// - public static string Combine(string path1, string path2) + public static void Warning(string info) { - return StringUtility.Format("{0}/{1}", path1, path2); + if (Logger != null) + { + Logger.Warning(GetTime() + info); + } + else + { + UnityEngine.Debug.LogWarning(GetTime() + info); + } } /// - /// 合并路径 + /// 错误 /// - public static string Combine(string path1, string path2, string path3) + public static void Error(string info) { - return StringUtility.Format("{0}/{1}/{2}", path1, path2, path3); + if (Logger != null) + { + Logger.Error(GetTime() + info); + } + else + { + UnityEngine.Debug.LogError(GetTime() + info); + } + } + + private static string GetTime() + { + return $"[YooAsset]:[{DateTime.Now:HH:mm:ss.fff}]:"; } /// - /// 合并路径 + /// 异常 /// - public static string Combine(string path1, string path2, string path3, string path4) + public static void Exception(System.Exception exception) { - return StringUtility.Format("{0}/{1}/{2}/{3}", path1, path2, path3, path4); + if (Logger != null) + { + Logger.Exception(exception); + } + else + { + UnityEngine.Debug.LogException(exception); + } } } - - /// - /// 字符串工具类 - /// - internal static class StringUtility - { - [ThreadStatic] - private static StringBuilder _cacheBuilder = new StringBuilder(2048); - - public static string Format(string format, object arg0) - { - 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); - 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, arg1, arg2); - return _cacheBuilder.ToString(); - } - public static string Format(string format, params object[] args) - { - if (string.IsNullOrEmpty(format)) - throw new ArgumentNullException(); - - if (args == null) - throw new ArgumentNullException(); - - _cacheBuilder.Length = 0; - _cacheBuilder.AppendFormat(format, args); - return _cacheBuilder.ToString(); - } - } - - /// - /// 文件工具类 - /// - internal static class FileUtility - { - /// - /// 读取文件的文本数据 - /// - public static string ReadAllText(string filePath) - { - if (File.Exists(filePath) == false) - return null; - return File.ReadAllText(filePath, Encoding.UTF8); - } - - /// - /// 读取文件的字节数据 - /// - public static byte[] ReadAllBytes(string filePath) - { - if (File.Exists(filePath) == false) - return null; - return File.ReadAllBytes(filePath); - } - - /// - /// 写入文本数据(会覆盖指定路径的文件) - /// - public static void WriteAllText(string filePath, string content) - { - // 创建文件夹路径 - CreateFileDirectory(filePath); - - byte[] bytes = Encoding.UTF8.GetBytes(content); - File.WriteAllBytes(filePath, bytes); //避免写入BOM标记 - } - - /// - /// 写入字节数据(会覆盖指定路径的文件) - /// - public static void WriteAllBytes(string filePath, byte[] data) - { - // 创建文件夹路径 - CreateFileDirectory(filePath); - - File.WriteAllBytes(filePath, data); - } - - /// - /// 创建文件的文件夹路径 - /// - public static void CreateFileDirectory(string filePath) - { - // 获取文件的文件夹路径 - string directory = Path.GetDirectoryName(filePath); - CreateDirectory(directory); - } - - /// - /// 创建文件夹路径 - /// - public static void CreateDirectory(string directory) - { - // If the directory doesn't exist, create it. - if (Directory.Exists(directory) == false) - Directory.CreateDirectory(directory); - } - - /// - /// 获取文件大小(字节数) - /// - public static long GetFileSize(string filePath) - { - FileInfo fileInfo = new FileInfo(filePath); - return fileInfo.Length; - } - } - - /// - /// 哈希工具类 - /// - public static class HashUtility - { - private static string ToString(byte[] hashBytes) - { - string result = BitConverter.ToString(hashBytes); - result = result.Replace("-", ""); - return result.ToLower(); - } - - #region SHA1 - /// - /// 获取字符串的Hash值 - /// - public static string StringSHA1(string str) - { - byte[] buffer = Encoding.UTF8.GetBytes(str); - return BytesSHA1(buffer); - } - - /// - /// 获取文件的Hash值 - /// - public static string FileSHA1(string filePath) - { - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - return StreamSHA1(fs); - } - } - - /// - /// 获取文件的Hash值 - /// - public static string FileSHA1Safely(string filePath) - { - try - { - return FileSHA1(filePath); - } - catch (Exception e) - { - YooLogger.Exception(e); - return string.Empty; - } - } - - /// - /// 获取数据流的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 BytesSHA1(byte[] buffer) - { - // 说明:创建的是SHA1类的实例,生成的是160位的散列码 - HashAlgorithm hash = HashAlgorithm.Create(); - byte[] hashBytes = hash.ComputeHash(buffer); - return ToString(hashBytes); - } - #endregion - - #region MD5 - /// - /// 获取字符串的MD5 - /// - public static string StringMD5(string str) - { - byte[] buffer = Encoding.UTF8.GetBytes(str); - return BytesMD5(buffer); - } - - /// - /// 获取文件的MD5 - /// - public static string FileMD5(string filePath) - { - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - return StreamMD5(fs); - } - } - - /// - /// 获取文件的MD5 - /// - public static string FileMD5Safely(string filePath) - { - try - { - return FileMD5(filePath); - } - catch (Exception e) - { - YooLogger.Exception(e); - return string.Empty; - } - } - - /// - /// 获取数据流的MD5 - /// - public static string StreamMD5(Stream stream) - { - MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); - byte[] hashBytes = provider.ComputeHash(stream); - return ToString(hashBytes); - } - - /// - /// 获取字节数组的MD5 - /// - public static string BytesMD5(byte[] buffer) - { - MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); - byte[] hashBytes = provider.ComputeHash(buffer); - return ToString(hashBytes); - } - #endregion - - #region CRC32 - /// - /// 获取字符串的CRC32 - /// - public static string StringCRC32(string str) - { - byte[] buffer = Encoding.UTF8.GetBytes(str); - return BytesCRC32(buffer); - } - - /// - /// 获取文件的CRC32 - /// - public static string FileCRC32(string filePath) - { - using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - return StreamCRC32(fs); - } - } - - /// - /// 获取文件的CRC32 - /// - public static string FileCRC32Safely(string filePath) - { - try - { - return FileCRC32(filePath); - } - catch (Exception e) - { - YooLogger.Exception(e); - return string.Empty; - } - } - - /// - /// 获取数据流的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 +} From 60fe96fb9c350594bda0df33d4a1f32ec86846a3 Mon Sep 17 00:00:00 2001 From: Blank Date: Tue, 27 Aug 2024 19:19:42 +0800 Subject: [PATCH 4/4] =?UTF-8?q?[=E5=A2=9E=E5=8A=A0]=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E7=8A=B6=E6=80=81=E5=8F=98=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=8C=85=E5=90=8D=E7=A7=B0=E7=9A=84=E8=BF=94?= =?UTF-8?q?=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.主要是如果在同时下载包的时候且注册到统一的返回事件的时候无法识别当前是什么包下载的返回结果.这样给开发者有一定的操作成本.需要独立的去记录包和下载列表的关系 --- .../Operation/DownloaderOperation.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs index f713f1e..6a01929 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/DownloaderOperation.cs @@ -15,10 +15,10 @@ namespace YooAsset 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(string packageName, bool isSucceed); + public delegate void OnDownloadProgress(string packageName, int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes); + public delegate void OnDownloadError(string packageName, string fileName, string error); + public delegate void OnStartDownloadFile(string packageName, string fileName, long sizeBytes); private readonly string _packageName; private readonly int _downloadingMaxNumber; @@ -160,7 +160,7 @@ namespace YooAsset _lastDownloadBytes = downloadBytes; _lastDownloadCount = _cachedDownloadCount; Progress = (float)_lastDownloadBytes / TotalDownloadBytes; - OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes); + OnDownloadProgressCallback?.Invoke(GetPackageName(), TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes); } // 动态创建新的下载器到最大数量限制 @@ -177,7 +177,7 @@ namespace YooAsset var downloader = bundleInfo.CreateDownloader(_failedTryAgain, _timeout); _downloaders.Add(downloader); _bundleInfoList.RemoveAt(index); - OnStartDownloadFileCallback?.Invoke(bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize); + OnStartDownloadFileCallback?.Invoke(GetPackageName(), bundleInfo.Bundle.BundleName, bundleInfo.Bundle.FileSize); } } @@ -191,15 +191,15 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = $"Failed to download file : {bundleName}"; - OnDownloadErrorCallback?.Invoke(bundleName, failedDownloader.Error); - OnDownloadOverCallback?.Invoke(false); + OnDownloadErrorCallback?.Invoke(GetPackageName(), bundleName, failedDownloader.Error); + OnDownloadOverCallback?.Invoke(GetPackageName(), false); } else { // 结算成功 _steps = ESteps.Done; Status = EOperationStatus.Succeed; - OnDownloadOverCallback?.Invoke(true); + OnDownloadOverCallback?.Invoke(GetPackageName(), true); } } } @@ -361,4 +361,4 @@ namespace YooAsset return operation; } } -} \ No newline at end of file +}