using System; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; namespace TEngine { [Serializable] public struct fileMd5 { public string fileName; public string md5; public long fileSize; } [Serializable] public class Serialization { [SerializeField] List _target; public List ToList() { return _target; } public Serialization(List target) { this._target = target; } } public static class FileSystem { public const string ArtResourcePath = "Assets/ArtResources"; public const string GameResourcePath = AssetConfig.AssetRootPath; internal static Dictionary _fileFixList = new Dictionary(); private static string _persistentDataPath = null; private static string _resRootPath = null; private static string _resRootStreamAssetPath = null; public const string BuildPath = "Build"; public const string AssetBundleBuildPath = BuildPath + "/AssetBundles"; private const string AssetBundleTargetPath = "{0}/AssetBundles"; public const string Md5List = "Md5List.json"; /// /// 资源更新读取根目录 /// /// public static string ResourceRoot { get { if (string.IsNullOrEmpty(_resRootPath)) { _resRootPath = Path.Combine(PersistentDataPath, "TEngine"); } if (!Directory.Exists(_resRootPath)) { Directory.CreateDirectory(_resRootPath); } return _resRootPath.FixPath(); } } /// /// 持久化数据存储路径 /// public static string PersistentDataPath { get { if (string.IsNullOrEmpty(_persistentDataPath)) { #if UNITY_EDITOR_WIN _persistentDataPath = Application.dataPath + "/../TEnginePersistentDataPath"; if (!Directory.Exists(_persistentDataPath)) { Directory.CreateDirectory(_persistentDataPath); } #else _persistentDataPath = Application.persistentDataPath; #endif } return _persistentDataPath.FixPath(); } } /// /// 资源更新读取StreamAsset根目录 /// /// public static string ResourceRootInStreamAsset { get { if (string.IsNullOrEmpty(_resRootStreamAssetPath)) { _resRootStreamAssetPath = Path.Combine(Application.streamingAssetsPath, "TEngine"); } return _resRootStreamAssetPath.FixPath(); } } public static string GetAssetBundlePathInVersion(string bundlename) { //默认用外部目录 string path = FilePath($"{ResourceRoot}/AssetBundles/{bundlename}"); if (!File.Exists(path)) { path = $"{ResourceRootInStreamAsset}/AssetBundles/{bundlename}"; } return path; } public static string StreamAssetBundlePath { get { return string.Format(AssetBundleTargetPath, ResourceRootInStreamAsset); } } internal static string FilePath(string path) { path = path.FixPath(); #if UNITY_EDITOR_WIN path = path.Replace("Assets/../", ""); #endif if (_fileFixList.ContainsKey(path)) { return _fileFixList[path]; } else { return path; } } public static string FixPath(this string str) { str = str.Replace("\\", "/"); return str; } public static Stream OpenRead(string filePath) { #if UNITY_ANDROID && !UNITY_EDITOR byte[] bytes = ReadAllBytesFromOutOrInnerFolder(filePath); if (bytes != null) return new MemoryStream(bytes); else return null; #else return File.OpenRead(filePath); #endif } /// /// 从指定文件中读取字节串 /// /// 文件路径 /// public static byte[] ReadAllBytesFromOutOrInnerFolder(string filePath) { if (string.IsNullOrEmpty(filePath)) return null; #if UNITY_ANDROID && !UNITY_EDITOR //外部目录 if (filePath.StartsWith(Application.persistentDataPath)) { return ReadAllBytesFromOutFolder(filePath); } else //内部目录 { return ReadAllBytesFromInnerFolder(filePath); } #else return ReadAllBytesFromOutFolder(filePath); #endif } private static byte[] ReadAllBytesFromOutFolder(string filePath) { if (!File.Exists(filePath)) { TLogger.LogError("file:[{0}] is not exist!, please check!", filePath); return null; } return File.ReadAllBytes(filePath); } private static byte[] ReadAllBytesFromInnerFolder(string filePath) { #if !UNITY_ANDROID || UNITY_EDITOR filePath = $"file://{filePath}"; #endif UnityWebRequest www = UnityWebRequest.Get(filePath); UnityWebRequestAsyncOperation request = www.SendWebRequest(); while (!request.isDone) ; byte[] data = www.downloadHandler.data; www.downloadHandler.Dispose(); www.Dispose(); www = null; return data; } /// /// 读取内部目录 /// /// 文件完整路径 /// private static string ReadTextFromInnerFolder(string filePath) { #if !UNITY_ANDROID || UNITY_EDITOR filePath = $"file://{filePath}"; #endif UnityWebRequest www = UnityWebRequest.Get(filePath); UnityWebRequestAsyncOperation request = www.SendWebRequest(); while (!request.isDone) ; return www.downloadHandler.text; } } }