CacheSystem.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace YooAsset
  7. {
  8. internal static class CacheSystem
  9. {
  10. private readonly static Dictionary<string, PatchBundle> _cachedDic = new Dictionary<string, PatchBundle>(1000);
  11. /// <summary>
  12. /// 初始化时的验证级别
  13. /// </summary>
  14. public static EVerifyLevel InitVerifyLevel { private set; get; }
  15. public static void Initialize(EVerifyLevel initVerifyLevel)
  16. {
  17. InitVerifyLevel = initVerifyLevel;
  18. }
  19. public static void DestroyAll()
  20. {
  21. _cachedDic.Clear();
  22. }
  23. /// <summary>
  24. /// 查询是否为验证文件
  25. /// 注意:被收录的文件完整性是绝对有效的
  26. /// </summary>
  27. public static bool IsCached(PatchBundle patchBundle)
  28. {
  29. string fileHash = patchBundle.FileHash;
  30. if (_cachedDic.ContainsKey(fileHash))
  31. {
  32. string filePath = patchBundle.CachedFilePath;
  33. if (File.Exists(filePath))
  34. {
  35. return true;
  36. }
  37. else
  38. {
  39. _cachedDic.Remove(fileHash);
  40. YooLogger.Error($"Cache file is missing : {filePath}");
  41. return false;
  42. }
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. }
  49. /// <summary>
  50. /// 缓存补丁包文件
  51. /// </summary>
  52. public static void CacheBundle(PatchBundle patchBundle)
  53. {
  54. string fileHash = patchBundle.FileHash;
  55. if (_cachedDic.ContainsKey(fileHash) == false)
  56. {
  57. string filePath = patchBundle.CachedFilePath;
  58. YooLogger.Log($"Cache verify file : {filePath}");
  59. _cachedDic.Add(fileHash, patchBundle);
  60. }
  61. }
  62. /// <summary>
  63. /// 验证补丁包文件
  64. /// </summary>
  65. public static EVerifyResult VerifyBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
  66. {
  67. return VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
  68. }
  69. /// <summary>
  70. /// 验证并缓存补丁包文件
  71. /// </summary>
  72. public static EVerifyResult VerifyAndCacheBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
  73. {
  74. var verifyResult = VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
  75. if (verifyResult == EVerifyResult.Succeed)
  76. CacheBundle(patchBundle);
  77. return verifyResult;
  78. }
  79. /// <summary>
  80. /// 验证文件完整性
  81. /// </summary>
  82. public static EVerifyResult VerifyContentInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
  83. {
  84. try
  85. {
  86. if (File.Exists(filePath) == false)
  87. return EVerifyResult.FileNotExisted;
  88. // 先验证文件大小
  89. long size = FileUtility.GetFileSize(filePath);
  90. if (size < fileSize)
  91. return EVerifyResult.FileNotComplete;
  92. else if (size > fileSize)
  93. return EVerifyResult.FileOverflow;
  94. // 再验证文件CRC
  95. if (verifyLevel == EVerifyLevel.High)
  96. {
  97. string crc = HashUtility.FileCRC32(filePath);
  98. if (crc == fileCRC)
  99. return EVerifyResult.Succeed;
  100. else
  101. return EVerifyResult.FileCrcError;
  102. }
  103. else
  104. {
  105. return EVerifyResult.Succeed;
  106. }
  107. }
  108. catch (Exception)
  109. {
  110. return EVerifyResult.Exception;
  111. }
  112. }
  113. }
  114. }