CSharpAddressableManifest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using XGame.Framework.i18n;
  2. using XGame.Framework.Quality;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace XGame.Framework.Asset.Addressable
  6. {
  7. internal partial class CSharpAddressableManifest : IAddressableManifest, IDisposable
  8. {
  9. /// <summary>
  10. /// Addressable资源集合
  11. /// </summary>
  12. private Dictionary<long, Data.AssetInfo> _assetInfoMap;
  13. /// <summary>
  14. /// i18n信息集合
  15. /// <Id, <LangFlag, Id>>
  16. /// </summary>
  17. private Dictionary<long, Dictionary<LanguageType, long>> _i18nInfoMap;
  18. /// <summary>
  19. /// Lod信息集合
  20. /// <id, <KQualityLevel, Id>>
  21. /// </summary>
  22. private Dictionary<long, Dictionary<XQualityLevel, long>> _lodInfoMap;
  23. /// <summary>
  24. /// Resources目录下的资源集合
  25. /// </summary>
  26. private HashSet<string> _assetInResources;
  27. /// <summary>
  28. /// <addressableId, addressableName>
  29. /// </summary>
  30. private Dictionary<long, string> _idToNameMap;
  31. /// <summary>
  32. /// <addressableName, addressableId>
  33. /// </summary>
  34. private Dictionary<string, long> _nameToIdMap;
  35. public CSharpAddressableManifest()
  36. {
  37. _assetInResources = new HashSet<string>();
  38. _idToNameMap = new Dictionary<long, string>();
  39. _nameToIdMap = new Dictionary<string, long>();
  40. }
  41. public void Init(AddressableInfosSo addressableInfos)
  42. {
  43. if (addressableInfos == null)
  44. return;
  45. CopyAssetInfos(addressableInfos.assetInfos);
  46. AddressableUtils.CopyI18nInfos(addressableInfos.i18nInfos, ref _i18nInfoMap);
  47. AddressableUtils.CopyLodInfos(addressableInfos.lodInfos, ref _lodInfoMap);
  48. }
  49. private void CopyAssetInfos(Data.AssetInfo[] assetInfos)
  50. {
  51. if (assetInfos == null || assetInfos.Length == 0)
  52. return;
  53. if (_assetInfoMap == null)
  54. _assetInfoMap = new Dictionary<long, Data.AssetInfo>();
  55. foreach (var assetInfo in assetInfos)
  56. {
  57. _assetInfoMap.Add(assetInfo.addressableId, assetInfo);
  58. SetAssetInfo(assetInfo);
  59. }
  60. }
  61. private void SetAssetInfo(Data.AssetInfo assetInfo)
  62. {
  63. _idToNameMap.Add(assetInfo.addressableId, assetInfo.addressableName);
  64. _nameToIdMap.Add(assetInfo.addressableName, assetInfo.addressableId);
  65. if (!string.IsNullOrEmpty(assetInfo.relativePath))
  66. {
  67. _assetInResources.Add(assetInfo.addressableName);
  68. }
  69. }
  70. #region 接口实现
  71. public bool ContainsName(string addressableName)
  72. {
  73. return _nameToIdMap?.ContainsKey(addressableName) ?? false;
  74. }
  75. public bool IsInResourcesByName(string addressableName)
  76. {
  77. return _assetInResources?.Contains(addressableName) ?? false;
  78. }
  79. public string GetRelativePath(string addressableName)
  80. {
  81. if (_nameToIdMap.TryGetValue(addressableName, out var addressableId) &&
  82. _assetInfoMap.TryGetValue(addressableId, out var assetInfo))
  83. {
  84. return assetInfo.relativePath;
  85. }
  86. return string.Empty;
  87. }
  88. public bool TryGetI18nAddressableName(string addressableName, LanguageType langFlag, out string i18nName)
  89. {
  90. i18nName = string.Empty;
  91. if (langFlag == LanguageType.NONE || _i18nInfoMap == null || string.IsNullOrEmpty(addressableName))
  92. return false;
  93. if (_nameToIdMap.TryGetValue(addressableName, out var addressableId) &&
  94. _i18nInfoMap.TryGetValue(addressableId, out var langMap) &&
  95. langMap.TryGetValue(langFlag, out var i18nId) &&
  96. _idToNameMap.TryGetValue(i18nId, out i18nName))
  97. {
  98. return true;
  99. }
  100. return false;
  101. }
  102. public bool TryGetLodAddressableName(string addressableName, XQualityLevel quality, out string lodName)
  103. {
  104. lodName = string.Empty;
  105. if (_lodInfoMap == null || !quality.IsLodQuality() || string.IsNullOrEmpty(addressableName))
  106. {
  107. return false;
  108. }
  109. if (quality == XQualityLevel.UltraHD)
  110. {
  111. //HD使用High的资源
  112. quality = XQualityLevel.High;
  113. }
  114. if (_nameToIdMap.TryGetValue(addressableName, out var addressableId) &&
  115. _lodInfoMap.TryGetValue(addressableId, out var qualityMap) &&
  116. qualityMap.TryGetValue(quality, out var lodId) &&
  117. _idToNameMap.TryGetValue(lodId, out lodName))
  118. {
  119. return true;
  120. }
  121. return false;
  122. }
  123. void IDisposable.Dispose()
  124. {
  125. _assetInfoMap?.Clear();
  126. _assetInfoMap = null;
  127. _i18nInfoMap?.Clear();
  128. _i18nInfoMap = null;
  129. _lodInfoMap?.Clear();
  130. _lodInfoMap = null;
  131. _assetInResources?.Clear();
  132. _assetInResources = null;
  133. _idToNameMap?.Clear();
  134. _idToNameMap = null;
  135. _nameToIdMap?.Clear();
  136. _nameToIdMap = null;
  137. }
  138. #endregion
  139. }
  140. }