LoginHelper.cs 15 KB

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