HostPlayModeImpl.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. internal class HostPlayModeImpl : IPlayMode, IBundleQuery
  7. {
  8. private PackageManifest _activeManifest;
  9. private ResourceAssist _assist;
  10. private IBuildinQueryServices _buildinQueryServices;
  11. private IDeliveryQueryServices _deliveryQueryServices;
  12. private IRemoteServices _remoteServices;
  13. public readonly string PackageName;
  14. public DownloadManager Download
  15. {
  16. get { return _assist.Download; }
  17. }
  18. public PersistentManager Persistent
  19. {
  20. get { return _assist.Persistent; }
  21. }
  22. public CacheManager Cache
  23. {
  24. get { return _assist.Cache; }
  25. }
  26. public IRemoteServices RemoteServices
  27. {
  28. get { return _remoteServices; }
  29. }
  30. public HostPlayModeImpl(string packageName)
  31. {
  32. PackageName = packageName;
  33. }
  34. /// <summary>
  35. /// 异步初始化
  36. /// </summary>
  37. public InitializationOperation InitializeAsync(ResourceAssist assist, IBuildinQueryServices buildinQueryServices, IDeliveryQueryServices deliveryQueryServices, IRemoteServices remoteServices)
  38. {
  39. _assist = assist;
  40. _buildinQueryServices = buildinQueryServices;
  41. _deliveryQueryServices = deliveryQueryServices;
  42. _remoteServices = remoteServices;
  43. var operation = new HostPlayModeInitializationOperation(this);
  44. OperationSystem.StartOperation(PackageName, operation);
  45. return operation;
  46. }
  47. // 下载相关
  48. private List<BundleInfo> ConvertToDownloadList(List<PackageBundle> downloadList)
  49. {
  50. List<BundleInfo> result = new List<BundleInfo>(downloadList.Count);
  51. foreach (var packageBundle in downloadList)
  52. {
  53. var bundleInfo = ConvertToDownloadInfo(packageBundle);
  54. result.Add(bundleInfo);
  55. }
  56. return result;
  57. }
  58. private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle)
  59. {
  60. string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName);
  61. string remoteFallbackURL = _remoteServices.GetRemoteFallbackURL(packageBundle.FileName);
  62. BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL);
  63. return bundleInfo;
  64. }
  65. // 查询相关
  66. private bool IsDeliveryPackageBundle(PackageBundle packageBundle)
  67. {
  68. if (_deliveryQueryServices == null)
  69. return false;
  70. return _deliveryQueryServices.Query(PackageName, packageBundle.FileName, packageBundle.FileCRC);
  71. }
  72. private bool IsCachedPackageBundle(PackageBundle packageBundle)
  73. {
  74. return _assist.Cache.IsCached(packageBundle.CacheGUID);
  75. }
  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. if (_activeManifest != null)
  95. {
  96. _assist.Persistent.SaveSandboxPackageVersionFile(_activeManifest.PackageVersion);
  97. }
  98. }
  99. UpdatePackageVersionOperation IPlayMode.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout)
  100. {
  101. var operation = new HostPlayModeUpdatePackageVersionOperation(this, appendTimeTicks, timeout);
  102. OperationSystem.StartOperation(PackageName, operation);
  103. return operation;
  104. }
  105. UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
  106. {
  107. var operation = new HostPlayModeUpdatePackageManifestOperation(this, packageVersion, autoSaveVersion, timeout);
  108. OperationSystem.StartOperation(PackageName, operation);
  109. return operation;
  110. }
  111. PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout)
  112. {
  113. var operation = new HostPlayModePreDownloadContentOperation(this, packageVersion, timeout);
  114. OperationSystem.StartOperation(PackageName, operation);
  115. return operation;
  116. }
  117. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
  118. {
  119. List<BundleInfo> downloadList = GetDownloadListByAll(_activeManifest);
  120. var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  121. return operation;
  122. }
  123. public List<BundleInfo> GetDownloadListByAll(PackageManifest manifest)
  124. {
  125. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  126. foreach (var packageBundle in manifest.BundleList)
  127. {
  128. // 忽略分发文件
  129. if (IsDeliveryPackageBundle(packageBundle))
  130. continue;
  131. // 忽略缓存文件
  132. if (IsCachedPackageBundle(packageBundle))
  133. continue;
  134. // 忽略APP资源
  135. if (IsBuildinPackageBundle(packageBundle))
  136. continue;
  137. downloadList.Add(packageBundle);
  138. }
  139. return ConvertToDownloadList(downloadList);
  140. }
  141. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
  142. {
  143. List<BundleInfo> downloadList = GetDownloadListByTags(_activeManifest, tags);
  144. var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  145. return operation;
  146. }
  147. public List<BundleInfo> GetDownloadListByTags(PackageManifest manifest, string[] tags)
  148. {
  149. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  150. foreach (var packageBundle in manifest.BundleList)
  151. {
  152. // 忽略分发文件
  153. if (IsDeliveryPackageBundle(packageBundle))
  154. continue;
  155. // 忽略缓存文件
  156. if (IsCachedPackageBundle(packageBundle))
  157. continue;
  158. // 忽略APP资源
  159. if (IsBuildinPackageBundle(packageBundle))
  160. continue;
  161. // 如果未带任何标记,则统一下载
  162. if (packageBundle.HasAnyTags() == false)
  163. {
  164. downloadList.Add(packageBundle);
  165. }
  166. else
  167. {
  168. // 查询DLC资源
  169. if (packageBundle.HasTag(tags))
  170. {
  171. downloadList.Add(packageBundle);
  172. }
  173. }
  174. }
  175. return ConvertToDownloadList(downloadList);
  176. }
  177. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout)
  178. {
  179. List<BundleInfo> downloadList = GetDownloadListByPaths(_activeManifest, assetInfos);
  180. var operation = new ResourceDownloaderOperation(Download, PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  181. return operation;
  182. }
  183. public List<BundleInfo> GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos)
  184. {
  185. // 获取资源对象的资源包和所有依赖资源包
  186. List<PackageBundle> checkList = new List<PackageBundle>();
  187. foreach (var assetInfo in assetInfos)
  188. {
  189. if (assetInfo.IsInvalid)
  190. {
  191. YooLogger.Warning(assetInfo.Error);
  192. continue;
  193. }
  194. // 注意:如果清单里未找到资源包会抛出异常!
  195. PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath);
  196. if (checkList.Contains(mainBundle) == false)
  197. checkList.Add(mainBundle);
  198. // 注意:如果清单里未找到资源包会抛出异常!
  199. PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath);
  200. foreach (var dependBundle in dependBundles)
  201. {
  202. if (checkList.Contains(dependBundle) == false)
  203. checkList.Add(dependBundle);
  204. }
  205. }
  206. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  207. foreach (var packageBundle in checkList)
  208. {
  209. // 忽略分发文件
  210. if (IsDeliveryPackageBundle(packageBundle))
  211. continue;
  212. // 忽略缓存文件
  213. if (IsCachedPackageBundle(packageBundle))
  214. continue;
  215. // 忽略APP资源
  216. if (IsBuildinPackageBundle(packageBundle))
  217. continue;
  218. downloadList.Add(packageBundle);
  219. }
  220. return ConvertToDownloadList(downloadList);
  221. }
  222. ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
  223. {
  224. List<BundleInfo> unpcakList = GetUnpackListByAll(_activeManifest);
  225. var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout);
  226. return operation;
  227. }
  228. private List<BundleInfo> GetUnpackListByAll(PackageManifest manifest)
  229. {
  230. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  231. foreach (var packageBundle in manifest.BundleList)
  232. {
  233. // 忽略缓存文件
  234. if (IsCachedPackageBundle(packageBundle))
  235. continue;
  236. if (IsBuildinPackageBundle(packageBundle))
  237. {
  238. downloadList.Add(packageBundle);
  239. }
  240. }
  241. return BundleInfo.CreateUnpackInfos(_assist, downloadList);
  242. }
  243. ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
  244. {
  245. List<BundleInfo> unpcakList = GetUnpackListByTags(_activeManifest, tags);
  246. var operation = new ResourceUnpackerOperation(Download, PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout);
  247. return operation;
  248. }
  249. private List<BundleInfo> GetUnpackListByTags(PackageManifest manifest, string[] tags)
  250. {
  251. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  252. foreach (var packageBundle in manifest.BundleList)
  253. {
  254. // 忽略缓存文件
  255. if (IsCachedPackageBundle(packageBundle))
  256. continue;
  257. // 查询DLC资源
  258. if (IsBuildinPackageBundle(packageBundle))
  259. {
  260. if (packageBundle.HasTag(tags))
  261. {
  262. downloadList.Add(packageBundle);
  263. }
  264. }
  265. }
  266. return BundleInfo.CreateUnpackInfos(_assist, downloadList);
  267. }
  268. ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
  269. {
  270. List<BundleInfo> importerList = GetImporterListByFilePaths(_activeManifest, filePaths);
  271. var operation = new ResourceImporterOperation(Download, PackageName, importerList, importerMaxNumber, failedTryAgain, timeout);
  272. return operation;
  273. }
  274. private List<BundleInfo> GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths)
  275. {
  276. List<BundleInfo> result = new List<BundleInfo>();
  277. foreach (var filePath in filePaths)
  278. {
  279. string fileName = System.IO.Path.GetFileName(filePath);
  280. if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle))
  281. {
  282. // 忽略缓存文件
  283. if (IsCachedPackageBundle(packageBundle))
  284. continue;
  285. var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath);
  286. result.Add(bundleInfo);
  287. }
  288. else
  289. {
  290. YooLogger.Warning($"Not found package bundle, importer file path : {filePath}");
  291. }
  292. }
  293. return result;
  294. }
  295. #endregion
  296. #region IBundleQuery接口
  297. private BundleInfo CreateBundleInfo(PackageBundle packageBundle)
  298. {
  299. if (packageBundle == null)
  300. throw new Exception("Should never get here !");
  301. // 查询分发资源
  302. if (IsDeliveryPackageBundle(packageBundle))
  303. {
  304. string deliveryFilePath = _deliveryQueryServices.GetFilePath(PackageName, packageBundle.FileName);
  305. BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromDelivery, deliveryFilePath);
  306. return bundleInfo;
  307. }
  308. // 查询沙盒资源
  309. if (IsCachedPackageBundle(packageBundle))
  310. {
  311. BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromCache);
  312. return bundleInfo;
  313. }
  314. // 查询APP资源
  315. if (IsBuildinPackageBundle(packageBundle))
  316. {
  317. BundleInfo bundleInfo = new BundleInfo(_assist, packageBundle, BundleInfo.ELoadMode.LoadFromStreaming);
  318. return bundleInfo;
  319. }
  320. // 从服务端下载
  321. return ConvertToDownloadInfo(packageBundle);
  322. }
  323. BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo)
  324. {
  325. if (assetInfo.IsInvalid)
  326. throw new Exception("Should never get here !");
  327. // 注意:如果清单里未找到资源包会抛出异常!
  328. var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath);
  329. return CreateBundleInfo(packageBundle);
  330. }
  331. BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo)
  332. {
  333. if (assetInfo.IsInvalid)
  334. throw new Exception("Should never get here !");
  335. // 注意:如果清单里未找到资源包会抛出异常!
  336. var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath);
  337. List<BundleInfo> result = new List<BundleInfo>(depends.Length);
  338. foreach (var packageBundle in depends)
  339. {
  340. BundleInfo bundleInfo = CreateBundleInfo(packageBundle);
  341. result.Add(bundleInfo);
  342. }
  343. return result.ToArray();
  344. }
  345. string IBundleQuery.GetMainBundleName(AssetInfo assetInfo)
  346. {
  347. if (assetInfo.IsInvalid)
  348. throw new Exception("Should never get here !");
  349. // 注意:如果清单里未找到资源包会抛出异常!
  350. var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath);
  351. return packageBundle.BundleName;
  352. }
  353. string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo)
  354. {
  355. if (assetInfo.IsInvalid)
  356. throw new Exception("Should never get here !");
  357. // 注意:如果清单里未找到资源包会抛出异常!
  358. var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath);
  359. List<string> result = new List<string>(depends.Length);
  360. foreach (var packageBundle in depends)
  361. {
  362. result.Add(packageBundle.BundleName);
  363. }
  364. return result.ToArray();
  365. }
  366. bool IBundleQuery.ManifestValid()
  367. {
  368. return _activeManifest != null;
  369. }
  370. #endregion
  371. }
  372. }