PlayModeImpl.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. internal class PlayModeImpl : IPlayMode, IBundleQuery
  7. {
  8. public readonly string PackageName;
  9. public readonly EPlayMode PlayMode;
  10. public readonly List<IFileSystem> FileSystems = new List<IFileSystem>(10);
  11. public PlayModeImpl(string packageName, EPlayMode playMode)
  12. {
  13. PackageName = packageName;
  14. PlayMode = playMode;
  15. }
  16. /// <summary>
  17. /// 异步初始化
  18. /// </summary>
  19. public InitializationOperation InitializeAsync(FileSystemParameters fileSystemParameter)
  20. {
  21. var fileSystemParamList = new List<FileSystemParameters>();
  22. if (fileSystemParameter != null)
  23. fileSystemParamList.Add(fileSystemParameter);
  24. return InitializeAsync(fileSystemParamList);
  25. }
  26. /// <summary>
  27. /// 异步初始化
  28. /// </summary>
  29. public InitializationOperation InitializeAsync(FileSystemParameters fileSystemParameterA, FileSystemParameters fileSystemParameterB)
  30. {
  31. var fileSystemParamList = new List<FileSystemParameters>();
  32. if (fileSystemParameterA != null)
  33. fileSystemParamList.Add(fileSystemParameterA);
  34. if (fileSystemParameterB != null)
  35. fileSystemParamList.Add(fileSystemParameterB);
  36. return InitializeAsync(fileSystemParamList);
  37. }
  38. /// <summary>
  39. /// 异步初始化
  40. /// </summary>
  41. public InitializationOperation InitializeAsync(List<FileSystemParameters> fileSystemParameterList)
  42. {
  43. var operation = new InitializationOperation(this, fileSystemParameterList);
  44. return operation;
  45. }
  46. #region IPlayMode接口
  47. /// <summary>
  48. /// 当前激活的清单
  49. /// </summary>
  50. public PackageManifest ActiveManifest { set; get; }
  51. /// <summary>
  52. /// 销毁文件系统
  53. /// </summary>
  54. void IPlayMode.DestroyFileSystem()
  55. {
  56. foreach (var fileSystem in FileSystems)
  57. {
  58. fileSystem.OnDestroy();
  59. }
  60. FileSystems.Clear();
  61. }
  62. /// <summary>
  63. /// 向网络端请求最新的资源版本
  64. /// </summary>
  65. RequestPackageVersionOperation IPlayMode.RequestPackageVersionAsync(bool appendTimeTicks, int timeout)
  66. {
  67. var operation = new RequestPackageVersionImplOperation(this, appendTimeTicks, timeout);
  68. return operation;
  69. }
  70. /// <summary>
  71. /// 向网络端请求并更新清单
  72. /// </summary>
  73. UpdatePackageManifestOperation IPlayMode.UpdatePackageManifestAsync(string packageVersion, int timeout)
  74. {
  75. var operation = new UpdatePackageManifestOperation(this, packageVersion, timeout);
  76. return operation;
  77. }
  78. /// <summary>
  79. /// 预下载指定版本的包裹内容
  80. /// </summary>
  81. PreDownloadContentOperation IPlayMode.PreDownloadContentAsync(string packageVersion, int timeout)
  82. {
  83. var operation = new PreDownloadContentOperation(this, packageVersion, timeout);
  84. return operation;
  85. }
  86. /// <summary>
  87. /// 清理缓存文件
  88. /// </summary>
  89. ClearCacheFilesOperation IPlayMode.ClearCacheFilesAsync(string clearMode, object clearParam)
  90. {
  91. var operation = new ClearCacheFilesOperation(this, clearMode, clearParam);
  92. return operation;
  93. }
  94. // 下载相关
  95. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
  96. {
  97. List<BundleInfo> downloadList = GetDownloadListByAll(ActiveManifest);
  98. var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  99. return operation;
  100. }
  101. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
  102. {
  103. List<BundleInfo> downloadList = GetDownloadListByTags(ActiveManifest, tags);
  104. var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  105. return operation;
  106. }
  107. ResourceDownloaderOperation IPlayMode.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain, int timeout)
  108. {
  109. List<BundleInfo> downloadList = GetDownloadListByPaths(ActiveManifest, assetInfos, recursiveDownload);
  110. var operation = new ResourceDownloaderOperation(PackageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout);
  111. return operation;
  112. }
  113. // 解压相关
  114. ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
  115. {
  116. List<BundleInfo> unpcakList = GetUnpackListByAll(ActiveManifest);
  117. var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout);
  118. return operation;
  119. }
  120. ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
  121. {
  122. List<BundleInfo> unpcakList = GetUnpackListByTags(ActiveManifest, tags);
  123. var operation = new ResourceUnpackerOperation(PackageName, unpcakList, upackingMaxNumber, failedTryAgain, timeout);
  124. return operation;
  125. }
  126. // 导入相关
  127. ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
  128. {
  129. List<BundleInfo> importerList = GetImporterListByFilePaths(ActiveManifest, filePaths);
  130. var operation = new ResourceImporterOperation(PackageName, importerList, importerMaxNumber, failedTryAgain, timeout);
  131. return operation;
  132. }
  133. #endregion
  134. #region IBundleQuery接口
  135. private BundleInfo CreateBundleInfo(PackageBundle packageBundle)
  136. {
  137. if (packageBundle == null)
  138. throw new Exception("Should never get here !");
  139. var fileSystem = GetBelongFileSystem(packageBundle);
  140. if (fileSystem != null)
  141. {
  142. BundleInfo bundleInfo = new BundleInfo(fileSystem, packageBundle);
  143. return bundleInfo;
  144. }
  145. throw new Exception($"Can not found belong file system : {packageBundle.BundleName}");
  146. }
  147. BundleInfo IBundleQuery.GetMainBundleInfo(AssetInfo assetInfo)
  148. {
  149. if (assetInfo == null || assetInfo.IsInvalid)
  150. throw new Exception("Should never get here !");
  151. // 注意:如果清单里未找到资源包会抛出异常!
  152. var packageBundle = ActiveManifest.GetMainPackageBundle(assetInfo.Asset);
  153. return CreateBundleInfo(packageBundle);
  154. }
  155. BundleInfo[] IBundleQuery.GetDependBundleInfos(AssetInfo assetInfo)
  156. {
  157. if (assetInfo == null || assetInfo.IsInvalid)
  158. throw new Exception("Should never get here !");
  159. // 注意:如果清单里未找到资源包会抛出异常!
  160. PackageBundle[] depends;
  161. if (assetInfo.LoadMethod == AssetInfo.ELoadMethod.LoadAllAssets)
  162. {
  163. var mainBundle = ActiveManifest.GetMainPackageBundle(assetInfo.Asset);
  164. depends = ActiveManifest.GetAllDependencies(mainBundle);
  165. }
  166. else
  167. {
  168. depends = ActiveManifest.GetAllDependencies(assetInfo.Asset);
  169. }
  170. List<BundleInfo> result = new List<BundleInfo>(depends.Length);
  171. foreach (var packageBundle in depends)
  172. {
  173. BundleInfo bundleInfo = CreateBundleInfo(packageBundle);
  174. result.Add(bundleInfo);
  175. }
  176. return result.ToArray();
  177. }
  178. string IBundleQuery.GetMainBundleName(int bundleID)
  179. {
  180. // 注意:如果清单里未找到资源包会抛出异常!
  181. var packageBundle = ActiveManifest.GetMainPackageBundle(bundleID);
  182. return packageBundle.BundleName;
  183. }
  184. string IBundleQuery.GetMainBundleName(AssetInfo assetInfo)
  185. {
  186. if (assetInfo == null || assetInfo.IsInvalid)
  187. throw new Exception("Should never get here !");
  188. // 注意:如果清单里未找到资源包会抛出异常!
  189. var packageBundle = ActiveManifest.GetMainPackageBundle(assetInfo.Asset);
  190. return packageBundle.BundleName;
  191. }
  192. string[] IBundleQuery.GetDependBundleNames(AssetInfo assetInfo)
  193. {
  194. if (assetInfo == null || assetInfo.IsInvalid)
  195. throw new Exception("Should never get here !");
  196. // 注意:如果清单里未找到资源包会抛出异常!
  197. var depends = ActiveManifest.GetAllDependencies(assetInfo.Asset);
  198. List<string> result = new List<string>(depends.Length);
  199. foreach (var packageBundle in depends)
  200. {
  201. result.Add(packageBundle.BundleName);
  202. }
  203. return result.ToArray();
  204. }
  205. #endregion
  206. /// <summary>
  207. /// 获取主文件系统
  208. /// 说明:文件系统列表里,最后一个属于主文件系统
  209. /// </summary>
  210. public IFileSystem GetMainFileSystem()
  211. {
  212. int count = FileSystems.Count;
  213. if (count == 0)
  214. return null;
  215. return FileSystems[count - 1];
  216. }
  217. /// <summary>
  218. /// 获取资源包所属文件系统
  219. /// </summary>
  220. public IFileSystem GetBelongFileSystem(PackageBundle packageBundle)
  221. {
  222. for (int i = 0; i < FileSystems.Count; i++)
  223. {
  224. IFileSystem fileSystem = FileSystems[i];
  225. if (fileSystem.Belong(packageBundle))
  226. {
  227. return fileSystem;
  228. }
  229. }
  230. YooLogger.Error($"Can not found belong file system : {packageBundle.BundleName}");
  231. return null;
  232. }
  233. public List<BundleInfo> GetDownloadListByAll(PackageManifest manifest)
  234. {
  235. if (manifest == null)
  236. return new List<BundleInfo>();
  237. List<BundleInfo> result = new List<BundleInfo>(1000);
  238. foreach (var packageBundle in manifest.BundleList)
  239. {
  240. var fileSystem = GetBelongFileSystem(packageBundle);
  241. if (fileSystem == null)
  242. continue;
  243. if (fileSystem.NeedDownload(packageBundle))
  244. {
  245. var bundleInfo = new BundleInfo(fileSystem, packageBundle);
  246. result.Add(bundleInfo);
  247. }
  248. }
  249. return result;
  250. }
  251. public List<BundleInfo> GetDownloadListByTags(PackageManifest manifest, string[] tags)
  252. {
  253. if (manifest == null)
  254. return new List<BundleInfo>();
  255. List<BundleInfo> result = new List<BundleInfo>(1000);
  256. foreach (var packageBundle in manifest.BundleList)
  257. {
  258. var fileSystem = GetBelongFileSystem(packageBundle);
  259. if (fileSystem == null)
  260. continue;
  261. if (fileSystem.NeedDownload(packageBundle))
  262. {
  263. // 如果未带任何标记,则统一下载
  264. if (packageBundle.HasAnyTags() == false)
  265. {
  266. var bundleInfo = new BundleInfo(fileSystem, packageBundle);
  267. result.Add(bundleInfo);
  268. }
  269. else
  270. {
  271. // 查询DLC资源
  272. if (packageBundle.HasTag(tags))
  273. {
  274. var bundleInfo = new BundleInfo(fileSystem, packageBundle);
  275. result.Add(bundleInfo);
  276. }
  277. }
  278. }
  279. }
  280. return result;
  281. }
  282. public List<BundleInfo> GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos, bool recursiveDownload)
  283. {
  284. if (manifest == null)
  285. return new List<BundleInfo>();
  286. // 获取资源对象的资源包和所有依赖资源包
  287. List<PackageBundle> checkList = new List<PackageBundle>();
  288. foreach (var assetInfo in assetInfos)
  289. {
  290. if (assetInfo.IsInvalid)
  291. {
  292. YooLogger.Warning(assetInfo.Error);
  293. continue;
  294. }
  295. // 注意:如果清单里未找到资源包会抛出异常!
  296. PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.Asset);
  297. if (checkList.Contains(mainBundle) == false)
  298. checkList.Add(mainBundle);
  299. // 注意:如果清单里未找到资源包会抛出异常!
  300. PackageBundle[] mainDependBundles = manifest.GetAllDependencies(assetInfo.Asset);
  301. foreach (var dependBundle in mainDependBundles)
  302. {
  303. if (checkList.Contains(dependBundle) == false)
  304. checkList.Add(dependBundle);
  305. }
  306. // 下载主资源包内所有资源对象依赖的资源包
  307. if (recursiveDownload)
  308. {
  309. foreach (var otherMainAsset in mainBundle.IncludeMainAssets)
  310. {
  311. var otherMainBundle = manifest.GetMainPackageBundle(otherMainAsset.BundleID);
  312. if (checkList.Contains(otherMainBundle) == false)
  313. checkList.Add(otherMainBundle);
  314. PackageBundle[] otherDependBundles = manifest.GetAllDependencies(otherMainAsset);
  315. foreach (var dependBundle in otherDependBundles)
  316. {
  317. if (checkList.Contains(dependBundle) == false)
  318. checkList.Add(dependBundle);
  319. }
  320. }
  321. }
  322. }
  323. List<BundleInfo> result = new List<BundleInfo>(1000);
  324. foreach (var packageBundle in checkList)
  325. {
  326. var fileSystem = GetBelongFileSystem(packageBundle);
  327. if (fileSystem == null)
  328. continue;
  329. if (fileSystem.NeedDownload(packageBundle))
  330. {
  331. var bundleInfo = new BundleInfo(fileSystem, packageBundle);
  332. result.Add(bundleInfo);
  333. }
  334. }
  335. return result;
  336. }
  337. public List<BundleInfo> GetUnpackListByAll(PackageManifest manifest)
  338. {
  339. if (manifest == null)
  340. return new List<BundleInfo>();
  341. List<BundleInfo> result = new List<BundleInfo>(1000);
  342. foreach (var packageBundle in manifest.BundleList)
  343. {
  344. var fileSystem = GetBelongFileSystem(packageBundle);
  345. if (fileSystem == null)
  346. continue;
  347. if (fileSystem.NeedUnpack(packageBundle))
  348. {
  349. var bundleInfo = new BundleInfo(fileSystem, packageBundle);
  350. result.Add(bundleInfo);
  351. }
  352. }
  353. return result;
  354. }
  355. public List<BundleInfo> GetUnpackListByTags(PackageManifest manifest, string[] tags)
  356. {
  357. if (manifest == null)
  358. return new List<BundleInfo>();
  359. List<BundleInfo> result = new List<BundleInfo>(1000);
  360. foreach (var packageBundle in manifest.BundleList)
  361. {
  362. var fileSystem = GetBelongFileSystem(packageBundle);
  363. if (fileSystem == null)
  364. continue;
  365. if (fileSystem.NeedUnpack(packageBundle))
  366. {
  367. if (packageBundle.HasTag(tags))
  368. {
  369. var bundleInfo = new BundleInfo(fileSystem, packageBundle);
  370. result.Add(bundleInfo);
  371. }
  372. }
  373. }
  374. return result;
  375. }
  376. public List<BundleInfo> GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths)
  377. {
  378. if (manifest == null)
  379. return new List<BundleInfo>();
  380. List<BundleInfo> result = new List<BundleInfo>();
  381. foreach (var filePath in filePaths)
  382. {
  383. string fileName = System.IO.Path.GetFileName(filePath);
  384. if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle))
  385. {
  386. var fileSystem = GetBelongFileSystem(packageBundle);
  387. if (fileSystem == null)
  388. continue;
  389. if (fileSystem.NeedImport(packageBundle))
  390. {
  391. var bundleInfo = new BundleInfo(fileSystem, packageBundle, filePath);
  392. result.Add(bundleInfo);
  393. }
  394. }
  395. else
  396. {
  397. YooLogger.Warning($"Not found package bundle, importer file path : {filePath}");
  398. }
  399. }
  400. return result;
  401. }
  402. }
  403. }