RawFileOperationHandle.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace YooAsset
  5. {
  6. public class RawFileOperationHandle : OperationHandleBase, IDisposable
  7. {
  8. private System.Action<RawFileOperationHandle> _callback;
  9. internal RawFileOperationHandle(ProviderBase provider) : base(provider)
  10. {
  11. }
  12. internal override void InvokeCallback()
  13. {
  14. _callback?.Invoke(this);
  15. }
  16. /// <summary>
  17. /// 完成委托
  18. /// </summary>
  19. public event System.Action<RawFileOperationHandle> Completed
  20. {
  21. add
  22. {
  23. if (IsValidWithWarning == false)
  24. throw new System.Exception($"{nameof(RawFileOperationHandle)} is invalid");
  25. if (Provider.IsDone)
  26. value.Invoke(this);
  27. else
  28. _callback += value;
  29. }
  30. remove
  31. {
  32. if (IsValidWithWarning == false)
  33. throw new System.Exception($"{nameof(RawFileOperationHandle)} is invalid");
  34. _callback -= value;
  35. }
  36. }
  37. /// <summary>
  38. /// 等待异步执行完毕
  39. /// </summary>
  40. public void WaitForAsyncComplete()
  41. {
  42. if (IsValidWithWarning == false)
  43. return;
  44. Provider.WaitForAsyncComplete();
  45. }
  46. /// <summary>
  47. /// 释放资源句柄
  48. /// </summary>
  49. public void Release()
  50. {
  51. this.ReleaseInternal();
  52. }
  53. /// <summary>
  54. /// 释放资源句柄
  55. /// </summary>
  56. public void Dispose()
  57. {
  58. this.ReleaseInternal();
  59. }
  60. /// <summary>
  61. /// 获取原生文件的二进制数据
  62. /// </summary>
  63. public byte[] GetRawFileData()
  64. {
  65. if (IsValidWithWarning == false)
  66. return null;
  67. string filePath = Provider.RawFilePath;
  68. return FileUtility.ReadAllBytes(filePath);
  69. }
  70. /// <summary>
  71. /// 获取原生文件的文本数据
  72. /// </summary>
  73. public string GetRawFileText()
  74. {
  75. if (IsValidWithWarning == false)
  76. return null;
  77. string filePath = Provider.RawFilePath;
  78. return FileUtility.ReadAllText(filePath);
  79. }
  80. /// <summary>
  81. /// 获取原生文件的路径
  82. /// </summary>
  83. public string GetRawFilePath()
  84. {
  85. if (IsValidWithWarning == false)
  86. return string.Empty;
  87. return Provider.RawFilePath;
  88. }
  89. }
  90. }