UpdateStaticVersionOperation.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace YooAsset
  4. {
  5. /// <summary>
  6. /// 更新静态版本操作
  7. /// </summary>
  8. public abstract class UpdateStaticVersionOperation : AsyncOperationBase
  9. {
  10. /// <summary>
  11. /// 资源版本号
  12. /// </summary>
  13. public int ResourceVersion { protected set; get; } = 0;
  14. }
  15. /// <summary>
  16. /// 编辑器下模拟运行的更新静态版本操作
  17. /// </summary>
  18. internal sealed class EditorPlayModeUpdateStaticVersionOperation : UpdateStaticVersionOperation
  19. {
  20. internal override void Start()
  21. {
  22. Status = EOperationStatus.Succeed;
  23. }
  24. internal override void Update()
  25. {
  26. }
  27. }
  28. /// <summary>
  29. /// 离线模式的更新静态版本操作
  30. /// </summary>
  31. internal sealed class OfflinePlayModeUpdateStaticVersionOperation : UpdateStaticVersionOperation
  32. {
  33. internal override void Start()
  34. {
  35. Status = EOperationStatus.Succeed;
  36. }
  37. internal override void Update()
  38. {
  39. }
  40. }
  41. /// <summary>
  42. /// 联机模式的更新静态版本操作
  43. /// </summary>
  44. internal sealed class HostPlayModeUpdateStaticVersionOperation : UpdateStaticVersionOperation
  45. {
  46. private enum ESteps
  47. {
  48. None,
  49. LoadStaticVersion,
  50. CheckStaticVersion,
  51. Done,
  52. }
  53. private static int RequestCount = 0;
  54. private readonly HostPlayModeImpl _impl;
  55. private readonly int _timeout;
  56. private ESteps _steps = ESteps.None;
  57. private UnityWebDataRequester _downloader;
  58. internal HostPlayModeUpdateStaticVersionOperation(HostPlayModeImpl impl, int timeout)
  59. {
  60. _impl = impl;
  61. _timeout = timeout;
  62. }
  63. internal override void Start()
  64. {
  65. RequestCount++;
  66. _steps = ESteps.LoadStaticVersion;
  67. }
  68. internal override void Update()
  69. {
  70. if (_steps == ESteps.None || _steps == ESteps.Done)
  71. return;
  72. if (_steps == ESteps.LoadStaticVersion)
  73. {
  74. string webURL = GetStaticVersionRequestURL(YooAssetSettings.VersionFileName);
  75. YooLogger.Log($"Beginning to request static version : {webURL}");
  76. _downloader = new UnityWebDataRequester();
  77. _downloader.SendRequest(webURL, _timeout);
  78. _steps = ESteps.CheckStaticVersion;
  79. }
  80. if (_steps == ESteps.CheckStaticVersion)
  81. {
  82. Progress = _downloader.Progress();
  83. if (_downloader.IsDone() == false)
  84. return;
  85. if (_downloader.HasError())
  86. {
  87. _steps = ESteps.Done;
  88. Status = EOperationStatus.Failed;
  89. Error = _downloader.GetError();
  90. }
  91. else
  92. {
  93. if (int.TryParse(_downloader.GetText(), out int value))
  94. {
  95. ResourceVersion = value;
  96. _steps = ESteps.Done;
  97. Status = EOperationStatus.Succeed;
  98. }
  99. else
  100. {
  101. _steps = ESteps.Done;
  102. Status = EOperationStatus.Failed;
  103. Error = $"URL : {_downloader.URL} Error : static version content is invalid.";
  104. }
  105. }
  106. _downloader.Dispose();
  107. }
  108. }
  109. private string GetStaticVersionRequestURL(string fileName)
  110. {
  111. string url;
  112. // 轮流返回请求地址
  113. if (RequestCount % 2 == 0)
  114. url = _impl.GetPatchDownloadFallbackURL(fileName);
  115. else
  116. url = _impl.GetPatchDownloadMainURL(fileName);
  117. // 注意:在URL末尾添加时间戳
  118. return $"{url}?{System.DateTime.UtcNow.Ticks}";
  119. }
  120. }
  121. }