Explorar o código

增加EnumHelper.FromString方法

tanghai %!s(int64=10) %!d(string=hai) anos
pai
achega
4acaef0716

+ 2 - 1
CSharp/App/Modules/BehaviorTreeModule/NodeDataEditor.xaml.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Windows;
 using System.Windows.Controls;
+using Common.Helper;
 using Model;
 
 namespace Modules.BehaviorTreeModule
@@ -58,7 +59,7 @@ namespace Modules.BehaviorTreeModule
 				return;
 			}
 			this.TreeNodeViewModel.Type =
-					(int) Enum.Parse(typeof (NodeType), this.cbType.SelectedValue.ToString().Trim());
+					(int) EnumHelper.FromString<NodeType>(this.cbType.SelectedValue.ToString().Trim());
 		}
 	}
 }

+ 1 - 12
CSharp/Game/Model/Component/MessageComponent.cs

@@ -50,7 +50,7 @@ namespace Model
 
 		public void Register<T, R>(Func<T, R> func)
 		{
-			Opcode opcode = (Opcode) Enum.Parse(typeof (Opcode), typeof (T).Name);
+			Opcode opcode = EnumHelper.FromString<Opcode>(typeof (T).Name);
 			events.Add(opcode, messageBytes =>
 			{
 				T t = MongoHelper.FromBson<T>(messageBytes, 6);
@@ -59,17 +59,6 @@ namespace Model
 			});
 		}
 
-		public void RegisterAsync<T, R>(Func<T, Task<R>> func)
-		{
-			Opcode opcode = (Opcode)Enum.Parse(typeof(Opcode), typeof(T).Name);
-			eventsAsync.Add(opcode, async messageBytes =>
-			{
-				T t = MongoHelper.FromBson<T>(messageBytes, 6);
-				R r = await func(t);
-				return MongoHelper.ToBson(r);
-			});
-		}
-
 		public async Task<byte[]> RunAsync(Opcode opcode, byte[] messageBytes)
 		{
 			Func<byte[], byte[]> func = null;

+ 1 - 1
CSharp/Game/Model/Component/NetworkComponent.cs

@@ -169,7 +169,7 @@ namespace Model
 
 			++this.requestId;
 			byte[] requestBuffer = MongoHelper.ToBson(request);
-			Opcode opcode = (Opcode)Enum.Parse(typeof(Opcode), request.GetType().Name);
+			Opcode opcode = EnumHelper.FromString<Opcode>(request.GetType().Name);
 			byte[] opcodeBuffer = BitConverter.GetBytes((ushort)opcode);
 			byte[] idBuffer = BitConverter.GetBytes(this.requestId);
 			channel.SendAsync(new List<byte[]> { opcodeBuffer, idBuffer, requestBuffer });

+ 1 - 13
CSharp/Game/Model/IRegister.cs

@@ -1,6 +1,4 @@
-using System.Threading.Tasks;
-
-namespace Model
+namespace Model
 {
 	public interface IRegister
 	{
@@ -16,14 +14,4 @@ namespace Model
 
 		public abstract R Run(T t);
 	}
-
-	public abstract class MEventAsync<T, R> : IRegister
-	{
-		public void Register()
-		{
-			World.Instance.GetComponent<MessageComponent>().RegisterAsync<T, R>(this.Run);
-		}
-
-		public abstract Task<R> Run(T t);
-	}
 }

+ 1 - 1
CSharp/Game/Model/Opcode.cs

@@ -1,6 +1,6 @@
 namespace Model
 {
-	public enum Opcode: short
+	public enum Opcode: ushort
 	{
 		CMsgLogin = 1,
 		RpcResponse = 30000,

+ 0 - 18
CSharp/Game/Model/OpcodeHelper.cs

@@ -19,23 +19,5 @@
 			}
 			return false;
 		}
-
-		public static bool IsRpcRequestMessage(Opcode opcode)
-		{
-			if ((ushort)opcode > 20000 && (ushort)opcode < 30000)
-			{
-				return true;
-			}
-			return false;
-		}
-
-		public static bool IsRpcResponseMessage(Opcode opcode)
-		{
-			if ((ushort)opcode > 30000 && (ushort)opcode < 40000)
-			{
-				return true;
-			}
-			return false;
-		}
 	}
 }

+ 5 - 0
CSharp/Platform/Common/Helper/EnumHelper.cs

@@ -17,5 +17,10 @@ namespace Common.Helper
 			}
 			return -1;
 		}
+
+		public static T FromString<T>(string str)
+		{
+			return (T) Enum.Parse(typeof (T), str);
+		}
 	}
 }