ProcedureUpdateResources.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using GameFramework;
  2. using GameFramework.Event;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityGameFramework.Runtime;
  6. using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
  7. namespace GFMain
  8. {
  9. public class ProcedureUpdateResources : ProcedureBase
  10. {
  11. private bool m_UpdateResourcesComplete = false;
  12. private int m_UpdateCount = 0;
  13. private long m_UpdateTotalCompressedLength = 0L;
  14. private int m_UpdateSuccessCount = 0;
  15. private List<UpdateLengthData> m_UpdateLengthData = new List<UpdateLengthData>();
  16. private UpdateResourceForm m_UpdateResourceForm = null;
  17. public override bool UseNativeDialog
  18. {
  19. get
  20. {
  21. return true;
  22. }
  23. }
  24. protected override void OnEnter(ProcedureOwner procedureOwner)
  25. {
  26. base.OnEnter(procedureOwner);
  27. m_UpdateResourcesComplete = false;
  28. m_UpdateCount = procedureOwner.GetData<VarInt32>("UpdateResourceCount");
  29. procedureOwner.RemoveData("UpdateResourceCount");
  30. m_UpdateTotalCompressedLength = procedureOwner.GetData<VarInt64>("UpdateResourceTotalCompressedLength");
  31. procedureOwner.RemoveData("UpdateResourceTotalCompressedLength");
  32. m_UpdateSuccessCount = 0;
  33. m_UpdateLengthData.Clear();
  34. m_UpdateResourceForm = null;
  35. MainEntry.Event.Subscribe(ResourceUpdateStartEventArgs.EventId, OnResourceUpdateStart);
  36. MainEntry.Event.Subscribe(ResourceUpdateChangedEventArgs.EventId, OnResourceUpdateChanged);
  37. MainEntry.Event.Subscribe(ResourceUpdateSuccessEventArgs.EventId, OnResourceUpdateSuccess);
  38. MainEntry.Event.Subscribe(ResourceUpdateFailureEventArgs.EventId, OnResourceUpdateFailure);
  39. if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
  40. {
  41. //MainEntry.UI.OpenDialog(new DialogParams
  42. //{
  43. // Mode = 2,
  44. // Title = MainEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.Title"),
  45. // Message = MainEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.Message"),
  46. // ConfirmText = MainEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.UpdateButton"),
  47. // OnClickConfirm = StartUpdateResources,
  48. // CancelText = MainEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.QuitButton"),
  49. // OnClickCancel = delegate (object userData) { UnityGameFramework.Runtime.MainEntry.Shutdown(ShutdownType.Quit); },
  50. //});
  51. return;
  52. }
  53. StartUpdateResources(null);
  54. }
  55. protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
  56. {
  57. if (m_UpdateResourceForm != null)
  58. {
  59. Object.Destroy(m_UpdateResourceForm.gameObject);
  60. m_UpdateResourceForm = null;
  61. }
  62. MainEntry.Event.Unsubscribe(ResourceUpdateStartEventArgs.EventId, OnResourceUpdateStart);
  63. MainEntry.Event.Unsubscribe(ResourceUpdateChangedEventArgs.EventId, OnResourceUpdateChanged);
  64. MainEntry.Event.Unsubscribe(ResourceUpdateSuccessEventArgs.EventId, OnResourceUpdateSuccess);
  65. MainEntry.Event.Unsubscribe(ResourceUpdateFailureEventArgs.EventId, OnResourceUpdateFailure);
  66. base.OnLeave(procedureOwner, isShutdown);
  67. }
  68. protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
  69. {
  70. base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
  71. if (!m_UpdateResourcesComplete)
  72. {
  73. return;
  74. }
  75. ChangeState<ProcedurePreload>(procedureOwner);
  76. }
  77. private void StartUpdateResources(object userData)
  78. {
  79. if (m_UpdateResourceForm == null)
  80. {
  81. m_UpdateResourceForm = Object.Instantiate(MainEntry.BuiltinData.UpdateResourceFormTemplate);
  82. }
  83. Log.Info("Start update resources...");
  84. MainEntry.Resource.UpdateResources(OnUpdateResourcesComplete);
  85. }
  86. private void RefreshProgress()
  87. {
  88. long currentTotalUpdateLength = 0L;
  89. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  90. {
  91. currentTotalUpdateLength += m_UpdateLengthData[i].Length;
  92. }
  93. float progressTotal = (float)currentTotalUpdateLength / m_UpdateTotalCompressedLength;
  94. //string descriptionText = MainEntry.Localization.GetString("UpdateResource.Tips", m_UpdateSuccessCount.ToString(), m_UpdateCount.ToString(), GetByteLengthString(currentTotalUpdateLength), GetByteLengthString(m_UpdateTotalCompressedLength), progressTotal, GetByteLengthString((int)MainEntry.Download.CurrentSpeed));
  95. string descriptionText = $"{m_UpdateSuccessCount}/{m_UpdateCount}, {GetByteLengthString(currentTotalUpdateLength)}/{GetByteLengthString(m_UpdateTotalCompressedLength)}, { progressTotal.ToString("P")}, { GetByteLengthString((int)MainEntry.Download.CurrentSpeed)}/s";
  96. m_UpdateResourceForm.SetProgress(progressTotal, descriptionText);
  97. }
  98. private string GetByteLengthString(long byteLength)
  99. {
  100. if (byteLength < 1024L) // 2 ^ 10
  101. {
  102. return Utility.Text.Format("{0} Bytes", byteLength);
  103. }
  104. if (byteLength < 1048576L) // 2 ^ 20
  105. {
  106. return Utility.Text.Format("{0:F2} KB", byteLength / 1024f);
  107. }
  108. if (byteLength < 1073741824L) // 2 ^ 30
  109. {
  110. return Utility.Text.Format("{0:F2} MB", byteLength / 1048576f);
  111. }
  112. if (byteLength < 1099511627776L) // 2 ^ 40
  113. {
  114. return Utility.Text.Format("{0:F2} GB", byteLength / 1073741824f);
  115. }
  116. if (byteLength < 1125899906842624L) // 2 ^ 50
  117. {
  118. return Utility.Text.Format("{0:F2} TB", byteLength / 1099511627776f);
  119. }
  120. if (byteLength < 1152921504606846976L) // 2 ^ 60
  121. {
  122. return Utility.Text.Format("{0:F2} PB", byteLength / 1125899906842624f);
  123. }
  124. return Utility.Text.Format("{0:F2} EB", byteLength / 1152921504606846976f);
  125. }
  126. private void OnUpdateResourcesComplete(GameFramework.Resource.IResourceGroup resourceGroup, bool result)
  127. {
  128. if (result)
  129. {
  130. m_UpdateResourcesComplete = true;
  131. Log.Info("Update resources complete with no errors.");
  132. }
  133. else
  134. {
  135. Log.Error("Update resources complete with errors.");
  136. }
  137. }
  138. private void OnResourceUpdateStart(object sender, GameEventArgs e)
  139. {
  140. ResourceUpdateStartEventArgs ne = (ResourceUpdateStartEventArgs)e;
  141. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  142. {
  143. if (m_UpdateLengthData[i].Name == ne.Name)
  144. {
  145. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  146. m_UpdateLengthData[i].Length = 0;
  147. RefreshProgress();
  148. return;
  149. }
  150. }
  151. m_UpdateLengthData.Add(new UpdateLengthData(ne.Name));
  152. }
  153. private void OnResourceUpdateChanged(object sender, GameEventArgs e)
  154. {
  155. ResourceUpdateChangedEventArgs ne = (ResourceUpdateChangedEventArgs)e;
  156. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  157. {
  158. if (m_UpdateLengthData[i].Name == ne.Name)
  159. {
  160. m_UpdateLengthData[i].Length = ne.CurrentLength;
  161. RefreshProgress();
  162. return;
  163. }
  164. }
  165. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  166. }
  167. private void OnResourceUpdateSuccess(object sender, GameEventArgs e)
  168. {
  169. ResourceUpdateSuccessEventArgs ne = (ResourceUpdateSuccessEventArgs)e;
  170. Log.Info("Update resource '{0}' success.", ne.Name);
  171. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  172. {
  173. if (m_UpdateLengthData[i].Name == ne.Name)
  174. {
  175. m_UpdateLengthData[i].Length = ne.CompressedLength;
  176. m_UpdateSuccessCount++;
  177. RefreshProgress();
  178. return;
  179. }
  180. }
  181. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  182. }
  183. private void OnResourceUpdateFailure(object sender, GameEventArgs e)
  184. {
  185. ResourceUpdateFailureEventArgs ne = (ResourceUpdateFailureEventArgs)e;
  186. if (ne.RetryCount >= ne.TotalRetryCount)
  187. {
  188. Log.Error("Update resource '{0}' failure from '{1}' with error message '{2}', retry count '{3}'.", ne.Name, ne.DownloadUri, ne.ErrorMessage, ne.RetryCount.ToString());
  189. return;
  190. }
  191. else
  192. {
  193. Log.Info("Update resource '{0}' failure from '{1}' with error message '{2}', retry count '{3}'.", ne.Name, ne.DownloadUri, ne.ErrorMessage, ne.RetryCount.ToString());
  194. }
  195. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  196. {
  197. if (m_UpdateLengthData[i].Name == ne.Name)
  198. {
  199. m_UpdateLengthData.Remove(m_UpdateLengthData[i]);
  200. RefreshProgress();
  201. return;
  202. }
  203. }
  204. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  205. }
  206. private class UpdateLengthData
  207. {
  208. private readonly string m_Name;
  209. public UpdateLengthData(string name)
  210. {
  211. m_Name = name;
  212. }
  213. public string Name
  214. {
  215. get
  216. {
  217. return m_Name;
  218. }
  219. }
  220. public int Length
  221. {
  222. get;
  223. set;
  224. }
  225. }
  226. }
  227. }