From 0e29e9823df4f1ac16f1f9b6373cc879d2ff4dcb Mon Sep 17 00:00:00 2001 From: hevinci Date: Thu, 21 Dec 2023 16:10:54 +0800 Subject: [PATCH] update query services interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 内置文件和分发文件查询方法参数里增加了文件哈希值 --- Assets/YooAsset/Editor/EditorTools.cs | 1060 +++++++++-------- .../PlayMode/HostPlayModeImpl.cs | 4 +- .../PlayMode/WebPlayModeImpl.cs | 2 +- .../Runtime/Services/IBuildinQueryServices.cs | 18 +- .../Services/IDeliveryQueryServices.cs | 29 +- 5 files changed, 566 insertions(+), 547 deletions(-) diff --git a/Assets/YooAsset/Editor/EditorTools.cs b/Assets/YooAsset/Editor/EditorTools.cs index 1a82b36..2320540 100644 --- a/Assets/YooAsset/Editor/EditorTools.cs +++ b/Assets/YooAsset/Editor/EditorTools.cs @@ -11,39 +11,39 @@ using UnityEditor.SceneManagement; namespace YooAsset.Editor { - /// - /// 编辑器工具类 - /// - public static class EditorTools - { - static EditorTools() - { - InitAssembly(); - } + /// + /// 编辑器工具类 + /// + public static class EditorTools + { + static EditorTools() + { + InitAssembly(); + } - #region Assembly + #region Assembly #if UNITY_2019_4_OR_NEWER - private static void InitAssembly() - { - } + private static void InitAssembly() + { + } - /// - /// 获取带继承关系的所有类的类型 - /// - public static List GetAssignableTypes(System.Type parentType) - { - TypeCache.TypeCollection collection = TypeCache.GetTypesDerivedFrom(parentType); - return collection.ToList(); - } + /// + /// 获取带继承关系的所有类的类型 + /// + public static List GetAssignableTypes(System.Type parentType) + { + TypeCache.TypeCollection collection = TypeCache.GetTypesDerivedFrom(parentType); + return collection.ToList(); + } - /// - /// 获取带有指定属性的所有类的类型 - /// - public static List GetTypesWithAttribute(System.Type attrType) - { - TypeCache.TypeCollection collection = TypeCache.GetTypesWithAttribute(attrType); - return collection.ToList(); - } + /// + /// 获取带有指定属性的所有类的类型 + /// + public static List GetTypesWithAttribute(System.Type attrType) + { + TypeCache.TypeCollection collection = TypeCache.GetTypesWithAttribute(attrType); + return collection.ToList(); + } #else private static readonly List _cacheTypes = new List(10000); private static void InitAssembly() @@ -94,552 +94,560 @@ namespace YooAsset.Editor } #endif - /// - /// 调用私有的静态方法 - /// - /// 类的类型 - /// 类里要调用的方法名 - /// 调用方法传入的参数 - public static object InvokeNonPublicStaticMethod(System.Type type, string method, params object[] parameters) - { - var methodInfo = type.GetMethod(method, BindingFlags.NonPublic | BindingFlags.Static); - if (methodInfo == null) - { - UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}"); - return null; - } - return methodInfo.Invoke(null, parameters); - } + /// + /// 调用私有的静态方法 + /// + /// 类的类型 + /// 类里要调用的方法名 + /// 调用方法传入的参数 + public static object InvokeNonPublicStaticMethod(System.Type type, string method, params object[] parameters) + { + var methodInfo = type.GetMethod(method, BindingFlags.NonPublic | BindingFlags.Static); + if (methodInfo == null) + { + UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}"); + return null; + } + return methodInfo.Invoke(null, parameters); + } - /// - /// 调用公开的静态方法 - /// - /// 类的类型 - /// 类里要调用的方法名 - /// 调用方法传入的参数 - public 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); - } - #endregion + /// + /// 调用公开的静态方法 + /// + /// 类的类型 + /// 类里要调用的方法名 + /// 调用方法传入的参数 + public 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); + } + #endregion - #region EditorUtility - /// - /// 搜集资源 - /// - /// 搜集的资源类型 - /// 指定搜索的文件夹列表 - /// 返回搜集到的资源路径列表 - public static string[] FindAssets(EAssetSearchType searchType, string[] searchInFolders) - { - // 注意:AssetDatabase.FindAssets()不支持末尾带分隔符的文件夹路径 - for (int i = 0; i < searchInFolders.Length; i++) - { - string folderPath = searchInFolders[i]; - searchInFolders[i] = folderPath.TrimEnd('/'); - } + #region EditorUtility + /// + /// 搜集资源 + /// + /// 搜集的资源类型 + /// 指定搜索的文件夹列表 + /// 返回搜集到的资源路径列表 + public static string[] FindAssets(EAssetSearchType searchType, string[] searchInFolders) + { + // 注意:AssetDatabase.FindAssets()不支持末尾带分隔符的文件夹路径 + for (int i = 0; i < searchInFolders.Length; i++) + { + string folderPath = searchInFolders[i]; + searchInFolders[i] = folderPath.TrimEnd('/'); + } - // 注意:获取指定目录下的所有资源对象(包括子文件夹) - string[] guids; - if (searchType == EAssetSearchType.All) - guids = AssetDatabase.FindAssets(string.Empty, searchInFolders); - else - guids = AssetDatabase.FindAssets($"t:{searchType}", searchInFolders); + // 注意:获取指定目录下的所有资源对象(包括子文件夹) + string[] guids; + if (searchType == EAssetSearchType.All) + guids = AssetDatabase.FindAssets(string.Empty, searchInFolders); + else + guids = AssetDatabase.FindAssets($"t:{searchType}", searchInFolders); - // 注意:AssetDatabase.FindAssets()可能会获取到重复的资源 - HashSet result = new HashSet(); - for (int i = 0; i < guids.Length; i++) - { - string guid = guids[i]; - string assetPath = AssetDatabase.GUIDToAssetPath(guid); - if (result.Contains(assetPath) == false) - { - result.Add(assetPath); - } - } + // 注意:AssetDatabase.FindAssets()可能会获取到重复的资源 + HashSet result = new HashSet(); + for (int i = 0; i < guids.Length; i++) + { + string guid = guids[i]; + string assetPath = AssetDatabase.GUIDToAssetPath(guid); + if (result.Contains(assetPath) == false) + { + result.Add(assetPath); + } + } - // 返回结果 - return result.ToArray(); - } + // 返回结果 + return result.ToArray(); + } - /// - /// 搜集资源 - /// - /// 搜集的资源类型 - /// 指定搜索的文件夹 - /// 返回搜集到的资源路径列表 - public static string[] FindAssets(EAssetSearchType searchType, string searchInFolder) - { - return FindAssets(searchType, new string[] { searchInFolder }); - } + /// + /// 搜集资源 + /// + /// 搜集的资源类型 + /// 指定搜索的文件夹 + /// 返回搜集到的资源路径列表 + public static string[] FindAssets(EAssetSearchType searchType, string searchInFolder) + { + return FindAssets(searchType, new string[] { searchInFolder }); + } - /// - /// 打开搜索面板 - /// - /// 标题名称 - /// 默认搜索路径 - /// 返回选择的文件夹绝对路径,如果无效返回NULL - public static string OpenFolderPanel(string title, string defaultPath, string defaultName = "") - { - string openPath = EditorUtility.OpenFolderPanel(title, defaultPath, defaultName); - if (string.IsNullOrEmpty(openPath)) - return null; + /// + /// 打开搜索面板 + /// + /// 标题名称 + /// 默认搜索路径 + /// 返回选择的文件夹绝对路径,如果无效返回NULL + public static string OpenFolderPanel(string title, string defaultPath, string defaultName = "") + { + string openPath = EditorUtility.OpenFolderPanel(title, defaultPath, defaultName); + if (string.IsNullOrEmpty(openPath)) + return null; - if (openPath.Contains("/Assets") == false) - { - Debug.LogWarning("Please select unity assets folder."); - return null; - } - return openPath; - } + if (openPath.Contains("/Assets") == false) + { + Debug.LogWarning("Please select unity assets folder."); + return null; + } + return openPath; + } - /// - /// 打开搜索面板 - /// - /// 标题名称 - /// 默认搜索路径 - /// 返回选择的文件绝对路径,如果无效返回NULL - public static string OpenFilePath(string title, string defaultPath, string extension = "") - { - string openPath = EditorUtility.OpenFilePanel(title, defaultPath, extension); - if (string.IsNullOrEmpty(openPath)) - return null; + /// + /// 打开搜索面板 + /// + /// 标题名称 + /// 默认搜索路径 + /// 返回选择的文件绝对路径,如果无效返回NULL + public static string OpenFilePath(string title, string defaultPath, string extension = "") + { + string openPath = EditorUtility.OpenFilePanel(title, defaultPath, extension); + if (string.IsNullOrEmpty(openPath)) + return null; - if (openPath.Contains("/Assets") == false) - { - Debug.LogWarning("Please select unity assets file."); - return null; - } - return openPath; - } + if (openPath.Contains("/Assets") == false) + { + Debug.LogWarning("Please select unity assets file."); + return null; + } + return openPath; + } - /// - /// 显示进度框 - /// - public static void DisplayProgressBar(string tips, int progressValue, int totalValue) - { - EditorUtility.DisplayProgressBar("进度", $"{tips} : {progressValue}/{totalValue}", (float)progressValue / totalValue); - } + /// + /// 显示进度框 + /// + public static void DisplayProgressBar(string tips, int progressValue, int totalValue) + { + EditorUtility.DisplayProgressBar("进度", $"{tips} : {progressValue}/{totalValue}", (float)progressValue / totalValue); + } - /// - /// 隐藏进度框 - /// - public static void ClearProgressBar() - { - EditorUtility.ClearProgressBar(); - } - #endregion + /// + /// 隐藏进度框 + /// + public static void ClearProgressBar() + { + EditorUtility.ClearProgressBar(); + } + #endregion - #region EditorWindow - public static void FocusUnitySceneWindow() - { - EditorWindow.FocusWindowIfItsOpen(); - } - public static void CloseUnityGameWindow() - { - System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.GameView"); - EditorWindow.GetWindow(T, false, "GameView", true).Close(); - } - public static void FocusUnityGameWindow() - { - System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.GameView"); - EditorWindow.GetWindow(T, false, "GameView", true); - } - public static void FocueUnityProjectWindow() - { - System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.ProjectBrowser"); - EditorWindow.GetWindow(T, false, "Project", true); - } - public static void FocusUnityHierarchyWindow() - { - System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.SceneHierarchyWindow"); - EditorWindow.GetWindow(T, false, "Hierarchy", true); - } - public static void FocusUnityInspectorWindow() - { - System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.InspectorWindow"); - EditorWindow.GetWindow(T, false, "Inspector", true); - } - public static void FocusUnityConsoleWindow() - { - System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.ConsoleWindow"); - EditorWindow.GetWindow(T, false, "Console", true); - } - #endregion + #region EditorWindow + public static void FocusUnitySceneWindow() + { + EditorWindow.FocusWindowIfItsOpen(); + } + public static void CloseUnityGameWindow() + { + System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.GameView"); + EditorWindow.GetWindow(T, false, "GameView", true).Close(); + } + public static void FocusUnityGameWindow() + { + System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.GameView"); + EditorWindow.GetWindow(T, false, "GameView", true); + } + public static void FocueUnityProjectWindow() + { + System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.ProjectBrowser"); + EditorWindow.GetWindow(T, false, "Project", true); + } + public static void FocusUnityHierarchyWindow() + { + System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.SceneHierarchyWindow"); + EditorWindow.GetWindow(T, false, "Hierarchy", true); + } + public static void FocusUnityInspectorWindow() + { + System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.InspectorWindow"); + EditorWindow.GetWindow(T, false, "Inspector", true); + } + public static void FocusUnityConsoleWindow() + { + System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.ConsoleWindow"); + EditorWindow.GetWindow(T, false, "Console", true); + } + #endregion - #region EditorConsole - private static MethodInfo _clearConsoleMethod; - private static MethodInfo ClearConsoleMethod - { - get - { - if (_clearConsoleMethod == null) - { - Assembly assembly = Assembly.GetAssembly(typeof(SceneView)); - System.Type logEntries = assembly.GetType("UnityEditor.LogEntries"); - _clearConsoleMethod = logEntries.GetMethod("Clear"); - } - return _clearConsoleMethod; - } - } + #region EditorConsole + private static MethodInfo _clearConsoleMethod; + private static MethodInfo ClearConsoleMethod + { + get + { + if (_clearConsoleMethod == null) + { + Assembly assembly = Assembly.GetAssembly(typeof(SceneView)); + System.Type logEntries = assembly.GetType("UnityEditor.LogEntries"); + _clearConsoleMethod = logEntries.GetMethod("Clear"); + } + return _clearConsoleMethod; + } + } - /// - /// 清空控制台 - /// - public static void ClearUnityConsole() - { - ClearConsoleMethod.Invoke(new object(), null); - } - #endregion + /// + /// 清空控制台 + /// + public static void ClearUnityConsole() + { + ClearConsoleMethod.Invoke(new object(), null); + } + #endregion - #region SceneUtility - public static bool HasDirtyScenes() - { - var sceneCount = EditorSceneManager.sceneCount; - for (var i = 0; i < sceneCount; ++i) - { - var scene = EditorSceneManager.GetSceneAt(i); - if (scene.isDirty) - return true; - } - return false; - } - #endregion + #region SceneUtility + public static bool HasDirtyScenes() + { + var sceneCount = EditorSceneManager.sceneCount; + for (var i = 0; i < sceneCount; ++i) + { + var scene = EditorSceneManager.GetSceneAt(i); + if (scene.isDirty) + return true; + } + return false; + } + #endregion - #region StringUtility - public static string RemoveFirstChar(string str) - { - if (string.IsNullOrEmpty(str)) - return str; - return str.Substring(1); - } - public static string RemoveLastChar(string str) - { - if (string.IsNullOrEmpty(str)) - return str; - return str.Substring(0, str.Length - 1); - } - public static List StringToStringList(string str, char separator) - { - List result = new List(); - if (!String.IsNullOrEmpty(str)) - { - string[] splits = str.Split(separator); - foreach (string split in splits) - { - string value = split.Trim(); //移除首尾空格 - if (!String.IsNullOrEmpty(value)) - { - result.Add(value); - } - } - } - return result; - } - public static T NameToEnum(string name) - { - if (Enum.IsDefined(typeof(T), name) == false) - { - throw new ArgumentException($"Enum {typeof(T)} is not defined name {name}"); - } - return (T)Enum.Parse(typeof(T), name); - } - #endregion + #region StringUtility + public static string RemoveFirstChar(string str) + { + if (string.IsNullOrEmpty(str)) + return str; + return str.Substring(1); + } + public static string RemoveLastChar(string str) + { + if (string.IsNullOrEmpty(str)) + return str; + return str.Substring(0, str.Length - 1); + } + public static List StringToStringList(string str, char separator) + { + List result = new List(); + if (!String.IsNullOrEmpty(str)) + { + string[] splits = str.Split(separator); + foreach (string split in splits) + { + string value = split.Trim(); //移除首尾空格 + if (!String.IsNullOrEmpty(value)) + { + result.Add(value); + } + } + } + return result; + } + public static T NameToEnum(string name) + { + if (Enum.IsDefined(typeof(T), name) == false) + { + throw new ArgumentException($"Enum {typeof(T)} is not defined name {name}"); + } + return (T)Enum.Parse(typeof(T), name); + } + #endregion - #region 文件 - /// - /// 创建文件所在的目录 - /// - /// 文件路径 - public static void CreateFileDirectory(string filePath) - { - string destDirectory = Path.GetDirectoryName(filePath); - CreateDirectory(destDirectory); - } + #region 文件 + /// + /// 创建文件所在的目录 + /// + /// 文件路径 + public static void CreateFileDirectory(string filePath) + { + string destDirectory = Path.GetDirectoryName(filePath); + CreateDirectory(destDirectory); + } - /// - /// 创建文件夹 - /// - public static bool CreateDirectory(string directory) - { - if (Directory.Exists(directory) == false) - { - Directory.CreateDirectory(directory); - return true; - } - else - { - return false; - } - } + /// + /// 创建文件夹 + /// + public static bool CreateDirectory(string directory) + { + if (Directory.Exists(directory) == false) + { + Directory.CreateDirectory(directory); + return true; + } + else + { + return false; + } + } - /// - /// 删除文件夹及子目录 - /// - public static bool DeleteDirectory(string directory) - { - if (Directory.Exists(directory)) - { - Directory.Delete(directory, true); - return true; - } - else - { - return false; - } - } + /// + /// 删除文件夹及子目录 + /// + public static bool DeleteDirectory(string directory) + { + if (Directory.Exists(directory)) + { + Directory.Delete(directory, true); + return true; + } + else + { + return false; + } + } - /// - /// 文件重命名 - /// - public static void FileRename(string filePath, string newName) - { - string dirPath = Path.GetDirectoryName(filePath); - string destPath; - if (Path.HasExtension(filePath)) - { - string extentsion = Path.GetExtension(filePath); - destPath = $"{dirPath}/{newName}{extentsion}"; - } - else - { - destPath = $"{dirPath}/{newName}"; - } - FileInfo fileInfo = new FileInfo(filePath); - fileInfo.MoveTo(destPath); - } + /// + /// 文件重命名 + /// + public static void FileRename(string filePath, string newName) + { + string dirPath = Path.GetDirectoryName(filePath); + string destPath; + if (Path.HasExtension(filePath)) + { + string extentsion = Path.GetExtension(filePath); + destPath = $"{dirPath}/{newName}{extentsion}"; + } + else + { + destPath = $"{dirPath}/{newName}"; + } + FileInfo fileInfo = new FileInfo(filePath); + fileInfo.MoveTo(destPath); + } - /// - /// 移动文件 - /// - public static void MoveFile(string filePath, string destPath) - { - if (File.Exists(destPath)) - File.Delete(destPath); + /// + /// 移动文件 + /// + public static void MoveFile(string filePath, string destPath) + { + if (File.Exists(destPath)) + File.Delete(destPath); - FileInfo fileInfo = new FileInfo(filePath); - fileInfo.MoveTo(destPath); - } + FileInfo fileInfo = new FileInfo(filePath); + fileInfo.MoveTo(destPath); + } - /// - /// 拷贝文件夹 - /// 注意:包括所有子目录的文件 - /// - public static void CopyDirectory(string sourcePath, string destPath) - { - sourcePath = EditorTools.GetRegularPath(sourcePath); + /// + /// 拷贝文件夹 + /// 注意:包括所有子目录的文件 + /// + public static void CopyDirectory(string sourcePath, string destPath) + { + sourcePath = EditorTools.GetRegularPath(sourcePath); - // If the destination directory doesn't exist, create it. - if (Directory.Exists(destPath) == false) - Directory.CreateDirectory(destPath); + // If the destination directory doesn't exist, create it. + if (Directory.Exists(destPath) == false) + Directory.CreateDirectory(destPath); - string[] fileList = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories); - foreach (string file in fileList) - { - string temp = EditorTools.GetRegularPath(file); - string savePath = temp.Replace(sourcePath, destPath); - CopyFile(file, savePath, true); - } - } + string[] fileList = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories); + foreach (string file in fileList) + { + string temp = EditorTools.GetRegularPath(file); + string savePath = temp.Replace(sourcePath, destPath); + CopyFile(file, savePath, true); + } + } - /// - /// 拷贝文件 - /// - public static void CopyFile(string sourcePath, string destPath, bool overwrite) - { - if (File.Exists(sourcePath) == false) - throw new FileNotFoundException(sourcePath); + /// + /// 拷贝文件 + /// + public static void CopyFile(string sourcePath, string destPath, bool overwrite) + { + if (File.Exists(sourcePath) == false) + throw new FileNotFoundException(sourcePath); - // 创建目录 - CreateFileDirectory(destPath); + // 创建目录 + CreateFileDirectory(destPath); - // 复制文件 - File.Copy(sourcePath, destPath, overwrite); - } + // 复制文件 + File.Copy(sourcePath, destPath, overwrite); + } - /// - /// 清空文件夹 - /// - /// 要清理的文件夹路径 - public static void ClearFolder(string directoryPath) - { - if (Directory.Exists(directoryPath) == false) - return; + /// + /// 清空文件夹 + /// + /// 要清理的文件夹路径 + public static void ClearFolder(string directoryPath) + { + if (Directory.Exists(directoryPath) == false) + return; - // 删除文件 - string[] allFiles = Directory.GetFiles(directoryPath); - for (int i = 0; i < allFiles.Length; i++) - { - File.Delete(allFiles[i]); - } + // 删除文件 + string[] allFiles = Directory.GetFiles(directoryPath); + for (int i = 0; i < allFiles.Length; i++) + { + File.Delete(allFiles[i]); + } - // 删除文件夹 - string[] allFolders = Directory.GetDirectories(directoryPath); - for (int i = 0; i < allFolders.Length; i++) - { - Directory.Delete(allFolders[i], true); - } - } + // 删除文件夹 + string[] allFolders = Directory.GetDirectories(directoryPath); + for (int i = 0; i < allFolders.Length; i++) + { + Directory.Delete(allFolders[i], true); + } + } - /// - /// 获取文件字节大小 - /// - public static long GetFileSize(string filePath) - { - FileInfo fileInfo = new FileInfo(filePath); - return fileInfo.Length; - } + /// + /// 获取文件字节大小 + /// + public static long GetFileSize(string filePath) + { + FileInfo fileInfo = new FileInfo(filePath); + return fileInfo.Length; + } - /// - /// 读取文件的所有文本内容 - /// - public static string ReadFileAllText(string filePath) - { - if (File.Exists(filePath) == false) - return string.Empty; + /// + /// 获取文件的哈希值 + /// + public static string GetFileCRC32(string filePath) + { + return HashUtility.FileCRC32(filePath); + } - return File.ReadAllText(filePath, Encoding.UTF8); - } + /// + /// 读取文件的所有文本内容 + /// + public static string ReadFileAllText(string filePath) + { + if (File.Exists(filePath) == false) + return string.Empty; - /// - /// 读取文本的所有文本内容 - /// - public static string[] ReadFileAllLine(string filePath) - { - if (File.Exists(filePath) == false) - return null; + return File.ReadAllText(filePath, Encoding.UTF8); + } - return File.ReadAllLines(filePath, Encoding.UTF8); - } + /// + /// 读取文本的所有文本内容 + /// + public static string[] ReadFileAllLine(string filePath) + { + if (File.Exists(filePath) == false) + return null; - /// - /// 检测AssetBundle文件是否合法 - /// - public static bool CheckBundleFileValid(byte[] fileData) - { - string signature = ReadStringToNull(fileData, 20); - if (signature == "UnityFS" || signature == "UnityRaw" || signature == "UnityWeb" || signature == "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA") - return true; - else - return false; - } - private static string ReadStringToNull(byte[] data, int maxLength) - { - List bytes = new List(); - for (int i = 0; i < data.Length; i++) - { - if (i >= maxLength) - break; + return File.ReadAllLines(filePath, Encoding.UTF8); + } - byte bt = data[i]; - if (bt == 0) - break; + /// + /// 检测AssetBundle文件是否合法 + /// + public static bool CheckBundleFileValid(byte[] fileData) + { + string signature = ReadStringToNull(fileData, 20); + if (signature == "UnityFS" || signature == "UnityRaw" || signature == "UnityWeb" || signature == "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA") + return true; + else + return false; + } + private static string ReadStringToNull(byte[] data, int maxLength) + { + List bytes = new List(); + for (int i = 0; i < data.Length; i++) + { + if (i >= maxLength) + break; - bytes.Add(bt); - } + byte bt = data[i]; + if (bt == 0) + break; - if (bytes.Count == 0) - return string.Empty; - else - return Encoding.UTF8.GetString(bytes.ToArray()); - } - #endregion + bytes.Add(bt); + } - #region 路径 - /// - /// 获取规范的路径 - /// - public static string GetRegularPath(string path) - { - return path.Replace('\\', '/').Replace("\\", "/"); //替换为Linux路径格式 - } + if (bytes.Count == 0) + return string.Empty; + else + return Encoding.UTF8.GetString(bytes.ToArray()); + } + #endregion - /// - /// 获取项目工程路径 - /// - public static string GetProjectPath() - { - string projectPath = Path.GetDirectoryName(Application.dataPath); - return GetRegularPath(projectPath); - } + #region 路径 + /// + /// 获取规范的路径 + /// + public static string GetRegularPath(string path) + { + return path.Replace('\\', '/').Replace("\\", "/"); //替换为Linux路径格式 + } - /// - /// 转换文件的绝对路径为Unity资源路径 - /// 例如 D:\\YourPorject\\Assets\\Works\\file.txt 替换为 Assets/Works/file.txt - /// - public static string AbsolutePathToAssetPath(string absolutePath) - { - string content = GetRegularPath(absolutePath); - return Substring(content, "Assets/", true); - } + /// + /// 获取项目工程路径 + /// + public static string GetProjectPath() + { + string projectPath = Path.GetDirectoryName(Application.dataPath); + return GetRegularPath(projectPath); + } - /// - /// 转换Unity资源路径为文件的绝对路径 - /// 例如:Assets/Works/file.txt 替换为 D:\\YourPorject/Assets/Works/file.txt - /// - public static string AssetPathToAbsolutePath(string assetPath) - { - string projectPath = GetProjectPath(); - return $"{projectPath}/{assetPath}"; - } + /// + /// 转换文件的绝对路径为Unity资源路径 + /// 例如 D:\\YourPorject\\Assets\\Works\\file.txt 替换为 Assets/Works/file.txt + /// + public static string AbsolutePathToAssetPath(string absolutePath) + { + string content = GetRegularPath(absolutePath); + return Substring(content, "Assets/", true); + } - /// - /// 递归查找目标文件夹路径 - /// - /// 搜索的根目录 - /// 目标文件夹名称 - /// 返回找到的文件夹路径,如果没有找到返回空字符串 - public static string FindFolder(string root, string folderName) - { - DirectoryInfo rootInfo = new DirectoryInfo(root); - DirectoryInfo[] infoList = rootInfo.GetDirectories(); - for (int i = 0; i < infoList.Length; i++) - { - string fullPath = infoList[i].FullName; - if (infoList[i].Name == folderName) - return fullPath; + /// + /// 转换Unity资源路径为文件的绝对路径 + /// 例如:Assets/Works/file.txt 替换为 D:\\YourPorject/Assets/Works/file.txt + /// + public static string AssetPathToAbsolutePath(string assetPath) + { + string projectPath = GetProjectPath(); + return $"{projectPath}/{assetPath}"; + } - string result = FindFolder(fullPath, folderName); - if (string.IsNullOrEmpty(result) == false) - return result; - } - return string.Empty; - } + /// + /// 递归查找目标文件夹路径 + /// + /// 搜索的根目录 + /// 目标文件夹名称 + /// 返回找到的文件夹路径,如果没有找到返回空字符串 + public static string FindFolder(string root, string folderName) + { + DirectoryInfo rootInfo = new DirectoryInfo(root); + DirectoryInfo[] infoList = rootInfo.GetDirectories(); + for (int i = 0; i < infoList.Length; i++) + { + string fullPath = infoList[i].FullName; + if (infoList[i].Name == folderName) + return fullPath; - /// - /// 截取字符串 - /// 获取匹配到的后面内容 - /// - /// 内容 - /// 关键字 - /// 分割的结果里是否包含关键字 - /// 是否使用初始匹配的位置,否则使用末尾匹配的位置 - public static string Substring(string content, string key, bool includeKey, bool firstMatch = true) - { - if (string.IsNullOrEmpty(key)) - return content; + string result = FindFolder(fullPath, folderName); + if (string.IsNullOrEmpty(result) == false) + return result; + } + return string.Empty; + } - int startIndex = -1; - if (firstMatch) - startIndex = content.IndexOf(key); //返回子字符串第一次出现位置 - else - startIndex = content.LastIndexOf(key); //返回子字符串最后出现的位置 + /// + /// 截取字符串 + /// 获取匹配到的后面内容 + /// + /// 内容 + /// 关键字 + /// 分割的结果里是否包含关键字 + /// 是否使用初始匹配的位置,否则使用末尾匹配的位置 + public static string Substring(string content, string key, bool includeKey, bool firstMatch = true) + { + if (string.IsNullOrEmpty(key)) + return content; - // 如果没有找到匹配的关键字 - if (startIndex == -1) - return content; + int startIndex = -1; + if (firstMatch) + startIndex = content.IndexOf(key); //返回子字符串第一次出现位置 + else + startIndex = content.LastIndexOf(key); //返回子字符串最后出现的位置 - if (includeKey) - return content.Substring(startIndex); - else - return content.Substring(startIndex + key.Length); - } - #endregion - } + // 如果没有找到匹配的关键字 + if (startIndex == -1) + return content; + + if (includeKey) + return content.Substring(startIndex); + else + return content.Substring(startIndex + key.Length); + } + #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 6bdc244..b5bf902 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/HostPlayModeImpl.cs @@ -75,7 +75,7 @@ namespace YooAsset { if (_deliveryQueryServices == null) return false; - return _deliveryQueryServices.Query(PackageName, packageBundle.FileName); + return _deliveryQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); } private bool IsCachedPackageBundle(PackageBundle packageBundle) { @@ -83,7 +83,7 @@ namespace YooAsset } private bool IsBuildinPackageBundle(PackageBundle packageBundle) { - return _buildinQueryServices.Query(PackageName, packageBundle.FileName); + return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); } #region IPlayMode接口 diff --git a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs index 6af106f..106ee6c 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/PlayMode/WebPlayModeImpl.cs @@ -67,7 +67,7 @@ namespace YooAsset // 查询相关 private bool IsBuildinPackageBundle(PackageBundle packageBundle) { - return _buildinQueryServices.Query(PackageName, packageBundle.FileName); + return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC); } #region IPlayMode接口 diff --git a/Assets/YooAsset/Runtime/Services/IBuildinQueryServices.cs b/Assets/YooAsset/Runtime/Services/IBuildinQueryServices.cs index 0e30f92..81250ca 100644 --- a/Assets/YooAsset/Runtime/Services/IBuildinQueryServices.cs +++ b/Assets/YooAsset/Runtime/Services/IBuildinQueryServices.cs @@ -1,11 +1,15 @@  namespace YooAsset { - public interface IBuildinQueryServices - { - /// - /// 查询是否为应用程序内置的资源文件 - /// - bool Query(string packageName, string fileName); - } + public interface IBuildinQueryServices + { + /// + /// 查询是否为应用程序内置的资源文件 + /// + /// 包裹名称 + /// 文件名称(包含文件的后缀格式) + /// 文件哈希值 + /// 返回查询结果 + bool Query(string packageName, string fileName, string fileCRC); + } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IDeliveryQueryServices.cs b/Assets/YooAsset/Runtime/Services/IDeliveryQueryServices.cs index 7add9d0..79a2b8e 100644 --- a/Assets/YooAsset/Runtime/Services/IDeliveryQueryServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDeliveryQueryServices.cs @@ -1,16 +1,23 @@  namespace YooAsset { - public interface IDeliveryQueryServices - { - /// - /// 查询是否为开发者分发的资源文件 - /// - bool Query(string packageName, string fileName); + public interface IDeliveryQueryServices + { + /// + /// 查询是否为开发者分发的资源文件 + /// + /// 包裹名称 + /// 文件名称(包含文件的后缀格式) + /// 文件哈希值 + /// 返回查询结果 + bool Query(string packageName, string fileName, string fileCRC); - /// - /// 获取分发资源文件的路径 - /// - string GetFilePath(string packageName, string fileName); - } + /// + /// 获取分发资源文件的路径 + /// + /// 包裹名称 + /// 文件名称(包含文件的后缀格式) + /// 返回资源文件的路径 + string GetFilePath(string packageName, string fileName); + } } \ No newline at end of file