Просмотр исходного кода

1.修改了几个类的名字
2.去除了opcode定义,使用MessageAttribute定义opcode

tanghai 12 лет назад
Родитель
Сommit
0594329cae

+ 8 - 2
CSharp/Game/Component/Component.csproj

@@ -42,19 +42,21 @@
     <Compile Include="Buff.cs" />
     <Compile Include="BuffManager.cs" />
     <Compile Include="ConfigAttribute.cs" />
+    <Compile Include="ConfigCategory.cs" />
     <Compile Include="ConfigManager.cs" />
     <Compile Include="EventAttribute.cs" />
-    <Compile Include="EventDefine.cs" />
+    <Compile Include="EventType.cs" />
     <Compile Include="HandlerAttribute.cs" />
     <Compile Include="IConfigInitialize.cs" />
     <Compile Include="IEvent.cs" />
     <Compile Include="IHandler.cs" />
     <Compile Include="ILogic.cs" />
     <Compile Include="IType.cs" />
+    <Compile Include="LogicManager.cs" />
     <Compile Include="Message.cs" />
+    <Compile Include="MessageAttribute.cs" />
     <Compile Include="MessageEnv.cs" />
     <Compile Include="Object.cs" />
-    <Compile Include="OpcodeDefine.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
@@ -64,6 +66,10 @@
       <Project>{24233cd5-a5df-484b-a482-b79cb7a0d9cb}</Project>
       <Name>Helper</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\Platform\Log\Log.csproj">
+      <Project>{72e16572-fc1f-4a9e-bc96-035417239298}</Project>
+      <Name>Log</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />

+ 58 - 0
CSharp/Game/Component/ConfigCategory.cs

@@ -0,0 +1,58 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using Helper;
+
+namespace Component
+{
+	public abstract class ConfigCategory<T> : ISupportInitialize, IConfigInitialize where T : IType
+	{
+		protected readonly Dictionary<int, T> dict = new Dictionary<int, T>();
+
+		public T this[int type]
+		{
+			get
+			{
+				return dict[type];
+			}
+		}
+
+		public string ConfigName
+		{
+			get
+			{
+				return typeof (T).Name;
+			}
+		}
+
+		public Dictionary<int, T> GetAll()
+		{
+			return this.dict;
+		}
+
+		public void Init(string configsDir)
+		{
+			string path = Path.Combine(configsDir, this.ConfigName);
+
+			if (!Directory.Exists(path))
+			{
+				throw new Exception(string.Format("not found config path: {0}", path));
+			}
+
+			foreach (var file in Directory.GetFiles(path))
+			{
+				var t = MongoHelper.FromJson<T>(File.ReadAllText(file));
+				this.dict.Add(t.Type, t);
+			}
+		}
+
+		public void BeginInit()
+		{
+		}
+
+		public void EndInit()
+		{
+		}
+	}
+}

+ 63 - 26
CSharp/Game/Component/ConfigManager.cs

@@ -2,57 +2,94 @@
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.IO;
-using Helper;
 
 namespace Component
 {
-	public abstract class ConfigManager<T> : ISupportInitialize, IConfigInitialize where T : IType
+	public class ConfigManager
 	{
-		protected readonly Dictionary<int, T> dict = new Dictionary<int, T>();
+		private static readonly ConfigManager instance = new ConfigManager();
 
-		public T this[int type]
+		public static ConfigManager Instance
 		{
 			get
 			{
-				return dict[type];
+				return instance;
 			}
 		}
 
-		public string ConfigName
-		{
-			get
-			{
-				return typeof (T).Name;
-			}
-		}
+		public Dictionary<string, object> allConfig;
 
-		public Dictionary<int, T> GetAll()
+		private ConfigManager()
 		{
-			return this.dict;
+			this.Load();
 		}
 
-		public void Init(string configsDir)
+		private void Load()
 		{
-			string path = Path.Combine(configsDir, this.ConfigName);
-
-			if (!Directory.Exists(path))
+			this.allConfig = new Dictionary<string, object>();
+			string currentDir = AppDomain.CurrentDomain.BaseDirectory;
+			Type[] types = typeof(ConfigManager).Assembly.GetTypes();
+			foreach (var type in types)
 			{
-				throw new Exception(string.Format("not found config path: {0}", path));
-			}
+				object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
 
-			foreach (var file in Directory.GetFiles(path))
-			{
-				var t = MongoHelper.FromJson<T>(File.ReadAllText(file));
-				this.dict.Add(t.Type, t);
+				object obj = (Activator.CreateInstance(type));
+
+				var iInit = obj as IConfigInitialize;
+				if (iInit == null)
+				{
+					throw new Exception(string.Format("class {0} is not IConfigInitialize", type.Name));
+				}
+
+				var iSupportInitialize = obj as ISupportInitialize;
+				if (iSupportInitialize != null)
+				{
+					iSupportInitialize.BeginInit();
+				}
+
+				string configDir = Path.Combine(
+					currentDir, ((ConfigAttribute)attrs[0]).RelativeDirectory);
+
+				if (!Directory.Exists(configDir))
+				{
+					throw new Exception(string.Format("not found config dir: {0}", configDir));
+				}
+				iInit.Init(configDir);
+
+
+				if (iSupportInitialize != null)
+				{
+					iSupportInitialize.EndInit();
+				}
+
+				allConfig[iInit.ConfigName] = obj;
 			}
 		}
 
-		public void BeginInit()
+		public void Reload()
+		{
+			this.Load();
+		}
+ 
+		public T Get<T>(int type) where T : IType
+		{
+			var configManager = (ConfigCategory<T>)allConfig[typeof (T).Name];
+			return configManager[type];
+		}
+
+		public Dictionary<int, T> GetAll<T>() where T : IType
 		{
+			var configManager = (ConfigCategory<T>)allConfig[typeof (T).Name];
+			return configManager.GetAll();
 		}
 
-		public void EndInit()
+		public ConfigCategory<T> GetConfigManager<T>() where T : IType
 		{
+			return (ConfigCategory<T>)allConfig[typeof(T).Name];
 		}
 	}
 }

+ 1 - 1
CSharp/Game/Component/EventAttribute.cs

@@ -6,6 +6,6 @@ namespace Component
 	public class EventAttribute : Attribute
 	{
 		public EventType Type { get; set; }
-		public EventNumber Number { get; set; }
+		public short Number { get; set; }
 	}
 }

+ 0 - 19
CSharp/Game/Component/EventDefine.cs

@@ -1,19 +0,0 @@
-
-namespace Component
-{
-	// 定义事件类型
-	public enum EventType: short
-	{
-		// 登录world前触发
-		LoginWorldBeforeEvent,
-	}
-
-	public enum EventNumber: short
-	{
-		#region    LoginWorldBeforeEvent
-
-		CheckPlayerEvent,
-
-		#endregion LoginWorldBeforeEvent
-	}
-}

+ 13 - 0
CSharp/Game/Component/EventType.cs

@@ -0,0 +1,13 @@
+
+namespace Component
+{
+	// 定义事件类型
+	public enum EventType: short
+	{
+		// 登录world前触发
+		BeforeLoginWorldEvent,
+
+		// 使用物品前触发
+		BeforeUseItemEvent,
+	}
+}

+ 1 - 1
CSharp/Game/Component/HandlerAttribute.cs

@@ -5,6 +5,6 @@ namespace Component
 	[AttributeUsage(AttributeTargets.Class)]
 	public class HandlerAttribute : Attribute
 	{
-		public Opcode Opcode { get; set; }
+		public short Opcode { get; set; }
 	}
 }

+ 2 - 1
CSharp/Game/Component/IHandler.cs

@@ -1,4 +1,5 @@
-namespace Component
+
+namespace Component
 {
 	public interface IHandler
 	{

+ 1 - 1
CSharp/Game/Component/ILogic.cs

@@ -3,6 +3,6 @@ namespace Component
 {
 	public interface ILogic
 	{
-		void Handle(Opcode opcode, byte[] content);
+		void Handle(short opcode, byte[] content);
 	}
 }

+ 13 - 14
CSharp/Game/World/Logic.cs → CSharp/Game/Component/LogicManager.cs

@@ -1,21 +1,20 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
-using Component;
 using Helper;
 using Log;
 
-namespace World
+namespace Component
 {
-	public class Logic : ILogic
+	public class LogicManager : ILogic
     {
-		private static readonly Logic instance = new Logic();
+		private static readonly LogicManager instance = new LogicManager();
 
-		private Dictionary<Opcode, IHandler> handlers;
+		private Dictionary<short, IHandler> handlers;
 
-		private Dictionary<EventType, SortedDictionary<EventNumber, IEvent>> events;
+		private Dictionary<EventType, SortedDictionary<short, IEvent>> events;
 
-		public static Logic Instance
+		public static LogicManager Instance
 		{
 			get
 			{
@@ -23,7 +22,7 @@ namespace World
 			}
 		}
 
-		private Logic()
+		private LogicManager()
 		{
 			this.Load();
 		}
@@ -35,7 +34,7 @@ namespace World
 			Type[] types = assembly.GetTypes();
 
 			// 加载封包处理器
-			this.handlers = new Dictionary<Opcode, IHandler>();
+			this.handlers = new Dictionary<short, IHandler>();
 			foreach (var type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(HandlerAttribute), false);
@@ -44,7 +43,7 @@ namespace World
 					continue;
 				}
 				var handler = (IHandler)Activator.CreateInstance(type);
-				Opcode opcode = ((HandlerAttribute)attrs[0]).Opcode;
+				short opcode = ((HandlerAttribute)attrs[0]).Opcode;
 				if (this.handlers.ContainsKey(opcode))
 				{
 					throw new Exception(string.Format(
@@ -54,7 +53,7 @@ namespace World
 			}
 
 			// 加载事件处理器
-			this.events = new Dictionary<EventType, SortedDictionary<EventNumber, IEvent>>();
+			this.events = new Dictionary<EventType, SortedDictionary<short, IEvent>>();
 			foreach (var type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
@@ -67,7 +66,7 @@ namespace World
 				var eventNumber = ((EventAttribute)attrs[0]).Number;
 				if (!this.events.ContainsKey(eventType))
 				{
-					this.events[eventType] = new SortedDictionary<EventNumber, IEvent>();
+					this.events[eventType] = new SortedDictionary<short, IEvent>();
 				}
 				if (this.events[eventType].ContainsKey(eventNumber))
 				{
@@ -84,7 +83,7 @@ namespace World
 			this.Load();
 		}
 
-		public void Handle(Opcode opcode, byte[] content)
+		public void Handle(short opcode, byte[] content)
 	    {
 		    IHandler handler = null;
 			if (!handlers.TryGetValue(opcode, out handler))
@@ -105,7 +104,7 @@ namespace World
 
 		public void Trigger(MessageEnv messageEnv, EventType type)
 		{
-			SortedDictionary<EventNumber, IEvent> iEventDict = null;
+			SortedDictionary<short, IEvent> iEventDict = null;
 			if (!this.events.TryGetValue(type, out iEventDict))
 			{
 				return;

+ 2 - 0
CSharp/Game/Component/Message.cs

@@ -1,11 +1,13 @@
 
 namespace Component
 {
+	[MessageAttribute(Opcode = 1)]
 	public class CChat
 	{
 		public string Content { get; set; }
 	}
 
+	[MessageAttribute(Opcode = 2)]
 	public class CLoginWorld
 	{
 	}

+ 10 - 0
CSharp/Game/Component/MessageAttribute.cs

@@ -0,0 +1,10 @@
+using System;
+
+namespace Component
+{
+	[AttributeUsage(AttributeTargets.Class)]
+	public class MessageAttribute : Attribute
+	{
+		public short Opcode { get; set; }
+	}
+}

+ 0 - 11
CSharp/Game/Component/OpcodeDefine.cs

@@ -1,11 +0,0 @@
-
-namespace Component
-{
-	public enum Opcode: short
-	{
-		ReloadHandler = 1,
-		ReloadConfig  = 2,
-		Chat          = 3,
-		LoginWorld    = 4,
-	}
-}

+ 1 - 1
CSharp/Game/Logic/Event/LoginWorldBeforeEvent.cs → CSharp/Game/Logic/Event/BeforeLoginWorldEvent.cs

@@ -3,7 +3,7 @@ using Log;
 
 namespace Logic
 {
-	[EventAttribute(Type = EventType.LoginWorldBeforeEvent, Number = EventNumber.CheckPlayerEvent)]
+	[EventAttribute(Type = EventType.BeforeLoginWorldEvent, Number = 1)]
 	public class CheckPlayerEvent : IEvent
 	{
 		public void Trigger(MessageEnv messageEnv)

+ 14 - 0
CSharp/Game/Logic/Event/BeforeUseItemEvent.cs

@@ -0,0 +1,14 @@
+using Component;
+using Log;
+
+namespace Logic
+{
+	[EventAttribute(Type = EventType.BeforeUseItemEvent, Number = 1)]
+	public class UseCountStatisticsEvent : IEvent
+	{
+		public void Trigger(MessageEnv messageEnv)
+		{
+			Logger.Trace("check player");
+		}
+	}
+}

+ 2 - 2
CSharp/Game/Logic/Handler/ChatHandler.cs

@@ -5,7 +5,7 @@ using Component;
 
 namespace Logic
 {
-	[HandlerAttribute(Opcode = Opcode.Chat)]
+	[HandlerAttribute(Opcode = 1)]
 	class ChatHandler: IHandler
 	{
 		public void Handle(MessageEnv messageEnv, byte[] content)
@@ -13,7 +13,7 @@ namespace Logic
 			var chat = MongoHelper.FromBson<CChat>(content);
 
 			var world = World.World.Instance;
-			var globalConfig = world.Config.Get<GlobalConfig>(1);
+			var globalConfig = world.ConfigManager.Get<GlobalConfig>(1);
 			Logger.Debug(MongoHelper.ToJson(globalConfig));
 			Logger.Debug("chat content: {0}", chat.Content);
 		}

+ 2 - 2
CSharp/Game/Logic/Handler/LoginWorldHandler.cs

@@ -2,14 +2,14 @@
 
 namespace Logic.Handler
 {
-	[Handler(Opcode = Opcode.LoginWorld)]
+	[Handler(Opcode = 2)]
 	public class LoginWorldHandler: IHandler
 	{
 		public void Handle(MessageEnv messageEnv, byte[] content)
 		{
 			var world = World.World.Instance;
 			// 登录world前触发事件
-			world.Logic.Trigger(messageEnv, EventType.LoginWorldBeforeEvent);
+			world.LogicManager.Trigger(messageEnv, EventType.BeforeLoginWorldEvent);
 		}
 	}
 }

+ 4 - 4
CSharp/Game/Logic/Handler/ReloadHandler.cs

@@ -2,23 +2,23 @@
 
 namespace Logic
 {
-	[HandlerAttribute(Opcode = Opcode.ReloadHandler)]
+	[HandlerAttribute(Opcode = 3)]
 	class ReloadHandlerHandler : IHandler
 	{
 		public void Handle(MessageEnv messageEnv, byte[] content)
 		{
 			var world = World.World.Instance;
-			world.Logic.Reload();
+			world.LogicManager.Reload();
 		}
 	}
 
-	[HandlerAttribute(Opcode = Opcode.ReloadConfig)]
+	[HandlerAttribute(Opcode = 4)]
 	class ReloadConfigHandler: IHandler
 	{
 		public void Handle(MessageEnv messageEnv, byte[] content)
 		{
 			var world = World.World.Instance;
-			world.Config.Reload();
+			world.ConfigManager.Reload();
 		}
 	}
 }

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

@@ -34,7 +34,8 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Event\LoginWorldBeforeEvent.cs" />
+    <Compile Include="Event\BeforeUseItemEvent.cs" />
+    <Compile Include="Event\BeforeLoginWorldEvent.cs" />
     <Compile Include="Handler\ChatHandler.cs" />
     <Compile Include="Handler\LoginWorldHandler.cs" />
     <Compile Include="Handler\ReloadHandler.cs" />

+ 0 - 96
CSharp/Game/World/Config.cs

@@ -1,96 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.IO;
-using Component;
-
-namespace World
-{
-	public class Config
-	{
-		private static readonly Config instance = new Config();
-
-		public static Config Instance
-		{
-			get
-			{
-				return instance;
-			}
-		}
-
-		public Dictionary<string, object> allConfig;
-
-		private Config()
-		{
-			this.Load();
-		}
-
-		private void Load()
-		{
-			this.allConfig = new Dictionary<string, object>();
-			string currentDir = AppDomain.CurrentDomain.BaseDirectory;
-			Type[] types = typeof(Config).Assembly.GetTypes();
-			foreach (var type in types)
-			{
-				object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
-				if (attrs.Length == 0)
-				{
-					continue;
-				}
-
-				object obj = (Activator.CreateInstance(type));
-
-				var iInit = obj as IConfigInitialize;
-				if (iInit == null)
-				{
-					throw new Exception(string.Format("class {0} is not IConfigInitialize", type.Name));
-				}
-
-				var iSupportInitialize = obj as ISupportInitialize;
-				if (iSupportInitialize != null)
-				{
-					iSupportInitialize.BeginInit();
-				}
-
-				string configDir = Path.Combine(
-					currentDir, ((ConfigAttribute)attrs[0]).RelativeDirectory);
-
-				if (!Directory.Exists(configDir))
-				{
-					throw new Exception(string.Format("not found config dir: {0}", configDir));
-				}
-				iInit.Init(configDir);
-
-
-				if (iSupportInitialize != null)
-				{
-					iSupportInitialize.EndInit();
-				}
-
-				allConfig[iInit.ConfigName] = obj;
-			}
-		}
-
-		public void Reload()
-		{
-			this.Load();
-		}
- 
-		public T Get<T>(int type) where T : IType
-		{
-			var configManager = (ConfigManager<T>)allConfig[typeof (T).Name];
-			return configManager[type];
-		}
-
-		public Dictionary<int, T> GetAll<T>() where T : IType
-		{
-			var configManager = (ConfigManager<T>)allConfig[typeof (T).Name];
-			return configManager.GetAll();
-		}
-
-		public ConfigManager<T> GetConfigManager<T>() where T : IType
-		{
-			return (ConfigManager<T>)allConfig[typeof(T).Name];
-		}
-	}
-}

+ 1 - 1
CSharp/Game/World/Config/GlobalConfig.cs

@@ -7,7 +7,7 @@ namespace Component.Config
 	}
 
 	[ConfigAttribute]
-	public class GlobalManager: ConfigManager<GlobalConfig>
+	public class GlobalCategory: ConfigCategory<GlobalConfig>
 	{
 	}
 }

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

@@ -1,13 +1,15 @@
 
+using Component;
+
 namespace World
 {
 	public class World
 	{
 		private static readonly World instance = new World();
 
-		private readonly Logic logic = Logic.Instance;
+		private readonly LogicManager logicManager = LogicManager.Instance;
 
-		private readonly Config config = Config.Instance;
+		private readonly ConfigManager configManager = ConfigManager.Instance;
 
 		public static World Instance
 		{
@@ -17,19 +19,19 @@ namespace World
 			}
 		}
 
-		public Logic Logic
+		public LogicManager LogicManager
 		{
 			get
 			{
-				return this.logic;
+				return this.logicManager;
 			}
 		}
 
-		public Config Config
+		public ConfigManager ConfigManager
 		{
 			get
 			{
-				return this.config;
+				return this.configManager;
 			}
 		}
 	}

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

@@ -36,9 +36,7 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Config.cs" />
     <Compile Include="Config\GlobalConfig.cs" />
-    <Compile Include="Logic.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="World.cs" />
   </ItemGroup>

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

@@ -12,13 +12,13 @@ namespace WorldTest
 		public void TestReload()
 		{
 			var world = World.World.Instance;
-			world.Logic.Handle(Opcode.LoginWorld, MongoHelper.ToBson(new CLoginWorld()));
+			world.LogicManager.Handle(2, MongoHelper.ToBson(new CLoginWorld()));
 			int count = 2;
 			while (--count != 0)
 			{
-				world.Logic.Handle(Opcode.ReloadHandler, "tanghai".ToByteArray());
+				world.LogicManager.Handle(3, "tanghai".ToByteArray());
 				Thread.Sleep(1);
-				world.Logic.Reload();
+				world.LogicManager.Reload();
 			}
 		}
 	}

+ 7 - 1
CSharp/Platform/Helper/MongoHelper.cs

@@ -1,4 +1,5 @@
-using MongoDB.Bson;
+using System;
+using MongoDB.Bson;
 using MongoDB.Bson.IO;
 using MongoDB.Bson.Serialization;
 
@@ -30,5 +31,10 @@ namespace Helper
 		{
 			return BsonSerializer.Deserialize<T>(bytes);
 		}
+
+		public static object FromBson(byte[] bytes, Type type)
+		{
+			return BsonSerializer.Deserialize(bytes, type);
+		}
 	}
 }