DownloadVersions.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. namespace VEngine
  4. {
  5. public sealed class DownloadVersions : Operation
  6. {
  7. public readonly List<DownloadInfo> items = new List<DownloadInfo>();
  8. private Download download;
  9. private long lastDownloadedBytes;
  10. public Action<DownloadVersions> updated;
  11. public long totalSize { get; set; }
  12. public long downloadedBytes { get; private set; }
  13. public override void Start()
  14. {
  15. base.Start();
  16. downloadedBytes = 0;
  17. lastDownloadedBytes = 0;
  18. foreach (var info in items) totalSize += info.size;
  19. if (items.Count > 0)
  20. download = Download.DownloadAsync(items[0]);
  21. else
  22. Finish();
  23. }
  24. protected override void Update()
  25. {
  26. if (status == OperationStatus.Processing)
  27. {
  28. if (download.isDone)
  29. {
  30. if (download.status == DownloadStatus.Success)
  31. {
  32. lastDownloadedBytes += download.downloadedBytes;
  33. items.RemoveAt(0);
  34. if (items.Count > 0)
  35. download = Download.DownloadAsync(items[0]);
  36. else
  37. Finish();
  38. }
  39. else
  40. {
  41. Finish(download.error);
  42. }
  43. }
  44. else
  45. {
  46. downloadedBytes = lastDownloadedBytes + download.downloadedBytes;
  47. progress = downloadedBytes * 1f / totalSize;
  48. }
  49. if (updated != null) updated(this);
  50. }
  51. }
  52. }
  53. }