LoginHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System;
  2. namespace ET
  3. {
  4. public static class LoginHelper
  5. {
  6. public static async ETTask<int> CheckVersion(Scene zoneScene, string address, string version)
  7. {
  8. A2C_CheckVersion a2C_GetVersion = null;
  9. Session session = null;
  10. try
  11. {
  12. session = zoneScene.GetComponent<NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address));
  13. a2C_GetVersion = (A2C_CheckVersion)await session.Call(new C2A_CheckVersion() { Version = version });
  14. }
  15. catch (Exception e)
  16. {
  17. session?.Dispose();
  18. Log.Error(e.ToString());
  19. return ErrorCode.ERR_NetWorkError;
  20. }
  21. if (a2C_GetVersion.Error != ErrorCode.ERR_Success)
  22. {
  23. session?.Dispose();
  24. return a2C_GetVersion.Error;
  25. }
  26. return ErrorCode.ERR_Success;
  27. }
  28. public static async ETTask<int> Login(Scene zoneScene, string address, string account, string password, bool isMD5)
  29. {
  30. A2C_LoginAccount a2CLoginAccount = null;
  31. Session accountSession = null;
  32. try
  33. {
  34. accountSession = zoneScene.GetComponent<NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address));
  35. var passwordMD5 = password;
  36. //密码禁止明文传输
  37. if (!isMD5)
  38. {
  39. passwordMD5 = MD5Helper.stringMD5(password);
  40. }
  41. a2CLoginAccount = (A2C_LoginAccount)await accountSession.Call(new C2A_LoginAccount() { Account = account, Password = passwordMD5 });
  42. }
  43. catch (Exception e)
  44. {
  45. accountSession?.Dispose();
  46. Log.Error(e.ToString());
  47. return ErrorCode.ERR_NetWorkError;
  48. }
  49. if (a2CLoginAccount.Error != ErrorCode.ERR_Success)
  50. {
  51. accountSession?.Dispose();
  52. return a2CLoginAccount.Error;
  53. }
  54. if (zoneScene.GetComponent<SessionComponent>() == null)
  55. {
  56. zoneScene.AddComponent<SessionComponent>();
  57. }
  58. zoneScene.GetComponent<SessionComponent>().Session = accountSession;
  59. zoneScene.GetComponent<SessionComponent>().Session.AddComponent<PingComponent>();
  60. AccountInfoComponent accountInfoComponent = zoneScene.GetComponent<AccountInfoComponent>();
  61. accountInfoComponent.Token = a2CLoginAccount.Token;
  62. accountInfoComponent.AccountId = a2CLoginAccount.AccountId;
  63. accountInfoComponent.Age = a2CLoginAccount.Age;
  64. return ErrorCode.ERR_Success;
  65. }
  66. public static async ETTask<int> Register(Scene zoneScene, string address, string account, string password, string name, string identityNum, string code)
  67. {
  68. A2C_Register r2C_Register = null;
  69. Session session = null;
  70. try
  71. {
  72. session = zoneScene.GetComponent<NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(address));
  73. {
  74. //密码禁止明文传输
  75. var passwordMD5 = MD5Helper.stringMD5(password);
  76. r2C_Register = (A2C_Register)await session.Call(new C2A_Register()
  77. { Account = account, Password = passwordMD5, Name = name, IdentityNum = identityNum, Code = code });
  78. if (r2C_Register.Error != ErrorCode.ERR_Success)
  79. {
  80. return r2C_Register.Error;
  81. }
  82. }
  83. }
  84. catch (Exception e)
  85. {
  86. session?.Dispose();
  87. Log.Error(e.ToString());
  88. return ErrorCode.ERR_NetWorkError;
  89. }
  90. return ErrorCode.ERR_Success;
  91. }
  92. public static async ETTask<int> GetServerInfos(Scene zoneScene)
  93. {
  94. A2C_GetServerInfos a2C_GetServerInfos = null;
  95. try
  96. {
  97. a2C_GetServerInfos = (A2C_GetServerInfos)await zoneScene.GetComponent<SessionComponent>().Session.Call(new C2A_GetServerInfos()
  98. {
  99. AccountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId,
  100. Token = zoneScene.GetComponent<AccountInfoComponent>().Token
  101. });
  102. }
  103. catch (Exception e)
  104. {
  105. Log.Error(e.ToString());
  106. return ErrorCode.ERR_NetWorkError;
  107. }
  108. if (a2C_GetServerInfos.Error != ErrorCode.ERR_Success)
  109. {
  110. return a2C_GetServerInfos.Error;
  111. }
  112. foreach (var serverInfoProto in a2C_GetServerInfos.ServerInfosList)
  113. {
  114. ServerInfo serverInfo = zoneScene.GetComponent<ServerInfosComponent>().AddChild<ServerInfo>();
  115. serverInfo.FromMessage(serverInfoProto);
  116. zoneScene.GetComponent<ServerInfosComponent>().Add(serverInfo);
  117. }
  118. zoneScene.GetComponent<ServerInfosComponent>().SetRecentlyServerInfo(a2C_GetServerInfos.RecentId);
  119. await ETTask.CompletedTask;
  120. return ErrorCode.ERR_Success;
  121. }
  122. public static async ETTask<int> GetRoles(Scene zoneScene)
  123. {
  124. A2C_GetRoles a2C_GetRoles = null;
  125. try
  126. {
  127. a2C_GetRoles = (A2C_GetRoles)await zoneScene.GetComponent<SessionComponent>().Session.Call(new C2A_GetRoles()
  128. {
  129. AccountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId,
  130. Token = zoneScene.GetComponent<AccountInfoComponent>().Token,
  131. ServerId = zoneScene.GetComponent<ServerInfosComponent>().CurrentServerId,
  132. });
  133. }
  134. catch (Exception e)
  135. {
  136. Log.Error(e.ToString());
  137. return ErrorCode.ERR_NetWorkError;
  138. }
  139. if (a2C_GetRoles.Error != ErrorCode.ERR_Success)
  140. {
  141. Log.Error(a2C_GetRoles.Error.ToString());
  142. return a2C_GetRoles.Error;
  143. }
  144. zoneScene.GetComponent<RoleInfosComponent>().RoleInfos.Clear();
  145. foreach (var roleInfoProto in a2C_GetRoles.RoleInfos)
  146. {
  147. RoleInfo roleInfo = zoneScene.GetComponent<RoleInfosComponent>().AddChild<RoleInfo>();
  148. roleInfo.FromMessage(roleInfoProto);
  149. zoneScene.GetComponent<RoleInfosComponent>().RoleInfos.Add(roleInfo);
  150. }
  151. return ErrorCode.ERR_Success;
  152. }
  153. public static async ETTask<int> CreateRole(Scene zoneScene, string name)
  154. {
  155. A2C_CreateRole a2C_CreateRole = null;
  156. try
  157. {
  158. a2C_CreateRole = (A2C_CreateRole)await zoneScene.GetComponent<SessionComponent>().Session.Call(new C2A_CreateRole
  159. {
  160. AccountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId,
  161. Token = zoneScene.GetComponent<AccountInfoComponent>().Token,
  162. Name = name,
  163. ServerId = zoneScene.GetComponent<ServerInfosComponent>().CurrentServerId,
  164. });
  165. }
  166. catch (Exception e)
  167. {
  168. Log.Error(e.ToString());
  169. return ErrorCode.ERR_NetWorkError;
  170. }
  171. if (a2C_CreateRole.Error != ErrorCode.ERR_Success)
  172. {
  173. Log.Error(a2C_CreateRole.Error.ToString());
  174. return a2C_CreateRole.Error;
  175. }
  176. RoleInfo newRoleInfo = zoneScene.GetComponent<RoleInfosComponent>().AddChild<RoleInfo>();
  177. newRoleInfo.FromMessage(a2C_CreateRole.RoleInfo);
  178. zoneScene.GetComponent<RoleInfosComponent>().RoleInfos.Add(newRoleInfo);
  179. return ErrorCode.ERR_Success;
  180. }
  181. public static async ETTask<int> DeleteRole(Scene zoneScene, long roleId)
  182. {
  183. A2C_DeleteRole a2C_DeleteRole = null;
  184. try
  185. {
  186. a2C_DeleteRole = (A2C_DeleteRole)await zoneScene.GetComponent<SessionComponent>().Session.Call(new C2A_DeleteRole()
  187. {
  188. AccountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId,
  189. Token = zoneScene.GetComponent<AccountInfoComponent>().Token,
  190. RoleInfoId = roleId,
  191. ServerId = zoneScene.GetComponent<ServerInfosComponent>().CurrentServerId,
  192. });
  193. }
  194. catch (Exception e)
  195. {
  196. Log.Error(e.ToString());
  197. return ErrorCode.ERR_NetWorkError;
  198. }
  199. if (a2C_DeleteRole.Error != ErrorCode.ERR_Success)
  200. {
  201. Log.Error(a2C_DeleteRole.Error.ToString());
  202. return a2C_DeleteRole.Error;
  203. }
  204. int index = zoneScene.GetComponent<RoleInfosComponent>().RoleInfos.FindIndex((info) => { return info.Id == a2C_DeleteRole.DeleteRoleInfoId; });
  205. zoneScene.GetComponent<RoleInfosComponent>().RoleInfos.RemoveAt(index);
  206. await ETTask.CompletedTask;
  207. return ErrorCode.ERR_Success;
  208. }
  209. public static async ETTask<int> GetRealmKey(Scene zoneScene)
  210. {
  211. A2C_GetRealmKey a2C_GetRealmKey = null;
  212. try
  213. {
  214. a2C_GetRealmKey = (A2C_GetRealmKey)await zoneScene.GetComponent<SessionComponent>().Session.Call(new C2A_GetRealmKey()
  215. {
  216. AccountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId,
  217. Token = zoneScene.GetComponent<AccountInfoComponent>().Token,
  218. ServerId = zoneScene.GetComponent<ServerInfosComponent>().CurrentServerId,
  219. });
  220. }
  221. catch (Exception e)
  222. {
  223. Log.Error(e.ToString());
  224. return ErrorCode.ERR_NetWorkError;
  225. }
  226. if (a2C_GetRealmKey.Error != ErrorCode.ERR_Success)
  227. {
  228. Log.Error(a2C_GetRealmKey.Error.ToString());
  229. return a2C_GetRealmKey.Error;
  230. }
  231. zoneScene.GetComponent<AccountInfoComponent>().RealmKey = a2C_GetRealmKey.RealmKey;
  232. zoneScene.GetComponent<AccountInfoComponent>().RealmAddress = a2C_GetRealmKey.RealmAddress;
  233. zoneScene.GetComponent<SessionComponent>().Session.Dispose();
  234. await ETTask.CompletedTask;
  235. return ErrorCode.ERR_Success;
  236. }
  237. public static async ETTask<int> EnterGame(Scene zoneScene)
  238. {
  239. string realmAddress = zoneScene.GetComponent<AccountInfoComponent>().RealmAddress;
  240. //1.连接Realm,获取分配的Gate
  241. R2C_LoginRealm r2C_LoginRealm = null;
  242. Session session = zoneScene.GetComponent<NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(realmAddress));
  243. try
  244. {
  245. r2C_LoginRealm = (R2C_LoginRealm)await session.Call(new C2R_LoginRealm()
  246. {
  247. AccountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId,
  248. RealmTokenKey = zoneScene.GetComponent<AccountInfoComponent>().RealmKey
  249. });
  250. }
  251. catch (Exception e)
  252. {
  253. Log.Error(e);
  254. session?.Dispose();
  255. return ErrorCode.ERR_NetWorkError;
  256. }
  257. session?.Dispose();
  258. if (r2C_LoginRealm.Error != ErrorCode.ERR_Success)
  259. {
  260. return r2C_LoginRealm.Error;
  261. }
  262. Log.Warning($"GateAddress : {r2C_LoginRealm.GateAddress}");
  263. Session gateSession = zoneScene.GetComponent<NetKcpComponent>().Create(NetworkHelper.ToIPEndPoint(r2C_LoginRealm.GateAddress));
  264. gateSession.AddComponent<PingComponent>();
  265. gateSession.AddComponent<DisConnectedCompnent>();
  266. zoneScene.GetComponent<SessionComponent>().Session = gateSession;
  267. //2.开始连接Gate
  268. long currentRoleId = zoneScene.GetComponent<RoleInfosComponent>().CurrentRoleId;
  269. G2C_LoginGate g2C_LoginGate = null;
  270. try
  271. {
  272. long accountId = zoneScene.GetComponent<AccountInfoComponent>().AccountId;
  273. g2C_LoginGate = (G2C_LoginGate)await gateSession.Call(new C2G_LoginGate()
  274. {
  275. Key = r2C_LoginRealm.GateSessionKey,
  276. AccountId = accountId,
  277. RoleId = currentRoleId
  278. });
  279. }
  280. catch (Exception e)
  281. {
  282. Log.Error(e);
  283. zoneScene.GetComponent<SessionComponent>().Session?.Dispose();
  284. return ErrorCode.ERR_NetWorkError;
  285. }
  286. if (g2C_LoginGate.Error != ErrorCode.ERR_Success)
  287. {
  288. zoneScene.GetComponent<SessionComponent>().Session?.Dispose();
  289. return g2C_LoginGate.Error;
  290. }
  291. Log.Debug("登陆gate成功!");
  292. //3.角色正式请求进入游戏逻辑服
  293. G2C_EnterGame g2C_EnterGame = null;
  294. try
  295. {
  296. g2C_EnterGame = (G2C_EnterGame)await gateSession.Call(new C2G_EnterGame() { });
  297. }
  298. catch (Exception e)
  299. {
  300. Log.Error(e);
  301. zoneScene.GetComponent<SessionComponent>().Session.Dispose();
  302. return ErrorCode.ERR_NetWorkError;
  303. }
  304. if (g2C_EnterGame.Error != ErrorCode.ERR_Success)
  305. {
  306. Log.Error(g2C_EnterGame.Error.ToString());
  307. return g2C_EnterGame.Error;
  308. }
  309. Log.Debug("角色进入游戏成功!");
  310. zoneScene.GetComponent<PlayerComponent>().MyId = g2C_EnterGame.MyId;
  311. await zoneScene.GetComponent<ObjectWait>().Wait<WaitType.Wait_SceneChangeFinish>();
  312. await ETTask.CompletedTask;
  313. return ErrorCode.ERR_Success;
  314. }
  315. }
  316. }