Browse Source

LogicEntry类移动到World

tanghai 12 năm trước cách đây
mục cha
commit
6d63e1fdc4

+ 0 - 1
CSharp/Game/Logic/Logic.csproj

@@ -40,7 +40,6 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Handler\ChatHandler.cs" />
-    <Compile Include="LogicEntry.cs" />
     <Compile Include="Handler\ReloadHandler.cs" />
   </ItemGroup>
   <ItemGroup>

+ 0 - 43
CSharp/Game/Logic/LogicEntry.cs

@@ -1,43 +0,0 @@
-using System;
-using System.Collections.Generic;
-using Component;
-
-namespace Logic
-{
-	public class LogicEntry : ILogicEntry
-    {
-	    private readonly Dictionary<short, IHandler> handlers = new Dictionary<short, IHandler>();
-
-		public LogicEntry()
-		{
-			Type[] types = typeof (LogicEntry).Assembly.GetTypes();
-			foreach (var type in types)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof(HandlerAttribute), false);
-				if (attrs.Length == 0)
-				{
-					continue;
-				}
-				var handler = (IHandler)Activator.CreateInstance(type);
-				short opcode = ((HandlerAttribute)attrs[0]).Opcode;
-				if (handlers.ContainsKey(opcode))
-				{
-					throw new Exception(string.Format(
-						"same opcode, opcode: {0}, name: {1}", opcode, type.Name));
-				}
-				handlers[opcode] = handler;
-			}
-		}
-
-	    public void Enter(MessageEnv messageEnv, short opcode, byte[] content)
-	    {
-		    IHandler handler = null;
-			if (!handlers.TryGetValue(opcode, out handler))
-			{
-				throw new Exception(string.Format("not found handler opcode {0}", opcode));
-			}
-
-			handler.Handle(messageEnv, content);
-	    }
-	}
-}

+ 6 - 22
CSharp/Game/World/World.cs

@@ -3,6 +3,7 @@ using System.IO;
 using Component;
 using Helper;
 using Log;
+using Logic;
 
 namespace World
 {
@@ -10,26 +11,9 @@ namespace World
 	{
 		private static readonly World instance = new World();
 
-		private ILogicEntry iLogicEntry;
+		private readonly LogicEntry logicEntry = LogicEntry.Instance;
 
-		private readonly Config config;
-
-		private World()
-		{
-			this.config = Config.Instance;
-			this.LoadLogic();
-		}
-
-		private void LoadLogic()
-		{
-			string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logic.dll");
-			if (!File.Exists(dllPath))
-			{
-				throw new Exception(string.Format("not found logic dll, path: {0}", dllPath));
-			}
-			var assembly = LoaderHelper.Load(dllPath);
-			this.iLogicEntry = (ILogicEntry)assembly.CreateInstance("Logic.LogicEntry");
-		}
+		private readonly Config config = Config.Instance;
 
 		public static World Instance
 		{
@@ -41,7 +25,7 @@ namespace World
 
 		public void ReloadLogic()
 		{
-			this.LoadLogic();
+			this.logicEntry.Reload();
 		}
 
 		public void ReloadConfig()
@@ -49,13 +33,13 @@ namespace World
 			this.config.Reload();
 		}
 
-		public void Dispatcher(short opcode, byte[] content)
+		public void Enter(short opcode, byte[] content)
 		{
 			try
 			{
 				var messageEnv = new MessageEnv();
 				messageEnv.Set(this);
-				this.iLogicEntry.Enter(messageEnv, opcode, content);
+				this.logicEntry.Enter(messageEnv, opcode, content);
 			}
 			catch (Exception e)
 			{

+ 1 - 0
CSharp/Game/World/World.csproj

@@ -52,6 +52,7 @@
   <ItemGroup>
     <Compile Include="Config.cs" />
     <Compile Include="Config\GlobalConfig.cs" />
+    <Compile Include="LogicEntry.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="World.cs" />
   </ItemGroup>

+ 2 - 2
CSharp/Game/WorldTest/WorldTest.cs

@@ -11,11 +11,11 @@ namespace WorldTest
 		public void TestReload()
 		{
 			var world = World.World.Instance;
-			world.Dispatcher(3, "tanghai".ToByteArray());
+			world.Enter(3, "tanghai".ToByteArray());
 			int count = 10;
 			while (--count != 0)
 			{
-				world.Dispatcher(1, "tanghai".ToByteArray());
+				world.Enter(1, "tanghai".ToByteArray());
 				Thread.Sleep(1);
 				world.ReloadLogic();
 			}