PersistentManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace YooAsset
  6. {
  7. internal class PersistentManager
  8. {
  9. private readonly Dictionary<string, string> _cachedDataFilePaths = new Dictionary<string, string>(10000);
  10. private readonly Dictionary<string, string> _cachedInfoFilePaths = new Dictionary<string, string>(10000);
  11. private readonly Dictionary<string, string> _tempDataFilePaths = new Dictionary<string, string>(10000);
  12. private readonly Dictionary<string, string> _buildinFilePaths = new Dictionary<string, string>(10000);
  13. /// <summary>
  14. /// 所属包裹
  15. /// </summary>
  16. public readonly string PackageName;
  17. public string BuildinRoot { private set; get; }
  18. public string BuildinPackageRoot { private set; get; }
  19. public string SandboxRoot { private set; get; }
  20. public string SandboxPackageRoot { private set; get; }
  21. public string SandboxCacheFilesRoot { private set; get; }
  22. public string SandboxManifestFilesRoot { private set; get; }
  23. public string SandboxAppFootPrintFilePath { private set; get; }
  24. public bool AppendFileExtension { private set; get; }
  25. public PersistentManager(string packageName)
  26. {
  27. PackageName = packageName;
  28. }
  29. /// <summary>
  30. /// 初始化
  31. /// </summary>
  32. public void Initialize(string buildinRoot, string sandboxRoot, bool appendFileExtension)
  33. {
  34. if (string.IsNullOrEmpty(buildinRoot))
  35. BuildinRoot = CreateDefaultBuildinRoot();
  36. else
  37. BuildinRoot = buildinRoot;
  38. if (string.IsNullOrEmpty(sandboxRoot))
  39. SandboxRoot = CreateDefaultSandboxRoot();
  40. else
  41. SandboxRoot = sandboxRoot;
  42. BuildinPackageRoot = PathUtility.Combine(BuildinRoot, PackageName);
  43. SandboxPackageRoot = PathUtility.Combine(SandboxRoot, PackageName);
  44. SandboxCacheFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.CacheFilesFolderName);
  45. SandboxManifestFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.ManifestFolderName);
  46. SandboxAppFootPrintFilePath = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.AppFootPrintFileName);
  47. AppendFileExtension = appendFileExtension;
  48. }
  49. private static string CreateDefaultBuildinRoot()
  50. {
  51. return PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
  52. }
  53. private static string CreateDefaultSandboxRoot()
  54. {
  55. #if UNITY_EDITOR
  56. // 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
  57. string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
  58. projectPath = PathUtility.RegularPath(projectPath);
  59. return PathUtility.Combine(projectPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
  60. #elif UNITY_STANDALONE
  61. return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
  62. #else
  63. return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettingsData.Setting.DefaultYooFolderName);
  64. #endif
  65. }
  66. public string GetCachedDataFilePath(PackageBundle bundle)
  67. {
  68. if (_cachedDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
  69. {
  70. string folderName = bundle.FileHash.Substring(0, 2);
  71. filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleDataFileName);
  72. if (AppendFileExtension)
  73. filePath += bundle.FileExtension;
  74. _cachedDataFilePaths.Add(bundle.CacheGUID, filePath);
  75. }
  76. return filePath;
  77. }
  78. public string GetCachedInfoFilePath(PackageBundle bundle)
  79. {
  80. if (_cachedInfoFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
  81. {
  82. string folderName = bundle.FileHash.Substring(0, 2);
  83. filePath = PathUtility.Combine(SandboxCacheFilesRoot, folderName, bundle.CacheGUID, YooAssetSettings.CacheBundleInfoFileName);
  84. _cachedInfoFilePaths.Add(bundle.CacheGUID, filePath);
  85. }
  86. return filePath;
  87. }
  88. public string GetTempDataFilePath(PackageBundle bundle)
  89. {
  90. if (_tempDataFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
  91. {
  92. string cachedDataFilePath = GetCachedDataFilePath(bundle);
  93. filePath = $"{cachedDataFilePath}.temp";
  94. _tempDataFilePaths.Add(bundle.CacheGUID, filePath);
  95. }
  96. return filePath;
  97. }
  98. public string GetBuildinFilePath(PackageBundle bundle)
  99. {
  100. if (_buildinFilePaths.TryGetValue(bundle.CacheGUID, out string filePath) == false)
  101. {
  102. filePath = PathUtility.Combine(BuildinPackageRoot, bundle.FileName);
  103. _buildinFilePaths.Add(bundle.CacheGUID, filePath);
  104. }
  105. return filePath;
  106. }
  107. /// <summary>
  108. /// 删除沙盒里的包裹目录
  109. /// </summary>
  110. public void DeleteSandboxPackageFolder()
  111. {
  112. if (Directory.Exists(SandboxPackageRoot))
  113. Directory.Delete(SandboxPackageRoot, true);
  114. }
  115. /// <summary>
  116. /// 删除沙盒内的缓存文件夹
  117. /// </summary>
  118. public void DeleteSandboxCacheFilesFolder()
  119. {
  120. if (Directory.Exists(SandboxCacheFilesRoot))
  121. Directory.Delete(SandboxCacheFilesRoot, true);
  122. }
  123. /// <summary>
  124. /// 删除沙盒内的清单文件夹
  125. /// </summary>
  126. public void DeleteSandboxManifestFilesFolder()
  127. {
  128. if (Directory.Exists(SandboxManifestFilesRoot))
  129. Directory.Delete(SandboxManifestFilesRoot, true);
  130. }
  131. /// <summary>
  132. /// 获取沙盒内包裹的清单文件的路径
  133. /// </summary>
  134. public string GetSandboxPackageManifestFilePath(string packageVersion)
  135. {
  136. string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
  137. return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
  138. }
  139. /// <summary>
  140. /// 获取沙盒内包裹的哈希文件的路径
  141. /// </summary>
  142. public string GetSandboxPackageHashFilePath(string packageVersion)
  143. {
  144. string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
  145. return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
  146. }
  147. /// <summary>
  148. /// 获取沙盒内包裹的版本文件的路径
  149. /// </summary>
  150. public string GetSandboxPackageVersionFilePath()
  151. {
  152. string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
  153. return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
  154. }
  155. /// <summary>
  156. /// 保存沙盒内默认的包裹版本
  157. /// </summary>
  158. public void SaveSandboxPackageVersionFile(string version)
  159. {
  160. string filePath = GetSandboxPackageVersionFilePath();
  161. FileUtility.WriteAllText(filePath, version);
  162. }
  163. /// <summary>
  164. /// 获取APP内包裹的清单文件的路径
  165. /// </summary>
  166. public string GetBuildinPackageManifestFilePath(string packageVersion)
  167. {
  168. string fileName = YooAssetSettingsData.GetManifestBinaryFileName(PackageName, packageVersion);
  169. return PathUtility.Combine(BuildinPackageRoot, fileName);
  170. }
  171. /// <summary>
  172. /// 获取APP内包裹的哈希文件的路径
  173. /// </summary>
  174. public string GetBuildinPackageHashFilePath(string packageVersion)
  175. {
  176. string fileName = YooAssetSettingsData.GetPackageHashFileName(PackageName, packageVersion);
  177. return PathUtility.Combine(BuildinPackageRoot, fileName);
  178. }
  179. /// <summary>
  180. /// 获取APP内包裹的版本文件的路径
  181. /// </summary>
  182. public string GetBuildinPackageVersionFilePath()
  183. {
  184. string fileName = YooAssetSettingsData.GetPackageVersionFileName(PackageName);
  185. return PathUtility.Combine(BuildinPackageRoot, fileName);
  186. }
  187. }
  188. }