Browse Source

可以world发回的消息了,实现了服务器列表显示指令和reload指令

tanghai 13 năm trước cách đây
mục cha
commit
9c08ad6f8b

+ 4 - 21
CSharp/App/BossClient/BossClient.cs

@@ -12,8 +12,6 @@ namespace BossClient
 		
 		private readonly ClientHost clientHost = new ClientHost();
 
-		private GateSession gateSession;
-		
 		public void Dispose()
 		{
 			this.clientHost.Dispose();
@@ -29,22 +27,7 @@ namespace BossClient
 			this.clientHost.Start(timeout);
 		}
 
-		public async void HandleMessages()
-		{
-			if (this.gateSession == null)
-			{
-				throw new BossException("gate session is null");
-			}
-
-			try
-			{
-				await this.gateSession.HandleMessages();
-			}
-			catch (BossException e)
-			{
-				Logger.Trace("session: {0}, exception: {1}", this.gateSession.ID, e.ToString());
-			}
-		}
+		public GateSession GateSession { get; private set; }
 
 		public async Task Login(
 			string hostName, ushort port, string account, string password)
@@ -65,8 +48,8 @@ namespace BossClient
 
 				// 登录gate
 				Peer peer = await this.clientHost.ConnectAsync(realmInfo.Item1, realmInfo.Item2);
-				this.gateSession = new GateSession(loginSessionId, new ENetChannel(peer));
-				await gateSession.Login(realmInfo.Item3);
+				this.GateSession = new GateSession(loginSessionId, new ENetChannel(peer));
+				await this.GateSession.Login(realmInfo.Item3);
 			}
 			catch (Exception e)
 			{
@@ -80,7 +63,7 @@ namespace BossClient
 			{
 				Message = command
 			};
-			this.gateSession.SendMessage(MessageOpcode.CMSG_BOSS_GM, cmsgBossGm);
+			this.GateSession.SendMessage(MessageOpcode.CMSG_BOSS_GM, cmsgBossGm);
 		}
 	}
 }

+ 0 - 23
CSharp/App/BossClient/GateSession.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Threading.Tasks;
 using Helper;
 using Log;
@@ -12,16 +11,10 @@ namespace BossClient
 
 		public IMessageChannel IMessageChannel { get; set; }
 
-		public readonly Dictionary<ushort, Action<byte[]>> messageHandlers = 
-			new Dictionary<ushort, Action<byte[]>>();
-
-		public bool IsStop { get; set; }
-
 		public GateSession(int id, IMessageChannel eNetChannel)
 		{
 			this.ID = id;
 			this.IMessageChannel = eNetChannel;
-			this.IsStop = false;
 		}
 
 		public void Dispose()
@@ -29,22 +22,6 @@ namespace BossClient
 			this.IMessageChannel.Dispose();
 		}
 
-		public async Task HandleMessages()
-		{
-			while (!this.IsStop)
-			{
-				var result = await this.IMessageChannel.RecvMessage();
-				ushort opcode = result.Item1;
-				byte[] message = result.Item2;
-				if (!messageHandlers.ContainsKey(opcode))
-				{
-					Logger.Debug("not found opcode: {0}", opcode);
-					continue;
-				}
-				messageHandlers[opcode](message);
-			}
-		}
-
 		public void SendMessage<T>(ushort opcode, T message, byte channelID = 0)
 		{
 			this.IMessageChannel.SendMessage(opcode, message, channelID);

+ 9 - 0
CSharp/App/BossClient/Messages.cs

@@ -26,6 +26,8 @@ namespace BossClient
 		public const ushort SMSG_AUTH_RESPONSE = 503;
 
 		public const ushort SMSG_SERVERTIME = 510;
+
+		public const ushort SMSG_BOSS_SERVERSINFO = 22000;
 	}
 
 	public static class ErrorCode
@@ -233,4 +235,11 @@ namespace BossClient
 		[DataMember(Order = 1, IsRequired = true)]
 		public string Message { get; set; }
 	}
+	
+	[DataContract]
+	public class SMSG_Boss_ServersInfo
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public List<string> Name { get; set; }
+	}
 }

+ 6 - 5
CSharp/App/Editor/Shell.xaml

@@ -3,16 +3,17 @@
 		xmlns:prism="http://www.codeplex.com/prism" Title="Editor" Height="800" Width="1280"
 		WindowStartupLocation="CenterScreen">
 	<Grid>
-		<TabControl>
+		<ContentControl Name="robot" prism:RegionManager.RegionName="RobotRegion" />
+		<!--<TabControl>
 			<TabItem Height="25" Width="80" Header="机器人">
 				<ContentControl Name="robot" prism:RegionManager.RegionName="RobotRegion" />
 			</TabItem>
 			<TabItem Height="25" Width="80" Header="行为树">
 				<ContentControl Name="treeCanvas" prism:RegionManager.RegionName="TreeCanvasRegion" />
 			</TabItem>
-            <TabItem Height="25" Width="80" Header="外挂">
-                <ContentControl Name="waigua" prism:RegionManager.RegionName="WaiGuaRegion" />
-            </TabItem>
-        </TabControl>
+			<TabItem Height="25" Width="80" Header="外挂">
+				<ContentControl Name="waigua" prism:RegionManager.RegionName="WaiGuaRegion" />
+			</TabItem>
+		</TabControl>-->
 	</Grid>
 </Window>

+ 2 - 0
CSharp/App/Modules/Robot/Packages.config

@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
   <package id="CommonServiceLocator" version="1.0" targetFramework="net45" />
+  <package id="Extended.Wpf.Toolkit" version="1.9.0" targetFramework="net45" />
   <package id="Prism" version="4.1.0.0" targetFramework="net45" />
   <package id="Prism.MEFExtensions" version="4.1.0.0" targetFramework="net45" />
+  <package id="treeviewex" version="0.1.0.1" targetFramework="net45" />
 </packages>

+ 24 - 1
CSharp/App/Modules/Robot/Robot.csproj

@@ -7,7 +7,7 @@
     <ProjectGuid>{5D6ECBCD-BE14-4DCB-BAEC-57089748B164}</ProjectGuid>
     <OutputType>Library</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>Robot</RootNamespace>
+    <RootNamespace>Modules.Robot</RootNamespace>
     <AssemblyName>Robot</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
@@ -32,6 +32,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Accessibility" />
     <Reference Include="Microsoft.Practices.Prism">
       <HintPath>..\..\..\packages\Prism.4.1.0.0\lib\NET40\Microsoft.Practices.Prism.dll</HintPath>
     </Reference>
@@ -40,12 +41,28 @@
     </Reference>
     <Reference Include="PresentationCore" />
     <Reference Include="PresentationFramework" />
+    <Reference Include="PresentationUI, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+    <Reference Include="ReachFramework" />
     <Reference Include="System" />
     <Reference Include="System.ComponentModel.Composition" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Deployment" />
+    <Reference Include="System.Printing" />
+    <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.Xaml" />
     <Reference Include="System.Xml" />
+    <Reference Include="TreeViewEx">
+      <HintPath>..\..\..\packages\treeviewex.0.1.0.1\lib\net40\TreeViewEx.dll</HintPath>
+    </Reference>
+    <Reference Include="UIAutomationProvider" />
+    <Reference Include="UIAutomationTypes" />
     <Reference Include="WindowsBase" />
+    <Reference Include="Xceed.Wpf.DataGrid">
+      <HintPath>..\..\..\packages\Extended.Wpf.Toolkit.1.9.0\lib\net40\Xceed.Wpf.DataGrid.dll</HintPath>
+    </Reference>
+    <Reference Include="Xceed.Wpf.Toolkit">
+      <HintPath>..\..\..\packages\Extended.Wpf.Toolkit.1.9.0\lib\net40\Xceed.Wpf.Toolkit.dll</HintPath>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Robot.cs" />
@@ -54,6 +71,8 @@
     <Compile Include="RobotView.xaml.cs">
       <DependentUpon>RobotView.xaml</DependentUpon>
     </Compile>
+    <Compile Include="ServerConfig.cs" />
+    <Compile Include="ServerViewModel.cs" />
   </ItemGroup>
   <ItemGroup>
     <Page Include="RobotView.xaml">
@@ -66,6 +85,10 @@
       <Project>{d0b4cfac-a368-4742-9863-68776cfa9938}</Project>
       <Name>ENet</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\..\Platform\Helper\Helper.csproj">
+      <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>

+ 22 - 13
CSharp/App/Modules/Robot/RobotView.xaml

@@ -1,13 +1,15 @@
-<UserControl x:Class="Modules.Robot.RobotView"
+<UserControl xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=TreeViewEx"  x:Class="Modules.Robot.RobotView"
 		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Robot="clr-namespace:Modules.Robot"
+		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+		xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
+		xmlns:Robot="clr-namespace:Modules.Robot"
 		mc:Ignorable="d" 
-		d:DesignHeight="600" d:DesignWidth="800" d:DataContext="{d:DesignInstance Robot:RobotViewModel}">
+		d:DesignHeight="600" d:DesignWidth="1280" d:DataContext="{d:DesignInstance Robot:RobotViewModel}">
 	<Grid>
-		<StackPanel>
-			<StackPanel Orientation="Horizontal" >
+		<DockPanel>
+			<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Height="27" >
 				<Label Content="LoginIP: "></Label>
 				<TextBox Width="120" Text="{Binding LoginIP}" />
 				<Label Content="LoginPort: "></Label>
@@ -16,14 +18,21 @@
 				<TextBox Width="120" Text="{Binding Account}" />
 				<Label Content="Password: "></Label>
 				<TextBox Width="120" Text="{Binding Password}" />
-				<Button Name="btnLogin" Content="Login" Width="80" Height="25" Click="btnLogin_Click" />
-				<TextBlock></TextBlock>
+				<Button Name="btnLogin" Content="登录" Width="80" Click="btnLogin_Click" />
+				<Button Name="btnServers" Content="服务器列表" Click="btnServers_Click" 
+						IsEnabled="{Binding IsButtonEnable}"></Button>
+				<Button Name="btnReload" Content="Reload" Click="btnReload_Click" 
+						IsEnabled="{Binding IsButtonEnable}"></Button>
 			</StackPanel>
-			<StackPanel Orientation="Horizontal">
-				<TextBox Name="tbCommand" Width="400" Text="{Binding Command}"></TextBox>
-				<Button Name="btnSendCommand" Content="发送" Click="btnSendCommand_Click" 
-						IsEnabled="{Binding IsEnableSendCommandButton}"></Button>
-			</StackPanel>
-		</StackPanel>
+			<ListBox Width="100" SelectionMode="Multiple" ItemsSource="{Binding ServerInfos}" 
+					d:DataContext="{d:DesignInstance Robot:RobotViewModel}">
+				<ListBox.ItemTemplate>
+					<DataTemplate>
+						<Label Content="{Binding Name}" ></Label>
+					</DataTemplate>
+				</ListBox.ItemTemplate>
+			</ListBox>
+			<Grid />
+		</DockPanel>
 	</Grid>
 </UserControl>

+ 7 - 2
CSharp/App/Modules/Robot/RobotView.xaml.cs

@@ -33,9 +33,14 @@ namespace Modules.Robot
 			await this.ViewModel.Login();
 		}
 
-		private void btnSendCommand_Click(object sender, RoutedEventArgs e)
+		private void btnServers_Click(object sender, RoutedEventArgs e)
 		{
-			this.ViewModel.SendCommand();
+			this.ViewModel.Servers();
+		}
+
+		private void btnReload_Click(object sender, RoutedEventArgs e)
+		{
+			this.ViewModel.Reload();
 		}
 	}
 }

+ 75 - 8
CSharp/App/Modules/Robot/RobotViewModel.cs

@@ -1,8 +1,12 @@
 using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.ComponentModel.Composition;
 using System.Threading.Tasks;
 using System.Windows.Threading;
 using BossClient;
+using Helper;
+using Log;
 using Microsoft.Practices.Prism.ViewModel;
 
 namespace Modules.Robot
@@ -16,8 +20,13 @@ namespace Modules.Robot
 		private string account = "egametang@163.com";
 		private string password = "163bio1";
 		private string command = "";
-		private bool isEnableSendCommandButton;
+		private bool isButtonEnable;
 		private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
+		private readonly ObservableCollection<ServerViewModel> serverInfos = 
+			new ObservableCollection<ServerViewModel>();
+
+		public readonly Dictionary<ushort, Action<byte[]>> messageHandlers =
+			new Dictionary<ushort, Action<byte[]>>();
 
 		private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
 		{ Interval = new TimeSpan(0, 0, 0, 0, 50) };
@@ -107,25 +116,36 @@ namespace Modules.Robot
 			}
 		}
 
-		public bool IsEnableSendCommandButton
+		public bool IsButtonEnable
 		{
 			get
 			{
-				return this.isEnableSendCommandButton;
+				return this.isButtonEnable;
 			}
 			set
 			{
-				if (this.isEnableSendCommandButton == value)
+				if (this.isButtonEnable == value)
 				{
 					return;
 				}
-				this.isEnableSendCommandButton = value;
-				this.RaisePropertyChanged("IsEnableSendCommandButton");
+				this.isButtonEnable = value;
+				this.RaisePropertyChanged("IsButtonEnable");
+			}
+		}
+
+		public ObservableCollection<ServerViewModel> ServerInfos
+		{
+			get
+			{
+				return this.serverInfos;
 			}
 		}
 
 		public RobotViewModel()
 		{
+			this.messageHandlers.Add(
+				MessageOpcode.SMSG_BOSS_SERVERSINFO, Handle_SMSG_Boss_ServersInfo);
+
 			this.timer.Tick += delegate { this.bossClient.RunOnce(); };
 			this.timer.Start();
 		}
@@ -150,13 +170,60 @@ namespace Modules.Robot
 		{
 			await this.bossClient.Login(
 				this.LoginIP, this.LoginPort, this.Account, this.Password);
-			this.IsEnableSendCommandButton = true;
-			this.bossClient.HandleMessages();
+
+			this.IsButtonEnable = true;
+
+			this.HandleMessages();
+		}
+
+		public async void HandleMessages()
+		{
+			try
+			{
+				while (true)
+				{
+					var result = await this.bossClient.GateSession.IMessageChannel.RecvMessage();
+					ushort opcode = result.Item1;
+					byte[] message = result.Item2;
+					if (!messageHandlers.ContainsKey(opcode))
+					{
+						Logger.Debug("not found opcode: {0}", opcode);
+						continue;
+					}
+					messageHandlers[opcode](message);
+				}
+			}
+			catch (Exception e)
+			{
+				this.IsButtonEnable = false;
+				Logger.Trace(e.ToString());
+			}
 		}
 
 		public void SendCommand()
 		{
 			this.bossClient.SendCommand(this.Command);
 		}
+
+		public void Servers()
+		{
+			this.bossClient.SendCommand("servers");
+		}
+
+		public void Reload()
+		{
+			this.bossClient.SendCommand("reload");
+		}
+
+		public void Handle_SMSG_Boss_ServersInfo(byte[] message)
+		{
+			var smsgBossServersInfo = ProtobufHelper.FromBytes<SMSG_Boss_ServersInfo>(message);
+
+			this.ServerInfos.Clear();
+			foreach (var name in smsgBossServersInfo.Name)
+			{
+				this.ServerInfos.Add(new ServerViewModel {Name = name});
+			}
+		}
 	}
 }

+ 120 - 0
CSharp/App/Modules/Robot/ServerConfig.cs

@@ -0,0 +1,120 @@
+using System.Collections.Generic;
+using System.Runtime.Serialization;
+
+namespace Modules.Robot
+{
+	[DataContract]
+	public class ServerConfig
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public string Host { get; set; }
+
+		[DataMember(Order = 2, IsRequired = true)]
+		public string Name { get; set; }
+
+		[DataMember(Order = 3, IsRequired = true)]
+		public uint Port { get; set; }
+	}
+
+	[DataContract]
+	public class CityServerConfig
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public string CityServerName { get; set; }
+
+		[DataMember(Order = 2, IsRequired = true)]
+		public ServerConfig ServerConfig { get; set; }
+
+		[DataMember(Order = 3, IsRequired = true)]
+		public int FrameMilliseconds { get; set; }
+
+		[DataMember(Order = 4, IsRequired = true)]
+		public int Icon { get; set; }
+
+		[DataMember(Order = 5, IsRequired = true)]
+		public int Color { get; set; }
+
+		[DataMember(Order = 6, IsRequired = true)]
+		public int TimeZone { get; set; }
+
+		[DataMember(Order = 7, IsRequired = true)]
+		public int AllowedSecurityLevel { get; set; }
+
+		[DataMember(Order = 8, IsRequired = true)]
+		public double Population { get; set; }
+
+		[DataMember(Order = 9, IsRequired = true)]
+		public string RealmBuilds { get; set; }
+
+		[DataMember(Order = 10, IsRequired = true)]
+		public string CityDisplayName { get; set; }
+
+		[DataMember(Order = 11, IsRequired = true)]
+		public string MapConfigFilePath { get; set; }
+
+		[DataMember(Order = 13, IsRequired = true)]
+		public string FriendConfigFilePath { get; set; }
+
+		[DataMember(Order = 14, IsRequired = true)]
+		public string GmCommandPermitFile { get; set; }
+
+		[DataMember(Order = 15, IsRequired = true)]
+		public string AchievementProtoPath { get; set; }
+
+		[DataMember(Order = 16, IsRequired = true)]
+		public string CityTaskPath { get; set; }
+
+		[DataMember(Order = 17, IsRequired = true)]
+		public int MaxPlayers { get; set; }
+
+		[DataMember(Order = 18, IsRequired = true)]
+		public int DenyRoomInviteCooldownSeconds { get; set; }
+
+		[DataMember(Order = 19, IsRequired = true)]
+		public string CityNpcPath { get; set; }
+
+		[DataMember(Order = 20, IsRequired = true)]
+		public string PythonDataPath { get; set; }
+
+		[DataMember(Order = 21, IsRequired = true)]
+		public string CityDataPath { get; set; }
+	}
+
+	[DataContract]
+	public class MapServerConfig 
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public string MapServerName { get; set; }
+
+		[DataMember(Order = 2, IsRequired = true)]
+		public ServerConfig ServerConfig { get; set; }
+
+		[DataMember(Order = 3, IsRequired = true)]
+		public int FrameMilliseconds { get; set; }
+
+		[DataMember(Order = 4, IsRequired = true)]
+		public string MapConfigFilePath { get; set; }
+
+		[DataMember(Order = 5, IsRequired = true)]
+		public string CityTaskPath { get; set; }
+
+		[DataMember(Order = 7, IsRequired = true)]
+		public string MapDataPath { get; set; }
+
+		[DataMember(Order = 6, IsRequired = true)]
+		public string PythonDataPath { get; set; }
+
+		[DataMember(Order = 8, IsRequired = true)]
+		public string AchievementProtoPath { get; set; }
+	}
+
+	[DataContract]
+	public class WorldServiceConfig
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		public List<CityServerConfig> CityServerConfig { get; set; }
+
+		[DataMember(Order = 2, IsRequired = true)]
+		public List<MapServerConfig> MapServerConfig { get; set; }
+	}
+}

+ 35 - 0
CSharp/App/Modules/Robot/ServerViewModel.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.Practices.Prism.ViewModel;
+
+namespace Modules.Robot
+{
+	[DataContract]
+	public class ServerViewModel : NotificationObject
+	{
+		[DataMember(Order = 1, IsRequired = true)]
+		private string name;
+
+		public string Name
+		{
+			get
+			{
+				return this.name;
+			}
+			set
+			{
+				if (this.name == value)
+				{
+					return;
+				}
+				this.name = value;
+				this.RaisePropertyChanged("Name");
+			}
+		}
+	}
+}