ResourceLoader.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.IO;
  2. using UnityEngine;
  3. namespace YooAsset
  4. {
  5. internal class ResourceLoader
  6. {
  7. private IDecryptionServices _decryption;
  8. private IDeliveryLoadServices _delivery;
  9. public void Init(IDecryptionServices decryption, IDeliveryLoadServices delivery)
  10. {
  11. _decryption = decryption;
  12. _delivery = delivery;
  13. }
  14. /// <summary>
  15. /// 同步加载资源包对象
  16. /// </summary>
  17. public AssetBundle LoadAssetBundle(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream)
  18. {
  19. managedStream = null;
  20. if (bundleInfo.Bundle.Encrypted)
  21. {
  22. if (_decryption == null)
  23. {
  24. YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!");
  25. return null;
  26. }
  27. DecryptFileInfo fileInfo = new DecryptFileInfo();
  28. fileInfo.BundleName = bundleInfo.Bundle.BundleName;
  29. fileInfo.FileLoadPath = fileLoadPath;
  30. fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
  31. return _decryption.LoadAssetBundle(fileInfo, out managedStream);
  32. }
  33. else
  34. {
  35. return AssetBundle.LoadFromFile(fileLoadPath);
  36. }
  37. }
  38. /// <summary>
  39. /// 异步加载资源包对象
  40. /// </summary>
  41. public AssetBundleCreateRequest LoadAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath, out Stream managedStream)
  42. {
  43. managedStream = null;
  44. if (bundleInfo.Bundle.Encrypted)
  45. {
  46. if (_decryption == null)
  47. {
  48. YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load asset bundle {bundleInfo.Bundle.BundleName}!");
  49. return null;
  50. }
  51. DecryptFileInfo fileInfo = new DecryptFileInfo();
  52. fileInfo.BundleName = bundleInfo.Bundle.BundleName;
  53. fileInfo.FileLoadPath = fileLoadPath;
  54. fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
  55. return _decryption.LoadAssetBundleAsync(fileInfo, out managedStream);
  56. }
  57. else
  58. {
  59. return AssetBundle.LoadFromFileAsync(fileLoadPath);
  60. }
  61. }
  62. /// <summary>
  63. /// 同步加载分发的资源包对象
  64. /// </summary>
  65. public AssetBundle LoadDeliveryAssetBundle(BundleInfo bundleInfo, string fileLoadPath)
  66. {
  67. if (_delivery == null)
  68. throw new System.Exception("Should never get here !");
  69. // 注意:对于已经加密的资源包,需要开发者自行解密。
  70. DeliveryFileInfo fileInfo = new DeliveryFileInfo();
  71. fileInfo.BundleName = bundleInfo.Bundle.BundleName;
  72. fileInfo.FileLoadPath = fileLoadPath;
  73. fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
  74. fileInfo.Encrypted = bundleInfo.Bundle.Encrypted;
  75. return _delivery.LoadAssetBundle(fileInfo);
  76. }
  77. /// <summary>
  78. /// 异步加载分发的资源包对象
  79. /// </summary>
  80. public AssetBundleCreateRequest LoadDeliveryAssetBundleAsync(BundleInfo bundleInfo, string fileLoadPath)
  81. {
  82. if (_delivery == null)
  83. throw new System.Exception("Should never get here !");
  84. // 注意:对于已经加密的资源包,需要开发者自行解密。
  85. DeliveryFileInfo fileInfo = new DeliveryFileInfo();
  86. fileInfo.BundleName = bundleInfo.Bundle.BundleName;
  87. fileInfo.FileLoadPath = fileLoadPath;
  88. fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC;
  89. fileInfo.Encrypted = bundleInfo.Bundle.Encrypted;
  90. return _delivery.LoadAssetBundleAsync(fileInfo);
  91. }
  92. }
  93. }