Procházet zdrojové kódy

ILRuntime支持了await,去掉Session回调形式调用

tanghai před 8 roky
rodič
revize
30550b5ec0

+ 0 - 23
Unity/Assets/Scripts/Entity/Session.cs

@@ -153,29 +153,6 @@ namespace Model
 
 			this.network.MessageDispatcher.Dispatch(this, op, offset, messageBytes, (AMessage)message);
 		}
-		
-		/// <summary>
-		/// Rpc调用
-		/// </summary>
-		public void CallWithAction(ARequest request, Action<AResponse> action)
-		{
-			request.RpcId = ++RpcId;
-
-			this.requestCallback[request.RpcId] = (message) =>
-			{
-				try
-				{
-					AResponse response = (AResponse)message;
-					action(response);
-				}
-				catch (Exception e)
-				{
-					Log.Error(e.ToString());
-				}
-			};
-			
-			this.SendMessage(request);
-		}
 
 		/// <summary>
 		/// Rpc调用,发送一个消息,等待返回一个消息

+ 1 - 1
Unity/Assets/Scripts/Init.cs

@@ -62,7 +62,7 @@ namespace Model
 
 				this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
 				Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
-				ObjectEvents.Instance.LoadHotfixDll();
+				EventSystem.Instance.LoadHotfixDll();
 				Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
 				ILHelper.InitILRuntime();
 				

+ 9 - 0
Unity/Assets/ThirdParty/ILRuntime/Editor.meta

@@ -0,0 +1,9 @@
+fileFormatVersion: 2
+guid: d583df3fc92b1f24cbeada2e709eb8cb
+folderAsset: yes
+timeCreated: 1516269017
+licenseType: Free
+DefaultImporter:
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 7 - 4
Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs

@@ -49,7 +49,8 @@ namespace Hotfix
 			try
 			{
 				// 向actor发起一次rpc调用
-				Actor_TestResponse response = await SessionComponent.Instance.Session.Call<Actor_TestResponse>(new Actor_TestRequest() { request = "request actor test rpc" });
+				Actor_TestResponse response = (Actor_TestResponse) await SessionComponent.Instance.Session.Call(
+					new Actor_TestRequest() { request = "request actor test rpc" }, true);
 				Log.Info($"recv response: {MongoHelper.ToJson(response)}");
 			}
 			catch (Exception e)
@@ -62,7 +63,8 @@ namespace Hotfix
 		{
 			try
 			{
-				Actor_TransferResponse response = await SessionComponent.Instance.Session.Call<Actor_TransferResponse>(new Actor_TransferRequest() {MapIndex = 0});
+				Actor_TransferResponse response = (Actor_TransferResponse) await SessionComponent.Instance.Session.Call(
+					new Actor_TransferRequest() {MapIndex = 0}, true);
 				Log.Info($"传送成功! {MongoHelper.ToJson(response)}");
 			}
 			catch (Exception e)
@@ -73,7 +75,8 @@ namespace Hotfix
 
 		private async void OnTransfer2()
 		{
-			Actor_TransferResponse response = await SessionComponent.Instance.Session.Call<Actor_TransferResponse>(new Actor_TransferRequest() { MapIndex = 1 });
+			Actor_TransferResponse response = (Actor_TransferResponse)await SessionComponent.Instance.Session.Call(
+				new Actor_TransferRequest() { MapIndex = 1 }, true);
 			Log.Info($"传送成功! {MongoHelper.ToJson(response)}");
 		}
 
@@ -81,7 +84,7 @@ namespace Hotfix
 		{
 			try
 			{
-				G2C_EnterMap g2CEnterMap = await SessionComponent.Instance.Session.Call<G2C_EnterMap>(new C2G_EnterMap());
+				G2C_EnterMap g2CEnterMap = (G2C_EnterMap)await SessionComponent.Instance.Session.Call(new C2G_EnterMap(), true);
 				Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILobby);
 			}
 			catch (Exception e)

+ 29 - 41
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -1,4 +1,5 @@
-using System.Net;
+using System;
+using System.Net;
 using Model;
 using UnityEngine;
 using UnityEngine.UI;
@@ -24,58 +25,45 @@ namespace Hotfix
 			ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
 			loginBtn = rc.Get<GameObject>("LoginBtn");
 			loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
-
 			this.account = rc.Get<GameObject>("Account");
 		}
 
-		private void OnLogin()
-		{
-			Session session = null;
-			IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
-			session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
-			string text = this.account.GetComponent<InputField>().text;
-			session.CallWithAction(new C2R_Login() { Account = text, Password = "111111" }, (response) => LoginOK(session, response));
-		}
-
-		private void LoginOK(Session loginSession, AResponse response)
+		public async void OnLogin()
 		{
-			loginSession.Dispose();
-
-			R2C_Login r2CLogin = (R2C_Login) response;
-			if (r2CLogin.Error != ErrorCode.ERR_Success)
+			try
 			{
-				Log.Error(r2CLogin.Error.ToString());
-				return;
-			}
+				IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
+				Session session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
+				string text = this.account.GetComponent<InputField>().text;
 
-			IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
-			Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
-			Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
 
-			SessionComponent.Instance.Session.CallWithAction(new C2G_LoginGate() { Key = r2CLogin.Key },
-				(response2)=>LoginGateOk(response2)
-			);
+				R2C_Login r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = text, Password = "111111" }, true);
+				if (r2CLogin.Error != ErrorCode.ERR_Success)
+				{
+					Log.Error(r2CLogin.Error.ToString());
+					return;
+				}
 
-		}
+				connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
+				Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
+				Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
 
-		private void LoginGateOk(AResponse response)
-		{
-			G2C_LoginGate g2CLoginGate = (G2C_LoginGate) response;
-			if (g2CLoginGate.Error != ErrorCode.ERR_Success)
-			{
-				Log.Error(g2CLoginGate.Error.ToString());
-				return;
-			}
+				G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key }, true);
 
-			Log.Info("登陆gate成功!");
+				Log.Info("登陆gate成功!");
 
-			// 创建Player
-			Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
-			PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
-			playerComponent.MyPlayer = player;
+				// 创建Player
+				Player player = Model.EntityFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
+				PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
+				playerComponent.MyPlayer = player;
 
-			Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
-			Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
+				Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.UILobby);
+				Hotfix.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
+			}
+			catch (Exception e)
+			{
+				Log.Error(e.ToString());
+			}
 		}
 	}
 }

+ 3 - 3
Unity/Unity.Editor.Plugins.csproj

@@ -142,6 +142,9 @@
     <Reference Include="ICSharpCode.SharpZipLib">
       <HintPath>Assets/Plugins/ICSharpCode.SharpZipLib.dll</HintPath>
     </Reference>
+    <Reference Include="Newtonsoft.Json">
+      <HintPath>Assets/Plugins/Newtonsoft.Json.dll</HintPath>
+    </Reference>
     <Reference Include="NPOI">
       <HintPath>Assets/Plugins/Editor/npoi/NPOI.dll</HintPath>
     </Reference>
@@ -154,9 +157,6 @@
     <Reference Include="NPOI.OpenXmlFormats">
       <HintPath>Assets/Plugins/Editor/npoi/NPOI.OpenXmlFormats.dll</HintPath>
     </Reference>
-    <Reference Include="Newtonsoft.Json">
-      <HintPath>Assets/Plugins/Newtonsoft.Json.dll</HintPath>
-    </Reference>
     <Reference Include="UnityEditor.iOS.Extensions.Xcode">
       <HintPath>C:/Apps/Unity/Editor/Data/PlaybackEngines/iOSSupport\UnityEditor.iOS.Extensions.Xcode.dll</HintPath>
     </Reference>