DefaultCacheFileSystem.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace YooAsset
  6. {
  7. /// <summary>
  8. /// 缓存文件系统
  9. /// 说明:正在进行的下载器会在ResourcePackage销毁的时候执行Abort操作!
  10. /// </summary>
  11. internal class DefaultCacheFileSystem : IFileSystem
  12. {
  13. protected readonly Dictionary<string, RecordFileElement> _records = new Dictionary<string, RecordFileElement>(10000);
  14. protected readonly Dictionary<string, string> _bundleDataFilePathMapping = new Dictionary<string, string>(10000);
  15. protected readonly Dictionary<string, string> _bundleInfoFilePathMapping = new Dictionary<string, string>(10000);
  16. protected readonly Dictionary<string, string> _tempFilePathMapping = new Dictionary<string, string>(10000);
  17. protected string _packageRoot;
  18. protected string _tempFilesRoot;
  19. protected string _cacheBundleFilesRoot;
  20. protected string _cacheManifestFilesRoot;
  21. /// <summary>
  22. /// 下载中心
  23. /// 说明:当异步操作任务终止的时候,所有下载子任务都会一同被终止!
  24. /// </summary>
  25. public DownloadCenterOperation DownloadCenter { set; get; }
  26. /// <summary>
  27. /// 包裹名称
  28. /// </summary>
  29. public string PackageName { private set; get; }
  30. /// <summary>
  31. /// 文件根目录
  32. /// </summary>
  33. public string FileRoot
  34. {
  35. get
  36. {
  37. return _packageRoot;
  38. }
  39. }
  40. /// <summary>
  41. /// 文件数量
  42. /// </summary>
  43. public int FileCount
  44. {
  45. get
  46. {
  47. return _records.Count;
  48. }
  49. }
  50. #region 自定义参数
  51. /// <summary>
  52. /// 自定义参数:远程服务接口
  53. /// </summary>
  54. public IRemoteServices RemoteServices { private set; get; } = null;
  55. /// <summary>
  56. /// 自定义参数:初始化的时候缓存文件校验级别
  57. /// </summary>
  58. public EFileVerifyLevel FileVerifyLevel { private set; get; } = EFileVerifyLevel.Middle;
  59. /// <summary>
  60. /// 自定义参数:数据文件追加文件格式
  61. /// </summary>
  62. public bool AppendFileExtension { private set; get; } = false;
  63. /// <summary>
  64. /// 自定义参数:最大并发连接数
  65. /// </summary>
  66. public int DownloadMaxConcurrency { private set; get; } = int.MaxValue;
  67. /// <summary>
  68. /// 自定义参数:每帧发起的最大请求数
  69. /// </summary>
  70. public int DownloadMaxRequestPerFrame { private set; get; } = int.MaxValue;
  71. /// <summary>
  72. /// 自定义参数:启用断点续传的最小尺寸
  73. /// </summary>
  74. public long ResumeDownloadMinimumSize { private set; get; } = long.MaxValue;
  75. /// <summary>
  76. /// 自定义参数:断点续传下载器关注的错误码
  77. /// </summary>
  78. public List<long> ResumeDownloadResponseCodes { private set; get; } = null;
  79. /// <summary>
  80. /// 自定义参数:解密方法类
  81. /// </summary>
  82. public IDecryptionServices DecryptionServices { private set; get; }
  83. #endregion
  84. public DefaultCacheFileSystem()
  85. {
  86. }
  87. public virtual FSInitializeFileSystemOperation InitializeFileSystemAsync()
  88. {
  89. var operation = new DCFSInitializeOperation(this);
  90. return operation;
  91. }
  92. public virtual FSLoadPackageManifestOperation LoadPackageManifestAsync(string packageVersion, int timeout)
  93. {
  94. var operation = new DCFSLoadPackageManifestOperation(this, packageVersion, timeout);
  95. return operation;
  96. }
  97. public virtual FSRequestPackageVersionOperation RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
  98. {
  99. var operation = new DCFSRequestPackageVersionOperation(this, appendTimeTicks, timeout);
  100. return operation;
  101. }
  102. public virtual FSClearCacheFilesOperation ClearCacheFilesAsync(PackageManifest manifest, string clearMode, object clearParam)
  103. {
  104. if (clearMode == EFileClearMode.ClearAllBundleFiles.ToString())
  105. {
  106. var operation = new ClearAllCacheBundleFilesOperation(this);
  107. return operation;
  108. }
  109. else if (clearMode == EFileClearMode.ClearUnusedBundleFiles.ToString())
  110. {
  111. var operation = new ClearUnusedCacheBundleFilesOperation(this, manifest);
  112. return operation;
  113. }
  114. else if (clearMode == EFileClearMode.ClearBundleFilesByTags.ToString())
  115. {
  116. var operation = new ClearCacheBundleFilesByTagsOperaiton(this, manifest, clearParam);
  117. return operation;
  118. }
  119. else if (clearMode == EFileClearMode.ClearAllManifestFiles.ToString())
  120. {
  121. var operation = new ClearAllCacheManifestFilesOperation(this);
  122. return operation;
  123. }
  124. else if (clearMode == EFileClearMode.ClearUnusedManifestFiles.ToString())
  125. {
  126. var operation = new ClearUnusedCacheManifestFilesOperation(this, manifest);
  127. return operation;
  128. }
  129. else
  130. {
  131. string error = $"Invalid clear mode : {clearMode}";
  132. var operation = new FSClearCacheFilesCompleteOperation(error);
  133. return operation;
  134. }
  135. }
  136. public virtual FSDownloadFileOperation DownloadFileAsync(PackageBundle bundle, DownloadParam param)
  137. {
  138. var downloader = DownloadCenter.DownloadFileAsync(bundle, param);
  139. downloader.Reference(); //增加下载器的引用计数
  140. // 注意:将下载器进行包裹,可以避免父类任务终止的时候,连带子任务里的下载器也一起被终止!
  141. var wrapper = new DownloadFileWrapper(downloader);
  142. return wrapper;
  143. }
  144. public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
  145. {
  146. if (bundle.BundleType == (int)EBuildBundleType.AssetBundle)
  147. {
  148. var operation = new DCFSLoadAssetBundleOperation(this, bundle);
  149. return operation;
  150. }
  151. else if (bundle.BundleType == (int)EBuildBundleType.RawBundle)
  152. {
  153. var operation = new DCFSLoadRawBundleOperation(this, bundle);
  154. return operation;
  155. }
  156. else
  157. {
  158. string error = $"{nameof(DefaultCacheFileSystem)} not support load bundle type : {bundle.BundleType}";
  159. var operation = new FSLoadBundleCompleteOperation(error);
  160. return operation;
  161. }
  162. }
  163. public virtual void SetParameter(string name, object value)
  164. {
  165. if (name == FileSystemParametersDefine.REMOTE_SERVICES)
  166. {
  167. RemoteServices = (IRemoteServices)value;
  168. }
  169. else if (name == FileSystemParametersDefine.FILE_VERIFY_LEVEL)
  170. {
  171. FileVerifyLevel = (EFileVerifyLevel)value;
  172. }
  173. else if (name == FileSystemParametersDefine.APPEND_FILE_EXTENSION)
  174. {
  175. AppendFileExtension = Convert.ToBoolean(value);
  176. }
  177. else if (name == FileSystemParametersDefine.DOWNLOAD_MAX_CONCURRENCY)
  178. {
  179. DownloadMaxConcurrency = Convert.ToInt32(value);
  180. }
  181. else if (name == FileSystemParametersDefine.DOWNLOAD_MAX_REQUEST_PER_FRAME)
  182. {
  183. DownloadMaxRequestPerFrame = Convert.ToInt32(value);
  184. }
  185. else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_MINMUM_SIZE)
  186. {
  187. ResumeDownloadMinimumSize = Convert.ToInt64(value);
  188. }
  189. else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_RESPONSE_CODES)
  190. {
  191. ResumeDownloadResponseCodes = (List<long>)value;
  192. }
  193. else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
  194. {
  195. DecryptionServices = (IDecryptionServices)value;
  196. }
  197. else
  198. {
  199. YooLogger.Warning($"Invalid parameter : {name}");
  200. }
  201. }
  202. public virtual void OnCreate(string packageName, string packageRoot)
  203. {
  204. PackageName = packageName;
  205. if (string.IsNullOrEmpty(packageRoot))
  206. _packageRoot = GetDefaultCachePackageRoot(packageName);
  207. else
  208. _packageRoot = packageRoot;
  209. _cacheBundleFilesRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.BundleFilesFolderName);
  210. _tempFilesRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.TempFilesFolderName);
  211. _cacheManifestFilesRoot = PathUtility.Combine(_packageRoot, DefaultCacheFileSystemDefine.ManifestFilesFolderName);
  212. }
  213. public virtual void OnDestroy()
  214. {
  215. if (DownloadCenter != null)
  216. {
  217. DownloadCenter.AbortOperation();
  218. DownloadCenter = null;
  219. }
  220. }
  221. public virtual bool Belong(PackageBundle bundle)
  222. {
  223. // 注意:缓存文件系统保底加载!
  224. return true;
  225. }
  226. public virtual bool Exists(PackageBundle bundle)
  227. {
  228. return _records.ContainsKey(bundle.BundleGUID);
  229. }
  230. public virtual bool NeedDownload(PackageBundle bundle)
  231. {
  232. if (Belong(bundle) == false)
  233. return false;
  234. return Exists(bundle) == false;
  235. }
  236. public virtual bool NeedUnpack(PackageBundle bundle)
  237. {
  238. return false;
  239. }
  240. public virtual bool NeedImport(PackageBundle bundle)
  241. {
  242. if (Belong(bundle) == false)
  243. return false;
  244. return Exists(bundle) == false;
  245. }
  246. public virtual string GetBundleFilePath(PackageBundle bundle)
  247. {
  248. return GetCacheBundleFileLoadPath(bundle);
  249. }
  250. public virtual byte[] ReadBundleFileData(PackageBundle bundle)
  251. {
  252. if (Exists(bundle) == false)
  253. return null;
  254. if (bundle.Encrypted)
  255. {
  256. if (DecryptionServices == null)
  257. {
  258. YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
  259. return null;
  260. }
  261. string filePath = GetCacheBundleFileLoadPath(bundle);
  262. var fileInfo = new DecryptFileInfo()
  263. {
  264. BundleName = bundle.BundleName,
  265. FileLoadCRC = bundle.UnityCRC,
  266. FileLoadPath = filePath,
  267. };
  268. return DecryptionServices.ReadFileData(fileInfo);
  269. }
  270. else
  271. {
  272. string filePath = GetCacheBundleFileLoadPath(bundle);
  273. return FileUtility.ReadAllBytes(filePath);
  274. }
  275. }
  276. public virtual string ReadBundleFileText(PackageBundle bundle)
  277. {
  278. if (Exists(bundle) == false)
  279. return null;
  280. if (bundle.Encrypted)
  281. {
  282. if (DecryptionServices == null)
  283. {
  284. YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
  285. return null;
  286. }
  287. string filePath = GetCacheBundleFileLoadPath(bundle);
  288. var fileInfo = new DecryptFileInfo()
  289. {
  290. BundleName = bundle.BundleName,
  291. FileLoadCRC = bundle.UnityCRC,
  292. FileLoadPath = filePath,
  293. };
  294. return DecryptionServices.ReadFileText(fileInfo);
  295. }
  296. else
  297. {
  298. string filePath = GetCacheBundleFileLoadPath(bundle);
  299. return FileUtility.ReadAllText(filePath);
  300. }
  301. }
  302. #region 缓存相关
  303. public List<string> GetAllCachedBundleGUIDs()
  304. {
  305. return _records.Keys.ToList();
  306. }
  307. public RecordFileElement GetRecordFileElement(PackageBundle bundle)
  308. {
  309. if (_records.TryGetValue(bundle.BundleGUID, out RecordFileElement element))
  310. return element;
  311. else
  312. return null;
  313. }
  314. public string GetTempFilePath(PackageBundle bundle)
  315. {
  316. if (_tempFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
  317. {
  318. filePath = PathUtility.Combine(_tempFilesRoot, bundle.BundleGUID);
  319. _tempFilePathMapping.Add(bundle.BundleGUID, filePath);
  320. }
  321. return filePath;
  322. }
  323. public string GetBundleDataFilePath(PackageBundle bundle)
  324. {
  325. if (_bundleDataFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
  326. {
  327. string folderName = bundle.FileHash.Substring(0, 2);
  328. filePath = PathUtility.Combine(_cacheBundleFilesRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.BundleDataFileName);
  329. if (AppendFileExtension)
  330. filePath += bundle.FileExtension;
  331. _bundleDataFilePathMapping.Add(bundle.BundleGUID, filePath);
  332. }
  333. return filePath;
  334. }
  335. public string GetBundleInfoFilePath(PackageBundle bundle)
  336. {
  337. if (_bundleInfoFilePathMapping.TryGetValue(bundle.BundleGUID, out string filePath) == false)
  338. {
  339. string folderName = bundle.FileHash.Substring(0, 2);
  340. filePath = PathUtility.Combine(_cacheBundleFilesRoot, folderName, bundle.BundleGUID, DefaultCacheFileSystemDefine.BundleInfoFileName);
  341. _bundleInfoFilePathMapping.Add(bundle.BundleGUID, filePath);
  342. }
  343. return filePath;
  344. }
  345. public bool IsRecordBundleFile(string bundleGUID)
  346. {
  347. return _records.ContainsKey(bundleGUID);
  348. }
  349. public bool RecordBundleFile(string bundleGUID, RecordFileElement element)
  350. {
  351. if (_records.ContainsKey(bundleGUID))
  352. {
  353. YooLogger.Error($"{nameof(DefaultCacheFileSystem)} has element : {bundleGUID}");
  354. return false;
  355. }
  356. _records.Add(bundleGUID, element);
  357. return true;
  358. }
  359. public EFileVerifyResult VerifyCacheFile(PackageBundle bundle)
  360. {
  361. if (_records.TryGetValue(bundle.BundleGUID, out RecordFileElement element) == false)
  362. return EFileVerifyResult.CacheNotFound;
  363. EFileVerifyResult result = FileVerifyHelper.FileVerify(element.DataFilePath, element.DataFileSize, element.DataFileCRC, EFileVerifyLevel.High);
  364. return result;
  365. }
  366. public bool WriteCacheBundleFile(PackageBundle bundle, string copyPath)
  367. {
  368. if (_records.ContainsKey(bundle.BundleGUID))
  369. {
  370. throw new Exception("Should never get here !");
  371. }
  372. string infoFilePath = GetBundleInfoFilePath(bundle);
  373. string dataFilePath = GetBundleDataFilePath(bundle);
  374. try
  375. {
  376. if (File.Exists(infoFilePath))
  377. File.Delete(infoFilePath);
  378. if (File.Exists(dataFilePath))
  379. File.Delete(dataFilePath);
  380. FileUtility.CreateFileDirectory(dataFilePath);
  381. // 拷贝数据文件
  382. FileInfo fileInfo = new FileInfo(copyPath);
  383. fileInfo.CopyTo(dataFilePath);
  384. // 写入文件信息
  385. WriteBundleInfoFile(infoFilePath, bundle.FileCRC, bundle.FileSize);
  386. }
  387. catch (Exception e)
  388. {
  389. YooLogger.Error($"Failed to write cache file ! {e.Message}");
  390. return false;
  391. }
  392. var recordFileElement = new RecordFileElement(infoFilePath, dataFilePath, bundle.FileCRC, bundle.FileSize);
  393. return RecordBundleFile(bundle.BundleGUID, recordFileElement);
  394. }
  395. public bool DeleteCacheBundleFile(string bundleGUID)
  396. {
  397. if (_records.TryGetValue(bundleGUID, out RecordFileElement element))
  398. {
  399. _records.Remove(bundleGUID);
  400. return element.DeleteFolder();
  401. }
  402. else
  403. {
  404. return false;
  405. }
  406. }
  407. private readonly BufferWriter _sharedBuffer = new BufferWriter(1024);
  408. public void WriteBundleInfoFile(string filePath, string dataFileCRC, long dataFileSize)
  409. {
  410. using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
  411. {
  412. _sharedBuffer.Clear();
  413. _sharedBuffer.WriteUTF8(dataFileCRC);
  414. _sharedBuffer.WriteInt64(dataFileSize);
  415. _sharedBuffer.WriteToStream(fs);
  416. fs.Flush();
  417. }
  418. }
  419. public void ReadBundleInfoFile(string filePath, out string dataFileCRC, out long dataFileSize)
  420. {
  421. byte[] binaryData = FileUtility.ReadAllBytes(filePath);
  422. BufferReader buffer = new BufferReader(binaryData);
  423. dataFileCRC = buffer.ReadUTF8();
  424. dataFileSize = buffer.ReadInt64();
  425. }
  426. #endregion
  427. #region 内部方法
  428. public string GetDefaultCachePackageRoot(string packageName)
  429. {
  430. string rootDirectory = YooAssetSettingsData.GetYooDefaultCacheRoot();
  431. return PathUtility.Combine(rootDirectory, packageName);
  432. }
  433. public string GetCacheBundleFileLoadPath(PackageBundle bundle)
  434. {
  435. return GetBundleDataFilePath(bundle);
  436. }
  437. public string GetCachePackageHashFilePath(string packageVersion)
  438. {
  439. string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
  440. return PathUtility.Combine(_cacheManifestFilesRoot, fileName);
  441. }
  442. public string GetCachePackageManifestFilePath(string packageVersion)
  443. {
  444. string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
  445. return PathUtility.Combine(_cacheManifestFilesRoot, fileName);
  446. }
  447. public string GetSandboxAppFootPrintFilePath()
  448. {
  449. return PathUtility.Combine(_cacheManifestFilesRoot, DefaultCacheFileSystemDefine.AppFootPrintFileName);
  450. }
  451. public string GetCacheBundleFilesRoot()
  452. {
  453. return _cacheBundleFilesRoot;
  454. }
  455. public string GetCacheManifestFilesRoot()
  456. {
  457. return _cacheManifestFilesRoot;
  458. }
  459. /// <summary>
  460. /// 删除所有清单文件
  461. /// </summary>
  462. public void DeleteAllManifestFiles()
  463. {
  464. if (Directory.Exists(_cacheManifestFilesRoot))
  465. {
  466. Directory.Delete(_cacheManifestFilesRoot, true);
  467. }
  468. }
  469. /// <summary>
  470. /// 加载加密资源文件
  471. /// </summary>
  472. public DecryptResult LoadEncryptedAssetBundle(PackageBundle bundle)
  473. {
  474. string filePath = GetCacheBundleFileLoadPath(bundle);
  475. var fileInfo = new DecryptFileInfo()
  476. {
  477. BundleName = bundle.BundleName,
  478. FileLoadCRC = bundle.UnityCRC,
  479. FileLoadPath = filePath,
  480. };
  481. return DecryptionServices.LoadAssetBundle(fileInfo);
  482. }
  483. /// <summary>
  484. /// 加载加密资源文件
  485. /// </summary>
  486. public DecryptResult LoadEncryptedAssetBundleAsync(PackageBundle bundle)
  487. {
  488. string filePath = GetCacheBundleFileLoadPath(bundle);
  489. var fileInfo = new DecryptFileInfo()
  490. {
  491. BundleName = bundle.BundleName,
  492. FileLoadCRC = bundle.UnityCRC,
  493. FileLoadPath = filePath,
  494. };
  495. return DecryptionServices.LoadAssetBundleAsync(fileInfo);
  496. }
  497. #endregion
  498. }
  499. }