ShowLoginUIEventHandle.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using Cysharp.Threading.Tasks;
  2. using ET.EventType;
  3. using FairyGUI;
  4. using Sirenix.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. namespace ET.Client
  9. {
  10. [Event]
  11. public class ShowLoginUIEventHandle : BEvent<EventType.ShowLoginUIEvent>
  12. {
  13. private string mIP;
  14. private int mPort;
  15. private string strRoomToken;
  16. private bool bEnd = false;
  17. private bool bIsCloud = false;
  18. private bool bIsMobile = false;
  19. private bool bIsFullScreen = false;
  20. private int iScreenHeight = 0;
  21. private int iScreenWidth = 0;
  22. protected override async ETTask OnEvent(ShowLoginUIEvent a)
  23. {
  24. InitArgs();
  25. UIHelper.RemoveAllUiExceptSth();
  26. try
  27. {
  28. UIPackage.RemovePackage("CheckForResUpdate");
  29. }
  30. catch
  31. { }
  32. var view = await UIHelper.Create( "Login" );
  33. bEnd = false;
  34. InitLogin(view);
  35. }
  36. private void InitArgs()
  37. {
  38. var args = Environment.GetCommandLineArgs();
  39. for (var i = 0; i < args.Length; i++)
  40. {
  41. var arg = args[i];
  42. bool hasNext = args.Length > i + 1;
  43. if (arg.StartsWith("-token="))
  44. {
  45. strRoomToken = args[i][7..];
  46. Log.Debug($"env token: {strRoomToken}");
  47. continue;
  48. }
  49. switch(arg)
  50. {
  51. case "-cloud-game":
  52. bIsCloud = hasNext && args[i + 1] == "1";
  53. Log.Debug($"args: -cloud-game: {bIsCloud}");
  54. ++i;
  55. break;
  56. case "-mobile":
  57. bIsMobile = hasNext && args[i + 1] == "1";
  58. Log.Debug($"args: -mobile: {bIsMobile}");
  59. ++i;
  60. break;
  61. case "-screen-fullscreen":
  62. bIsFullScreen = hasNext && args[i + 1] == "1";
  63. Log.Debug($"args: -screen-fullscreen: {bIsFullScreen}");
  64. ++i;
  65. break;
  66. case "-screen-height":
  67. try
  68. {
  69. if(hasNext)
  70. {
  71. iScreenHeight = int.Parse(args[i + 1]);
  72. Log.Debug($"args: -screen-height: {iScreenHeight}");
  73. }
  74. }
  75. catch { }
  76. ++i;
  77. break;
  78. case "-screen-width":
  79. try
  80. {
  81. if (hasNext)
  82. {
  83. iScreenWidth = int.Parse(args[i + 1]);
  84. Log.Debug($"args: -screen-width: {iScreenWidth}");
  85. }
  86. }
  87. catch { }
  88. ++i;
  89. break;
  90. }
  91. }
  92. }
  93. private void InitServerList(GComboBox list)
  94. {
  95. var saveip = GameSetting.Instance.GetString(GameSetting.Sets.Server_str);
  96. int select = -1;
  97. List<string> servers = new();
  98. foreach (var info in ServerInfo.ServerList)
  99. {
  100. servers.Add(info.ShowName);
  101. if (select < 0 && !saveip.IsNullOrWhitespace())
  102. {
  103. if (saveip == info.HostStr)
  104. {
  105. select = servers.Count - 1;
  106. }
  107. }
  108. }
  109. if(select < 0 && !saveip.IsNullOrWhitespace())
  110. {
  111. try
  112. {
  113. var ipendpoint = NetworkHelper.ToIPEndPoint(saveip);
  114. servers.Add($"Custom[{saveip}]");
  115. ServerInfo.ServerList.Add(new ServerInfo()
  116. {
  117. ShowName = $"Custom[{saveip}]",
  118. Ip = ipendpoint.Address.ToString(),
  119. Port = ipendpoint.Port,
  120. }) ;
  121. select = servers.Count - 1;
  122. }
  123. catch(Exception)
  124. {
  125. Log.Error($"error save ip: {saveip}");
  126. }
  127. }
  128. if(select < 0)
  129. {
  130. select = 0;
  131. }
  132. var cur = ServerInfo.ServerList[select];
  133. mIP = cur.Ip;
  134. mPort = cur.Port;
  135. list.items = servers.ToArray();
  136. list.selectedIndex = select;
  137. list.onChanged.Set(() =>
  138. {
  139. var info = ServerInfo.ServerList[list.selectedIndex];
  140. GameSetting.Instance.SetString(GameSetting.Sets.Server_str, info.HostStr);
  141. mIP = info.Ip;
  142. mPort = info.Port;
  143. });
  144. list.visible = false;
  145. }
  146. private void InitResolutionList(GComboBox list)
  147. {
  148. list.visible = !bIsCloud;
  149. if(bIsFullScreen)
  150. {
  151. if(iScreenHeight == 0) iScreenHeight = Screen.height;
  152. if(iScreenWidth == 0) iScreenWidth = iScreenHeight * 9 / 16;
  153. Log.Debug($"fullscreen : {iScreenWidth}X{iScreenHeight}");
  154. }
  155. int val = GameSetting.Instance.GetInt(GameSetting.Sets.Resolution_int, iScreenHeight);
  156. int select = -1;
  157. List<string> showlist = new();
  158. foreach (var info in ResolutionInfo.ResolutionList)
  159. {
  160. showlist.Add(info.ShowName);
  161. if (select < 0 && val == info.Y)
  162. {
  163. select = showlist.Count - 1;
  164. }
  165. }
  166. if (select < 0)
  167. {
  168. select = 0;
  169. }
  170. list.items = showlist.ToArray();
  171. list.selectedIndex = select;
  172. list.onChanged.Set(() =>
  173. {
  174. var info = ResolutionInfo.ResolutionList[list.selectedIndex];
  175. GameSetting.Instance.SetInt(GameSetting.Sets.Resolution_int, info.Y);
  176. Screen.SetResolution(info.X, info.Y, false);
  177. Log.Debug($"SetResolution x:{info.X}, y:{info.Y}");
  178. });
  179. if (iScreenWidth == 0 || iScreenHeight == 0)
  180. {
  181. var cur = ResolutionInfo.ResolutionList[select];
  182. iScreenWidth = cur.X;
  183. iScreenHeight = cur.Y;
  184. }
  185. Screen.SetResolution(iScreenWidth, iScreenHeight, bIsFullScreen);
  186. }
  187. private async ETTask FakeUpdate(GComboBox listsvr, GComboBox listResolution)
  188. {
  189. while(!bEnd)
  190. {
  191. await TimerComponent.Instance.WaitAsync(100);
  192. if(Input.GetKey(KeyCode.F11))
  193. {
  194. listsvr.visible = true;
  195. listResolution.visible = true;
  196. break;
  197. }
  198. }
  199. }
  200. private void InitLogin(GComponent view)
  201. {
  202. var globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  203. var field = typeof(GlobalConfig).GetField("ExeVersion");
  204. string exeVersion = "first";
  205. if (field != null)
  206. {
  207. exeVersion = field.GetValue(globalConfig).ToString();
  208. }
  209. var txtVer = view.GetChild("Text_Version");
  210. txtVer.text = $"Main({exeVersion}) Res({YooAsset.YooAssets.GetResourceVersion()})@{globalConfig.Version}";
  211. var comp = view.GetChild("comp_login") as GComponent;
  212. var listSvr = view.GetChild("listServer").asComboBox;
  213. var listResolution = view.GetChild("listResolution").asComboBox;
  214. InitServerList(listSvr);
  215. InitResolutionList(listResolution);
  216. FakeUpdate(listSvr, listResolution).Coroutine();
  217. var tips = comp.GetChild( "txt_tips" );
  218. var inputID = comp.GetChild("txtID").asTextInput;
  219. var imgtips = comp.GetChild("img_tips");
  220. inputID.text = strRoomToken;
  221. var btn = comp.GetChild("btn_go").asButton;
  222. btn.onClick.Add(async () =>
  223. {
  224. tips.visible = false;
  225. imgtips.visible = true;
  226. btn.visible = false;
  227. var ret = await LoginHelper.Login(mIP, mPort, strRoomToken);
  228. if (ret != ErrorCode.ERR_Success)
  229. {
  230. imgtips.visible = false;
  231. tips.visible = true;
  232. btn.visible = true;
  233. if (ret == ErrorCode.ERR_UserNameOrPasswordFormatError ||
  234. ret == ErrorCode.ERR_UserNameOrPasswordError)
  235. {
  236. tips.text = "用户名或密码错误";
  237. }
  238. else
  239. {
  240. tips.text = "连接服务器过程中出现了问题\n" +
  241. "如重试后还是不行,请与客服联系" +
  242. "\n抖音:[color=#FF0000]奥陌陌网络科技[/color]" +
  243. "\n微信:[color=#FF0000]omm_wh[/color]";
  244. }
  245. listSvr.visible = true;
  246. }
  247. else
  248. {
  249. bEnd = true;
  250. imgtips.visible = false;
  251. }
  252. });
  253. if (strRoomToken.IsNullOrWhitespace())
  254. {
  255. tips.visible = true;
  256. tips.text = "获得直播间token失败,请重新启动\n" +
  257. "如重试后还是不行,请与客服联系" +
  258. "\n抖音:[color=#FF0000]奥陌陌网络科技[/color]" +
  259. "\n微信:[color=#FF0000]omm_wh[/color]";
  260. }
  261. //else if(bIsFullScreen || bIsCloud)
  262. //{
  263. // await TimerComponent.Instance.WaitAsync(1000);
  264. // btn.FireClick(false, true);
  265. //}
  266. }
  267. }
  268. }