CSharpAssetBundleManifest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. namespace XGame.Framework.Asset
  4. {
  5. public class CSharpAssetBundleManifest : IAssetBundleManifest, IDisposable
  6. {
  7. /// <summary>
  8. /// <bundleId, FullName>
  9. /// </summary>
  10. private Dictionary<uint, string> _bundleIdToNameMap = new Dictionary<uint, string>();
  11. /// <summary>
  12. /// key:bundleFullName
  13. /// </summary>
  14. private Dictionary<string, AssetBundleInfo> _bundleInfoMap;
  15. /// <summary>
  16. /// key:bundleFullName
  17. /// </summary>
  18. private Dictionary<string, uint> _bundleOffsetMap = new Dictionary<string, uint>();
  19. /// <summary>
  20. /// assetName和对应bundleName的缓存
  21. /// key:assetName
  22. /// value:bundleName
  23. /// </summary>
  24. private Dictionary<string, string> _assetToBundleNameMap = new Dictionary<string, string>();
  25. /// <summary>
  26. /// bundle和Dependencies的缓存
  27. /// 方便查找
  28. /// </summary>
  29. private Dictionary<string, string[]> _dependenciesMap = new Dictionary<string, string[]>();
  30. public CSharpAssetBundleManifest(AssetBundleInfosSo assetBundles)
  31. {
  32. _bundleInfoMap = new Dictionary<string, AssetBundleInfo>();
  33. Init(assetBundles);
  34. }
  35. private void Init(AssetBundleInfosSo assetBundles)
  36. {
  37. if (assetBundles == null || assetBundles.bundleInfos == null) return;
  38. foreach (var bundleInfo in assetBundles.bundleInfos)
  39. {
  40. var bundleName = bundleInfo.FullName;
  41. _bundleInfoMap.Add(bundleName, bundleInfo);
  42. _bundleIdToNameMap.Add(bundleInfo.bundleId, bundleName);
  43. _bundleOffsetMap.Add(bundleName, bundleInfo.offset);
  44. if (bundleInfo.assets != null)
  45. {
  46. foreach (var asset in bundleInfo.assets)
  47. {
  48. if (!_assetToBundleNameMap.ContainsKey(asset))
  49. {
  50. _assetToBundleNameMap.Add(asset, bundleName);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. //public string[] GetAllBundleNames()
  57. //{
  58. // var bundles = BundleInfoMap.Keys.ToArray();
  59. // return bundles;
  60. //}
  61. //public Hash128 GetAssetBundleHash(string assetBundleName)
  62. //{
  63. // return Hash128.Parse(assetBundleName);
  64. //}
  65. public uint GetBundleOffest(string assetBundleName)
  66. {
  67. if (_bundleOffsetMap.TryGetValue(assetBundleName, out var offset))
  68. {
  69. //Log.Debug($"GetBundleOffest bundle:{assetBundleName} offset:{offset}");
  70. return offset;
  71. }
  72. return 0;
  73. }
  74. public string GetBundleNameByAssetName(string assetName)
  75. {
  76. if (string.IsNullOrEmpty(assetName))
  77. {
  78. return string.Empty;
  79. }
  80. if (_assetToBundleNameMap.TryGetValue(assetName, out var bundleName))
  81. {
  82. return bundleName;
  83. }
  84. return bundleName;
  85. }
  86. public string[] GetDependenciesByBundleName(string assetBundleName)
  87. {
  88. if (string.IsNullOrEmpty(assetBundleName))
  89. {
  90. return null;
  91. }
  92. if (_dependenciesMap.TryGetValue(assetBundleName, out string[] dependencies))
  93. {
  94. return dependencies;
  95. }
  96. if (_bundleInfoMap.TryGetValue(assetBundleName, out var bundleInfo))
  97. {
  98. int count = bundleInfo.DependenciesCount;
  99. if (count > 0)
  100. {
  101. dependencies = new string[count];
  102. for (var index = 0; index < count; index++)
  103. {
  104. var depId = bundleInfo.dependencies[index];
  105. dependencies[index] = _bundleIdToNameMap[depId];
  106. }
  107. }
  108. _dependenciesMap.Add(assetBundleName, dependencies);
  109. }
  110. return dependencies;
  111. }
  112. //public string[] GetDependenciesByAssetName(string assetName)
  113. //{
  114. // if (string.IsNullOrEmpty(assetName))
  115. // {
  116. // return null;
  117. // }
  118. // return GetDependenciesByBundleName(GetBundleNameByAssetName(assetName));
  119. //}
  120. void IDisposable.Dispose()
  121. {
  122. _dependenciesMap.Clear();
  123. _assetToBundleNameMap.Clear();
  124. _bundleInfoMap.Clear();
  125. _bundleOffsetMap.Clear();
  126. _bundleIdToNameMap.Clear();
  127. }
  128. }
  129. }