ProcedureUpdateResources.cs 9.9 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 MetaClient
  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. GameEntry.Event.Subscribe(ResourceUpdateStartEventArgs.EventId, OnResourceUpdateStart);
  36. GameEntry.Event.Subscribe(ResourceUpdateChangedEventArgs.EventId, OnResourceUpdateChanged);
  37. GameEntry.Event.Subscribe(ResourceUpdateSuccessEventArgs.EventId, OnResourceUpdateSuccess);
  38. GameEntry.Event.Subscribe(ResourceUpdateFailureEventArgs.EventId, OnResourceUpdateFailure);
  39. if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
  40. {
  41. GameEntry.UI.OpenDialog(new DialogParams
  42. {
  43. Mode = 2,
  44. Title = GameEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.Title"),
  45. Message = GameEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.Message"),
  46. ConfirmText = GameEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.UpdateButton"),
  47. OnClickConfirm = StartUpdateResources,
  48. CancelText = GameEntry.Localization.GetString("UpdateResourceViaCarrierDataNetwork.QuitButton"),
  49. OnClickCancel = delegate (object userData) { UnityGameFramework.Runtime.GameEntry.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. GameEntry.Event.Unsubscribe(ResourceUpdateStartEventArgs.EventId, OnResourceUpdateStart);
  63. GameEntry.Event.Unsubscribe(ResourceUpdateChangedEventArgs.EventId, OnResourceUpdateChanged);
  64. GameEntry.Event.Unsubscribe(ResourceUpdateSuccessEventArgs.EventId, OnResourceUpdateSuccess);
  65. GameEntry.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(GameEntry.BuiltinData.UpdateResourceFormTemplate);
  82. }
  83. Log.Info("Start update resources...");
  84. GameEntry.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 = GameEntry.Localization.GetString("UpdateResource.Tips", m_UpdateSuccessCount.ToString(), m_UpdateCount.ToString(), GetByteLengthString(currentTotalUpdateLength), GetByteLengthString(m_UpdateTotalCompressedLength), progressTotal, GetByteLengthString((int)GameEntry.Download.CurrentSpeed));
  95. m_UpdateResourceForm.SetProgress(progressTotal, descriptionText);
  96. }
  97. private string GetByteLengthString(long byteLength)
  98. {
  99. if (byteLength < 1024L) // 2 ^ 10
  100. {
  101. return Utility.Text.Format("{0} Bytes", byteLength);
  102. }
  103. if (byteLength < 1048576L) // 2 ^ 20
  104. {
  105. return Utility.Text.Format("{0:F2} KB", byteLength / 1024f);
  106. }
  107. if (byteLength < 1073741824L) // 2 ^ 30
  108. {
  109. return Utility.Text.Format("{0:F2} MB", byteLength / 1048576f);
  110. }
  111. if (byteLength < 1099511627776L) // 2 ^ 40
  112. {
  113. return Utility.Text.Format("{0:F2} GB", byteLength / 1073741824f);
  114. }
  115. if (byteLength < 1125899906842624L) // 2 ^ 50
  116. {
  117. return Utility.Text.Format("{0:F2} TB", byteLength / 1099511627776f);
  118. }
  119. if (byteLength < 1152921504606846976L) // 2 ^ 60
  120. {
  121. return Utility.Text.Format("{0:F2} PB", byteLength / 1125899906842624f);
  122. }
  123. return Utility.Text.Format("{0:F2} EB", byteLength / 1152921504606846976f);
  124. }
  125. private void OnUpdateResourcesComplete(GameFramework.Resource.IResourceGroup resourceGroup, bool result)
  126. {
  127. if (result)
  128. {
  129. m_UpdateResourcesComplete = true;
  130. Log.Info("Update resources complete with no errors.");
  131. }
  132. else
  133. {
  134. Log.Error("Update resources complete with errors.");
  135. }
  136. }
  137. private void OnResourceUpdateStart(object sender, GameEventArgs e)
  138. {
  139. ResourceUpdateStartEventArgs ne = (ResourceUpdateStartEventArgs)e;
  140. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  141. {
  142. if (m_UpdateLengthData[i].Name == ne.Name)
  143. {
  144. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  145. m_UpdateLengthData[i].Length = 0;
  146. RefreshProgress();
  147. return;
  148. }
  149. }
  150. m_UpdateLengthData.Add(new UpdateLengthData(ne.Name));
  151. }
  152. private void OnResourceUpdateChanged(object sender, GameEventArgs e)
  153. {
  154. ResourceUpdateChangedEventArgs ne = (ResourceUpdateChangedEventArgs)e;
  155. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  156. {
  157. if (m_UpdateLengthData[i].Name == ne.Name)
  158. {
  159. m_UpdateLengthData[i].Length = ne.CurrentLength;
  160. RefreshProgress();
  161. return;
  162. }
  163. }
  164. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  165. }
  166. private void OnResourceUpdateSuccess(object sender, GameEventArgs e)
  167. {
  168. ResourceUpdateSuccessEventArgs ne = (ResourceUpdateSuccessEventArgs)e;
  169. Log.Info("Update resource '{0}' success.", ne.Name);
  170. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  171. {
  172. if (m_UpdateLengthData[i].Name == ne.Name)
  173. {
  174. m_UpdateLengthData[i].Length = ne.CompressedLength;
  175. m_UpdateSuccessCount++;
  176. RefreshProgress();
  177. return;
  178. }
  179. }
  180. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  181. }
  182. private void OnResourceUpdateFailure(object sender, GameEventArgs e)
  183. {
  184. ResourceUpdateFailureEventArgs ne = (ResourceUpdateFailureEventArgs)e;
  185. if (ne.RetryCount >= ne.TotalRetryCount)
  186. {
  187. Log.Error("Update resource '{0}' failure from '{1}' with error message '{2}', retry count '{3}'.", ne.Name, ne.DownloadUri, ne.ErrorMessage, ne.RetryCount.ToString());
  188. return;
  189. }
  190. else
  191. {
  192. Log.Info("Update resource '{0}' failure from '{1}' with error message '{2}', retry count '{3}'.", ne.Name, ne.DownloadUri, ne.ErrorMessage, ne.RetryCount.ToString());
  193. }
  194. for (int i = 0; i < m_UpdateLengthData.Count; i++)
  195. {
  196. if (m_UpdateLengthData[i].Name == ne.Name)
  197. {
  198. m_UpdateLengthData.Remove(m_UpdateLengthData[i]);
  199. RefreshProgress();
  200. return;
  201. }
  202. }
  203. Log.Warning("Update resource '{0}' is invalid.", ne.Name);
  204. }
  205. private class UpdateLengthData
  206. {
  207. private readonly string m_Name;
  208. public UpdateLengthData(string name)
  209. {
  210. m_Name = name;
  211. }
  212. public string Name
  213. {
  214. get
  215. {
  216. return m_Name;
  217. }
  218. }
  219. public int Length
  220. {
  221. get;
  222. set;
  223. }
  224. }
  225. }
  226. }