Persistent.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System.IO;
  2. namespace YooAsset
  3. {
  4. internal class Persistent
  5. {
  6. private readonly string _packageName;
  7. public string BuildinRoot { private set; get; }
  8. public string BuildinPackageRoot { private set; get; }
  9. public string SandboxRoot { private set; get; }
  10. public string SandboxPackageRoot { private set; get; }
  11. public string SandboxCacheBundleFilesRoot { private set; get; }
  12. public string SandboxCacheRawFilesRoot { private set; get; }
  13. public string SandboxManifestFilesRoot { private set; get; }
  14. public string SandboxAppFootPrintFilePath { private set; get; }
  15. public Persistent(string packageName)
  16. {
  17. _packageName = packageName;
  18. }
  19. /// <summary>
  20. /// 重写根路径
  21. /// </summary>
  22. public void OverwriteRootDirectory(string buildinRoot, string sandboxRoot)
  23. {
  24. if (string.IsNullOrEmpty(buildinRoot))
  25. BuildinRoot = CreateDefaultBuildinRoot();
  26. else
  27. BuildinRoot = buildinRoot;
  28. if (string.IsNullOrEmpty(sandboxRoot))
  29. SandboxRoot = CreateDefaultSandboxRoot();
  30. else
  31. SandboxRoot = sandboxRoot;
  32. BuildinPackageRoot = PathUtility.Combine(BuildinRoot, _packageName);
  33. SandboxPackageRoot = PathUtility.Combine(SandboxRoot, _packageName);
  34. SandboxCacheBundleFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.CachedBundleFileFolder);
  35. SandboxCacheRawFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.CachedRawFileFolder);
  36. SandboxManifestFilesRoot = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.ManifestFolderName);
  37. SandboxAppFootPrintFilePath = PathUtility.Combine(SandboxPackageRoot, YooAssetSettings.AppFootPrintFileName);
  38. }
  39. private static string CreateDefaultBuildinRoot()
  40. {
  41. return PathUtility.Combine(UnityEngine.Application.streamingAssetsPath, YooAssetSettings.DefaultYooFolderName);
  42. }
  43. private static string CreateDefaultSandboxRoot()
  44. {
  45. #if UNITY_EDITOR
  46. // 注意:为了方便调试查看,编辑器下把存储目录放到项目里。
  47. string projectPath = Path.GetDirectoryName(UnityEngine.Application.dataPath);
  48. projectPath = PathUtility.RegularPath(projectPath);
  49. return PathUtility.Combine(projectPath, YooAssetSettings.DefaultYooFolderName);
  50. #elif UNITY_STANDALONE
  51. return PathUtility.Combine(UnityEngine.Application.dataPath, YooAssetSettings.DefaultYooFolderName);
  52. #else
  53. return PathUtility.Combine(UnityEngine.Application.persistentDataPath, YooAssetSettings.DefaultYooFolderName);
  54. #endif
  55. }
  56. /// <summary>
  57. /// 删除沙盒里的包裹目录
  58. /// </summary>
  59. public void DeleteSandboxPackageFolder()
  60. {
  61. if (Directory.Exists(SandboxPackageRoot))
  62. Directory.Delete(SandboxPackageRoot, true);
  63. }
  64. /// <summary>
  65. /// 删除沙盒内的缓存文件夹
  66. /// </summary>
  67. public void DeleteSandboxCacheFilesFolder()
  68. {
  69. // CacheBundleFiles
  70. if (Directory.Exists(SandboxCacheBundleFilesRoot))
  71. Directory.Delete(SandboxCacheBundleFilesRoot, true);
  72. // CacheRawFiles
  73. if (Directory.Exists(SandboxCacheRawFilesRoot))
  74. Directory.Delete(SandboxCacheRawFilesRoot, true);
  75. }
  76. /// <summary>
  77. /// 删除沙盒内的清单文件夹
  78. /// </summary>
  79. public void DeleteSandboxManifestFilesFolder()
  80. {
  81. if (Directory.Exists(SandboxManifestFilesRoot))
  82. Directory.Delete(SandboxManifestFilesRoot, true);
  83. }
  84. /// <summary>
  85. /// 获取沙盒内包裹的清单文件的路径
  86. /// </summary>
  87. public string GetSandboxPackageManifestFilePath(string packageVersion)
  88. {
  89. string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, packageVersion);
  90. return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
  91. }
  92. /// <summary>
  93. /// 获取沙盒内包裹的哈希文件的路径
  94. /// </summary>
  95. public string GetSandboxPackageHashFilePath(string packageVersion)
  96. {
  97. string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, packageVersion);
  98. return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
  99. }
  100. /// <summary>
  101. /// 获取沙盒内包裹的版本文件的路径
  102. /// </summary>
  103. public string GetSandboxPackageVersionFilePath()
  104. {
  105. string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName);
  106. return PathUtility.Combine(SandboxManifestFilesRoot, fileName);
  107. }
  108. /// <summary>
  109. /// 保存沙盒内默认的包裹版本
  110. /// </summary>
  111. public void SaveSandboxPackageVersionFile(string version)
  112. {
  113. string filePath = GetSandboxPackageVersionFilePath();
  114. FileUtility.WriteAllText(filePath, version);
  115. }
  116. /// <summary>
  117. /// 获取APP内包裹的清单文件的路径
  118. /// </summary>
  119. public string GetBuildinPackageManifestFilePath(string packageVersion)
  120. {
  121. string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, packageVersion);
  122. return PathUtility.Combine(BuildinPackageRoot, fileName);
  123. }
  124. /// <summary>
  125. /// 获取APP内包裹的哈希文件的路径
  126. /// </summary>
  127. public string GetBuildinPackageHashFilePath(string packageVersion)
  128. {
  129. string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, packageVersion);
  130. return PathUtility.Combine(BuildinPackageRoot, fileName);
  131. }
  132. /// <summary>
  133. /// 获取APP内包裹的版本文件的路径
  134. /// </summary>
  135. public string GetBuildinPackageVersionFilePath()
  136. {
  137. string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName);
  138. return PathUtility.Combine(BuildinPackageRoot, fileName);
  139. }
  140. }
  141. }