Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6KB

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine.Networking;
  5. namespace Svelto.Services
  6. {
  7. public class UnityDownloadHandler : DownloadHandlerScript
  8. {
  9. public string GetError()
  10. {
  11. if (String.IsNullOrEmpty(_errorString) == true)
  12. {
  13. MemoryStream errorBuffer = new MemoryStream(_dataLength);
  14. errorBuffer.Write(data, 0, _dataLength);
  15. _errorString = Encoding.UTF8.GetString(errorBuffer.GetBuffer(), 0, (int) errorBuffer.Length);
  16. errorBuffer.Dispose();
  17. }
  18. return _errorString;
  19. }
  20. protected override bool ReceiveData(byte[] data, int dataLength)
  21. {
  22. if (data.Length < 1)
  23. return false; // No need to receive data
  24. _data = data;
  25. _dataLength = dataLength;
  26. if (_handler == null)
  27. return false; // No need to receive data
  28. return _handler.ReceiveData(data, dataLength);
  29. }
  30. public UnityDownloadHandler(IResponseHandler handler)
  31. {
  32. _handler = handler;
  33. }
  34. protected override byte[] GetData()
  35. {
  36. return _data;
  37. }
  38. // Called when all data has been received from the server and delivered via ReceiveData.
  39. protected override void CompleteContent()
  40. {
  41. _handler?.CompleteContent();
  42. }
  43. readonly IResponseHandler _handler;
  44. byte[] _data;
  45. int _dataLength;
  46. string _errorString;
  47. }
  48. }