From c2a710622143469ac627671d880100400d2162c3 Mon Sep 17 00:00:00 2001 From: hevinci Date: Thu, 21 Dec 2023 16:18:36 +0800 Subject: [PATCH] fix #202 --- .../BuildinFileManifest.cs | 11 +- .../StreamingAssetsDefine.cs | 5 +- .../StreamingAssetsHelper.cs | 207 +++++++++++------- .../StreamingAssetsHelper2.cs | 151 ------------- .../StreamingAssetsHelper2.cs.meta | 11 - 5 files changed, 142 insertions(+), 243 deletions(-) delete mode 100644 Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs delete mode 100644 Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs.meta diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/BuildinFileManifest.cs b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/BuildinFileManifest.cs index c36d20f..174b52d 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/BuildinFileManifest.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/BuildinFileManifest.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using UnityEngine; @@ -6,5 +7,13 @@ using UnityEngine; /// public class BuildinFileManifest : ScriptableObject { - public List BuildinFiles = new List(); + [Serializable] + public class Element + { + public string PackageName; + public string FileName; + public string FileCRC32; + } + + public List BuildinFiles = new List(); } \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsDefine.cs b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsDefine.cs index 739aa3e..043a8aa 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsDefine.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsDefine.cs @@ -1,5 +1,8 @@ - + public class StreamingAssetsDefine { + /// + /// 根目录名称(保持和YooAssets资源系统一致) + /// public const string RootFolderName = "yoo"; } \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper.cs b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper.cs index a166ac2..ceef92c 100644 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper.cs +++ b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper.cs @@ -8,63 +8,102 @@ using YooAsset; /// public class GameQueryServices : IBuildinQueryServices { - public bool Query(string packageName, string fileName) - { - // 注意:fileName包含文件格式 - return StreamingAssetsHelper.FileExists(packageName, fileName); - } + /// + /// 查询内置文件的时候,是否比对文件哈希值 + /// + public static bool CompareFileCRC = false; + + public bool Query(string packageName, string fileName, string fileCRC) + { + // 注意:fileName包含文件格式 + return StreamingAssetsHelper.FileExists(packageName, fileName, fileCRC); + } } #if UNITY_EDITOR -/// -/// StreamingAssets目录下资源查询帮助类 -/// public sealed class StreamingAssetsHelper { - public static void Init() { } - public static bool FileExists(string packageName, string fileName) - { - string filePath = Path.Combine(Application.streamingAssetsPath, StreamingAssetsDefine.RootFolderName, packageName, fileName); - return File.Exists(filePath); - } + public static void Init() { } + public static bool FileExists(string packageName, string fileName, string fileCRC) + { + string filePath = Path.Combine(Application.streamingAssetsPath, StreamingAssetsDefine.RootFolderName, packageName, fileName); + if (File.Exists(filePath)) + { + if (GameQueryServices.CompareFileCRC) + { + string crc32 = YooAsset.Editor.EditorTools.GetFileCRC32(filePath); + return crc32 == fileCRC; + } + else + { + return true; + } + } + else + { + return false; + } + } } #else -/// -/// StreamingAssets目录下资源查询帮助类 -/// public sealed class StreamingAssetsHelper { - private static bool _isInit = false; - private static readonly HashSet _cacheData = new HashSet(); + private class PackageQuery + { + public readonly Dictionary Elements = new Dictionary(1000); + } - /// - /// 初始化 - /// - public static void Init() - { - if (_isInit == false) - { - _isInit = true; - var manifest = Resources.Load("BuildinFileManifest"); - if (manifest != null) - { - foreach (string fileName in manifest.BuildinFiles) - { - _cacheData.Add(fileName); - } - } - } - } + private static bool _isInit = false; + private static readonly Dictionary _packages = new Dictionary(10); - /// - /// 内置文件查询方法 - /// - public static bool FileExists(string packageName, string fileName) - { - if (_isInit == false) - Init(); - return _cacheData.Contains(fileName); - } + /// + /// 初始化 + /// + public static void Init() + { + if (_isInit == false) + { + _isInit = true; + + var manifest = Resources.Load("BuildinFileManifest"); + if (manifest != null) + { + foreach (var element in manifest.BuildinFiles) + { + if (_packages.TryGetValue(element.PackageName, out PackageQuery package) == false) + { + package = new PackageQuery(); + _packages.Add(element.PackageName, package); + } + package.Elements.Add(element.FileName, element); + } + } + } + } + + /// + /// 内置文件查询方法 + /// + public static bool FileExists(string packageName, string fileName, string fileCRC32) + { + if (_isInit == false) + Init(); + + if (_packages.TryGetValue(packageName, out PackageQuery package) == false) + return false; + + if (package.Elements.TryGetValue(fileName, out var element) == false) + return false; + + if (GameQueryServices.CompareFileCRC) + { + return element.FileCRC32 == fileCRC32; + } + else + { + return true; + } + } } #endif @@ -72,42 +111,52 @@ public sealed class StreamingAssetsHelper #if UNITY_EDITOR internal class PreprocessBuild : UnityEditor.Build.IPreprocessBuildWithReport { - public int callbackOrder { get { return 0; } } + public int callbackOrder { get { return 0; } } - /// - /// 在构建应用程序前处理 - /// - public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report) - { - string saveFilePath = "Assets/Resources/BuildinFileManifest.asset"; - if (File.Exists(saveFilePath)) - File.Delete(saveFilePath); + /// + /// 在构建应用程序前处理 + /// 原理:在构建APP之前,搜索StreamingAssets目录下的所有资源文件,然后将这些文件信息写入内置清单,内置清单存储在Resources文件夹下。 + /// + public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report) + { + string saveFilePath = "Assets/Resources/BuildinFileManifest.asset"; + if (File.Exists(saveFilePath)) + { + File.Delete(saveFilePath); + UnityEditor.AssetDatabase.SaveAssets(); + UnityEditor.AssetDatabase.Refresh(); + } - string folderPath = $"{Application.dataPath}/StreamingAssets/{StreamingAssetsDefine.RootFolderName}"; - DirectoryInfo root = new DirectoryInfo(folderPath); - if (root.Exists == false) - { - Debug.Log($"没有发现YooAsset内置目录 : {folderPath}"); - return; - } + string folderPath = $"{Application.dataPath}/StreamingAssets/{StreamingAssetsDefine.RootFolderName}"; + DirectoryInfo root = new DirectoryInfo(folderPath); + if (root.Exists == false) + { + Debug.LogWarning($"没有发现YooAsset内置目录 : {folderPath}"); + return; + } - var manifest = ScriptableObject.CreateInstance(); - FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories); - foreach (var fileInfo in files) - { - if (fileInfo.Extension == ".meta") - continue; - if (fileInfo.Name.StartsWith("PackageManifest_")) - continue; - manifest.BuildinFiles.Add(fileInfo.Name); - } + var manifest = ScriptableObject.CreateInstance(); + FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories); + foreach (var fileInfo in files) + { + if (fileInfo.Extension == ".meta") + continue; + if (fileInfo.Name.StartsWith("PackageManifest_")) + continue; - if (Directory.Exists("Assets/Resources") == false) - Directory.CreateDirectory("Assets/Resources"); - UnityEditor.AssetDatabase.CreateAsset(manifest, saveFilePath); - UnityEditor.AssetDatabase.SaveAssets(); - UnityEditor.AssetDatabase.Refresh(); - Debug.Log($"一共{manifest.BuildinFiles.Count}个内置文件,内置资源清单保存成功 : {saveFilePath}"); - } + BuildinFileManifest.Element element = new BuildinFileManifest.Element(); + element.PackageName = fileInfo.Directory.Name; + element.FileCRC32 = YooAsset.Editor.EditorTools.GetFileCRC32(fileInfo.FullName); + element.FileName = fileInfo.Name; + manifest.BuildinFiles.Add(element); + } + + if (Directory.Exists("Assets/Resources") == false) + Directory.CreateDirectory("Assets/Resources"); + UnityEditor.AssetDatabase.CreateAsset(manifest, saveFilePath); + UnityEditor.AssetDatabase.SaveAssets(); + UnityEditor.AssetDatabase.Refresh(); + Debug.Log($"一共{manifest.BuildinFiles.Count}个内置文件,内置资源清单保存成功 : {saveFilePath}"); + } } #endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs deleted file mode 100644 index 987f1b8..0000000 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs +++ /dev/null @@ -1,151 +0,0 @@ -//------------------------------------- -// 作者:Stark -//------------------------------------- -using System.Collections.Generic; -using UnityEngine; -using YooAsset; - -/* -/// -/// 资源文件查询服务类 -/// -public class GameQueryServices2 : IBuildinQueryServices -{ - public bool QueryStreamingAssets(string packageName, string fileName) - { - return StreamingAssetsHelper2.FileExists($"{StreamingAssetsDefine.RootFolderName}/{packageName}/{fileName}"); - } -} - -/// -/// StreamingAssets目录下资源查询帮助类 -/// -public sealed class StreamingAssetsHelper2 -{ - private static readonly Dictionary _cacheData = new Dictionary(1000); - -#if UNITY_ANDROID && !UNITY_EDITOR - private static AndroidJavaClass _unityPlayerClass; - public static AndroidJavaClass UnityPlayerClass - { - get - { - if (_unityPlayerClass == null) - _unityPlayerClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"); - return _unityPlayerClass; - } - } - - private static AndroidJavaObject _currentActivity; - public static AndroidJavaObject CurrentActivity - { - get - { - if (_currentActivity == null) - _currentActivity = UnityPlayerClass.GetStatic("currentActivity"); - return _currentActivity; - } - } - - /// - /// 利用安卓原生接口查询内置文件是否存在 - /// - public static bool FileExists(string filePath) - { - if (_cacheData.TryGetValue(filePath, out bool result) == false) - { - result = CurrentActivity.Call("CheckAssetExist", filePath); - _cacheData.Add(filePath, result); - } - return result; - } -#else - public static bool FileExists(string filePath) - { - if (_cacheData.TryGetValue(filePath, out bool result) == false) - { - result = System.IO.File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, filePath)); - _cacheData.Add(filePath, result); - } - return result; - } -#endif -} - - -#if UNITY_ANDROID && UNITY_EDITOR -/// -/// 为Github对开发者的友好,采用自动补充UnityPlayerActivity.java文件的通用姿势满足各个开发者 -/// -internal class AndroidPost : UnityEditor.Android.IPostGenerateGradleAndroidProject -{ - public int callbackOrder => 99; - public void OnPostGenerateGradleAndroidProject(string path) - { - path = path.Replace("\\", "/"); - string untityActivityFilePath = $"{path}/src/main/java/com/unity3d/player/UnityPlayerActivity.java"; - var readContent = System.IO.File.ReadAllLines(untityActivityFilePath); - string postContent = - " //auto-gen-function \n" + - " public boolean CheckAssetExist(String filePath) \n" + - " { \n" + - " android.content.res.AssetManager assetManager = getAssets(); \n" + - " try \n" + - " { \n" + - " java.io.InputStream inputStream = assetManager.open(filePath); \n" + - " if (null != inputStream) \n" + - " { \n" + - " inputStream.close(); \n" + - " return true; \n" + - " } \n" + - " } \n" + - " catch(java.io.IOException e) \n" + - " { \n" + - " } \n" + - " return false; \n" + - " } \n" + - "}"; - - if (CheckFunctionExist(readContent) == false) - readContent[readContent.Length - 1] = postContent; - System.IO.File.WriteAllLines(untityActivityFilePath, readContent); - } - private bool CheckFunctionExist(string[] contents) - { - for (int i = 0; i < contents.Length; i++) - { - if (contents[i].Contains("CheckAssetExist")) - { - return true; - } - } - return false; - } -} -#endif -*/ - - - -/* -// 以下代码为安卓原生代码,不需要解除注释 -//auto-gen-function -public boolean CheckAssetExist(String filePath) -{ - android.content.res.AssetManager assetManager = getAssets(); - try - { - java.io.InputStream inputStream = assetManager.open(filePath); - if(null != inputStream) - { - inputStream.close(); - return true; - } - } - catch(java.io.IOException e) - { - //e.printStackTrace(); - } - return false; -} -*/ \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs.meta b/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs.meta deleted file mode 100644 index 774fc7f..0000000 --- a/Assets/YooAsset/Samples~/Space Shooter/ThirdParty/StreamingAssetsHelper/StreamingAssetsHelper2.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 358145d67e230b34883002b08b23cba3 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: