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

1.ENetException改成EException
2.Game模块增加Config管理类,用来管理Game的配置

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

+ 26 - 0
CSharp/Game/Component/AllConfigManager.cs

@@ -0,0 +1,26 @@
+using System.Collections.Generic;
+
+namespace Component
+{
+	public class AllConfigManager
+	{
+		public readonly Dictionary<string, object> allConfig = new Dictionary<string, object>();
+ 
+		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>(int type) 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];
+		}
+	}
+}

+ 10 - 3
CSharp/Game/Component/BuffManager.cs

@@ -15,6 +15,13 @@ namespace Component
 		[BsonIgnore]
 		public Dictionary<int, Buff> BuffTypeDict { get; private set; }
 
+		public BuffManager()
+		{
+			this.Buffs = new HashSet<Buff>();
+			this.BuffGuidDict = new Dictionary<ObjectId, Buff>();
+			this.BuffTypeDict = new Dictionary<int, Buff>();
+		}
+
 		void ISupportInitialize.BeginInit()
 		{
 		}
@@ -34,17 +41,17 @@ namespace Component
 
 		public bool Add(Buff buff)
 		{
-			if (!this.Buffs.Contains(buff))
+			if (this.Buffs.Contains(buff))
 			{
 				return false;
 			}
 
-			if (!this.BuffGuidDict.ContainsKey(buff.Id))
+			if (this.BuffGuidDict.ContainsKey(buff.Id))
 			{
 				return false;
 			}
 
-			if (!this.BuffTypeDict.ContainsKey(buff.Type))
+			if (this.BuffTypeDict.ContainsKey(buff.Type))
 			{
 				return false;
 			}

+ 9 - 0
CSharp/Game/Component/Component.csproj

@@ -50,14 +50,23 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AllConfigManager.cs" />
     <Compile Include="Buff.cs" />
     <Compile Include="BuffManager.cs" />
+    <Compile Include="ConfigManager.cs" />
+    <Compile Include="IType.cs" />
     <Compile Include="Object.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="Packages.config" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\..\Platform\Helper\Helper.csproj">
+      <Project>{24233cd5-a5df-484b-a482-b79cb7a0d9cb}</Project>
+      <Name>Helper</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 33 - 0
CSharp/Game/Component/ConfigManager.cs

@@ -0,0 +1,33 @@
+using System.Collections.Generic;
+using System.IO;
+using Helper;
+
+namespace Component
+{
+	public class ConfigManager<T> where T : IType
+	{
+		private readonly Dictionary<int, T> dict = new Dictionary<int, T>();
+
+		public virtual void LoadConfig(string dir)
+		{
+			foreach (var file in Directory.GetFiles(dir))
+			{
+				var t = MongoHelper.FromJson<T>(File.ReadAllText(file));
+				this.dict.Add(t.Type, t);
+			}
+		}
+
+		public T this[int type]
+		{
+			get
+			{
+				return dict[type];
+			}
+		}
+
+		public Dictionary<int, T> GetAll()
+		{
+			return this.dict;
+		}
+	}
+}

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

@@ -0,0 +1,10 @@
+namespace Component
+{
+	public interface IType
+	{
+		int Type
+		{
+			get;
+		}
+	}
+}

+ 7 - 9
CSharp/Game/ComponentTest/BuffManagerTest.cs

@@ -1,6 +1,4 @@
-using System;
-using System.Text;
-using System.Collections.Generic;
+using Component;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 
 namespace ComponentTest
@@ -11,10 +9,6 @@ namespace ComponentTest
 	[TestClass]
 	public class BuffManagerTest
 	{
-		public BuffManagerTest()
-		{
-		}
-
 		private TestContext testContextInstance;
 
 		public TestContext TestContext
@@ -30,9 +24,13 @@ namespace ComponentTest
 		}
 
 		[TestMethod]
-		public void TestMethod1()
+		public void TestAdd()
 		{
-
+			var buffManager = new BuffManager();
+			var buff = new Buff { Type = 1 };
+			buffManager.Add(buff);
+			var getBuff = buffManager.Get(buff.Id);
+			Assert.AreSame(buff, getBuff);
 		}
 	}
 }

+ 0 - 3
CSharp/Game/ComponentTest/ObjectTest.cs

@@ -7,9 +7,6 @@ namespace ObjectTest
 {
 	class Player: Object
 	{
-		public Player()
-		{
-		}
 	}
 
 	[TestClass]

+ 25 - 0
CSharp/Platform/ENet/EException.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Runtime.Serialization;
+
+namespace ENet
+{
+	[Serializable]
+	public class EException: Exception
+	{
+		public EException()
+		{
+		}
+
+		public EException(string message): base(message)
+		{
+		}
+
+		public EException(string message, Exception inner): base(message, inner)
+		{
+		}
+
+		protected EException(SerializationInfo info, StreamingContext context)
+		{
+		}
+	}
+}

+ 1 - 1
CSharp/Platform/ENet/ENet.csproj

@@ -38,7 +38,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Address.cs" />
-    <Compile Include="ENetException.cs" />
+    <Compile Include="EException.cs" />
     <Compile Include="Event.cs" />
     <Compile Include="IOService.cs" />
     <Compile Include="Library.cs" />

+ 0 - 25
CSharp/Platform/ENet/ENetException.cs

@@ -1,25 +0,0 @@
-using System;
-using System.Runtime.Serialization;
-
-namespace ENet
-{
-	[Serializable]
-	public class ENetException: Exception
-	{
-		public ENetException()
-		{
-		}
-
-		public ENetException(string message): base(message)
-		{
-		}
-
-		public ENetException(string message, Exception inner): base(message, inner)
-		{
-		}
-
-		protected ENetException(SerializationInfo info, StreamingContext context)
-		{
-		}
-	}
-}

+ 3 - 3
CSharp/Platform/ENet/ESocket.cs

@@ -100,7 +100,7 @@ namespace ENet
 				this.service.HostPtr, ref nativeAddress, channelLimit, data);
 			if (this.peerPtr == IntPtr.Zero)
 			{
-				throw new ENetException("Host connect call failed.");
+				throw new EException("Host connect call failed.");
 			}
 			this.service.PeersManager.Add(this.peerPtr, this);
 			this.ESocketEvent.Connected += e => tcs.TrySetResult(true);
@@ -111,7 +111,7 @@ namespace ENet
 		{
 			if (this.service.PeersManager.ContainsKey(IntPtr.Zero))
 			{
-				throw new ENetException("Do Not Accept Twice!");
+				throw new EException("Do Not Accept Twice!");
 			}
 			var tcs = new TaskCompletionSource<bool>();
 			this.service.PeersManager.Add(this.PeerPtr, this);
@@ -134,7 +134,7 @@ namespace ENet
 			{
 				if (e.EventState == EventState.DISCONNECTED)
 				{
-					tcs.TrySetException(new ENetException("Peer Disconnected In Received"));
+					tcs.TrySetException(new EException("Peer Disconnected In Received"));
 				}
 
 				using (var packet = new Packet(e.PacketPtr))

+ 2 - 2
CSharp/Platform/ENet/IOService.cs

@@ -48,7 +48,7 @@ namespace ENet
 
 			if (this.host == IntPtr.Zero)
 			{
-				throw new ENetException("Host creation call failed.");
+				throw new EException("Host creation call failed.");
 			}
 		}
 
@@ -73,7 +73,7 @@ namespace ENet
 
 			if (this.host == IntPtr.Zero)
 			{
-				throw new ENetException("Host creation call failed.");
+				throw new EException("Host creation call failed.");
 			}
 		}
 

+ 1 - 1
CSharp/Platform/ENet/Library.cs

@@ -9,7 +9,7 @@
 				NativeMethods.ENET_VERSION, ref inits);
 			if (ret < 0)
 			{
-				throw new ENetException(string.Format("Initialization failed, ret: {0}", ret));
+				throw new EException(string.Format("Initialization failed, ret: {0}", ret));
 			}
 		}
 

+ 1 - 1
CSharp/Platform/ENet/Packet.cs

@@ -21,7 +21,7 @@ namespace ENet
 			this.packet = NativeMethods.enet_packet_create(data, (uint) data.Length, flags);
 			if (this.packet == IntPtr.Zero)
 			{
-				throw new ENetException("Packet creation call failed");
+				throw new EException("Packet creation call failed");
 			}
 		}