CacheHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. namespace YooAsset
  7. {
  8. internal static class CacheHelper
  9. {
  10. /// <summary>
  11. /// 禁用Unity缓存系统在WebGL平台
  12. /// </summary>
  13. public static bool DisableUnityCacheOnWebGL = false;
  14. /// <summary>
  15. /// 验证缓存文件(子线程内操作)
  16. /// </summary>
  17. public static EVerifyResult VerifyingCacheFile(VerifyCacheFileElement element, EVerifyLevel verifyLevel)
  18. {
  19. try
  20. {
  21. if (verifyLevel == EVerifyLevel.Low)
  22. {
  23. if (File.Exists(element.InfoFilePath) == false)
  24. return EVerifyResult.InfoFileNotExisted;
  25. if (File.Exists(element.DataFilePath) == false)
  26. return EVerifyResult.DataFileNotExisted;
  27. return EVerifyResult.Succeed;
  28. }
  29. else
  30. {
  31. if (File.Exists(element.InfoFilePath) == false)
  32. return EVerifyResult.InfoFileNotExisted;
  33. // 解析信息文件获取验证数据
  34. CacheFileInfo.ReadInfoFromFile(element.InfoFilePath, out element.DataFileCRC, out element.DataFileSize);
  35. }
  36. }
  37. catch (Exception)
  38. {
  39. return EVerifyResult.Exception;
  40. }
  41. return VerifyingInternal(element.DataFilePath, element.DataFileSize, element.DataFileCRC, verifyLevel);
  42. }
  43. /// <summary>
  44. /// 验证下载文件(子线程内操作)
  45. /// </summary>
  46. public static EVerifyResult VerifyingTempFile(VerifyTempFileElement element)
  47. {
  48. return VerifyingInternal(element.TempDataFilePath, element.FileSize, element.FileCRC, EVerifyLevel.High);
  49. }
  50. /// <summary>
  51. /// 验证记录文件(主线程内操作)
  52. /// </summary>
  53. public static EVerifyResult VerifyingRecordFile(CacheManager cache, string cacheGUID)
  54. {
  55. var wrapper = cache.TryGetWrapper(cacheGUID);
  56. if (wrapper == null)
  57. return EVerifyResult.CacheNotFound;
  58. EVerifyResult result = VerifyingInternal(wrapper.DataFilePath, wrapper.DataFileSize, wrapper.DataFileCRC, EVerifyLevel.High);
  59. return result;
  60. }
  61. private static EVerifyResult VerifyingInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
  62. {
  63. try
  64. {
  65. if (File.Exists(filePath) == false)
  66. return EVerifyResult.DataFileNotExisted;
  67. // 先验证文件大小
  68. long size = FileUtility.GetFileSize(filePath);
  69. if (size < fileSize)
  70. return EVerifyResult.FileNotComplete;
  71. else if (size > fileSize)
  72. return EVerifyResult.FileOverflow;
  73. // 再验证文件CRC
  74. if (verifyLevel == EVerifyLevel.High)
  75. {
  76. string crc = HashUtility.FileCRC32(filePath);
  77. if (crc == fileCRC)
  78. return EVerifyResult.Succeed;
  79. else
  80. return EVerifyResult.FileCrcError;
  81. }
  82. else
  83. {
  84. return EVerifyResult.Succeed;
  85. }
  86. }
  87. catch (Exception)
  88. {
  89. return EVerifyResult.Exception;
  90. }
  91. }
  92. }
  93. }