HostPlayModeImpl.cs 10 KB

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