OfflinePlayModeImpl.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. internal class OfflinePlayModeImpl : IPlayModeServices, IBundleServices
  7. {
  8. private PackageManifest _activeManifest;
  9. /// <summary>
  10. /// 异步初始化
  11. /// </summary>
  12. public InitializationOperation InitializeAsync(string packageName)
  13. {
  14. var operation = new OfflinePlayModeInitializationOperation(this, packageName);
  15. OperationSystem.StartOperation(operation);
  16. return operation;
  17. }
  18. #region IPlayModeServices接口
  19. public PackageManifest ActiveManifest
  20. {
  21. set
  22. {
  23. _activeManifest = value;
  24. }
  25. get
  26. {
  27. return _activeManifest;
  28. }
  29. }
  30. public void FlushManifestVersionFile()
  31. {
  32. }
  33. private bool IsCachedPackageBundle(PackageBundle packageBundle)
  34. {
  35. return CacheSystem.IsCached(packageBundle.PackageName, packageBundle.CacheGUID);
  36. }
  37. UpdatePackageVersionOperation IPlayModeServices.UpdatePackageVersionAsync(bool appendTimeTicks, int timeout)
  38. {
  39. var operation = new OfflinePlayModeUpdatePackageVersionOperation();
  40. OperationSystem.StartOperation(operation);
  41. return operation;
  42. }
  43. UpdatePackageManifestOperation IPlayModeServices.UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion, int timeout)
  44. {
  45. var operation = new OfflinePlayModeUpdatePackageManifestOperation();
  46. OperationSystem.StartOperation(operation);
  47. return operation;
  48. }
  49. PreDownloadContentOperation IPlayModeServices.PreDownloadContentAsync(string packageVersion, int timeout)
  50. {
  51. var operation = new OfflinePlayModePreDownloadContentOperation();
  52. OperationSystem.StartOperation(operation);
  53. return operation;
  54. }
  55. ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout)
  56. {
  57. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  58. }
  59. ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout)
  60. {
  61. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  62. }
  63. ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout)
  64. {
  65. return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout);
  66. }
  67. ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)
  68. {
  69. List<BundleInfo> unpcakList = GetUnpackListByAll(_activeManifest);
  70. var operation = new ResourceUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout);
  71. return operation;
  72. }
  73. private List<BundleInfo> GetUnpackListByAll(PackageManifest manifest)
  74. {
  75. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  76. foreach (var packageBundle in manifest.BundleList)
  77. {
  78. // 忽略缓存文件
  79. if (IsCachedPackageBundle(packageBundle))
  80. continue;
  81. downloadList.Add(packageBundle);
  82. }
  83. return ManifestTools.ConvertToUnpackInfos(downloadList);
  84. }
  85. ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
  86. {
  87. List<BundleInfo> unpcakList = GetUnpackListByTags(_activeManifest, tags);
  88. var operation = new ResourceUnpackerOperation(unpcakList, upackingMaxNumber, failedTryAgain, timeout);
  89. return operation;
  90. }
  91. private List<BundleInfo> GetUnpackListByTags(PackageManifest manifest, string[] tags)
  92. {
  93. List<PackageBundle> downloadList = new List<PackageBundle>(1000);
  94. foreach (var packageBundle in manifest.BundleList)
  95. {
  96. // 忽略缓存文件
  97. if (IsCachedPackageBundle(packageBundle))
  98. continue;
  99. // 查询DLC资源
  100. if (packageBundle.HasTag(tags))
  101. {
  102. downloadList.Add(packageBundle);
  103. }
  104. }
  105. return ManifestTools.ConvertToUnpackInfos(downloadList);
  106. }
  107. #endregion
  108. #region IBundleServices接口
  109. private BundleInfo CreateBundleInfo(PackageBundle packageBundle)
  110. {
  111. if (packageBundle == null)
  112. throw new Exception("Should never get here !");
  113. // 查询沙盒资源
  114. if (CacheSystem.IsCached(packageBundle.PackageName, packageBundle.CacheGUID))
  115. {
  116. BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromCache);
  117. return bundleInfo;
  118. }
  119. // 查询APP资源
  120. {
  121. BundleInfo bundleInfo = new BundleInfo(packageBundle, BundleInfo.ELoadMode.LoadFromStreaming);
  122. return bundleInfo;
  123. }
  124. }
  125. BundleInfo IBundleServices.GetBundleInfo(AssetInfo assetInfo)
  126. {
  127. if (assetInfo.IsInvalid)
  128. throw new Exception("Should never get here !");
  129. // 注意:如果清单里未找到资源包会抛出异常!
  130. var packageBundle = _activeManifest.GetMainPackageBundle(assetInfo.AssetPath);
  131. return CreateBundleInfo(packageBundle);
  132. }
  133. BundleInfo[] IBundleServices.GetAllDependBundleInfos(AssetInfo assetInfo)
  134. {
  135. if (assetInfo.IsInvalid)
  136. throw new Exception("Should never get here !");
  137. // 注意:如果清单里未找到资源包会抛出异常!
  138. var depends = _activeManifest.GetAllDependencies(assetInfo.AssetPath);
  139. List<BundleInfo> result = new List<BundleInfo>(depends.Length);
  140. foreach (var packageBundle in depends)
  141. {
  142. BundleInfo bundleInfo = CreateBundleInfo(packageBundle);
  143. result.Add(bundleInfo);
  144. }
  145. return result.ToArray();
  146. }
  147. string IBundleServices.GetBundleName(int bundleID)
  148. {
  149. return _activeManifest.GetBundleName(bundleID);
  150. }
  151. bool IBundleServices.IsServicesValid()
  152. {
  153. return _activeManifest != null;
  154. }
  155. #endregion
  156. }
  157. }