FormClient.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using CommonAIClient.Client;
  9. using RoomService.Net.BsServer;
  10. using RoomService.Net.BsClient;
  11. using CommonLang.Protocol;
  12. using CommonAI.ZoneClient;
  13. using CommonLang;
  14. using CommonAI.RTS; using CommonLang.Vector;
  15. using CommonAI.Zone;
  16. using CommonAI.Zone.ZoneEditor;
  17. using CommonLang.Log;
  18. using GameEditorPlugin.Win32.BattleClient;
  19. using CommonFroms.Net;
  20. using CommonNetwork.Sockets;
  21. using CommonNetwork.Net;
  22. using System.IO;
  23. using CommonLang.IO;
  24. using CommonLang.ByteOrder;
  25. using CommonLang.Property;
  26. using CommonAI.ZoneServer;
  27. using GameEditorPlugin.Win32.Runtime;
  28. namespace CommonAIServer.Connector
  29. {
  30. public class FormClient : FormAbstractClient
  31. {
  32. // -----------------------------------------------------------------------------------------------------------------
  33. private BattleClient mClient;
  34. private EventHandler mAppExit;
  35. private FormNetSession mSessionView;
  36. private int mClientSyncRange;
  37. private EditorTemplates mDataRoot;
  38. protected readonly Pen RangePen = new Pen(Color.FromArgb(128, 255, 255, 255));
  39. public void Init(
  40. string gameDataRoot,
  41. string playerUUID,
  42. string roomID,
  43. string netDirver,
  44. string connectString,
  45. int intervalMS,
  46. int syncRange,
  47. CreateUnitInfoR2B unit,
  48. bool isProxy = false,
  49. string proxyConnectString = null)
  50. {
  51. Console.WriteLine(" GameDataRoot = " + gameDataRoot);
  52. Console.WriteLine(" PlayerUUID = " + playerUUID);
  53. Console.WriteLine(" RoomID = " + roomID);
  54. Console.WriteLine(" NetDirver = " + netDirver);
  55. Console.WriteLine(" ConnectString = " + connectString);
  56. Console.WriteLine(" IntervalMS = " + intervalMS);
  57. Console.WriteLine(" SyncRange = " + syncRange);
  58. Console.WriteLine(" UnitTemplateID = " + unit.UnitTemplateID);
  59. Console.WriteLine(" Force = " + unit.Force);
  60. Console.WriteLine(" IsProxy = " + isProxy);
  61. MessageFactoryGenerator factory = TemplateManager.MessageCodec;
  62. if (FormLauncher.Templates != null)
  63. {
  64. this.mDataRoot = FormLauncher.Templates;
  65. }
  66. else
  67. {
  68. this.mDataRoot = new EditorTemplates(gameDataRoot, factory);
  69. this.mDataRoot.LoadAllTemplates();
  70. }
  71. if (unit.UnitPropData == null)
  72. {
  73. UnitInfo temp = mDataRoot.Templates.getUnit(unit.UnitTemplateID);
  74. unit.UnitPropData = IOUtil.Clone(factory, temp.Properties);
  75. }
  76. PlayerWillConnectResponseB2R room = new PlayerWillConnectResponseB2R();
  77. room.PlayerUUID = playerUUID;
  78. room.Room = new RoomInfo();
  79. room.Room.ClientConnectString = connectString;
  80. room.Room.NetDriverString = netDirver;
  81. room.Room.Dummy = 0;
  82. room.Room.RoomID = roomID;
  83. PlayerWillConnectRequestR2B enter = new PlayerWillConnectRequestR2B();
  84. {
  85. enter.PlayerUUID = playerUUID;
  86. enter.PlayerDisplayName = playerUUID;
  87. enter.Token = playerUUID;
  88. enter.TokenValidTimeSec = int.MaxValue;
  89. enter.RoomID = roomID;
  90. enter.Data = unit; //IOUtil.ObjectToBin(factory, unit);
  91. }
  92. if (isProxy)
  93. {
  94. this.mClient = new BattleClientProxy(
  95. new TestProxySession(),
  96. proxyConnectString,
  97. mDataRoot, factory, room, enter, playerUUID);
  98. }
  99. else
  100. {
  101. this.mClient = new BattleClientDirect(
  102. mDataRoot, factory, room, enter, playerUUID);
  103. }
  104. this.mClientSyncRange = syncRange;
  105. this.mSessionView = new FormNetSession(mClient.Session);
  106. this.mSessionView.ShowInTaskbar = false;
  107. this.mSessionView.FormClosing += (object sender, FormClosingEventArgs e)=>
  108. {
  109. if (this.Visible)
  110. {
  111. e.Cancel = true;
  112. mSessionView.Hide();
  113. }
  114. };
  115. this.FormClosed += FormClient_FormClosed;
  116. base.BattlePanel.RenderFPS = DataRoot.Templates.CFG.SYSTEM_FPS;
  117. base.BattlePanel.btn_Exit.Visible = true;
  118. base.BattlePanel.OnExitClicked += BattlePanel_OnExitClicked;
  119. }
  120. private void BattlePanel_OnExitClicked()
  121. {
  122. this.mClient.SendLeaveRoom();
  123. this.Close();
  124. }
  125. private void FormClient_FormClosed(object sender, FormClosedEventArgs e)
  126. {
  127. mSessionView.Close();
  128. mClient.Stop();
  129. }
  130. private void Application_ApplicationExit(object sender, EventArgs e)
  131. {
  132. mClient.Stop();
  133. }
  134. //--------------------------------------------------------------------------------------------
  135. public override EditorTemplates DataRoot
  136. {
  137. get { return mDataRoot; }
  138. }
  139. public override AbstractBattle GenBattle()
  140. {
  141. return mClient;
  142. }
  143. public override DisplayLayerWorld GenDisplay(PictureBox control)
  144. {
  145. return new DisplayLayerWorld();
  146. }
  147. protected override void OnLoaded()
  148. {
  149. this.mClient.Start();
  150. this.mAppExit = new EventHandler(Application_ApplicationExit);
  151. Application.ApplicationExit += mAppExit;
  152. }
  153. protected override void OnClose()
  154. {
  155. Application.ApplicationExit -= mAppExit;
  156. this.mSessionView.Dispose();
  157. mClient.Stop();
  158. }
  159. protected override void OnUpdate(int intervalMS)
  160. {
  161. string conn = mClient.Session.IsConnected ? "已连接" : "未连接";
  162. this.Text = mClient.PlayerUUID + " - [" + conn + "]";
  163. }
  164. protected override void DisplayActor_OnRender(Graphics g, DisplayGameUnit unit)
  165. {
  166. RangePen.Width = 1f / BattlePanel.DisplayWorld.getCameraScale();
  167. g.DrawArc(RangePen,
  168. -mClientSyncRange,
  169. -mClientSyncRange,
  170. mClientSyncRange << 1,
  171. mClientSyncRange << 1,
  172. 0, 360);
  173. }
  174. protected override void OnNetworkViewClicked()
  175. {
  176. mSessionView.Show();
  177. }
  178. //--------------------------------------------------------------------------------------------
  179. class TestProxySession : BattleClientProxy.ProxyNetSession
  180. {
  181. public override bool IsMessageTypeGS(object msg)
  182. {
  183. return false;
  184. }
  185. }
  186. }
  187. }