WebPlayModeImpl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. internal class WebPlayModeImpl : IPlayMode, IBundleQuery
  7. {
  8. private PackageManifest _activeManifest;
  9. private ResourceAssist _assist;
  10. private IBuildinQueryServices _buildinQueryServices;
  11. private IRemoteServices _remoteServices;
  12. public readonly string PackageName;
  13. public DownloadManager Download
  14. {
  15. get { return _assist.Download; }
  16. }
  17. public PersistentManager Persistent
  18. {
  19. get { return _assist.Persistent; }
  20. }
  21. public IRemoteServices RemoteServices
  22. {
  23. get { return _remoteServices; }
  24. }
  25. public WebPlayModeImpl(string packageName)
  26. {
  27. PackageName = packageName;
  28. }
  29. /// <summary>
  30. /// 异步初始化
  31. /// </summary>
  32. public InitializationOperation InitializeAsync(ResourceAssist assist, IBuildinQueryServices buildinQueryServices, IRemoteServices remoteServices)
  33. {
  34. _assist = assist;
  35. _buildinQueryServices = buildinQueryServices;
  36. _remoteServices = remoteServices;
  37. var operation = new WebPlayModeInitializationOperation(this);
  38. OperationSystem.StartOperation(PackageName, operation);
  39. return operation;
  40. }
  41. // 下载相关
  42. private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle)
  43. {
  44. string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName);
  45. string remoteFallbackURL = _remoteServices.GetRemoteFallbackURL(packageBundle.FileName);
  46. BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL);
  47. return bundleInfo;
  48. }
  49. private List<BundleInfo> ConvertToDownloadList(List<PackageBundle> downloadList)
  50. {
  51. List<BundleInfo> result = new List<BundleInfo>(downloadList.Count);
  52. foreach (var packageBundle in downloadList)
  53. {
  54. var bundleInfo = ConvertToDownloadInfo(packageBundle);
  55. result.Add(bundleInfo);
  56. }
  57. return result;
  58. }
  59. // 查询相关
  60. #if UNITY_WECHAT_GAME
  61. private WeChatWASM.WXFileSystemManager _wxFileSystemMgr;
  62. private bool IsCachedPackageBundle(PackageBundle packageBundle)
  63. {
  64. if (_wxFileSystemMgr == null)
  65. _wxFileSystemMgr = WeChatWASM.WX.GetFileSystemManager();
  66. string filePath = WeChatWASM.WX.env.USER_DATA_PATH + packageBundle.FileName;
  67. string result = _wxFileSystemMgr.AccessSync(filePath);
  68. return result.Equals("access:ok");
  69. }
  70. #else
  71. private bool IsCachedPackageBundle(PackageBundle packageBundle)
  72. {
  73. return false;
  74. }
  75. #endif
  76. private bool IsBuildinPackageBundle(PackageBundle packageBundle)
  77. {
  78. return _buildinQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC);
  79. }
  80. #region IPlayMode接口
  81. public PackageManifest ActiveManifest
  82. {
  83. set
  84. {
  85. _activeManifest = value;
  86. }
  87. get
  88. {
  89. return _activeManifest;
  90. }
  91. }
  92. public void FlushManifestVersionFile()
  93. {
  94. }
  95. UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout)
  96. {
  97. var operation = new WebPlayModeUpdatePackageVersionOperation(this, appendTimeTicks, timeout);
  98. OperationSystem.StartOperation(PackageName, operation);
  99. return operation;
  100. }
  101. UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
  102. {
  103. var operation = new WebPlayModeUpdatePackageManifestOperation(this, packageVersion, timeout);
  104. OperationSystem.StartOperation(PackageName, operation);
  105. return operation;
  106. }
  107. PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout)
  108. {
  109. var operation = new WebPlayModePreDownloadContentOperation(this);
  110. OperationSystem.StartOperation(PackageName, operation);
  111. return operation;
  112. }
  113. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
  114. {
  115. List<BundleInfo> downloadList = GetDownloadListByAll(_activeManifest);
  116. var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  117. return operation;
  118. }
  119. public List<BundleInfo> GetDownloadListByAll(PackageManifest manifest)
  120. {
  121. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  122. foreach (var packageBundle in manifest.BundleList)
  123. {
  124. // 忽略缓存文件
  125. if (IsCachedPackageBundle(packageBundle))
  126. continue;
  127. // 忽略APP资源
  128. if (IsBuildinPackageBundle(packageBundle))
  129. continue;
  130. downloadList.Add(packageBundle);
  131. }
  132. return ConvertToDownloadList(downloadList);
  133. }
  134. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
  135. {
  136. List<BundleInfo> downloadList = GetDownloadListByTags(_activeManifest, tags);
  137. var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  138. return operation;
  139. }
  140. public List<BundleInfo> GetDownloadListByTags(PackageManifest manifest, string[] tags)
  141. {
  142. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  143. foreach (var packageBundle in manifest.BundleList)
  144. {
  145. // 忽略缓存文件
  146. if (IsCachedPackageBundle(packageBundle))
  147. continue;
  148. // 忽略APP资源
  149. if (IsBuildinPackageBundle(packageBundle))
  150. continue;
  151. // 如果未带任何标记,则统一下载
  152. if (packageBundle.HasAnyTags() == false)
  153. {
  154. downloadList.Add(packageBundle);
  155. }
  156. else
  157. {
  158. // 查询DLC资源
  159. if (packageBundle.HasTag(tags))
  160. {
  161. downloadList.Add(packageBundle);
  162. }
  163. }
  164. }
  165. return ConvertToDownloadList(downloadList);
  166. }
  167. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout)
  168. {
  169. List<BundleInfo> downloadList = GetDownloadListByPaths(_activeManifest, assetInfos);
  170. var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  171. return operation;
  172. }
  173. public List<BundleInfo> GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos)
  174. {
  175. // 获取资源对象的资源包和所有依赖资源包
  176. List<PackageBundle> checkList = new List<PackageBundle>();
  177. foreach (var assetInfo in assetInfos)
  178. {
  179. if (assetInfo.IsInvalid)
  180. {
  181. YooLogger.Warning(assetInfo.Error);
  182. continue;
  183. }
  184. // 注意:如果清单里未找到资源包会抛出异常!
  185. PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath);
  186. if (checkList.Contains(mainBundle) == false)
  187. checkList.Add(mainBundle);
  188. // 注意:如果清单里未找到资源包会抛出异常!
  189. PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath);
  190. foreach (var dependBundle in dependBundles)
  191. {
  192. if (checkList.Contains(dependBundle) == false)
  193. checkList.Add(dependBundle);
  194. }
  195. }
  196. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  197. foreach (var packageBundle in checkList)
  198. {
  199. // 忽略缓存文件
  200. if (IsCachedPackageBundle(packageBundle))
  201. continue;
  202. // 忽略APP资源
  203. if (IsBuildinPackageBundle(packageBundle))
  204. continue;
  205. downloadList.Add(packageBundle);
  206. }
  207. return ConvertToDownloadList(downloadList);
  208. }
  209. ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
  210. {
  211. return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout);
  212. }
  213. ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
  214. {
  215. return ResourceUnpackerOperation.CreateEmptyUnpacker(Download, PackageName, upackingMaxNumber, failedTryAgain, timeout);
  216. }
  217. ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
  218. {
  219. return ResourceImporterOperation.CreateEmptyImporter(Download, PackageName, importerMaxNumber, failedTryAgain, timeout);
  220. }
  221. #endregion
  222. #region IBundleQuery接口
  223. private BundleInfo CreateBundleInfo(PackageBundle packageBundle)
  224. {
  225. if (packageBundle == null)
  226. throw new Exception("Should never get here !");
  227. // 查询APP资源
  228. if (IsBuildinPackageBundle(packageBundle))
  229. {
  230. BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming);
  231. return bundleInfo;
  232. }
  233. // 从服务端下载
  234. return ConvertToDownloadInfo(packageBundle);
  235. }
  236. BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo)
  237. {
  238. if (assetInfo.IsInvalid)
  239. throw new Exception("Should never get here !");
  240. // 注意:如果清单里未找到资源包会抛出异常!
  241. var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath);
  242. return CreateBundleInfo(packageBundle);
  243. }
  244. BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo)
  245. {
  246. if (assetInfo.IsInvalid)
  247. throw new Exception("Should never get here !");
  248. // 注意:如果清单里未找到资源包会抛出异常!
  249. var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath);
  250. List<BundleInfo> result = new List<BundleInfo>(depends.Length);
  251. foreach (var packageBundle in depends)
  252. {
  253. BundleInfo bundleInfo = CreateBundleInfo(packageBundle);
  254. result.Add(bundleInfo);
  255. }
  256. return result.ToArray();
  257. }
  258. string IBundleQuery.GetMainBundleName(AssetInfo assetInfo)
  259. {
  260. if (assetInfo.IsInvalid)
  261. throw new Exception("Should never get here !");
  262. // 注意:如果清单里未找到资源包会抛出异常!
  263. var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath);
  264. return packageBundle.BundleName;
  265. }
  266. string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo)
  267. {
  268. if (assetInfo.IsInvalid)
  269. throw new Exception("Should never get here !");
  270. // 注意:如果清单里未找到资源包会抛出异常!
  271. var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath);
  272. List<string> result = new List<string>(depends.Length);
  273. foreach (var packageBundle in depends)
  274. {
  275. result.Add(packageBundle.BundleName);
  276. }
  277. return result.ToArray();
  278. }
  279. bool IBundleQuery.ManifestValid()
  280. {
  281. return _activeManifest != null;
  282. }
  283. #endregion
  284. }
  285. }